From b6d47b00a35e9dfec74ee5e89ec34085616512b1 Mon Sep 17 00:00:00 2001 From: lor6 Date: Mon, 12 Jun 2017 11:20:42 +0300 Subject: [PATCH] junit 5 example (#2050) --- guest/junit5-example/.gitignore | 4 + guest/junit5-example/pom.xml | 68 ++++++++ .../main/java/com/stackify/daos/UserDAO.java | 141 ++++++++++++++++ .../main/java/com/stackify/models/User.java | 63 ++++++++ .../com/stackify/utils/ConnectionUtil.java | 38 +++++ .../com/stackify/utils/jdbc.properties | 4 + .../src/main/resources/log4j2.xml | 13 ++ .../stackify/test/DatabaseConnectionTest.java | 19 +++ .../java/com/stackify/test/DynamicTests.java | 38 +++++ .../java/com/stackify/test/IncrementTest.java | 25 +++ .../java/com/stackify/test/TaggedTest.java | 16 ++ .../java/com/stackify/test/UsersTest.java | 153 ++++++++++++++++++ 12 files changed, 582 insertions(+) create mode 100644 guest/junit5-example/.gitignore create mode 100644 guest/junit5-example/pom.xml create mode 100644 guest/junit5-example/src/main/java/com/stackify/daos/UserDAO.java create mode 100644 guest/junit5-example/src/main/java/com/stackify/models/User.java create mode 100644 guest/junit5-example/src/main/java/com/stackify/utils/ConnectionUtil.java create mode 100644 guest/junit5-example/src/main/resources/com/stackify/utils/jdbc.properties create mode 100644 guest/junit5-example/src/main/resources/log4j2.xml create mode 100644 guest/junit5-example/src/test/java/com/stackify/test/DatabaseConnectionTest.java create mode 100644 guest/junit5-example/src/test/java/com/stackify/test/DynamicTests.java create mode 100644 guest/junit5-example/src/test/java/com/stackify/test/IncrementTest.java create mode 100644 guest/junit5-example/src/test/java/com/stackify/test/TaggedTest.java create mode 100644 guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java diff --git a/guest/junit5-example/.gitignore b/guest/junit5-example/.gitignore new file mode 100644 index 0000000000..60be5b80aa --- /dev/null +++ b/guest/junit5-example/.gitignore @@ -0,0 +1,4 @@ +/target/ +.settings/ +.classpath +.project diff --git a/guest/junit5-example/pom.xml b/guest/junit5-example/pom.xml new file mode 100644 index 0000000000..aec7f9228a --- /dev/null +++ b/guest/junit5-example/pom.xml @@ -0,0 +1,68 @@ + + 4.0.0 + junit5-example + junit5-example + 0.0.1-SNAPSHOT + + + + org.junit.jupiter + junit-jupiter-engine + 5.0.0-M4 + + + org.junit.jupiter + junit-jupiter-params + 5.0.0-M4 + + + + org.junit.vintage + junit-vintage-engine + 4.12.0-M4 + + + + com.h2database + h2 + 1.4.195 + + + + org.apache.logging.log4j + log4j-core + 2.8.2 + + + + + + + + maven-compiler-plugin + 3.5 + + 1.8 + 1.8 + + + + maven-surefire-plugin + 2.19.1 + + + org.junit.platform + junit-platform-surefire-provider + 1.0.0-M4 + + + + + math + + + + + + \ No newline at end of file diff --git a/guest/junit5-example/src/main/java/com/stackify/daos/UserDAO.java b/guest/junit5-example/src/main/java/com/stackify/daos/UserDAO.java new file mode 100644 index 0000000000..091d077ef6 --- /dev/null +++ b/guest/junit5-example/src/main/java/com/stackify/daos/UserDAO.java @@ -0,0 +1,141 @@ +package com.stackify.daos; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import com.stackify.models.User; +import com.stackify.utils.ConnectionUtil; + +public class UserDAO { + + private Logger logger = LogManager.getLogger(UserDAO.class); + + public void createTable() { + try (Connection con = ConnectionUtil.getConnection()) { + String createQuery = "CREATE TABLE users(email varchar(50) primary key, name varchar(50))"; + PreparedStatement pstmt = con.prepareStatement(createQuery); + + pstmt.execute(); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + + } + + public void add(User user) { + try (Connection con = ConnectionUtil.getConnection()) { + + String insertQuery = "INSERT INTO users(email,name) VALUES(?,?)"; + PreparedStatement pstmt = con.prepareStatement(insertQuery); + pstmt.setString(1, user.getEmail()); + pstmt.setString(2, user.getName()); + + pstmt.executeUpdate(); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + } + + public void update(User user) { + try (Connection con = ConnectionUtil.getConnection()) { + + String updateQuery = "UPDATE users SET name=? WHERE email=?"; + PreparedStatement pstmt = con.prepareStatement(updateQuery); + pstmt.setString(1, user.getName()); + pstmt.setString(2, user.getEmail()); + + pstmt.executeUpdate(); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + } + + public void delete(User user) { + try (Connection con = ConnectionUtil.getConnection()) { + + String deleteQuery = "DELETE FROM users WHERE email=?"; + PreparedStatement pstmt = con.prepareStatement(deleteQuery); + pstmt.setString(1, user.getEmail()); + + pstmt.executeUpdate(); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + } + + public void delete(String email) { + try (Connection con = ConnectionUtil.getConnection()) { + + String deleteQuery = "DELETE FROM users WHERE email=?"; + PreparedStatement pstmt = con.prepareStatement(deleteQuery); + pstmt.setString(1, email); + + pstmt.executeUpdate(); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + } + + public User findOne(String email) { + User user = null; + + try (Connection con = ConnectionUtil.getConnection()) { + String query = "SELECT * FROM users WHERE email=?"; + PreparedStatement pstmt = con.prepareStatement(query); + pstmt.setString(1, email); + + ResultSet rs = pstmt.executeQuery(); + while (rs.next()) { + user = new User(); + user.setEmail(rs.getString("email")); + user.setName(rs.getString("name")); + } + + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + + return user; + } + + public List findAll() { + List users = new ArrayList<>(); + + try (Connection con = ConnectionUtil.getConnection()) { + String query = "SELECT * FROM users"; + PreparedStatement pstmt = con.prepareStatement(query); + + ResultSet rs = pstmt.executeQuery(); + while (rs.next()) { + User user = new User(); + user.setEmail(rs.getString("email")); + user.setName(rs.getString("name")); + users.add(user); + } + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + + return users; + } + + public void deleteAll() { + try (Connection con = ConnectionUtil.getConnection()) { + + String deleteQuery = "DELETE FROM users"; + PreparedStatement pstmt = con.prepareStatement(deleteQuery); + + pstmt.executeUpdate(); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + } + +} diff --git a/guest/junit5-example/src/main/java/com/stackify/models/User.java b/guest/junit5-example/src/main/java/com/stackify/models/User.java new file mode 100644 index 0000000000..d6951102a7 --- /dev/null +++ b/guest/junit5-example/src/main/java/com/stackify/models/User.java @@ -0,0 +1,63 @@ +package com.stackify.models; + +public class User { + private String email; + private String name; + + public User() { + } + + public User(String email, String name) { + super(); + this.email = email; + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((email == null) ? 0 : email.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + User other = (User) obj; + if (email == null) { + if (other.email != null) + return false; + } else if (!email.equals(other.email)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + +} diff --git a/guest/junit5-example/src/main/java/com/stackify/utils/ConnectionUtil.java b/guest/junit5-example/src/main/java/com/stackify/utils/ConnectionUtil.java new file mode 100644 index 0000000000..bcfe5058a7 --- /dev/null +++ b/guest/junit5-example/src/main/java/com/stackify/utils/ConnectionUtil.java @@ -0,0 +1,38 @@ +package com.stackify.utils; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class ConnectionUtil { + + private static Logger logger = LogManager.getLogger(ConnectionUtil.class); + + public static Connection getConnection() { + try { + Properties props = new Properties(); + props.load(ConnectionUtil.class.getResourceAsStream("jdbc.properties")); + Class.forName(props.getProperty("jdbc.driver")); + Connection con = DriverManager.getConnection(props.getProperty("jdbc.url"), props.getProperty("jdbc.user"), props.getProperty("jdbc.password")); + return con; + } + + catch (FileNotFoundException exc) { + logger.error(exc.getMessage()); + } catch (IOException exc) { + logger.error(exc.getMessage()); + } catch (ClassNotFoundException exc) { + logger.error(exc.getMessage()); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + return null; + } + +} diff --git a/guest/junit5-example/src/main/resources/com/stackify/utils/jdbc.properties b/guest/junit5-example/src/main/resources/com/stackify/utils/jdbc.properties new file mode 100644 index 0000000000..2d9a39b157 --- /dev/null +++ b/guest/junit5-example/src/main/resources/com/stackify/utils/jdbc.properties @@ -0,0 +1,4 @@ +jdbc.driver=org.h2.Driver +jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1 +jdbc.user= +jdbc.password= \ No newline at end of file diff --git a/guest/junit5-example/src/main/resources/log4j2.xml b/guest/junit5-example/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..602b5ab490 --- /dev/null +++ b/guest/junit5-example/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/guest/junit5-example/src/test/java/com/stackify/test/DatabaseConnectionTest.java b/guest/junit5-example/src/test/java/com/stackify/test/DatabaseConnectionTest.java new file mode 100644 index 0000000000..d8f13b5715 --- /dev/null +++ b/guest/junit5-example/src/test/java/com/stackify/test/DatabaseConnectionTest.java @@ -0,0 +1,19 @@ +package com.stackify.test; + +import java.sql.Connection; + +import org.junit.jupiter.api.Test; + +import com.stackify.utils.ConnectionUtil; + +import static org.junit.jupiter.api.Assertions.*; + +public interface DatabaseConnectionTest { + + @Test + default void testDatabaseConnection() { + Connection con = ConnectionUtil.getConnection(); + assertNotNull(con); + } + +} diff --git a/guest/junit5-example/src/test/java/com/stackify/test/DynamicTests.java b/guest/junit5-example/src/test/java/com/stackify/test/DynamicTests.java new file mode 100644 index 0000000000..39b3b5aac5 --- /dev/null +++ b/guest/junit5-example/src/test/java/com/stackify/test/DynamicTests.java @@ -0,0 +1,38 @@ +package com.stackify.test; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.function.Function; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; +import org.junit.jupiter.api.function.ThrowingConsumer; + +import com.stackify.daos.UserDAO; +import com.stackify.models.User; + +public class DynamicTests { + + @TestFactory + public Collection dynamicTestCollection() { + return Arrays.asList(DynamicTest.dynamicTest("Dynamic Test", () -> assertTrue(1 == 1))); + } + + @TestFactory + public Stream dynamicUserTestCollection() { + List inputList = Arrays.asList(new User("john@yahoo.com", "John"), new User("ana@yahoo.com", "Ana")); + + Function displayNameGenerator = (input) -> "Saving user: " + input; + + UserDAO userDAO = new UserDAO(); + ThrowingConsumer testExecutor = (input) -> { + userDAO.add(input); + assertNotNull(userDAO.findOne(input.getEmail())); + }; + + return DynamicTest.stream(inputList.iterator(), displayNameGenerator, testExecutor); + } +} diff --git a/guest/junit5-example/src/test/java/com/stackify/test/IncrementTest.java b/guest/junit5-example/src/test/java/com/stackify/test/IncrementTest.java new file mode 100644 index 0000000000..a23a5bf20b --- /dev/null +++ b/guest/junit5-example/src/test/java/com/stackify/test/IncrementTest.java @@ -0,0 +1,25 @@ +package com.stackify.test; + +import static org.junit.jupiter.api.Assertions.*; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.RepeatedTest; +import org.junit.jupiter.api.RepetitionInfo; + +public class IncrementTest { + + private static Logger logger = LogManager.getLogger(IncrementTest.class); + + @BeforeEach + public void increment() { + logger.info("Before Each Test"); + } + + @RepeatedTest(value = 3, name = RepeatedTest.SHORT_DISPLAY_NAME) + public void test(RepetitionInfo info) { + assertTrue(1 == 1); + logger.info("Repetition #" + info.getCurrentRepetition()); + } +} diff --git a/guest/junit5-example/src/test/java/com/stackify/test/TaggedTest.java b/guest/junit5-example/src/test/java/com/stackify/test/TaggedTest.java new file mode 100644 index 0000000000..db3c38ceac --- /dev/null +++ b/guest/junit5-example/src/test/java/com/stackify/test/TaggedTest.java @@ -0,0 +1,16 @@ +package com.stackify.test; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +@Tag("math") +public class TaggedTest { + + @Test + @Tag("arithmetic") + public void testEquals() { + assertTrue(1 == 1); + } + +} diff --git a/guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java b/guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java new file mode 100644 index 0000000000..fbbd56287d --- /dev/null +++ b/guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java @@ -0,0 +1,153 @@ +package com.stackify.test; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assumptions.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import com.stackify.daos.UserDAO; +import com.stackify.models.User; + +public class UsersTest implements DatabaseConnectionTest { + + private static UserDAO userDAO; + + private static Logger logger = LogManager.getLogger(UsersTest.class); + + @BeforeAll + public static void addData() { + userDAO = new UserDAO(); + userDAO.createTable(); + + User user1 = new User("john@gmail.com", "John"); + User user2 = new User("ana@gmail.com", "Ana"); + userDAO.add(user1); + userDAO.add(user2); + } + + @Test + @DisplayName("Test Get Users") + public void testGetUsersNumber() { + assertEquals(2, userDAO.findAll().size()); + + } + + @Test + @DisplayName("Test Get Users") + public void testGetUsersNumberWithInfo(TestInfo testInfo) { + assertEquals(2, userDAO.findAll().size()); + assertEquals("Test Get Users", testInfo.getDisplayName()); + assertEquals(UsersTest.class, testInfo.getTestClass().get()); + logger.info("Running test method:" + testInfo.getTestMethod().get().getName()); + } + + @Test + public void testGetUser() { + User user = userDAO.findOne("john@gmail.com"); + assertEquals("John", user.getName(), "User name:" + user.getName() + " incorrect"); + } + + @Test + public void testClassicAssertions() { + User user1 = userDAO.findOne("john@gmail.com"); + User user2 = userDAO.findOne("john@yahoo.com"); + + assertNotNull(user1); + assertNull(user2); + + user2 = new User("john@yahoo.com", "John"); + assertEquals(user1.getName(), user2.getName(), "Names are not equal"); + assertFalse(user1.getEmail().equals(user2.getEmail()), "Emails are equal"); + assertNotSame(user1, user2); + } + + @Test + @Disabled + public void testGroupedAssertions() { + User user = userDAO.findOne("john@gmail.com"); + assertAll("user", () -> assertEquals("Johnson", user.getName()), () -> assertEquals("johnson@gmail.com", user.getEmail())); + } + + @Test + public void testIterableEquals() { + User user1 = new User("john@gmail.com", "John"); + User user2 = new User("ana@gmail.com", "Ana"); + + List users = new ArrayList<>(); + users.add(user1); + users.add(user2); + + assertIterableEquals(users, userDAO.findAll()); + } + + @Test + public void testLinesMatch() { + List expectedLines = Collections.singletonList("(.*)@(.*)"); + List emails = Arrays.asList("john@gmail.com"); + assertLinesMatch(expectedLines, emails); + } + + @Test + void testThrows() { + User user = null; + Exception exception = assertThrows(NullPointerException.class, () -> user.getName()); + logger.info(exception.getMessage()); + } + + @Test + @Disabled + void testFail() { + fail("this test fails"); + } + + @Test + void testAssumptions() { + List users = userDAO.findAll(); + assumeFalse(users == null); + assumeTrue(users.size() > 0); + + User user1 = new User("john@gmail.com", "John"); + + assumingThat(users.contains(user1), () -> assertTrue(users.size() > 1)); + } + + @ParameterizedTest + @ValueSource(strings = { "john@gmail.com", "ana@gmail.com" }) + public void testParameterized(String email) { + assertNotNull(userDAO.findOne(email)); + } + + @AfterAll + public static void removeData() { + userDAO.deleteAll(); + } + + @Nested + class DeleteUsersTest { + @Test + public void addUser() { + User user = new User("bob@gmail.com", "Bob"); + userDAO.add(user); + assertNotNull(userDAO.findOne("bob@gmail.com")); + + userDAO.delete("bob@gmail.com"); + assertNull(userDAO.findOne("bob@gmail.com")); + } + } + +}