diff --git a/junit5/README.md b/junit5/README.md new file mode 100644 index 0000000000..ad16ad164d --- /dev/null +++ b/junit5/README.md @@ -0,0 +1,6 @@ +## JUnit5 + +This module contains articles about the JUnit 5 + +### Relevant Articles: + diff --git a/junit5/pom.xml b/junit5/pom.xml new file mode 100644 index 0000000000..b9804408a2 --- /dev/null +++ b/junit5/pom.xml @@ -0,0 +1,36 @@ + + + + 4.0.0 + junit5 + junit5 + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + + + 8 + 8 + + + + + org.junit.jupiter + junit-jupiter-api + 5.8.1 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.8.1 + test + + + + \ No newline at end of file diff --git a/junit5/src/test/java/com/baeldung/junit5/A_UnitTest.java b/junit5/src/test/java/com/baeldung/junit5/A_UnitTest.java new file mode 100644 index 0000000000..e4ba59b22d --- /dev/null +++ b/junit5/src/test/java/com/baeldung/junit5/A_UnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.Test; + +public class A_UnitTest { + + @Test + public void first() throws Exception{ + System.out.println("Test A first() start => " + Thread.currentThread().getName()); + Thread.sleep(500); + System.out.println("Test A first() end => " + Thread.currentThread().getName()); + } + + @Test + public void second() throws Exception{ + System.out.println("Test A second() start => " + Thread.currentThread().getName()); + Thread.sleep(500); + System.out.println("Test A second() end => " + Thread.currentThread().getName()); + } + +} diff --git a/junit5/src/test/java/com/baeldung/junit5/B_UnitTest.java b/junit5/src/test/java/com/baeldung/junit5/B_UnitTest.java new file mode 100644 index 0000000000..2b195d2551 --- /dev/null +++ b/junit5/src/test/java/com/baeldung/junit5/B_UnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; + +public class B_UnitTest { + + @Test + public void first() throws Exception{ + System.out.println("Test B first() start => " + Thread.currentThread().getName()); + Thread.sleep(500); + System.out.println("Test B first() end => " + Thread.currentThread().getName()); + } + + @Test + public void second() throws Exception{ + System.out.println("Test B second() start => " + Thread.currentThread().getName()); + Thread.sleep(500); + System.out.println("Test B second() end => " + Thread.currentThread().getName()); + } + +} diff --git a/junit5/src/test/java/com/baeldung/junit5/C_UnitTest.java b/junit5/src/test/java/com/baeldung/junit5/C_UnitTest.java new file mode 100644 index 0000000000..ce545f6bee --- /dev/null +++ b/junit5/src/test/java/com/baeldung/junit5/C_UnitTest.java @@ -0,0 +1,45 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.ResourceLock; + +import java.util.ArrayList; +import java.util.List; + +public class C_UnitTest { + + private List resources; + + @BeforeEach + void before() { + resources = new ArrayList<>(); + resources.add("test"); + } + + @AfterEach + void after() { + resources.clear(); + } + + @Test + @ResourceLock(value = "resources") + public void first() throws Exception { + System.out.println("Test C first() start => " + Thread.currentThread().getName()); + resources.add("first"); + System.out.println(resources); + Thread.sleep(500); + System.out.println("Test C first() end => " + Thread.currentThread().getName()); + } + + @Test + @ResourceLock(value = "resources") + public void second() throws Exception { + System.out.println("Test C second() start => " + Thread.currentThread().getName()); + resources.add("second"); + System.out.println(resources); + Thread.sleep(500); + System.out.println("Test C second() end => " + Thread.currentThread().getName()); + } +} diff --git a/junit5/src/test/resources/junit-platform.properties b/junit5/src/test/resources/junit-platform.properties new file mode 100644 index 0000000000..42100f85da --- /dev/null +++ b/junit5/src/test/resources/junit-platform.properties @@ -0,0 +1,4 @@ +junit.jupiter.execution.parallel.enabled = true +junit.jupiter.execution.parallel.config.strategy=dynamic +junit.jupiter.execution.parallel.mode.default = concurrent +junit.jupiter.execution.parallel.mode.classes.default = concurrent diff --git a/persistence-modules/java-jpa-3/pom.xml b/persistence-modules/java-jpa-3/pom.xml index ec507c4fca..ad649d58d8 100644 --- a/persistence-modules/java-jpa-3/pom.xml +++ b/persistence-modules/java-jpa-3/pom.xml @@ -74,6 +74,12 @@ ${junit-jupiter.version} test + + org.testcontainers + postgresql + 1.16.0 + test + diff --git a/persistence-modules/java-jpa-3/src/main/java/com/baeldung/jpa/postgresql_schema/Product.java b/persistence-modules/java-jpa-3/src/main/java/com/baeldung/jpa/postgresql_schema/Product.java new file mode 100644 index 0000000000..cf74e8bb6d --- /dev/null +++ b/persistence-modules/java-jpa-3/src/main/java/com/baeldung/jpa/postgresql_schema/Product.java @@ -0,0 +1,30 @@ +package com.baeldung.jpa.postgresql_schema; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "product", schema = "store") +public class Product { + + @Id + private int id; + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/persistence-modules/java-jpa-3/src/main/resources/META-INF/persistence.xml b/persistence-modules/java-jpa-3/src/main/resources/META-INF/persistence.xml index 1166aaca71..ef1c4fb3d3 100644 --- a/persistence-modules/java-jpa-3/src/main/resources/META-INF/persistence.xml +++ b/persistence-modules/java-jpa-3/src/main/resources/META-INF/persistence.xml @@ -149,4 +149,15 @@ + + org.hibernate.jpa.HibernatePersistenceProvider + com.baeldung.jpa.postgresql_schema.Product + true + + + + + + + diff --git a/persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlSchemaLiveTest.java b/persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlSchemaLiveTest.java new file mode 100644 index 0000000000..e46ea2892c --- /dev/null +++ b/persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlSchemaLiveTest.java @@ -0,0 +1,85 @@ +package com.baeldung.jpa.postgresql_schema; + +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.postgresql.ds.PGSimpleDataSource; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +public class PostgresqlSchemaLiveTest { + //This tests require running Docker application with working internet connection to fetch + //Postgres image if the image is not available locally. + + @ClassRule + public static PostgresqlTestContainer container = PostgresqlTestContainer.getInstance(); + + + @BeforeClass + public static void setup() throws Exception { + Properties properties = new Properties(); + properties.setProperty("user", container.getUsername()); + properties.setProperty("password", container.getPassword()); + Connection connection = DriverManager.getConnection(container.getJdbcUrl(), properties); + connection.createStatement().execute("CREATE SCHEMA store"); + connection.createStatement().execute("CREATE TABLE store.product(id SERIAL PRIMARY KEY, name VARCHAR(20))"); + connection.createStatement().execute("INSERT INTO store.product VALUES(1, 'test product')"); + } + + @Test + public void settingUpSchemaUsingJdbcURL() throws Exception { + Properties properties = new Properties(); + properties.setProperty("user", container.getUsername()); + properties.setProperty("password", container.getPassword()); + Connection connection = DriverManager.getConnection(container.getJdbcUrl().concat("¤tSchema=store"), properties); + + ResultSet resultSet = connection.createStatement().executeQuery("SELECT * FROM product"); + resultSet.next(); + + assertThat(resultSet.getInt(1), equalTo(1)); + assertThat(resultSet.getString(2), equalTo("test product")); + } + + @Test + public void settingUpSchemaUsingPGSimpleDataSource() throws Exception { + int port = Integer.parseInt(container.getJdbcUrl().substring(container.getJdbcUrl().lastIndexOf(":") + 1, container.getJdbcUrl().lastIndexOf("/"))); + PGSimpleDataSource ds = new PGSimpleDataSource(); + ds.setServerNames(new String[]{container.getHost()}); + ds.setPortNumbers(new int[]{port}); + ds.setUser(container.getUsername()); + ds.setPassword(container.getPassword()); + ds.setDatabaseName("test"); + ds.setCurrentSchema("store"); + + ResultSet resultSet = ds.getConnection().createStatement().executeQuery("SELECT * FROM product"); + resultSet.next(); + + assertThat(resultSet.getInt(1), equalTo(1)); + assertThat(resultSet.getString(2), equalTo("test product")); + } + + @Test + public void settingUpSchemaUsingTableAnnotation() { + Map props = new HashMap<>(); + props.put("hibernate.connection.url", container.getJdbcUrl()); + props.put("hibernate.connection.user", container.getUsername()); + props.put("hibernate.connection.password", container.getPassword()); + EntityManagerFactory emf = Persistence.createEntityManagerFactory("postgresql_schema_unit", props); + EntityManager entityManager = emf.createEntityManager(); + + Product product = entityManager.find(Product.class, 1); + + assertThat(product.getName(), equalTo("test product")); + } +} diff --git a/persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlTestContainer.java b/persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlTestContainer.java new file mode 100644 index 0000000000..edc84845c6 --- /dev/null +++ b/persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlTestContainer.java @@ -0,0 +1,22 @@ +package com.baeldung.jpa.postgresql_schema; + +import org.testcontainers.containers.PostgreSQLContainer; + +public class PostgresqlTestContainer extends PostgreSQLContainer { + + private static final String IMAGE_VERSION = "postgres"; + + private static PostgresqlTestContainer container; + + + private PostgresqlTestContainer() { + super(IMAGE_VERSION); + } + + public static PostgresqlTestContainer getInstance() { + if (container == null) { + container = new PostgresqlTestContainer(); + } + return container; + } +} diff --git a/pom.xml b/pom.xml index 220e417869..f2a53f38b7 100644 --- a/pom.xml +++ b/pom.xml @@ -472,6 +472,7 @@ json-path jsoup jta + junit5 kubernetes ksqldb