Merge pull request #6927 from alimate/BAEL-2916
Bael 2916: Ordering Extension Registrations
This commit is contained in:
commit
84f0795f88
11
pom.xml
11
pom.xml
@ -124,11 +124,6 @@
|
|||||||
</excludes>
|
</excludes>
|
||||||
</configuration>
|
</configuration>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
|
||||||
<groupId>org.junit.platform</groupId>
|
|
||||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
|
||||||
<version>${junit-platform.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
@ -1527,7 +1522,7 @@
|
|||||||
<logback.version>1.1.7</logback.version>
|
<logback.version>1.1.7</logback.version>
|
||||||
|
|
||||||
<!-- plugins -->
|
<!-- 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>
|
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||||
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
|
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
@ -1549,8 +1544,8 @@
|
|||||||
<jstl.version>1.2</jstl.version>
|
<jstl.version>1.2</jstl.version>
|
||||||
<jackson.version>2.9.8</jackson.version>
|
<jackson.version>2.9.8</jackson.version>
|
||||||
<commons-fileupload.version>1.3</commons-fileupload.version>
|
<commons-fileupload.version>1.3</commons-fileupload.version>
|
||||||
<junit-platform.version>1.2.0</junit-platform.version>
|
<junit-platform.version>1.4.2</junit-platform.version>
|
||||||
<junit-jupiter.version>5.2.0</junit-jupiter.version>
|
<junit-jupiter.version>5.4.2</junit-jupiter.version>
|
||||||
<directory-maven-plugin.version>0.3.1</directory-maven-plugin.version>
|
<directory-maven-plugin.version>0.3.1</directory-maven-plugin.version>
|
||||||
<maven-install-plugin.version>2.5.1</maven-install-plugin.version>
|
<maven-install-plugin.version>2.5.1</maven-install-plugin.version>
|
||||||
<custom-pmd.version>0.0.1</custom-pmd.version>
|
<custom-pmd.version>0.0.1</custom-pmd.version>
|
||||||
|
@ -139,7 +139,7 @@
|
|||||||
<junit.vintage.version>5.4.2</junit.vintage.version>
|
<junit.vintage.version>5.4.2</junit.vintage.version>
|
||||||
<log4j2.version>2.8.2</log4j2.version>
|
<log4j2.version>2.8.2</log4j2.version>
|
||||||
<powermock.version>2.0.0-RC.1</powermock.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>
|
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
|
||||||
<spring.version>5.0.1.RELEASE</spring.version>
|
<spring.version>5.0.1.RELEASE</spring.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
@ -15,10 +15,20 @@ import com.baeldung.helpers.JdbcConnectionUtil;
|
|||||||
|
|
||||||
public class EmployeeDatabaseSetupExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
|
public class EmployeeDatabaseSetupExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
|
||||||
|
|
||||||
private Connection con = JdbcConnectionUtil.getConnection();
|
private Connection con;
|
||||||
private EmployeeJdbcDao employeeDao = new EmployeeJdbcDao(con);
|
private EmployeeJdbcDao employeeDao;
|
||||||
private Savepoint savepoint;
|
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
|
@Override
|
||||||
public void afterAll(ExtensionContext context) throws SQLException {
|
public void afterAll(ExtensionContext context) throws SQLException {
|
||||||
if (con != null) {
|
if (con != null) {
|
||||||
|
@ -19,7 +19,7 @@ public class EmployeeJdbcDao {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void createTable() throws SQLException {
|
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);
|
PreparedStatement pstmt = con.prepareStatement(createQuery);
|
||||||
|
|
||||||
pstmt.execute();
|
pstmt.execute();
|
||||||
|
@ -11,22 +11,49 @@ public class JdbcConnectionUtil {
|
|||||||
private static Connection con;
|
private static Connection con;
|
||||||
|
|
||||||
public static Connection getConnection() {
|
public static Connection getConnection() {
|
||||||
if (con == null) {
|
if (con == null || isClosed(con)) {
|
||||||
try {
|
try {
|
||||||
Properties props = new Properties();
|
Properties props = new Properties();
|
||||||
props.load(JdbcConnectionUtil.class.getResourceAsStream("jdbc.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;
|
return con;
|
||||||
} catch (IOException exc) {
|
} catch (IOException exc) {
|
||||||
exc.printStackTrace();
|
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) {
|
} catch (ClassNotFoundException exc) {
|
||||||
exc.printStackTrace();
|
exc.printStackTrace();
|
||||||
} catch (SQLException exc) {
|
} catch (SQLException exc) {
|
||||||
exc.printStackTrace();
|
exc.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return con;
|
return con;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isClosed(Connection con) {
|
||||||
|
try {
|
||||||
|
return con.isClosed();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ public class AssertionUnitTest {
|
|||||||
"heading",
|
"heading",
|
||||||
() -> assertEquals(4, 2 * 2, "4 is 2 times 2"),
|
() -> assertEquals(4, 2 * 2, "4 is 2 times 2"),
|
||||||
() -> assertEquals("java", "JAVA".toLowerCase()),
|
() -> assertEquals("java", "JAVA".toLowerCase()),
|
||||||
() -> assertEquals(null, null, "null is equal to null")
|
() -> assertEquals((String) null, (String) null, "null is equal to null")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user