Merge pull request #6927 from alimate/BAEL-2916

Bael 2916: Ordering Extension Registrations
This commit is contained in:
Eric Martin 2019-05-18 15:00:54 -05:00 committed by GitHub
commit 84f0795f88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 116 additions and 16 deletions

11
pom.xml
View File

@ -124,11 +124,6 @@
</excludes>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit-platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
@ -1527,7 +1522,7 @@
<logback.version>1.1.7</logback.version>
<!-- plugins -->
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
<maven-surefire-plugin.version>2.22.0</maven-surefire-plugin.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<java.version>1.8</java.version>
@ -1549,8 +1544,8 @@
<jstl.version>1.2</jstl.version>
<jackson.version>2.9.8</jackson.version>
<commons-fileupload.version>1.3</commons-fileupload.version>
<junit-platform.version>1.2.0</junit-platform.version>
<junit-jupiter.version>5.2.0</junit-jupiter.version>
<junit-platform.version>1.4.2</junit-platform.version>
<junit-jupiter.version>5.4.2</junit-jupiter.version>
<directory-maven-plugin.version>0.3.1</directory-maven-plugin.version>
<maven-install-plugin.version>2.5.1</maven-install-plugin.version>
<custom-pmd.version>0.0.1</custom-pmd.version>

View File

@ -139,7 +139,7 @@
<junit.vintage.version>5.4.2</junit.vintage.version>
<log4j2.version>2.8.2</log4j2.version>
<powermock.version>2.0.0-RC.1</powermock.version>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
<maven-surefire-plugin.version>2.22.0</maven-surefire-plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<spring.version>5.0.1.RELEASE</spring.version>
</properties>

View File

@ -0,0 +1,28 @@
package com.baeldung;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.extension.RegisterExtension;
import com.baeldung.extensions.EmployeeDatabaseSetupExtension;
public class MultipleExtensionsUnitTest {
@Order(1)
@RegisterExtension
static EmployeeDatabaseSetupExtension SECOND_DB =
new EmployeeDatabaseSetupExtension("jdbc:h2:mem:DbTwo;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", "");
@Order(0)
@RegisterExtension
static EmployeeDatabaseSetupExtension FIRST_DB =
new EmployeeDatabaseSetupExtension("jdbc:h2:mem:DbOne;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", "");
@RegisterExtension
static EmployeeDatabaseSetupExtension LAST_DB =
new EmployeeDatabaseSetupExtension("jdbc:h2:mem:DbLast;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", "");
@Test
public void justDemonstratingTheIdea() {
// empty test
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung;
import java.sql.SQLException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import com.baeldung.helpers.Employee;
import com.baeldung.extensions.EmployeeDaoParameterResolver;
import com.baeldung.extensions.EmployeeDatabaseSetupExtension;
import com.baeldung.extensions.EnvironmentExtension;
import com.baeldung.helpers.EmployeeJdbcDao;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith({ EnvironmentExtension.class, EmployeeDaoParameterResolver.class })
public class ProgrammaticEmployeesUnitTest {
private EmployeeJdbcDao employeeDao;
@RegisterExtension static EmployeeDatabaseSetupExtension DB =
new EmployeeDatabaseSetupExtension("jdbc:h2:mem:AnotherDb;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", "");
public ProgrammaticEmployeesUnitTest(EmployeeJdbcDao employeeDao) {
this.employeeDao = employeeDao;
}
@Test
public void whenAddEmployee_thenGetEmployee() throws SQLException {
Employee emp = new Employee(1, "john");
employeeDao.add(emp);
assertEquals(1, employeeDao.findAll().size());
}
@Test
public void whenGetEmployees_thenEmptyList() throws SQLException {
assertEquals(0, employeeDao.findAll().size());
}
}

View File

@ -15,10 +15,20 @@ import com.baeldung.helpers.JdbcConnectionUtil;
public class EmployeeDatabaseSetupExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
private Connection con = JdbcConnectionUtil.getConnection();
private EmployeeJdbcDao employeeDao = new EmployeeJdbcDao(con);
private Connection con;
private EmployeeJdbcDao employeeDao;
private Savepoint savepoint;
public EmployeeDatabaseSetupExtension() {
con = JdbcConnectionUtil.getConnection();
employeeDao = new EmployeeJdbcDao(con);
}
public EmployeeDatabaseSetupExtension(String jdbcUrl, String driver, String username, String password) {
con = JdbcConnectionUtil.getConnection(jdbcUrl, driver, username, password);
employeeDao = new EmployeeJdbcDao(con);
}
@Override
public void afterAll(ExtensionContext context) throws SQLException {
if (con != null) {

View File

@ -19,7 +19,7 @@ public class EmployeeJdbcDao {
}
public void createTable() throws SQLException {
String createQuery = "CREATE TABLE employees(id long primary key, firstName varchar(50))";
String createQuery = "CREATE TABLE IF NOT EXISTS employees(id long primary key, firstName varchar(50))";
PreparedStatement pstmt = con.prepareStatement(createQuery);
pstmt.execute();

View File

@ -11,22 +11,49 @@ public class JdbcConnectionUtil {
private static Connection con;
public static Connection getConnection() {
if (con == null) {
if (con == null || isClosed(con)) {
try {
Properties props = new Properties();
props.load(JdbcConnectionUtil.class.getResourceAsStream("jdbc.properties"));
Class.forName(props.getProperty("jdbc.driver"));
con = DriverManager.getConnection(props.getProperty("jdbc.url"), props.getProperty("jdbc.user"), props.getProperty("jdbc.password"));
String jdbcUrl = props.getProperty("jdbc.url");
String driver = props.getProperty("jdbc.driver");
String username = props.getProperty("jdbc.user");
String password = props.getProperty("jdbc.password");
con = getConnection(jdbcUrl, driver, username, password);
return con;
} catch (IOException exc) {
exc.printStackTrace();
}
return null;
}
return con;
}
public static Connection getConnection(String jdbcUrl, String driver, String username, String password) {
if (con == null || isClosed(con)) {
try {
Class.forName(driver);
con = DriverManager.getConnection(jdbcUrl, username, password);
return con;
} catch (ClassNotFoundException exc) {
exc.printStackTrace();
} catch (SQLException exc) {
exc.printStackTrace();
}
return null;
}
return con;
}
private static boolean isClosed(Connection con) {
try {
return con.isClosed();
} catch (SQLException e) {
return true;
}
}
}

View File

@ -34,7 +34,7 @@ public class AssertionUnitTest {
"heading",
() -> assertEquals(4, 2 * 2, "4 is 2 times 2"),
() -> assertEquals("java", "JAVA".toLowerCase()),
() -> assertEquals(null, null, "null is equal to null")
() -> assertEquals((String) null, (String) null, "null is equal to null")
);
}
}