From ef8e084d6d540d2d01a73c15394e238f49c73589 Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Fri, 1 Oct 2021 13:04:07 +0200 Subject: [PATCH 01/34] Parallel Test Execution for JUnit 5 --- junit5/README.md | 6 +++ junit5/pom.xml | 36 +++++++++++++++ .../java/com/baeldung/junit5/A_UnitTest.java | 21 +++++++++ .../java/com/baeldung/junit5/B_UnitTest.java | 23 ++++++++++ .../java/com/baeldung/junit5/C_UnitTest.java | 45 +++++++++++++++++++ .../test/resources/junit-platform.properties | 4 ++ pom.xml | 1 + 7 files changed, 136 insertions(+) create mode 100644 junit5/README.md create mode 100644 junit5/pom.xml create mode 100644 junit5/src/test/java/com/baeldung/junit5/A_UnitTest.java create mode 100644 junit5/src/test/java/com/baeldung/junit5/B_UnitTest.java create mode 100644 junit5/src/test/java/com/baeldung/junit5/C_UnitTest.java create mode 100644 junit5/src/test/resources/junit-platform.properties 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/pom.xml b/pom.xml index f5ac14a009..8d28669313 100644 --- a/pom.xml +++ b/pom.xml @@ -472,6 +472,7 @@ json-path jsoup jta + junit5 kubernetes ksqldb From c85be323470cdd25d82de9ffad2cfd56543bec4a Mon Sep 17 00:00:00 2001 From: Shashank Date: Wed, 13 Oct 2021 09:54:36 +0530 Subject: [PATCH 02/34] Spring Boot 2.5.x compatible updates in spring-security-acl --- .../src/main/resources/com.baeldung.acl.datasource.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-security-modules/spring-security-acl/src/main/resources/com.baeldung.acl.datasource.properties b/spring-security-modules/spring-security-acl/src/main/resources/com.baeldung.acl.datasource.properties index 40f1e6ef38..7a08528f2d 100644 --- a/spring-security-modules/spring-security-acl/src/main/resources/com.baeldung.acl.datasource.properties +++ b/spring-security-modules/spring-security-acl/src/main/resources/com.baeldung.acl.datasource.properties @@ -8,5 +8,5 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect spring.h2.console.path=/myconsole spring.h2.console.enabled=true spring.datasource.initialize=true -spring.datasource.schema=classpath:acl-schema.sql -spring.datasource.data=classpath:acl-data.sql \ No newline at end of file +spring.sql.init.schema-locations=classpath:acl-schema.sql +spring.sql.init.data-locations=classpath:acl-data.sql \ No newline at end of file From bfde84589c28e90e6ac595d1df4f5c9424cc9959 Mon Sep 17 00:00:00 2001 From: Shashank Date: Wed, 13 Oct 2021 19:17:31 +0530 Subject: [PATCH 03/34] Spring Boot 2.5.x compatible updates in spring-security-web-boot-2 --- .../src/main/resources/application-mysql.properties | 5 ++--- .../src/main/resources/application-postgre.properties | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/spring-security-modules/spring-security-web-boot-2/src/main/resources/application-mysql.properties b/spring-security-modules/spring-security-web-boot-2/src/main/resources/application-mysql.properties index 568d0c5ca3..0b81b046fb 100644 --- a/spring-security-modules/spring-security-web-boot-2/src/main/resources/application-mysql.properties +++ b/spring-security-modules/spring-security-web-boot-2/src/main/resources/application-mysql.properties @@ -1,9 +1,8 @@ -spring.datasource.platform=mysql +spring.sql.init.platform=mysql spring.datasource.url=jdbc:mysql://localhost:3306/jdbc_authentication spring.datasource.username=root spring.datasource.password=pass -spring.datasource.initialization-mode=always +spring.sql.init.mode=always spring.jpa.hibernate.ddl-auto=none -spring.profiles.active=mysql diff --git a/spring-security-modules/spring-security-web-boot-2/src/main/resources/application-postgre.properties b/spring-security-modules/spring-security-web-boot-2/src/main/resources/application-postgre.properties index 69faece45e..e37aec231a 100644 --- a/spring-security-modules/spring-security-web-boot-2/src/main/resources/application-postgre.properties +++ b/spring-security-modules/spring-security-web-boot-2/src/main/resources/application-postgre.properties @@ -1,7 +1,7 @@ -spring.datasource.platform=postgre +spring.sql.init.platform=postgre spring.datasource.url=jdbc:postgresql://localhost:5432/jdbc_authentication spring.datasource.username=postgres spring.datasource.password=pass -spring.datasource.initialization-mode=always +spring.sql.init.mode=always spring.jpa.hibernate.ddl-auto=none From cd3868d8635de8c98e1ccdddafff0776982be62f Mon Sep 17 00:00:00 2001 From: Shashank Date: Thu, 14 Oct 2021 19:08:58 +0530 Subject: [PATCH 04/34] Spring Boot 2.5.x compatible updates in spring-boot-persistence --- .../src/test/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistence-modules/spring-boot-persistence/src/test/resources/application.properties b/persistence-modules/spring-boot-persistence/src/test/resources/application.properties index 07101ca2f5..d22bd38426 100644 --- a/persistence-modules/spring-boot-persistence/src/test/resources/application.properties +++ b/persistence-modules/spring-boot-persistence/src/test/resources/application.properties @@ -13,4 +13,4 @@ hibernate.cache.use_query_cache=true hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory spring.jpa.properties.hibernate.hbm2ddl.import_files=import_books.sql -spring.datasource.data=import_*_users.sql \ No newline at end of file +spring.sql.init.data-locations=import_*_users.sql \ No newline at end of file From 8c5391ca7534f68df0553f05050e36939ed9e210 Mon Sep 17 00:00:00 2001 From: Shashank Date: Thu, 14 Oct 2021 20:09:00 +0530 Subject: [PATCH 05/34] Spring Boot 2.5.x compatible updates in spring-data-jpa-repo --- .../com/baeldung/boot/daos/JpaRepositoriesIntegrationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/boot/daos/JpaRepositoriesIntegrationTest.java b/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/boot/daos/JpaRepositoriesIntegrationTest.java index 9e4b78dce3..fe02f79a56 100644 --- a/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/boot/daos/JpaRepositoriesIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/boot/daos/JpaRepositoriesIntegrationTest.java @@ -21,7 +21,7 @@ import com.baeldung.boot.domain.Location; import com.baeldung.boot.domain.Store; @RunWith(SpringRunner.class) -@DataJpaTest(properties="spring.datasource.data=classpath:import_entities.sql") +@DataJpaTest(properties="spring.sql.init.data-locations=classpath:import_entities.sql") public class JpaRepositoriesIntegrationTest { @Autowired private LocationRepository locationRepository; From 40aa009be24b008c2b60ca633b82387c9e3b30b7 Mon Sep 17 00:00:00 2001 From: Shashank Date: Fri, 15 Oct 2021 09:16:33 +0530 Subject: [PATCH 06/34] Spring Boot 2.5.x compatible updates in spring-data-jpa-query --- .../src/main/resources/application-joins.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistence-modules/spring-data-jpa-query/src/main/resources/application-joins.properties b/persistence-modules/spring-data-jpa-query/src/main/resources/application-joins.properties index fe2270293b..5fc1fd78e0 100644 --- a/persistence-modules/spring-data-jpa-query/src/main/resources/application-joins.properties +++ b/persistence-modules/spring-data-jpa-query/src/main/resources/application-joins.properties @@ -1 +1 @@ -spring.datasource.data=classpath:db/import_joins.sql +spring.sql.init.data-locations=classpath:db/import_joins.sql From df95f2e2a83ffad9de7fd698640ee3952059d76f Mon Sep 17 00:00:00 2001 From: Shashank Date: Fri, 15 Oct 2021 09:24:10 +0530 Subject: [PATCH 07/34] Spring Boot 2.5.x compatible updates in spring-data-jpa-query-2 --- .../spring/data/jpa/query/UserRepositoryIntegrationTest.java | 2 +- .../jpa/query/datetime/ArticleRepositoryIntegrationTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/persistence-modules/spring-data-jpa-query-2/src/test/java/com/baeldung/spring/data/jpa/query/UserRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-query-2/src/test/java/com/baeldung/spring/data/jpa/query/UserRepositoryIntegrationTest.java index 0ede418acd..a9ab13feed 100644 --- a/persistence-modules/spring-data-jpa-query-2/src/test/java/com/baeldung/spring/data/jpa/query/UserRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-query-2/src/test/java/com/baeldung/spring/data/jpa/query/UserRepositoryIntegrationTest.java @@ -19,7 +19,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @RunWith(SpringRunner.class) -@DataJpaTest(properties = "spring.datasource.data=classpath:insert_users.sql") +@DataJpaTest(properties = "spring.sql.init.data-locations=classpath:insert_users.sql") public class UserRepositoryIntegrationTest { @Autowired diff --git a/persistence-modules/spring-data-jpa-query-2/src/test/java/com/baeldung/spring/data/jpa/query/datetime/ArticleRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-query-2/src/test/java/com/baeldung/spring/data/jpa/query/datetime/ArticleRepositoryIntegrationTest.java index b1158b3dae..e00a340615 100644 --- a/persistence-modules/spring-data-jpa-query-2/src/test/java/com/baeldung/spring/data/jpa/query/datetime/ArticleRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-query-2/src/test/java/com/baeldung/spring/data/jpa/query/datetime/ArticleRepositoryIntegrationTest.java @@ -17,7 +17,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @RunWith(SpringRunner.class) -@DataJpaTest(properties="spring.datasource.data=classpath:import_entities.sql") +@DataJpaTest(properties="spring.sql.init.data-locations=classpath:import_entities.sql") public class ArticleRepositoryIntegrationTest { @Autowired From b5ed1148cc072f8bdf1bfd9c97a59dc5b82b4eac Mon Sep 17 00:00:00 2001 From: Shashank Date: Fri, 15 Oct 2021 09:46:03 +0530 Subject: [PATCH 08/34] Spring Boot 2.5.x compatible updates in spring-boot-persistence-h2 --- .../resources/application-lazy-load-no-trans-off.properties | 2 +- .../main/resources/application-lazy-load-no-trans-on.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-off.properties b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-off.properties index ca60ef3ce3..a1243dc1df 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-off.properties +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-off.properties @@ -5,7 +5,7 @@ spring.datasource.password= spring.jpa.hibernate.ddl-auto=create-drop spring.h2.console.enabled=true spring.h2.console.path=/h2-console -spring.datasource.data=data-trans.sql +spring.sql.init.data-locations=data-trans.sql logging.level.org.hibernate.SQL=INFO logging.level.org.hibernate.type=INFO diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-on.properties b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-on.properties index 0469ea0dde..2ea89b2ee6 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-on.properties +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-on.properties @@ -5,7 +5,7 @@ spring.datasource.password= spring.jpa.hibernate.ddl-auto=create-drop spring.h2.console.enabled=true spring.h2.console.path=/h2-console -spring.datasource.data=data-trans.sql +spring.sql.init.data-locations=data-trans.sql logging.level.org.hibernate.SQL=INFO logging.level.org.hibernate.type=INFO From 4bd4abe9839b38335d8f7b206ed6a48956e67694 Mon Sep 17 00:00:00 2001 From: Shashank Date: Fri, 15 Oct 2021 09:51:04 +0530 Subject: [PATCH 09/34] Spring Boot 2.5.x compatible updates in spring-data-jpa-annotations --- .../src/main/resources/ddd.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistence-modules/spring-data-jpa-annotations/src/main/resources/ddd.properties b/persistence-modules/spring-data-jpa-annotations/src/main/resources/ddd.properties index e5126b694b..af14453993 100644 --- a/persistence-modules/spring-data-jpa-annotations/src/main/resources/ddd.properties +++ b/persistence-modules/spring-data-jpa-annotations/src/main/resources/ddd.properties @@ -1 +1 @@ -spring.datasource.initialization-mode=never \ No newline at end of file +spring.sql.init.mode=never \ No newline at end of file From 1afa3bfcea5becd3fc0f7c4d26027c2975ad9508 Mon Sep 17 00:00:00 2001 From: Ashish Gupta <30566001+gupta-ashu01@users.noreply.github.com> Date: Fri, 15 Oct 2021 12:24:11 +0530 Subject: [PATCH 10/34] BAEL-5126 --- .../src/main/resources/application.yaml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/resources/application.yaml diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.yaml b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.yaml new file mode 100644 index 0000000000..96f59859a4 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.yaml @@ -0,0 +1,17 @@ +spring: + h2: + console: + enabled: true + path: /h2-console + datasource: + url: jdbc:h2:mem:mydb + username: sa + password: + driverClassName: org.h2.Driver + jpa: + defer-datasource-initialization: true + show-sql: true + hibernate: + format_sql: true + validator.apply_to_ddl: false + ddl-auto: create-drop \ No newline at end of file From 6d1cdbe9d75b7e34cf05a8fd9f459600ec5aab13 Mon Sep 17 00:00:00 2001 From: lsieun <331505785@qq.com> Date: Sun, 17 Oct 2021 13:29:38 +0800 Subject: [PATCH 11/34] BAEL-4522: Convert Byte Array to its Numeric Representation (#11267) * Convert Byte Array to its Numeric Representation * Remove Redundant Getter Method --- .../core-java-arrays-convert/pom.xml | 5 + .../ByteArrayToNumericRepresentation.java | 281 ++++++++++++++++ ...eArrayToNumericRepresentationUnitTest.java | 316 ++++++++++++++++++ 3 files changed, 602 insertions(+) create mode 100644 core-java-modules/core-java-arrays-convert/src/main/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentation.java create mode 100644 core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentationUnitTest.java diff --git a/core-java-modules/core-java-arrays-convert/pom.xml b/core-java-modules/core-java-arrays-convert/pom.xml index 4cb2946ac9..6e001e12b0 100644 --- a/core-java-modules/core-java-arrays-convert/pom.xml +++ b/core-java-modules/core-java-arrays-convert/pom.xml @@ -19,6 +19,11 @@ guava ${guava.version} + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + diff --git a/core-java-modules/core-java-arrays-convert/src/main/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentation.java b/core-java-modules/core-java-arrays-convert/src/main/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentation.java new file mode 100644 index 0000000000..82d60e1666 --- /dev/null +++ b/core-java-modules/core-java-arrays-convert/src/main/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentation.java @@ -0,0 +1,281 @@ +package com.baeldung.array.conversions; + +import com.google.common.primitives.Ints; +import com.google.common.primitives.Longs; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.Conversion; + +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.util.Arrays; + +class ByteArrayToNumericRepresentation { + + static int convertByteArrayToIntUsingShiftOperator(byte[] bytes) { + int value = 0; + for (byte b : bytes) { + value = (value << 8) + (b & 0xFF); + } + return value; + } + + static byte[] convertIntToByteArrayUsingShiftOperator(int value) { + byte[] bytes = new byte[Integer.BYTES]; + int length = bytes.length; + for (int i = 0; i < length; i++) { + bytes[length - i - 1] = (byte) (value & 0xFF); + value >>= 8; + } + return bytes; + } + + static long convertByteArrayToLongUsingShiftOperator(byte[] bytes) { + long value = 0; + for (byte b : bytes) { + value <<= 8; + value |= (b & 0xFF); + } + return value; + } + + static byte[] convertLongToByteArrayUsingShiftOperator(long value) { + byte[] bytes = new byte[Long.BYTES]; + int length = bytes.length; + for (int i = 0; i < length; i++) { + bytes[length - i - 1] = (byte) (value & 0xFF); + value >>= 8; + } + return bytes; + } + + static float convertByteArrayToFloatUsingShiftOperator(byte[] bytes) { + // convert bytes to int + int intValue = 0; + for (byte b : bytes) { + intValue = (intValue << 8) + (b & 0xFF); + } + + // convert int to float + return Float.intBitsToFloat(intValue); + } + + static byte[] convertFloatToByteArrayUsingShiftOperator(float value) { + // convert float to int + int intValue = Float.floatToIntBits(value); + + // convert int to bytes + byte[] bytes = new byte[Float.BYTES]; + int length = bytes.length; + for (int i = 0; i < length; i++) { + bytes[length - i - 1] = (byte) (intValue & 0xFF); + intValue >>= 8; + } + return bytes; + } + + static double convertingByteArrayToDoubleUsingShiftOperator(byte[] bytes) { + long longValue = 0; + for (byte b : bytes) { + longValue = (longValue << 8) + (b & 0xFF); + } + + return Double.longBitsToDouble(longValue); + } + + static byte[] convertDoubleToByteArrayUsingShiftOperator(double value) { + long longValue = Double.doubleToLongBits(value); + + byte[] bytes = new byte[Double.BYTES]; + int length = bytes.length; + for (int i = 0; i < length; i++) { + bytes[length - i - 1] = (byte) (longValue & 0xFF); + longValue >>= 8; + } + return bytes; + } + + static int convertByteArrayToIntUsingByteBuffer(byte[] bytes) { + ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); + buffer.put(bytes); + buffer.rewind(); + return buffer.getInt(); + } + + static byte[] convertIntToByteArrayUsingByteBuffer(int value) { + ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); + buffer.putInt(value); + buffer.rewind(); + return buffer.array(); + } + + static long convertByteArrayToLongUsingByteBuffer(byte[] bytes) { + ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); + buffer.put(bytes); + buffer.rewind(); + return buffer.getLong(); + } + + static byte[] convertLongToByteArrayUsingByteBuffer(long value) { + ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); + buffer.putLong(value); + buffer.rewind(); + return buffer.array(); + } + + static float convertByteArrayToFloatUsingByteBuffer(byte[] bytes) { + ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES); + buffer.put(bytes); + buffer.rewind(); + return buffer.getFloat(); + } + + static byte[] convertFloatToByteArrayUsingByteBuffer(float value) { + ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES); + buffer.putFloat(value); + buffer.rewind(); + return buffer.array(); + } + + static double convertByteArrayToDoubleUsingByteBuffer(byte[] bytes) { + ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES); + buffer.put(bytes); + buffer.rewind(); + return buffer.getDouble(); + } + + static byte[] convertDoubleToByteArrayUsingByteBuffer(double value) { + ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES); + buffer.putDouble(value); + buffer.rewind(); + return buffer.array(); + } + + static int convertByteArrayToIntUsingBigInteger(byte[] bytes) { + return new BigInteger(bytes).intValue(); + } + + static byte[] convertIntToByteArrayUsingBigInteger(int value) { + return BigInteger.valueOf(value).toByteArray(); + } + + static long convertByteArrayToLongUsingBigInteger(byte[] bytes) { + return new BigInteger(bytes).longValue(); + } + + static byte[] convertLongToByteArrayUsingBigInteger(long value) { + return BigInteger.valueOf(value).toByteArray(); + } + + static float convertByteArrayToFloatUsingBigInteger(byte[] bytes) { + int intValue = new BigInteger(bytes).intValue(); + return Float.intBitsToFloat(intValue); + } + + static byte[] convertFloatToByteArrayUsingBigInteger(float value) { + int intValue = Float.floatToIntBits(value); + return BigInteger.valueOf(intValue).toByteArray(); + } + + static double convertByteArrayToDoubleUsingBigInteger(byte[] bytes) { + long longValue = new BigInteger(bytes).longValue(); + return Double.longBitsToDouble(longValue); + } + + static byte[] convertDoubleToByteArrayUsingBigInteger(double value) { + long longValue = Double.doubleToLongBits(value); + return BigInteger.valueOf(longValue).toByteArray(); + } + + static int convertingByteArrayToIntUsingGuava(byte[] bytes) { + return Ints.fromByteArray(bytes); + } + + static byte[] convertIntToByteArrayUsingGuava(int value) { + return Ints.toByteArray(value); + } + + static long convertByteArrayToLongUsingGuava(byte[] bytes) { + return Longs.fromByteArray(bytes); + } + + static byte[] convertLongToByteArrayUsingGuava(long value) { + return Longs.toByteArray(value); + } + + static float convertByteArrayToFloatUsingGuava(byte[] bytes) { + int intValue = Ints.fromByteArray(bytes); + return Float.intBitsToFloat(intValue); + } + + static byte[] convertFloatToByteArrayUsingGuava(float value) { + int intValue = Float.floatToIntBits(value); + return Ints.toByteArray(intValue); + } + + static double convertByteArrayToDoubleUsingGuava(byte[] bytes) { + long longValue = Longs.fromByteArray(bytes); + return Double.longBitsToDouble(longValue); + } + + static byte[] convertDoubleToByteArrayUsingGuava(double value) { + long longValue = Double.doubleToLongBits(value); + return Longs.toByteArray(longValue); + } + + static int convertByteArrayToIntUsingCommonsLang(byte[] bytes) { + byte[] copyBytes = Arrays.copyOf(bytes, bytes.length); + ArrayUtils.reverse(copyBytes); + return Conversion.byteArrayToInt(copyBytes, 0, 0, 0, copyBytes.length); + } + + static byte[] convertIntToByteArrayUsingCommonsLang(int value) { + byte[] bytes = new byte[Integer.BYTES]; + Conversion.intToByteArray(value, 0, bytes, 0, bytes.length); + ArrayUtils.reverse(bytes); + return bytes; + } + + static long convertByteArrayToLongUsingCommonsLang(byte[] bytes) { + byte[] copyBytes = Arrays.copyOf(bytes, bytes.length); + ArrayUtils.reverse(copyBytes); + return Conversion.byteArrayToLong(copyBytes, 0, 0, 0, copyBytes.length); + } + + static byte[] convertLongToByteArrayUsingCommonsLang(long value) { + byte[] bytes = new byte[Long.BYTES]; + Conversion.longToByteArray(value, 0, bytes, 0, bytes.length); + ArrayUtils.reverse(bytes); + return bytes; + } + + static float convertByteArrayToFloatUsingCommonsLang(byte[] bytes) { + byte[] copyBytes = Arrays.copyOf(bytes, bytes.length); + ArrayUtils.reverse(copyBytes); + int intValue = Conversion.byteArrayToInt(copyBytes, 0, 0, 0, copyBytes.length); + return Float.intBitsToFloat(intValue); + } + + static byte[] convertFloatToByteArrayUsingCommonsLang(float value) { + int intValue = Float.floatToIntBits(value); + byte[] bytes = new byte[Float.BYTES]; + Conversion.intToByteArray(intValue, 0, bytes, 0, bytes.length); + ArrayUtils.reverse(bytes); + return bytes; + } + + static double convertByteArrayToDoubleUsingCommonsLang(byte[] bytes) { + byte[] copyBytes = Arrays.copyOf(bytes, bytes.length); + ArrayUtils.reverse(copyBytes); + long longValue = Conversion.byteArrayToLong(copyBytes, 0, 0, 0, copyBytes.length); + return Double.longBitsToDouble(longValue); + } + + static byte[] convertDoubleToByteArrayUsingCommonsLang(double value) { + long longValue = Double.doubleToLongBits(value); + byte[] bytes = new byte[Long.BYTES]; + Conversion.longToByteArray(longValue, 0, bytes, 0, bytes.length); + ArrayUtils.reverse(bytes); + return bytes; + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentationUnitTest.java b/core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentationUnitTest.java new file mode 100644 index 0000000000..0fae765d9c --- /dev/null +++ b/core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentationUnitTest.java @@ -0,0 +1,316 @@ +package com.baeldung.array.conversions; + +import org.junit.Test; + +import static com.baeldung.array.conversions.ByteArrayToNumericRepresentation.*; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +public class ByteArrayToNumericRepresentationUnitTest { + private static final byte[] INT_BYTE_ARRAY = new byte[]{ + (byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE + }; + private static final int INT_VALUE = 0xCAFEBABE; + + + private static final byte[] FLOAT_BYTE_ARRAY = new byte[]{ + (byte) 0x40, (byte) 0x48, (byte) 0xF5, (byte) 0xC3 + }; + private static final float FLOAT_VALUE = 3.14F; + + + private static final byte[] LONG_BYTE_ARRAY = new byte[]{ + (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67, + (byte) 0x89, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF + }; + private static final long LONG_VALUE = 0x0123456789ABCDEFL; + + + private static final byte[] DOUBLE_BYTE_ARRAY = new byte[]{ + (byte) 0x3F, (byte) 0xE3, (byte) 0xC6, (byte) 0xA7, + (byte) 0xEF, (byte) 0x9D, (byte) 0xB2, (byte) 0x2D + }; + private static final double DOUBLE_VALUE = 0.618D; + + + @Test + public void givenShiftOperator_whenConvertingByteArrayToInt_thenSuccess() { + int value = convertByteArrayToIntUsingShiftOperator(INT_BYTE_ARRAY); + + assertEquals(INT_VALUE, value); + } + + @Test + public void givenShiftOperator_whenConvertingIntToByteArray_thenSuccess() { + byte[] bytes = convertIntToByteArrayUsingShiftOperator(INT_VALUE); + + assertArrayEquals(INT_BYTE_ARRAY, bytes); + } + + @Test + public void givenShiftOperator_whenConvertingByteArrayToLong_thenSuccess() { + long value = convertByteArrayToLongUsingShiftOperator(LONG_BYTE_ARRAY); + + assertEquals(LONG_VALUE, value); + } + + @Test + public void givenShiftOperator_whenConvertingLongToByteArray_thenSuccess() { + byte[] bytes = convertLongToByteArrayUsingShiftOperator(LONG_VALUE); + + assertArrayEquals(LONG_BYTE_ARRAY, bytes); + } + + @Test + public void givenShiftOperator_whenConvertingByteArrayToFloat_thenSuccess() { + float value = convertByteArrayToFloatUsingShiftOperator(FLOAT_BYTE_ARRAY); + + assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value)); + } + + @Test + public void givenShiftOperator_whenConvertingFloatToByteArray_thenSuccess() { + byte[] bytes = convertFloatToByteArrayUsingShiftOperator(FLOAT_VALUE); + + assertArrayEquals(FLOAT_BYTE_ARRAY, bytes); + } + + @Test + public void givenShiftOperator_whenConvertingByteArrayToDouble_thenSuccess() { + double value = convertingByteArrayToDoubleUsingShiftOperator(DOUBLE_BYTE_ARRAY); + + assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value)); + } + + @Test + public void givenShiftOperator_whenConvertingDoubleToByteArray_thenSuccess() { + byte[] bytes = convertDoubleToByteArrayUsingShiftOperator(DOUBLE_VALUE); + + assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes); + } + + @Test + public void givenByteBuffer_whenConvertingByteArrayToInt_thenSuccess() { + int value = convertByteArrayToIntUsingByteBuffer(INT_BYTE_ARRAY); + + assertEquals(INT_VALUE, value); + } + + @Test + public void givenByteBuffer_whenConvertingIntToByteArray_thenSuccess() { + byte[] bytes = convertIntToByteArrayUsingByteBuffer(INT_VALUE); + + assertArrayEquals(INT_BYTE_ARRAY, bytes); + } + + @Test + public void givenByteBuffer_whenConvertingByteArrayToLong_thenSuccess() { + long value = convertByteArrayToLongUsingByteBuffer(LONG_BYTE_ARRAY); + + assertEquals(LONG_VALUE, value); + } + + @Test + public void givenByteBuffer_whenConvertingLongToByteArray_thenSuccess() { + byte[] bytes = convertLongToByteArrayUsingByteBuffer(LONG_VALUE); + + assertArrayEquals(LONG_BYTE_ARRAY, bytes); + } + + @Test + public void givenByteBuffer_whenConvertingByteArrayToFloat_thenSuccess() { + float value = convertByteArrayToFloatUsingByteBuffer(FLOAT_BYTE_ARRAY); + + assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value)); + } + + @Test + public void givenByteBuffer_whenConvertingFloatToByteArray_thenSuccess() { + byte[] bytes = convertFloatToByteArrayUsingByteBuffer(FLOAT_VALUE); + + assertArrayEquals(FLOAT_BYTE_ARRAY, bytes); + } + + @Test + public void givenByteBuffer_whenConvertingByteArrayToDouble_thenSuccess() { + double value = convertByteArrayToDoubleUsingByteBuffer(DOUBLE_BYTE_ARRAY); + + assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value)); + } + + @Test + public void givenByteBuffer_whenConvertingDoubleToByteArray_thenSuccess() { + byte[] bytes = convertDoubleToByteArrayUsingByteBuffer(DOUBLE_VALUE); + + assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes); + } + + @Test + public void givenBigInteger_whenConvertingByteArrayToInt_thenSuccess() { + int value = convertByteArrayToIntUsingBigInteger(INT_BYTE_ARRAY); + + assertEquals(INT_VALUE, value); + } + + @Test + public void givenBigInteger_whenConvertingIntToByteArray_thenSuccess() { + byte[] bytes = convertIntToByteArrayUsingBigInteger(INT_VALUE); + + assertArrayEquals(INT_BYTE_ARRAY, bytes); + } + + @Test + public void givenBigInteger_whenConvertingByteArrayToLong_thenSuccess() { + long value = convertByteArrayToLongUsingBigInteger(LONG_BYTE_ARRAY); + + assertEquals(LONG_VALUE, value); + } + + @Test + public void givenBigInteger_whenConvertingLongToByteArray_thenSuccess() { + byte[] bytes = convertLongToByteArrayUsingBigInteger(LONG_VALUE); + + assertArrayEquals(LONG_BYTE_ARRAY, bytes); + } + + @Test + public void givenBigInteger_whenConvertingByteArrayToFloat_thenSuccess() { + float value = convertByteArrayToFloatUsingBigInteger(FLOAT_BYTE_ARRAY); + + assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value)); + } + + @Test + public void givenBigInteger_whenConvertingFloatToByteArray_thenSuccess() { + byte[] bytes = convertFloatToByteArrayUsingBigInteger(FLOAT_VALUE); + + assertArrayEquals(FLOAT_BYTE_ARRAY, bytes); + } + + @Test + public void givenBigInteger_whenConvertingByteArrayToDouble_thenSuccess() { + double value = convertByteArrayToDoubleUsingBigInteger(DOUBLE_BYTE_ARRAY); + + assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value)); + } + + @Test + public void givenBigInteger_whenConvertingDoubleToByteArray_thenSuccess() { + byte[] bytes = convertDoubleToByteArrayUsingBigInteger(DOUBLE_VALUE); + + assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes); + } + + @Test + public void givenGuava_whenConvertingByteArrayToInt_thenSuccess() { + int value = convertingByteArrayToIntUsingGuava(INT_BYTE_ARRAY); + + assertEquals(INT_VALUE, value); + } + + @Test + public void givenGuava_whenConvertingIntToByteArray_thenSuccess() { + byte[] bytes = convertIntToByteArrayUsingGuava(INT_VALUE); + + assertArrayEquals(INT_BYTE_ARRAY, bytes); + } + + @Test + public void givenGuava_whenConvertingByteArrayToLong_thenSuccess() { + long value = convertByteArrayToLongUsingGuava(LONG_BYTE_ARRAY); + + assertEquals(LONG_VALUE, value); + } + + @Test + public void givenGuava_whenConvertingLongToByteArray_thenSuccess() { + byte[] bytes = convertLongToByteArrayUsingGuava(LONG_VALUE); + + assertArrayEquals(LONG_BYTE_ARRAY, bytes); + } + + @Test + public void givenGuava_whenConvertingByteArrayToFloat_thenSuccess() { + float value = convertByteArrayToFloatUsingGuava(FLOAT_BYTE_ARRAY); + + assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value)); + } + + @Test + public void givenGuava_whenConvertingFloatToByteArray_thenSuccess() { + byte[] bytes = convertFloatToByteArrayUsingGuava(FLOAT_VALUE); + + assertArrayEquals(FLOAT_BYTE_ARRAY, bytes); + } + + @Test + public void givenGuava_whenConvertingByteArrayToDouble_thenSuccess() { + double value = convertByteArrayToDoubleUsingGuava(DOUBLE_BYTE_ARRAY); + + assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value)); + } + + @Test + public void givenGuava_whenConvertingDoubleToByteArray_thenSuccess() { + byte[] bytes = convertDoubleToByteArrayUsingGuava(DOUBLE_VALUE); + + assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes); + } + + @Test + public void givenCommonsLang_whenConvertingByteArrayToInt_thenSuccess() { + int value = convertByteArrayToIntUsingCommonsLang(INT_BYTE_ARRAY); + + assertEquals(INT_VALUE, value); + } + + @Test + public void givenCommonsLang_whenConvertingIntToByteArray_thenSuccess() { + byte[] bytes = convertIntToByteArrayUsingCommonsLang(INT_VALUE); + + assertArrayEquals(INT_BYTE_ARRAY, bytes); + } + + @Test + public void givenCommonsLang_whenConvertingByteArrayToLong_thenSuccess() { + long value = convertByteArrayToLongUsingCommonsLang(LONG_BYTE_ARRAY); + + assertEquals(LONG_VALUE, value); + } + + @Test + public void givenCommonsLang_whenConvertingLongToByteArray_thenSuccess() { + byte[] bytes = convertLongToByteArrayUsingCommonsLang(LONG_VALUE); + + assertArrayEquals(LONG_BYTE_ARRAY, bytes); + } + + @Test + public void givenCommonsLang_whenConvertingByteArrayToFloat_thenSuccess() { + float value = convertByteArrayToFloatUsingCommonsLang(FLOAT_BYTE_ARRAY); + + assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value)); + } + + @Test + public void givenCommonsLang_whenConvertingFloatToByteArray_thenSuccess() { + byte[] bytes = convertFloatToByteArrayUsingCommonsLang(FLOAT_VALUE); + + assertArrayEquals(FLOAT_BYTE_ARRAY, bytes); + } + + @Test + public void givenCommonsLang_whenConvertingByteArrayToDouble_thenSuccess() { + double value = convertByteArrayToDoubleUsingCommonsLang(DOUBLE_BYTE_ARRAY); + + assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value)); + } + + @Test + public void givenCommonsLang_whenConvertingDoubleToByteArray_thenSuccess() { + byte[] bytes = convertDoubleToByteArrayUsingCommonsLang(DOUBLE_VALUE); + + assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes); + } + +} \ No newline at end of file From 979db86a5121098e120995553e906eccee0f805c Mon Sep 17 00:00:00 2001 From: Willian Nalepa Oizumi Date: Sun, 17 Oct 2021 03:14:31 -0300 Subject: [PATCH 12/34] examples for BAEL-5153 - remove double quotes (#11344) --- .../DoubleQuotesRemovalUtils.java | 37 ++++++++++++++++ .../DoubleQuotesRemovalUtilsUnitTest.java | 42 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/doublequotesremoval/DoubleQuotesRemovalUtils.java create mode 100644 core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/doublequotesremoval/DoubleQuotesRemovalUtilsUnitTest.java diff --git a/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/doublequotesremoval/DoubleQuotesRemovalUtils.java b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/doublequotesremoval/DoubleQuotesRemovalUtils.java new file mode 100644 index 0000000000..c7cc162026 --- /dev/null +++ b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/doublequotesremoval/DoubleQuotesRemovalUtils.java @@ -0,0 +1,37 @@ +package com.baeldung.doublequotesremoval; + +import com.google.common.base.CharMatcher; + +public class DoubleQuotesRemovalUtils { + + public static String removeWithSubString(String input) { + if (input != null && input.length() >= 2 && input.charAt(0) == '\"' + && input.charAt(input.length() - 1) == '\"') { + return input.substring(1, input.length() - 1); + } + + return input; + } + + public static String removeWithReplaceAllSimple(String input) { + if (input == null || input.isEmpty()) + return input; + + return input.replaceAll("\"", ""); + } + + public static String removeWithReplaceAllAdvanced(String input) { + if (input == null || input.isEmpty()) + return input; + + return input.replaceAll("^\"|\"$", ""); + } + + public static String removeWithGuava(String input) { + if (input == null || input.isEmpty()) + return input; + + return CharMatcher.is('\"').trimFrom(input); + } + +} diff --git a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/doublequotesremoval/DoubleQuotesRemovalUtilsUnitTest.java b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/doublequotesremoval/DoubleQuotesRemovalUtilsUnitTest.java new file mode 100644 index 0000000000..2761cc3139 --- /dev/null +++ b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/doublequotesremoval/DoubleQuotesRemovalUtilsUnitTest.java @@ -0,0 +1,42 @@ +package com.baeldung.doublequotesremoval; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.jupiter.api.Test; + +public class DoubleQuotesRemovalUtilsUnitTest { + + @Test + public void given_EmptyString_ShouldReturn_EmptyString() { + String input = ""; + + assertTrue(DoubleQuotesRemovalUtils.removeWithSubString(input).isEmpty()); + assertTrue(DoubleQuotesRemovalUtils.removeWithReplaceAllSimple(input).isEmpty()); + assertTrue(DoubleQuotesRemovalUtils.removeWithReplaceAllAdvanced(input).isEmpty()); + assertTrue(DoubleQuotesRemovalUtils.removeWithGuava(input).isEmpty()); + } + + @Test + public void given_DoubleQuotesOnly_ShouldReturn_EmptyString() { + String input = "\"\""; + + assertTrue(DoubleQuotesRemovalUtils.removeWithSubString(input).isEmpty()); + assertTrue(DoubleQuotesRemovalUtils.removeWithReplaceAllSimple(input).isEmpty()); + assertTrue(DoubleQuotesRemovalUtils.removeWithReplaceAllAdvanced(input).isEmpty()); + assertTrue(DoubleQuotesRemovalUtils.removeWithGuava(input).isEmpty()); + } + + @Test + public void given_TextWithDoubleQuotes_ShouldReturn_TextOnly() { + + String input = "\"Example of text for this test suit\""; + String expectedResult = "Example of text for this test suit"; + + assertEquals(expectedResult, DoubleQuotesRemovalUtils.removeWithSubString(input)); + assertEquals(expectedResult, DoubleQuotesRemovalUtils.removeWithReplaceAllSimple(input)); + assertEquals(expectedResult, DoubleQuotesRemovalUtils.removeWithReplaceAllAdvanced(input)); + assertEquals(expectedResult, DoubleQuotesRemovalUtils.removeWithGuava(input)); + } + +} From 2f183181d4c5a564f6896f4af64352f19149ac44 Mon Sep 17 00:00:00 2001 From: JoannaaKL <67866556+JoannaaKL@users.noreply.github.com> Date: Sun, 17 Oct 2021 08:36:43 +0200 Subject: [PATCH 13/34] BAEL-5149 (#11241) * Init * Removing mvnw files * Apply eclipse code format * Refactoring * Refactoring * BAEL-4211 Add benchmarks * Delete hexagonal directory * Refactoring based on the feedback * Refactoring based on feedback - package rename * Directory rename * BAEL-5149 Remove accents from String in Java * BAEL-5149 Remove accents from String in Java * Including suggestions after a review Co-authored-by: asia --- .../StringNormalizer.java | 49 +++++++++++++ .../CollatorUnitTest.java | 70 +++++++++++++++++++ .../StringNormalizerUnitTest.java | 51 ++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/accentsanddiacriticsremoval/StringNormalizer.java create mode 100644 core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/accentsanddiacriticsremoval/CollatorUnitTest.java create mode 100644 core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/accentsanddiacriticsremoval/StringNormalizerUnitTest.java diff --git a/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/accentsanddiacriticsremoval/StringNormalizer.java b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/accentsanddiacriticsremoval/StringNormalizer.java new file mode 100644 index 0000000000..d33b9178ea --- /dev/null +++ b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/accentsanddiacriticsremoval/StringNormalizer.java @@ -0,0 +1,49 @@ +package com.baeldung.accentsanddiacriticsremoval; + +import org.apache.commons.lang3.StringUtils; + +import java.text.Normalizer; +import java.util.StringJoiner; + +class StringNormalizer { + + static String removeAccentsWithApacheCommons(String input) { + return StringUtils.stripAccents(input); + } + + static String removeAccents(String input) { + return normalize(input).replaceAll("\\p{M}", ""); + } + + static String unicodeValueOfNormalizedString(String input) { + return toUnicode(normalize(input)); + } + + private static String normalize(String input) { + return input == null ? null : Normalizer.normalize(input, Normalizer.Form.NFKD); + } + + private static String toUnicode(String input) { + if (input.length() == 1) { + return toUnicode(input.charAt(0)); + } else { + StringJoiner stringJoiner = new StringJoiner(" "); + for (char c : input.toCharArray()) { + stringJoiner.add(toUnicode(c)); + } + return stringJoiner.toString(); + } + } + + private static String toUnicode(char input) { + + String hex = Integer.toHexString(input); + StringBuilder sb = new StringBuilder(hex); + + while (sb.length() < 4) { + sb.insert(0, "0"); + } + sb.insert(0, "\\u"); + return sb.toString(); + } +} diff --git a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/accentsanddiacriticsremoval/CollatorUnitTest.java b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/accentsanddiacriticsremoval/CollatorUnitTest.java new file mode 100644 index 0000000000..93b4f5af2e --- /dev/null +++ b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/accentsanddiacriticsremoval/CollatorUnitTest.java @@ -0,0 +1,70 @@ +package com.baeldung.accentsanddiacriticsremoval; + +import org.junit.Test; +import org.openjdk.jmh.annotations.Setup; + +import java.text.Collator; + +import static java.lang.Character.*; +import static java.lang.String.valueOf; +import static org.junit.Assert.assertEquals; + +public class CollatorUnitTest { + + private final Collator collator = Collator.getInstance(); + + @Setup + public void setup() { + collator.setDecomposition(2); + } + + @Test + public void givenAccentedStringAndPrimaryCollatorStrength_whenCompareWithASCIIString_thenReturnTrue() { + Collator collator = Collator.getInstance(); + collator.setDecomposition(2); + collator.setStrength(0); + assertEquals(0, collator.compare("a", "a")); + assertEquals(0, collator.compare("ä", "a")); + assertEquals(0, collator.compare("A", "a")); + assertEquals(1, collator.compare("b", "a")); + assertEquals(0, collator.compare(valueOf(toChars(0x0001)), valueOf(toChars(0x0002)))); + } + + @Test + public void givenAccentedStringAndSecondaryCollatorStrength_whenCompareWithASCIIString_thenReturnTrue() { + collator.setStrength(1); + assertEquals(1, collator.compare("ä", "a")); + assertEquals(1, collator.compare("b", "a")); + assertEquals(0, collator.compare("A", "a")); + assertEquals(0, collator.compare("a", "a")); + assertEquals(0, collator.compare(valueOf(toChars(0x0001)), valueOf(toChars(0x0002)))); + + } + + @Test + public void givenAccentedStringAndTeriaryCollatorStrength_whenCompareWithASCIIString_thenReturnTrue() { + collator.setStrength(2); + assertEquals(1, collator.compare("A", "a")); + assertEquals(1, collator.compare("ä", "a")); + assertEquals(1, collator.compare("b", "a")); + assertEquals(0, collator.compare("a", "a")); + assertEquals(0, collator.compare(valueOf(toChars(0x0001)), valueOf(toChars(0x0002)))); + } + + @Test + public void givenAccentedStringAndIdenticalCollatorStrength_whenCompareWithASCIIString_thenReturnTrue() { + collator.setStrength(3); + assertEquals(1, collator.compare("A", "a")); + assertEquals(1, collator.compare("ä", "a")); + assertEquals(1, collator.compare("b", "a")); + assertEquals(-1, collator.compare(valueOf(toChars(0x0001)), valueOf(toChars(0x0002)))); + assertEquals(0, collator.compare("a", "a")); + } + + @Test + public void givenNondecomposableAccentedStringAndIdenticalCollatorStrength_whenCompareWithASCIIString_thenReturnTrue() { + collator.setStrength(0); + assertEquals(1, collator.compare("ł", "l")); + assertEquals(1, collator.compare("ø", "o")); + } +} diff --git a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/accentsanddiacriticsremoval/StringNormalizerUnitTest.java b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/accentsanddiacriticsremoval/StringNormalizerUnitTest.java new file mode 100644 index 0000000000..74359726b7 --- /dev/null +++ b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/accentsanddiacriticsremoval/StringNormalizerUnitTest.java @@ -0,0 +1,51 @@ +package com.baeldung.accentsanddiacriticsremoval; + +import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.text.Normalizer; + +import org.junit.jupiter.api.Test; + +class StringNormalizerUnitTest { + + @Test + public void givenNotNormalizedString_whenIsNormalized_thenReturnFalse() { + assertFalse(Normalizer.isNormalized("āăąēîïĩíĝġńñšŝśûůŷ", Normalizer.Form.NFKD)); + } + + @Test + void givenStringWithDecomposableUnicodeCharacters_whenRemoveAccents_thenReturnASCIIString() { + assertEquals("aaaeiiiiggnnsssuuy", StringNormalizer.removeAccents("āăąēîïĩíĝġńñšŝśûůŷ")); + } + + @Test + void givenStringWithDecomposableUnicodeCharacters_whenRemoveAccentsWithApacheCommons_thenReturnASCIIString() { + assertEquals("aaaeiiiiggnnsssuuy", StringNormalizer.removeAccentsWithApacheCommons("āăąēîïĩíĝġńñšŝśûůŷ")); + } + + @Test + void givenStringWithNondecomposableUnicodeCharacters_whenRemoveAccents_thenReturnOriginalString() { + assertEquals("łđħœ", StringNormalizer.removeAccents("łđħœ")); + } + + @Test + void givenStringWithNondecomposableUnicodeCharacters_whenRemoveAccentsWithApacheCommons_thenReturnModifiedString() { + assertEquals("lđħœ", StringNormalizer.removeAccentsWithApacheCommons("łđħœ")); + } + + @Test + void givenStringWithDecomposableUnicodeCharacters_whenUnicodeValueOfNormalizedString_thenReturnUnicodeValue() { + assertEquals("\\u0066 \\u0069", StringNormalizer.unicodeValueOfNormalizedString("fi")); + assertEquals("\\u0061 \\u0304", StringNormalizer.unicodeValueOfNormalizedString("ā")); + assertEquals("\\u0069 \\u0308", StringNormalizer.unicodeValueOfNormalizedString("ï")); + assertEquals("\\u006e \\u0301", StringNormalizer.unicodeValueOfNormalizedString("ń")); + } + + @Test + void givenStringWithNonDecomposableUnicodeCharacters_whenUnicodeValueOfNormalizedString_thenReturnOriginalValue() { + assertEquals("\\u0142", StringNormalizer.unicodeValueOfNormalizedString("ł")); + assertEquals("\\u0127", StringNormalizer.unicodeValueOfNormalizedString("ħ")); + assertEquals("\\u0111", StringNormalizer.unicodeValueOfNormalizedString("đ")); + } +} \ No newline at end of file From a11613cf3650017f2f92b2d372aae480cc2a3b75 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Sun, 17 Oct 2021 08:39:39 +0200 Subject: [PATCH 14/34] BAEL-5214: Upgrade xstream and jettison versions (#11332) --- xstream/pom.xml | 4 ++-- .../com/baeldung/initializer/SimpleXstreamInitializer.java | 6 +++++- .../test/java/com/baeldung/rce/XStreamBasicsUnitTest.java | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/xstream/pom.xml b/xstream/pom.xml index c4104d29c4..682b830dd8 100644 --- a/xstream/pom.xml +++ b/xstream/pom.xml @@ -30,8 +30,8 @@ - 1.4.10 - 1.3.8 + 1.4.18 + 1.4.1 \ No newline at end of file diff --git a/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java index 27b8cc84f3..c631726eb3 100644 --- a/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java +++ b/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java @@ -7,7 +7,11 @@ import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver; public class SimpleXstreamInitializer { public XStream getXstreamInstance() { - return new XStream(); + XStream xstream = new XStream(); + xstream.allowTypesByWildcard(new String[]{ + "com.baeldung.**" + }); + return xstream; } public XStream getXstreamJettisonMappedInstance() { diff --git a/xstream/src/test/java/com/baeldung/rce/XStreamBasicsUnitTest.java b/xstream/src/test/java/com/baeldung/rce/XStreamBasicsUnitTest.java index d762813b22..6a9e3c6782 100644 --- a/xstream/src/test/java/com/baeldung/rce/XStreamBasicsUnitTest.java +++ b/xstream/src/test/java/com/baeldung/rce/XStreamBasicsUnitTest.java @@ -21,6 +21,7 @@ public final class XStreamBasicsUnitTest { public void before() { xstream = new XStream(); xstream.alias("person", Person.class); + xstream.allowTypes(new Class[] { Person.class }); } @Test From 125aa509c088ea93b1224b078c723ac09848648d Mon Sep 17 00:00:00 2001 From: kwoyke Date: Sun, 17 Oct 2021 08:52:26 +0200 Subject: [PATCH 15/34] BAEL-5215: Remove redundant @EnableAutoConfiguration annotations (#11331) --- .../kafka/embedded/KafkaProducerConsumerApplication.java | 1 - .../main/java/com/baeldung/kafka/ssl/KafkaSslApplication.java | 1 - 2 files changed, 2 deletions(-) diff --git a/spring-kafka/src/main/java/com/baeldung/kafka/embedded/KafkaProducerConsumerApplication.java b/spring-kafka/src/main/java/com/baeldung/kafka/embedded/KafkaProducerConsumerApplication.java index bf14251d75..b4a03a12b0 100644 --- a/spring-kafka/src/main/java/com/baeldung/kafka/embedded/KafkaProducerConsumerApplication.java +++ b/spring-kafka/src/main/java/com/baeldung/kafka/embedded/KafkaProducerConsumerApplication.java @@ -5,7 +5,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -@EnableAutoConfiguration public class KafkaProducerConsumerApplication { public static void main(String[] args) { diff --git a/spring-kafka/src/main/java/com/baeldung/kafka/ssl/KafkaSslApplication.java b/spring-kafka/src/main/java/com/baeldung/kafka/ssl/KafkaSslApplication.java index ae6df5bee2..b7747ebfef 100644 --- a/spring-kafka/src/main/java/com/baeldung/kafka/ssl/KafkaSslApplication.java +++ b/spring-kafka/src/main/java/com/baeldung/kafka/ssl/KafkaSslApplication.java @@ -5,7 +5,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -@EnableAutoConfiguration public class KafkaSslApplication { public static void main(String[] args) { From 6ada7ea8618d8b606a860af6d0877fa45f612130 Mon Sep 17 00:00:00 2001 From: Ashish Gupta <30566001+gupta-ashu01@users.noreply.github.com> Date: Mon, 18 Oct 2021 10:33:43 +0530 Subject: [PATCH 16/34] Update application.yaml --- .../src/main/resources/application.yaml | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.yaml b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.yaml index 96f59859a4..0ad77cc3cb 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.yaml +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.yaml @@ -1,17 +1,14 @@ spring: h2: console: - enabled: true - path: /h2-console + enabled:true + path:/h2-console + console.settings.trace:false + spring.h2.console.settings.web-allow-others:false datasource: - url: jdbc:h2:mem:mydb - username: sa - password: + url:jdbc:h2:mem:mydb + username:sa + password:password driverClassName: org.h2.Driver jpa: - defer-datasource-initialization: true - show-sql: true - hibernate: - format_sql: true - validator.apply_to_ddl: false - ddl-auto: create-drop \ No newline at end of file + spring.jpa.database-platform:org.hibernate.dialect.H2Dialect From faf9c9e3eee649c64cde590ca92f720a6d1d5e23 Mon Sep 17 00:00:00 2001 From: Ashish Gupta <30566001+gupta-ashu01@users.noreply.github.com> Date: Mon, 18 Oct 2021 10:39:18 +0530 Subject: [PATCH 17/34] Update application.yaml --- .../src/main/resources/application.yaml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.yaml b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.yaml index 0ad77cc3cb..aeb33f797a 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.yaml +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.yaml @@ -1,14 +1,14 @@ spring: h2: console: - enabled:true - path:/h2-console - console.settings.trace:false - spring.h2.console.settings.web-allow-others:false + enabled: true + path: /h2-console + console.settings.trace: false + spring.h2.console.settings.web-allow-others: false datasource: - url:jdbc:h2:mem:mydb - username:sa - password:password + url: jdbc:h2:mem:mydb + username: sa + password: password driverClassName: org.h2.Driver jpa: - spring.jpa.database-platform:org.hibernate.dialect.H2Dialect + spring.jpa.database-platform: org.hibernate.dialect.H2Dialect From 4d072e5e090c688e1e419904df96057b39c75dd9 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Mon, 18 Oct 2021 13:30:21 +0200 Subject: [PATCH 18/34] JAVA-7578: Fix xstream tests --- .../initializer/SimpleXstreamInitializer.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java index c631726eb3..a391b0dca0 100644 --- a/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java +++ b/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java @@ -15,10 +15,18 @@ public class SimpleXstreamInitializer { } public XStream getXstreamJettisonMappedInstance() { - return new XStream(new JettisonMappedXmlDriver()); + XStream xstream = new XStream(new JettisonMappedXmlDriver()); + xstream.allowTypesByWildcard(new String[]{ + "com.baeldung.**" + }); + return xstream; } public XStream getXstreamJsonHierarchicalInstance() { - return new XStream(new JsonHierarchicalStreamDriver()); + XStream xstream = new XStream(new JsonHierarchicalStreamDriver()); + xstream.allowTypesByWildcard(new String[]{ + "com.baeldung.**" + }); + return xstream; } } \ No newline at end of file From b2c4765cc3b305b6044176f0be8be0f0374fbe2d Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Mon, 18 Oct 2021 18:44:47 +0200 Subject: [PATCH 19/34] Parallel Test Execution for JUnit 5 - refactoring --- junit5/README.md | 6 ---- junit5/pom.xml | 36 ------------------- .../java/com/baeldung/junit5/A_UnitTest.java | 21 ----------- .../java/com/baeldung/junit5/B_UnitTest.java | 23 ------------ .../parallel/FirstParallelUnitTest.java | 21 +++++++++++ .../ParallelResourceLockUnitTest.java | 12 +++---- .../parallel/SecondParallelUnitTest.java | 21 +++++++++++ .../test/resources/junit-platform.properties | 0 8 files changed, 48 insertions(+), 92 deletions(-) delete mode 100644 junit5/README.md delete mode 100644 junit5/pom.xml delete mode 100644 junit5/src/test/java/com/baeldung/junit5/A_UnitTest.java delete mode 100644 junit5/src/test/java/com/baeldung/junit5/B_UnitTest.java create mode 100644 testing-modules/junit-5-advanced/src/test/java/com/baeldung/parallel/FirstParallelUnitTest.java rename junit5/src/test/java/com/baeldung/junit5/C_UnitTest.java => testing-modules/junit-5-advanced/src/test/java/com/baeldung/parallel/ParallelResourceLockUnitTest.java (61%) create mode 100644 testing-modules/junit-5-advanced/src/test/java/com/baeldung/parallel/SecondParallelUnitTest.java rename {junit5 => testing-modules/junit-5-advanced}/src/test/resources/junit-platform.properties (100%) diff --git a/junit5/README.md b/junit5/README.md deleted file mode 100644 index ad16ad164d..0000000000 --- a/junit5/README.md +++ /dev/null @@ -1,6 +0,0 @@ -## JUnit5 - -This module contains articles about the JUnit 5 - -### Relevant Articles: - diff --git a/junit5/pom.xml b/junit5/pom.xml deleted file mode 100644 index b9804408a2..0000000000 --- a/junit5/pom.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - 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 deleted file mode 100644 index e4ba59b22d..0000000000 --- a/junit5/src/test/java/com/baeldung/junit5/A_UnitTest.java +++ /dev/null @@ -1,21 +0,0 @@ -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 deleted file mode 100644 index 2b195d2551..0000000000 --- a/junit5/src/test/java/com/baeldung/junit5/B_UnitTest.java +++ /dev/null @@ -1,23 +0,0 @@ -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/testing-modules/junit-5-advanced/src/test/java/com/baeldung/parallel/FirstParallelUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/parallel/FirstParallelUnitTest.java new file mode 100644 index 0000000000..8ac8a161ba --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/parallel/FirstParallelUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.parallel; + +import org.junit.jupiter.api.Test; + +public class FirstParallelUnitTest { + + @Test + public void first() throws Exception{ + System.out.println("FirstParallelUnitTest first() start => " + Thread.currentThread().getName()); + Thread.sleep(500); + System.out.println("FirstParallelUnitTest first() end => " + Thread.currentThread().getName()); + } + + @Test + public void second() throws Exception{ + System.out.println("FirstParallelUnitTest second() start => " + Thread.currentThread().getName()); + Thread.sleep(500); + System.out.println("FirstParallelUnitTest second() end => " + Thread.currentThread().getName()); + } + +} diff --git a/junit5/src/test/java/com/baeldung/junit5/C_UnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/parallel/ParallelResourceLockUnitTest.java similarity index 61% rename from junit5/src/test/java/com/baeldung/junit5/C_UnitTest.java rename to testing-modules/junit-5-advanced/src/test/java/com/baeldung/parallel/ParallelResourceLockUnitTest.java index ce545f6bee..ba607b2654 100644 --- a/junit5/src/test/java/com/baeldung/junit5/C_UnitTest.java +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/parallel/ParallelResourceLockUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.junit5; +package com.baeldung.parallel; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -8,7 +8,7 @@ import org.junit.jupiter.api.parallel.ResourceLock; import java.util.ArrayList; import java.util.List; -public class C_UnitTest { +public class ParallelResourceLockUnitTest { private List resources; @@ -26,20 +26,20 @@ public class C_UnitTest { @Test @ResourceLock(value = "resources") public void first() throws Exception { - System.out.println("Test C first() start => " + Thread.currentThread().getName()); + System.out.println("ParallelResourceLockUnitTest 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()); + System.out.println("ParallelResourceLockUnitTest first() end => " + Thread.currentThread().getName()); } @Test @ResourceLock(value = "resources") public void second() throws Exception { - System.out.println("Test C second() start => " + Thread.currentThread().getName()); + System.out.println("ParallelResourceLockUnitTest 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()); + System.out.println("ParallelResourceLockUnitTest second() end => " + Thread.currentThread().getName()); } } diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/parallel/SecondParallelUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/parallel/SecondParallelUnitTest.java new file mode 100644 index 0000000000..6a6a83fbd8 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/parallel/SecondParallelUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.parallel; + +import org.junit.jupiter.api.Test; + +public class SecondParallelUnitTest { + + @Test + public void first() throws Exception{ + System.out.println("SecondParallelUnitTest first() start => " + Thread.currentThread().getName()); + Thread.sleep(500); + System.out.println("SecondParallelUnitTest first() end => " + Thread.currentThread().getName()); + } + + @Test + public void second() throws Exception{ + System.out.println("SecondParallelUnitTest second() start => " + Thread.currentThread().getName()); + Thread.sleep(500); + System.out.println("SecondParallelUnitTest second() end => " + Thread.currentThread().getName()); + } + +} diff --git a/junit5/src/test/resources/junit-platform.properties b/testing-modules/junit-5-advanced/src/test/resources/junit-platform.properties similarity index 100% rename from junit5/src/test/resources/junit-platform.properties rename to testing-modules/junit-5-advanced/src/test/resources/junit-platform.properties From 3a2eee308c0259169ab0762bb29cecc42cddb054 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Mon, 18 Oct 2021 18:48:13 +0200 Subject: [PATCH 20/34] BAEL-5095 Dockerfile strategies for Git (#11348) Co-authored-by: majewsk6 --- docker/dockerfile-with-git/.gitmodules | 4 ++++ docker/dockerfile-with-git/Dockerfile | 13 +++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 docker/dockerfile-with-git/.gitmodules create mode 100644 docker/dockerfile-with-git/Dockerfile diff --git a/docker/dockerfile-with-git/.gitmodules b/docker/dockerfile-with-git/.gitmodules new file mode 100644 index 0000000000..aa3911dfc3 --- /dev/null +++ b/docker/dockerfile-with-git/.gitmodules @@ -0,0 +1,4 @@ +[submodule "project"] + path = project + url = https://github.com/eugenp/tutorials.git + branch = master \ No newline at end of file diff --git a/docker/dockerfile-with-git/Dockerfile b/docker/dockerfile-with-git/Dockerfile new file mode 100644 index 0000000000..91dfee3bc6 --- /dev/null +++ b/docker/dockerfile-with-git/Dockerfile @@ -0,0 +1,13 @@ +ADD . /project/ +ADD /build/ /project/ +ADD /output/project.jar /project/ + +ADD ssh-private-key /root/.ssh/id_rsa +RUN git clone git@github.com:eugenp/tutorials.git + +ARG username=$GIT_USERNAME +ARG password=$GIT_PASSWORD +RUN git clone https://username:password@github.com:eugenp/tutorials.git + +VOLUME /build/ /project/ + From 5f20ca1a780625961a56d353901aea06431f4e32 Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Mon, 18 Oct 2021 21:40:50 +0200 Subject: [PATCH 21/34] Parallel Test Execution for JUnit 5 - fixed module issue --- pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/pom.xml b/pom.xml index fccee8823f..1e26d09906 100644 --- a/pom.xml +++ b/pom.xml @@ -472,7 +472,6 @@ json-path jsoup jta - junit5 kubernetes ksqldb From 025afb1f7d348d3ef8027aa9f3eb7bf9584e713a Mon Sep 17 00:00:00 2001 From: Andrey Bichkevski <1533091+andbi@users.noreply.github.com> Date: Mon, 18 Oct 2021 20:22:37 -0500 Subject: [PATCH 22/34] BAEL-5143 (#11273) * BAEL-5143 * Modified as per PR comments * The List<> type for session data is changed to a more generic Collection<> * Version upgrade Co-authored-by: 0swald --- rule-engines/evrete/pom.xml | 35 ++++++++ .../evrete/introduction/IntroductionAJR.java | 47 ++++++++++ .../introduction/IntroductionInline.java | 58 ++++++++++++ .../evrete/introduction/model/Customer.java | 26 ++++++ .../evrete/introduction/model/Invoice.java | 19 ++++ .../evrete/src/main/resources/logback.xml | 13 +++ .../main/resources/rules/SalesRuleset.java | 20 +++++ .../introduction/IntroductionAJRUnitTest.java | 80 +++++++++++++++++ .../IntroductionInlineUnitTest.java | 90 +++++++++++++++++++ rule-engines/pom.xml | 1 + 10 files changed, 389 insertions(+) create mode 100644 rule-engines/evrete/pom.xml create mode 100644 rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/IntroductionAJR.java create mode 100644 rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/IntroductionInline.java create mode 100644 rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/model/Customer.java create mode 100644 rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/model/Invoice.java create mode 100644 rule-engines/evrete/src/main/resources/logback.xml create mode 100644 rule-engines/evrete/src/main/resources/rules/SalesRuleset.java create mode 100644 rule-engines/evrete/src/test/java/com/baeldung/evrete/introduction/IntroductionAJRUnitTest.java create mode 100644 rule-engines/evrete/src/test/java/com/baeldung/evrete/introduction/IntroductionInlineUnitTest.java diff --git a/rule-engines/evrete/pom.xml b/rule-engines/evrete/pom.xml new file mode 100644 index 0000000000..819a912c43 --- /dev/null +++ b/rule-engines/evrete/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + com.baeldung.evrete + evrete + 1.0 + evrete + + + 2.1.04 + + + + com.baeldung + rule-engines + 1.0.0-SNAPSHOT + + + + + + org.evrete + evrete-core + ${evrete.version} + + + + org.evrete + evrete-dsl-java + ${evrete.version} + + + \ No newline at end of file diff --git a/rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/IntroductionAJR.java b/rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/IntroductionAJR.java new file mode 100644 index 0000000000..287d083311 --- /dev/null +++ b/rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/IntroductionAJR.java @@ -0,0 +1,47 @@ +package com.baeldung.evrete.introduction; + +import com.baeldung.evrete.introduction.model.Customer; +import com.baeldung.evrete.introduction.model.Invoice; +import org.evrete.KnowledgeService; +import org.evrete.api.Knowledge; + +import java.io.IOException; +import java.net.URL; +import java.util.*; + +public class IntroductionAJR { + public static void main(String[] args) throws IOException { + ClassLoader classLoader = IntroductionAJR.class.getClassLoader(); + KnowledgeService service = new KnowledgeService(); + URL rulesetUrl = classLoader.getResource("rules/SalesRuleset.java"); + Knowledge knowledge = service.newKnowledge( + "JAVA-SOURCE", + rulesetUrl + ); + + List customers = Arrays.asList( + new Customer("Customer A"), + new Customer("Customer B"), + new Customer("Customer C") + ); + + Random random = new Random(); + Collection sessionData = new LinkedList<>(customers); + for (int i = 0; i < 100_000; i++) { + Customer randomCustomer = customers.get(random.nextInt(customers.size())); + Invoice invoice = new Invoice(randomCustomer, 100 * random.nextDouble()); + sessionData.add(invoice); + } + + knowledge + .newStatelessSession() + .insert(sessionData) + .fire(); + + for (Customer c : customers) { + System.out.printf("%s:\t$%,.2f%n", c.getName(), c.getTotal()); + } + + service.shutdown(); + } +} diff --git a/rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/IntroductionInline.java b/rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/IntroductionInline.java new file mode 100644 index 0000000000..8867a72d23 --- /dev/null +++ b/rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/IntroductionInline.java @@ -0,0 +1,58 @@ +package com.baeldung.evrete.introduction; + +import org.evrete.KnowledgeService; +import org.evrete.api.Knowledge; +import com.baeldung.evrete.introduction.model.*; + +import java.util.*; + +public class IntroductionInline { + public static void main(String[] args) { + KnowledgeService service = new KnowledgeService(); + Knowledge knowledge = service + .newKnowledge() + .newRule("Clear total sales") + .forEach("$c", Customer.class) + .execute(ctx -> { + Customer c = ctx.get("$c"); + c.setTotal(0.0); + }) + .newRule("Compute totals") + .forEach( + "$c", Customer.class, + "$i", Invoice.class + ) + .where("$i.customer == $c") + .execute(ctx -> { + Customer c = ctx.get("$c"); + Invoice i = ctx.get("$i"); + c.addToTotal(i.getAmount()); + }); + + + List customers = Arrays.asList( + new Customer("Customer A"), + new Customer("Customer B"), + new Customer("Customer C") + ); + + Random random = new Random(); + Collection sessionData = new LinkedList<>(customers); + for (int i = 0; i < 100_000; i++) { + Customer randomCustomer = customers.get(random.nextInt(customers.size())); + Invoice invoice = new Invoice(randomCustomer, 100 * random.nextDouble()); + sessionData.add(invoice); + } + + knowledge + .newStatelessSession() + .insert(sessionData) + .fire(); + + for (Customer c : customers) { + System.out.printf("%s:\t$%,.2f%n", c.getName(), c.getTotal()); + } + + service.shutdown(); + } +} diff --git a/rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/model/Customer.java b/rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/model/Customer.java new file mode 100644 index 0000000000..9a60850d7c --- /dev/null +++ b/rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/model/Customer.java @@ -0,0 +1,26 @@ +package com.baeldung.evrete.introduction.model; + +public class Customer { + private double total = 0.0; + private final String name; + + public Customer(String name) { + this.name = name; + } + + public void addToTotal(double amount) { + this.total += amount; + } + + public String getName() { + return name; + } + + public double getTotal() { + return total; + } + + public void setTotal(double total) { + this.total = total; + } +} diff --git a/rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/model/Invoice.java b/rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/model/Invoice.java new file mode 100644 index 0000000000..3be4fd2908 --- /dev/null +++ b/rule-engines/evrete/src/main/java/com/baeldung/evrete/introduction/model/Invoice.java @@ -0,0 +1,19 @@ +package com.baeldung.evrete.introduction.model; + +public class Invoice { + private final Customer customer; + private final double amount; + + public Invoice(Customer customer, double amount) { + this.customer = customer; + this.amount = amount; + } + + public Customer getCustomer() { + return customer; + } + + public double getAmount() { + return amount; + } +} diff --git a/rule-engines/evrete/src/main/resources/logback.xml b/rule-engines/evrete/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/rule-engines/evrete/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/rule-engines/evrete/src/main/resources/rules/SalesRuleset.java b/rule-engines/evrete/src/main/resources/rules/SalesRuleset.java new file mode 100644 index 0000000000..d36aca9c4d --- /dev/null +++ b/rule-engines/evrete/src/main/resources/rules/SalesRuleset.java @@ -0,0 +1,20 @@ +package org.abc.author1; + +import com.baeldung.evrete.introduction.model.Customer; +import com.baeldung.evrete.introduction.model.Invoice; +import org.evrete.dsl.annotation.Rule; +import org.evrete.dsl.annotation.Where; + +public class SalesRuleset { + + @Rule + public void rule1(Customer $c) { + $c.setTotal(0.0); + } + + @Rule + @Where("$i.customer == $c") + public void rule2(Customer $c, Invoice $i) { + $c.addToTotal($i.getAmount()); + } +} \ No newline at end of file diff --git a/rule-engines/evrete/src/test/java/com/baeldung/evrete/introduction/IntroductionAJRUnitTest.java b/rule-engines/evrete/src/test/java/com/baeldung/evrete/introduction/IntroductionAJRUnitTest.java new file mode 100644 index 0000000000..955dbb2fe1 --- /dev/null +++ b/rule-engines/evrete/src/test/java/com/baeldung/evrete/introduction/IntroductionAJRUnitTest.java @@ -0,0 +1,80 @@ +package com.baeldung.evrete.introduction; + +import com.baeldung.evrete.introduction.model.Customer; +import com.baeldung.evrete.introduction.model.Invoice; +import org.evrete.KnowledgeService; +import org.evrete.api.Knowledge; +import org.evrete.api.RuleSession; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.io.IOException; +import java.util.*; + +class IntroductionAJRUnitTest { + private static KnowledgeService service; + + @BeforeAll + static void setUpClass() { + service = new KnowledgeService(); + } + + @AfterAll + static void shutDownClass() { + service.shutdown(); + } + + /** + * This test makes sure that each customer's actual total sales is equal to the amount + * computed by the rule engine + */ + @ParameterizedTest + @ValueSource(strings = {"true", "false"}) + void sessionTotalsTest(String type) throws IOException { + boolean stateful = Boolean.parseBoolean(type); + ClassLoader classLoader = IntroductionAJR.class.getClassLoader(); + KnowledgeService service = new KnowledgeService(); + Knowledge knowledge = service + .newKnowledge( + "JAVA-SOURCE", + classLoader.getResource("rules/SalesRuleset.java") + ); + + + List customers = Arrays.asList( + new Customer("Customer A"), + new Customer("Customer B"), + new Customer("Customer C") + ); + Collection sessionData = new LinkedList<>(customers); + + HashMap actualTotals = new HashMap<>(); + Random random = new Random(); + for (int i = 0; i < 1_000; i++) { + Customer randomCustomer = customers.get(random.nextInt(customers.size())); + Invoice invoice = new Invoice(randomCustomer, random.nextInt(100)); + sessionData.add(invoice); + + Double d = actualTotals.get(randomCustomer); + if(d == null) { + d = 0.0; + } + d = d + invoice.getAmount(); + actualTotals.put(randomCustomer, d); + } + + RuleSession session = stateful ? knowledge.newStatefulSession() : knowledge.newStatelessSession(); + session + .insert(sessionData) + .fire(); + + for(Customer c : customers) { + double d1 = c.getTotal(); + double d2 = actualTotals.get(c); + assert d1 == d2; + } + + } +} \ No newline at end of file diff --git a/rule-engines/evrete/src/test/java/com/baeldung/evrete/introduction/IntroductionInlineUnitTest.java b/rule-engines/evrete/src/test/java/com/baeldung/evrete/introduction/IntroductionInlineUnitTest.java new file mode 100644 index 0000000000..0a6aac83bd --- /dev/null +++ b/rule-engines/evrete/src/test/java/com/baeldung/evrete/introduction/IntroductionInlineUnitTest.java @@ -0,0 +1,90 @@ +package com.baeldung.evrete.introduction; + +import com.baeldung.evrete.introduction.model.Customer; +import com.baeldung.evrete.introduction.model.Invoice; +import org.evrete.KnowledgeService; +import org.evrete.api.Knowledge; +import org.evrete.api.RuleSession; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.*; + +class IntroductionInlineUnitTest { + private static KnowledgeService service; + + @BeforeAll + static void setUpClass() { + service = new KnowledgeService(); + } + + @AfterAll + static void shutDownClass() { + service.shutdown(); + } + + /** + * This test makes sure that each customer's actual total sales is equal to the amount + * computed by the rule engine + */ + @ParameterizedTest + @ValueSource(strings = {"true", "false"}) + void sessionTotalsTest(String type) { + boolean stateful = Boolean.parseBoolean(type); + Knowledge knowledge = service + .newKnowledge() + .newRule("Clear customer's total sales") + .forEach("$c", Customer.class) + .execute(ctx -> { + Customer c = ctx.get("$c"); + c.setTotal(0.0); + }) + .newRule("Compute totals") + .forEach( + "$c", Customer.class, + "$i", Invoice.class + ) + .where("$i.customer == $c") + .execute(ctx -> { + Customer c = ctx.get("$c"); + Invoice i = ctx.get("$i"); + c.addToTotal(i.getAmount()); + }); + + + List customers = Arrays.asList( + new Customer("Customer A"), + new Customer("Customer B"), + new Customer("Customer C") + ); + Collection sessionData = new LinkedList<>(customers); + + HashMap actualTotals = new HashMap<>(); + Random random = new Random(); + for (int i = 0; i < 1_000; i++) { + Customer randomCustomer = customers.get(random.nextInt(customers.size())); + Invoice invoice = new Invoice(randomCustomer, random.nextInt(100)); + sessionData.add(invoice); + + Double d = actualTotals.get(randomCustomer); + if(d == null) { + d = 0.0; + } + d = d + invoice.getAmount(); + actualTotals.put(randomCustomer, d); + } + + RuleSession session = stateful ? knowledge.newStatefulSession() : knowledge.newStatelessSession(); + session + .insert(sessionData) + .fire(); + + for(Customer c : customers) { + double d1 = c.getTotal(); + double d2 = actualTotals.get(c); + assert d1 == d2; + } + } +} \ No newline at end of file diff --git a/rule-engines/pom.xml b/rule-engines/pom.xml index db6b2e47ef..6d8a014128 100644 --- a/rule-engines/pom.xml +++ b/rule-engines/pom.xml @@ -15,6 +15,7 @@ easy-rules + evrete openl-tablets rulebook From 2dfd2e9e273a27c74c43033d80ef786c7ff0ee9c Mon Sep 17 00:00:00 2001 From: kwoyke Date: Tue, 19 Oct 2021 10:37:14 +0200 Subject: [PATCH 23/34] BAEL-5219: Fix spring-mvc-webflow and spring-mvc-xml poms (#11350) --- spring-web-modules/spring-mvc-webflow/pom.xml | 4 ++-- spring-web-modules/spring-mvc-xml/pom.xml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-web-modules/spring-mvc-webflow/pom.xml b/spring-web-modules/spring-mvc-webflow/pom.xml index 2e150e2d01..49037e7186 100644 --- a/spring-web-modules/spring-mvc-webflow/pom.xml +++ b/spring-web-modules/spring-mvc-webflow/pom.xml @@ -10,8 +10,8 @@ com.baeldung - parent-modules - 1.0.0-SNAPSHOT + spring-web-modules + 0.0.1-SNAPSHOT diff --git a/spring-web-modules/spring-mvc-xml/pom.xml b/spring-web-modules/spring-mvc-xml/pom.xml index 354d652095..e67052e0cd 100644 --- a/spring-web-modules/spring-mvc-xml/pom.xml +++ b/spring-web-modules/spring-mvc-xml/pom.xml @@ -10,8 +10,8 @@ com.baeldung - parent-modules - 1.0.0-SNAPSHOT + spring-web-modules + 0.0.1-SNAPSHOT From 00b22063ef0acd1aa7ea0d19a369d050d20c7554 Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Wed, 20 Oct 2021 18:59:46 +0200 Subject: [PATCH 24/34] Feature/bael 5177 switch pattern matching (#11345) * BAEL-5177: New module using Java 17 * BAEL-5177: Add unit tests * BAEL-5177: Add switch example * BAEL-5177: Update type pattern test * BAEL-5177: Total type example * BAEL-5177: Refactor * BAEL-5177: Move implementation to separate class * BAEL-5177: Tabs to spaces --- core-java-modules/core-java-17/README.md | 3 + core-java-modules/core-java-17/pom.xml | 81 +++++++++++++++++++ .../switchpatterns/GuardedPatterns.java | 25 ++++++ .../switchpatterns/HandlingNullValues.java | 20 +++++ .../switchpatterns/ParenthesizedPatterns.java | 29 +++++++ .../switchpatterns/PatternMatching.java | 14 ++++ .../switchpatterns/SwitchStatement.java | 14 ++++ .../baeldung/switchpatterns/TypePatterns.java | 30 +++++++ .../GuardedPatternsUnitTest.java | 30 +++++++ .../HandlingNullValuesUnitTest.java | 30 +++++++ .../ParenthesizedPatternsUnitTest.java | 40 +++++++++ .../switchpatterns/TypePatternsUnitTest.java | 49 +++++++++++ 12 files changed, 365 insertions(+) create mode 100644 core-java-modules/core-java-17/README.md create mode 100644 core-java-modules/core-java-17/pom.xml create mode 100644 core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/GuardedPatterns.java create mode 100644 core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/HandlingNullValues.java create mode 100644 core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/ParenthesizedPatterns.java create mode 100644 core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/PatternMatching.java create mode 100644 core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/SwitchStatement.java create mode 100644 core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/TypePatterns.java create mode 100644 core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/GuardedPatternsUnitTest.java create mode 100644 core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/HandlingNullValuesUnitTest.java create mode 100644 core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/ParenthesizedPatternsUnitTest.java create mode 100644 core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/TypePatternsUnitTest.java diff --git a/core-java-modules/core-java-17/README.md b/core-java-modules/core-java-17/README.md new file mode 100644 index 0000000000..798fd3a903 --- /dev/null +++ b/core-java-modules/core-java-17/README.md @@ -0,0 +1,3 @@ +### Relevant articles: + +- TODO \ No newline at end of file diff --git a/core-java-modules/core-java-17/pom.xml b/core-java-modules/core-java-17/pom.xml new file mode 100644 index 0000000000..f9a7ec326b --- /dev/null +++ b/core-java-modules/core-java-17/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + core-java-17 + 0.1.0-SNAPSHOT + core-java-17 + jar + http://maven.apache.org + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + org.assertj + assertj-core + ${assertj.version} + test + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.release} + --enable-preview + ${maven.compiler.source.version} + ${maven.compiler.target.version} + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.plugin.version} + + --enable-preview + 1 + + + + org.apache.maven.surefire + surefire-api + ${surefire.plugin.version} + + + + + + + + 17 + 17 + 17 + 3.8.1 + 3.0.0-M5 + 3.17.2 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/GuardedPatterns.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/GuardedPatterns.java new file mode 100644 index 0000000000..a76287f64a --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/GuardedPatterns.java @@ -0,0 +1,25 @@ +package com.baeldung.switchpatterns; + +public class GuardedPatterns { + + static double getDoubleValueUsingIf(Object o) { + return switch (o) { + case String s -> { + if (s.length() > 0) { + yield Double.parseDouble(s); + } else { + yield 0d; + } + } + default -> 0d; + }; + } + + static double getDoubleValueUsingGuardedPatterns(Object o) { + return switch (o) { + case String s && s.length() > 0 -> Double.parseDouble(s); + default -> 0d; + }; + } + +} diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/HandlingNullValues.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/HandlingNullValues.java new file mode 100644 index 0000000000..8e64480a41 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/HandlingNullValues.java @@ -0,0 +1,20 @@ +package com.baeldung.switchpatterns; + +public class HandlingNullValues { + + static double getDoubleUsingSwitchNullCase(Object o) { + return switch (o) { + case String s -> Double.parseDouble(s); + case null -> 0d; + default -> 0d; + }; + } + + static double getDoubleUsingSwitchTotalType(Object o) { + return switch (o) { + case String s -> Double.parseDouble(s); + case Object ob -> 0d; + }; + } + +} diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/ParenthesizedPatterns.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/ParenthesizedPatterns.java new file mode 100644 index 0000000000..49dd5edb31 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/ParenthesizedPatterns.java @@ -0,0 +1,29 @@ +package com.baeldung.switchpatterns; + +public class ParenthesizedPatterns { + + static double getDoubleValueUsingIf(Object o) { + return switch (o) { + case String s -> { + if (s.length() > 0) { + if (s.contains("#") || s.contains("@")) { + yield 0d; + } else { + yield Double.parseDouble(s); + } + } else { + yield 0d; + } + } + default -> 0d; + }; + } + + static double getDoubleValueUsingParenthesizedPatterns(Object o) { + return switch (o) { + case String s && s.length() > 0 && !(s.contains("#") || s.contains("@")) -> Double.parseDouble(s); + default -> 0d; + }; + } + +} diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/PatternMatching.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/PatternMatching.java new file mode 100644 index 0000000000..f026caa3f1 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/PatternMatching.java @@ -0,0 +1,14 @@ +package com.baeldung.switchpatterns; + +public class PatternMatching { + + public static void main(String[] args) { + Object o = args[0]; + if (o instanceof String s) { + System.out.printf("Object is a string %s", s); + } else if(o instanceof Number n) { + System.out.printf("Object is a number %n", n); + } + } + +} diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/SwitchStatement.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/SwitchStatement.java new file mode 100644 index 0000000000..17d2b1856d --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/SwitchStatement.java @@ -0,0 +1,14 @@ +package com.baeldung.switchpatterns; + +public class SwitchStatement { + + public static void main(String[] args) { + final String b = "B"; + switch (args[0]) { + case "A" -> System.out.println("Parameter is A"); + case b -> System.out.println("Parameter is b"); + default -> System.out.println("Parameter is unknown"); + }; + } + +} diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/TypePatterns.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/TypePatterns.java new file mode 100644 index 0000000000..47af090ad0 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/TypePatterns.java @@ -0,0 +1,30 @@ +package com.baeldung.switchpatterns; + +public class TypePatterns { + + static double getDoubleUsingIf(Object o) { + double result; + + if (o instanceof Integer) { + result = ((Integer) o).doubleValue(); + } else if (o instanceof Float) { + result = ((Float) o).doubleValue(); + } else if (o instanceof String) { + result = Double.parseDouble(((String) o)); + } else { + result = 0d; + } + + return result; + } + + static double getDoubleUsingSwitch(Object o) { + return switch (o) { + case Integer i -> i.doubleValue(); + case Float f -> f.doubleValue(); + case String s -> Double.parseDouble(s); + default -> 0d; + }; + } + +} diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/GuardedPatternsUnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/GuardedPatternsUnitTest.java new file mode 100644 index 0000000000..cff8b1caca --- /dev/null +++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/GuardedPatternsUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.switchpatterns; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static com.baeldung.switchpatterns.GuardedPatterns.*; + +class GuardedPatternsUnitTest { + + @Test + void givenIfImplementation_whenUsingEmptyString_thenDoubleIsReturned() { + assertEquals(0d, getDoubleValueUsingIf("")); + } + + @Test + void givenIfImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() { + assertEquals(10d, getDoubleValueUsingIf("10")); + } + + @Test + void givenPatternsImplementation_whenUsingEmptyString_thenDoubleIsReturned() { + assertEquals(0d, getDoubleValueUsingGuardedPatterns("")); + } + + @Test + void givenPatternsImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() { + assertEquals(10d, getDoubleValueUsingGuardedPatterns("10")); + } + +} diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/HandlingNullValuesUnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/HandlingNullValuesUnitTest.java new file mode 100644 index 0000000000..ffe045cc26 --- /dev/null +++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/HandlingNullValuesUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.switchpatterns; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static com.baeldung.switchpatterns.HandlingNullValues.*; + +class HandlingNullValuesUnitTest { + + @Test + void givenNullCaseInSwitch_whenUsingStringAsArgument_thenDoubleIsReturned() { + assertEquals(10d, getDoubleUsingSwitchNullCase("10")); + } + + @Test + void givenTotalTypeInSwitch_whenUsingNullArgument_thenDoubleIsReturned() { + assertEquals(0d, getDoubleUsingSwitchNullCase(null)); + } + + @Test + void givenTotalTypeInSwitch_whenUsingStringAsArgument_thenDoubleIsReturned() { + assertEquals(10d, getDoubleUsingSwitchTotalType("10")); + } + + @Test + void givenNullCaseInSwitch_whenUsingNullArgument_thenDoubleIsReturned() { + assertEquals(0d, getDoubleUsingSwitchTotalType(null)); + } + +} diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/ParenthesizedPatternsUnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/ParenthesizedPatternsUnitTest.java new file mode 100644 index 0000000000..9548c9f0b6 --- /dev/null +++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/ParenthesizedPatternsUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.switchpatterns; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static com.baeldung.switchpatterns.ParenthesizedPatterns.*; + +class ParenthesizedPatternsUnitTest { + + @Test + void givenIfImplementation_whenUsingEmptyString_thenDoubleIsReturned() { + assertEquals(0d, getDoubleValueUsingIf("")); + } + + @Test + void givenIfImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() { + assertEquals(10d, getDoubleValueUsingIf("10")); + } + + @Test + void givenIfImplementation_whenStringContainsSpecialChar_thenDoubleIsReturned() { + assertEquals(0d, getDoubleValueUsingIf("@10")); + } + + @Test + void givenPatternsImplementation_whenUsingEmptyString_thenDoubleIsReturned() { + assertEquals(0d, getDoubleValueUsingParenthesizedPatterns("")); + } + + @Test + void givenPatternsImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() { + assertEquals(10d, getDoubleValueUsingParenthesizedPatterns("10")); + } + + @Test + void givenPatternsImplementation_whenStringContainsSpecialChar_thenDoubleIsReturned() { + assertEquals(0d, getDoubleValueUsingParenthesizedPatterns("@10")); + } + +} diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/TypePatternsUnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/TypePatternsUnitTest.java new file mode 100644 index 0000000000..25988be53d --- /dev/null +++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/switchpatterns/TypePatternsUnitTest.java @@ -0,0 +1,49 @@ +package com.baeldung.switchpatterns; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static com.baeldung.switchpatterns.TypePatterns.*; + +class TypePatternsUnitTest { + + @Test + void givenIfImplementation_whenUsingIntegerAsArgument_thenDoubleIsReturned() { + assertEquals(10d, getDoubleUsingIf(10)); + } + + @Test + void givenIfImplementation_whenUsingDoubleAsArgument_thenDoubleIsReturned() { + assertEquals(10d, getDoubleUsingIf(10.0f)); + } + + @Test + void givenIfImplementation_whenUsingStringAsArgument_thenDoubleIsReturned() { + assertEquals(10d, getDoubleUsingIf("10")); + } + + @Test + void givenIfImplementation_whenUsingCharAsArgument_thenDoubleIsReturned() { + assertEquals(0d, getDoubleUsingIf('c')); + } + + @Test + void givenSwitchImplementation_whenUsingIntegerAsArgument_thenDoubleIsReturned() { + assertEquals(10d, getDoubleUsingSwitch(10)); + } + + @Test + void givenSwitchImplementation_whenUsingDoubleAsArgument_thenDoubleIsReturned() { + assertEquals(10d, getDoubleUsingSwitch(10.0f)); + } + + @Test + void givenSwitchImplementation_whenUsingStringAsArgument_thenDoubleIsReturned() { + assertEquals(10d, getDoubleUsingSwitch("10")); + } + + @Test + void givenSwitchImplementation_whenUsingCharAsArgument_thenDoubleIsReturned() { + assertEquals(0d, getDoubleUsingSwitch('c')); + } + +} From 3d58e691a4e42b45ce32df846eae3c09067b54ca Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 21 Oct 2021 21:58:48 +0800 Subject: [PATCH 25/34] Update README.md --- testing-modules/junit-4/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/testing-modules/junit-4/README.md b/testing-modules/junit-4/README.md index cb5def7144..1f7517c5b9 100644 --- a/testing-modules/junit-4/README.md +++ b/testing-modules/junit-4/README.md @@ -7,3 +7,4 @@ - [Introduction to Lambda Behave](https://www.baeldung.com/lambda-behave) - [Conditionally Run or Ignore Tests in JUnit 4](https://www.baeldung.com/junit-conditional-assume) - [JUnit 4 on How to Ignore a Base Test Class](https://www.baeldung.com/junit-ignore-base-test-class) +- [Using Fail Assertion in JUnit](https://www.baeldung.com/junit-fail) From ebd12c4cd002b20884ad86ad75067e78acf3e445 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 21 Oct 2021 22:00:30 +0800 Subject: [PATCH 26/34] Update README.md --- ratpack/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ratpack/README.md b/ratpack/README.md index 9c24670709..f42d4c030b 100644 --- a/ratpack/README.md +++ b/ratpack/README.md @@ -11,3 +11,4 @@ This module contains articles about Ratpack. - [Ratpack HTTP Client](https://www.baeldung.com/ratpack-http-client) - [Ratpack with RxJava](https://www.baeldung.com/ratpack-rxjava) - [Ratpack with Groovy](https://www.baeldung.com/ratpack-groovy) +- [Reactive Streams API with Ratpack](https://www.baeldung.com/ratpack-reactive-streams-api) From 12410d9bbc57ceb8cdb035c9ab1481eb0ac03da0 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 21 Oct 2021 22:02:15 +0800 Subject: [PATCH 27/34] Update README.md --- spring-boot-modules/spring-boot-environment/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-boot-modules/spring-boot-environment/README.md b/spring-boot-modules/spring-boot-environment/README.md index e7b0ace7a4..687322938e 100644 --- a/spring-boot-modules/spring-boot-environment/README.md +++ b/spring-boot-modules/spring-boot-environment/README.md @@ -6,3 +6,4 @@ This module contains articles about configuring the Spring Boot `Environment` - [EnvironmentPostProcessor in Spring Boot](https://www.baeldung.com/spring-boot-environmentpostprocessor) - [Spring Properties File Outside jar](https://www.baeldung.com/spring-properties-file-outside-jar) - [Get the Running Port in Spring Boot](https://www.baeldung.com/spring-boot-running-port) + - [Environment Variable Prefixes in Spring Boot 2.5](https://www.baeldung.com/spring-boot-env-variable-prefixes) From 6b244618b8503220a9640bc2c67249f84f1d4916 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 21 Oct 2021 22:04:21 +0800 Subject: [PATCH 28/34] Update README.md --- core-java-modules/core-java-arrays-convert/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-arrays-convert/README.md b/core-java-modules/core-java-arrays-convert/README.md index 4bd060a246..b28b97cb09 100644 --- a/core-java-modules/core-java-arrays-convert/README.md +++ b/core-java-modules/core-java-arrays-convert/README.md @@ -5,3 +5,4 @@ This module contains articles about arrays conversion in Java ## Relevant Articles - [Convert a Float to a Byte Array in Java](https://www.baeldung.com/java-convert-float-to-byte-array) - [Converting Between Stream and Array in Java](https://www.baeldung.com/java-stream-to-array) +- [Convert a Byte Array to a Numeric Representation in Java](https://www.baeldung.com/java-byte-array-to-number) From 8ae64fa5dc5ebe4f393286a7be7c775854481ce1 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 21 Oct 2021 22:06:53 +0800 Subject: [PATCH 29/34] Update README.md --- core-java-modules/core-java-string-operations-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-string-operations-3/README.md b/core-java-modules/core-java-string-operations-3/README.md index f4cde6104f..1a131c57ac 100644 --- a/core-java-modules/core-java-string-operations-3/README.md +++ b/core-java-modules/core-java-string-operations-3/README.md @@ -6,3 +6,4 @@ - [Split a String in Java and Keep the Delimiters](https://www.baeldung.com/java-split-string-keep-delimiters) - [Validate String as Filename in Java](https://www.baeldung.com/java-validate-filename) - [Count Spaces in a Java String](https://www.baeldung.com/java-string-count-spaces) +- [Remove Accents and Diacritics From a String in Java](https://www.baeldung.com/java-remove-accents-from-text) From 06afc4c7b6a02ac8f82686deb62766a94f742a84 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 21 Oct 2021 22:08:29 +0800 Subject: [PATCH 30/34] Update README.md --- testing-modules/junit-5-advanced/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/testing-modules/junit-5-advanced/README.md b/testing-modules/junit-5-advanced/README.md index 5d70e6f058..7790cb6770 100644 --- a/testing-modules/junit-5-advanced/README.md +++ b/testing-modules/junit-5-advanced/README.md @@ -4,3 +4,4 @@ - [JUnit Custom Display Name Generator API](https://www.baeldung.com/junit-custom-display-name-generator) - [@TestInstance Annotation in JUnit 5](https://www.baeldung.com/junit-testinstance-annotation) - [Run JUnit Test Cases From the Command Line](https://www.baeldung.com/junit-run-from-command-line) +- [Parallel Test Execution for JUnit 5](https://www.baeldung.com/junit-5-parallel-tests) From 70d973a22fedf0de6a0dd7ed77c501a5c6e03a01 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 21 Oct 2021 22:10:30 +0800 Subject: [PATCH 31/34] Create README.md --- rule-engines/evrete/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 rule-engines/evrete/README.md diff --git a/rule-engines/evrete/README.md b/rule-engines/evrete/README.md new file mode 100644 index 0000000000..aa9a3a4b9d --- /dev/null +++ b/rule-engines/evrete/README.md @@ -0,0 +1,3 @@ +## Relevant Articles: + +- [Introduction to the Evrete Rule Engine](https://www.baeldung.com/java-evrete-rule-engine) From 6194ed1d086eb678d33473930fe648db17ae4fc3 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 21 Oct 2021 22:12:41 +0800 Subject: [PATCH 32/34] Update README.md --- core-java-modules/core-java-17/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-17/README.md b/core-java-modules/core-java-17/README.md index 798fd3a903..074c5e4f86 100644 --- a/core-java-modules/core-java-17/README.md +++ b/core-java-modules/core-java-17/README.md @@ -1,3 +1,3 @@ ### Relevant articles: -- TODO \ No newline at end of file +- [Pattern Matching for Switch](https://www.baeldung.com/java-switch-pattern-matching) From a591d1ff74cdd114686a1b01f72b1efc0d3bf8ac Mon Sep 17 00:00:00 2001 From: kwoyke Date: Thu, 21 Oct 2021 22:30:52 +0200 Subject: [PATCH 33/34] JAVA-7662: Upgrade jmh-core and jmh-generator dependencies to 1.33 (#11311) --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 1e26d09906..7263c95599 100644 --- a/pom.xml +++ b/pom.xml @@ -1403,8 +1403,8 @@ 1.8 1.2.17 2.2.2.0 - 1.28 - 1.28 + 1.33 + 1.33 2.21.0 2.11.0 2.6 From e944857c0589cb4309429b7f02a72860b3e0d983 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Thu, 21 Oct 2021 22:48:44 +0200 Subject: [PATCH 34/34] JAVA-7659: Upgrade byte-buddy to 1.11.20 (#11319) --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 7263c95599..4709d4a86a 100644 --- a/pom.xml +++ b/pom.xml @@ -1389,7 +1389,7 @@ 2.2 1.3 3.3.0 - 1.10.22 + 1.11.20 1.7.30