From 812057b43cad1b07aa2133898e608cd04b8048ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer?= <59844362+omertopuz@users.noreply.github.com> Date: Sat, 5 Jun 2021 23:04:19 +0300 Subject: [PATCH 001/118] Update Graph.java for dfsWithoutRecursion, when a node popped from the stack it should be checked that the current node is visited or not. Since stack has duplicated nodes, some nodes is visired doubly. --- .../main/java/com/baeldung/algorithms/dfs/Graph.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/algorithms-searching/src/main/java/com/baeldung/algorithms/dfs/Graph.java b/algorithms-searching/src/main/java/com/baeldung/algorithms/dfs/Graph.java index d2cc723cf9..4623dbb7a4 100644 --- a/algorithms-searching/src/main/java/com/baeldung/algorithms/dfs/Graph.java +++ b/algorithms-searching/src/main/java/com/baeldung/algorithms/dfs/Graph.java @@ -29,11 +29,13 @@ public class Graph { stack.push(start); while (!stack.isEmpty()) { int current = stack.pop(); - isVisited[current] = true; - visit(current); - for (int dest : adjVertices.get(current)) { - if (!isVisited[dest]) - stack.push(dest); + if(!isVisited[current]){ + isVisited[current] = true; + visit(current); + for (int dest : adjVertices.get(current)) { + if (!isVisited[dest]) + stack.push(dest); + } } } } From a20b32c41313210258b1e4870bf462213152983c Mon Sep 17 00:00:00 2001 From: rmkellogg Date: Tue, 27 Jul 2021 15:30:33 -0400 Subject: [PATCH 002/118] Added comments on settings. --- .../src/main/resources/application.properties | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spring-security-modules/spring-security-saml/src/main/resources/application.properties b/spring-security-modules/spring-security-saml/src/main/resources/application.properties index f9d6a5df3c..1d93a12737 100644 --- a/spring-security-modules/spring-security-saml/src/main/resources/application.properties +++ b/spring-security-modules/spring-security-saml/src/main/resources/application.properties @@ -1,6 +1,8 @@ saml.keystore.location=classpath:/saml/samlKeystore.jks +# Password for Java keystore and item therein saml.keystore.password= saml.keystore.alias= +# SAML Entity ID extracted from top of SAML metadata file saml.idp= saml.sp=http://localhost:8080/saml/metadata \ No newline at end of file From 9c3bf05bb32be6828ee15cbe10eebce53c1aedbb Mon Sep 17 00:00:00 2001 From: rmkellogg Date: Tue, 27 Jul 2021 15:32:47 -0400 Subject: [PATCH 003/118] Correction to allow execution outside of Eclipse. --- .../main/java/com/baeldung/saml/config/SamlSecurityConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-security-modules/spring-security-saml/src/main/java/com/baeldung/saml/config/SamlSecurityConfig.java b/spring-security-modules/spring-security-saml/src/main/java/com/baeldung/saml/config/SamlSecurityConfig.java index 378db478cf..8bc97cb81e 100644 --- a/spring-security-modules/spring-security-saml/src/main/java/com/baeldung/saml/config/SamlSecurityConfig.java +++ b/spring-security-modules/spring-security-saml/src/main/java/com/baeldung/saml/config/SamlSecurityConfig.java @@ -143,7 +143,7 @@ public class SamlSecurityConfig { public ExtendedMetadataDelegate oktaExtendedMetadataProvider() throws MetadataProviderException { File metadata = null; try { - metadata = new File("./src/main/resources/saml/metadata/sso.xml"); + metadata = new ClassPathResource("saml/metadata/sso.xml").getFile(); } catch (Exception e) { e.printStackTrace(); } From 404c05ad321838450de24285392d3e47c238aaee Mon Sep 17 00:00:00 2001 From: rmkellogg Date: Tue, 27 Jul 2021 15:39:59 -0400 Subject: [PATCH 004/118] Added missing import. --- .../main/java/com/baeldung/saml/config/SamlSecurityConfig.java | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-modules/spring-security-saml/src/main/java/com/baeldung/saml/config/SamlSecurityConfig.java b/spring-security-modules/spring-security-saml/src/main/java/com/baeldung/saml/config/SamlSecurityConfig.java index 8bc97cb81e..7c6f5defdf 100644 --- a/spring-security-modules/spring-security-saml/src/main/java/com/baeldung/saml/config/SamlSecurityConfig.java +++ b/spring-security-modules/spring-security-saml/src/main/java/com/baeldung/saml/config/SamlSecurityConfig.java @@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.security.saml.*; From dddf541bb863d75d02f456b9bcdcb8d7dbff2ee1 Mon Sep 17 00:00:00 2001 From: rmkellogg Date: Mon, 2 Aug 2021 16:05:00 -0400 Subject: [PATCH 005/118] Revised to use built in Sprint Security SAML resource resolution. Otherwise when used from Spring Boot self-contained JAR the File could not be located. --- .../saml/config/SamlSecurityConfig.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/spring-security-modules/spring-security-saml/src/main/java/com/baeldung/saml/config/SamlSecurityConfig.java b/spring-security-modules/spring-security-saml/src/main/java/com/baeldung/saml/config/SamlSecurityConfig.java index 7c6f5defdf..10e37b346f 100644 --- a/spring-security-modules/spring-security-saml/src/main/java/com/baeldung/saml/config/SamlSecurityConfig.java +++ b/spring-security-modules/spring-security-saml/src/main/java/com/baeldung/saml/config/SamlSecurityConfig.java @@ -5,17 +5,19 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Timer; import org.opensaml.saml2.metadata.provider.FilesystemMetadataProvider; import org.opensaml.saml2.metadata.provider.MetadataProvider; import org.opensaml.saml2.metadata.provider.MetadataProviderException; +import org.opensaml.saml2.metadata.provider.ResourceBackedMetadataProvider; +import org.opensaml.util.resource.ClasspathResource; import org.opensaml.util.resource.ResourceException; import org.opensaml.xml.parse.StaticBasicParserPool; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.security.saml.*; @@ -142,13 +144,19 @@ public class SamlSecurityConfig { @Bean @Qualifier("okta") public ExtendedMetadataDelegate oktaExtendedMetadataProvider() throws MetadataProviderException { - File metadata = null; - try { - metadata = new ClassPathResource("saml/metadata/sso.xml").getFile(); - } catch (Exception e) { - e.printStackTrace(); - } - FilesystemMetadataProvider provider = new FilesystemMetadataProvider(metadata); + // Use the Spring Security SAML resource mechanism to load + // metadata from the Java classpath. This works from Spring Boot + // self contained JAR file. + org.opensaml.util.resource.Resource resource = null; + + try { + resource = new ClasspathResource("/saml/metadata/sso.xml"); + } catch (ResourceException e) { + e.printStackTrace(); + } + + Timer timer = new Timer("saml-metadata"); + ResourceBackedMetadataProvider provider = new ResourceBackedMetadataProvider(timer,resource); provider.setParserPool(parserPool()); return new ExtendedMetadataDelegate(provider, extendedMetadata()); } From dcf8b902dcfe260193e82bd08d2593e1d11e133e Mon Sep 17 00:00:00 2001 From: Ashish Gupta <30566001+gupta-ashu01@users.noreply.github.com> Date: Sat, 7 Aug 2021 12:33:12 +0530 Subject: [PATCH 006/118] Update pom.xml --- core-java-modules/core-java-string-algorithms-3/pom.xml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/core-java-modules/core-java-string-algorithms-3/pom.xml b/core-java-modules/core-java-string-algorithms-3/pom.xml index c8243258c1..6d1c0b0e48 100644 --- a/core-java-modules/core-java-string-algorithms-3/pom.xml +++ b/core-java-modules/core-java-string-algorithms-3/pom.xml @@ -32,6 +32,11 @@ junit-jupiter test + + commons-validator + commons-validator + ${validator.version} + @@ -59,6 +64,7 @@ 3.6.1 28.1-jre + 1.7 - \ No newline at end of file + From 6b7dcdf847e0b7875f97c5b80760d2d6ba36c70c Mon Sep 17 00:00:00 2001 From: Ashish Gupta <30566001+gupta-ashu01@users.noreply.github.com> Date: Sat, 7 Aug 2021 12:34:38 +0530 Subject: [PATCH 007/118] Add files via upload --- .../emailvalidation/EmailValidation.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java diff --git a/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java b/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java new file mode 100644 index 0000000000..97d1b90ff3 --- /dev/null +++ b/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java @@ -0,0 +1,64 @@ +package com.baeldung.emailvalidation; + +import java.util.regex.Pattern; + +public class EmailValidation { + + private static String regexPattern; + + public static boolean usingSimpleRegex(String emailAddress) { + regexPattern = "^(.+)@(\\S+)$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } + + public static boolean usingStrictRegex(String emailAddress) { + regexPattern = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@" + "[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } + + public static boolean usingUnicodeRegex(String emailAddress) { + regexPattern = "^(?=.{1,64}@)[\\p{L}0-9_-]+(\\.[\\p{L}0-9_-]+)*@" + "[^-][\\p{L}0-9-]+(\\.[\\p{L}0-9-]+)*(\\.[\\p{L}]{2,})$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } + + public static boolean usingRFC5322Regex(String emailAddress) { + regexPattern = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } + + public static boolean restrictDots(String emailAddress) { + regexPattern = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@" + "[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } + + public static boolean owaspValidation(String emailAddress) { + regexPattern = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } + + public static boolean topLevelDomain(String emailAddress) { + regexPattern = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*" + "@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } +} \ No newline at end of file From 0d22a1fdeea1cdd70c9fbac829d514dbb586cd49 Mon Sep 17 00:00:00 2001 From: Ashish Gupta <30566001+gupta-ashu01@users.noreply.github.com> Date: Sat, 7 Aug 2021 12:38:09 +0530 Subject: [PATCH 008/118] Add files via upload --- .../EmailValidationUnitTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java diff --git a/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java new file mode 100644 index 0000000000..3b2b77fc4a --- /dev/null +++ b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java @@ -0,0 +1,59 @@ +package com.baeldung.emailvalidation; + +import static org.junit.Assert.assertEquals; +import org.apache.commons.validator.routines.EmailValidator; +import org.junit.Test; + +public class EmailValidationUnitTest { + + private String emailAddress; + + @Test + public void testUsingEmailValidator() { + emailAddress = "username@domain.com"; + assertEquals(EmailValidator.getInstance() + .isValid(emailAddress), true); + } + + @Test + public void testUsingSimpleRegex() { + emailAddress = "username@domain.com"; + assertEquals(EmailValidation.usingSimpleRegex(emailAddress), true); + } + + @Test + public void testUsingStrictRegex() { + emailAddress = "username@domain.com"; + assertEquals(EmailValidation.usingStrictRegex(emailAddress), true); + } + + @Test + public void testUsingUnicodeRegex() { + emailAddress = "用户名@领域.电脑"; + assertEquals(EmailValidation.usingUnicodeRegex(emailAddress), true); + } + + @Test + public void testUsingRFC5322Regex() { + emailAddress = "username@domain.com"; + assertEquals(EmailValidation.usingRFC5322Regex(emailAddress), true); + } + + @Test + public void testRestrictDots() { + emailAddress = "username@domain.com"; + assertEquals(EmailValidation.restrictDots(emailAddress), true); + } + + @Test + public void testOwaspValidation() { + emailAddress = "username@domain.com"; + assertEquals(EmailValidation.owaspValidation(emailAddress), true); + } + + @Test + public void testTopLevelDomain() { + emailAddress = "username@domain.com"; + assertEquals(EmailValidation.topLevelDomain(emailAddress), true); + } +} \ No newline at end of file From 75e9acc874ef3a54b0c590080af95b7b2874b774 Mon Sep 17 00:00:00 2001 From: Trixi Turny Date: Sun, 1 Aug 2021 14:26:57 +0100 Subject: [PATCH 009/118] BAEL-5054 2 examples of file upload with webclient --- webclient_upload/pom.xml | 76 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 webclient_upload/pom.xml diff --git a/webclient_upload/pom.xml b/webclient_upload/pom.xml new file mode 100644 index 0000000000..69f7f78cd1 --- /dev/null +++ b/webclient_upload/pom.xml @@ -0,0 +1,76 @@ + + + + 4.0.0 + + com.baeldung + webclient_upload + 1.0-SNAPSHOT + jar + + + + org.springframework.boot + spring-boot-starter-parent + 2.5.0 + + + webclient_upload Maven Webapp + + 16 + 1.0.1.RELEASE + + + + + + + org.projectreactor + reactor-spring + ${reactor-spring.version} + + + + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-webflux + + + org.projectreactor + reactor-spring + + + org.springframework.boot + spring-boot-starter-test + test + + + io.rest-assured + json-schema-validator + test + + + org.springframework.boot + spring-boot-starter-web + + + + + From 6d75bb0cc1751ccc2d35b2b2f4347909c6461cfa Mon Sep 17 00:00:00 2001 From: Ashish Gupta <30566001+gupta-ashu01@users.noreply.github.com> Date: Mon, 16 Aug 2021 11:50:58 +0530 Subject: [PATCH 010/118] Update EmailValidation.java --- .../emailvalidation/EmailValidation.java | 56 +------------------ 1 file changed, 2 insertions(+), 54 deletions(-) diff --git a/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java b/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java index 97d1b90ff3..4d68ea9c55 100644 --- a/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java +++ b/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java @@ -4,61 +4,9 @@ import java.util.regex.Pattern; public class EmailValidation { - private static String regexPattern; - - public static boolean usingSimpleRegex(String emailAddress) { - regexPattern = "^(.+)@(\\S+)$"; - + public static boolean patternMatcher(String emailAddress, String regexPattern) { return Pattern.compile(regexPattern) .matcher(emailAddress) .matches(); } - - public static boolean usingStrictRegex(String emailAddress) { - regexPattern = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@" + "[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"; - - return Pattern.compile(regexPattern) - .matcher(emailAddress) - .matches(); - } - - public static boolean usingUnicodeRegex(String emailAddress) { - regexPattern = "^(?=.{1,64}@)[\\p{L}0-9_-]+(\\.[\\p{L}0-9_-]+)*@" + "[^-][\\p{L}0-9-]+(\\.[\\p{L}0-9-]+)*(\\.[\\p{L}]{2,})$"; - - return Pattern.compile(regexPattern) - .matcher(emailAddress) - .matches(); - } - - public static boolean usingRFC5322Regex(String emailAddress) { - regexPattern = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$"; - - return Pattern.compile(regexPattern) - .matcher(emailAddress) - .matches(); - } - - public static boolean restrictDots(String emailAddress) { - regexPattern = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@" + "[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"; - - return Pattern.compile(regexPattern) - .matcher(emailAddress) - .matches(); - } - - public static boolean owaspValidation(String emailAddress) { - regexPattern = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; - - return Pattern.compile(regexPattern) - .matcher(emailAddress) - .matches(); - } - - public static boolean topLevelDomain(String emailAddress) { - regexPattern = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*" + "@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"; - - return Pattern.compile(regexPattern) - .matcher(emailAddress) - .matches(); - } -} \ No newline at end of file +} From e817cffca0bd9c33b132e45636746bc2f2782054 Mon Sep 17 00:00:00 2001 From: Ashish Gupta <30566001+gupta-ashu01@users.noreply.github.com> Date: Mon, 16 Aug 2021 11:51:31 +0530 Subject: [PATCH 011/118] Update EmailValidationUnitTest.java --- .../EmailValidationUnitTest.java | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java index 3b2b77fc4a..b387cc9dc5 100644 --- a/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java +++ b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java @@ -1,59 +1,72 @@ package com.baeldung.emailvalidation; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import org.apache.commons.validator.routines.EmailValidator; import org.junit.Test; public class EmailValidationUnitTest { private String emailAddress; + private String regexPattern; @Test public void testUsingEmailValidator() { emailAddress = "username@domain.com"; - assertEquals(EmailValidator.getInstance() - .isValid(emailAddress), true); + assertTrue(EmailValidator.getInstance() + .isValid(emailAddress)); } @Test public void testUsingSimpleRegex() { emailAddress = "username@domain.com"; - assertEquals(EmailValidation.usingSimpleRegex(emailAddress), true); + regexPattern = "^(.+)@(\\S+)$"; + assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); } @Test public void testUsingStrictRegex() { emailAddress = "username@domain.com"; - assertEquals(EmailValidation.usingStrictRegex(emailAddress), true); + regexPattern = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@" + + "[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"; + assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); } @Test public void testUsingUnicodeRegex() { emailAddress = "用户名@领域.电脑"; - assertEquals(EmailValidation.usingUnicodeRegex(emailAddress), true); + regexPattern = "^(?=.{1,64}@)[\\p{L}0-9_-]+(\\.[\\p{L}0-9_-]+)*@" + + "[^-][\\p{L}0-9-]+(\\.[\\p{L}0-9-]+)*(\\.[\\p{L}]{2,})$"; + assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); } @Test public void testUsingRFC5322Regex() { emailAddress = "username@domain.com"; - assertEquals(EmailValidation.usingRFC5322Regex(emailAddress), true); + regexPattern = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$"; + assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); } @Test public void testRestrictDots() { emailAddress = "username@domain.com"; - assertEquals(EmailValidation.restrictDots(emailAddress), true); + regexPattern = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@" + + "[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"; + assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); } @Test public void testOwaspValidation() { emailAddress = "username@domain.com"; - assertEquals(EmailValidation.owaspValidation(emailAddress), true); + regexPattern = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; + assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); } @Test public void testTopLevelDomain() { emailAddress = "username@domain.com"; - assertEquals(EmailValidation.topLevelDomain(emailAddress), true); + regexPattern = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*" + + "@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"; + assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); } -} \ No newline at end of file +} From 7caefe62b70c637fd4c933c5ce5a89b51926c6c5 Mon Sep 17 00:00:00 2001 From: Ashish Gupta <30566001+gupta-ashu01@users.noreply.github.com> Date: Mon, 16 Aug 2021 11:58:55 +0530 Subject: [PATCH 012/118] Update EmailValidationUnitTest.java --- .../com/baeldung/emailvalidation/EmailValidationUnitTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java index b387cc9dc5..c49690289a 100644 --- a/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java +++ b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java @@ -1,7 +1,6 @@ package com.baeldung.emailvalidation; import static org.junit.Assert.assertTrue; - import org.apache.commons.validator.routines.EmailValidator; import org.junit.Test; From 59355e07cc73bbfbca4a4d3ecec931c0c48cc74c Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Fri, 20 Aug 2021 10:54:24 +0300 Subject: [PATCH 013/118] JAVA-6511 update readme, add context test --- .../spring-data-mongodb-reactive/README.md | 4 +--- spring-5-data-reactive/README.md | 2 -- .../java/com/baeldung/SpringContextTest.java | 17 +++++++++++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 spring-5-data-reactive/src/test/java/com/baeldung/SpringContextTest.java diff --git a/persistence-modules/spring-data-mongodb-reactive/README.md b/persistence-modules/spring-data-mongodb-reactive/README.md index 0931161700..0d80fc8b92 100644 --- a/persistence-modules/spring-data-mongodb-reactive/README.md +++ b/persistence-modules/spring-data-mongodb-reactive/README.md @@ -7,6 +7,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles - [Spring Data Reactive Repositories with MongoDB](https://www.baeldung.com/spring-data-mongodb-reactive) -- [Spring Data MongoDB Tailable Cursors](https://www.baeldung.com/spring-data-mongodb-tailable-cursors) -- [A Quick Look at R2DBC with Spring Data](https://www.baeldung.com/spring-data-r2dbc) -- [Spring Data Reactive Repositories with Couchbase](https://www.baeldung.com/spring-data-reactive-couchbase) +- [Spring Data MongoDB Tailable Cursors](https://www.baeldung.com/spring-data-mongodb-tailable-cursors) \ No newline at end of file diff --git a/spring-5-data-reactive/README.md b/spring-5-data-reactive/README.md index 0931161700..ecb6d01267 100644 --- a/spring-5-data-reactive/README.md +++ b/spring-5-data-reactive/README.md @@ -6,7 +6,5 @@ This module contains articles about reactive Spring 5 Data The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles -- [Spring Data Reactive Repositories with MongoDB](https://www.baeldung.com/spring-data-mongodb-reactive) -- [Spring Data MongoDB Tailable Cursors](https://www.baeldung.com/spring-data-mongodb-tailable-cursors) - [A Quick Look at R2DBC with Spring Data](https://www.baeldung.com/spring-data-r2dbc) - [Spring Data Reactive Repositories with Couchbase](https://www.baeldung.com/spring-data-reactive-couchbase) diff --git a/spring-5-data-reactive/src/test/java/com/baeldung/SpringContextTest.java b/spring-5-data-reactive/src/test/java/com/baeldung/SpringContextTest.java new file mode 100644 index 0000000000..dc7bcd1e37 --- /dev/null +++ b/spring-5-data-reactive/src/test/java/com/baeldung/SpringContextTest.java @@ -0,0 +1,17 @@ +package com.baeldung; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.r2dbc.R2dbcApplication; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = R2dbcApplication.class) +public class SpringContextTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } +} From b8f15c88fadf66b3223824cc208be3b1275e05bb Mon Sep 17 00:00:00 2001 From: rvsathe Date: Tue, 24 Aug 2021 09:22:12 +0530 Subject: [PATCH 014/118] commits for BAEL 4293 --- core-java-modules/core-java-os/pom.xml | 6 + .../com/baeldung/example/soundapi/App.java | 34 +++++ .../soundapi/ApplicationProperties.java | 10 ++ .../example/soundapi/SoundRecorder.java | 123 ++++++++++++++++++ .../example/soundapi/WaveDataUtil.java | 34 +++++ .../baeldung/example/soundapi/AppTest.java | 75 +++++++++++ 6 files changed, 282 insertions(+) create mode 100644 core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/App.java create mode 100644 core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/ApplicationProperties.java create mode 100644 core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/SoundRecorder.java create mode 100644 core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/WaveDataUtil.java create mode 100644 core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppTest.java diff --git a/core-java-modules/core-java-os/pom.xml b/core-java-modules/core-java-os/pom.xml index 5f00b709e3..04db6a53f3 100644 --- a/core-java-modules/core-java-os/pom.xml +++ b/core-java-modules/core-java-os/pom.xml @@ -16,6 +16,12 @@ + + org.junit.jupiter + junit-jupiter-engine + 5.7.2 + test + org.apache.commons commons-collections4 diff --git a/core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/App.java b/core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/App.java new file mode 100644 index 0000000000..afd7687528 --- /dev/null +++ b/core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/App.java @@ -0,0 +1,34 @@ +package com.baeldung.example.soundapi; + +import javax.sound.sampled.AudioFileFormat; +import javax.sound.sampled.AudioFormat; + +public class App { + public static void main(String[] args) throws Exception { + + AudioFormat format = buildAudioFormatInstance(); + + SoundRecorder soundRecorder = new SoundRecorder(); + soundRecorder.build(format); + + System.out.println("Start recording ...."); + soundRecorder.start(); + Thread.sleep(20000); + soundRecorder.stop(); + + WaveDataUtil wd = new WaveDataUtil(); + Thread.sleep(3000); + wd.saveToFile("/SoundClip", AudioFileFormat.Type.WAVE, soundRecorder.getAudioInputStream()); + } + + public static AudioFormat buildAudioFormatInstance() { + ApplicationProperties aConstants = new ApplicationProperties(); + AudioFormat.Encoding encoding = aConstants.ENCODING; + float rate = aConstants.RATE; + int channels = aConstants.CHANNELS; + int sampleSize = aConstants.SAMPLE_SIZE; + boolean bigEndian = aConstants.BIG_ENDIAN; + + return new AudioFormat(encoding, rate, sampleSize, channels, (sampleSize / 8) * channels, rate, bigEndian); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/ApplicationProperties.java b/core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/ApplicationProperties.java new file mode 100644 index 0000000000..985229b969 --- /dev/null +++ b/core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/ApplicationProperties.java @@ -0,0 +1,10 @@ +package com.baeldung.example.soundapi; +import javax.sound.sampled.AudioFormat; + +public class ApplicationProperties { + public final AudioFormat.Encoding ENCODING = AudioFormat.Encoding.PCM_SIGNED; + public final float RATE = 44100.0f; + public final int CHANNELS = 1; + public final int SAMPLE_SIZE = 16; + public final boolean BIG_ENDIAN = true; +} \ No newline at end of file diff --git a/core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/SoundRecorder.java b/core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/SoundRecorder.java new file mode 100644 index 0000000000..cae91ee74f --- /dev/null +++ b/core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/SoundRecorder.java @@ -0,0 +1,123 @@ +package com.baeldung.example.soundapi; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.TargetDataLine; + +public class SoundRecorder implements Runnable { + private AudioInputStream audioInputStream; + private AudioFormat format; + public Thread thread; + private double duration; + + public SoundRecorder() { + super(); + } + + public SoundRecorder(AudioFormat format) { + this.format = format; + } + + public SoundRecorder build(AudioFormat format) { + this.format = format; + return this; + } + + public void start() { + thread = new Thread(this); + thread.setName("Capture Microphone"); + thread.start(); + } + + public void stop() { + thread = null; + } + + @Override + public void run() { + duration = 0; + + try (final ByteArrayOutputStream out = new ByteArrayOutputStream(); final TargetDataLine line = getTargetDataLineForRecord();) { + + int frameSizeInBytes = format.getFrameSize(); + int bufferLengthInFrames = line.getBufferSize() / 8; + final int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes; + buildByteOutputStream(out, line, frameSizeInBytes, bufferLengthInBytes); + this.audioInputStream = new AudioInputStream(line); + setAudioInputStream(convertToAudioIStream(out, frameSizeInBytes)); + audioInputStream.reset(); + } catch (IOException ex) { + ex.printStackTrace(); + } catch (Exception ex) { + ex.printStackTrace(); + return; + } + } + + public void buildByteOutputStream(final ByteArrayOutputStream out, final TargetDataLine line, int frameSizeInBytes, final int bufferLengthInBytes) throws IOException { + final byte[] data = new byte[bufferLengthInBytes]; + int numBytesRead; + + line.start(); + while (thread != null) { + if ((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1) { + break; + } + out.write(data, 0, numBytesRead); + } + } + + private void setAudioInputStream(AudioInputStream aStream) { + this.audioInputStream = aStream; + } + + public AudioInputStream convertToAudioIStream(final ByteArrayOutputStream out, int frameSizeInBytes) { + byte audioBytes[] = out.toByteArray(); + ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes); + AudioInputStream audioStream = new AudioInputStream(bais, format, audioBytes.length / frameSizeInBytes); + long milliseconds = (long) ((audioInputStream.getFrameLength() * 1000) / format.getFrameRate()); + duration = milliseconds / 1000.0; + System.out.println("Recorded duration in seconds:" + duration); + return audioStream; + } + + public TargetDataLine getTargetDataLineForRecord() { + TargetDataLine line; + DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); + if (!AudioSystem.isLineSupported(info)) { + return null; + } + try { + line = (TargetDataLine) AudioSystem.getLine(info); + line.open(format, line.getBufferSize()); + } catch (final Exception ex) { + return null; + } + return line; + } + + public AudioInputStream getAudioInputStream() { + return audioInputStream; + } + + public AudioFormat getFormat() { + return format; + } + + public void setFormat(AudioFormat format) { + this.format = format; + } + + public Thread getThread() { + return thread; + } + + public double getDuration() { + return duration; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/WaveDataUtil.java b/core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/WaveDataUtil.java new file mode 100644 index 0000000000..7d63e66fdd --- /dev/null +++ b/core-java-modules/core-java-os/src/main/java/com/baeldung/example/soundapi/WaveDataUtil.java @@ -0,0 +1,34 @@ +package com.baeldung.example.soundapi; + +import java.io.File; + +import javax.sound.sampled.AudioFileFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; + +public class WaveDataUtil { + public boolean saveToFile(String name, AudioFileFormat.Type fileType, AudioInputStream audioInputStream) { + System.out.println("Saving..."); + if (null == name || null == fileType || audioInputStream == null) { + return false; + } + File myFile = new File(name + "." + fileType.getExtension()); + try { + audioInputStream.reset(); + } catch (Exception e) { + return false; + } + int i = 0; + while (myFile.exists()) { + String temp = "" + i + myFile.getName(); + myFile = new File(temp); + } + try { + AudioSystem.write(audioInputStream, fileType, myFile); + } catch (Exception ex) { + return false; + } + System.out.println("Saved " + myFile.getAbsolutePath()); + return true; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppTest.java b/core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppTest.java new file mode 100644 index 0000000000..ff00523eec --- /dev/null +++ b/core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppTest.java @@ -0,0 +1,75 @@ +package com.baeldung.example.soundapi; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import java.io.ByteArrayOutputStream; + +import javax.sound.sampled.AudioFileFormat; +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.TargetDataLine; + +public class AppTest { + + AudioFormat af = App.buildAudioFormatInstance(); + SoundRecorder soundRecorder = new SoundRecorder(); + + @Test + public void when_run_save_SoundClip() { + + soundRecorder.build(af); + try { + soundRecorder.start(); + Thread.sleep(20000); + soundRecorder.stop(); + } catch (InterruptedException ex) { + fail("Exception: " + ex); + } + + } + + @Test + public void when_run_get_targetdataline() { + soundRecorder.setFormat(af); + Assertions.assertDoesNotThrow(() -> soundRecorder.getTargetDataLineForRecord()); + } + + @Test + public void when_run_build_byte_ouptstream() { + + soundRecorder.setFormat(af); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + TargetDataLine tLine = soundRecorder.getTargetDataLineForRecord(); + + int frameSizeInBytes = af.getFrameSize(); + int bufferLengthInFrames = tLine.getBufferSize() / 8; + final int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes; + + Assertions.assertDoesNotThrow( + () -> soundRecorder.buildByteOutputStream(out, tLine, frameSizeInBytes, bufferLengthInBytes)); + } + + @Test + public void when_run_then_save_file() { + soundRecorder.setFormat(af); + soundRecorder.build(af); + try { + soundRecorder.start(); + Thread.sleep(20000); + soundRecorder.stop(); + WaveDataUtil wd = new WaveDataUtil(); + Thread.sleep(3000); + boolean saveFile = wd.saveToFile("/SoundClip", AudioFileFormat.Type.WAVE, + soundRecorder.getAudioInputStream()); + + assertEquals(saveFile, true); + + } catch (InterruptedException ex) { + fail("Exception: " + ex); + } + + } + +} From 6d979cf2e34860d7b33dd00ad5eb91a214ff6aa2 Mon Sep 17 00:00:00 2001 From: mikr Date: Tue, 24 Aug 2021 10:17:23 +0200 Subject: [PATCH 015/118] JAVA-3587 Update Jackson version in the main pom --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f56006713a..fb2c140992 100644 --- a/pom.xml +++ b/pom.xml @@ -1408,7 +1408,7 @@ 1.2 2.3.1 1.2 - 2.11.1 + 2.12.4 1.4 1.2.0 5.2.0 From de5c114c227765da06b8d363f7382822b089dd82 Mon Sep 17 00:00:00 2001 From: mikr Date: Wed, 25 Aug 2021 23:00:50 +0200 Subject: [PATCH 016/118] JAVA-3587 Update Jackson version in the main pom --- jackson-modules/jackson-conversions-2/pom.xml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/jackson-modules/jackson-conversions-2/pom.xml b/jackson-modules/jackson-conversions-2/pom.xml index 799fcb106a..a498c8b4f8 100644 --- a/jackson-modules/jackson-conversions-2/pom.xml +++ b/jackson-modules/jackson-conversions-2/pom.xml @@ -24,7 +24,7 @@ com.fasterxml.jackson.datatype jackson-datatype-jsr310 - ${jackson-datatype.version} + ${jackson.version} @@ -52,7 +52,6 @@ 3.11.0 - 2.9.8 \ No newline at end of file From 4a8956e7a97de63901360acd2cf137a4d82dda3a Mon Sep 17 00:00:00 2001 From: mikr Date: Thu, 26 Aug 2021 08:18:11 +0200 Subject: [PATCH 017/118] JAVA-3587 Ignore failing test (unrelated to Jackson version change) --- apache-libraries/pom.xml | 5 +++++ .../com/baeldung/apache/beam/intro/WordCountUnitTest.java | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/apache-libraries/pom.xml b/apache-libraries/pom.xml index ded10b939f..bca80d9acd 100644 --- a/apache-libraries/pom.xml +++ b/apache-libraries/pom.xml @@ -133,6 +133,11 @@ jackson-databind ${jackson.version} + + com.fasterxml.jackson.core + jackson-annotations + ${jackson.version} + com.jayway.awaitility awaitility diff --git a/apache-libraries/src/test/java/com/baeldung/apache/beam/intro/WordCountUnitTest.java b/apache-libraries/src/test/java/com/baeldung/apache/beam/intro/WordCountUnitTest.java index f2558635dc..805f79742d 100644 --- a/apache-libraries/src/test/java/com/baeldung/apache/beam/intro/WordCountUnitTest.java +++ b/apache-libraries/src/test/java/com/baeldung/apache/beam/intro/WordCountUnitTest.java @@ -9,8 +9,8 @@ import com.baeldung.apache.beam.intro.WordCount; public class WordCountUnitTest { - @Test - // @Ignore +// @Test + @Ignore public void givenInputFile_whenWordCountRuns_thenJobFinishWithoutError() { boolean jobDone = WordCount.wordCount("src/test/resources/wordcount.txt", "target/output"); assertTrue(jobDone); From 638d34924ba3cf06e6d274684f4a4a1a2a6ab720 Mon Sep 17 00:00:00 2001 From: Trixi Turny Date: Sun, 1 Aug 2021 14:28:51 +0100 Subject: [PATCH 018/118] BAEL-5054 make sure all files are added BAEL-5054 organise packages BAEL-5054 move project to correct folder BAEL-5044 remove old folder --- spring-5-reactive-client/pom.xml | 2 +- .../com/baeldung/reactive/Application.java | 12 +++ .../reactive/controller/UploadController.java | 47 ++++++++++++ .../service/ReactiveUploadService.java | 69 +++++++++++++++++ .../src/main/resources/application.properties | 2 +- webclient_upload/pom.xml | 76 ------------------- 6 files changed, 130 insertions(+), 78 deletions(-) create mode 100644 spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java create mode 100644 spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java create mode 100644 spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java delete mode 100644 webclient_upload/pom.xml diff --git a/spring-5-reactive-client/pom.xml b/spring-5-reactive-client/pom.xml index 136f31b49e..6a0a2d6fe3 100644 --- a/spring-5-reactive-client/pom.xml +++ b/spring-5-reactive-client/pom.xml @@ -133,7 +133,7 @@ org.springframework.boot spring-boot-maven-plugin - com.baeldung.Spring5Application + com.baeldung.reactive.Application JAR diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java new file mode 100644 index 0000000000..65e497b519 --- /dev/null +++ b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java @@ -0,0 +1,12 @@ +package com.baeldung.reactive; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} + diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java new file mode 100644 index 0000000000..71cea35685 --- /dev/null +++ b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java @@ -0,0 +1,47 @@ +package com.baeldung.reactive.controller; + + +import com.baeldung.reactive.service.ReactiveUploadService; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import reactor.core.publisher.Mono; + + +@RestController +public class UploadController { + final ReactiveUploadService uploadService; + + public UploadController(ReactiveUploadService uploadService) { + this.uploadService = uploadService; + } + + @PostMapping(path = "/upload") + @ResponseBody + public Mono uploadPdf(@RequestParam("file") final MultipartFile multipartFile) { + return uploadService.uploadPdf(multipartFile.getResource()); + } + + @PostMapping(path = "/upload/multipart") + @ResponseBody + public Mono uploadMultipart(@RequestParam("file") final MultipartFile multipartFile) { + return uploadService.uploadMultipart(multipartFile); + } + + + /** + * Fake upload endpoint returning "OK" HttpStatus + * @return "OK" HttpStatus + */ + @PostMapping(path = "/external/upload") + @ResponseBody + public HttpStatus externalUpload() { + return HttpStatus.OK; + } + + @GetMapping("/trixi") + public String returnTrixi() { + return "Trixi"; + + } +} diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java new file mode 100644 index 0000000000..e31674282b --- /dev/null +++ b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java @@ -0,0 +1,69 @@ +package com.baeldung.reactive.service; + + +import org.springframework.core.io.Resource; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.client.MultipartBodyBuilder; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.UriComponentsBuilder; +import reactor.core.publisher.Mono; + +import java.net.URI; + +@Service +public class ReactiveUploadService { + + private final WebClient webClient; + private static final String EXTERNAL_UPLOAD_URL = "http://localhost:8080/external/upload"; + + public ReactiveUploadService() { + this.webClient = WebClient.create(); + } + + + public Mono uploadPdf(final Resource resource){ + + final URI url = UriComponentsBuilder.fromHttpUrl(EXTERNAL_UPLOAD_URL).build().toUri(); + Mono httpStatusMono = webClient.post() + .uri(url) + .contentType(MediaType.APPLICATION_PDF) + .body(BodyInserters.fromResource(resource)) + .exchangeToMono(response -> { + if (response.statusCode().equals(HttpStatus.OK)) { + return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode()); + } else { + System.out.println("Failed to upload pdf. " + response.statusCode()); + } + return null; + }); + return httpStatusMono; + } + + + public Mono uploadMultipart(final MultipartFile multipartFile){ + final URI url = UriComponentsBuilder.fromHttpUrl(EXTERNAL_UPLOAD_URL).build().toUri(); + + final MultipartBodyBuilder builder = new MultipartBodyBuilder(); + builder.part("file", multipartFile.getResource()); + + Mono httpStatusMono = webClient.post() + .uri(url) + .contentType(MediaType.MULTIPART_FORM_DATA) + .body(BodyInserters.fromMultipartData(builder.build())) + .exchangeToMono(response -> { + if (response.statusCode().equals(HttpStatus.OK)) { + return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode()); + } else { + System.out.println("Failed to upload pdf. " + response.statusCode()); + } + return null; + }); + return httpStatusMono; + } + + +} diff --git a/spring-5-reactive-client/src/main/resources/application.properties b/spring-5-reactive-client/src/main/resources/application.properties index 05033054b1..7859537230 100644 --- a/spring-5-reactive-client/src/main/resources/application.properties +++ b/spring-5-reactive-client/src/main/resources/application.properties @@ -1,5 +1,5 @@ logging.level.root=INFO -server.port=8081 +server.port=8080 logging.level.reactor.netty.http.client.HttpClient=DEBUG \ No newline at end of file diff --git a/webclient_upload/pom.xml b/webclient_upload/pom.xml deleted file mode 100644 index 69f7f78cd1..0000000000 --- a/webclient_upload/pom.xml +++ /dev/null @@ -1,76 +0,0 @@ - - - - 4.0.0 - - com.baeldung - webclient_upload - 1.0-SNAPSHOT - jar - - - - org.springframework.boot - spring-boot-starter-parent - 2.5.0 - - - webclient_upload Maven Webapp - - 16 - 1.0.1.RELEASE - - - - - - - org.projectreactor - reactor-spring - ${reactor-spring.version} - - - - - - - - org.springframework.boot - spring-boot-starter - - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-webflux - - - org.projectreactor - reactor-spring - - - org.springframework.boot - spring-boot-starter-test - test - - - io.rest-assured - json-schema-validator - test - - - org.springframework.boot - spring-boot-starter-web - - - - - From 82c3389ba042400766dd94f3223936729fa86533 Mon Sep 17 00:00:00 2001 From: Trixi Turny Date: Sun, 22 Aug 2021 22:08:17 +0100 Subject: [PATCH 019/118] BAEL-5054 throw ServiceException when not OK BAEL-5054 fix test name and tidy indent BAEL-5054 remove unnecessary changes BAEL-5054 restore main class name BAEL-5054 rename test to end with UnitTest --- spring-5-reactive-client/pom.xml | 2 +- .../com/baeldung/reactive/Application.java | 12 ----- .../reactive/controller/UploadController.java | 17 ------ .../reactive/exception/ServiceException.java | 8 +++ .../service/ReactiveUploadService.java | 53 +++++++++---------- .../src/main/resources/application.properties | 2 +- .../ReactiveUploadServiceUnitTest.java | 44 +++++++++++++++ 7 files changed, 79 insertions(+), 59 deletions(-) delete mode 100644 spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java create mode 100644 spring-5-reactive-client/src/main/java/com/baeldung/reactive/exception/ServiceException.java create mode 100644 spring-5-reactive-client/src/test/java/com/baeldung/reactive/service/ReactiveUploadServiceUnitTest.java diff --git a/spring-5-reactive-client/pom.xml b/spring-5-reactive-client/pom.xml index 6a0a2d6fe3..136f31b49e 100644 --- a/spring-5-reactive-client/pom.xml +++ b/spring-5-reactive-client/pom.xml @@ -133,7 +133,7 @@ org.springframework.boot spring-boot-maven-plugin - com.baeldung.reactive.Application + com.baeldung.Spring5Application JAR diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java deleted file mode 100644 index 65e497b519..0000000000 --- a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.baeldung.reactive; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class Application { - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } -} - diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java index 71cea35685..08d6ff55ef 100644 --- a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java +++ b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java @@ -27,21 +27,4 @@ public class UploadController { public Mono uploadMultipart(@RequestParam("file") final MultipartFile multipartFile) { return uploadService.uploadMultipart(multipartFile); } - - - /** - * Fake upload endpoint returning "OK" HttpStatus - * @return "OK" HttpStatus - */ - @PostMapping(path = "/external/upload") - @ResponseBody - public HttpStatus externalUpload() { - return HttpStatus.OK; - } - - @GetMapping("/trixi") - public String returnTrixi() { - return "Trixi"; - - } } diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/exception/ServiceException.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/exception/ServiceException.java new file mode 100644 index 0000000000..cd639ec1f9 --- /dev/null +++ b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/exception/ServiceException.java @@ -0,0 +1,8 @@ +package com.baeldung.reactive.exception; + +public class ServiceException extends RuntimeException{ + + public ServiceException(String message) { + super(message); + } +} diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java index e31674282b..11409ce986 100644 --- a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java +++ b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java @@ -1,6 +1,7 @@ package com.baeldung.reactive.service; +import com.baeldung.reactive.exception.ServiceException; import org.springframework.core.io.Resource; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -20,50 +21,46 @@ public class ReactiveUploadService { private final WebClient webClient; private static final String EXTERNAL_UPLOAD_URL = "http://localhost:8080/external/upload"; - public ReactiveUploadService() { - this.webClient = WebClient.create(); + public ReactiveUploadService(final WebClient webClient) { + this.webClient = webClient; } - public Mono uploadPdf(final Resource resource){ + public Mono uploadPdf(final Resource resource) { final URI url = UriComponentsBuilder.fromHttpUrl(EXTERNAL_UPLOAD_URL).build().toUri(); Mono httpStatusMono = webClient.post() - .uri(url) - .contentType(MediaType.APPLICATION_PDF) - .body(BodyInserters.fromResource(resource)) - .exchangeToMono(response -> { - if (response.statusCode().equals(HttpStatus.OK)) { - return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode()); - } else { - System.out.println("Failed to upload pdf. " + response.statusCode()); - } - return null; - }); + .uri(url) + .contentType(MediaType.APPLICATION_PDF) + .body(BodyInserters.fromResource(resource)) + .exchangeToMono(response -> { + if (response.statusCode().equals(HttpStatus.OK)) { + return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode()); + } else { + throw new ServiceException("Error uploading file"); + } + }); return httpStatusMono; } - public Mono uploadMultipart(final MultipartFile multipartFile){ + public Mono uploadMultipart(final MultipartFile multipartFile) { final URI url = UriComponentsBuilder.fromHttpUrl(EXTERNAL_UPLOAD_URL).build().toUri(); final MultipartBodyBuilder builder = new MultipartBodyBuilder(); builder.part("file", multipartFile.getResource()); Mono httpStatusMono = webClient.post() - .uri(url) - .contentType(MediaType.MULTIPART_FORM_DATA) - .body(BodyInserters.fromMultipartData(builder.build())) - .exchangeToMono(response -> { - if (response.statusCode().equals(HttpStatus.OK)) { - return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode()); - } else { - System.out.println("Failed to upload pdf. " + response.statusCode()); - } - return null; - }); + .uri(url) + .contentType(MediaType.MULTIPART_FORM_DATA) + .body(BodyInserters.fromMultipartData(builder.build())) + .exchangeToMono(response -> { + if (response.statusCode().equals(HttpStatus.OK)) { + return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode()); + } else { + throw new ServiceException("Error uploading file"); + } + }); return httpStatusMono; } - - } diff --git a/spring-5-reactive-client/src/main/resources/application.properties b/spring-5-reactive-client/src/main/resources/application.properties index 7859537230..05033054b1 100644 --- a/spring-5-reactive-client/src/main/resources/application.properties +++ b/spring-5-reactive-client/src/main/resources/application.properties @@ -1,5 +1,5 @@ logging.level.root=INFO -server.port=8080 +server.port=8081 logging.level.reactor.netty.http.client.HttpClient=DEBUG \ No newline at end of file diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/service/ReactiveUploadServiceUnitTest.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/service/ReactiveUploadServiceUnitTest.java new file mode 100644 index 0000000000..0ca59848d7 --- /dev/null +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/service/ReactiveUploadServiceUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.reactive.service; + +import org.junit.jupiter.api.Test; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpStatus; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.reactive.function.client.ClientResponse; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class ReactiveUploadServiceUnitTest { + + private static final String BASE_URL = "http://localhost:8080/external/upload"; + + final WebClient webClientMock = WebClient.builder().baseUrl(BASE_URL) + .exchangeFunction(clientRequest -> Mono.just(ClientResponse.create(HttpStatus.OK) + .header("content-type", "application/json") + .build())) + .build(); + + private final ReactiveUploadService tested = new ReactiveUploadService(webClientMock); + + @Test + void givenAPdf_whenUploadingWithWebClient_thenOK() { + final Resource file = mock(Resource.class); + final Mono result = tested.uploadPdf(file); + final HttpStatus status = result.block(); + assertThat(status).isEqualTo(HttpStatus.OK); + } + + @Test + void givenAMultipartPdf_whenUploadingWithWebClient_thenOK() { + final Resource file = mock(Resource.class); + final MultipartFile multipartFile = mock(MultipartFile.class); + when(multipartFile.getResource()).thenReturn(file); + final Mono result = tested.uploadMultipart(multipartFile); + final HttpStatus status = result.block(); + assertThat(status).isEqualTo(HttpStatus.OK); + } +} \ No newline at end of file From 06adaba4bff95db43cd45eac8b7b3ea7a35a8067 Mon Sep 17 00:00:00 2001 From: mikr Date: Tue, 31 Aug 2021 14:54:12 +0200 Subject: [PATCH 020/118] JAVA-6006 Upgrade Spring 5 Webflux module --- spring-5-webflux/pom.xml | 4 -- .../rsocket/client/ClientConfiguration.java | 31 ++++++++-------- .../CustomNettyWebServerFactory.java | 9 ++--- .../MarketDataRSocketControllerLiveTest.java | 37 ++++++++++++------- 4 files changed, 42 insertions(+), 39 deletions(-) diff --git a/spring-5-webflux/pom.xml b/spring-5-webflux/pom.xml index ad1a66943c..b37e93ded8 100644 --- a/spring-5-webflux/pom.xml +++ b/spring-5-webflux/pom.xml @@ -65,8 +65,4 @@ - - 2.3.3.RELEASE - - \ No newline at end of file diff --git a/spring-5-webflux/src/main/java/com/baeldung/spring/rsocket/client/ClientConfiguration.java b/spring-5-webflux/src/main/java/com/baeldung/spring/rsocket/client/ClientConfiguration.java index abfe2e7807..2e2c309240 100644 --- a/spring-5-webflux/src/main/java/com/baeldung/spring/rsocket/client/ClientConfiguration.java +++ b/spring-5-webflux/src/main/java/com/baeldung/spring/rsocket/client/ClientConfiguration.java @@ -1,30 +1,29 @@ package com.baeldung.spring.rsocket.client; -import io.rsocket.RSocket; -import io.rsocket.RSocketFactory; -import io.rsocket.frame.decoder.PayloadDecoder; -import io.rsocket.transport.netty.client.TcpClientTransport; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.rsocket.RSocketRequester; -import org.springframework.messaging.rsocket.RSocketStrategies; import org.springframework.util.MimeTypeUtils; +import reactor.util.retry.Retry; + +import java.time.Duration; @Configuration public class ClientConfiguration { @Bean - public RSocket rSocket() { - return RSocketFactory.connect() - .mimeType(MimeTypeUtils.APPLICATION_JSON_VALUE, MimeTypeUtils.APPLICATION_JSON_VALUE) - .frameDecoder(PayloadDecoder.ZERO_COPY) - .transport(TcpClientTransport.create(7000)) - .start() - .block(); - } + public RSocketRequester getRSocketRequester(){ - @Bean - RSocketRequester rSocketRequester(RSocketStrategies rSocketStrategies) { - return RSocketRequester.wrap(rSocket(), MimeTypeUtils.APPLICATION_JSON, MimeTypeUtils.APPLICATION_JSON, rSocketStrategies); + RSocketRequester.Builder builder = RSocketRequester.builder(); + + return builder + .rsocketConnector( + rSocketConnector -> + rSocketConnector.reconnect( + Retry.fixedDelay(2, Duration.ofSeconds(2)) + ) + ) + .dataMimeType(MimeTypeUtils.APPLICATION_JSON) + .tcp("localhost", 7000); } } diff --git a/spring-5-webflux/src/main/java/com/baeldung/spring/serverconfig/CustomNettyWebServerFactory.java b/spring-5-webflux/src/main/java/com/baeldung/spring/serverconfig/CustomNettyWebServerFactory.java index f9de3b4006..2d11a51160 100644 --- a/spring-5-webflux/src/main/java/com/baeldung/spring/serverconfig/CustomNettyWebServerFactory.java +++ b/spring-5-webflux/src/main/java/com/baeldung/spring/serverconfig/CustomNettyWebServerFactory.java @@ -25,12 +25,9 @@ public class CustomNettyWebServerFactory { @Override public HttpServer apply(HttpServer httpServer) { - EventLoopGroup parentGroup = new NioEventLoopGroup(); - EventLoopGroup childGroup = new NioEventLoopGroup(); - return httpServer - .tcpConfiguration(tcpServer -> tcpServer.bootstrap( - serverBootstrap -> serverBootstrap.group(parentGroup, childGroup).channel(NioServerSocketChannel.class) - )); + EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); + eventLoopGroup.register(new NioServerSocketChannel()); + return httpServer.runOn(eventLoopGroup); } } } diff --git a/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java b/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java index 40ddc732ac..7d8ed1f22d 100644 --- a/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java +++ b/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java @@ -1,15 +1,8 @@ package com.baeldung.spring.rsocket.server; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.verify; - import com.baeldung.spring.rsocket.model.MarketData; import com.baeldung.spring.rsocket.model.MarketDataRequest; import io.rsocket.RSocket; -import io.rsocket.RSocketFactory; -import io.rsocket.frame.decoder.PayloadDecoder; -import io.rsocket.transport.netty.client.TcpClientTransport; -import java.time.Duration; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -23,6 +16,12 @@ import org.springframework.messaging.rsocket.RSocketStrategies; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.util.MimeTypeUtils; +import reactor.util.retry.Retry; + +import java.time.Duration; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; @RunWith(SpringRunner.class) @SpringBootTest @@ -81,12 +80,24 @@ public class MarketDataRSocketControllerLiveTest { @Bean @Lazy public RSocket rSocket() { - return RSocketFactory.connect() - .mimeType(MimeTypeUtils.APPLICATION_JSON_VALUE, MimeTypeUtils.APPLICATION_JSON_VALUE) - .frameDecoder(PayloadDecoder.ZERO_COPY) - .transport(TcpClientTransport.create(7000)) - .start() - .block(); + + RSocketRequester.Builder builder = RSocketRequester.builder(); + + return builder + .rsocketConnector( + rSocketConnector -> + rSocketConnector.reconnect(Retry.fixedDelay(2, Duration.ofSeconds(2)))) + .dataMimeType(MimeTypeUtils.APPLICATION_JSON) + .tcp("localhost", 7000) + .rsocket(); +// .connec/t(TcpClientTransport.create(6565)); + +// return RSocketFactory.connect() +// .mimeType(MimeTypeUtils.APPLICATION_JSON_VALUE, MimeTypeUtils.APPLICATION_JSON_VALUE) +// .frameDecoder(PayloadDecoder.ZERO_COPY) +// .transport(TcpClientTransport.create(7000)) +// .start() +// .block(); } @Bean From 69d65ee12afde97ba354defb6652703d8c69b677 Mon Sep 17 00:00:00 2001 From: mikr Date: Thu, 2 Sep 2021 09:16:14 +0200 Subject: [PATCH 021/118] JAVA-6006 Remove commented code --- .../server/MarketDataRSocketControllerLiveTest.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java b/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java index 7d8ed1f22d..98d604b178 100644 --- a/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java +++ b/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java @@ -90,14 +90,6 @@ public class MarketDataRSocketControllerLiveTest { .dataMimeType(MimeTypeUtils.APPLICATION_JSON) .tcp("localhost", 7000) .rsocket(); -// .connec/t(TcpClientTransport.create(6565)); - -// return RSocketFactory.connect() -// .mimeType(MimeTypeUtils.APPLICATION_JSON_VALUE, MimeTypeUtils.APPLICATION_JSON_VALUE) -// .frameDecoder(PayloadDecoder.ZERO_COPY) -// .transport(TcpClientTransport.create(7000)) -// .start() -// .block(); } @Bean From 0595a3506213ca0c18dd2dbcd2a23d8b85848bce Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Thu, 2 Sep 2021 23:21:16 +0530 Subject: [PATCH 022/118] JAVA-6303: Update "Jackson Inheritance" article --- .../inheritance/SubTypeHandlingUnitTest.java | 41 +++++++++++++------ .../TypeInfoInclusionUnitTest.java | 8 +++- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingUnitTest.java b/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingUnitTest.java index b5b81fa4a3..9c065bcc7c 100644 --- a/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingUnitTest.java +++ b/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingUnitTest.java @@ -1,14 +1,22 @@ package com.baeldung.jackson.inheritance; +import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import org.junit.Test; -import java.util.List; -import java.util.ArrayList; -import java.io.IOException; - +import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Car; +import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Fleet; +import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Truck; +import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Vehicle; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator; +import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; public class SubTypeHandlingUnitTest { @Test @@ -23,21 +31,30 @@ public class SubTypeHandlingUnitTest { } @Test - public void givenSubType_whenNotUsingNoArgsConstructors_thenSucceed() throws IOException { + public void givenSubType_whenNotUsingNoArgsConstructors_thenSucceed() throws IOException { ObjectMapper mapper = new ObjectMapper(); - mapper.enableDefaultTyping(); + PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder() + .allowIfSubType("com.baeldung.jackson.inheritance") + .allowIfSubType("java.util.ArrayList") + .build(); + mapper.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.NON_FINAL); + + Car car = new Car("Mercedes-Benz", "S500", 5, 250.0); + Truck truck = new Truck("Isuzu", "NQR", 7500.0); - SubTypeConstructorStructure.Car car = new SubTypeConstructorStructure.Car("Mercedes-Benz", "S500", 5, 250.0); - SubTypeConstructorStructure.Truck truck = new SubTypeConstructorStructure.Truck("Isuzu", "NQR", 7500.0); - - List vehicles = new ArrayList<>(); + List vehicles = new ArrayList<>(); vehicles.add(car); vehicles.add(truck); - SubTypeConstructorStructure.Fleet serializedFleet = new SubTypeConstructorStructure.Fleet(); + Fleet serializedFleet = new Fleet(); serializedFleet.setVehicles(vehicles); String jsonDataString = mapper.writeValueAsString(serializedFleet); - mapper.readValue(jsonDataString, SubTypeConstructorStructure.Fleet.class); + mapper.readValue(jsonDataString, Fleet.class); + + Fleet deserializedFleet = mapper.readValue(jsonDataString, Fleet.class); + + assertThat(deserializedFleet.getVehicles().get(0), instanceOf(Car.class)); + assertThat(deserializedFleet.getVehicles().get(1), instanceOf(Truck.class)); } } \ No newline at end of file diff --git a/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoInclusionUnitTest.java b/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoInclusionUnitTest.java index 02297b9ee8..ca057edadc 100644 --- a/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoInclusionUnitTest.java +++ b/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoInclusionUnitTest.java @@ -10,12 +10,18 @@ import java.util.ArrayList; import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator; +import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; public class TypeInfoInclusionUnitTest { @Test public void givenTypeInfo_whenAnnotatingGlobally_thenTypesAreCorrectlyRecovered() throws IOException { ObjectMapper mapper = new ObjectMapper(); - mapper.enableDefaultTyping(); + PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder() + .allowIfSubType("com.baeldung.jackson.inheritance") + .allowIfSubType("java.util.ArrayList") + .build(); + mapper.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.NON_FINAL); TypeInfoStructure.Car car = new TypeInfoStructure.Car("Mercedes-Benz", "S500", 5, 250.0); TypeInfoStructure.Truck truck = new TypeInfoStructure.Truck("Isuzu", "NQR", 7500.0); From 5868802a11c65d8cbcf7a85e87b5a48d745d5842 Mon Sep 17 00:00:00 2001 From: rvsathe Date: Fri, 3 Sep 2021 08:28:55 +0530 Subject: [PATCH 023/118] changing Test names as per BDD convention --- .../test/java/com/baeldung/example/soundapi/AppTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppTest.java b/core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppTest.java index ff00523eec..77012fdf6e 100644 --- a/core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppTest.java +++ b/core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppTest.java @@ -17,7 +17,7 @@ public class AppTest { SoundRecorder soundRecorder = new SoundRecorder(); @Test - public void when_run_save_SoundClip() { + public void Given_SoundRecorderObject_When_Run_Then_ThrowsNoException() { soundRecorder.build(af); try { @@ -31,13 +31,13 @@ public class AppTest { } @Test - public void when_run_get_targetdataline() { + public void Given_AudioFormatObject_When_NotNull_Then_ReturnsTargetDataLine() { soundRecorder.setFormat(af); Assertions.assertDoesNotThrow(() -> soundRecorder.getTargetDataLineForRecord()); } @Test - public void when_run_build_byte_ouptstream() { + public void Given_TargetLineDataObject_When_Run_Then_GeneratesOutputStream() { soundRecorder.setFormat(af); ByteArrayOutputStream out = new ByteArrayOutputStream(); @@ -52,7 +52,7 @@ public class AppTest { } @Test - public void when_run_then_save_file() { + public void Given_AudioInputStream_When_NotNull_Then_SaveToWavFile() { soundRecorder.setFormat(af); soundRecorder.build(af); try { From d600970b66f415fc7c2edfa6523e31ea337a522e Mon Sep 17 00:00:00 2001 From: Azhwani Date: Sat, 4 Sep 2021 20:15:52 +0200 Subject: [PATCH 024/118] Add test case --- .../InventoryRepositoryIntegrationTest.java | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/boot/daos/InventoryRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/boot/daos/InventoryRepositoryIntegrationTest.java index 22e2c81739..e4bd3dabff 100644 --- a/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/boot/daos/InventoryRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/boot/daos/InventoryRepositoryIntegrationTest.java @@ -3,20 +3,23 @@ package com.baeldung.boot.daos; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.math.BigDecimal; +import java.util.Optional; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; import com.baeldung.Application; import com.baeldung.boot.domain.MerchandiseEntity; @RunWith(SpringRunner.class) -@SpringBootTest(classes=Application.class) +@SpringBootTest(classes = Application.class) public class InventoryRepositoryIntegrationTest { private static final String ORIGINAL_TITLE = "Pair of Pants"; @@ -58,4 +61,28 @@ public class InventoryRepositoryIntegrationTest { assertEquals(BigDecimal.TEN, result.getPrice()); assertEquals(UPDATED_BRAND, result.getBrand()); } + + @Test + @Transactional + public void shouldUpdateExistingEntryInDBWithoutSave() { + MerchandiseEntity pants = new MerchandiseEntity(ORIGINAL_TITLE, BigDecimal.ONE); + pants = repository.save(pants); + + Long originalId = pants.getId(); + + // Update using setters + pants.setTitle(UPDATED_TITLE); + pants.setPrice(BigDecimal.TEN); + pants.setBrand(UPDATED_BRAND); + + Optional resultOp = repository.findById(originalId); + + assertTrue(resultOp.isPresent()); + MerchandiseEntity result = resultOp.get(); + + assertEquals(originalId, result.getId()); + assertEquals(UPDATED_TITLE, result.getTitle()); + assertEquals(BigDecimal.TEN, result.getPrice()); + assertEquals(UPDATED_BRAND, result.getBrand()); + } } From 439848c1659da015d6ed55e18313b0916330b11f Mon Sep 17 00:00:00 2001 From: Trixi Turny Date: Sun, 5 Sep 2021 12:55:26 +0200 Subject: [PATCH 025/118] BAEL-5054 fix indentations --- .../service/ReactiveUploadService.java | 20 +++++++++---------- .../ReactiveUploadServiceUnitTest.java | 8 ++++++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java index 11409ce986..a12d54960a 100644 --- a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java +++ b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java @@ -34,11 +34,11 @@ public class ReactiveUploadService { .contentType(MediaType.APPLICATION_PDF) .body(BodyInserters.fromResource(resource)) .exchangeToMono(response -> { - if (response.statusCode().equals(HttpStatus.OK)) { - return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode()); - } else { - throw new ServiceException("Error uploading file"); - } + if (response.statusCode().equals(HttpStatus.OK)) { + return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode()); + } else { + throw new ServiceException("Error uploading file"); + } }); return httpStatusMono; } @@ -55,11 +55,11 @@ public class ReactiveUploadService { .contentType(MediaType.MULTIPART_FORM_DATA) .body(BodyInserters.fromMultipartData(builder.build())) .exchangeToMono(response -> { - if (response.statusCode().equals(HttpStatus.OK)) { - return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode()); - } else { - throw new ServiceException("Error uploading file"); - } + if (response.statusCode().equals(HttpStatus.OK)) { + return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode()); + } else { + throw new ServiceException("Error uploading file"); + } }); return httpStatusMono; } diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/service/ReactiveUploadServiceUnitTest.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/service/ReactiveUploadServiceUnitTest.java index 0ca59848d7..40c1e40d92 100644 --- a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/service/ReactiveUploadServiceUnitTest.java +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/service/ReactiveUploadServiceUnitTest.java @@ -18,8 +18,8 @@ class ReactiveUploadServiceUnitTest { final WebClient webClientMock = WebClient.builder().baseUrl(BASE_URL) .exchangeFunction(clientRequest -> Mono.just(ClientResponse.create(HttpStatus.OK) - .header("content-type", "application/json") - .build())) + .header("content-type", "application/json") + .build())) .build(); private final ReactiveUploadService tested = new ReactiveUploadService(webClientMock); @@ -27,8 +27,10 @@ class ReactiveUploadServiceUnitTest { @Test void givenAPdf_whenUploadingWithWebClient_thenOK() { final Resource file = mock(Resource.class); + final Mono result = tested.uploadPdf(file); final HttpStatus status = result.block(); + assertThat(status).isEqualTo(HttpStatus.OK); } @@ -37,8 +39,10 @@ class ReactiveUploadServiceUnitTest { final Resource file = mock(Resource.class); final MultipartFile multipartFile = mock(MultipartFile.class); when(multipartFile.getResource()).thenReturn(file); + final Mono result = tested.uploadMultipart(multipartFile); final HttpStatus status = result.block(); + assertThat(status).isEqualTo(HttpStatus.OK); } } \ No newline at end of file From a7fa2acb20cc91220d4029663f044e230752d497 Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Sun, 5 Sep 2021 17:08:06 +0530 Subject: [PATCH 026/118] JAVA-6439: Check PR for spring-security-web-sockets module --- .../spring-security-web-sockets/README.md | 4 ++-- .../spring-security-web-sockets/pom.xml | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/spring-security-modules/spring-security-web-sockets/README.md b/spring-security-modules/spring-security-web-sockets/README.md index 76717e2fe6..12f7a6ad56 100644 --- a/spring-security-modules/spring-security-web-sockets/README.md +++ b/spring-security-modules/spring-security-web-sockets/README.md @@ -10,8 +10,8 @@ This module contains articles about WebSockets with Spring Security ### Running This Project: -To build the project, run the command: mvn clean install. This will build a war file in the target folder that you can deploye on a server like Tomcat. +To build the project, run the command: `mvn clean install`. This will build a war file in the target folder that you can deploye on a server like Tomcat. -Alternatively, run the project from an IDE. +Alternatively, run the project from an IDE, with the maven goal `org.codehaus.cargo:cargo-maven2-plugin:run` To login, use credentials from the data.sql file in src/main/resource, eg: user/password. diff --git a/spring-security-modules/spring-security-web-sockets/pom.xml b/spring-security-modules/spring-security-web-sockets/pom.xml index b1536e88ea..db0d280da8 100644 --- a/spring-security-modules/spring-security-web-sockets/pom.xml +++ b/spring-security-modules/spring-security-web-sockets/pom.xml @@ -155,11 +155,14 @@ spring-security-web-sockets - org.apache.tomcat.maven - tomcat7-maven-plugin - 2.2 + org.codehaus.cargo + cargo-maven2-plugin + 1.7.6 - /spring-security-mvc-socket + + tomcat9x + embedded + From 90c48445d32308ef329fb8477ec7c2c66db6b527 Mon Sep 17 00:00:00 2001 From: mikr Date: Tue, 7 Sep 2021 23:27:46 +0200 Subject: [PATCH 027/118] JAVA-6006 Remove @ignore in test --- .../java/com/baeldung/apache/beam/intro/WordCountUnitTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apache-libraries/src/test/java/com/baeldung/apache/beam/intro/WordCountUnitTest.java b/apache-libraries/src/test/java/com/baeldung/apache/beam/intro/WordCountUnitTest.java index 805f79742d..77287066ac 100644 --- a/apache-libraries/src/test/java/com/baeldung/apache/beam/intro/WordCountUnitTest.java +++ b/apache-libraries/src/test/java/com/baeldung/apache/beam/intro/WordCountUnitTest.java @@ -9,8 +9,7 @@ import com.baeldung.apache.beam.intro.WordCount; public class WordCountUnitTest { -// @Test - @Ignore + @Test public void givenInputFile_whenWordCountRuns_thenJobFinishWithoutError() { boolean jobDone = WordCount.wordCount("src/test/resources/wordcount.txt", "target/output"); assertTrue(jobDone); From 540690b06e52beb7d7d878a909a823d60a50e319 Mon Sep 17 00:00:00 2001 From: polomos Date: Wed, 8 Sep 2021 14:59:09 +0200 Subject: [PATCH 028/118] BAEL-4266 Test default cryptography strength (#11155) --- .../CryptographyStrengthUnitTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 core-java-modules/core-java-security-3/src/test/java/com/baeldung/cryptography/CryptographyStrengthUnitTest.java diff --git a/core-java-modules/core-java-security-3/src/test/java/com/baeldung/cryptography/CryptographyStrengthUnitTest.java b/core-java-modules/core-java-security-3/src/test/java/com/baeldung/cryptography/CryptographyStrengthUnitTest.java new file mode 100644 index 0000000000..5d5c30dc34 --- /dev/null +++ b/core-java-modules/core-java-security-3/src/test/java/com/baeldung/cryptography/CryptographyStrengthUnitTest.java @@ -0,0 +1,17 @@ +package com.baeldung.cryptography; + +import org.junit.Test; + +import java.security.NoSuchAlgorithmException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CryptographyStrengthUnitTest { + private static final int UNLIMITED_KEY_SIZE = 2147483647; + + @Test + public void whenDefaultCheck_thenUnlimitedReturned() throws NoSuchAlgorithmException { + int maxKeySize = javax.crypto.Cipher.getMaxAllowedKeyLength("AES"); + assertThat(maxKeySize).isEqualTo(UNLIMITED_KEY_SIZE); + } +} From d85f4ef8b4b1334a609f1b83e22481f6883c1bae Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 9 Sep 2021 00:31:47 +0800 Subject: [PATCH 029/118] Update README.md --- kubernetes/k8s-admission-controller/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/kubernetes/k8s-admission-controller/README.md b/kubernetes/k8s-admission-controller/README.md index c446ab403d..9c824d76b3 100644 --- a/kubernetes/k8s-admission-controller/README.md +++ b/kubernetes/k8s-admission-controller/README.md @@ -1,3 +1,4 @@ ## Relevant Articles: - [Creating a Kubertes Admission Controller in Java](https://www.baeldung.com/java-kubernetes-admission-controller) +- [Access Control Models](https://www.baeldung.com/java-access-control-models) From 7a0c377fe5bcf2a7d8e47898e63e374e71c3cbfc Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 9 Sep 2021 00:35:27 +0800 Subject: [PATCH 030/118] Update README.md --- spring-websockets/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-websockets/README.md b/spring-websockets/README.md index 9cc84f0fda..7d69c21b78 100644 --- a/spring-websockets/README.md +++ b/spring-websockets/README.md @@ -6,3 +6,4 @@ This module contains articles about Spring WebSockets. - [Intro to WebSockets with Spring](https://www.baeldung.com/websockets-spring) - [A Quick Example of Spring Websockets’ @SendToUser Annotation](https://www.baeldung.com/spring-websockets-sendtouser) - [Scheduled WebSocket Push with Spring Boot](https://www.baeldung.com/spring-boot-scheduled-websocket) +- [Test WebSocket APIs With Postman](https://www.baeldung.com/postman-websocket-apis) From ba840cd65521e84e519bd2bc9859613d06fb6f5f Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 9 Sep 2021 00:40:12 +0800 Subject: [PATCH 031/118] Update README.md --- core-java-modules/core-java-networking-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-networking-3/README.md b/core-java-modules/core-java-networking-3/README.md index 2e76ab5d51..0dc9ad9f70 100644 --- a/core-java-modules/core-java-networking-3/README.md +++ b/core-java-modules/core-java-networking-3/README.md @@ -8,4 +8,5 @@ This module contains articles about networking in Java - [Downloading Email Attachments in Java](https://www.baeldung.com/java-download-email-attachments) - [Connection Timeout vs. Read Timeout for Java Sockets](https://www.baeldung.com/java-socket-connection-read-timeout) - [Find Whether an IP Address Is in the Specified Range or Not in Java](https://www.baeldung.com/java-check-ip-address-range) +- [Find the IP Address of a Client Connected to a Server](https://www.baeldung.com/java-client-get-ip-address) - [[<-- Prev]](/core-java-modules/core-java-networking-2) From 7fe837abd2a28e96fbda73a3d6311a8ea224c5fa Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 9 Sep 2021 00:43:30 +0800 Subject: [PATCH 032/118] Update README.md --- persistence-modules/java-jpa-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/persistence-modules/java-jpa-3/README.md b/persistence-modules/java-jpa-3/README.md index d517c55d7a..202c97a830 100644 --- a/persistence-modules/java-jpa-3/README.md +++ b/persistence-modules/java-jpa-3/README.md @@ -14,3 +14,4 @@ This module contains articles about the Java Persistence API (JPA) in Java. - [How to Return Multiple Entities In JPA Query](https://www.baeldung.com/jpa-return-multiple-entities) - [Defining Unique Constraints in JPA](https://www.baeldung.com/jpa-unique-constraints) - [How to Check Field Existence in MongoDB?](https://www.baeldung.com/mongodb-check-field-exists) +- [Connecting to a Specific Schema in JDBC](https://www.baeldung.com/jdbc-connect-to-schema) From e45023ec07307b3f09d5e736957c2b87f40932e1 Mon Sep 17 00:00:00 2001 From: vaibhav007jain <72961247+vaibhav007jain@users.noreply.github.com> Date: Fri, 10 Sep 2021 06:35:29 +0530 Subject: [PATCH 033/118] BAEL-5071 (#11171) * commited initial code for hexagonal architecture * Deleting to check in again * Deleing to check in again * Push first code for Hexagonal Architecture * final code with UT for JSON to Java conversion * removed hexagonal-architecture code from last commit * BEL-5071 updated README * BAEL-5071: Undo README changes and added a nested object in the JSON example. * BAEL-5071: fixed whitespace/indentation in JsonToJavaClassConversion.java Co-authored-by: Vaibhav Jain --- .../JsonToJavaClassConversion.java | 20 +- .../jsontojavaclass/pojo/SamplePojo.java | 53 +++-- .../jsontojavaclass/pojo/Address.java | 119 ++++++++++ .../baeldung/jsontojavaclass/pojo/Input.java | 213 ++++++++++++++++++ json-2/src/main/resources/input.json | 16 ++ .../JsonToJavaClassConversionUnitTest.java | 14 +- .../jsontojavaclass/pojo/SamplePojo.java | 213 ++++++++++++++++++ 7 files changed, 611 insertions(+), 37 deletions(-) create mode 100644 json-2/src/main/resources/convertedPojo/com/baeldung/jsontojavaclass/pojo/Address.java create mode 100644 json-2/src/main/resources/convertedPojo/com/baeldung/jsontojavaclass/pojo/Input.java create mode 100644 json-2/src/main/resources/input.json create mode 100644 json-2/src/test/resources/com/baeldung/jsontojavaclass/pojo/SamplePojo.java diff --git a/json-2/src/main/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversion.java b/json-2/src/main/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversion.java index 65eecd0d38..1b18463856 100644 --- a/json-2/src/main/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversion.java +++ b/json-2/src/main/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversion.java @@ -17,7 +17,22 @@ import com.sun.codemodel.JCodeModel; public class JsonToJavaClassConversion { - public Object convertJsonToJavaClass(URL inputJson, File outputJavaClassDirectory, String packageName, String className) throws IOException { + public static void main(String[] args) { + String packageName = "com.baeldung.jsontojavaclass.pojo"; + String basePath = "src/main/resources"; + File inputJson = new File(basePath + File.separator + "input.json"); + File outputPojoDirectory = new File(basePath + File.separator + "convertedPojo"); + outputPojoDirectory.mkdirs(); + try { + new JsonToJavaClassConversion().convertJsonToJavaClass(inputJson.toURI().toURL(), outputPojoDirectory, packageName, inputJson.getName().replace(".json", "")); + } catch (IOException e) { + System.out.println("Encountered issue while converting to pojo: " + e.getMessage()); + e.printStackTrace(); + } + } + + + public void convertJsonToJavaClass(URL inputJsonUrl, File outputJavaClassDirectory, String packageName, String javaClassName) throws IOException { JCodeModel jcodeModel = new JCodeModel(); GenerationConfig config = new DefaultGenerationConfig() { @@ -33,10 +48,9 @@ public class JsonToJavaClassConversion { }; SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator()); - mapper.generate(jcodeModel, className, packageName, inputJson); + mapper.generate(jcodeModel, javaClassName, packageName, inputJsonUrl); jcodeModel.build(outputJavaClassDirectory); - return mapper; } } diff --git a/json-2/src/main/java/com/baeldung/jsontojavaclass/pojo/SamplePojo.java b/json-2/src/main/java/com/baeldung/jsontojavaclass/pojo/SamplePojo.java index 4c8a719de0..38ebb8a0f8 100644 --- a/json-2/src/main/java/com/baeldung/jsontojavaclass/pojo/SamplePojo.java +++ b/json-2/src/main/java/com/baeldung/jsontojavaclass/pojo/SamplePojo.java @@ -5,9 +5,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; - import javax.annotation.Generated; - import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonAnySetter; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -16,7 +14,14 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyOrder; @JsonInclude(JsonInclude.Include.NON_NULL) -@JsonPropertyOrder({ "name", "area", "author", "id", "salary", "topics" }) +@JsonPropertyOrder({ + "name", + "area", + "author", + "id", + "salary", + "topics" +}) @Generated("jsonschema2pojo") public class SamplePojo { @@ -143,40 +148,37 @@ public class SamplePojo { @Override public String toString() { StringBuilder sb = new StringBuilder(); - sb.append(SamplePojo.class.getName()) - .append('@') - .append(Integer.toHexString(System.identityHashCode(this))) - .append('['); + sb.append(SamplePojo.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); sb.append("name"); sb.append('='); - sb.append(((this.name == null) ? "" : this.name)); + sb.append(((this.name == null)?"":this.name)); sb.append(','); sb.append("area"); sb.append('='); - sb.append(((this.area == null) ? "" : this.area)); + sb.append(((this.area == null)?"":this.area)); sb.append(','); sb.append("author"); sb.append('='); - sb.append(((this.author == null) ? "" : this.author)); + sb.append(((this.author == null)?"":this.author)); sb.append(','); sb.append("id"); sb.append('='); - sb.append(((this.id == null) ? "" : this.id)); + sb.append(((this.id == null)?"":this.id)); sb.append(','); sb.append("salary"); sb.append('='); - sb.append(((this.salary == null) ? "" : this.salary)); + sb.append(((this.salary == null)?"":this.salary)); sb.append(','); sb.append("topics"); sb.append('='); - sb.append(((this.topics == null) ? "" : this.topics)); + sb.append(((this.topics == null)?"":this.topics)); sb.append(','); sb.append("additionalProperties"); sb.append('='); - sb.append(((this.additionalProperties == null) ? "" : this.additionalProperties)); + sb.append(((this.additionalProperties == null)?"":this.additionalProperties)); sb.append(','); - if (sb.charAt((sb.length() - 1)) == ',') { - sb.setCharAt((sb.length() - 1), ']'); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); } else { sb.append(']'); } @@ -186,13 +188,13 @@ public class SamplePojo { @Override public int hashCode() { int result = 1; - result = ((result * 31) + ((this.area == null) ? 0 : this.area.hashCode())); - result = ((result * 31) + ((this.author == null) ? 0 : this.author.hashCode())); - result = ((result * 31) + ((this.topics == null) ? 0 : this.topics.hashCode())); - result = ((result * 31) + ((this.name == null) ? 0 : this.name.hashCode())); - result = ((result * 31) + ((this.id == null) ? 0 : this.id.hashCode())); - result = ((result * 31) + ((this.additionalProperties == null) ? 0 : this.additionalProperties.hashCode())); - result = ((result * 31) + ((this.salary == null) ? 0 : this.salary.hashCode())); + result = ((result* 31)+((this.area == null)? 0 :this.area.hashCode())); + result = ((result* 31)+((this.author == null)? 0 :this.author.hashCode())); + result = ((result* 31)+((this.topics == null)? 0 :this.topics.hashCode())); + result = ((result* 31)+((this.name == null)? 0 :this.name.hashCode())); + result = ((result* 31)+((this.id == null)? 0 :this.id.hashCode())); + result = ((result* 31)+((this.additionalProperties == null)? 0 :this.additionalProperties.hashCode())); + result = ((result* 31)+((this.salary == null)? 0 :this.salary.hashCode())); return result; } @@ -205,10 +207,7 @@ public class SamplePojo { return false; } SamplePojo rhs = ((SamplePojo) other); - return ((((((((this.area == rhs.area) || ((this.area != null) && this.area.equals(rhs.area))) && ((this.author == rhs.author) || ((this.author != null) && this.author.equals(rhs.author)))) - && ((this.topics == rhs.topics) || ((this.topics != null) && this.topics.equals(rhs.topics)))) && ((this.name == rhs.name) || ((this.name != null) && this.name.equals(rhs.name)))) - && ((this.id == rhs.id) || ((this.id != null) && this.id.equals(rhs.id)))) && ((this.additionalProperties == rhs.additionalProperties) || ((this.additionalProperties != null) && this.additionalProperties.equals(rhs.additionalProperties)))) - && ((this.salary == rhs.salary) || ((this.salary != null) && this.salary.equals(rhs.salary)))); + return ((((((((this.area == rhs.area)||((this.area!= null)&&this.area.equals(rhs.area)))&&((this.author == rhs.author)||((this.author!= null)&&this.author.equals(rhs.author))))&&((this.topics == rhs.topics)||((this.topics!= null)&&this.topics.equals(rhs.topics))))&&((this.name == rhs.name)||((this.name!= null)&&this.name.equals(rhs.name))))&&((this.id == rhs.id)||((this.id!= null)&&this.id.equals(rhs.id))))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties))))&&((this.salary == rhs.salary)||((this.salary!= null)&&this.salary.equals(rhs.salary)))); } } diff --git a/json-2/src/main/resources/convertedPojo/com/baeldung/jsontojavaclass/pojo/Address.java b/json-2/src/main/resources/convertedPojo/com/baeldung/jsontojavaclass/pojo/Address.java new file mode 100644 index 0000000000..e3d901edf4 --- /dev/null +++ b/json-2/src/main/resources/convertedPojo/com/baeldung/jsontojavaclass/pojo/Address.java @@ -0,0 +1,119 @@ + +package com.baeldung.jsontojavaclass.pojo; + +import java.util.HashMap; +import java.util.Map; +import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "city", + "country" +}) +@Generated("jsonschema2pojo") +public class Address { + + @JsonProperty("city") + private String city; + @JsonProperty("country") + private String country; + @JsonIgnore + private Map additionalProperties = new HashMap(); + + @JsonProperty("city") + public String getCity() { + return city; + } + + @JsonProperty("city") + public void setCity(String city) { + this.city = city; + } + + public Address withCity(String city) { + this.city = city; + return this; + } + + @JsonProperty("country") + public String getCountry() { + return country; + } + + @JsonProperty("country") + public void setCountry(String country) { + this.country = country; + } + + public Address withCountry(String country) { + this.country = country; + return this; + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + public Address withAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + return this; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(Address.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); + sb.append("city"); + sb.append('='); + sb.append(((this.city == null)?"":this.city)); + sb.append(','); + sb.append("country"); + sb.append('='); + sb.append(((this.country == null)?"":this.country)); + sb.append(','); + sb.append("additionalProperties"); + sb.append('='); + sb.append(((this.additionalProperties == null)?"":this.additionalProperties)); + sb.append(','); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); + } else { + sb.append(']'); + } + return sb.toString(); + } + + @Override + public int hashCode() { + int result = 1; + result = ((result* 31)+((this.country == null)? 0 :this.country.hashCode())); + result = ((result* 31)+((this.additionalProperties == null)? 0 :this.additionalProperties.hashCode())); + result = ((result* 31)+((this.city == null)? 0 :this.city.hashCode())); + return result; + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + if ((other instanceof Address) == false) { + return false; + } + Address rhs = ((Address) other); + return ((((this.country == rhs.country)||((this.country!= null)&&this.country.equals(rhs.country)))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties))))&&((this.city == rhs.city)||((this.city!= null)&&this.city.equals(rhs.city)))); + } + +} diff --git a/json-2/src/main/resources/convertedPojo/com/baeldung/jsontojavaclass/pojo/Input.java b/json-2/src/main/resources/convertedPojo/com/baeldung/jsontojavaclass/pojo/Input.java new file mode 100644 index 0000000000..19bee8db4d --- /dev/null +++ b/json-2/src/main/resources/convertedPojo/com/baeldung/jsontojavaclass/pojo/Input.java @@ -0,0 +1,213 @@ + +package com.baeldung.jsontojavaclass.pojo; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "name", + "area", + "author", + "id", + "topics", + "address" +}) +@Generated("jsonschema2pojo") +public class Input { + + @JsonProperty("name") + private String name; + @JsonProperty("area") + private String area; + @JsonProperty("author") + private String author; + @JsonProperty("id") + private Integer id; + @JsonProperty("topics") + private List topics = new ArrayList(); + @JsonProperty("address") + private Address address; + @JsonIgnore + private Map additionalProperties = new HashMap(); + + @JsonProperty("name") + public String getName() { + return name; + } + + @JsonProperty("name") + public void setName(String name) { + this.name = name; + } + + public Input withName(String name) { + this.name = name; + return this; + } + + @JsonProperty("area") + public String getArea() { + return area; + } + + @JsonProperty("area") + public void setArea(String area) { + this.area = area; + } + + public Input withArea(String area) { + this.area = area; + return this; + } + + @JsonProperty("author") + public String getAuthor() { + return author; + } + + @JsonProperty("author") + public void setAuthor(String author) { + this.author = author; + } + + public Input withAuthor(String author) { + this.author = author; + return this; + } + + @JsonProperty("id") + public Integer getId() { + return id; + } + + @JsonProperty("id") + public void setId(Integer id) { + this.id = id; + } + + public Input withId(Integer id) { + this.id = id; + return this; + } + + @JsonProperty("topics") + public List getTopics() { + return topics; + } + + @JsonProperty("topics") + public void setTopics(List topics) { + this.topics = topics; + } + + public Input withTopics(List topics) { + this.topics = topics; + return this; + } + + @JsonProperty("address") + public Address getAddress() { + return address; + } + + @JsonProperty("address") + public void setAddress(Address address) { + this.address = address; + } + + public Input withAddress(Address address) { + this.address = address; + return this; + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + public Input withAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + return this; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(Input.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); + sb.append("name"); + sb.append('='); + sb.append(((this.name == null)?"":this.name)); + sb.append(','); + sb.append("area"); + sb.append('='); + sb.append(((this.area == null)?"":this.area)); + sb.append(','); + sb.append("author"); + sb.append('='); + sb.append(((this.author == null)?"":this.author)); + sb.append(','); + sb.append("id"); + sb.append('='); + sb.append(((this.id == null)?"":this.id)); + sb.append(','); + sb.append("topics"); + sb.append('='); + sb.append(((this.topics == null)?"":this.topics)); + sb.append(','); + sb.append("address"); + sb.append('='); + sb.append(((this.address == null)?"":this.address)); + sb.append(','); + sb.append("additionalProperties"); + sb.append('='); + sb.append(((this.additionalProperties == null)?"":this.additionalProperties)); + sb.append(','); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); + } else { + sb.append(']'); + } + return sb.toString(); + } + + @Override + public int hashCode() { + int result = 1; + result = ((result* 31)+((this.area == null)? 0 :this.area.hashCode())); + result = ((result* 31)+((this.address == null)? 0 :this.address.hashCode())); + result = ((result* 31)+((this.author == null)? 0 :this.author.hashCode())); + result = ((result* 31)+((this.topics == null)? 0 :this.topics.hashCode())); + result = ((result* 31)+((this.name == null)? 0 :this.name.hashCode())); + result = ((result* 31)+((this.id == null)? 0 :this.id.hashCode())); + result = ((result* 31)+((this.additionalProperties == null)? 0 :this.additionalProperties.hashCode())); + return result; + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + if ((other instanceof Input) == false) { + return false; + } + Input rhs = ((Input) other); + return ((((((((this.area == rhs.area)||((this.area!= null)&&this.area.equals(rhs.area)))&&((this.address == rhs.address)||((this.address!= null)&&this.address.equals(rhs.address))))&&((this.author == rhs.author)||((this.author!= null)&&this.author.equals(rhs.author))))&&((this.topics == rhs.topics)||((this.topics!= null)&&this.topics.equals(rhs.topics))))&&((this.name == rhs.name)||((this.name!= null)&&this.name.equals(rhs.name))))&&((this.id == rhs.id)||((this.id!= null)&&this.id.equals(rhs.id))))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties)))); + } + +} diff --git a/json-2/src/main/resources/input.json b/json-2/src/main/resources/input.json new file mode 100644 index 0000000000..22706ede53 --- /dev/null +++ b/json-2/src/main/resources/input.json @@ -0,0 +1,16 @@ +{ + "name": "Baeldung", + "area": "tech blogs", + "author": "Eugen", + "id": 32134, + "topics": [ + "java", + "kotlin", + "cs", + "linux" + ], + "address": { + "city": "Bucharest", + "country": "Romania" + } +} \ No newline at end of file diff --git a/json-2/src/test/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversionUnitTest.java b/json-2/src/test/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversionUnitTest.java index 8dbfb14b45..f8000fa6fb 100644 --- a/json-2/src/test/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversionUnitTest.java +++ b/json-2/src/test/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversionUnitTest.java @@ -3,6 +3,7 @@ package com.baeldung.jsontojavaclass; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; +import java.util.Arrays; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -21,17 +22,16 @@ class JsonToJavaClassConversionUnitTest { File inputJson = new File(jsonPath + "sample_input.json"); // create the local directory for generating the Java Class file - String outputPath = "src/main/java/"; + String outputPath = "src/test/resources/"; File outputJavaClassDirectory = new File(outputPath); - outputJavaClassDirectory.mkdirs(); - String className = "SamplePojo"; + String javaClassName = "SamplePojo"; - Object object = jsonToJavaConversion.convertJsonToJavaClass(inputJson.toURI() - .toURL(), outputJavaClassDirectory, packageName, className); - System.out.println(object); + jsonToJavaConversion.convertJsonToJavaClass(inputJson.toURI() + .toURL(), outputJavaClassDirectory, packageName, javaClassName); - Assertions.assertNotNull(object); + File outputJavaClassPath = new File(outputPath + packageName.replace(".", "/")); + Assertions.assertTrue(Arrays.stream(outputJavaClassPath.listFiles()).peek(System.out::println).anyMatch(file -> (javaClassName+".java").equalsIgnoreCase(file.getName()))); } diff --git a/json-2/src/test/resources/com/baeldung/jsontojavaclass/pojo/SamplePojo.java b/json-2/src/test/resources/com/baeldung/jsontojavaclass/pojo/SamplePojo.java new file mode 100644 index 0000000000..38ebb8a0f8 --- /dev/null +++ b/json-2/src/test/resources/com/baeldung/jsontojavaclass/pojo/SamplePojo.java @@ -0,0 +1,213 @@ + +package com.baeldung.jsontojavaclass.pojo; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "name", + "area", + "author", + "id", + "salary", + "topics" +}) +@Generated("jsonschema2pojo") +public class SamplePojo { + + @JsonProperty("name") + private String name; + @JsonProperty("area") + private String area; + @JsonProperty("author") + private String author; + @JsonProperty("id") + private Integer id; + @JsonProperty("salary") + private Integer salary; + @JsonProperty("topics") + private List topics = new ArrayList(); + @JsonIgnore + private Map additionalProperties = new HashMap(); + + @JsonProperty("name") + public String getName() { + return name; + } + + @JsonProperty("name") + public void setName(String name) { + this.name = name; + } + + public SamplePojo withName(String name) { + this.name = name; + return this; + } + + @JsonProperty("area") + public String getArea() { + return area; + } + + @JsonProperty("area") + public void setArea(String area) { + this.area = area; + } + + public SamplePojo withArea(String area) { + this.area = area; + return this; + } + + @JsonProperty("author") + public String getAuthor() { + return author; + } + + @JsonProperty("author") + public void setAuthor(String author) { + this.author = author; + } + + public SamplePojo withAuthor(String author) { + this.author = author; + return this; + } + + @JsonProperty("id") + public Integer getId() { + return id; + } + + @JsonProperty("id") + public void setId(Integer id) { + this.id = id; + } + + public SamplePojo withId(Integer id) { + this.id = id; + return this; + } + + @JsonProperty("salary") + public Integer getSalary() { + return salary; + } + + @JsonProperty("salary") + public void setSalary(Integer salary) { + this.salary = salary; + } + + public SamplePojo withSalary(Integer salary) { + this.salary = salary; + return this; + } + + @JsonProperty("topics") + public List getTopics() { + return topics; + } + + @JsonProperty("topics") + public void setTopics(List topics) { + this.topics = topics; + } + + public SamplePojo withTopics(List topics) { + this.topics = topics; + return this; + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + public SamplePojo withAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + return this; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(SamplePojo.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); + sb.append("name"); + sb.append('='); + sb.append(((this.name == null)?"":this.name)); + sb.append(','); + sb.append("area"); + sb.append('='); + sb.append(((this.area == null)?"":this.area)); + sb.append(','); + sb.append("author"); + sb.append('='); + sb.append(((this.author == null)?"":this.author)); + sb.append(','); + sb.append("id"); + sb.append('='); + sb.append(((this.id == null)?"":this.id)); + sb.append(','); + sb.append("salary"); + sb.append('='); + sb.append(((this.salary == null)?"":this.salary)); + sb.append(','); + sb.append("topics"); + sb.append('='); + sb.append(((this.topics == null)?"":this.topics)); + sb.append(','); + sb.append("additionalProperties"); + sb.append('='); + sb.append(((this.additionalProperties == null)?"":this.additionalProperties)); + sb.append(','); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); + } else { + sb.append(']'); + } + return sb.toString(); + } + + @Override + public int hashCode() { + int result = 1; + result = ((result* 31)+((this.area == null)? 0 :this.area.hashCode())); + result = ((result* 31)+((this.author == null)? 0 :this.author.hashCode())); + result = ((result* 31)+((this.topics == null)? 0 :this.topics.hashCode())); + result = ((result* 31)+((this.name == null)? 0 :this.name.hashCode())); + result = ((result* 31)+((this.id == null)? 0 :this.id.hashCode())); + result = ((result* 31)+((this.additionalProperties == null)? 0 :this.additionalProperties.hashCode())); + result = ((result* 31)+((this.salary == null)? 0 :this.salary.hashCode())); + return result; + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + if ((other instanceof SamplePojo) == false) { + return false; + } + SamplePojo rhs = ((SamplePojo) other); + return ((((((((this.area == rhs.area)||((this.area!= null)&&this.area.equals(rhs.area)))&&((this.author == rhs.author)||((this.author!= null)&&this.author.equals(rhs.author))))&&((this.topics == rhs.topics)||((this.topics!= null)&&this.topics.equals(rhs.topics))))&&((this.name == rhs.name)||((this.name!= null)&&this.name.equals(rhs.name))))&&((this.id == rhs.id)||((this.id!= null)&&this.id.equals(rhs.id))))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties))))&&((this.salary == rhs.salary)||((this.salary!= null)&&this.salary.equals(rhs.salary)))); + } + +} From f8353b071df6cb646fb43e79ba822958f126d255 Mon Sep 17 00:00:00 2001 From: sharifi Date: Sat, 11 Sep 2021 12:21:33 +0430 Subject: [PATCH 034/118] BAEL-5079: upgrade commons-codec version --- core-java-modules/core-java-string-operations/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-string-operations/pom.xml b/core-java-modules/core-java-string-operations/pom.xml index a7f75f37e2..17b8562caf 100644 --- a/core-java-modules/core-java-string-operations/pom.xml +++ b/core-java-modules/core-java-string-operations/pom.xml @@ -61,7 +61,7 @@ 3.6.1 - 1.10 + 1.15 \ No newline at end of file From 8542ba368a7b462d3d63a7eff4b41bbd2ae86092 Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Sat, 11 Sep 2021 14:57:21 +0530 Subject: [PATCH 035/118] JAVA-6303: corrected formatting --- .../jackson/inheritance/SubTypeHandlingUnitTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingUnitTest.java b/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingUnitTest.java index 9c065bcc7c..8d6bc9ad6a 100644 --- a/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingUnitTest.java +++ b/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingUnitTest.java @@ -34,9 +34,9 @@ public class SubTypeHandlingUnitTest { public void givenSubType_whenNotUsingNoArgsConstructors_thenSucceed() throws IOException { ObjectMapper mapper = new ObjectMapper(); PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder() - .allowIfSubType("com.baeldung.jackson.inheritance") - .allowIfSubType("java.util.ArrayList") - .build(); + .allowIfSubType("com.baeldung.jackson.inheritance") + .allowIfSubType("java.util.ArrayList") + .build(); mapper.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.NON_FINAL); Car car = new Car("Mercedes-Benz", "S500", 5, 250.0); From f8bea1acc2e1d871e6cbdb1148f851d798ed885e Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Sat, 11 Sep 2021 15:05:02 +0530 Subject: [PATCH 036/118] JAVA-6439: review comments incorporated --- spring-security-modules/spring-security-web-sockets/README.md | 2 +- spring-security-modules/spring-security-web-sockets/pom.xml | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/spring-security-modules/spring-security-web-sockets/README.md b/spring-security-modules/spring-security-web-sockets/README.md index 12f7a6ad56..34e06fa832 100644 --- a/spring-security-modules/spring-security-web-sockets/README.md +++ b/spring-security-modules/spring-security-web-sockets/README.md @@ -10,7 +10,7 @@ This module contains articles about WebSockets with Spring Security ### Running This Project: -To build the project, run the command: `mvn clean install`. This will build a war file in the target folder that you can deploye on a server like Tomcat. +To build the project, run the command: `mvn clean install`. This will build a war file in the target folder that you can deploy on a server like Tomcat. Alternatively, run the project from an IDE, with the maven goal `org.codehaus.cargo:cargo-maven2-plugin:run` diff --git a/spring-security-modules/spring-security-web-sockets/pom.xml b/spring-security-modules/spring-security-web-sockets/pom.xml index db0d280da8..7b7a5e4659 100644 --- a/spring-security-modules/spring-security-web-sockets/pom.xml +++ b/spring-security-modules/spring-security-web-sockets/pom.xml @@ -157,7 +157,7 @@ org.codehaus.cargo cargo-maven2-plugin - 1.7.6 + ${cargo-maven2-plugin.version} tomcat9x @@ -182,6 +182,7 @@ 1.11.3.RELEASE 1.2.3 1.5.10.RELEASE + 1.7.6 \ No newline at end of file From 34af1ae63d1f17143a39562350451a3c83086f81 Mon Sep 17 00:00:00 2001 From: rvsathe Date: Sat, 11 Sep 2021 16:24:30 +0530 Subject: [PATCH 037/118] BAEL 4293 changed the pom.xml formatting. --- core-java-modules/core-java-os/pom.xml | 175 +++++++++++++------------ 1 file changed, 88 insertions(+), 87 deletions(-) diff --git a/core-java-modules/core-java-os/pom.xml b/core-java-modules/core-java-os/pom.xml index 04db6a53f3..d088014816 100644 --- a/core-java-modules/core-java-os/pom.xml +++ b/core-java-modules/core-java-os/pom.xml @@ -1,99 +1,100 @@ - 4.0.0 - core-java-os - 0.1.0-SNAPSHOT - core-java-os - jar + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + core-java-os + 0.1.0-SNAPSHOT + core-java-os + jar - - com.baeldung.core-java-modules - core-java-modules - 0.0.1-SNAPSHOT - ../ - + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + ../ + - - + + org.junit.jupiter junit-jupiter-engine - 5.7.2 + ${junit-jupiter-engine.version} test - - org.apache.commons - commons-collections4 - ${commons-collections4.version} - - - commons-io - commons-io - ${commons-io.version} - - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - - - log4j - log4j - ${log4j.version} - - - - org.assertj - assertj-core - ${assertj.version} - test - - - org.unix4j - unix4j-command - ${unix4j.version} - - - com.googlecode.grep4j - grep4j - ${grep4j.version} - - + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + commons-io + commons-io + ${commons-io.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + log4j + log4j + ${log4j.version} + + + + org.assertj + assertj-core + ${assertj.version} + test + + + org.unix4j + unix4j-command + ${unix4j.version} + + + com.googlecode.grep4j + grep4j + ${grep4j.version} + + - - core-java-os - - - src/main/resources - true - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - ${maven.compiler.source} - ${maven.compiler.target} - - - - + + core-java-os + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + - - - 4.1 - 4.01 - - 3.6.1 - 1.8.9 - 1.9 - 1.9 - 25.1-jre - 0.4 - 1.8.7 - + + + 4.1 + 4.01 + + 3.6.1 + 1.8.9 + 1.9 + 1.9 + 25.1-jre + 0.4 + 1.8.7 + 5.7.2 + \ No newline at end of file From 23c11caf77af22c08a088eba4d20dee99a14477d Mon Sep 17 00:00:00 2001 From: Ashish Gupta <30566001+gupta-ashu01@users.noreply.github.com> Date: Sun, 12 Sep 2021 13:19:16 +0530 Subject: [PATCH 038/118] Update EmailValidation.java --- .../main/java/com/baeldung/emailvalidation/EmailValidation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java b/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java index 4d68ea9c55..f80c87ea01 100644 --- a/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java +++ b/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java @@ -4,7 +4,7 @@ import java.util.regex.Pattern; public class EmailValidation { - public static boolean patternMatcher(String emailAddress, String regexPattern) { + public static boolean patternMatches(String emailAddress, String regexPattern) { return Pattern.compile(regexPattern) .matcher(emailAddress) .matches(); From c942476ebaedae8622e9b54fdae21ef05f78ecfa Mon Sep 17 00:00:00 2001 From: Ashish Gupta <30566001+gupta-ashu01@users.noreply.github.com> Date: Sun, 12 Sep 2021 13:20:42 +0530 Subject: [PATCH 039/118] Update EmailValidationUnitTest.java --- .../EmailValidationUnitTest.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java index c49690289a..a59aded293 100644 --- a/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java +++ b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java @@ -20,7 +20,7 @@ public class EmailValidationUnitTest { public void testUsingSimpleRegex() { emailAddress = "username@domain.com"; regexPattern = "^(.+)@(\\S+)$"; - assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); + assertTrue(EmailValidation.patternMatches(emailAddress, regexPattern)); } @Test @@ -28,7 +28,7 @@ public class EmailValidationUnitTest { emailAddress = "username@domain.com"; regexPattern = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@" + "[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"; - assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); + assertTrue(EmailValidation.patternMatches(emailAddress, regexPattern)); } @Test @@ -36,14 +36,14 @@ public class EmailValidationUnitTest { emailAddress = "用户名@领域.电脑"; regexPattern = "^(?=.{1,64}@)[\\p{L}0-9_-]+(\\.[\\p{L}0-9_-]+)*@" + "[^-][\\p{L}0-9-]+(\\.[\\p{L}0-9-]+)*(\\.[\\p{L}]{2,})$"; - assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); + assertTrue(EmailValidation.patternMatches(emailAddress, regexPattern)); } @Test public void testUsingRFC5322Regex() { emailAddress = "username@domain.com"; regexPattern = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$"; - assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); + assertTrue(EmailValidation.patternMatches(emailAddress, regexPattern)); } @Test @@ -51,14 +51,14 @@ public class EmailValidationUnitTest { emailAddress = "username@domain.com"; regexPattern = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@" + "[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"; - assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); + assertTrue(EmailValidation.patternMatches(emailAddress, regexPattern)); } @Test public void testOwaspValidation() { emailAddress = "username@domain.com"; regexPattern = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; - assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); + assertTrue(EmailValidation.patternMatches(emailAddress, regexPattern)); } @Test @@ -66,6 +66,14 @@ public class EmailValidationUnitTest { emailAddress = "username@domain.com"; regexPattern = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*" + "@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"; - assertTrue(EmailValidation.patternMatcher(emailAddress, regexPattern)); + assertTrue(EmailValidation.patternMatches(emailAddress, regexPattern)); + } + + @Test + public void testGmailSpecialCase() { + emailAddress = "username+something@domain.com"; + regexPattern = "^(?=.{1,64}@)[A-Za-z0-9_-+]+(\\\\.[A-Za-z0-9_-+]+)*@[^-][A-Za-z0-9-+]+" + + "(\\\\.[A-Za-z0-9-+]+)*(\\\\.[A-Za-z]{2,})$\r\n"; + assertTrue(EmailValidation.patternMatches(emailAddress, regexPattern)); } } From f73f1c2fdfb5c683c69c5b0b3439846ce7447e90 Mon Sep 17 00:00:00 2001 From: Ashish Gupta <30566001+gupta-ashu01@users.noreply.github.com> Date: Sun, 12 Sep 2021 14:53:30 +0530 Subject: [PATCH 040/118] Update EmailValidationUnitTest.java --- .../com/baeldung/emailvalidation/EmailValidationUnitTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java index a59aded293..e6bee0ce63 100644 --- a/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java +++ b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/emailvalidation/EmailValidationUnitTest.java @@ -72,8 +72,8 @@ public class EmailValidationUnitTest { @Test public void testGmailSpecialCase() { emailAddress = "username+something@domain.com"; - regexPattern = "^(?=.{1,64}@)[A-Za-z0-9_-+]+(\\\\.[A-Za-z0-9_-+]+)*@[^-][A-Za-z0-9-+]+" - + "(\\\\.[A-Za-z0-9-+]+)*(\\\\.[A-Za-z]{2,})$\r\n"; + regexPattern = "^(?=.{1,64}@)[A-Za-z0-9\\+_-]+(\\.[A-Za-z0-9\\+_-]+)*@" + + "[^-][A-Za-z0-9\\+-]+(\\.[A-Za-z0-9\\+-]+)*(\\.[A-Za-z]{2,})$"; assertTrue(EmailValidation.patternMatches(emailAddress, regexPattern)); } } From 141a837679a0e32ca2191ebd1e550b4979a29464 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Mon, 13 Sep 2021 17:18:32 +0200 Subject: [PATCH 041/118] BAEL-5061 Joinpoint vs. ProceedingJoinPoint in AspectJ (#11209) * BAEL-5061 Joinpoint vs. ProceedingJoinPoint in AspectJ * BAEL-5061 Joinpoint vs. ProceedingJoinPoint in AspectJ * BAEL-5061 Joinpoint vs. ProceedingJoinPoint in AspectJ * BAEL-5061 Joinpoint vs. ProceedingJoinPoint in AspectJ * BAEL-5061 Joinpoint vs. ProceedingJoinPoint in AspectJ * BAEL-5061 Joinpoint vs. ProceedingJoinPoint in AspectJ - add tests * BAEL-5061 Joinpoint vs. ProceedingJoinPoint in AspectJ - add tests * BAEL-5061 Joinpoint vs. ProceedingJoinPoint in AspectJ - add tests * BAEL-5061 Joinpoint vs. ProceedingJoinPoint in AspectJ - add tests Co-authored-by: majewsk6 --- .../baeldung/joinpoint/ArticleService.java | 32 ++++++++++ .../JoinPointAfterThrowingAspect.java | 27 +++++++++ .../joinpoint/JoinPointAroundCacheAspect.java | 30 ++++++++++ .../JoinPointAroundExceptionAspect.java | 30 ++++++++++ .../joinpoint/JoinPointBeforeAspect.java | 32 ++++++++++ .../ArticleServiceIntegrationTest.java | 39 ++++++++++++ ...intAfterThrowingAspectIntegrationTest.java | 60 +++++++++++++++++++ ...PointAroundCacheAspectIntegrationTest.java | 35 +++++++++++ ...tAroundExceptionAspectIntegrationTest.java | 60 +++++++++++++++++++ .../JoinPointBeforeAspectIntegrationTest.java | 60 +++++++++++++++++++ 10 files changed, 405 insertions(+) create mode 100644 spring-aop/src/main/java/com/baeldung/joinpoint/ArticleService.java create mode 100644 spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointAfterThrowingAspect.java create mode 100644 spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointAroundCacheAspect.java create mode 100644 spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointAroundExceptionAspect.java create mode 100644 spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointBeforeAspect.java create mode 100644 spring-aop/src/test/java/com/baeldung/joinpoint/ArticleServiceIntegrationTest.java create mode 100644 spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointAfterThrowingAspectIntegrationTest.java create mode 100644 spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointAroundCacheAspectIntegrationTest.java create mode 100644 spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointAroundExceptionAspectIntegrationTest.java create mode 100644 spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointBeforeAspectIntegrationTest.java diff --git a/spring-aop/src/main/java/com/baeldung/joinpoint/ArticleService.java b/spring-aop/src/main/java/com/baeldung/joinpoint/ArticleService.java new file mode 100644 index 0000000000..5a6b5ce63a --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/joinpoint/ArticleService.java @@ -0,0 +1,32 @@ +package com.baeldung.joinpoint; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +import java.util.Arrays; +import java.util.List; + +import static java.util.stream.Collectors.toList; + +@Service +public class ArticleService { + + public List getArticleList() { + return Arrays.asList( + "Article 1", + "Article 2" + ); + } + + public List getArticleList(String startsWithFilter) { + if (StringUtils.isBlank(startsWithFilter)) { + throw new IllegalArgumentException("startsWithFilter can't be blank"); + } + + return getArticleList() + .stream() + .filter(a -> a.startsWith(startsWithFilter)) + .collect(toList()); + } + +} diff --git a/spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointAfterThrowingAspect.java b/spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointAfterThrowingAspect.java new file mode 100644 index 0000000000..a1f991e90d --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointAfterThrowingAspect.java @@ -0,0 +1,27 @@ +package com.baeldung.joinpoint; + +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.AfterThrowing; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; + +import java.util.logging.Logger; + +@Aspect +@Component +public class JoinPointAfterThrowingAspect { + + private static final java.util.logging.Logger log = Logger.getLogger(JoinPointAfterThrowingAspect.class.getName()); + + @Pointcut("execution(* com.baeldung.joinpoint.ArticleService.getArticleList(..))") + public void articleListPointcut() { } + + @AfterThrowing( + pointcut = "articleListPointcut()", + throwing = "e" + ) + public void logExceptions(JoinPoint jp, Exception e) { + log.severe(e.getMessage()); + } +} diff --git a/spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointAroundCacheAspect.java b/spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointAroundCacheAspect.java new file mode 100644 index 0000000000..dbf2b2e1e4 --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointAroundCacheAspect.java @@ -0,0 +1,30 @@ +package com.baeldung.joinpoint; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +@Aspect +@Component +public class JoinPointAroundCacheAspect { + + public final static Map CACHE = new HashMap<>(); + + @Pointcut("execution(* com.baeldung.joinpoint.ArticleService.getArticleList(..))") + public void articleListPointcut() { } + + @Around("articleListPointcut()") + public Object aroundAdviceCache(ProceedingJoinPoint pjp) throws Throwable { + Object articles = CACHE.get(pjp.getArgs()); + if (articles == null) { + articles = pjp.proceed(pjp.getArgs()); + CACHE.put(pjp.getArgs(), articles); + } + return articles; + } +} diff --git a/spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointAroundExceptionAspect.java b/spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointAroundExceptionAspect.java new file mode 100644 index 0000000000..8d15de150d --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointAroundExceptionAspect.java @@ -0,0 +1,30 @@ +package com.baeldung.joinpoint; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; + +import java.util.logging.Logger; + +@Aspect +@Component +public class JoinPointAroundExceptionAspect { + + private static final java.util.logging.Logger log = Logger.getLogger(JoinPointAroundExceptionAspect.class.getName()); + + @Pointcut("execution(* com.baeldung.joinpoint.ArticleService.getArticleList(..))") + public void articleListPointcut() { } + + @Around("articleListPointcut()") + public Object aroundAdviceException(ProceedingJoinPoint pjp) throws Throwable { + try { + return pjp.proceed(pjp.getArgs()); + } catch (Throwable e) { + log.severe(e.getMessage()); + log.info("Retrying operation"); + return pjp.proceed(pjp.getArgs()); + } + } +} diff --git a/spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointBeforeAspect.java b/spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointBeforeAspect.java new file mode 100644 index 0000000000..4485363ff9 --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/joinpoint/JoinPointBeforeAspect.java @@ -0,0 +1,32 @@ +package com.baeldung.joinpoint; + +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.logging.Logger; + +import static java.lang.String.format; + +@Aspect +@Component +public class JoinPointBeforeAspect { + + private static final Logger log = Logger.getLogger(JoinPointBeforeAspect.class.getName()); + + @Pointcut("execution(* com.baeldung.joinpoint.ArticleService.getArticleList(..))") + public void articleListPointcut() { } + + @Before("articleListPointcut()") + public void beforeAdvice(JoinPoint joinPoint) { + log.info( + format("Method %s executed with %s arguments", + joinPoint.getStaticPart().getSignature(), + Arrays.toString(joinPoint.getArgs()) + ) + ); + } +} diff --git a/spring-aop/src/test/java/com/baeldung/joinpoint/ArticleServiceIntegrationTest.java b/spring-aop/src/test/java/com/baeldung/joinpoint/ArticleServiceIntegrationTest.java new file mode 100644 index 0000000000..a67b843262 --- /dev/null +++ b/spring-aop/src/test/java/com/baeldung/joinpoint/ArticleServiceIntegrationTest.java @@ -0,0 +1,39 @@ +package com.baeldung.joinpoint; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.List; + +import static org.junit.Assert.assertFalse; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +public class ArticleServiceIntegrationTest { + + @Autowired + private ArticleService articleService; + + @Test + public void shouldGetNotEmptyArticleList() { + List articleList = articleService.getArticleList(); + + assertFalse(articleList.isEmpty()); + } + + @Test + public void shouldGetNotEmptyArticleListWithStartsWithFilter() { + List articleList = articleService.getArticleList("Article"); + + assertFalse(articleList.isEmpty()); + } + + @Test(expected = IllegalArgumentException.class) + public void shouldThrowExceptionIfStartsWithFilterIsBlank() { + articleService.getArticleList(" "); + } + +} diff --git a/spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointAfterThrowingAspectIntegrationTest.java b/spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointAfterThrowingAspectIntegrationTest.java new file mode 100644 index 0000000000..cdef76ac0d --- /dev/null +++ b/spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointAfterThrowingAspectIntegrationTest.java @@ -0,0 +1,60 @@ +package com.baeldung.joinpoint; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Handler; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +@EnableAspectJAutoProxy +public class JoinPointAfterThrowingAspectIntegrationTest { + + private final List messages = new ArrayList<>(); + + @Before + public void setUp() { + Handler logEventHandler = new Handler() { + @Override + public void publish(LogRecord record) { + messages.add(record.getLevel().getName() + " " + record.getMessage()); + } + + @Override + public void flush() { + } + + @Override + public void close() throws SecurityException { + } + }; + + Logger logger = Logger.getLogger(JoinPointAfterThrowingAspect.class.getName()); + logger.addHandler(logEventHandler); + } + + @Autowired + private ArticleService articleService; + + @Test(expected = IllegalArgumentException.class) + public void shouldLogMethodSignatureBeforeExecution() { + articleService.getArticleList(" "); + + assertThat(messages, hasSize(1)); + assertTrue(messages.contains("SEVERE startsWithFilter can't be blank")); + } + +} diff --git a/spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointAroundCacheAspectIntegrationTest.java b/spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointAroundCacheAspectIntegrationTest.java new file mode 100644 index 0000000000..4bd8f2ad8e --- /dev/null +++ b/spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointAroundCacheAspectIntegrationTest.java @@ -0,0 +1,35 @@ +package com.baeldung.joinpoint; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +@EnableAspectJAutoProxy +public class JoinPointAroundCacheAspectIntegrationTest { + + @Autowired + private ArticleService articleService; + + @Test + public void shouldPopulateCache() { + assertTrue(JoinPointAroundCacheAspect.CACHE.isEmpty()); + + List articles = articleService.getArticleList(); + + assertFalse(JoinPointAroundCacheAspect.CACHE.isEmpty()); + assertEquals(JoinPointAroundCacheAspect.CACHE.size(), 1); + assertEquals(JoinPointAroundCacheAspect.CACHE.values().iterator().next(), articles); + } + +} diff --git a/spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointAroundExceptionAspectIntegrationTest.java b/spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointAroundExceptionAspectIntegrationTest.java new file mode 100644 index 0000000000..dbdda30c15 --- /dev/null +++ b/spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointAroundExceptionAspectIntegrationTest.java @@ -0,0 +1,60 @@ +package com.baeldung.joinpoint; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Handler; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +@EnableAspectJAutoProxy +public class JoinPointAroundExceptionAspectIntegrationTest { + + private final List messages = new ArrayList<>(); + + @Before + public void setUp() { + Handler logEventHandler = new Handler() { + @Override + public void publish(LogRecord record) { + messages.add(record.getLevel().getName() + " " + record.getMessage()); + } + + @Override + public void flush() { + } + + @Override + public void close() throws SecurityException { + } + }; + + Logger logger = Logger.getLogger(JoinPointAroundExceptionAspect.class.getName()); + logger.addHandler(logEventHandler); + } + + @Autowired + private ArticleService articleService; + + @Test(expected = IllegalArgumentException.class) + public void shouldLogMethodSignatureBeforeExecution() { + articleService.getArticleList(" "); + + assertThat(messages, hasSize(1)); + assertTrue(messages.contains("INFO Retrying operation")); + } + +} diff --git a/spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointBeforeAspectIntegrationTest.java b/spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointBeforeAspectIntegrationTest.java new file mode 100644 index 0000000000..631852d57f --- /dev/null +++ b/spring-aop/src/test/java/com/baeldung/joinpoint/JoinPointBeforeAspectIntegrationTest.java @@ -0,0 +1,60 @@ +package com.baeldung.joinpoint; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Handler; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +@EnableAspectJAutoProxy +public class JoinPointBeforeAspectIntegrationTest { + + private final List messages = new ArrayList<>(); + + @Before + public void setUp() { + Handler logEventHandler = new Handler() { + @Override + public void publish(LogRecord record) { + messages.add(record.getLevel().getName() + " " + record.getMessage()); + } + + @Override + public void flush() { + } + + @Override + public void close() throws SecurityException { + } + }; + + Logger logger = Logger.getLogger(JoinPointBeforeAspect.class.getName()); + logger.addHandler(logEventHandler); + } + + @Autowired + private ArticleService articleService; + + @Test + public void shouldLogMethodSignatureBeforeExecution() { + articleService.getArticleList(); + + assertThat(messages, hasSize(1)); + assertTrue(messages.contains("INFO Method List com.baeldung.joinpoint.ArticleService.getArticleList() executed with [] arguments")); + } + +} From 3304dad9c5952a904a45dcf39fb84e0fe209f0e2 Mon Sep 17 00:00:00 2001 From: Carlos Cavero Date: Mon, 13 Sep 2021 17:42:04 +0200 Subject: [PATCH 042/118] Add custom serializer and deserializer including the message dto based in lombok --- apache-kafka/pom.xml | 7 ++ .../com/baeldung/kafka/dto/MessageDto.java | 15 +++ .../kafka/serdes/CustomDeserializer.java | 35 +++++++ .../kafka/serdes/CustomSerializer.java | 34 +++++++ .../kafka/serdes/KafkaSerDesLiveTest.java | 92 +++++++++++++++++++ 5 files changed, 183 insertions(+) create mode 100644 apache-kafka/src/main/java/com/baeldung/kafka/dto/MessageDto.java create mode 100644 apache-kafka/src/main/java/com/baeldung/kafka/serdes/CustomDeserializer.java create mode 100644 apache-kafka/src/main/java/com/baeldung/kafka/serdes/CustomSerializer.java create mode 100644 apache-kafka/src/test/java/com/baeldung/kafka/serdes/KafkaSerDesLiveTest.java diff --git a/apache-kafka/pom.xml b/apache-kafka/pom.xml index cda91ed92f..8003743f95 100644 --- a/apache-kafka/pom.xml +++ b/apache-kafka/pom.xml @@ -161,6 +161,12 @@ spark-cassandra-connector-java_2.11 ${com.datastax.spark.spark-cassandra-connector-java.version} + + org.projectlombok + lombok + ${lombok.version} + provided + @@ -175,6 +181,7 @@ 0.8.1-spark3.0-s_2.12 2.5.2 1.6.0-M1 + 1.18.20 \ No newline at end of file diff --git a/apache-kafka/src/main/java/com/baeldung/kafka/dto/MessageDto.java b/apache-kafka/src/main/java/com/baeldung/kafka/dto/MessageDto.java new file mode 100644 index 0000000000..7f9e206358 --- /dev/null +++ b/apache-kafka/src/main/java/com/baeldung/kafka/dto/MessageDto.java @@ -0,0 +1,15 @@ +package com.baeldung.kafka.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class MessageDto { + private String message; + private String version; +} diff --git a/apache-kafka/src/main/java/com/baeldung/kafka/serdes/CustomDeserializer.java b/apache-kafka/src/main/java/com/baeldung/kafka/serdes/CustomDeserializer.java new file mode 100644 index 0000000000..ee6e79dcd1 --- /dev/null +++ b/apache-kafka/src/main/java/com/baeldung/kafka/serdes/CustomDeserializer.java @@ -0,0 +1,35 @@ +package com.baeldung.kafka.serdes; + +import com.baeldung.kafka.dto.MessageDto; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.kafka.common.errors.SerializationException; +import org.apache.kafka.common.serialization.Deserializer; + +import java.util.Map; + +public class CustomDeserializer implements Deserializer { + + private ObjectMapper objectMapper = new ObjectMapper(); + + @Override + public void configure(Map configs, boolean isKey) { + } + + @Override + public MessageDto deserialize(String topic, byte[] data) { + try { + if (data == null){ + System.out.println("Null received at deserializing"); + return null; + } + System.out.println("Deserializing..."); + return objectMapper.readValue(new String(data, "UTF-8"), MessageDto.class); + } catch (Exception e) { + throw new SerializationException("Error when deserializing byte[] to MessageDto"); + } + } + + @Override + public void close() { + } +} diff --git a/apache-kafka/src/main/java/com/baeldung/kafka/serdes/CustomSerializer.java b/apache-kafka/src/main/java/com/baeldung/kafka/serdes/CustomSerializer.java new file mode 100644 index 0000000000..a414ad8e23 --- /dev/null +++ b/apache-kafka/src/main/java/com/baeldung/kafka/serdes/CustomSerializer.java @@ -0,0 +1,34 @@ +package com.baeldung.kafka.serdes; + +import com.baeldung.kafka.dto.MessageDto; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.kafka.common.errors.SerializationException; +import org.apache.kafka.common.serialization.Serializer; + +import java.util.Map; + +public class CustomSerializer implements Serializer { + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Override + public void configure(Map configs, boolean isKey) { + } + + @Override + public byte[] serialize(String topic, MessageDto data) { + try { + if (data == null){ + System.out.println("Null received at serializing"); + return null; + } + System.out.println("Serializing..."); + return objectMapper.writeValueAsBytes(data); + } catch (Exception e) { + throw new SerializationException("Error when serializing MessageDto to byte[]"); + } + } + + @Override + public void close() { + } +} diff --git a/apache-kafka/src/test/java/com/baeldung/kafka/serdes/KafkaSerDesLiveTest.java b/apache-kafka/src/test/java/com/baeldung/kafka/serdes/KafkaSerDesLiveTest.java new file mode 100644 index 0000000000..67fff12f5e --- /dev/null +++ b/apache-kafka/src/test/java/com/baeldung/kafka/serdes/KafkaSerDesLiveTest.java @@ -0,0 +1,92 @@ +package com.baeldung.kafka.serdes; + +import com.baeldung.kafka.dto.MessageDto; +import org.apache.kafka.clients.consumer.ConsumerConfig; +import org.apache.kafka.clients.consumer.ConsumerRecords; +import org.apache.kafka.clients.consumer.KafkaConsumer; +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.ProducerConfig; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.testcontainers.containers.KafkaContainer; +import org.testcontainers.utility.DockerImageName; + +import java.time.Duration; +import java.util.Arrays; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.Assert.assertEquals; + +public class KafkaSerDesLiveTest { + private static final String CONSUMER_APP_ID = "consumer_id"; + private static final String CONSUMER_GROUP_ID = "group_id"; + + @ClassRule + public static KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.4.3")); + private final String TOPIC = "mytopic"; + + private static KafkaConsumer createKafkaConsumer() { + + Properties props = new Properties(); + props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers()); + props.put(ConsumerConfig.CLIENT_ID_CONFIG, CONSUMER_APP_ID); + props.put(ConsumerConfig.GROUP_ID_CONFIG, CONSUMER_GROUP_ID); + props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); + props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer"); + props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "com.baeldung.kafka.serdes.CustomDeserializer"); + + return new KafkaConsumer<>(props); + + } + + private static KafkaProducer createKafkaProducer() { + + Properties props = new Properties(); + props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers()); + props.put(ProducerConfig.CLIENT_ID_CONFIG, CONSUMER_APP_ID); + props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer"); + props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "com.baeldung.kafka.serdes.CustomSerializer"); + + return new KafkaProducer(props); + + } + + @Before + public void setUp() { + } + + @Test + public void givenKafkaClientShouldSerializeAndDeserialize() throws InterruptedException { + + MessageDto msgProd = MessageDto.builder().message("test").version("1.0").build(); + + KafkaProducer producer = createKafkaProducer(); + producer.send(new ProducerRecord(TOPIC, "1", msgProd)); + System.out.println("Message sent " + msgProd); + producer.close(); + + Thread.sleep(2000); + + AtomicReference msgCons = new AtomicReference<>(); + + KafkaConsumer consumer = createKafkaConsumer(); + consumer.subscribe(Arrays.asList(TOPIC)); + + ConsumerRecords records = consumer.poll(Duration.ofSeconds(1)); + records.forEach(record -> { + msgCons.set(record.value()); + System.out.println("Message received " + record.value()); + }); + + consumer.close(); + + assertEquals(msgProd, msgCons.get()); + + } + +} + + From 6a638120dc5afe2ad88c8066fbd123987df7e3fe Mon Sep 17 00:00:00 2001 From: JoannaaKL <67866556+JoannaaKL@users.noreply.github.com> Date: Tue, 14 Sep 2021 03:01:26 +0200 Subject: [PATCH 043/118] Jira BAEL-4211 (#11193) * 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 Co-authored-by: asia --- .../pom.xml | 40 +++++++++++++++++ .../BenchmarkRunner.java | 11 +++++ .../ObjectsCopyBenchmark.java | 42 ++++++++++++++++++ .../PrimitivesCopyBenchmark.java | 43 +++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/copyarraymethodsperformance/BenchmarkRunner.java create mode 100644 core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/copyarraymethodsperformance/ObjectsCopyBenchmark.java create mode 100644 core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/copyarraymethodsperformance/PrimitivesCopyBenchmark.java diff --git a/core-java-modules/core-java-arrays-operations-advanced/pom.xml b/core-java-modules/core-java-arrays-operations-advanced/pom.xml index 5663a7d1ca..065f1930e2 100644 --- a/core-java-modules/core-java-arrays-operations-advanced/pom.xml +++ b/core-java-modules/core-java-arrays-operations-advanced/pom.xml @@ -26,10 +26,50 @@ ${assertj-core.version} test + + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + 3.10.0 + 1.33 + + + + maven-assembly-plugin + + + jar-with-dependencies + + + + com.baeldung.copyarraymethodsperformance.BenchmarkRunner + + + + + + make-assembly + package + + single + + + + + + + \ No newline at end of file diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/copyarraymethodsperformance/BenchmarkRunner.java b/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/copyarraymethodsperformance/BenchmarkRunner.java new file mode 100644 index 0000000000..0adbcc1986 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/copyarraymethodsperformance/BenchmarkRunner.java @@ -0,0 +1,11 @@ +package com.baeldung.copyarraymethodsperformance; + +public class BenchmarkRunner { + + public static void main(String[] args) throws Exception { + + org.openjdk.jmh.Main.main(args); + + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/copyarraymethodsperformance/ObjectsCopyBenchmark.java b/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/copyarraymethodsperformance/ObjectsCopyBenchmark.java new file mode 100644 index 0000000000..6a492bf0d1 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/copyarraymethodsperformance/ObjectsCopyBenchmark.java @@ -0,0 +1,42 @@ +package com.baeldung.copyarraymethodsperformance; + +import org.openjdk.jmh.annotations.*; + +import java.util.Arrays; +import java.util.Random; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@State(Scope.Thread) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Warmup(iterations = 10) +@Fork(1) +@Measurement(iterations = 100) +public class ObjectsCopyBenchmark { + + @Param({ "10", "1000000" }) + public int SIZE; + Integer[] src; + + @Setup + public void setup() { + Random r = new Random(); + src = new Integer[SIZE]; + + for (int i = 0; i < SIZE; i++) { + src[i] = r.nextInt(); + } + } + + @Benchmark + public Integer[] systemArrayCopyBenchmark() { + Integer[] target = new Integer[SIZE]; + System.arraycopy(src, 0, target, 0, SIZE); + return target; + } + + @Benchmark + public Integer[] arraysCopyOfBenchmark() { + return Arrays.copyOf(src, SIZE); + } +} diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/copyarraymethodsperformance/PrimitivesCopyBenchmark.java b/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/copyarraymethodsperformance/PrimitivesCopyBenchmark.java new file mode 100644 index 0000000000..767e91a350 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/copyarraymethodsperformance/PrimitivesCopyBenchmark.java @@ -0,0 +1,43 @@ +package com.baeldung.copyarraymethodsperformance; + +import org.openjdk.jmh.annotations.*; + +import java.util.Arrays; +import java.util.Random; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@State(Scope.Thread) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Warmup(iterations = 10) +@Fork(1) +@Measurement(iterations = 100) +public class PrimitivesCopyBenchmark { + + @Param({ "10", "1000000" }) + public int SIZE; + + int[] src; + + @Setup + public void setup() { + Random r = new Random(); + src = new int[SIZE]; + + for (int i = 0; i < SIZE; i++) { + src[i] = r.nextInt(); + } + } + + @Benchmark + public int[] systemArrayCopyBenchmark() { + int[] target = new int[SIZE]; + System.arraycopy(src, 0, target, 0, SIZE); + return target; + } + + @Benchmark + public int[] arraysCopyOfBenchmark() { + return Arrays.copyOf(src, SIZE); + } +} From 28d708a2fa3f760b135793a398d1b8062e5d7df4 Mon Sep 17 00:00:00 2001 From: 515882294 <515882294@qq.com> Date: Tue, 14 Sep 2021 19:41:34 +0800 Subject: [PATCH 044/118] Add new section on instanceof --- .../IsInstanceIsAssignableFromUnitTest.java | 74 ++++++++++++++++--- 1 file changed, 62 insertions(+), 12 deletions(-) diff --git a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/isinstancevsisassignablefrom/IsInstanceIsAssignableFromUnitTest.java b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/isinstancevsisassignablefrom/IsInstanceIsAssignableFromUnitTest.java index e0ad264797..3e4ee0a1bb 100644 --- a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/isinstancevsisassignablefrom/IsInstanceIsAssignableFromUnitTest.java +++ b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/isinstancevsisassignablefrom/IsInstanceIsAssignableFromUnitTest.java @@ -6,57 +6,80 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; public class IsInstanceIsAssignableFromUnitTest { - + @Test - public void whenUsingIsInstanceProperly_desiredResultsHappen() { + public void whenUsingInstanceOfProperly_thenDesiredResultsHappen() { + Shape shape = new Triangle(); + Triangle triangle = new Triangle(); + IsoscelesTriangle isoscelesTriangle = new IsoscelesTriangle(); + Shape nonspecificShape = null; + + assertTrue(shape instanceof Shape); + assertTrue(triangle instanceof Shape); + assertTrue(isoscelesTriangle instanceof Shape); + assertFalse(nonspecificShape instanceof Shape); + + assertTrue(shape instanceof Triangle); + assertTrue(triangle instanceof Triangle); + assertTrue(isoscelesTriangle instanceof Triangle); + assertFalse(nonspecificShape instanceof Triangle); + + assertFalse(shape instanceof IsoscelesTriangle); + assertFalse(triangle instanceof IsoscelesTriangle); + assertTrue(isoscelesTriangle instanceof IsoscelesTriangle); + assertFalse(nonspecificShape instanceof IsoscelesTriangle); + } + + @Test + public void whenUsingIsInstanceProperly_thenDesiredResultsHappen() { Shape shape = new Triangle(); Triangle triangle = new Triangle(); IsoscelesTriangle isoscelesTriangle = new IsoscelesTriangle(); Triangle isoscelesTriangle2 = new IsoscelesTriangle(); Shape nonspecificShape = null; - + assertTrue(Shape.class.isInstance(shape)); assertTrue(Shape.class.isInstance(triangle)); assertTrue(Shape.class.isInstance(isoscelesTriangle)); assertTrue(Shape.class.isInstance(isoscelesTriangle2)); assertFalse(Shape.class.isInstance(nonspecificShape)); - + assertTrue(Triangle.class.isInstance(shape)); assertTrue(Triangle.class.isInstance(triangle)); assertTrue(Triangle.class.isInstance(isoscelesTriangle)); assertTrue(Triangle.class.isInstance(isoscelesTriangle2)); - + assertFalse(IsoscelesTriangle.class.isInstance(shape)); assertFalse(IsoscelesTriangle.class.isInstance(triangle)); assertTrue(IsoscelesTriangle.class.isInstance(isoscelesTriangle)); - assertTrue(IsoscelesTriangle.class.isInstance(isoscelesTriangle2)); + assertTrue(IsoscelesTriangle.class.isInstance(isoscelesTriangle2)); } - + @Test - public void whenUsingIsAssignableFromProperly_desiredResultsHappen() { + public void whenUsingIsAssignableFromProperly_thenDesiredResultsHappen() { Shape shape = new Triangle(); Triangle triangle = new Triangle(); IsoscelesTriangle isoscelesTriangle = new IsoscelesTriangle(); Triangle isoscelesTriangle2 = new IsoscelesTriangle(); - + assertFalse(shape.getClass().isAssignableFrom(Shape.class)); assertTrue(shape.getClass().isAssignableFrom(shape.getClass())); assertTrue(shape.getClass().isAssignableFrom(triangle.getClass())); assertTrue(shape.getClass().isAssignableFrom(isoscelesTriangle.getClass())); assertTrue(shape.getClass().isAssignableFrom(isoscelesTriangle2.getClass())); - + assertFalse(triangle.getClass().isAssignableFrom(Shape.class)); assertTrue(triangle.getClass().isAssignableFrom(shape.getClass())); assertTrue(triangle.getClass().isAssignableFrom(triangle.getClass())); assertTrue(triangle.getClass().isAssignableFrom(isoscelesTriangle.getClass())); assertTrue(triangle.getClass().isAssignableFrom(isoscelesTriangle2.getClass())); - + assertFalse(isoscelesTriangle.getClass().isAssignableFrom(Shape.class)); assertFalse(isoscelesTriangle.getClass().isAssignableFrom(shape.getClass())); assertFalse(isoscelesTriangle.getClass().isAssignableFrom(triangle.getClass())); assertTrue(isoscelesTriangle.getClass().isAssignableFrom(isoscelesTriangle.getClass())); assertTrue(isoscelesTriangle.getClass().isAssignableFrom(isoscelesTriangle2.getClass())); - + assertFalse(isoscelesTriangle2.getClass().isAssignableFrom(Shape.class)); assertFalse(isoscelesTriangle2.getClass().isAssignableFrom(shape.getClass())); assertFalse(isoscelesTriangle2.getClass().isAssignableFrom(triangle.getClass())); @@ -64,4 +87,31 @@ public class IsInstanceIsAssignableFromUnitTest { assertTrue(isoscelesTriangle2.getClass().isAssignableFrom(isoscelesTriangle2.getClass())); } + @Test(expected = NullPointerException.class) + public void whenUsingNull_thenDesiredResultsHappen() { + assertFalse(null instanceof Shape); + assertFalse(Shape.class.isInstance(null)); + assertFalse(Shape.class.isAssignableFrom(null)); // NullPointerException + } + + @Test + public void whenUsingPrimitiveType_thenDesiredResultsHappen() { + //assertFalse(10 instanceof int); // illegal + assertFalse(int.class.isInstance(10)); + assertTrue(Integer.class.isInstance(10)); + assertTrue(int.class.isAssignableFrom(int.class)); + assertFalse(float.class.isAssignableFrom(int.class)); + } + + @Test + public void whenUsingClassInstanceVariable_thenDesiredResultsHappen() { + Shape shape = new Triangle(); + Triangle triangle = new Triangle(); + Class clazz = shape.getClass(); + + //assertFalse(triangle instanceof clazz); // illegal + assertTrue(clazz.isInstance(triangle)); + assertTrue(clazz.isAssignableFrom(triangle.getClass())); + } + } From 852268910c420a8e4d95ba49ce0997a7a65139ab Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Wed, 15 Sep 2021 11:28:50 +0200 Subject: [PATCH 045/118] JAVA-7055: Rollback to Jackson 2.11.1 --- spring-jersey/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-jersey/pom.xml b/spring-jersey/pom.xml index 08c19f1d3b..64a34074bb 100644 --- a/spring-jersey/pom.xml +++ b/spring-jersey/pom.xml @@ -212,6 +212,7 @@ 2.29.1 + 2.11.1 1.6.1 4.4.9 4.5.5 From b87c7b3d4ca514706c3d51ba1c32c2ed4508bfef Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 15 Sep 2021 21:58:26 +0800 Subject: [PATCH 046/118] Update README.md --- maven-modules/maven-parent-pom-resolution/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maven-modules/maven-parent-pom-resolution/README.md b/maven-modules/maven-parent-pom-resolution/README.md index 6f72b5e70b..709a5f12ed 100644 --- a/maven-modules/maven-parent-pom-resolution/README.md +++ b/maven-modules/maven-parent-pom-resolution/README.md @@ -1,3 +1,3 @@ ### Relevant Articles: -- [Understanding the "relativePath" Tag - Maven Parent POM Resolution At A Glance](https://www.baeldung.com/maven-relativepath) +- [https://www.baeldung.com/maven-relativepath](https://www.baeldung.com/maven-relativepath) From 188b8fa368442a4389414b25a48a1b52521bc1e4 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 15 Sep 2021 21:59:56 +0800 Subject: [PATCH 047/118] Update README.md --- core-java-modules/core-java-security-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-security-3/README.md b/core-java-modules/core-java-security-3/README.md index 970faaac88..a37719964b 100644 --- a/core-java-modules/core-java-security-3/README.md +++ b/core-java-modules/core-java-security-3/README.md @@ -5,4 +5,5 @@ This module contains articles about core Java Security ### Relevant Articles: - [Secret Key and String Conversion in Java](https://www.baeldung.com/java-secret-key-to-string) +- [Enabling Unlimited Strength Cryptography in Java](https://www.baeldung.com/jce-enable-unlimited-strength) - More articles: [[<-- prev]](/core-java-modules/core-java-security-2) From 3647443986916555f0fc1d755d4cd56c00266e35 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 15 Sep 2021 22:01:49 +0800 Subject: [PATCH 048/118] Update README.md --- json-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/json-2/README.md b/json-2/README.md index 64ca7e6449..aebc42c472 100644 --- a/json-2/README.md +++ b/json-2/README.md @@ -7,3 +7,4 @@ This module contains articles about JSON. - [Introduction to Jsoniter](https://www.baeldung.com/java-jsoniter) - [Introduction to Moshi Json](https://www.baeldung.com/java-json-moshi) - [Hypermedia Serialization With JSON-LD](https://www.baeldung.com/json-linked-data) +- [Generate a Java Class From JSON](https://www.baeldung.com/java-generate-class-from-json) From d1f81f20da81af940bb7b91cf0d972a8cdc2ab1f Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 15 Sep 2021 22:03:24 +0800 Subject: [PATCH 049/118] Update README.md --- core-java-modules/core-java-16/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-16/README.md b/core-java-modules/core-java-16/README.md index 760513189f..68215b3964 100644 --- a/core-java-modules/core-java-16/README.md +++ b/core-java-modules/core-java-16/README.md @@ -1,3 +1,4 @@ ### Relevant articles: - [Collect a Java Stream to an Immutable Collection](https://www.baeldung.com/java-stream-immutable-collection) +- [Guide to mapMulti in Stream API](https://www.baeldung.com/java-mapmulti) From 8e92dc0c5ca7b12bb84c2dd3de767152390a7621 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 15 Sep 2021 22:04:47 +0800 Subject: [PATCH 050/118] Update README.md --- core-java-modules/core-java-arrays-operations-advanced/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-arrays-operations-advanced/README.md b/core-java-modules/core-java-arrays-operations-advanced/README.md index 0e003b5cd9..f0d3d1fd93 100644 --- a/core-java-modules/core-java-arrays-operations-advanced/README.md +++ b/core-java-modules/core-java-arrays-operations-advanced/README.md @@ -10,3 +10,4 @@ This module contains articles about advanced operations on arrays in Java. They - [Intersection Between two Integer Arrays](https://www.baeldung.com/java-array-intersection) - [Comparing Arrays in Java](https://www.baeldung.com/java-comparing-arrays) - [Concatenate Two Arrays in Java](https://www.baeldung.com/java-concatenate-arrays) +- [Performance of System.arraycopy() vs. Arrays.copyOf()](https://www.baeldung.com/java-system-arraycopy-arrays-copyof-performance) From 0a2e726f689ff1f68cc899cbec5bad1b65f9de33 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 16 Sep 2021 20:04:12 +0800 Subject: [PATCH 051/118] Update README.md --- maven-modules/maven-parent-pom-resolution/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maven-modules/maven-parent-pom-resolution/README.md b/maven-modules/maven-parent-pom-resolution/README.md index 709a5f12ed..b315b2f626 100644 --- a/maven-modules/maven-parent-pom-resolution/README.md +++ b/maven-modules/maven-parent-pom-resolution/README.md @@ -1,3 +1,3 @@ ### Relevant Articles: -- [https://www.baeldung.com/maven-relativepath](https://www.baeldung.com/maven-relativepath) +- [Understanding Maven’s “relativePath” Tag for a Parent POM](https://www.baeldung.com/maven-relativepath) From 8c7827bebd4d977f2fd6f5cae3bf76513058f432 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Thu, 16 Sep 2021 15:41:15 +0300 Subject: [PATCH 052/118] BAEL-5033 close resources, create file --- .../java/io/zip4j/CreateSplitZipFile.java | 7 ++++--- .../baeldung/java/io/zip4j/ExtractAllFile.java | 6 ++++-- .../java/io/zip4j/ExtractSingleFile.java | 6 ++++-- .../com/baeldung/java/io/zip4j/ZipFolder.java | 5 +++-- .../com/baeldung/java/io/zip4j/ZipMultiFile.java | 16 ++++++++++------ .../baeldung/java/io/zip4j/ZipSingleFile.java | 5 +++-- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/CreateSplitZipFile.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/CreateSplitZipFile.java index cc39bc9dd2..a7ecd7946e 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/CreateSplitZipFile.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/CreateSplitZipFile.java @@ -1,22 +1,23 @@ package com.baeldung.java.io.zip4j; import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.enums.EncryptionMethod; import java.io.File; +import java.io.IOException; import java.util.Arrays; public class CreateSplitZipFile { - public static void main(String[] args) throws ZipException { + public static void main(String[] args) throws IOException { ZipParameters zipParameters = new ZipParameters(); zipParameters.setEncryptFiles(true); zipParameters.setEncryptionMethod(EncryptionMethod.AES); ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); - int splitLength = 1024 * 1024 * 10; //10MB + int splitLength = 1024 * 1024 * 10; // 10MB zipFile.createSplitZipFile(Arrays.asList(new File("aFile.txt")), zipParameters, true, splitLength); zipFile.createSplitZipFileFromFolder(new File("/users/folder_to_add"), zipParameters, true, splitLength); + zipFile.close(); } } diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractAllFile.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractAllFile.java index 10e7ddd339..924d80a921 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractAllFile.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractAllFile.java @@ -1,12 +1,14 @@ package com.baeldung.java.io.zip4j; +import java.io.IOException; + import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.exception.ZipException; public class ExtractAllFile { - public static void main(String[] args) throws ZipException { + public static void main(String[] args) throws IOException { ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); zipFile.extractAll("/destination_directory"); + zipFile.close(); } } diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractSingleFile.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractSingleFile.java index 4cf466d02b..ad79557a37 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractSingleFile.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractSingleFile.java @@ -1,12 +1,14 @@ package com.baeldung.java.io.zip4j; +import java.io.IOException; + import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.exception.ZipException; public class ExtractSingleFile { - public static void main(String[] args) throws ZipException { + public static void main(String[] args) throws IOException { ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); zipFile.extractFile("aFile.txt", "/destination_directory"); + zipFile.close(); } } diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipFolder.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipFolder.java index 4d89e8665f..ed25494083 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipFolder.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipFolder.java @@ -1,19 +1,20 @@ package com.baeldung.java.io.zip4j; import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.enums.EncryptionMethod; import java.io.File; +import java.io.IOException; public class ZipFolder { - public static void main(String[] args) throws ZipException { + public static void main(String[] args) throws IOException { ZipParameters zipParameters = new ZipParameters(); zipParameters.setEncryptFiles(true); zipParameters.setEncryptionMethod(EncryptionMethod.AES); ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); zipFile.addFolder(new File("/users/folder_to_add"), zipParameters); + zipFile.close(); } } diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipMultiFile.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipMultiFile.java index dcb860ef92..b34eccd7db 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipMultiFile.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipMultiFile.java @@ -1,27 +1,31 @@ package com.baeldung.java.io.zip4j; import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.enums.EncryptionMethod; import java.io.File; +import java.io.IOException; import java.util.Arrays; import java.util.List; public class ZipMultiFile { - public static void main(String[] args) throws ZipException { + public static void main(String[] args) throws IOException { ZipParameters zipParameters = new ZipParameters(); zipParameters.setEncryptFiles(true); zipParameters.setEncryptionMethod(EncryptionMethod.AES); - List filesToAdd = Arrays.asList( - new File("aFile.txt"), - new File("bFile.txt") - ); + File firstFile = new File("aFile.txt"); + File secondFile = new File("bFile.txt"); + + firstFile.createNewFile(); + secondFile.createNewFile(); + + List filesToAdd = Arrays.asList(firstFile, secondFile); ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); zipFile.addFiles(filesToAdd, zipParameters); + zipFile.close(); } } diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipSingleFile.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipSingleFile.java index a5f600df47..4d72c71c8d 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipSingleFile.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipSingleFile.java @@ -1,21 +1,22 @@ package com.baeldung.java.io.zip4j; import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.enums.CompressionLevel; import net.lingala.zip4j.model.enums.EncryptionMethod; import java.io.File; +import java.io.IOException; public class ZipSingleFile { - public static void main(String[] args) throws ZipException { + public static void main(String[] args) throws IOException { ZipParameters zipParameters = new ZipParameters(); zipParameters.setEncryptFiles(true); zipParameters.setCompressionLevel(CompressionLevel.HIGHER); zipParameters.setEncryptionMethod(EncryptionMethod.AES); ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); zipFile.addFile(new File("aFile.txt")); + zipFile.close(); } } From 656c697f4c766a11200b25e95776c1768e9ce95a Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Thu, 16 Sep 2021 15:50:41 +0300 Subject: [PATCH 053/118] BAEL-5033 create file if it doesn't exist --- .../java/com/baeldung/java/io/zip4j/ZipMultiFile.java | 9 ++++++--- .../java/com/baeldung/java/io/zip4j/ZipSingleFile.java | 7 ++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipMultiFile.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipMultiFile.java index b34eccd7db..081d207294 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipMultiFile.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipMultiFile.java @@ -18,9 +18,12 @@ public class ZipMultiFile { File firstFile = new File("aFile.txt"); File secondFile = new File("bFile.txt"); - - firstFile.createNewFile(); - secondFile.createNewFile(); + if (!firstFile.exists()) { + firstFile.createNewFile(); + } + if (!secondFile.exists()) { + secondFile.createNewFile(); + } List filesToAdd = Arrays.asList(firstFile, secondFile); diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipSingleFile.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipSingleFile.java index 4d72c71c8d..d0947afa2e 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipSingleFile.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipSingleFile.java @@ -16,7 +16,12 @@ public class ZipSingleFile { zipParameters.setCompressionLevel(CompressionLevel.HIGHER); zipParameters.setEncryptionMethod(EncryptionMethod.AES); ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); - zipFile.addFile(new File("aFile.txt")); + + File fileToAdd = new File("aFile.txt"); + if (!fileToAdd.exists()) { + fileToAdd.createNewFile(); + } + zipFile.addFile(fileToAdd); zipFile.close(); } } From 6ed0aee6a087394a2676695278a37242d526305c Mon Sep 17 00:00:00 2001 From: kwoyke Date: Thu, 16 Sep 2021 19:04:26 +0200 Subject: [PATCH 054/118] JAVA-7133: POM Properties Cleanup (#11229) * JAVA-7133: Use common org.slf4j.version property * JAVA-7133: Use common logback.version property --- apache-libraries/pom.xml | 1 - apache-thrift/pom.xml | 3 +-- core-groovy-2/pom.xml | 1 - deeplearning4j/pom.xml | 5 ++--- ethereum/pom.xml | 4 +--- libraries-data-2/pom.xml | 5 ++--- libraries-data/pom.xml | 3 +-- logging-modules/logback/pom.xml | 1 - micronaut/pom.xml | 3 +-- persistence-modules/hibernate-libraries/pom.xml | 4 +--- restx/pom.xml | 3 +-- spf4j/spf4j-aspects-app/pom.xml | 1 - spf4j/spf4j-core-app/pom.xml | 1 - .../kubernetes-guide/travel-agency-service/pom.xml | 4 ---- spring-security-modules/spring-security-web-sockets/pom.xml | 3 +-- vertx-and-rxjava/pom.xml | 1 - vraptor/pom.xml | 3 +-- 17 files changed, 12 insertions(+), 34 deletions(-) diff --git a/apache-libraries/pom.xml b/apache-libraries/pom.xml index bca80d9acd..b4cf11b07d 100644 --- a/apache-libraries/pom.xml +++ b/apache-libraries/pom.xml @@ -201,7 +201,6 @@ 1.8 1.8 1.8.2 - 1.7.25 2.19.0 3.9.0 1.1.2 diff --git a/apache-thrift/pom.xml b/apache-thrift/pom.xml index 9562ae7dfe..d2623f92e7 100644 --- a/apache-thrift/pom.xml +++ b/apache-thrift/pom.xml @@ -29,7 +29,7 @@ org.slf4j slf4j-simple - ${org.slf4j.slf4j-simple.version} + ${org.slf4j.version} test @@ -61,7 +61,6 @@ 0.10.0 0.1.11 - 1.7.12 3.0.0 diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml index f8ef654293..89df666333 100644 --- a/core-groovy-2/pom.xml +++ b/core-groovy-2/pom.xml @@ -166,7 +166,6 @@ 2.4.0 1.1-groovy-2.4 1.1.3 - 1.2.3 2.5.7 3.1.0 3.8.0 diff --git a/deeplearning4j/pom.xml b/deeplearning4j/pom.xml index f1f9b9fa7b..c63e67d573 100644 --- a/deeplearning4j/pom.xml +++ b/deeplearning4j/pom.xml @@ -39,12 +39,12 @@ org.slf4j slf4j-api - ${slf4j.version} + ${org.slf4j.version} org.slf4j slf4j-log4j12 - ${slf4j.version} + ${org.slf4j.version} @@ -62,7 +62,6 @@ 0.9.1 4.3.5 - 1.7.5 \ No newline at end of file diff --git a/ethereum/pom.xml b/ethereum/pom.xml index 4283714b98..b9a3870702 100644 --- a/ethereum/pom.xml +++ b/ethereum/pom.xml @@ -99,7 +99,7 @@ org.slf4j jcl-over-slf4j - ${slf4j.version} + ${org.slf4j.version} ch.qos.logback @@ -204,8 +204,6 @@ 1.5.6.RELEASE 2.21.0 2.4.0 - 1.2.3 - 1.7.25 2.0.4.RELEASE 3.1 diff --git a/libraries-data-2/pom.xml b/libraries-data-2/pom.xml index cce2e57d22..75b2cc962d 100644 --- a/libraries-data-2/pom.xml +++ b/libraries-data-2/pom.xml @@ -104,12 +104,12 @@ org.slf4j slf4j-api - ${slf4j.version} + ${org.slf4j.version} org.slf4j slf4j-log4j12 - ${slf4j.version} + ${org.slf4j.version} com.univocity @@ -161,7 +161,6 @@ 4.0.0 1.1.0 3.6.2 - 1.7.25 3.0.0 2.8.4 29.0-jre diff --git a/libraries-data/pom.xml b/libraries-data/pom.xml index 717ee802db..c5ad08448f 100644 --- a/libraries-data/pom.xml +++ b/libraries-data/pom.xml @@ -108,7 +108,7 @@ org.slf4j slf4j-api - ${slf4j.version} + ${org.slf4j.version} org.apache.storm @@ -173,7 +173,6 @@ 3.8.4 0.15.0 2.2.0 - 1.7.25 1.6.0.1 diff --git a/logging-modules/logback/pom.xml b/logging-modules/logback/pom.xml index 6d32025d94..512dc9e5a3 100644 --- a/logging-modules/logback/pom.xml +++ b/logging-modules/logback/pom.xml @@ -69,7 +69,6 @@ - 1.2.3 0.1.5 3.3.5 1.4.7 diff --git a/micronaut/pom.xml b/micronaut/pom.xml index 196218d856..e9b5a0409f 100644 --- a/micronaut/pom.xml +++ b/micronaut/pom.xml @@ -61,7 +61,7 @@ ch.qos.logback logback-classic - ${lombok.version} + ${logback.version} runtime @@ -145,7 +145,6 @@ 1.0.0.RC2 1.8 1.3.2 - 1.2.3 3.1.6.RELEASE 3.7.0 3.1.0 diff --git a/persistence-modules/hibernate-libraries/pom.xml b/persistence-modules/hibernate-libraries/pom.xml index 19537156aa..7d552b262d 100644 --- a/persistence-modules/hibernate-libraries/pom.xml +++ b/persistence-modules/hibernate-libraries/pom.xml @@ -77,7 +77,7 @@ org.slf4j slf4j-api - ${slf4j.version} + ${org.slf4j.version} provided true @@ -174,12 +174,10 @@ 3.27.0-GA 2.3.1 2.0.0 - 1.2.3 3.0.2 3.8.1 3.8.1 8.0.19 - 1.7.30 2.1.3.RELEASE diff --git a/restx/pom.xml b/restx/pom.xml index ee25c88047..ac0ff36376 100644 --- a/restx/pom.xml +++ b/restx/pom.xml @@ -99,7 +99,7 @@ ch.qos.logback logback-classic - ${logback-classic.version} + ${logback.version} io.restx @@ -149,7 +149,6 @@ 0.35-rc4 - 1.2.3 diff --git a/spf4j/spf4j-aspects-app/pom.xml b/spf4j/spf4j-aspects-app/pom.xml index a44ee805fb..c4940b9c97 100644 --- a/spf4j/spf4j-aspects-app/pom.xml +++ b/spf4j/spf4j-aspects-app/pom.xml @@ -99,7 +99,6 @@ 8.9.0 - 1.7.21 3.8.0 3.1.1 diff --git a/spf4j/spf4j-core-app/pom.xml b/spf4j/spf4j-core-app/pom.xml index 1f9be97854..28c104afe1 100644 --- a/spf4j/spf4j-core-app/pom.xml +++ b/spf4j/spf4j-core-app/pom.xml @@ -105,7 +105,6 @@ 8.9.0 - 1.7.21 3.8.0 3.1.1 diff --git a/spring-cloud/spring-cloud-kubernetes/kubernetes-guide/travel-agency-service/pom.xml b/spring-cloud/spring-cloud-kubernetes/kubernetes-guide/travel-agency-service/pom.xml index c6c6de94c3..ba5ba93682 100644 --- a/spring-cloud/spring-cloud-kubernetes/kubernetes-guide/travel-agency-service/pom.xml +++ b/spring-cloud/spring-cloud-kubernetes/kubernetes-guide/travel-agency-service/pom.xml @@ -62,8 +62,4 @@ - - 1.2.3 - - \ No newline at end of file diff --git a/spring-security-modules/spring-security-web-sockets/pom.xml b/spring-security-modules/spring-security-web-sockets/pom.xml index 7b7a5e4659..802c894612 100644 --- a/spring-security-modules/spring-security-web-sockets/pom.xml +++ b/spring-security-modules/spring-security-web-sockets/pom.xml @@ -103,7 +103,7 @@ ch.qos.logback logback-classic - ${logback-classic.version} + ${logback.version} @@ -180,7 +180,6 @@ 5.2.10.Final 1.11.3.RELEASE - 1.2.3 1.5.10.RELEASE 1.7.6 diff --git a/vertx-and-rxjava/pom.xml b/vertx-and-rxjava/pom.xml index fb04ba784c..1793cff1e7 100644 --- a/vertx-and-rxjava/pom.xml +++ b/vertx-and-rxjava/pom.xml @@ -47,7 +47,6 @@ 3.5.0.Beta1 - 1.2.3 \ No newline at end of file diff --git a/vraptor/pom.xml b/vraptor/pom.xml index ab78c0d97a..fad17e7aae 100644 --- a/vraptor/pom.xml +++ b/vraptor/pom.xml @@ -68,7 +68,7 @@ org.slf4j slf4j-log4j12 - ${slf4j-log4j12.version} + ${org.slf4j.version} br.com.caelum.vraptor @@ -117,7 +117,6 @@ 2.1.2.Final 2.2 5.1.1.Final - 1.7.5 4.1.0-RC3 4.0.4 8.0.8-dmr From b441c62f0e7715dc9bc5fc01d63421cd43150d83 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Thu, 16 Sep 2021 19:07:12 +0200 Subject: [PATCH 055/118] JAVA-7177: Upgrade to Spring Boot 2.5.4 (#11218) * JAVA-7177: Upgrade to Spring Boot 2.5.4 * JAVA-7177: Remove overriden Spring Boot version * JAVA-7177: Leave spring-5-security-oauth on Spring Boot 2.5.2 --- parent-boot-2/pom.xml | 2 +- spring-boot-modules/spring-boot-keycloak/pom.xml | 2 -- spring-security-modules/spring-5-security-oauth/pom.xml | 2 ++ 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/parent-boot-2/pom.xml b/parent-boot-2/pom.xml index accbc2df96..2e520640ec 100644 --- a/parent-boot-2/pom.xml +++ b/parent-boot-2/pom.xml @@ -81,7 +81,7 @@ 3.3.0 1.0.22.RELEASE - 2.5.1 + 2.5.4 1.9.1 3.4.0 diff --git a/spring-boot-modules/spring-boot-keycloak/pom.xml b/spring-boot-modules/spring-boot-keycloak/pom.xml index 505d486509..b80dbfa191 100644 --- a/spring-boot-modules/spring-boot-keycloak/pom.xml +++ b/spring-boot-modules/spring-boot-keycloak/pom.xml @@ -77,8 +77,6 @@ 13.0.1 - - 2.5.3 \ No newline at end of file diff --git a/spring-security-modules/spring-5-security-oauth/pom.xml b/spring-security-modules/spring-5-security-oauth/pom.xml index 03e1880431..194ace35b0 100644 --- a/spring-security-modules/spring-5-security-oauth/pom.xml +++ b/spring-security-modules/spring-5-security-oauth/pom.xml @@ -68,6 +68,8 @@ + + 2.5.2 com.baeldung.oauth2.SpringOAuthApplication From 2a68c0e2bb6dabae2155e96dc325e30908d08aea Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Fri, 17 Sep 2021 07:31:41 +0530 Subject: [PATCH 056/118] Html2pdf using open pdf (#11178) * Added inital changes html to pdf using openPdf * Added changes to pdf for external style * Added chnages via flying saucer * Added changes ti fix the issue * Added inital changes * Simplyfied the core logic * Created a sperate package * Added changes for making it more modular * Added Image attribute update * Added chnages to add images * Added changes * Style validation * Added changes to fix the styles * Review comments updates * Corrected the versions * Fixed the review commit * Added changes to fix the indendation issue Co-authored-by: Amitabh.Tiwari --- pdf/pom.xml | 24 ++++++++ .../pdf/openpdf/CustomElementFactoryImpl.java | 56 ++++++++++++++++++ .../openpdf/Html2PdfUsingFlyingSaucer.java | 53 +++++++++++++++++ .../pdf/openpdf/Html2PdfUsingOpenHtml.java | 55 +++++++++++++++++ pdf/src/main/resources/html2pdf.pdf | Bin 0 -> 15570 bytes pdf/src/main/resources/htmlforopenpdf.html | 26 ++++++++ pdf/src/main/resources/style.css | 6 ++ 7 files changed, 220 insertions(+) create mode 100644 pdf/src/main/java/com/baeldung/pdf/openpdf/CustomElementFactoryImpl.java create mode 100644 pdf/src/main/java/com/baeldung/pdf/openpdf/Html2PdfUsingFlyingSaucer.java create mode 100644 pdf/src/main/java/com/baeldung/pdf/openpdf/Html2PdfUsingOpenHtml.java create mode 100644 pdf/src/main/resources/html2pdf.pdf create mode 100644 pdf/src/main/resources/htmlforopenpdf.html create mode 100644 pdf/src/main/resources/style.css diff --git a/pdf/pom.xml b/pdf/pom.xml index fb9508156e..6bd1d97402 100644 --- a/pdf/pom.xml +++ b/pdf/pom.xml @@ -71,6 +71,26 @@ flying-saucer-pdf ${flying-saucer-pdf.version} + + org.xhtmlrenderer + flying-saucer-pdf-openpdf + ${flying-saucer-pdf-openpdf.version} + + + org.jsoup + jsoup + ${jsoup.version} + + + com.openhtmltopdf + openhtmltopdf-core + ${open-html-pdf-core.version} + + + com.openhtmltopdf + openhtmltopdf-pdfbox + ${open-html-pdfbox.version} + @@ -93,6 +113,10 @@ 3.15 3.0.11.RELEASE 9.1.20 + 1.0.6 + 1.0.6 + 9.1.22 + 1.14.2 \ No newline at end of file diff --git a/pdf/src/main/java/com/baeldung/pdf/openpdf/CustomElementFactoryImpl.java b/pdf/src/main/java/com/baeldung/pdf/openpdf/CustomElementFactoryImpl.java new file mode 100644 index 0000000000..d8256a68f7 --- /dev/null +++ b/pdf/src/main/java/com/baeldung/pdf/openpdf/CustomElementFactoryImpl.java @@ -0,0 +1,56 @@ +package com.baeldung.pdf.openpdf; + +import java.io.FileInputStream; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; +import org.w3c.dom.Element; +import org.xhtmlrenderer.extend.FSImage; +import org.xhtmlrenderer.extend.ReplacedElement; +import org.xhtmlrenderer.extend.ReplacedElementFactory; +import org.xhtmlrenderer.extend.UserAgentCallback; +import org.xhtmlrenderer.layout.LayoutContext; +import org.xhtmlrenderer.pdf.ITextFSImage; +import org.xhtmlrenderer.pdf.ITextImageElement; +import org.xhtmlrenderer.render.BlockBox; +import org.xhtmlrenderer.simple.extend.FormSubmissionListener; + +import com.lowagie.text.Image; + +public class CustomElementFactoryImpl implements ReplacedElementFactory { + @Override + public ReplacedElement createReplacedElement(LayoutContext lc, BlockBox box, UserAgentCallback uac, int cssWidth, int cssHeight) { + Element e = box.getElement(); + String nodeName = e.getNodeName(); + if (nodeName.equals("img")) { + String imagePath = e.getAttribute("src"); + try { + InputStream input = new FileInputStream("src/main/resources/" + imagePath); + byte[] bytes = IOUtils.toByteArray(input); + Image image = Image.getInstance(bytes); + FSImage fsImage = new ITextFSImage(image); + if (cssWidth != -1 || cssHeight != -1) { + fsImage.scale(cssWidth, cssHeight); + } else { + fsImage.scale(2000, 1000); + } + return new ITextImageElement(fsImage); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + return null; + } + + @Override + public void reset() { + } + + @Override + public void remove(Element e) { + } + + @Override + public void setFormSubmissionListener(FormSubmissionListener listener) { + } +} \ No newline at end of file diff --git a/pdf/src/main/java/com/baeldung/pdf/openpdf/Html2PdfUsingFlyingSaucer.java b/pdf/src/main/java/com/baeldung/pdf/openpdf/Html2PdfUsingFlyingSaucer.java new file mode 100644 index 0000000000..927d85a4cb --- /dev/null +++ b/pdf/src/main/java/com/baeldung/pdf/openpdf/Html2PdfUsingFlyingSaucer.java @@ -0,0 +1,53 @@ +package com.baeldung.pdf.openpdf; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.xhtmlrenderer.layout.SharedContext; +import org.xhtmlrenderer.pdf.ITextRenderer; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class Html2PdfUsingFlyingSaucer { + + private static final String HTML_INPUT = "src/main/resources/htmlforopenpdf.html"; + private static final String PDF_OUTPUT = "src/main/resources/html2pdf.pdf"; + + public static void main(String[] args) { + try { + Html2PdfUsingFlyingSaucer htmlToPdf = new Html2PdfUsingFlyingSaucer(); + htmlToPdf.generateHtmlToPdf(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void generateHtmlToPdf() throws Exception { + File inputHTML = new File(HTML_INPUT); + Document inputHtml = createWellFormedHtml(inputHTML); + File outputPdf = new File(PDF_OUTPUT); + xhtmlToPdf(inputHtml, outputPdf); + } + + private Document createWellFormedHtml(File inputHTML) throws IOException { + Document document = Jsoup.parse(inputHTML, "UTF-8"); + document.outputSettings() + .syntax(Document.OutputSettings.Syntax.xml); + return document; + } + + private void xhtmlToPdf(Document xhtml, File outputPdf) throws Exception { + try (OutputStream outputStream = new FileOutputStream(outputPdf)) { + ITextRenderer renderer = new ITextRenderer(); + SharedContext sharedContext = renderer.getSharedContext(); + sharedContext.setPrint(true); + sharedContext.setInteractive(false); + sharedContext.setReplacedElementFactory(new CustomElementFactoryImpl()); + renderer.setDocumentFromString(xhtml.html()); + renderer.layout(); + renderer.createPDF(outputStream); + } + } +} diff --git a/pdf/src/main/java/com/baeldung/pdf/openpdf/Html2PdfUsingOpenHtml.java b/pdf/src/main/java/com/baeldung/pdf/openpdf/Html2PdfUsingOpenHtml.java new file mode 100644 index 0000000000..bc83da6102 --- /dev/null +++ b/pdf/src/main/java/com/baeldung/pdf/openpdf/Html2PdfUsingOpenHtml.java @@ -0,0 +1,55 @@ +package com.baeldung.pdf.openpdf; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.FileSystems; + +import org.jsoup.Jsoup; +import org.jsoup.helper.W3CDom; +import org.jsoup.nodes.Document; + +import com.openhtmltopdf.pdfboxout.PdfRendererBuilder; + +public class Html2PdfUsingOpenHtml { + + private static final String HTML_INPUT = "src/main/resources/htmlforopenpdf.html"; + private static final String PDF_OUTPUT = "src/main/resources/html2pdf.pdf"; + + public static void main(String[] args) { + try { + Html2PdfUsingOpenHtml htmlToPdf = new Html2PdfUsingOpenHtml(); + htmlToPdf.generateHtmlToPdf(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void generateHtmlToPdf() throws IOException { + File inputHTML = new File(HTML_INPUT); + Document doc = createWellFormedHtml(inputHTML); + xhtmlToPdf(doc, PDF_OUTPUT); + } + + private Document createWellFormedHtml(File inputHTML) throws IOException { + Document document = Jsoup.parse(inputHTML, "UTF-8"); + document.outputSettings() + .syntax(Document.OutputSettings.Syntax.xml); + return document; + } + + private void xhtmlToPdf(Document doc, String outputPdf) throws IOException { + try (OutputStream os = new FileOutputStream(outputPdf)) { + String baseUri = FileSystems.getDefault() + .getPath("src/main/resources/") + .toUri() + .toString(); + PdfRendererBuilder builder = new PdfRendererBuilder(); + builder.withUri(outputPdf); + builder.toStream(os); + builder.withW3cDocument(new W3CDom().fromJsoup(doc), baseUri); + builder.run(); + } + } +} diff --git a/pdf/src/main/resources/html2pdf.pdf b/pdf/src/main/resources/html2pdf.pdf new file mode 100644 index 0000000000000000000000000000000000000000..877ff4cacd94862e6b9bf5132e3bc548e896435e GIT binary patch literal 15570 zcmcJ01z6kJ(sybUD^{e?;;zAh%i<2jix&wHf(C+?(xnua;toZE7ATP5(qg4(aV-u7 zin|o}Lc4oy@7?#k_r2fAlRTM|GjnXtf96c)$M{%Qo|A`*ADi)L>H91;4-GeslZ73& zm>4!t$IBT^1C%j$Gq-oL!3OGqVXhD-M;agxmmoIqvAGS{m4+AHTnk%50vqgTbFUHlQY=S z*3H4*&B@uyn#M`rQze_!v@Mj?A_2a10|}I z9;vGvdaWGpVSA><$8@W=audiM?~}CR32cR%l~WWN`%vvZ8wysJNS#z{BimI zDj!GDlZt1{wO{+%7!(;X{G=9e2ugp&GMm4+h4ppkPFFTPy>X3}KaXi=+v{m)dvu;X za*!)HYyH5jCbRXS{fhugOYxYUl!^8)>j5tHx4T=~WJ8Yq!EHyO?&?Bg-V_K~Y1!{l zlllsCbaS}n)t*fXk3!nuHKi(uR|1VnW6Ljgy*`t|3nD+%kW1C(KSC~qR%LG^WW1t% zS%wIf`M*B?`cb&-5vT)*>z>xK8m(!kO7=dY%QluPbD*VSm}-c~F|#7Oz*(#}^whDk zp|;iS9zMj=cSFtS!BNvN3mH`!o}ZIm%WADvL)p`tC4T+!ov>hN(w#e{&>Be7S;xC| zu~u(={izY;`??kb)aj=x&K4~@|8+xgcWy>9szRvsGRpN#UnG(2eC`MWc4&HQ`I-}at| zPgsEa_XG7?XV^e(xP{w~Bc|klK1_dA4p|wU-&6xA4RLdQ42H=#IXF8xUTdNdHc%g8 zg;potYk`Vjh>a~;J+HsZIN3YFw4Kc@(Pvf`><+O6Ybi*hr-N2mn6uNhzR|eB;D4uX zSL0W600L!sC3(P&8#e%o=ojE>3Lpcxb@RGhf8Iiu+Zea63nm7}9Skf?Y-}t{EG%qX zd|YfCJRB^ndxZD!@CgVA39xaA?h_H*M}H@{7INd7^VV%lG$R2H77n`h-z-<}0fd-0 zZr`|l>jojn8TyJGbv( z-p0RfapUH#+jj`jZHP!{Wv%?PKg{3fk{)3TBPV6VKMVNAOUR)*9tQKUD>Ni04|#G zCgCkYfF$5h6#!7ov^_Tl5CH%fHU9^~3-Y_h(<=b*riS0^8;L0K%U|wxTx!UmBFxn1Zqa;8O?!{~ zveeNPzp=VM33e77o2&|bwP=Ja}>#43(>A zawwV@af2!-aju>P(Z3l30I=?FQbS+d`~?8`M*@H@3>rw^QUDeJaLf3ACh$;mWp-)F z@}?y5YigYk@!Syi`{FJ}a44UWrzJywkMulA?N*7zE9QJJq?b~wZljpQu^OXRxK6!A zLez)TM`uhC#(z3pzzzDp*-#0=J#Wglc4r=dpDOuNF@C5d#t+4yet0U5Hwy9|xv2L~HyBFXe2`m6FhPL7rkX5&YC2#TySj$y&eBp?rLYR-M8;5yn8(OiG|$* z>Tl}|D!gnPa<@0%otf1I&XjlnZ@ZUWpQQHS!Pfuoe1Dz<^mBq_NkqP%;EprkM)aQ$ z8kv(=poG|Vjgs5#Ne?jjA4W;67sN$Bx0w^tDaPh@O&y?T>*`n}s);Ch`zW6|TX9ux zbp}+@(saOBh1V`479uR0>WoWe!F%DE=u?oA`K$Fe9_5C*!f<2x2%tyKdcPJptr`5O z>Yg@X8wK2ORAV0V_@r{+VePxY)T(mcu=|Ws#Qdd{oR(UF3umL1yISG>J;n|N@{@58 z1(;ZG#KK5^(|)o8X)1hwDEQ*DB54=@yYegFOeh zMfWd>;BBsh4c}o!G6_^ALq#k-i>I57H?7|*tj)7SGTZk0280csul|M7Msvb0Zm(O3 zy?Ghg07-l0o9bkrvG{-5-c{wd~8`dv)N)#ZLnXggio>um>Z>xH@&nRw5d`+3C`Zh2)DzNjs$MXtMd$?nAmu5!o#3W*%iVs8{#;}M+=@q6cIRq5T zHMVpg{xuu1F&Nd1Ug(8cX%V_skLp+O)%#GdiMT-?T@YJg;EN#oZ~0mTrg^?A772|u zq%}i6&#|c9q<-)cYXK=qr}?G8POF#4y~$t?B*(?5H`ZqZ1uU3~Y^cD(!F0;u!A03> ze#Q2cD~DvFk0c_g@9AA8ctwG}a;ONKuypHcm|3LkjfH1k0r2Y0X2|)94aDWXOp9{W zEk>qs={PiaSm;^HDY_kzIyHHnE2mpfoKvJ5c;iU)J`e2adX_LbK6O!?oT9|#!P7{r zPu?7zS2ImavxJXw+y)P28hZ3(GSQJbqOdFbb;-wa%|ceV`gW6p=GIdW#w3O>skLg; zyi^N)$O50L3>cN9ltii&YrwslVk=mcwPKVNgM$lXO=P7#hdIBYQvS@3xIRVyqk$qH zBj{G~YR!Hes})rf{BQ_z#N%qm?ju&(Wb`4`G<~s`6W21#kc^ z&Bs#;ECqYHur_VAPI+nE%1YP?)r%XA;IumVRAS^evpDdbY)je1Ro@j0Z$kvnSHOW# zTQSsUd_|Wj_zHlHxu$-WR5$O=Q}HHoQ~YZp-;rH$@~)DSrs+0NU6j3R+!IV-{W6jPa;4Ov z7NEFvOq{MJ*deV8v4ml!P9$>&tS`RJLg5dgIwC@xf`Vb01@+~#2DcA3&0-;!0qtAdMtqdyXW&) zLn)aimntiRCsWG3Ya;dLqOX#Wp1(V6!s(C9K*~u~TnJWTaEVCvxn#pj}mU5%vCFGZg1Ck9VJow@_;zVTi zVU8%`P^9~3bZI=g2 z*l@V@ykO|o7w9%;crh#_fiaQCYoBc04$2}CQ9tIo_c`oUdrzCGZ=mdg9Ij)({gLQ~ zK+JKb)!Lwd$=9Ut29-9rr5@K-lmykm^V?C3rxoO`_uY?y${?iLrGcAdtk^~6(CJ7d zl0Yv|iFoNzmjOXyvWsxstK74wJ>IiWHIdjHkdVFYq1Ru^=>0srRfyx@Rxf9^!4UUg1Ai)ztE~(rtVF443O= z>cSB{m^ONLdU}Y&-T@h@I9XtGmpSpETjSTd-DGcuHZ|`PHK6rmT>cnye#}`c=>d=T z%t-H()*^N@!2@B6ni-YJe$_LzyyN{pAr=srC=-8k7pQ!spRr@dTc5U>uy{_kBBYReAvxn^-$2sP+i2^ zDo!)K_F^H9QmJqH-eFCtedefkQg$t2o@7n}4#8v6wL{h+KG5Sz!c2$T_8j7%k0r)V ztHz?<-MyoZ=X`e}%|6UTd)LJa3RgEO1b7P}VFmG^wLRHBMs5`q7gt4jJ(LB4ODrx0 zZ7=(UHXP0*BhSySC1vpYpeAG%fYBVHtR z6w%t+){W{G>BVf)vkf&EbBF8Wx7)Fce)DAJ3_;}0Myqykrzv2*Y z#OPr%ZYDR1VOPUz1Leih=ekXB=q*g}DY=DctS3`ha=l%$qO0UGJ*ToOAuADfS$&3O zUzT(^@e*MG(UO2h4X+I7a=!Va%|oB+e-#(GF{(=MEC@T@c7>f-X=RjbAJ45jS-F?9U=B(A}aRm`-q7NM==?4$2-%O4@cIj`ZW zE!VR_JYyC`@FYx!7^aw^tii*HM@V>h6h_iv^?vsWi%I)&$(Pkqqvf|gSOx0_pX^S= zFA7sSHUlYP0Woy;_PDKN)f*cH{oliAA8hiX%-nOLd``2~Os%^_8I@FOpCzAPdT@_g z9yl5w&#Y3D@pG(SY z=l7yKq}f~7{e-&j2I=ffc2`HU#AU&=_>sEey5|v@LAG<-747)2u*BohzIO8z==os| zHGYg_E)`C7p(w9zV+-E%GyH87U@-)kMG+@=9;CVtdPUu0&<=0JTLnLpZe9d3oFz1y zMR!%hT@tW2V~;|t9uASmRF;ByT{j7t4>VVld@%#?Xd}KXh%%j)e=i>o!=U`t(7YZ} z>ekF(Rhil*#SI3Uda+b`Kq2{jBX8bhExSbb#@iLqwCr4|y*_b2kj7_!jReWMj zl7!Oj+>hXPy3^U0cQcc4y7G7uTM0q09Kd&ParkUvvn`yjE73A98ehs-+jzeVd{o%& z6EjpZQcD{5HAEzOI{R>296%8_{r}&sdg@ehyi{#uo>}FZBsjB5sVc_LC)|6SXsiXI z#A4h^PCmg>rN=1@n=bZtQcpOcV}H;NnL8?7$sWt%;#jb!dY4o=_Ii4hYnKz5(XT2o zsP;iQEr$~ks*|A3aRpG!;o}uMEiedJ5l*lhccq7@^@%tn9G6*42jkx2*J>kP_R``` zsHd-mOdeNeW-(Psh%*#CbYKalUti6h(qAgqk4LnLuY#t#t$>_gw}!*E1e(5&>P7<>e^nB3gNxb$0{F5^#R(C7k<72%E<pck>btUk^4A7C1UU_G1ForavQE41hlgZs50dd^9NbqDh)aHDa-_i?_{|sL*sLw;?C3)_q%4G|bo=v&OSk!ZI`H=1AWcwkk4f-imHI)*3mSZoBnViwdoG9(70~?BuK4L&eEIVPj zvdUdnt{g!su)=bvUs7VAS9xNyy2^*|oscE=6UmX`uw65H@yl2GN8wi4Hmtlm$sEOX zqRp!B63>CB`on7%*zH#Ue!t`k{H*XhPNCdul(trCWR7Cm#ou2Gsff;hKBfiUITU^y z7NRkmrtfq15C^!ik)leBFvR&7XwH(yYfcka*>9Sondsr%Qj%$YUIc7R9`=$bxIC-a zGSg15udcjYUJ8>L8m}EbZ2QXJOrVOixftz?Iu3@WZ3d4TYH0>pjGV^mO+3WQsVa^; zHx29DV9cl_3ZtgA>o>3^otvk93<5R&uS#4okK!gN7|}#?FdKaAkp_pUK+s7h9Jd>1;9_@+E4!1k+7e?+(H~52Hsnk zU|;d>EN6$A*yIwfs;crzD%|I_EwJ`wjb1>QBsHd63#jA7mvFs1H0d_zwf7k|vmqIU zayWrz3>Ub*D5R?6+f6UkdrIWQGR%BdiH>q;$iHLD7|GJOR7%K&U_O+NZ1SfnRcd>B z5k&5>wkzBF=rFL?$4=8X@dsTSWXY&ZWzvT+4;duG6zFYFBlw9W(9d(kR^GyoNn%6Kcxu zDQPvH&6Y_VF_RGPy^T|H;Og#3L5HWUvaSIV?r(_BIr9LluMnZCO zaw-@vNz9qMUyj5G`%oBFG7H5!H>vL ziXzTTb%tJ|i7?2#Kh?vfFAAWw(|d%BHNZ_rBW&?g7M!If=tOb5L^rhXv= z;Q=I`cMpp>1tmxfzi1&{oAf)4Yd#uUQlDgRKgp-Q&_1o?@vIi~@F>JnGkPf2qq3^b zuIX?YNn5dFV3YsraL{w=a8q+9BgkrvlPQJ-{YwLjVwEJjldb2>07GhsK#6G%W$J+%;Ze0SJr66 zg!#G6q6{T)M^r8{NcD8`9wDoaz?4zPDFHz;SiPzvdOSxbnCqu|4ONeG6QNG4N>h7& zPL!zi>hYMfG?WFmYwbB8Z-Jpof@HO76%rb8`Ff4oi=vV&nxJH6<&pdqKvyqE#ecjZ z3>0bH>U_bdoZ=KV`Mpn>j*gA+ajgo{M`Xq=kJj*Z|pFf$8LpsLFP9-uq zMuV(G&QLG8Gcre@tE1#R4&oH*^!`IrGa{&ix1>%@p)oH1%j0un48pX3@_*j-q5h|# zw;Q~}(tSB^r~B~wH+uA37$JuFpdrPU09>y}E0*x9TIs$~ejn%36L(PDM> z;pg|=x)nM#3@cIAdZzcqr_a3Nf&0v1Xppt8rvPDcj`Js`GhN;q=2e5x+H%z+#a~RU zrf{mhLiJP23}em`9;$u|HbAIvB?@!pqwOJKlr3mNZSaHI2J*}psZULu>cwaXCufPO ztdSn+tS&#VZ9mC|Mf#Q}&a`OmfIB((9jQ2ir!!Q|q2mXQJ+sRNA2QV2&Fff?_-$lF z%=u;o>b2R*Um;p#wGx(*`j;@0rJ)h}Yy*ALsI7*v;m$~h7RK{g<^z!5n%B#Z7gkb! zOZAtl^JhGvSAgvAk$#9L3TNH@tA5PyT0eYwNhjJGWa4+5uj_k^#M_e4<}Y4lh3zXn zw9n5!UEoe%0q&_%>!_xIib8|iN|b7Sz96@iJn&YRaShGhC3=g1)7}MN?l!G}Ep}FF z0~NdQsQgGvTT}^|nu^FDe-es4PSf{0;IYYYZr>`D*j|6eIUhwSVJ2g~r8V4}kdv*Z z*shVmrh6LsF^*me3U|f40{DK^>eDVgK%vr_n@ZHuDBrbm%?$7sH&^OW5cbSMQpECS zq U&%WQI&vvhU-aT8)RuAuAenXYX2Bhl&#ZMJ^{Nsv`B+Xv_L)#V&Kw@ZOBx?4`?eEQjq#af-$0J?f0#7$lE8?RE|UHU_JMs z^7Be?b>M>3aHNTb@@OBhl*sI+*4q_;i|(5B>(6;P9Nnr=YqR$rMYTnuD8H^A4+U0@ z=V7POC!*pTms~YTL|1@zy4KN3v*qW6RE2TPyt~mCns+z8`?nW?9Y05v3UGE^xZ+2a zLNjG{LXJN#KX5A}r~bTY4R}qgO{Ap9XYwTGB>1ISq3846{SjAPlRao7ePRh%L{vw6 z%l?dZC-j5qFP>HEEgiBoX099V4w!tBMd9Ia6|Tkj%gYLrN*gOzraMk2-ke8L)Y9)t zkwd`MY9D9PBGGb;eQXG|AuaQ1Bo3Qq{-(*7c0XVH4X>(34eP-^r|!9^^~0t4YA2kW z=qE~?Q9-Up1N?$SCRc!5ttdCxp(@I<3Spq*Yc{m*;*p$j4rbHLt-Au0Hs1h#@Sd2m z&07&X^|>D=?XM@?{d87a)fcy?uDLGeERuvz4wd5EvNtBWAqf)vmn0h1yimYe>sINjO&# zA?1&M=vmP2%(--(QSgyh{%3l_zmt{lGrD{m9mD^hQUG+dh>$6IF`Co{l)of(Z}03C;IkM0Wm^Av&i16B-+Fg@rbJ)m6+l_CtLas$ z=u-}M6JeAk1B919S<7_b@%|NH;*rVWaP@Fvm@qrelPv`GZ9DVam?ymL0dZ2?a^uq? z_fHAd;_YIOb7#F~Dfs8N+PM}tep)wpdLY^^{mDOf|1uwf@+ z-9zC}F8AI^964^7h$T55s+nM(kkwk9rO1trE1On4a3mf2L_KGuQp+Y4$DWVdc+yZN zRh`o|QN9f}eeE3cg(m+=%>Iwj{6BNwK9|{&9j(lshzYJ_DfUo@Z7CGhKD+{OHJ>u> ziN5leigAXQ7ioSDz1PZz4v{{on5Xs+GSBf+FkA^kHuGKqig?Kq1LM043f`szljSum zvf^@uk}%jN*bu3?x|nD6Y$DLbggDcX{Y*{ffIZD57a_Jve+1cS#_XNqU7_wv0#U{x ziz|R&rDJHb-=@Q8`awQn(3z5onafFn-*;-i-m%g7vYpDL(o{^J+NL{f*nJ;G@9{Z2 z_?Fng@Oldf$rq04Y`|4ESa1WnYkN!Jj-95`Eu8AICAw(k9l0)|JPu0t<$P-tCLRWd zCCC_l;#gq3uQysn^%8R3=ZccVy32vigc z*_=LNGOSHqtMHh&q2pwXv*QfRqEjbXu^5c2c|}MVoy^cvVC7XAImJHd@!2*1^Hb~a z5}&xO9EkT2%rKwQZOB7eK4!3Cn(tnlh?|U(;jnL;th^;>BPjw#0B6Xe+%_Mb5)Sm8 znhevpVYLinJq*Mx%pGNC1qHDer-doy(YFT`w#8??@st{b7z#R7OhN#x1sm z+5|@-I2SDPxJb#p%0+c?lF#FDRADVBC_|x2LE7b`kwp{#ak#7o&w$4GOY2XlB^BaA zO6asS&|mEWJ3(;r9{m2v(Htj34AdrMgh-ioT7N<*bAjK6ZuiYs1@5U~Z#ALQq@+xG zq26>st{?9{6ot>TEMj4C7 z;@kf z)5U_G8O3QsrRf@l28-F86NQ-upSk&*_rHOOL^uNg(GmkkH2`$T;U1bmmz%QZ3X^ky z*8l+M{ofEGB%3!cr>Cv}cLTqVvXTewLd&U&mYlw$AL&bpom*6xOA0Sh5%g@)A8M}v zJ>}pQ$vb$h@_SL2w)K~cXqUk{?fupwSX=)qPXc@`SGI+5JU-4!Is!=%9}ybI^h&zS zLBW{YNbysb=13Eh$wxNw$P!GW!MBAStUxfQX_#8c`twZ;TtRw?_0f>+wB@fR z#E>5*vGH-< zeuuEz(sU1{d}~__A`Tu-CVl?u`C?{@MyKKKI#t40M)gO?*9^sSr^@m}DD%dsy7)J{ z)6#wa#thPL3^TV^TG|@rZQg+iT2%MlHir8$m|n4e&t~QtThWVW9&EyBTh7NiOyiBD zzf%>5^o_YJ^h~d7q52#jUl%4L{*j@G%77q_{1TM=I&&tQ6nfSaIO)31udem(T;&m} zYEkIhct+VLD!)0nwXW@UhdwEiA|)1i@WDnMLFA&{kbOYBL6nGS2%AS9d}JFBeb=oE zDLJI@-yJ!nuOG{IIifnDV+0y8S){3EsXw$epNYz494yp~*2`FS`ZXex$}?=gS7SSqw*zM-sNYT|nv# z%o@evcAGvsHC1!BP4rjLaGtIaC5IDMh^m=Ct#Bh=R)6;$i(@$Oixg3aVh0~DJ-tZc zi;_im;*v);O17C&4PLB$WT&C&P*r*3)1iSvH{_A+!F=1wTg(!U7B84>!pDYcZKf1S z96URb1ti45DrSdX^DHF%=G!8}eKQ$QK8GHE3E$0PHOLxG?l~N4MxWU>U-ZW$Hvrbp zk9A*7(HcAl3${- z<5eMl&dN054_9$f$s$r3Nlsqr;Yd;$?7FwZ@5fT^+AF9TI#LVt7m`2TSxxQv`Vq`! z*Dpw%E8@KQvEh|iL40!b^N?)1LY+$T(Z&|GZK~MJ9F*bV?uL9@ciQX65fzO+BwqC( z7d0-sD}Y;5YE5+3yzd**_?Sp=Nceq6LRMBy>DF|~IO6v~rc!g;Bs#Wd1* z0`wiWKMgOj*GOq3Zk>F^@)a|Kk#Hr4ULrG}&LBC#^0;`IrV-_G*6(A|SeNP|3VAv? zaWR(jY<7NSV|Lj4gPqY!RF$%gHT@jc<9$VDT2lGa_BUOPCh-O1C`i%p7r{ZKm`c!0 zKVD)0pu)HZ~ocfZ;hOD!m1O(lP2&29OG84(9%9+e0$!5J$Et zNiVf08lc=Hsw*T|SPCe;`~@5xf+RjwYS%rkb1X*gH9MY83G zrmU#q)+%&iD-6z>kt9h~o4#||Wwh0i9cws-@jyVkVY?Vys!O5eVhPrIaF0jdLF z;6a30xV_3wXt3)B!?*B)m=$27v{SL5=Z7}SXF1eHUk2)z4EJ>H^R-aAiF9!>#+$?m zz3xumL^(H31J2QiexBCLZ^MW|xVE5Hr$L_d74+312(=oNq>2mQVRREjEHXYX3P zhm`-!;{7jX-`-75a=ilBZ+^W3#AICoK!1Ng9yKX|6tfrSoXLz;Q7S5H(G4;D@EEma zqH5JPl(4dpMEU$a=*u#jZmr7WlC85Rr*fdLSuIa+dN|seF3fdp(}Env-k8~$R`-N3 z61im&W@<$UwMjp`887su)bCz;UjMRP#25Q|pspH%rIzvZdCFSh8c7a{)V)(VBf}8V z!%&4m$y7}h?EE?gh<%v(s|}f5yt%yCS(L1o=|}v!wqkZ4Wk*+^7{d&u{(RnZIo|=- zHSN3&;+DZQRgqq%H0!bZI_dbr*1Qy3xg#q1LR+xYjNwXDAvkf~!2rUdHBgGWN51~j zZzTUbS3qjvZo_^KZYtULxF*kum$(bk{S&euD(QNllJMT8u^vsHE~>!IPmT#gqtkH~ zvykO+t=LhW)R!AG4Vlr=jiS9trYZITC0Ov*807~EF|6sGkqB~+(GVbQgqH_W%gvB-{G%DnsvLlo;p1fIes}-E;v+*o773q^VF-dmssmF6>N#G9U2g`E2maWi- zl@}X^#Rn92P2y#260WFD$!{eHx;`8}-Ga>yq9*e7Qf%Rg_&Ba$W(P%%w;6DJa^8ft zx&tFs_D9LeuVrNsq!uDa@X$;18iaPLI#91VH8J`b#aW*|*%ojuF=h2|yn1aE zW>cAe&V`g*l)O|T_wv};+dL&{GQ0wG6rkVD8+{WvH}c;9Ars}=5BlfHC-@xP?U^6? zodz8rYcRH{j9XwSD-GAl($4_tMZB;{1vPaV64EK?$I{{rbSk>}edz{zyG(jFPACU! zrdU20-%F|3ALn&R`XtYw!Uk*zDvz^zu1q>D5qij?oifwJoL(8p%$(xrU)R z0fvgqLwD|0Z>lZOZB4>&Py!NpCLCE8Eofu-dgR@*3)SmhmraA$2NIAJ&=09m7|W^m z${e%8wBwlb#z^|YPD?yV-j?1ZtF^O4pSN_VdoTowp$n-&b%;T>)CKD@Oa0y~tK6t9 z8hfiDH&r+g46FAyKUh8jRg2`TxjZ&Ga{w0`yb#;0Zx2fQvec!2$Hv?wz@@;mI!*h~ zK6a2i9XGsJ)@DT%`t-hTTmdrY-TT&B^XddLwg z{)4u6QE9XR7aw}ID~uR*Gs0g6Br9h)M|8$&GOE4}E$mT93lF|PBie$LFL_qPDFhUl z4JIQWml&9IXyt2ihGmteH>xhCM!okQn&|!*H<@Z96eriUv`ws)H4Z|rMhu^Qft8%9 z1-;~(h>G`inq?I6h7bp?)6DoO4i)SYo1mdIJ2h9hw{7ff?e^zw8f1o_$XdW9*1NHJ z;`ZXoSUs?NBzcUv^_l8Kn0;}mGH+iziF%Ms53P$TqnV`A-P|UofljgPe%2N}{l~6v zX6gmo57ycx?mj_yKAOk5y|DZ)HkkFj+@ML`VC}LI zo1QXqt;^L2`mD6IW)OCFzNg^fbIQ7>>x}+_0WcmZENo%Pq3DHhb$=W8KgY zPa22Hr1CG(NC?p&4*#muGQzx3@+fuHVZ%s>K5a*__FT$UK)}VOgxLO(_O9kK@hj<+ z2!>cGrXVejL!}Tgwk~)6G&XS~?>LFam1?E-ZLD7=<=W*R%|g}Ass|=rYvNdF{*WU6 zuW>7n#9!TMY7lL#A7dKDew-LF@{q@6q{A4SBqCL^dhbRD6u623bSOkn(;#V z{!p9I%8hlB9zm>P5ADHGLj^xb-F`ERyD)52?}1fZ@mjC4=$mSX)e$$~{Xx<$3n7soYs^VKvU zR{SFI)w8qa7dI7>&t6d3tOG_r*T29fbH1s1d}K4b;seoG-}_4) z4;m|aU1%KtQpY2Jp8h}7aSNhxh5t~;%Zmmy{zDxfKN_t14|Rh4f0QT0{YQC1AT*Zo z?|6Cmg@yl-mj}cxj7CHL-p&nX4zUNrejtXmoSe|u+aGvHH1N>ciRKy)j0Vao$ + + + + + +
+

Hello Baeldung!

+ + +
+

This is the tutorial to convert html to pdf.

+
+
+ + \ No newline at end of file diff --git a/pdf/src/main/resources/style.css b/pdf/src/main/resources/style.css new file mode 100644 index 0000000000..381ffadf00 --- /dev/null +++ b/pdf/src/main/resources/style.css @@ -0,0 +1,6 @@ +.myclass{ + font-family: Helvetica, sans-serif; + font-size:25; + font-weight: normal; + color: blue; +} \ No newline at end of file From a6ecf8743d6edb1cce82fa18ff60e645e96e864f Mon Sep 17 00:00:00 2001 From: rvsathe Date: Fri, 17 Sep 2021 08:41:48 +0530 Subject: [PATCH 057/118] BAEL 4293 reinserted the pom dependency --- core-java-modules/core-java-os/pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core-java-modules/core-java-os/pom.xml b/core-java-modules/core-java-os/pom.xml index d088014816..572000a714 100644 --- a/core-java-modules/core-java-os/pom.xml +++ b/core-java-modules/core-java-os/pom.xml @@ -16,10 +16,10 @@ - - org.junit.jupiter + + org.junit.jupiter junit-jupiter-engine - ${junit-jupiter-engine.version} + ${junit-jupiter-engine.version} test From 3260466f5b1f545a16325f030bcb0b304a64914b Mon Sep 17 00:00:00 2001 From: Trying something new <50325284+tancafa@users.noreply.github.com> Date: Sat, 18 Sep 2021 08:50:24 +0530 Subject: [PATCH 058/118] Formatting Changes for Java 5074 (#11206) * Formatting Changes for Java 5074 Changed line change indentation to 2 spaces * Rename StreamToListComparisonWithCollectorsToListUnitTest to StreamToListComparisonWithCollectorsToListUnitTest.java --- ...omparisonWithCollectorsToListUnitTest.java | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/streams/StreamToListComparisonWithCollectorsToListUnitTest.java diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/streams/StreamToListComparisonWithCollectorsToListUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/streams/StreamToListComparisonWithCollectorsToListUnitTest.java new file mode 100644 index 0000000000..e1330bb4f7 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/streams/StreamToListComparisonWithCollectorsToListUnitTest.java @@ -0,0 +1,128 @@ +package com.baeldung.streams; + +import java.util.List; +import java.util.Locale; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class StreamToListComparisonWithCollectorsToListUnitTest { + + @Test + public void whenAddingtoCollectToList_thenSuccess() { + + List result = Stream.of(Locale.getISOCountries()) + .collect(Collectors.toList()); + + Assertions.assertDoesNotThrow(() -> { + result.add("test"); + }); + } + + @Test + public void whenSortingtoCollectToList_thenSuccess() { + + List result = Stream.of(Locale.getISOCountries()) + .collect(Collectors.toList()); + + Assertions.assertDoesNotThrow(() -> { + result.sort(String::compareToIgnoreCase); + }); + } + + @Test + public void whenAddingCollectUnmodifiableToList_thenException() { + + List result = Stream.of(Locale.getISOCountries()) + .collect(Collectors.toUnmodifiableList()); + + Assertions.assertThrows(UnsupportedOperationException.class, () -> { + result.add("test"); + }); + } + + @Test + public void whenSortingCollectUnmodifiableToList_thenSortException() { + + List result = Stream.of(Locale.getISOCountries()) + .collect(Collectors.toUnmodifiableList()); + + Assertions.assertThrows(UnsupportedOperationException.class, () -> { + result.sort(String::compareToIgnoreCase); + }); + } + + @Test + public void whenAddingStreamToList_thenException() { + + List result = Stream.of(Locale.getISOCountries()) + .toList(); + + Assertions.assertThrows(UnsupportedOperationException.class, () -> { + result.add("test"); + }); + } + + @Test + public void whenSortingStreamToList_thenException() { + + List result = Stream.of(Locale.getISOCountries()) + .toList(); + + Assertions.assertThrows(UnsupportedOperationException.class, () -> { + result.sort(String::compareToIgnoreCase); + }); + } + + @Test + public void whenComparingStreamToList_withCollectToList_thenEqual() { + + List streamToList = Stream.of(Locale.getISOCountries()) + .toList(); + List collectToList = Stream.of(Locale.getISOCountries()) + .collect(Collectors.toList()); + + Assertions.assertEquals(streamToList, collectToList); + } + + @Test + public void whenComparingStreamToList_withUnmodifiedCollectToList_thenEqual() { + + List streamToList = Stream.of(Locale.getISOCountries()) + .toList(); + List collectToUnmodifiableList = Stream.of(Locale.getISOCountries()) + .collect(Collectors.toUnmodifiableList()); + + Assertions.assertEquals(streamToList, collectToUnmodifiableList); + } + + @Test + public void whenNullCollectorsToList_thenSuccess() { + + Assertions.assertDoesNotThrow(() -> { + Stream.of(null, null) + .collect(Collectors.toList()); + }); + } + + @Test + public void whenNullCollectorsUnmodifiableToList_thenException() { + + Assertions.assertThrows(NullPointerException.class, () -> { + Stream.of(null, null) + .collect(Collectors.toUnmodifiableList()); + }); + } + + @Test + public void whenNullStreamToList_thenSuccess() { + + Assertions.assertDoesNotThrow(() -> { + Stream.of(null, null) + .toList(); + }); + } + +} From 2c837e030fa3555f79e5c9d1fe6c4c0f83238c39 Mon Sep 17 00:00:00 2001 From: mbarriola <85458535+mbarriola@users.noreply.github.com> Date: Sat, 18 Sep 2021 04:43:16 -0400 Subject: [PATCH 059/118] Bael 5119 streaming in g rpc (#11215) * Commit source code to branch * BAEL-5065 improvement of groupBy with complex key * Streaming in gRPC --- grpc/pom.xml | 151 ++++++++------- .../baeldung/grpc/streaming/StockClient.java | 183 ++++++++++++++++++ .../baeldung/grpc/streaming/StockServer.java | 147 ++++++++++++++ grpc/src/main/proto/stock_quote.proto | 31 +++ 4 files changed, 437 insertions(+), 75 deletions(-) create mode 100644 grpc/src/main/java/com/baeldung/grpc/streaming/StockClient.java create mode 100644 grpc/src/main/java/com/baeldung/grpc/streaming/StockServer.java create mode 100644 grpc/src/main/proto/stock_quote.proto diff --git a/grpc/pom.xml b/grpc/pom.xml index 915777f3bd..77ec9be464 100644 --- a/grpc/pom.xml +++ b/grpc/pom.xml @@ -1,81 +1,82 @@ - 4.0.0 - grpc - 0.0.1-SNAPSHOT - grpc - jar + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + grpc + 0.0.1-SNAPSHOT + grpc + jar - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + - - - io.grpc - grpc-netty - ${io.grpc.version} - - - io.grpc - grpc-protobuf - ${io.grpc.version} - - - io.grpc - grpc-stub - ${io.grpc.version} - - - junit - junit - ${junit.version} - test - - - - - - - kr.motd.maven - os-maven-plugin - ${os-maven-plugin.version} - - - - - org.xolstice.maven.plugins - protobuf-maven-plugin - ${protobuf-maven-plugin.version} - - - com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier} - - grpc-java - - io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier} - - - - - - compile - compile-custom - - - - - - - - - 1.16.1 - 1.6.1 - 0.6.1 - + + + io.grpc + grpc-netty-shaded + runtime + ${io.grpc.version} + + + io.grpc + grpc-protobuf + ${io.grpc.version} + + + io.grpc + grpc-stub + ${io.grpc.version} + + + junit + junit + ${junit.version} + test + + + javax.annotation + javax.annotation-api + 1.2 + + + + + + kr.motd.maven + os-maven-plugin + ${os-maven-plugin.version} + + + + + org.xolstice.maven.plugins + protobuf-maven-plugin + ${protobuf-maven-plugin.version} + + com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier} + grpc-java + io.grpc:protoc-gen-grpc-java:${io.grpc.version}:exe:${os.detected.classifier} + + + + + compile + compile-custom + + + + + + + + 1.40.1 + 3.17.2 + 1.6.2 + 0.6.1 + \ No newline at end of file diff --git a/grpc/src/main/java/com/baeldung/grpc/streaming/StockClient.java b/grpc/src/main/java/com/baeldung/grpc/streaming/StockClient.java new file mode 100644 index 0000000000..1850c975a2 --- /dev/null +++ b/grpc/src/main/java/com/baeldung/grpc/streaming/StockClient.java @@ -0,0 +1,183 @@ +package com.baeldung.grpc.streaming; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.baeldung.grpc.streaming.StockQuoteProviderGrpc.StockQuoteProviderBlockingStub; +import com.baeldung.grpc.streaming.StockQuoteProviderGrpc.StockQuoteProviderStub; + +import io.grpc.Channel; +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; +import io.grpc.Status; +import io.grpc.StatusRuntimeException; +import io.grpc.stub.StreamObserver; + +public class StockClient { + private static final Logger logger = Logger.getLogger(StockClient.class.getName()); + + private final StockQuoteProviderBlockingStub blockingStub; + private final StockQuoteProviderStub nonBlockingStub; + private List stocks; + + public StockClient(Channel channel) { + + blockingStub = StockQuoteProviderGrpc.newBlockingStub(channel); + nonBlockingStub = StockQuoteProviderGrpc.newStub(channel); + initializeStocks(); + } + + public void serverSideStreamingListOfStockPrices() { + + logInfo("######START EXAMPLE######: ServerSideStreaming - list of Stock prices from a given stock"); + Stock request = Stock.newBuilder() + .setTickerSymbol("AU") + .setCompanyName("Austich") + .setDescription("server streaming example") + .build(); + Iterator stockQuotes; + try { + logInfo("REQUEST - ticker symbol {0}", request.getTickerSymbol()); + stockQuotes = blockingStub.serverSideStreamingGetListStockQuotes(request); + for (int i = 1; stockQuotes.hasNext(); i++) { + StockQuote stockQuote = stockQuotes.next(); + logInfo("RESPONSE - Price #" + i + ": {0}", stockQuote.getPrice()); + } + } catch (StatusRuntimeException e) { + logInfo("RPC failed: {0}", e.getStatus()); + } + } + + public void clientSideStreamingGetStatisticsOfStocks() throws InterruptedException { + + logInfo("######START EXAMPLE######: ClientSideStreaming - getStatisticsOfStocks from a list of stocks"); + final CountDownLatch finishLatch = new CountDownLatch(1); + StreamObserver responseObserver = new StreamObserver() { + @Override + public void onNext(StockQuote summary) { + logInfo("RESPONSE, got stock statistics - Average Price: {0}, description: {1}", summary.getPrice(), summary.getDescription()); + } + + @Override + public void onCompleted() { + logInfo("Finished clientSideStreamingGetStatisticsOfStocks"); + finishLatch.countDown(); + } + + @Override + public void onError(Throwable t) { + logWarning("Stock Statistics Failed: {0}", Status.fromThrowable(t)); + finishLatch.countDown(); + } + }; + + StreamObserver requestObserver = nonBlockingStub.clientSideStreamingGetStatisticsOfStocks(responseObserver); + try { + + for (Stock stock : stocks) { + logInfo("REQUEST: {0}, {1}", stock.getTickerSymbol(), stock.getCompanyName()); + requestObserver.onNext(stock); + if (finishLatch.getCount() == 0) { + return; + } + } + } catch (RuntimeException e) { + requestObserver.onError(e); + throw e; + } + requestObserver.onCompleted(); + if (!finishLatch.await(1, TimeUnit.MINUTES)) { + logWarning("clientSideStreamingGetStatisticsOfStocks can not finish within 1 minutes"); + } + } + + public void bidirectionalStreamingGetListsStockQuotes() throws InterruptedException{ + + logInfo("#######START EXAMPLE#######: BidirectionalStreaming - getListsStockQuotes from list of stocks"); + final CountDownLatch finishLatch = new CountDownLatch(1); + StreamObserver responseObserver = new StreamObserver() { + @Override + public void onNext(StockQuote stockQuote) { + logInfo("RESPONSE price#{0} : {1}, description:{2}", stockQuote.getOfferNumber(), stockQuote.getPrice(), stockQuote.getDescription()); + } + + @Override + public void onCompleted() { + logInfo("Finished bidirectionalStreamingGetListsStockQuotes"); + finishLatch.countDown(); + } + + @Override + public void onError(Throwable t) { + logWarning("bidirectionalStreamingGetListsStockQuotes Failed: {0}", Status.fromThrowable(t)); + finishLatch.countDown(); + } + }; + StreamObserver requestObserver = nonBlockingStub.bidirectionalStreamingGetListsStockQuotes(responseObserver); + try { + for (Stock stock : stocks) { + logInfo("REQUEST: {0}, {1}", stock.getTickerSymbol(), stock.getCompanyName()); + requestObserver.onNext(stock); + Thread.sleep(200); + if (finishLatch.getCount() == 0) { + return; + } + } + } catch (RuntimeException e) { + requestObserver.onError(e); + throw e; + } + requestObserver.onCompleted(); + + if (!finishLatch.await(1, TimeUnit.MINUTES)) { + logWarning("bidirectionalStreamingGetListsStockQuotes can not finish within 1 minute"); + } + + } + + public static void main(String[] args) throws InterruptedException { + String target = "localhost:8980"; + if (args.length > 0) { + target = args[0]; + } + + ManagedChannel channel = ManagedChannelBuilder.forTarget(target) + .usePlaintext() + .build(); + try { + StockClient client = new StockClient(channel); + + client.serverSideStreamingListOfStockPrices(); + + client.clientSideStreamingGetStatisticsOfStocks(); + + client.bidirectionalStreamingGetListsStockQuotes(); + + } finally { + channel.shutdownNow() + .awaitTermination(5, TimeUnit.SECONDS); + } + } + + private void initializeStocks() { + + this.stocks = Arrays.asList(Stock.newBuilder().setTickerSymbol("AU").setCompanyName("Auburn Corp").setDescription("Aptitude Intel").build() + , Stock.newBuilder().setTickerSymbol("BAS").setCompanyName("Bassel Corp").setDescription("Business Intel").build() + , Stock.newBuilder().setTickerSymbol("COR").setCompanyName("Corvine Corp").setDescription("Corporate Intel").build() + , Stock.newBuilder().setTickerSymbol("DIA").setCompanyName("Dialogic Corp").setDescription("Development Intel").build() + , Stock.newBuilder().setTickerSymbol("EUS").setCompanyName("Euskaltel Corp").setDescription("English Intel").build()); + } + + private void logInfo(String msg, Object... params) { + logger.log(Level.INFO, msg, params); + } + + private void logWarning(String msg, Object... params) { + logger.log(Level.WARNING, msg, params); + } +} diff --git a/grpc/src/main/java/com/baeldung/grpc/streaming/StockServer.java b/grpc/src/main/java/com/baeldung/grpc/streaming/StockServer.java new file mode 100644 index 0000000000..f4dc6c39ac --- /dev/null +++ b/grpc/src/main/java/com/baeldung/grpc/streaming/StockServer.java @@ -0,0 +1,147 @@ +package com.baeldung.grpc.streaming; + +import java.io.IOException; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.logging.Logger; + +import io.grpc.Server; +import io.grpc.ServerBuilder; +import io.grpc.stub.StreamObserver; + +public class StockServer { + + private static final Logger logger = Logger.getLogger(StockServer.class.getName()); + private final int port; + private final Server server; + + public StockServer(int port) throws IOException { + this.port = port; + server = ServerBuilder.forPort(port) + .addService(new StockService()) + .build(); + } + + public void start() throws IOException { + server.start(); + logger.info("Server started, listening on " + port); + Runtime.getRuntime() + .addShutdownHook(new Thread() { + @Override + public void run() { + System.err.println("shutting down server"); + try { + StockServer.this.stop(); + } catch (InterruptedException e) { + e.printStackTrace(System.err); + } + System.err.println("server shutted down"); + } + }); + } + + public void stop() throws InterruptedException { + if (server != null) { + server.shutdown() + .awaitTermination(30, TimeUnit.SECONDS); + } + } + + public static void main(String[] args) throws Exception { + StockServer stockServer = new StockServer(8980); + stockServer.start(); + if (stockServer.server != null) { + stockServer.server.awaitTermination(); + } + } + + private static class StockService extends StockQuoteProviderGrpc.StockQuoteProviderImplBase { + + StockService() { + } + + @Override + public void serverSideStreamingGetListStockQuotes(Stock request, StreamObserver responseObserver) { + + for (int i = 1; i <= 5; i++) { + + StockQuote stockQuote = StockQuote.newBuilder() + .setPrice(fetchStockPriceBid(request)) + .setOfferNumber(i) + .setDescription("Price for stock:" + request.getTickerSymbol()) + .build(); + responseObserver.onNext(stockQuote); + } + responseObserver.onCompleted(); + } + + @Override + public StreamObserver clientSideStreamingGetStatisticsOfStocks(final StreamObserver responseObserver) { + return new StreamObserver() { + int count; + double price = 0.0; + StringBuffer sb = new StringBuffer(); + + @Override + public void onNext(Stock stock) { + count++; + price = +fetchStockPriceBid(stock); + sb.append(":") + .append(stock.getTickerSymbol()); + } + + @Override + public void onCompleted() { + responseObserver.onNext(StockQuote.newBuilder() + .setPrice(price / count) + .setDescription("Statistics-" + sb.toString()) + .build()); + responseObserver.onCompleted(); + } + + @Override + public void onError(Throwable t) { + logger.log(Level.WARNING, "error:{0}", t.getMessage()); + } + }; + } + + @Override + public StreamObserver bidirectionalStreamingGetListsStockQuotes(final StreamObserver responseObserver) { + return new StreamObserver() { + @Override + public void onNext(Stock request) { + + for (int i = 1; i <= 5; i++) { + + StockQuote stockQuote = StockQuote.newBuilder() + .setPrice(fetchStockPriceBid(request)) + .setOfferNumber(i) + .setDescription("Price for stock:" + request.getTickerSymbol()) + .build(); + responseObserver.onNext(stockQuote); + } + } + + @Override + public void onCompleted() { + responseObserver.onCompleted(); + } + + @Override + public void onError(Throwable t) { + logger.log(Level.WARNING, "error:{0}", t.getMessage()); + } + }; + } + } + + private static double fetchStockPriceBid(Stock stock) { + + return stock.getTickerSymbol() + .length() + + ThreadLocalRandom.current() + .nextDouble(-0.1d, 0.1d); + } +} \ No newline at end of file diff --git a/grpc/src/main/proto/stock_quote.proto b/grpc/src/main/proto/stock_quote.proto new file mode 100644 index 0000000000..66891a5008 --- /dev/null +++ b/grpc/src/main/proto/stock_quote.proto @@ -0,0 +1,31 @@ +syntax = "proto3"; + +package stockquote; + +option java_multiple_files = true; +option java_package = "com.baeldung.grpc.streaming"; +option java_outer_classname = "StockQuoteProto"; +option objc_class_prefix = "RTG"; + +//basic setup ... + +service StockQuoteProvider { + + rpc serverSideStreamingGetListStockQuotes(Stock) returns (stream StockQuote) {} + + rpc clientSideStreamingGetStatisticsOfStocks(stream Stock) returns (StockQuote) {} + + rpc bidirectionalStreamingGetListsStockQuotes(stream Stock) returns (stream StockQuote) {} +} + +message Stock { + string ticker_symbol = 1; + string company_name = 2; + string description = 3; +} + +message StockQuote { + double price = 1; + int32 offer_number = 2; + string description = 3; +} From da13dd1aafd260ee75aaace7c5b23b036ebcfaa4 Mon Sep 17 00:00:00 2001 From: saikatcse03 <40471715+saikatcse03@users.noreply.github.com> Date: Sat, 18 Sep 2021 22:06:00 +0530 Subject: [PATCH 060/118] Added code to demo expected package error (#11045) * Added code for demo package error * Corrected the package declaration * BAEL-4216 Corrected Formatting and Removed final and Super * Added few comments as per PR * Update Book.java Formatted comments with spaces Co-authored-by: ine12363914 --- .../java/com/baeldung/bookstore/Book.java | 21 +++++++++++++++++++ .../com/baeldung/bookstore/LibraryAdmin.java | 14 +++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/bookstore/Book.java create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/bookstore/LibraryAdmin.java diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/bookstore/Book.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/bookstore/Book.java new file mode 100644 index 0000000000..83ed76bab0 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/bookstore/Book.java @@ -0,0 +1,21 @@ +// The next line is commented out to avoid the code to fail the build. +// package com.baeldung; + +/** +* If the below package declaration is commented out and the above incorrect package +* declaration is uncommented, then the problem will replicate. +*/ +package com.baeldung.bookstore; + +public class Book { + + private String title; + private String author; + private long isbn; + + public Book(String title, String author, long isbn) { + this.title = title; + this.author = author; + this.isbn = isbn; + } +} diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/bookstore/LibraryAdmin.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/bookstore/LibraryAdmin.java new file mode 100644 index 0000000000..9abd654589 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/bookstore/LibraryAdmin.java @@ -0,0 +1,14 @@ +package com.baeldung.bookstore; + +import java.util.Random; + +public class LibraryAdmin { + + public Book createBook(String title, String author) { + + final long isbn = new Random().nextLong(); + + return new Book(title, author, isbn); + } + +} \ No newline at end of file From fe7d2af8c0e47c99369cbad13b4829f5a9c7bfa2 Mon Sep 17 00:00:00 2001 From: freelansam <79205526+freelansam@users.noreply.github.com> Date: Mon, 20 Sep 2021 00:04:26 +0530 Subject: [PATCH 061/118] JAVA-7178: Fix formatting of POMs (#11236) * JAVA-7178: Fix formatting of POMs * JAVA-7178: fix failing build --- algorithms-genetic/pom.xml | 2 +- apache-tapestry/pom.xml | 3 - aws-reactive/pom.xml | 2 +- core-java-modules/core-java-12/pom.xml | 104 ++++++++-------- core-java-modules/core-java-14/pom.xml | 4 +- core-java-modules/core-java-16/pom.xml | 90 +++++++------- core-java-modules/core-java-8-2/pom.xml | 2 +- .../core-java-8-datetime-2/pom.xml | 2 +- .../core-java-8-datetime/pom.xml | 2 +- core-java-modules/core-java-8/pom.xml | 2 +- .../core-java-9-new-features/pom.xml | 4 +- .../core-java-arrays-sorting/pom.xml | 1 + core-java-modules/core-java-char/pom.xml | 2 +- .../core-java-collections-2/pom.xml | 2 +- .../core-java-collections-3/pom.xml | 2 +- .../core-java-collections-4/pom.xml | 48 ++++---- .../core-java-collections-maps/pom.xml | 4 +- .../core-java-collections/pom.xml | 2 +- .../core-java-date-operations-1/pom.xml | 1 - .../core-java-exceptions-3/pom.xml | 1 + .../core-java-lang-oop-types/pom.xml | 1 + core-java-modules/pom.xml | 1 + java-lite/pom.xml | 5 - javaxval/pom.xml | 1 + jersey/pom.xml | 6 +- jta/pom.xml | 1 - ksqldb/pom.xml | 7 +- kubernetes/k8s-intro/pom.xml | 1 - mapstruct/pom.xml | 6 +- maven-modules/host-maven-repo-example/pom.xml | 24 ++-- .../aggregator/module1/pom.xml | 3 +- .../aggregator/module2/module3/pom.xml | 3 +- .../aggregator/module2/pom.xml | 3 +- .../aggregator/pom.xml | 3 +- .../maven-parent-pom-resolution/pom.xml | 2 +- maven-modules/maven-printing-plugins/pom.xml | 5 +- maven-modules/maven-properties/pom.xml | 8 +- maven-modules/maven-surefire-plugin/pom.xml | 2 +- maven-modules/pom.xml | 2 +- .../oauth2-authorization-server/pom.xml | 4 +- oauth2-framework-impl/oauth2-client/pom.xml | 4 +- .../oauth2-resource-server/pom.xml | 4 +- oauth2-framework-impl/pom.xml | 4 +- osgi/osgi-intro-sample-client/pom.xml | 2 - .../design-patterns-architectural/pom.xml | 1 - patterns/design-patterns-cloud/pom.xml | 1 - patterns/simplehexagonalexample/pom.xml | 53 +++++---- persistence-modules/deltaspike/pom.xml | 64 +++++----- .../hibernate-enterprise/pom.xml | 6 +- persistence-modules/java-mongodb/pom.xml | 6 +- persistence-modules/redis/pom.xml | 6 +- .../spring-data-arangodb/pom.xml | 5 +- .../spring-data-cassandra-test/pom.xml | 99 +++++++--------- .../spring-data-jpa-crud/pom.xml | 3 +- .../spring-data-mongodb-reactive/pom.xml | 5 +- reactor-core/pom.xml | 2 +- restx/pom.xml | 12 +- spring-5-data-reactive/pom.xml | 5 +- .../parent-multi-module/application/pom.xml | 1 + .../spring-boot-environment/pom.xml | 2 +- .../spring-boot-exceptions/pom.xml | 2 +- .../spring-boot-flowable/pom.xml | 2 +- .../spring-boot-jasypt/pom.xml | 2 +- spring-boot-modules/spring-boot-mvc-2/pom.xml | 2 +- .../spring-boot-mvc-birt/pom.xml | 2 +- .../spring-boot-mvc-jersey/pom.xml | 2 +- spring-boot-modules/spring-boot-mvc/pom.xml | 2 +- .../spring-boot-nashorn/pom.xml | 2 +- .../spring-boot-parent/pom.xml | 2 +- .../spring-boot-performance/pom.xml | 2 +- .../spring-boot-validation/pom.xml | 2 +- spring-caching-2/pom.xml | 16 +-- spring-cloud/pom.xml | 1 - .../extra-configs/pom.xml | 22 ++-- spring-cloud/spring-cloud-aws/pom.xml | 24 ++-- .../spring-cloud-bootstrap/config/pom.xml | 24 ++-- .../spring-cloud-bootstrap/svc-rating/pom.xml | 24 ++-- .../spring-cloud-bootstrap/zipkin/pom.xml | 24 ++-- .../spring-cloud-connectors-heroku/pom.xml | 24 ++-- .../spring-cloud-contract-producer/pom.xml | 1 + .../spring-cloud-dapr/gateway/pom.xml | 22 ++-- .../spring-cloud-dapr/greeting/pom.xml | 21 ++-- spring-cloud/spring-cloud-dapr/pom.xml | 5 +- .../docker-message-server/pom.xml | 2 +- .../readiness-example/pom.xml | 2 - spring-roo/pom.xml | 4 +- spring-security-modules/pom.xml | 2 +- .../spring-security-web-react/pom.xml | 112 ++++-------------- spring-web-modules/spring-mvc-views/pom.xml | 2 +- testing-modules/junit-5-basics/pom.xml | 1 - testing-modules/rest-assured/pom.xml | 1 - 91 files changed, 461 insertions(+), 548 deletions(-) diff --git a/algorithms-genetic/pom.xml b/algorithms-genetic/pom.xml index 1c9224aff2..00c9b88dfe 100644 --- a/algorithms-genetic/pom.xml +++ b/algorithms-genetic/pom.xml @@ -50,4 +50,4 @@ 1.11 - + \ No newline at end of file diff --git a/apache-tapestry/pom.xml b/apache-tapestry/pom.xml index 13ae49b0ba..7a4c2b53b5 100644 --- a/apache-tapestry/pom.xml +++ b/apache-tapestry/pom.xml @@ -32,7 +32,6 @@ - @@ -110,8 +109,6 @@
- - jboss diff --git a/aws-reactive/pom.xml b/aws-reactive/pom.xml index ea1e0c44a4..923e1361ab 100644 --- a/aws-reactive/pom.xml +++ b/aws-reactive/pom.xml @@ -93,5 +93,5 @@ 2.2.1.RELEASE 2.10.27 - + \ No newline at end of file diff --git a/core-java-modules/core-java-12/pom.xml b/core-java-modules/core-java-12/pom.xml index ce7ec72aeb..931fce820b 100644 --- a/core-java-modules/core-java-12/pom.xml +++ b/core-java-modules/core-java-12/pom.xml @@ -1,60 +1,60 @@ - 4.0.0 - core-java-12 - 0.1.0-SNAPSHOT - core-java-12 - jar - http://maven.apache.org + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + core-java-12 + 0.1.0-SNAPSHOT + core-java-12 + jar + http://maven.apache.org - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../ - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + - - - org.assertj - assertj-core - ${assertj.version} - test - - - commons-io - commons-io - 2.11.0 - - + + + org.assertj + assertj-core + ${assertj.version} + test + + + commons-io + commons-io + 2.11.0 + + - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - ${maven.compiler.source.version} - ${maven.compiler.target.version} - --enable-preview - - - - maven-surefire-plugin - - --enable-preview - - - - + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source.version} + ${maven.compiler.target.version} + --enable-preview + + + + maven-surefire-plugin + + --enable-preview + + + + - - 12 - 12 - 3.6.1 - + + 12 + 12 + 3.6.1 + \ No newline at end of file diff --git a/core-java-modules/core-java-14/pom.xml b/core-java-modules/core-java-14/pom.xml index f3382f6577..a03332d8bc 100644 --- a/core-java-modules/core-java-14/pom.xml +++ b/core-java-modules/core-java-14/pom.xml @@ -14,7 +14,7 @@ 1.0.0-SNAPSHOT ../../ - + org.assertj @@ -35,7 +35,7 @@ test - + diff --git a/core-java-modules/core-java-16/pom.xml b/core-java-modules/core-java-16/pom.xml index a8a84511db..5a78d9a46f 100644 --- a/core-java-modules/core-java-16/pom.xml +++ b/core-java-modules/core-java-16/pom.xml @@ -1,53 +1,53 @@ - 4.0.0 - core-java-16 - 0.1.0-SNAPSHOT - core-java-16 - jar - http://maven.apache.org + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + core-java-16 + 0.1.0-SNAPSHOT + core-java-16 + jar + http://maven.apache.org - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../ - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + - - - org.assertj - assertj-core - ${assertj.version} - test - - - org.apache.commons - commons-lang3 - 3.12.0 - - + + + org.assertj + assertj-core + ${assertj.version} + test + + + org.apache.commons + commons-lang3 + 3.12.0 + + - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - ${maven.compiler.source.version} - ${maven.compiler.target.version} - - - - + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source.version} + ${maven.compiler.target.version} + + + + - - 16 - 16 - 3.6.1 - + + 16 + 16 + 3.6.1 + \ No newline at end of file diff --git a/core-java-modules/core-java-8-2/pom.xml b/core-java-modules/core-java-8-2/pom.xml index e1a861a63c..f3e60f8d5f 100644 --- a/core-java-modules/core-java-8-2/pom.xml +++ b/core-java-modules/core-java-8-2/pom.xml @@ -7,7 +7,7 @@ 0.1.0-SNAPSHOT core-java-8-2 jar - + com.baeldung.core-java-modules core-java-modules diff --git a/core-java-modules/core-java-8-datetime-2/pom.xml b/core-java-modules/core-java-8-datetime-2/pom.xml index 9972122544..a5a0417374 100644 --- a/core-java-modules/core-java-8-datetime-2/pom.xml +++ b/core-java-modules/core-java-8-datetime-2/pom.xml @@ -7,7 +7,7 @@ ${project.parent.version} core-java-8-datetime-2 jar - + com.baeldung.core-java-modules core-java-modules diff --git a/core-java-modules/core-java-8-datetime/pom.xml b/core-java-modules/core-java-8-datetime/pom.xml index 48b2d062b0..a79d5d089b 100644 --- a/core-java-modules/core-java-8-datetime/pom.xml +++ b/core-java-modules/core-java-8-datetime/pom.xml @@ -7,7 +7,7 @@ ${project.parent.version} core-java-8-datetime jar - + com.baeldung.core-java-modules core-java-modules diff --git a/core-java-modules/core-java-8/pom.xml b/core-java-modules/core-java-8/pom.xml index 13b2fbcf89..a7a2a1a0f8 100644 --- a/core-java-modules/core-java-8/pom.xml +++ b/core-java-modules/core-java-8/pom.xml @@ -7,7 +7,7 @@ 0.1.0-SNAPSHOT core-java-8 jar - + com.baeldung.core-java-modules core-java-modules diff --git a/core-java-modules/core-java-9-new-features/pom.xml b/core-java-modules/core-java-9-new-features/pom.xml index 2f174002bb..00480a28e1 100644 --- a/core-java-modules/core-java-9-new-features/pom.xml +++ b/core-java-modules/core-java-9-new-features/pom.xml @@ -39,7 +39,7 @@ test
- + incubator-features @@ -131,7 +131,7 @@ - + core-java-9-new-features diff --git a/core-java-modules/core-java-arrays-sorting/pom.xml b/core-java-modules/core-java-arrays-sorting/pom.xml index d30e2bfabc..8c55204347 100644 --- a/core-java-modules/core-java-arrays-sorting/pom.xml +++ b/core-java-modules/core-java-arrays-sorting/pom.xml @@ -78,4 +78,5 @@ 28.2-jre 3.10.0 + \ No newline at end of file diff --git a/core-java-modules/core-java-char/pom.xml b/core-java-modules/core-java-char/pom.xml index ef356d9542..23156b5a22 100644 --- a/core-java-modules/core-java-char/pom.xml +++ b/core-java-modules/core-java-char/pom.xml @@ -7,7 +7,7 @@ 0.1.0-SNAPSHOT core-java-char jar - + com.baeldung.core-java-modules core-java-modules diff --git a/core-java-modules/core-java-collections-2/pom.xml b/core-java-modules/core-java-collections-2/pom.xml index 421ba9abac..ad76990af3 100644 --- a/core-java-modules/core-java-collections-2/pom.xml +++ b/core-java-modules/core-java-collections-2/pom.xml @@ -6,7 +6,7 @@ core-java-collections-2 core-java-collections-2 jar - + com.baeldung.core-java-modules core-java-modules diff --git a/core-java-modules/core-java-collections-3/pom.xml b/core-java-modules/core-java-collections-3/pom.xml index c7b1284d5c..40f1867738 100644 --- a/core-java-modules/core-java-collections-3/pom.xml +++ b/core-java-modules/core-java-collections-3/pom.xml @@ -7,7 +7,7 @@ 0.1.0-SNAPSHOT core-java-collections-3 jar - + com.baeldung.core-java-modules core-java-modules diff --git a/core-java-modules/core-java-collections-4/pom.xml b/core-java-modules/core-java-collections-4/pom.xml index 7eb8222211..9dc26cd5d5 100644 --- a/core-java-modules/core-java-collections-4/pom.xml +++ b/core-java-modules/core-java-collections-4/pom.xml @@ -1,31 +1,31 @@ - 4.0.0 - core-java-collections-4 - 0.1.0-SNAPSHOT - core-java-collections-4 - jar + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + core-java-collections-4 + 0.1.0-SNAPSHOT + core-java-collections-4 + jar - - com.baeldung.core-java-modules - core-java-modules - 0.0.1-SNAPSHOT - ../pom.xml - + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + ../pom.xml + - - - org.assertj - assertj-core - ${assertj.version} - test - - + + + org.assertj + assertj-core + ${assertj.version} + test + + - - 3.19.0 - + + 3.19.0 + \ No newline at end of file diff --git a/core-java-modules/core-java-collections-maps/pom.xml b/core-java-modules/core-java-collections-maps/pom.xml index 68e5c736cd..855f40c304 100644 --- a/core-java-modules/core-java-collections-maps/pom.xml +++ b/core-java-modules/core-java-collections-maps/pom.xml @@ -33,5 +33,5 @@ 4.1 3.6.1 - - + + \ No newline at end of file diff --git a/core-java-modules/core-java-collections/pom.xml b/core-java-modules/core-java-collections/pom.xml index 81bcdc74aa..38513c889a 100644 --- a/core-java-modules/core-java-collections/pom.xml +++ b/core-java-modules/core-java-collections/pom.xml @@ -7,7 +7,7 @@ 0.1.0-SNAPSHOT core-java-collections jar - + com.baeldung.core-java-modules core-java-modules diff --git a/core-java-modules/core-java-date-operations-1/pom.xml b/core-java-modules/core-java-date-operations-1/pom.xml index 955cd910e9..7b7c68d1b6 100644 --- a/core-java-modules/core-java-date-operations-1/pom.xml +++ b/core-java-modules/core-java-date-operations-1/pom.xml @@ -26,7 +26,6 @@ commons-lang3 ${commons-lang3.version} - org.assertj assertj-core diff --git a/core-java-modules/core-java-exceptions-3/pom.xml b/core-java-modules/core-java-exceptions-3/pom.xml index 6e35845c6a..c4c3c00b56 100644 --- a/core-java-modules/core-java-exceptions-3/pom.xml +++ b/core-java-modules/core-java-exceptions-3/pom.xml @@ -36,4 +36,5 @@ 3.10.0 + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-types/pom.xml b/core-java-modules/core-java-lang-oop-types/pom.xml index b770caf970..a5b492f5ca 100644 --- a/core-java-modules/core-java-lang-oop-types/pom.xml +++ b/core-java-modules/core-java-lang-oop-types/pom.xml @@ -25,6 +25,7 @@ ${commons-codec.version} + 1.15 diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 13dd20b5da..b272d2aa13 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -137,4 +137,5 @@ 2.22.2 5.6.2 + \ No newline at end of file diff --git a/java-lite/pom.xml b/java-lite/pom.xml index c422e9a421..092063a110 100644 --- a/java-lite/pom.xml +++ b/java-lite/pom.xml @@ -21,13 +21,11 @@ activeweb ${activeweb.version} - mysql mysql-connector-java ${mysql.connector.java.version} - com.sun tools @@ -35,14 +33,12 @@ system ${java.home}/../lib/tools.jar - org.javalite activeweb-testing ${activeweb-testing.version} test - @@ -73,7 +69,6 @@ - org.javalite activejdbc-instrumentation diff --git a/javaxval/pom.xml b/javaxval/pom.xml index d684e9dfe2..f0093e0aa4 100644 --- a/javaxval/pom.xml +++ b/javaxval/pom.xml @@ -48,4 +48,5 @@ 5.0.2.RELEASE 3.11.1 + \ No newline at end of file diff --git a/jersey/pom.xml b/jersey/pom.xml index 9f57179065..c8a7de66ae 100644 --- a/jersey/pom.xml +++ b/jersey/pom.xml @@ -1,7 +1,7 @@ - + 4.0.0 jersey 0.0.1-SNAPSHOT diff --git a/jta/pom.xml b/jta/pom.xml index 1937f55a20..e9f9364646 100644 --- a/jta/pom.xml +++ b/jta/pom.xml @@ -29,7 +29,6 @@ org.springframework.boot spring-boot-starter - org.hsqldb hsqldb diff --git a/ksqldb/pom.xml b/ksqldb/pom.xml index 13867b16e3..970e8c3788 100644 --- a/ksqldb/pom.xml +++ b/ksqldb/pom.xml @@ -28,34 +28,29 @@ ksqldb-api-client ${ksqldb.version} - org.projectlombok lombok ${lombok.version} - org.awaitility awaitility ${awaitility.version} test - org.assertj assertj-core ${assertj.version} test - org.testcontainers testcontainers ${testcontainers.version} test - org.testcontainers junit-jupiter @@ -71,4 +66,4 @@ 1.15.3 - + \ No newline at end of file diff --git a/kubernetes/k8s-intro/pom.xml b/kubernetes/k8s-intro/pom.xml index 5da137ebb6..61722cb2c8 100644 --- a/kubernetes/k8s-intro/pom.xml +++ b/kubernetes/k8s-intro/pom.xml @@ -17,7 +17,6 @@ client-java 11.0.0 - ch.qos.logback logback-classic diff --git a/mapstruct/pom.xml b/mapstruct/pom.xml index 1e7ce6cbfc..48687a73b9 100644 --- a/mapstruct/pom.xml +++ b/mapstruct/pom.xml @@ -1,7 +1,7 @@ - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 4.0.0 mapstruct 1.0 @@ -90,4 +90,4 @@ 3.16.1 - \ No newline at end of file + \ No newline at end of file diff --git a/maven-modules/host-maven-repo-example/pom.xml b/maven-modules/host-maven-repo-example/pom.xml index 9434e4a3b3..bd58dddeda 100644 --- a/maven-modules/host-maven-repo-example/pom.xml +++ b/maven-modules/host-maven-repo-example/pom.xml @@ -1,28 +1,17 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung.maven.plugin host-maven-repo-example 1.0-SNAPSHOT - https://github.com/sgrverma23/host-maven-repo-example.git - - - github - 8 - 8 - - https://github.com/sgrverma23/host-maven-repo-example.git scm:git:git@github.com:sgrverma23/host-maven-repo-example.git scm:git:git@github.com:sgrverma23/host-maven-repo-example.git - - internal.repo @@ -30,6 +19,7 @@ file://${project.build.directory}/mvn-artifact + @@ -95,6 +85,7 @@ + PROJECT-REPO-URL @@ -105,4 +96,11 @@ + + + github + 8 + 8 + + \ No newline at end of file diff --git a/maven-modules/maven-parent-pom-resolution/aggregator/module1/pom.xml b/maven-modules/maven-parent-pom-resolution/aggregator/module1/pom.xml index 01b522748f..e1f411db2f 100644 --- a/maven-modules/maven-parent-pom-resolution/aggregator/module1/pom.xml +++ b/maven-modules/maven-parent-pom-resolution/aggregator/module1/pom.xml @@ -4,12 +4,13 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 module1 + pom + com.baeldung.maven-parent-pom-resolution aggregator 1.0.0-SNAPSHOT - pom diff --git a/maven-modules/maven-parent-pom-resolution/aggregator/module2/module3/pom.xml b/maven-modules/maven-parent-pom-resolution/aggregator/module2/module3/pom.xml index da0193be10..d983f35e3e 100644 --- a/maven-modules/maven-parent-pom-resolution/aggregator/module2/module3/pom.xml +++ b/maven-modules/maven-parent-pom-resolution/aggregator/module2/module3/pom.xml @@ -4,6 +4,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 module3 + pom + com.baeldung.maven-parent-pom-resolution aggregator @@ -12,6 +14,5 @@ ../../pom.xml - pom diff --git a/maven-modules/maven-parent-pom-resolution/aggregator/module2/pom.xml b/maven-modules/maven-parent-pom-resolution/aggregator/module2/pom.xml index bf9c89ecf4..48c1ebab9d 100644 --- a/maven-modules/maven-parent-pom-resolution/aggregator/module2/pom.xml +++ b/maven-modules/maven-parent-pom-resolution/aggregator/module2/pom.xml @@ -4,6 +4,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 module2 + pom + com.baeldung.maven-parent-pom-resolution module1 @@ -11,7 +13,6 @@ ../module1/pom.xml - pom module3 diff --git a/maven-modules/maven-parent-pom-resolution/aggregator/pom.xml b/maven-modules/maven-parent-pom-resolution/aggregator/pom.xml index 0067dfc009..8f6e3453a5 100644 --- a/maven-modules/maven-parent-pom-resolution/aggregator/pom.xml +++ b/maven-modules/maven-parent-pom-resolution/aggregator/pom.xml @@ -5,13 +5,14 @@ 4.0.0 com.baeldung.maven-parent-pom-resolution aggregator + pom + com.baeldung maven-parent-pom-resolution 1.0.0-SNAPSHOT - pom module1 diff --git a/maven-modules/maven-parent-pom-resolution/pom.xml b/maven-modules/maven-parent-pom-resolution/pom.xml index d5a96c0998..e5340032b7 100644 --- a/maven-modules/maven-parent-pom-resolution/pom.xml +++ b/maven-modules/maven-parent-pom-resolution/pom.xml @@ -25,4 +25,4 @@ - + \ No newline at end of file diff --git a/maven-modules/maven-printing-plugins/pom.xml b/maven-modules/maven-printing-plugins/pom.xml index b68c88dbee..07ddd8d1e8 100644 --- a/maven-modules/maven-printing-plugins/pom.xml +++ b/maven-modules/maven-printing-plugins/pom.xml @@ -53,7 +53,8 @@ Hello, world Embed a line break: ${line.separator} - ArtifactId is ${project.artifactId} + ArtifactId is + ${project.artifactId} INFO /logs/log-echo.txt @@ -76,7 +77,7 @@ log.info('Test message: {}', 'Hello, World!') log.info('Embed a line break {}', System.lineSeparator()) - log.info('ArtifactId is: ${project.artifactId}') + log.info('ArtifactId is: ${project.artifactId}') log.warn('Message only in debug mode') diff --git a/maven-modules/maven-properties/pom.xml b/maven-modules/maven-properties/pom.xml index 9454664c3f..f4c657c2e4 100644 --- a/maven-modules/maven-properties/pom.xml +++ b/maven-modules/maven-properties/pom.xml @@ -13,12 +13,12 @@ 1.0.0-SNAPSHOT ../.. - + junit junit - ${junit.version} + ${junit.version} @@ -46,7 +46,7 @@ ${project.name} property-from-pom - 4.13 + 4.13 - + \ No newline at end of file diff --git a/maven-modules/maven-surefire-plugin/pom.xml b/maven-modules/maven-surefire-plugin/pom.xml index 903adf3c11..98decc69e1 100644 --- a/maven-modules/maven-surefire-plugin/pom.xml +++ b/maven-modules/maven-surefire-plugin/pom.xml @@ -7,11 +7,11 @@ 0.0.1-SNAPSHOT maven-surefire-plugin jar + com.baeldung maven-modules 0.0.1-SNAPSHOT - diff --git a/maven-modules/pom.xml b/maven-modules/pom.xml index fe3bbd2653..9e60e895a0 100644 --- a/maven-modules/pom.xml +++ b/maven-modules/pom.xml @@ -39,4 +39,4 @@ maven-parent-pom-resolution - + \ No newline at end of file diff --git a/oauth2-framework-impl/oauth2-authorization-server/pom.xml b/oauth2-framework-impl/oauth2-authorization-server/pom.xml index f8ced851ba..c206fc2e55 100644 --- a/oauth2-framework-impl/oauth2-authorization-server/pom.xml +++ b/oauth2-framework-impl/oauth2-authorization-server/pom.xml @@ -1,7 +1,7 @@ - + xmlns="http://maven.apache.org/POM/4.0.0"> 4.0.0 oauth2-authorization-server oauth2-authorization-server diff --git a/oauth2-framework-impl/oauth2-client/pom.xml b/oauth2-framework-impl/oauth2-client/pom.xml index 814dabc664..715b7e729c 100644 --- a/oauth2-framework-impl/oauth2-client/pom.xml +++ b/oauth2-framework-impl/oauth2-client/pom.xml @@ -1,7 +1,7 @@ - + xmlns="http://maven.apache.org/POM/4.0.0"> 4.0.0 oauth2-client oauth2-client diff --git a/oauth2-framework-impl/oauth2-resource-server/pom.xml b/oauth2-framework-impl/oauth2-resource-server/pom.xml index 8f135055a2..d3af2cfd80 100644 --- a/oauth2-framework-impl/oauth2-resource-server/pom.xml +++ b/oauth2-framework-impl/oauth2-resource-server/pom.xml @@ -1,7 +1,7 @@ - + xmlns="http://maven.apache.org/POM/4.0.0"> 4.0.0 oauth2-resource-server oauth2-resource-server diff --git a/oauth2-framework-impl/pom.xml b/oauth2-framework-impl/pom.xml index 31983b08ad..5cfcb22fab 100644 --- a/oauth2-framework-impl/pom.xml +++ b/oauth2-framework-impl/pom.xml @@ -1,7 +1,7 @@ - + xmlns="http://maven.apache.org/POM/4.0.0"> 4.0.0 com.baeldung.oauth2 oauth2-framework-impl diff --git a/osgi/osgi-intro-sample-client/pom.xml b/osgi/osgi-intro-sample-client/pom.xml index b1b04aef78..7e679dd3a7 100644 --- a/osgi/osgi-intro-sample-client/pom.xml +++ b/osgi/osgi-intro-sample-client/pom.xml @@ -37,9 +37,7 @@ ${project.artifactId} ${project.version} com.baeldung.osgi.sample.client.Client - com.baeldung.osgi.sample.client - diff --git a/patterns/design-patterns-architectural/pom.xml b/patterns/design-patterns-architectural/pom.xml index 02716d7a10..11194a9c92 100644 --- a/patterns/design-patterns-architectural/pom.xml +++ b/patterns/design-patterns-architectural/pom.xml @@ -55,7 +55,6 @@ 6.0.6 2.5.3 3.3.0 - \ No newline at end of file diff --git a/patterns/design-patterns-cloud/pom.xml b/patterns/design-patterns-cloud/pom.xml index 950b6efb94..c3e2fcfc39 100644 --- a/patterns/design-patterns-cloud/pom.xml +++ b/patterns/design-patterns-cloud/pom.xml @@ -8,5 +8,4 @@ 1.0.0-SNAPSHOT design-patterns-cloud pom - \ No newline at end of file diff --git a/patterns/simplehexagonalexample/pom.xml b/patterns/simplehexagonalexample/pom.xml index b73e81be44..d9b9b36831 100644 --- a/patterns/simplehexagonalexample/pom.xml +++ b/patterns/simplehexagonalexample/pom.xml @@ -1,31 +1,32 @@ - 4.0.0 - 1.0.0-SNAPSHOT - simple-hexagonal-example - simpleHexagonalExample + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + 1.0.0-SNAPSHOT + simple-hexagonal-example + simpleHexagonalExample - - com.baeldung - parent-boot-2 - 0.0.1-SNAPSHOT - ../../parent-boot-2 - + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + - - - org.springframework.boot - spring-boot-starter-web - - + + + org.springframework.boot + spring-boot-starter-web + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - \ No newline at end of file diff --git a/persistence-modules/deltaspike/pom.xml b/persistence-modules/deltaspike/pom.xml index af02ba76c0..1255e5ab27 100644 --- a/persistence-modules/deltaspike/pom.xml +++ b/persistence-modules/deltaspike/pom.xml @@ -16,6 +16,38 @@ 1.0.0-SNAPSHOT + + + + + org.wildfly.bom + jboss-javaee-7.0-with-tools + ${jboss.bom.version} + pom + import + + + org.wildfly.bom + jboss-javaee-7.0-with-hibernate + ${jboss.bom.version} + pom + import + + + org.apache.deltaspike.distribution + distributions-bom + ${deltaspike.version} + pom + import + + + + @@ -245,38 +277,6 @@ - - - - - org.wildfly.bom - jboss-javaee-7.0-with-tools - ${jboss.bom.version} - pom - import - - - org.wildfly.bom - jboss-javaee-7.0-with-hibernate - ${jboss.bom.version} - pom - import - - - org.apache.deltaspike.distribution - distributions-bom - ${deltaspike.version} - pom - import - - - - 3.7.4 1.8.2 diff --git a/persistence-modules/hibernate-enterprise/pom.xml b/persistence-modules/hibernate-enterprise/pom.xml index dadfa211be..1d9ebfc156 100644 --- a/persistence-modules/hibernate-enterprise/pom.xml +++ b/persistence-modules/hibernate-enterprise/pom.xml @@ -1,7 +1,7 @@ - + 4.0.0 hibernate-enterprise 0.0.1-SNAPSHOT diff --git a/persistence-modules/java-mongodb/pom.xml b/persistence-modules/java-mongodb/pom.xml index f708a54f46..03229e72bd 100644 --- a/persistence-modules/java-mongodb/pom.xml +++ b/persistence-modules/java-mongodb/pom.xml @@ -1,7 +1,7 @@ - + 4.0.0 java-mongodb 1.0-SNAPSHOT diff --git a/persistence-modules/redis/pom.xml b/persistence-modules/redis/pom.xml index 8079d56cbd..d1cb927c20 100644 --- a/persistence-modules/redis/pom.xml +++ b/persistence-modules/redis/pom.xml @@ -1,7 +1,7 @@ - + 4.0.0 redis 0.1-SNAPSHOT diff --git a/persistence-modules/spring-data-arangodb/pom.xml b/persistence-modules/spring-data-arangodb/pom.xml index 562f06ae40..7303316edb 100644 --- a/persistence-modules/spring-data-arangodb/pom.xml +++ b/persistence-modules/spring-data-arangodb/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-data-arangodb spring-data-arangodb @@ -18,7 +18,6 @@ org.springframework.boot spring-boot-starter - com.arangodb arangodb-spring-data diff --git a/persistence-modules/spring-data-cassandra-test/pom.xml b/persistence-modules/spring-data-cassandra-test/pom.xml index f2cbc834de..5d9bcc63c2 100644 --- a/persistence-modules/spring-data-cassandra-test/pom.xml +++ b/persistence-modules/spring-data-cassandra-test/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-data-cassandra-test spring-data-cassandra-test @@ -14,6 +14,50 @@ ../../parent-boot-2 + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-cassandra + ${spring-boot-starter-data-cassandra.version} + + + com.datastax.oss + java-driver-core + ${java-driver-core.version} + + + org.projectlombok + lombok + ${lombok.version} + + + com.datastax.oss + native-protocol + ${native-protocol.version} + + + org.springframework.boot + spring-boot-starter-test + test + + + org.testcontainers + testcontainers + ${testcontainers.version} + test + + + org.testcontainers + cassandra + ${testcontainers.version} + test + + + 2.5.3 1.18.18 @@ -23,55 +67,4 @@ 1.5.0 - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-data-cassandra - ${spring-boot-starter-data-cassandra.version} - - - - com.datastax.oss - java-driver-core - ${java-driver-core.version} - - - - org.projectlombok - lombok - ${lombok.version} - - - - com.datastax.oss - native-protocol - ${native-protocol.version} - - - - org.springframework.boot - spring-boot-starter-test - test - - - - org.testcontainers - testcontainers - ${testcontainers.version} - test - - - - org.testcontainers - cassandra - ${testcontainers.version} - test - - - \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-crud/pom.xml b/persistence-modules/spring-data-jpa-crud/pom.xml index 8f9a3cc0e8..139632a42a 100644 --- a/persistence-modules/spring-data-jpa-crud/pom.xml +++ b/persistence-modules/spring-data-jpa-crud/pom.xml @@ -1,8 +1,7 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-data-jpa-crud spring-data-jpa-crud diff --git a/persistence-modules/spring-data-mongodb-reactive/pom.xml b/persistence-modules/spring-data-mongodb-reactive/pom.xml index 9fb22b6033..eabcc77352 100644 --- a/persistence-modules/spring-data-mongodb-reactive/pom.xml +++ b/persistence-modules/spring-data-mongodb-reactive/pom.xml @@ -147,10 +147,7 @@ 1.4.200 1.5.23 3.3.1.RELEASE - + 2.2.6.RELEASE diff --git a/reactor-core/pom.xml b/reactor-core/pom.xml index c13dda63d5..420b1b028a 100644 --- a/reactor-core/pom.xml +++ b/reactor-core/pom.xml @@ -45,4 +45,4 @@ 3.6.1 - + \ No newline at end of file diff --git a/restx/pom.xml b/restx/pom.xml index ac0ff36376..83dd2afd58 100644 --- a/restx/pom.xml +++ b/restx/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 restx 0.1-SNAPSHOT @@ -123,10 +123,8 @@ attach-docs - + prepare-package jar @@ -151,4 +149,4 @@ 0.35-rc4 - + \ No newline at end of file diff --git a/spring-5-data-reactive/pom.xml b/spring-5-data-reactive/pom.xml index dffd4be99b..3a9651de39 100644 --- a/spring-5-data-reactive/pom.xml +++ b/spring-5-data-reactive/pom.xml @@ -138,10 +138,7 @@ 1.4.200 1.5.23 3.3.1.RELEASE - + 2.2.6.RELEASE diff --git a/spring-boot-modules/spring-boot-custom-starter/parent-multi-module/application/pom.xml b/spring-boot-modules/spring-boot-custom-starter/parent-multi-module/application/pom.xml index 948482b21b..1c26ec32d3 100644 --- a/spring-boot-modules/spring-boot-custom-starter/parent-multi-module/application/pom.xml +++ b/spring-boot-modules/spring-boot-custom-starter/parent-multi-module/application/pom.xml @@ -34,4 +34,5 @@ + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-environment/pom.xml b/spring-boot-modules/spring-boot-environment/pom.xml index 5327825409..d4b260ee3d 100644 --- a/spring-boot-modules/spring-boot-environment/pom.xml +++ b/spring-boot-modules/spring-boot-environment/pom.xml @@ -4,8 +4,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-boot-environment - war spring-boot-environment + war Demo project for Spring Boot diff --git a/spring-boot-modules/spring-boot-exceptions/pom.xml b/spring-boot-modules/spring-boot-exceptions/pom.xml index 9866c418be..cec1bab4ff 100644 --- a/spring-boot-modules/spring-boot-exceptions/pom.xml +++ b/spring-boot-modules/spring-boot-exceptions/pom.xml @@ -4,8 +4,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-boot-exceptions - jar spring-boot-exceptions + jar Demo project for working with Spring Boot exceptions diff --git a/spring-boot-modules/spring-boot-flowable/pom.xml b/spring-boot-modules/spring-boot-flowable/pom.xml index fee4d9fdfc..320a684880 100644 --- a/spring-boot-modules/spring-boot-flowable/pom.xml +++ b/spring-boot-modules/spring-boot-flowable/pom.xml @@ -4,8 +4,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-boot-flowable - war spring-boot-flowable + war Spring Boot Flowable Module diff --git a/spring-boot-modules/spring-boot-jasypt/pom.xml b/spring-boot-modules/spring-boot-jasypt/pom.xml index 1032500de7..0a37c545ac 100644 --- a/spring-boot-modules/spring-boot-jasypt/pom.xml +++ b/spring-boot-modules/spring-boot-jasypt/pom.xml @@ -5,8 +5,8 @@ 4.0.0 com.example.jasypt spring-boot-jasypt - jar spring-boot-jasypt + jar Demo project for Spring Boot diff --git a/spring-boot-modules/spring-boot-mvc-2/pom.xml b/spring-boot-modules/spring-boot-mvc-2/pom.xml index 43e1f12efc..d347b36b3b 100644 --- a/spring-boot-modules/spring-boot-mvc-2/pom.xml +++ b/spring-boot-modules/spring-boot-mvc-2/pom.xml @@ -4,8 +4,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-boot-mvc-2 - jar spring-boot-mvc-2 + jar Module For Spring Boot MVC Web Fn diff --git a/spring-boot-modules/spring-boot-mvc-birt/pom.xml b/spring-boot-modules/spring-boot-mvc-birt/pom.xml index 54a5091559..16b07000f8 100644 --- a/spring-boot-modules/spring-boot-mvc-birt/pom.xml +++ b/spring-boot-modules/spring-boot-mvc-birt/pom.xml @@ -5,8 +5,8 @@ 4.0.0 spring-boot-mvc-birt 0.0.1-SNAPSHOT - jar spring-boot-mvc-birt + jar Module For Spring Boot Integration with BIRT 4.0.0 com.baeldung @@ -627,4 +627,4 @@ 1.2.0 - + \ No newline at end of file diff --git a/spring-security-modules/pom.xml b/spring-security-modules/pom.xml index 917360dc3e..e482a67a0e 100644 --- a/spring-security-modules/pom.xml +++ b/spring-security-modules/pom.xml @@ -49,4 +49,4 @@ spring-social-login - + \ No newline at end of file diff --git a/spring-security-modules/spring-security-web-react/pom.xml b/spring-security-modules/spring-security-web-react/pom.xml index 61df563edd..a31fafc5ad 100644 --- a/spring-security-modules/spring-security-web-react/pom.xml +++ b/spring-security-modules/spring-security-web-react/pom.xml @@ -108,41 +108,17 @@ - - + + org.eclipse.jetty jetty-maven-plugin @@ -152,61 +128,17 @@ - - + + diff --git a/spring-web-modules/spring-mvc-views/pom.xml b/spring-web-modules/spring-mvc-views/pom.xml index 7e48175ff2..ac2215f983 100644 --- a/spring-web-modules/spring-mvc-views/pom.xml +++ b/spring-web-modules/spring-mvc-views/pom.xml @@ -4,8 +4,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-mvc-views - war spring-mvc-views + war com.baeldung diff --git a/testing-modules/junit-5-basics/pom.xml b/testing-modules/junit-5-basics/pom.xml index 0358f0c29a..cf39068ae7 100644 --- a/testing-modules/junit-5-basics/pom.xml +++ b/testing-modules/junit-5-basics/pom.xml @@ -99,7 +99,6 @@ maven-surefire-plugin ${maven-surefire-plugin.version} - **/*IntegrationTest.java diff --git a/testing-modules/rest-assured/pom.xml b/testing-modules/rest-assured/pom.xml index 3bb351f123..0658094efd 100644 --- a/testing-modules/rest-assured/pom.xml +++ b/testing-modules/rest-assured/pom.xml @@ -12,7 +12,6 @@ parent-boot-2 0.0.1-SNAPSHOT ../../parent-boot-2 - From 32e5a9ccb0811672875c9593018486ae315fd440 Mon Sep 17 00:00:00 2001 From: Kayvan Tehrani Date: Mon, 20 Sep 2021 00:25:13 +0430 Subject: [PATCH 062/118] adding new module (#11199) --- .../maven-dependency-management/pom.xml | 45 +++++++++++++++++++ .../src/main/java/com/baeldung/Main.java | 11 +++++ maven-modules/pom.xml | 1 + 3 files changed, 57 insertions(+) create mode 100644 maven-modules/maven-dependency-management/pom.xml create mode 100644 maven-modules/maven-dependency-management/src/main/java/com/baeldung/Main.java diff --git a/maven-modules/maven-dependency-management/pom.xml b/maven-modules/maven-dependency-management/pom.xml new file mode 100644 index 0000000000..fb2bdfe602 --- /dev/null +++ b/maven-modules/maven-dependency-management/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.baeldung + maven-dependency + 1.0.0-SNAPSHOT + pom + + + maven-modules + com.baeldung + 0.0.1-SNAPSHOT + + + + + + + junit + junit + 4.13.2 + test + + + org.apache.commons + commons-lang3 + 3.12.0 + + + + + + + junit + junit + + + org.apache.commons + commons-lang3 + + + \ No newline at end of file diff --git a/maven-modules/maven-dependency-management/src/main/java/com/baeldung/Main.java b/maven-modules/maven-dependency-management/src/main/java/com/baeldung/Main.java new file mode 100644 index 0000000000..ea4451a41b --- /dev/null +++ b/maven-modules/maven-dependency-management/src/main/java/com/baeldung/Main.java @@ -0,0 +1,11 @@ +package com.baeldung; + +import org.apache.commons.lang3.StringUtils; + +public class Main { + + public static void main(String[] args) { + + StringUtils.isBlank(" "); + } +} diff --git a/maven-modules/pom.xml b/maven-modules/pom.xml index 9e60e895a0..3f87c60406 100644 --- a/maven-modules/pom.xml +++ b/maven-modules/pom.xml @@ -37,6 +37,7 @@ plugin-management maven-surefire-plugin maven-parent-pom-resolution + maven-dependency-management \ No newline at end of file From e03662dd54b2ebf35b93c9a5f5277b44ea39fcdf Mon Sep 17 00:00:00 2001 From: Rafael Lopez Date: Sun, 19 Sep 2021 17:56:49 -0400 Subject: [PATCH 063/118] JAVA-7334: Update 'Oracle Connection Pooling With Spring' article --- .../spring-boot-persistence-2/pom.xml | 2 +- .../configuration/OracleConfiguration.java | 3 +- .../configuration/OracleUCPConfiguration.java | 32 ------------------- ...pplication-oracle-pooling-basic.properties | 9 ++++++ ...lePoolingApplicationOracleUCPLiveTest.java | 6 ++-- 5 files changed, 16 insertions(+), 36 deletions(-) delete mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleUCPConfiguration.java diff --git a/persistence-modules/spring-boot-persistence-2/pom.xml b/persistence-modules/spring-boot-persistence-2/pom.xml index b51ab17659..1a02b18e50 100644 --- a/persistence-modules/spring-boot-persistence-2/pom.xml +++ b/persistence-modules/spring-boot-persistence-2/pom.xml @@ -146,7 +146,7 @@ 3.9.1 2.1.8.RELEASE 0.9.5.2 - 19.6.0.0 + 21.1.0.0 \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleConfiguration.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleConfiguration.java index 9cf7e27c99..e201c9eb76 100644 --- a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleConfiguration.java +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleConfiguration.java @@ -20,7 +20,8 @@ public class OracleConfiguration { dataSource.setUser("books"); dataSource.setPassword("books"); dataSource.setURL("jdbc:oracle:thin:@//localhost:11521/ORCLPDB1"); - dataSource.setFastConnectionFailoverEnabled(true); + // Only with clients prior to v21 + // dataSource.setFastConnectionFailoverEnabled(true); dataSource.setImplicitCachingEnabled(true); // Only with clients prior to v11.2 // dataSource.setConnectionCachingEnabled(true); diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleUCPConfiguration.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleUCPConfiguration.java deleted file mode 100644 index b4c1544149..0000000000 --- a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleUCPConfiguration.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.spring.oracle.pooling.configuration; - -import java.sql.SQLException; - -import javax.sql.DataSource; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; - -import oracle.ucp.jdbc.PoolDataSource; -import oracle.ucp.jdbc.PoolDataSourceFactory; - -@Configuration -@Profile("oracle-ucp") -public class OracleUCPConfiguration { - - @Bean - public DataSource dataSource() throws SQLException { - PoolDataSource dataSource = PoolDataSourceFactory.getPoolDataSource(); - dataSource.setUser("books"); - dataSource.setPassword("books"); - dataSource.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); - dataSource.setURL("jdbc:oracle:thin:@//localhost:11521/ORCLPDB1"); - - dataSource.setFastConnectionFailoverEnabled(true); - dataSource.setInitialPoolSize(5); - dataSource.setMinPoolSize(5); - dataSource.setMaxPoolSize(10); - return dataSource; - } -} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/application-oracle-pooling-basic.properties b/persistence-modules/spring-boot-persistence-2/src/main/resources/application-oracle-pooling-basic.properties index 9a1c7fc89d..9de2ee476b 100644 --- a/persistence-modules/spring-boot-persistence-2/src/main/resources/application-oracle-pooling-basic.properties +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/application-oracle-pooling-basic.properties @@ -21,6 +21,15 @@ spring.datasource.hikari.poolName=HikariPoolBooks spring.datasource.tomcat.maxActive=15 spring.datasource.tomcat.minIdle=5 +# UCP settings +#Note: These properties require JDBC version 21.0.0.0 +spring.datasource.ucp.connection-factory-class-name=oracle.jdbc.pool.OracleDataSource +spring.datasource.ucp.sql-for-validate-connection=select * from dual +spring.datasource.ucp.connection-pool-name=UcpPoolBooks +spring.datasource.ucp.initial-pool-size=5 +spring.datasource.ucp.min-pool-size=5 +spring.datasource.ucp.max-pool-size=10 + # JPA settings spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect spring.jpa.hibernate.use-new-id-generator-mappings=false diff --git a/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleUCPLiveTest.java b/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleUCPLiveTest.java index 4fb6aa6bae..cf418b2cf4 100644 --- a/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleUCPLiveTest.java +++ b/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleUCPLiveTest.java @@ -9,11 +9,13 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = {SpringOraclePoolingApplication.class}) -@ActiveProfiles({"oracle-pooling-basic", "oracle-ucp"}) +@ActiveProfiles({"oracle-pooling-basic"}) +@TestPropertySource(properties = "spring.datasource.type=oracle.ucp.jdbc.UCPDataSource") public class SpringOraclePoolingApplicationOracleUCPLiveTest { @Autowired @@ -21,7 +23,7 @@ public class SpringOraclePoolingApplicationOracleUCPLiveTest { @Test public void givenOracleUCPConfiguration_thenBuildsOraclePoolDataSource() { - assertTrue(dataSource instanceof oracle.ucp.jdbc.PoolDataSource); + assertTrue(dataSource instanceof oracle.ucp.jdbc.UCPDataSource); } } From bf1131eac55c0f938108474459484d896bdae868 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Tue, 21 Sep 2021 03:37:35 +0200 Subject: [PATCH 064/118] add unit-test class for the issue 5155 (#11217) --- .../core-java-string-operations-3/pom.xml | 14 +++- .../CountSpacesInStringUnitTest.java | 65 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/countspaces/CountSpacesInStringUnitTest.java diff --git a/core-java-modules/core-java-string-operations-3/pom.xml b/core-java-modules/core-java-string-operations-3/pom.xml index b73851f90c..642ade5ab3 100644 --- a/core-java-modules/core-java-string-operations-3/pom.xml +++ b/core-java-modules/core-java-string-operations-3/pom.xml @@ -16,6 +16,16 @@ + + org.springframework + spring-core + ${spring-core.version} + + + org.apache.commons + commons-lang3 + ${apache-commons-lang3.version} + org.assertj assertj-core @@ -68,10 +78,12 @@ 11 11 3.6.1 + 5.3.9 + 3.12.0 3.6.3 6.1.1 2.11.1 3.1.0 - \ No newline at end of file + diff --git a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/countspaces/CountSpacesInStringUnitTest.java b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/countspaces/CountSpacesInStringUnitTest.java new file mode 100644 index 0000000000..0432b7dd24 --- /dev/null +++ b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/countspaces/CountSpacesInStringUnitTest.java @@ -0,0 +1,65 @@ +package com.baeldung.countspaces; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; + +class CountSpacesInStringUnitTest { + private static final String INPUT_STRING = " This string has nine spaces and a Tab:' '"; + private static final int EXPECTED_COUNT = 9; + + @Test + void givenString_whenCountSpaceByLooping_thenReturnsExpectedCount() { + int spaceCount = 0; + for (char c : INPUT_STRING.toCharArray()) { + if (c == ' ') { + spaceCount++; + } + } + assertThat(spaceCount).isEqualTo(EXPECTED_COUNT); + } + + @Test + void givenString_whenCountSpaceByJava8StreamFilter_thenReturnsExpectedCount() { + long spaceCount = INPUT_STRING.chars().filter(c -> c == (int) ' ').count(); + assertThat(spaceCount).isEqualTo(EXPECTED_COUNT); + } + + @Test + void givenString_whenCountSpaceByRegexMatcher_thenReturnsExpectedCount() { + Pattern pattern = Pattern.compile(" "); + Matcher matcher = pattern.matcher(INPUT_STRING); + int spaceCount = 0; + while (matcher.find()) { + spaceCount++; + } + assertThat(spaceCount).isEqualTo(EXPECTED_COUNT); + } + + @Test + void givenString_whenCountSpaceByReplaceAll_thenReturnsExpectedCount() { + int spaceCount = INPUT_STRING.replaceAll("[^ ]", "").length(); + assertThat(spaceCount).isEqualTo(EXPECTED_COUNT); + } + + @Test + void givenString_whenCountSpaceBySplit_thenReturnsExpectedCount() { + int spaceCount = INPUT_STRING.split(" ").length - 1; + assertThat(spaceCount).isEqualTo(EXPECTED_COUNT); + } + + @Test + void givenString_whenCountSpaceUsingApacheCommons_thenReturnsExpectedCount() { + int spaceCount = StringUtils.countMatches(INPUT_STRING, " "); + assertThat(spaceCount).isEqualTo(EXPECTED_COUNT); + } + + @Test + void givenString_whenCountSpaceUsingSpring_thenReturnsExpectedCount() { + int spaceCount = org.springframework.util.StringUtils.countOccurrencesOf(INPUT_STRING, " "); + assertThat(spaceCount).isEqualTo(EXPECTED_COUNT); + } +} From 9d0330197ce4e512ea46701af60aaf3a3e9fa2ad Mon Sep 17 00:00:00 2001 From: kwoyke Date: Tue, 21 Sep 2021 21:20:10 +0200 Subject: [PATCH 065/118] JAVA-7333: Use BeDataDriven repository for renjin dependency (#11243) --- libraries-6/pom.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries-6/pom.xml b/libraries-6/pom.xml index d2ed06af6a..4b33df9a1d 100644 --- a/libraries-6/pom.xml +++ b/libraries-6/pom.xml @@ -111,9 +111,9 @@ - mulesoft - Mulesoft Repository - https://repository.mulesoft.org/nexus/content/repositories/public/ + bedatadriven + BeDataDriven repository + https://nexus.bedatadriven.com/content/groups/public/ @@ -144,7 +144,7 @@ 1.15 3.6 3.6.2 - RELEASE + 3.5-beta72 3.0 1.8.1 4.4 From 861a8e19a44b491c1623fa93872766a14d20b3a2 Mon Sep 17 00:00:00 2001 From: LiamGve Date: Wed, 22 Sep 2021 08:33:17 +0100 Subject: [PATCH 066/118] =?UTF-8?q?BAEL-5069=20code=20for=20article=20on?= =?UTF-8?q?=20new=20features=20in=20the=20Java=2016=20incremental=E2=80=A6?= =?UTF-8?q?=20(#11176)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * BAEL-5069 code for article on new features in the Java 16 incremental release * BAEL-5069 added some details around Vector API example and enable-preview in maven to allow package to build Co-authored-by: Liam Garvie --- core-java-modules/core-java-16/pom.xml | 35 ++++++++++++++- .../com/baeldung/features/HelloWorld.java | 7 +++ .../com/baeldung/features/OuterClass.java | 9 ++++ .../com/baeldung/features/VectorExample.java | 23 ++++++++++ .../com/baeldung/features/model/Book.java | 44 +++++++++++++++++++ .../com/baeldung/features/record/Book.java | 4 ++ .../features/sealed/JungleAnimal.java | 4 ++ .../com/baeldung/features/sealed/Monkey.java | 4 ++ .../com/baeldung/features/sealed/Sealed.java | 13 ++++++ .../com/baeldung/features/sealed/Snake.java | 4 ++ .../src/main/java/module-info.java | 4 ++ .../features/DayPeriodSupportUnitTest.java | 24 ++++++++++ .../baeldung/features/HelloWorldUnitTest.java | 28 ++++++++++++ .../InstanceOfPatternMatchingUnitTest.java | 18 ++++++++ .../features/StreamToListUnitTest.java | 20 +++++++++ .../features/VectorExampleUnitTest.java | 21 +++++++++ 16 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/HelloWorld.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/OuterClass.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/VectorExample.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/model/Book.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/record/Book.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/JungleAnimal.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Monkey.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Sealed.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Snake.java create mode 100644 core-java-modules/core-java-16/src/main/java/module-info.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/features/DayPeriodSupportUnitTest.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/features/HelloWorldUnitTest.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/features/InstanceOfPatternMatchingUnitTest.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/features/StreamToListUnitTest.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/features/VectorExampleUnitTest.java diff --git a/core-java-modules/core-java-16/pom.xml b/core-java-modules/core-java-16/pom.xml index 5a78d9a46f..5d10325f03 100644 --- a/core-java-modules/core-java-16/pom.xml +++ b/core-java-modules/core-java-16/pom.xml @@ -28,6 +28,18 @@ commons-lang3 3.12.0 + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + @@ -37,17 +49,38 @@ 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} + + + 16 16 - 3.6.1 + 16 + 3.8.1 + 3.0.0-M5 + 3.17.2 \ No newline at end of file diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/HelloWorld.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/HelloWorld.java new file mode 100644 index 0000000000..7d72c4b71c --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/HelloWorld.java @@ -0,0 +1,7 @@ +package com.baeldung.features; + +interface HelloWorld { + default String hello() { + return "world"; + } +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/OuterClass.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/OuterClass.java new file mode 100644 index 0000000000..6e08674bff --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/OuterClass.java @@ -0,0 +1,9 @@ +package com.baeldung.features; + +import com.baeldung.features.record.Book; + +public class OuterClass { + class InnerClass { + Book book = new Book("Title", "author", "isbn"); + } +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/VectorExample.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/VectorExample.java new file mode 100644 index 0000000000..1f25290270 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/VectorExample.java @@ -0,0 +1,23 @@ +package com.baeldung.features; + +import jdk.incubator.vector.IntVector; + +public class VectorExample { + public int[] scalarComputation(int[] a, int[] b) { + var c = new int[a.length]; + for (int i = 0; i < a.length; i++) { + c[i] = a[i] * b[i]; + } + return c; + } + + public int[] vectorComputation(int[] a, int[] b) { + var c = new int[a.length]; + + var vectorA = IntVector.fromArray(IntVector.SPECIES_128, a, 0); + var vectorB = IntVector.fromArray(IntVector.SPECIES_128, b, 0); + var vectorC = vectorA.mul(vectorB); + vectorC.intoArray(c, 0); + return c; + } +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/model/Book.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/model/Book.java new file mode 100644 index 0000000000..c532e41649 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/model/Book.java @@ -0,0 +1,44 @@ +package com.baeldung.features.model; + +import java.util.Objects; + +public final class Book { + private final String title; + private final String author; + private final String isbn; + + public Book(String title, String author, String isbn) { + this.title = title; + this.author = author; + this.isbn = isbn; + } + + public String getTitle() { + return title; + } + + public String getAuthor() { + return author; + } + + public String getIsbn() { + return isbn; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Book book = (Book) o; + return Objects.equals(title, book.title) && Objects.equals(author, book.author) && Objects.equals(isbn, book.isbn); + } + + @Override + public int hashCode() { + return Objects.hash(title, author, isbn); + } +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/record/Book.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/record/Book.java new file mode 100644 index 0000000000..3bddf143be --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/record/Book.java @@ -0,0 +1,4 @@ +package com.baeldung.features.record; + +public record Book(String title, String author, String isbn) { +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/JungleAnimal.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/JungleAnimal.java new file mode 100644 index 0000000000..1853f8449c --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/JungleAnimal.java @@ -0,0 +1,4 @@ +package com.baeldung.features.sealed; + +public sealed interface JungleAnimal permits Monkey, Snake { +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Monkey.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Monkey.java new file mode 100644 index 0000000000..aeddfdddda --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Monkey.java @@ -0,0 +1,4 @@ +package com.baeldung.features.sealed; + +public final class Monkey implements JungleAnimal { +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Sealed.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Sealed.java new file mode 100644 index 0000000000..1ee4ff6e57 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Sealed.java @@ -0,0 +1,13 @@ +package com.baeldung.features.sealed; + +public class Sealed { + public static void main(String... args) { + JungleAnimal j = new Monkey(); + + if (j instanceof Monkey m) { + // do logic + } else if (j instanceof Snake s) { + // do logic + } + } +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Snake.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Snake.java new file mode 100644 index 0000000000..c35575f5e0 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Snake.java @@ -0,0 +1,4 @@ +package com.baeldung.features.sealed; + +public non-sealed class Snake implements JungleAnimal { +} diff --git a/core-java-modules/core-java-16/src/main/java/module-info.java b/core-java-modules/core-java-16/src/main/java/module-info.java new file mode 100644 index 0000000000..ee983a6832 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/module-info.java @@ -0,0 +1,4 @@ +module core.java { + requires jdk.incubator.vector; + requires org.apache.commons.lang3; +} \ No newline at end of file diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/features/DayPeriodSupportUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/DayPeriodSupportUnitTest.java new file mode 100644 index 0000000000..6ae6232f00 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/DayPeriodSupportUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.features; + +import org.junit.jupiter.api.Test; + +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DayPeriodSupportUnitTest { + + @Test + public void givenASpecificTime_whenFormattingUsingTheBSymbol_thenExpectVerbosePeriodOfDay() { + LocalTime date = LocalTime.parse("15:25:08.690791"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h B"); + assertThat(date.format(formatter)).isEqualTo("3 in the afternoon"); + + formatter = DateTimeFormatter.ofPattern("h BBBB"); + assertThat(date.format(formatter)).isEqualTo("3 in the afternoon"); + + formatter = DateTimeFormatter.ofPattern("h BBBBB"); + assertThat(date.format(formatter)).isEqualTo("3 in the afternoon"); + } +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/features/HelloWorldUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/HelloWorldUnitTest.java new file mode 100644 index 0000000000..d421f13dd1 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/HelloWorldUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.features; + + +import org.junit.jupiter.api.Test; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +import static java.lang.ClassLoader.getSystemClassLoader; +import static org.assertj.core.api.Assertions.assertThat; + +class HelloWorldUnitTest { + + @Test + public void givenAnInterfaceWithDefaulMethod_whenCreatingProxyInstance_thenCanInvokeDefaultMethod() throws Exception { + Object proxy = Proxy.newProxyInstance(getSystemClassLoader(), new Class[] { HelloWorld.class }, + (prox, method, args) -> { + if (method.isDefault()) { + return InvocationHandler.invokeDefault(prox, method, args); + } + return method.invoke(prox, args); + } + ); + Method method = proxy.getClass().getMethod("hello"); + assertThat(method.invoke(proxy)).isEqualTo("world"); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/features/InstanceOfPatternMatchingUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/InstanceOfPatternMatchingUnitTest.java new file mode 100644 index 0000000000..7243957332 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/InstanceOfPatternMatchingUnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung.features; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class InstanceOfPatternMatchingUnitTest { + + @Test + void givenTheNewPatternMatchingAbility_whenComparingAgainstTheTradiationalApproach_thenBothVariablesAreEqual() { + Object obj = "TEST"; + + if (obj instanceof String a) { + String b = (String) obj; + assertThat(a).isEqualTo(b); + } + } +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/features/StreamToListUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/StreamToListUnitTest.java new file mode 100644 index 0000000000..00d090a240 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/StreamToListUnitTest.java @@ -0,0 +1,20 @@ +package com.baeldung.features; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +public class StreamToListUnitTest { + + @Test + void givenAStream_whenCreatingANewListFromStream_thenCollectorsOrInbuiltFunctionAreEquivalent() { + List integersAsString = Arrays.asList("1", "2", "3"); + List ints = integersAsString.stream().map(Integer::parseInt).collect(Collectors.toList()); + List intsEquivalent = integersAsString.stream().map(Integer::parseInt).toList(); + assertThat(ints).isEqualTo(intsEquivalent); + } +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/features/VectorExampleUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/VectorExampleUnitTest.java new file mode 100644 index 0000000000..981f106ec7 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/VectorExampleUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.features; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +class VectorExampleUnitTest { + + @Test + void givenAScalarComputation_whenCalculatingUsingVectorAPI_thenResultIsSameAsPreviousMethodOfComputation() { + VectorExample objectUnderTest = new VectorExample(); + + int[] a = {1, 2, 3, 4}; + int[] b = {5, 6, 7, 8}; + + int[] result = objectUnderTest.scalarComputation(a, b); + int[] result2 = objectUnderTest.vectorComputation(a, b); + + assertArrayEquals(result, result2); + } +} \ No newline at end of file From 56799eec5064d3174f8456900b0932a01a028df9 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 22 Sep 2021 22:35:37 +0800 Subject: [PATCH 067/118] Update README.md --- spring-5-reactive-client/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-5-reactive-client/README.md b/spring-5-reactive-client/README.md index 154a3cab0b..e485897d27 100644 --- a/spring-5-reactive-client/README.md +++ b/spring-5-reactive-client/README.md @@ -11,3 +11,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Mocking a WebClient in Spring](https://www.baeldung.com/spring-mocking-webclient) - [Spring WebClient Filters](https://www.baeldung.com/spring-webclient-filters) - [Get List of JSON Objects with WebClient](https://www.baeldung.com/spring-webclient-json-list) +- [Upload a File with WebClient](https://www.baeldung.com/spring-webclient-upload-file) From af55b9b091910482a09b22713bd446c3fbb624a3 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 22 Sep 2021 22:38:44 +0800 Subject: [PATCH 068/118] Update README.md --- spring-aop/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-aop/README.md b/spring-aop/README.md index 707e0fbf81..b49c2bd457 100644 --- a/spring-aop/README.md +++ b/spring-aop/README.md @@ -13,3 +13,4 @@ This module contains articles about Spring aspect oriented programming (AOP) - [When Does Java Throw UndeclaredThrowableException?](https://www.baeldung.com/java-undeclaredthrowableexception) - [Get Advised Method Info in Spring AOP](https://www.baeldung.com/spring-aop-get-advised-method-info) - [Advise Methods on Annotated Classes With AspectJ](https://www.baeldung.com/aspectj-advise-methods) +- [Joinpoint vs. ProceedingJoinPoint in AspectJ](https://www.baeldung.com/aspectj-joinpoint-proceedingjoinpoint) From b4b55cf2c62d6f7f5ccb1e613d61f8645f22c175 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 22 Sep 2021 22:41:26 +0800 Subject: [PATCH 069/118] Update README.md --- core-java-modules/core-java-16/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-16/README.md b/core-java-modules/core-java-16/README.md index 68215b3964..4998d82e43 100644 --- a/core-java-modules/core-java-16/README.md +++ b/core-java-modules/core-java-16/README.md @@ -2,3 +2,4 @@ - [Collect a Java Stream to an Immutable Collection](https://www.baeldung.com/java-stream-immutable-collection) - [Guide to mapMulti in Stream API](https://www.baeldung.com/java-mapmulti) +- [Collecting Stream Elements into a List in Java](https://www.baeldung.com/java-stream-to-list-collecting) From 903f20426113baf92e90baaa7353fdbe99158875 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 22 Sep 2021 22:43:18 +0800 Subject: [PATCH 070/118] Update README.md --- pdf/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/pdf/README.md b/pdf/README.md index bed468ad24..dd6931ba78 100644 --- a/pdf/README.md +++ b/pdf/README.md @@ -7,3 +7,4 @@ This module contains articles about PDF files. - [Creating PDF Files in Java](https://www.baeldung.com/java-pdf-creation) - [Generating PDF Files Using Thymeleaf](https://www.baeldung.com/thymeleaf-generate-pdf) - [Java Convert PDF to Base64](https://www.baeldung.com/java-convert-pdf-to-base64) +- [HTML to PDF Using OpenPDF](https://www.baeldung.com/java-html-to-pdf) From b6aaae3059d864023660a3d6c50688f426bdf5b4 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 22 Sep 2021 22:46:39 +0800 Subject: [PATCH 071/118] Update README.md --- core-java-modules/core-java-lang-4/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-lang-4/README.md b/core-java-modules/core-java-lang-4/README.md index 562be950c0..4546a00fe6 100644 --- a/core-java-modules/core-java-lang-4/README.md +++ b/core-java-modules/core-java-lang-4/README.md @@ -8,3 +8,4 @@ This module contains articles about core features in the Java language - [Java Objects.hash() vs Objects.hashCode()](https://www.baeldung.com/java-objects-hash-vs-objects-hashcode) - [Referencing a Method in Javadoc Comments](https://www.baeldung.com/java-method-in-javadoc) - [Tiered Compilation in JVM](https://www.baeldung.com/jvm-tiered-compilation) +- [Fixing the “Declared package does not match the expected package” Error](https://www.baeldung.com/java-declared-expected-package-error) From 8afaab0e943648e26ec14e97aa4a59990dc781fc Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 22 Sep 2021 22:48:30 +0800 Subject: [PATCH 072/118] Create README.md --- maven-modules/maven-dependency-management/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 maven-modules/maven-dependency-management/README.md diff --git a/maven-modules/maven-dependency-management/README.md b/maven-modules/maven-dependency-management/README.md new file mode 100644 index 0000000000..0abf99d2d3 --- /dev/null +++ b/maven-modules/maven-dependency-management/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Maven dependencyManagement vs. dependencies Tags](https://www.baeldung.com/maven-dependencymanagement-vs-dependencies-tags) From b4facc51bdcb10493d32c32fd483dff0b01a3d8a Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 22 Sep 2021 22:55:58 +0800 Subject: [PATCH 073/118] 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 ff6ac51fab..f4cde6104f 100644 --- a/core-java-modules/core-java-string-operations-3/README.md +++ b/core-java-modules/core-java-string-operations-3/README.md @@ -5,3 +5,4 @@ - [Split Java String by Newline](https://www.baeldung.com/java-string-split-by-newline) - [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) From 0734d1ba5390a180a784ade39d04f219b21aa7ef Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 22 Sep 2021 22:59:04 +0800 Subject: [PATCH 074/118] Update README.md --- core-java-modules/core-java-os/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-os/README.md b/core-java-modules/core-java-os/README.md index 10e04a8ba6..6d477de70a 100644 --- a/core-java-modules/core-java-os/README.md +++ b/core-java-modules/core-java-os/README.md @@ -13,5 +13,6 @@ This module contains articles about working with the operating system (OS) in Ja - [Pattern Search with Grep in Java](http://www.baeldung.com/grep-in-java) - [How to Run a Shell Command in Java](http://www.baeldung.com/run-shell-command-in-java) - [Taking Screenshots Using Java](https://www.baeldung.com/java-taking-screenshots) +- [Java Sound API – Capturing Microphone](https://www.baeldung.com/java-sound-api-capture-mic) This module uses Java 9, so make sure to have the JDK 9 installed to run it. From c081431dfe4f5fafe93b105af6de46bd9882567a Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 22 Sep 2021 23:00:59 +0800 Subject: [PATCH 075/118] Update README.md --- core-java-modules/core-java-16/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-16/README.md b/core-java-modules/core-java-16/README.md index 4998d82e43..4e428c7fc1 100644 --- a/core-java-modules/core-java-16/README.md +++ b/core-java-modules/core-java-16/README.md @@ -3,3 +3,4 @@ - [Collect a Java Stream to an Immutable Collection](https://www.baeldung.com/java-stream-immutable-collection) - [Guide to mapMulti in Stream API](https://www.baeldung.com/java-mapmulti) - [Collecting Stream Elements into a List in Java](https://www.baeldung.com/java-stream-to-list-collecting) +- [New Features in Java 16](https://www.baeldung.com/java-16-new-features) From 47249aa1ffe155b8a481b854b6e4361e8ea11388 Mon Sep 17 00:00:00 2001 From: Thiago dos Santos Hora Date: Thu, 23 Sep 2021 11:07:26 +0200 Subject: [PATCH 076/118] [BAEL-4747] Add read me and utils scripts (#11242) * [BAEL-4747] Create quarkus and spring boot projects * [BAEL-4747] Add quarkus implementation and fixing native image plugins setup * Fixing build config * [BAEL-4747] Add read me and utils scripts --- pom.xml | 1 + quarkus-vs-springboot/README.md | 95 +++++ quarkus-vs-springboot/cities.csv | 136 +++++++ quarkus-vs-springboot/load_test.jmx | 384 ++++++++++++++++++ quarkus-vs-springboot/pom.xml | 22 + .../quarkus-project/build.sh | 13 + quarkus-vs-springboot/quarkus-project/pom.xml | 149 +++++++ .../src/main/docker/Dockerfile.jvm | 55 +++ .../src/main/docker/Dockerfile.legacy-jar | 51 +++ .../src/main/docker/Dockerfile.native | 27 ++ .../main/docker/Dockerfile.native-distroless | 23 ++ .../src/main/docker/quarkus.yml | 23 ++ .../ReactiveGreetingResource.java | 17 + .../com/baeldung/quarkus_project/ZipCode.java | 68 ++++ .../baeldung/quarkus_project/ZipCodeRepo.java | 19 + .../quarkus_project/ZipCodeResource.java | 46 +++ .../src/main/resources/application.properties | 9 + .../NativeGreetingResourceIT.java | 21 + .../quarkus-project/start_app.sh | 5 + .../quarkus-project/start_jvm.sh | 5 + quarkus-vs-springboot/run_test.sh | 3 + .../spring-project/build_jvm_docker.sh | 6 + quarkus-vs-springboot/spring-project/pom.xml | 171 ++++++++ .../src/main/docker/Dockerfile.jvm | 12 + .../spring-project/src/main/docker/spring.yml | 22 + .../com/baeldung/spring_project/Startup.java | 37 ++ .../baeldung/spring_project/ZipCodeApi.java | 44 ++ .../spring_project/domain/ZIPRepo.java | 11 + .../spring_project/domain/ZipCode.java | 86 ++++ .../src/main/resources/application.properties | 5 + .../baeldung/spring_project/StartupIT.java | 13 + .../src/test/resources/access-filter.json | 11 + .../spring-project/start_app.sh | 6 + .../spring-project/start_jvm.sh | 6 + 34 files changed, 1602 insertions(+) create mode 100644 quarkus-vs-springboot/README.md create mode 100644 quarkus-vs-springboot/cities.csv create mode 100644 quarkus-vs-springboot/load_test.jmx create mode 100644 quarkus-vs-springboot/pom.xml create mode 100755 quarkus-vs-springboot/quarkus-project/build.sh create mode 100644 quarkus-vs-springboot/quarkus-project/pom.xml create mode 100644 quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.jvm create mode 100644 quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.legacy-jar create mode 100644 quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.native create mode 100644 quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.native-distroless create mode 100644 quarkus-vs-springboot/quarkus-project/src/main/docker/quarkus.yml create mode 100644 quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ReactiveGreetingResource.java create mode 100644 quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ZipCode.java create mode 100644 quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ZipCodeRepo.java create mode 100644 quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ZipCodeResource.java create mode 100644 quarkus-vs-springboot/quarkus-project/src/main/resources/application.properties create mode 100644 quarkus-vs-springboot/quarkus-project/src/test/java/com/baeldung/quarkus_project/NativeGreetingResourceIT.java create mode 100755 quarkus-vs-springboot/quarkus-project/start_app.sh create mode 100755 quarkus-vs-springboot/quarkus-project/start_jvm.sh create mode 100755 quarkus-vs-springboot/run_test.sh create mode 100755 quarkus-vs-springboot/spring-project/build_jvm_docker.sh create mode 100644 quarkus-vs-springboot/spring-project/pom.xml create mode 100644 quarkus-vs-springboot/spring-project/src/main/docker/Dockerfile.jvm create mode 100644 quarkus-vs-springboot/spring-project/src/main/docker/spring.yml create mode 100644 quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/Startup.java create mode 100644 quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/ZipCodeApi.java create mode 100644 quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/domain/ZIPRepo.java create mode 100644 quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/domain/ZipCode.java create mode 100644 quarkus-vs-springboot/spring-project/src/main/resources/application.properties create mode 100644 quarkus-vs-springboot/spring-project/src/test/java/com/baeldung/spring_project/StartupIT.java create mode 100644 quarkus-vs-springboot/spring-project/src/test/resources/access-filter.json create mode 100755 quarkus-vs-springboot/spring-project/start_app.sh create mode 100755 quarkus-vs-springboot/spring-project/start_jvm.sh diff --git a/pom.xml b/pom.xml index b94283277b..f5ac14a009 100644 --- a/pom.xml +++ b/pom.xml @@ -1356,6 +1356,7 @@ core-java-modules/core-java-time-measurements core-java-modules/multimodulemavenproject core-java-modules/core-java-strings + quarkus-vs-springboot diff --git a/quarkus-vs-springboot/README.md b/quarkus-vs-springboot/README.md new file mode 100644 index 0000000000..35fc8eb5eb --- /dev/null +++ b/quarkus-vs-springboot/README.md @@ -0,0 +1,95 @@ +# Spring Boot vs Quarkus + +To follow this tutorial, you will need the following things: +- GRAALVM (https://www.graalvm.org/) +- VisualVM (https://visualvm.github.io/) +- Maven (Embedded, IDE, or local installation) +- Docker (https://www.docker.com/) +- Jmeter (https://jmeter.apache.org/) + +To create this test, I used some custom features from Jmeter. You can install the Jmeter plugin manager here: +https://loadium.com/blog/how-to-install-use-jmeter-plugin. After that, please install the following plugins: +- https://jmeter-plugins.org/?search=jpgc-casutg + +The test file is `load_test.jmx` in case of any change need. You can open it with the Jmeter GUI. For example, to run the start, you can execute the file `run_test.sh` or run the comment bellow: + +``` +$jmeter_home/bin/jmeter -n -t load_test.jmx -l log.csv -e -o ./report +``` + +Just remember to change the variable `jmeter_home` with the path to the JMeter folder. The path to the data files is relative, so either keep them in the same folder as the test or use Jmeter GUI to change it. + +Open the VisualVM application and select your application to start monitoring before running the test, and of course, start the sample application first. + +## Spring Boot +To build the application, you only need to run the following command in the Spring project root: +``` +./mvnw package -f pom.xml +``` +Or this one in case you want to build the native one: +``` +./mvnw -DskipTests package -Pnative -f pom.xml +``` +In this case, you will need to have the `GRAALVM_HOME` env variable defined. You only need this if you want to build the image locally. Otherwise, you can build it using docker by leveraging the Spring Boot maven plugin. It will pull a docker image of the GraalVM, and with that, it will create the native image of the app. To do that, run: +``` +./mvnw spring-boot:build-image +``` +You can also create a docker image with the JVM version of the app running the script `build_jvm_docker.sh` or: +``` +docker build -f src/main/docker/Dockerfile.jvm -t spring-project:0.1-SNAPSHOT . +``` + +You can execute the script `start_app.sh` or `start_jvm.sh` to run the application locally. In this case, you will need the Postgres DB. You can run it in docker with the command: +``` +docker run -e POSTGRES_PASSWORD=example -p 5432:5432 postgres +``` +You can also run both application and DB from docker, using: +``` +docker-compose -f src/main/docker/spring.yml up +``` +But remember to rebuild the image to switch between native and JVM versions. + +## Quarkus +The process to build and run the Quarkus application is very similar to the Spring Boot one. First, to create the native image, you also need either the GRAALVM installed and the `GRAALVM_HOME` env variable set, or we can use docker to build the native image. + +To build the native version locally, run the command: +``` +./mvnw package -Pnative -f pom.xml +``` +Or this one to build using docker: +``` +./mvnw package -Pnative -Dquarkus.native.container-build=true -f pom.xml +``` +And to the JVM version: +``` +./mvnw package -f pom.xml +``` + +To start the application locally, use either the scripts `start_app.sh` and `start_jvm.sh` with the docker DB: +``` +docker run -e POSTGRES_PASSWORD=example -p 5432:5432 postgres +``` +Or use the script to build the docker image of the application, running: +```bash +./build.sh + +## script content +## ./mvnw quarkus:add-extension -Dextensions=container-image-docker +## ./mvnw package -Dquarkus.container-build=true -f pom.xml && +## docker build -f src/main/docker/Dockerfile.jvm -t quarkus-project:0.1-SNAPSHOT . +``` +To build the docker image of the JVM version, and running the following command to the native version: +```bash +./build.sh native + +## script content +## ./mvnw quarkus:add-extension -Dextensions=container-image-docker +## ./mvnw package -Pnative -Dquarkus.native.container-build=true -f pom.xml && +## docker build -f src/main/docker/Dockerfile.native -t quarkus-project:0.1-SNAPSHOT . +``` +Then, once again, you can also run both application and DB from docker, using: +``` +docker-compose -f src/main/docker/quarkus.yml up +``` + +Now you have all you need to reproduce the tests with your machine. \ No newline at end of file diff --git a/quarkus-vs-springboot/cities.csv b/quarkus-vs-springboot/cities.csv new file mode 100644 index 0000000000..3b7016f3b5 --- /dev/null +++ b/quarkus-vs-springboot/cities.csv @@ -0,0 +1,136 @@ +Holtsville +Adjuntas +Aguada +Aguadilla +Maricao +Anasco +Angeles +Arecibo +Bajadero +Barceloneta +Boqueron +Cabo Rojo +Penuelas +Camuy +Castaner +Rosario +Sabana Grande +Ciales +Utuado +Dorado +Ensenada +Florida +Garrochales +Guanica +Guayanilla +Hatillo +Hormigueros +Isabela +Jayuya +Lajas +Lares +Las Marias +Manati +Moca +Rincon +Quebradillas +Mayaguez +San German +San Sebastian +Morovis +Sabana Hoyos +San Antonio +Vega Alta +Vega Baja +Yauco +Aguas Buenas +Aguirre +Aibonito +Maunabo +Arroyo +Mercedita +Ponce +Naguabo +Naranjito +Orocovis +Palmer +Patillas +Caguas +Canovanas +Ceiba +Cayey +Fajardo +Cidra +Puerto Real +Punta Santiago +Roosevelt Roads +Rio Blanco +Rio Grande +Salinas +San Lorenzo +Santa Isabel +Vieques +Villalba +Yabucoa +Coamo +Las Piedras +Loiza +Luquillo +Culebra +Juncos +Gurabo +Coto Laurel +Comerio +Corozal +Guayama +La Plata +Humacao +Barranquitas +Juana Diaz +St Thomas +Christiansted +St John +Frederiksted +Kingshill +San Juan +Fort Buchanan +Toa Baja +Sabana Seca +Toa Alta +Bayamon +Catano +Guaynabo +Trujillo Alto +Saint Just +Carolina +Agawam +Amherst +Barre +Belchertown +Blandford +Bondsville +Brimfield +Chester +Chesterfield +Chicopee +Cummington +Easthampton +East Longmeadow +East Otis +Feeding Hills +Gilbertville +Goshen +Granby +Granville +Hadley +Hampden +Hardwick +Hatfield +Haydenville +Holyoke +Huntington +Leeds +Leverett +Ludlow +Monson +North Amherst \ No newline at end of file diff --git a/quarkus-vs-springboot/load_test.jmx b/quarkus-vs-springboot/load_test.jmx new file mode 100644 index 0000000000..7da984343b --- /dev/null +++ b/quarkus-vs-springboot/load_test.jmx @@ -0,0 +1,384 @@ + + + + + + false + true + false + + + + + + + + continue + + false + -1 + + 500 + 150 + true + 300 + + true + + + + , + + zip_code_database.csv + true + true + false + shareMode.group + true + zip,type,decommissioned,primary_city,acceptable_cities,unacceptable_cities,state,county,timezone,area_codes,world_region,country,latitude,longitude,irs_estimated_population + + + + true + + + + false + { + "zip":"${zip}", + "type":"${type}", + "city":"${primary_city}", + "state":"${state}", + "county":"${country}", + "timezone":"${timezone}" +} + = + + + + localhost + 8080 + + UTF-8 + zipcode + POST + true + false + true + false + + + + + + + + + Content-Type + application/json + + + Accept + */* + + + Cache-Control + no-cache + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + + + + 25 + 30 + 15 + 255 + + + + 125 + 30 + 15 + 255 + + + + 125 + 60 + 15 + 225 + + + + 225 + 150 + 30 + 120 + + + + + false + -1 + + continue + + + + , + + zip_code_database.csv + true + true + false + shareMode.group + true + zip,type,decommissioned,primary_city,acceptable_cities,unacceptable_cities,state,county,timezone,area_codes,world_region,country,latitude,longitude,irs_estimated_population + + + + + + + localhost + 8080 + + UTF-8 + zipcode/${zip} + GET + true + false + true + false + + + + + + + + + Content-Type + application/json + + + Accept + */* + + + Cache-Control + no-cache + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + + + + 25 + 30 + 15 + 255 + + + + 125 + 30 + 15 + 255 + + + + 125 + 60 + 15 + 225 + + + + 225 + 150 + 30 + 120 + + + + + false + -1 + + continue + + + + , + + cities.csv + false + false + true + shareMode.group + false + city + + + + + + + localhost + 8080 + + UTF-8 + zipcode/by_city?city=${city} + GET + true + false + true + false + + + + + + + + + Content-Type + application/json + + + Accept + */* + + + Cache-Control + no-cache + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + + + + + diff --git a/quarkus-vs-springboot/pom.xml b/quarkus-vs-springboot/pom.xml new file mode 100644 index 0000000000..cf1cbb5d85 --- /dev/null +++ b/quarkus-vs-springboot/pom.xml @@ -0,0 +1,22 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + + 1.0-SNAPSHOT + quarkus-vs-springboot + quarkus-vs-springboot + pom + 4.0.0 + + + quarkus-project + spring-project + + + \ No newline at end of file diff --git a/quarkus-vs-springboot/quarkus-project/build.sh b/quarkus-vs-springboot/quarkus-project/build.sh new file mode 100755 index 0000000000..85761adab0 --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/build.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" + +./mvnw quarkus:add-extension -Dextensions=container-image-docker + +if [ "$1" = "native" ]; then + ./mvnw package -Pnative -Dquarkus.native.container-build=true -f $SCRIPTPATH/pom.xml && + docker build -f $SCRIPTPATH/src/main/docker/Dockerfile.native -t quarkus-project:0.1-SNAPSHOT $SCRIPTPATH/. +else + ./mvnw package -Dquarkus.container-build=true -f $SCRIPTPATH/pom.xml && + docker build -f $SCRIPTPATH/src/main/docker/Dockerfile.jvm -t quarkus-project:0.1-SNAPSHOT $SCRIPTPATH/. +fi \ No newline at end of file diff --git a/quarkus-vs-springboot/quarkus-project/pom.xml b/quarkus-vs-springboot/quarkus-project/pom.xml new file mode 100644 index 0000000000..c9eae79a88 --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/pom.xml @@ -0,0 +1,149 @@ + + + 4.0.0 + + com.baeldung + quarkus-vs-springboot + 1.0-SNAPSHOT + + quarkus-project + 0.1-SNAPSHOT + + 3.8.1 + true + 11 + 11 + UTF-8 + UTF-8 + quarkus-bom + io.quarkus.platform + 2.2.2.Final + 3.0.0-M4 + + + + + ${quarkus.platform.group-id} + ${quarkus.platform.artifact-id} + ${quarkus.platform.version} + pom + import + + + + + + io.quarkus + quarkus-hibernate-reactive-panache + + + io.quarkus + quarkus-resteasy-reactive + + + io.quarkus + quarkus-resteasy-reactive-jackson + + + io.quarkus + quarkus-reactive-pg-client + + + io.quarkus + quarkus-arc + + + io.quarkus + quarkus-container-image-docker + + + io.quarkus + quarkus-junit5 + test + + + io.rest-assured + rest-assured + test + + + junit + junit + 4.8.2 + test + + + + + + ${quarkus.platform.group-id} + quarkus-maven-plugin + ${quarkus.platform.version} + true + + + + build + generate-code + generate-code-tests + + + + + + maven-compiler-plugin + ${compiler-plugin.version} + + ${maven.compiler.parameters} + + + + maven-surefire-plugin + ${surefire-plugin.version} + + false + + org.jboss.logmanager.LogManager + + + + + + + + native + + + native + + + + + + maven-failsafe-plugin + ${surefire-plugin.version} + + + + integration-test + verify + + + + ${project.build.directory}/${project.build.finalName}-runner + org.jboss.logmanager.LogManager + + + + + + + + + -H:+AllowVMInspection + native + + + + diff --git a/quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.jvm b/quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.jvm new file mode 100644 index 0000000000..e5d6d4d851 --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.jvm @@ -0,0 +1,55 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode +# +# Before building the container image run: +# +# ./mvnw package +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/code-with-quarkus-jvm . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus-jvm +# +# If you want to include the debug port into your docker image +# you will have to expose the debug port (default 5005) like this : EXPOSE 8080 5005 +# +# Then run the container using : +# +# docker run -i --rm -p 8080:8080 -p 5005:5005 -e JAVA_ENABLE_DEBUG="true" quarkus/code-with-quarkus-jvm +# +### +FROM registry.access.redhat.com/ubi8/ubi-minimal:8.4 + +ARG JAVA_PACKAGE=java-11-openjdk-headless +ARG RUN_JAVA_VERSION=1.3.8 +ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' +# Install java and the run-java script +# Also set up permissions for user `1001` +RUN microdnf install curl ca-certificates ${JAVA_PACKAGE} \ + && microdnf update \ + && microdnf clean all \ + && mkdir /deployments \ + && chown 1001 /deployments \ + && chmod "g+rwX" /deployments \ + && chown 1001:root /deployments \ + && curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \ + && chown 1001 /deployments/run-java.sh \ + && chmod 540 /deployments/run-java.sh \ + && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/conf/security/java.security + +# Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size. +ENV JAVA_OPTIONS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" +# We make four distinct layers so if there are application changes the library layers can be re-used +COPY --chown=1001 target/quarkus-app/lib/ /deployments/lib/ +COPY --chown=1001 target/quarkus-app/*.jar /deployments/ +COPY --chown=1001 target/quarkus-app/app/ /deployments/app/ +COPY --chown=1001 target/quarkus-app/quarkus/ /deployments/quarkus/ + +EXPOSE 8080 +USER 1001 + +ENTRYPOINT [ "/deployments/run-java.sh" ] + diff --git a/quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.legacy-jar b/quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.legacy-jar new file mode 100644 index 0000000000..da0fe018f6 --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.legacy-jar @@ -0,0 +1,51 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode +# +# Before building the container image run: +# +# ./mvnw package -Dquarkus.package.type=legacy-jar +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.legacy-jar -t quarkus/code-with-quarkus-legacy-jar . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus-legacy-jar +# +# If you want to include the debug port into your docker image +# you will have to expose the debug port (default 5005) like this : EXPOSE 8080 5005 +# +# Then run the container using : +# +# docker run -i --rm -p 8080:8080 -p 5005:5005 -e JAVA_ENABLE_DEBUG="true" quarkus/code-with-quarkus-legacy-jar +# +### +FROM registry.access.redhat.com/ubi8/ubi-minimal:8.4 + +ARG JAVA_PACKAGE=java-11-openjdk-headless +ARG RUN_JAVA_VERSION=1.3.8 +ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' +# Install java and the run-java script +# Also set up permissions for user `1001` +RUN microdnf install curl ca-certificates ${JAVA_PACKAGE} \ + && microdnf update \ + && microdnf clean all \ + && mkdir /deployments \ + && chown 1001 /deployments \ + && chmod "g+rwX" /deployments \ + && chown 1001:root /deployments \ + && curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \ + && chown 1001 /deployments/run-java.sh \ + && chmod 540 /deployments/run-java.sh \ + && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/conf/security/java.security + +# Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size. +ENV JAVA_OPTIONS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" +COPY target/lib/* /deployments/lib/ +COPY target/*-runner.jar /deployments/app.jar + +EXPOSE 8080 +USER 1001 + +ENTRYPOINT [ "/deployments/run-java.sh" ] diff --git a/quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.native b/quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.native new file mode 100644 index 0000000000..33698500f6 --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.native @@ -0,0 +1,27 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode +# +# Before building the container image run: +# +# ./mvnw package -Pnative +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.native -t quarkus/code-with-quarkus . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus +# +### +FROM registry.access.redhat.com/ubi8/ubi-minimal:8.4 +WORKDIR /work/ +RUN chown 1001 /work \ + && chmod "g+rwX" /work \ + && chown 1001:root /work +COPY --chown=1001:root target/*-runner /work/application + +EXPOSE 8080 +USER 1001 + +CMD ["./application", "-Dquarkus.http.host=0.0.0.0"] diff --git a/quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.native-distroless b/quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.native-distroless new file mode 100644 index 0000000000..5fda989696 --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/src/main/docker/Dockerfile.native-distroless @@ -0,0 +1,23 @@ +#### +# This Dockerfile is used in order to build a distroless container that runs the Quarkus application in native (no JVM) mode +# +# Before building the container image run: +# +# ./mvnw package -Pnative +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.native-distroless -t quarkus/code-with-quarkus . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus +# +### +FROM quay.io/quarkus/quarkus-distroless-image:1.0 +COPY target/*-runner /application + +EXPOSE 8080 +USER nonroot + +CMD ["./application", "-Dquarkus.http.host=0.0.0.0"] diff --git a/quarkus-vs-springboot/quarkus-project/src/main/docker/quarkus.yml b/quarkus-vs-springboot/quarkus-project/src/main/docker/quarkus.yml new file mode 100644 index 0000000000..00bdcf9292 --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/src/main/docker/quarkus.yml @@ -0,0 +1,23 @@ +version: '3.1' + +services: + db: + image: postgres + ports: + - '5432:5432' + environment: + POSTGRES_PASSWORD: example + app: + image: quarkus-project:0.1-SNAPSHOT + ports: + - '8080:8080' + environment: + DB_URL: postgresql://db:5432/postgres + links: + - "db" + depends_on: + - "db" +networks: + default: + driver: bridge + diff --git a/quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ReactiveGreetingResource.java b/quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ReactiveGreetingResource.java new file mode 100644 index 0000000000..b885e828c7 --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ReactiveGreetingResource.java @@ -0,0 +1,17 @@ +package com.baeldung.quarkus_project; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("/hello") +public class ReactiveGreetingResource { + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String hello() { + return "Hello RESTEasy Reactive"; + } + +} \ No newline at end of file diff --git a/quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ZipCode.java b/quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ZipCode.java new file mode 100644 index 0000000000..6764c510bf --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ZipCode.java @@ -0,0 +1,68 @@ +package com.baeldung.quarkus_project; + +import io.quarkus.hibernate.reactive.panache.PanacheEntityBase; +import io.quarkus.runtime.annotations.RegisterForReflection; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +@RegisterForReflection +public class ZipCode extends PanacheEntityBase { + + @Id + private String zip; + private String type; + private String city; + private String state; + private String county; + private String timezone; + + public String getZip() { + return zip; + } + + public void setZip(String zip) { + this.zip = zip; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getCounty() { + return county; + } + + public void setCounty(String county) { + this.county = county; + } + + public String getTimezone() { + return timezone; + } + + public void setTimezone(String timezone) { + this.timezone = timezone; + } +} diff --git a/quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ZipCodeRepo.java b/quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ZipCodeRepo.java new file mode 100644 index 0000000000..74f46c33ea --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ZipCodeRepo.java @@ -0,0 +1,19 @@ +package com.baeldung.quarkus_project; + +import io.quarkus.hibernate.reactive.panache.PanacheRepositoryBase; +import io.smallrye.mutiny.Multi; +import io.smallrye.mutiny.Uni; + +import javax.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class ZipCodeRepo implements PanacheRepositoryBase { + + public Multi findByCity(String city) { + return find("city = ?1", city).stream(); + } + + public Uni save(ZipCode zipCode) { + return zipCode.persistAndFlush(); + } +} diff --git a/quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ZipCodeResource.java b/quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ZipCodeResource.java new file mode 100644 index 0000000000..b4d41fd855 --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/src/main/java/com/baeldung/quarkus_project/ZipCodeResource.java @@ -0,0 +1,46 @@ +package com.baeldung.quarkus_project; + +import io.smallrye.mutiny.Multi; +import io.smallrye.mutiny.Uni; +import org.jboss.logging.Logger; + +import javax.transaction.Transactional; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; + +@Path("/zipcode") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class ZipCodeResource { + + private ZipCodeRepo zipRepo; + + public ZipCodeResource(ZipCodeRepo zipRepo) { + this.zipRepo = zipRepo; + } + + @GET + @Path("/{zipcode}") + public Uni findById(@PathParam("zipcode") String zipcode) { + return zipRepo.findById(zipcode); + } + + @GET + @Path("/by_city") + public Multi postZipCode(@QueryParam("city") String city) { + return zipRepo.findByCity(city); + } + + @POST + @Transactional + public Uni create(ZipCode zipCode) { + return zipRepo.findById(zipCode.getZip()) + .onItem() + .ifNull() + .switchTo(createZipCode(zipCode)); + } + + private Uni createZipCode(ZipCode zipCode) { + return Uni.createFrom().deferred(() -> zipRepo.save(zipCode)); + } +} \ No newline at end of file diff --git a/quarkus-vs-springboot/quarkus-project/src/main/resources/application.properties b/quarkus-vs-springboot/quarkus-project/src/main/resources/application.properties new file mode 100644 index 0000000000..918a129500 --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/src/main/resources/application.properties @@ -0,0 +1,9 @@ +quarkus.datasource.db-kind=postgresql +quarkus.datasource.username=postgres +quarkus.datasource.password=example + +quarkus.datasource.reactive.url=${DB_URL:postgresql://localhost:5432/postgres} +quarkus.datasource.reactive.max-size=20 + +#quarkus.hibernate-orm.log.sql=true +quarkus.hibernate-orm.database.generation=drop-and-create diff --git a/quarkus-vs-springboot/quarkus-project/src/test/java/com/baeldung/quarkus_project/NativeGreetingResourceIT.java b/quarkus-vs-springboot/quarkus-project/src/test/java/com/baeldung/quarkus_project/NativeGreetingResourceIT.java new file mode 100644 index 0000000000..99b91ea5a8 --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/src/test/java/com/baeldung/quarkus_project/NativeGreetingResourceIT.java @@ -0,0 +1,21 @@ +package com.baeldung.quarkus_project; + +import io.quarkus.test.junit.NativeImageTest; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; + +@NativeImageTest +@QuarkusTest +public class NativeGreetingResourceIT { + + @Test + void testEndpoint() { + given() + .when().get("/hello") + .then() + .statusCode(200); + } + +} \ No newline at end of file diff --git a/quarkus-vs-springboot/quarkus-project/start_app.sh b/quarkus-vs-springboot/quarkus-project/start_app.sh new file mode 100755 index 0000000000..ddffe29736 --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/start_app.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" + +$SCRIPTPATH/target/quarkus-project-0.1-SNAPSHOT-runner -XX:+FlightRecorder -XX:StartFlightRecording="filename=$SCRIPTPATH/recording.jfr,name=Profiling quarkus" \ No newline at end of file diff --git a/quarkus-vs-springboot/quarkus-project/start_jvm.sh b/quarkus-vs-springboot/quarkus-project/start_jvm.sh new file mode 100755 index 0000000000..e7a7039154 --- /dev/null +++ b/quarkus-vs-springboot/quarkus-project/start_jvm.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" + +java -jar $SCRIPTPATH/target/quarkus-app/quarkus-run.jar \ No newline at end of file diff --git a/quarkus-vs-springboot/run_test.sh b/quarkus-vs-springboot/run_test.sh new file mode 100755 index 0000000000..a2d31c8587 --- /dev/null +++ b/quarkus-vs-springboot/run_test.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +$Jmeter_home/bin/jmeter -n -t load_test.jmx -l log.csv -e -o ./report \ No newline at end of file diff --git a/quarkus-vs-springboot/spring-project/build_jvm_docker.sh b/quarkus-vs-springboot/spring-project/build_jvm_docker.sh new file mode 100755 index 0000000000..c7ee730ec7 --- /dev/null +++ b/quarkus-vs-springboot/spring-project/build_jvm_docker.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" + +docker build -f $SCRIPTPATH/src/main/docker/Dockerfile.jvm -t spring-project:0.1-SNAPSHOT $SCRIPTPATH/. + diff --git a/quarkus-vs-springboot/spring-project/pom.xml b/quarkus-vs-springboot/spring-project/pom.xml new file mode 100644 index 0000000000..be5cc57765 --- /dev/null +++ b/quarkus-vs-springboot/spring-project/pom.xml @@ -0,0 +1,171 @@ + + + + + org.springframework.boot + spring-boot-starter-parent + 2.5.4 + + + + 4.0.0 + spring-project + com.baeldung + 0.1-SNAPSHOT + + + 11 + + 0.10.3 + + + + + org.springframework.boot + spring-boot-starter-data-r2dbc + + + org.springframework.boot + spring-boot-starter-webflux + + + org.springframework.experimental + spring-native + ${spring-native.version} + + + io.r2dbc + r2dbc-postgresql + runtime + + + org.postgresql + postgresql + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + io.projectreactor + reactor-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + ${repackage.classifier} + + paketobuildpacks/builder:tiny + + true + + + + + + org.springframework.experimental + spring-aot-maven-plugin + ${spring-native.version} + + + test-generate + + test-generate + + + + generate + + generate + + + + + + + + + + spring-releases + Spring Releases + https://repo.spring.io/release + + false + + + + + + spring-releases + Spring Releases + https://repo.spring.io/release + + false + + + + + + + native + + exec + 0.9.3 + + + + org.graalvm.buildtools + junit-platform-native + ${native-buildtools.version} + + + + + + org.graalvm.buildtools + native-maven-plugin + ${native-buildtools.version} + + + -H:+AllowVMInspection + + + + + test-native + test + + test + + + + build-native + package + + build + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + -DspringAot=true -agentlib:native-image-agent=access-filter-file=src/test/resources/access-filter.json,config-merge-dir=target/classes/META-INF/native-image + + + + + + + \ No newline at end of file diff --git a/quarkus-vs-springboot/spring-project/src/main/docker/Dockerfile.jvm b/quarkus-vs-springboot/spring-project/src/main/docker/Dockerfile.jvm new file mode 100644 index 0000000000..ca3f3cca76 --- /dev/null +++ b/quarkus-vs-springboot/spring-project/src/main/docker/Dockerfile.jvm @@ -0,0 +1,12 @@ +FROM openjdk:11 + +ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' + +COPY --chown=1001 target/spring-project-0.1-SNAPSHOT-exec.jar /spring-app/ + +WORKDIR /spring-app + +EXPOSE 8080 +USER 1001 + +ENTRYPOINT ["java", "-jar", "spring-project-0.1-SNAPSHOT-exec.jar" ] \ No newline at end of file diff --git a/quarkus-vs-springboot/spring-project/src/main/docker/spring.yml b/quarkus-vs-springboot/spring-project/src/main/docker/spring.yml new file mode 100644 index 0000000000..2214e0a898 --- /dev/null +++ b/quarkus-vs-springboot/spring-project/src/main/docker/spring.yml @@ -0,0 +1,22 @@ +version: '3.1' + +services: + db: + image: postgres + ports: + - '5432:5432' + environment: + POSTGRES_PASSWORD: example + app: + image: spring-project:0.1-SNAPSHOT + ports: + - '8080:8080' + environment: + DB_URL: r2dbc:postgresql://db:5432/postgres + links: + - "db" + depends_on: + - "db" +networks: + default: + driver: bridge \ No newline at end of file diff --git a/quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/Startup.java b/quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/Startup.java new file mode 100644 index 0000000000..48cf7e8ed1 --- /dev/null +++ b/quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/Startup.java @@ -0,0 +1,37 @@ +package com.baeldung.spring_project; + +import com.baeldung.spring_project.domain.ZIPRepo; +import io.r2dbc.spi.ConnectionFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.r2dbc.connection.R2dbcTransactionManager; +import org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer; +import org.springframework.r2dbc.connection.init.ResourceDatabasePopulator; +import org.springframework.transaction.ReactiveTransactionManager; + +@SpringBootApplication +public class Startup { + + public static void main(String[] args) { + SpringApplication.run(Startup.class, args).getBean(ZIPRepo.class).findById(""); + } + + @Bean + ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) { + + var initializer = new ConnectionFactoryInitializer(); + initializer.setConnectionFactory(connectionFactory); + initializer.setDatabasePopulator(new ResourceDatabasePopulator(new ByteArrayResource(("" + + "DROP TABLE IF EXISTS zipcode;" + + "CREATE TABLE zipcode (zip VARCHAR(100) PRIMARY KEY, type VARCHAR(255) NULL, city VARCHAR(255) NULL, state VARCHAR(255) NULL, county VARCHAR(255) NULL, timezone VARCHAR(255) NULL);") + .getBytes()))); + + return initializer; + } + + @Bean ReactiveTransactionManager transactionManager(ConnectionFactory connectionFactory) { + return new R2dbcTransactionManager(connectionFactory); + } +} diff --git a/quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/ZipCodeApi.java b/quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/ZipCodeApi.java new file mode 100644 index 0000000000..263ce67e21 --- /dev/null +++ b/quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/ZipCodeApi.java @@ -0,0 +1,44 @@ +package com.baeldung.spring_project; + +import com.baeldung.spring_project.domain.ZIPRepo; +import com.baeldung.spring_project.domain.ZipCode; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.util.function.Supplier; + +@RestController +@RequestMapping(value = "/zipcode") +public class ZipCodeApi { + + private ZIPRepo zipRepo; + + public ZipCodeApi(ZIPRepo zipRepo) { + this.zipRepo = zipRepo; + } + + @GetMapping("/{zipcode}") + public Mono findById(@PathVariable String zipcode) { + return zipRepo.findById(zipcode); + } + + @GetMapping("/by_city") + public Flux postZipCode(@RequestParam String city) { + return zipRepo.findByCity(city); + } + + @Transactional + @PostMapping + public Mono create(@RequestBody ZipCode zipCode) { + return zipRepo.findById(zipCode.getZip()).switchIfEmpty(Mono.defer(createZipCode(zipCode))); + } + + private Supplier> createZipCode(ZipCode zipCode) { + return () -> { + zipCode.setId(zipCode.getZip()); + return zipRepo.save(zipCode); + }; + } +} diff --git a/quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/domain/ZIPRepo.java b/quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/domain/ZIPRepo.java new file mode 100644 index 0000000000..59d3fb4293 --- /dev/null +++ b/quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/domain/ZIPRepo.java @@ -0,0 +1,11 @@ +package com.baeldung.spring_project.domain; + +import org.springframework.data.r2dbc.repository.Query; +import org.springframework.data.repository.reactive.ReactiveCrudRepository; +import reactor.core.publisher.Flux; + +public interface ZIPRepo extends ReactiveCrudRepository { + + @Query("SELECT * FROM zipcode WHERE city = :city") + Flux findByCity(String city); +} diff --git a/quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/domain/ZipCode.java b/quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/domain/ZipCode.java new file mode 100644 index 0000000000..b2ef7b17a8 --- /dev/null +++ b/quarkus-vs-springboot/spring-project/src/main/java/com/baeldung/spring_project/domain/ZipCode.java @@ -0,0 +1,86 @@ +package com.baeldung.spring_project.domain; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Transient; +import org.springframework.data.domain.Persistable; +import org.springframework.data.relational.core.mapping.Table; + +@Table(value = "zipcode") +public class ZipCode implements Persistable { + + @Id + private String zip; + private String type; + private String city; + private String state; + private String county; + private String timezone; + + @Transient + private boolean persisted; + + public String getZip() { + return zip; + } + + void setZip(String zip) { + this.zip = zip; + this.persisted = true; + } + + public void setId(String zip) { + this.zip = zip; + this.persisted = false; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getCounty() { + return county; + } + + public void setCounty(String county) { + this.county = county; + } + + public String getTimezone() { + return timezone; + } + + public void setTimezone(String timezone) { + this.timezone = timezone; + } + + @JsonIgnore + @Override public String getId() { + return zip; + } + + @JsonIgnore + @Override public boolean isNew() { + return !persisted; + } +} diff --git a/quarkus-vs-springboot/spring-project/src/main/resources/application.properties b/quarkus-vs-springboot/spring-project/src/main/resources/application.properties new file mode 100644 index 0000000000..adc2f8b0b4 --- /dev/null +++ b/quarkus-vs-springboot/spring-project/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.r2dbc.url=${DB_URL:'r2dbc:postgresql://localhost:5432/postgres'} +spring.r2dbc.username=postgres +spring.r2dbc.password=example +spring.r2dbc.pool.enabled=true +spring.r2dbc.pool.maxSize=20 \ No newline at end of file diff --git a/quarkus-vs-springboot/spring-project/src/test/java/com/baeldung/spring_project/StartupIT.java b/quarkus-vs-springboot/spring-project/src/test/java/com/baeldung/spring_project/StartupIT.java new file mode 100644 index 0000000000..7487e5aa7f --- /dev/null +++ b/quarkus-vs-springboot/spring-project/src/test/java/com/baeldung/spring_project/StartupIT.java @@ -0,0 +1,13 @@ +package com.baeldung.spring_project; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class StartupIT { + + @Test + void contextLoads() { + } + +} diff --git a/quarkus-vs-springboot/spring-project/src/test/resources/access-filter.json b/quarkus-vs-springboot/spring-project/src/test/resources/access-filter.json new file mode 100644 index 0000000000..9718fed1c6 --- /dev/null +++ b/quarkus-vs-springboot/spring-project/src/test/resources/access-filter.json @@ -0,0 +1,11 @@ +{ "rules": [ + {"excludeClasses": "org.apache.maven.surefire.**"}, + {"excludeClasses": "net.bytebuddy.**"}, + {"excludeClasses": "org.apiguardian.**"}, + {"excludeClasses": "org.junit.**"}, + {"excludeClasses": "org.mockito.**"}, + {"excludeClasses": "org.springframework.test.**"}, + {"excludeClasses": "org.springframework.boot.test.**"}, + {"excludeClasses": "com.example.demo.test.**"} +] +} \ No newline at end of file diff --git a/quarkus-vs-springboot/spring-project/start_app.sh b/quarkus-vs-springboot/spring-project/start_app.sh new file mode 100755 index 0000000000..f596213c4c --- /dev/null +++ b/quarkus-vs-springboot/spring-project/start_app.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" + +$SCRIPTPATH/target/spring-project -XX:+FlightRecorder -XX:StartFlightRecording="filename=$SCRIPTPATH/recording.jfr,name=Profiling spring" + diff --git a/quarkus-vs-springboot/spring-project/start_jvm.sh b/quarkus-vs-springboot/spring-project/start_jvm.sh new file mode 100755 index 0000000000..f62762fcca --- /dev/null +++ b/quarkus-vs-springboot/spring-project/start_jvm.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" + +java -jar $SCRIPTPATH/target/spring-project-0.1-SNAPSHOT-exec.jar + From 9e7269b14d7590b7cb5f1afca7462711367e3fa6 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 12:37:24 +0800 Subject: [PATCH 077/118] Update README.md --- core-java-modules/core-java-16/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-16/README.md b/core-java-modules/core-java-16/README.md index 4e428c7fc1..b2740d194c 100644 --- a/core-java-modules/core-java-16/README.md +++ b/core-java-modules/core-java-16/README.md @@ -4,3 +4,4 @@ - [Guide to mapMulti in Stream API](https://www.baeldung.com/java-mapmulti) - [Collecting Stream Elements into a List in Java](https://www.baeldung.com/java-stream-to-list-collecting) - [New Features in Java 16](https://www.baeldung.com/java-16-new-features) +- [Guide to Java 8 groupingBy Collector](https://www.baeldung.com/java-groupingby-collector) From 3fd423081ef8df5b000588b3bc86688305be5ea0 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 12:43:24 +0800 Subject: [PATCH 078/118] Update README.md --- core-java-modules/core-java-io-conversions/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-io-conversions/README.md b/core-java-modules/core-java-io-conversions/README.md index 1f12c87241..52f5222040 100644 --- a/core-java-modules/core-java-io-conversions/README.md +++ b/core-java-modules/core-java-io-conversions/README.md @@ -13,4 +13,5 @@ This module contains articles about core Java input/output(IO) conversions. - [Java – Write a Reader to File](https://www.baeldung.com/java-write-reader-to-file) - [Java – Reader to Byte Array](https://www.baeldung.com/java-convert-reader-to-byte-array) - [Java – Reader to InputStream](https://www.baeldung.com/java-convert-reader-to-inputstream) +- [Java String to InputStream](https://www.baeldung.com/convert-string-to-input-stream) - More articles: [[next -->]](/core-java-modules/core-java-io-conversions-2) From d73e9d482b3a60bf84c230f329dd4971d21d41b8 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 12:46:57 +0800 Subject: [PATCH 079/118] Update README.md --- core-java-modules/core-java-11-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-11-2/README.md b/core-java-modules/core-java-11-2/README.md index ca9a306b82..93920864cc 100644 --- a/core-java-modules/core-java-11-2/README.md +++ b/core-java-modules/core-java-11-2/README.md @@ -8,3 +8,4 @@ This module contains articles about Java 11 core features - [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors) - [New Features in Java 11](https://www.baeldung.com/java-11-new-features) - [Getting the Java Version at Runtime](https://www.baeldung.com/get-java-version-runtime) +- [Invoking a SOAP Web Service in Java](https://www.baeldung.com/java-soap-web-service) From 09c91531d5f8e3c16fb856210808f1fd5d54aa2f Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 12:53:13 +0800 Subject: [PATCH 080/118] Update README.md --- kubernetes/k8s-intro/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/kubernetes/k8s-intro/README.md b/kubernetes/k8s-intro/README.md index 8c11f4d53e..bce7784aa1 100644 --- a/kubernetes/k8s-intro/README.md +++ b/kubernetes/k8s-intro/README.md @@ -18,3 +18,4 @@ If you get a valid response, then you're good to go. - [Using Watch with the Kubernetes API](https://www.baeldung.com/java-kubernetes-watch) - [Using Namespaces and Selectors With the Kubernetes Java API](https://www.baeldung.com/java-kubernetes-namespaces-selectors) - [Creating, Updating and Deleting Resources with the Java Kubernetes API](https://www.baeldung.com/java-kubernetes-api-crud) +- [A Quick Intro to the Kubernetes Java Client](https://www.baeldung.com/kubernetes-java-client) From 437643f9742c1b86a7ebc29ff1700aefe075438d Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 12:54:57 +0800 Subject: [PATCH 081/118] Update README.md --- spring-boot-modules/spring-boot-bootstrap/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-bootstrap/README.md b/spring-boot-modules/spring-boot-bootstrap/README.md index 146cd04551..02ec52f755 100644 --- a/spring-boot-modules/spring-boot-bootstrap/README.md +++ b/spring-boot-modules/spring-boot-bootstrap/README.md @@ -10,4 +10,4 @@ This module contains articles about bootstrapping Spring Boot applications. - [Deploy a Spring Boot Application to OpenShift](https://www.baeldung.com/spring-boot-deploy-openshift) - [Deploy a Spring Boot Application to AWS Beanstalk](https://www.baeldung.com/spring-boot-deploy-aws-beanstalk) - [Guide to @SpringBootConfiguration in Spring Boot](https://www.baeldung.com/springbootconfiguration-annotation) -- [Implement Health Checks in OpenShift](https://www.baeldung.com/openshift-health-checks) +- [Implement Health Checks in OpenShift](https://www.baeldung.com/ops/openshift-health-checks) From a82cc3b0be9dcc8fed49573b00189b4929db577a Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 12:56:21 +0800 Subject: [PATCH 082/118] Update README.md --- podman/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/podman/README.md b/podman/README.md index 3102036f04..24c5aa2653 100644 --- a/podman/README.md +++ b/podman/README.md @@ -1,3 +1,3 @@ ### Relevant Articles: -- [An Introduction to Podman](https://www.baeldung.com/podman-intro) +- [An Introduction to Podman](https://www.baeldung.com/ops/podman-intro) From 37b4d7a56905757ba81ccc37dc90df492f8a2729 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 13:23:31 +0800 Subject: [PATCH 083/118] Update README.md --- netflix-modules/genie/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netflix-modules/genie/README.md b/netflix-modules/genie/README.md index f6e15ba403..81b9d28dc4 100644 --- a/netflix-modules/genie/README.md +++ b/netflix-modules/genie/README.md @@ -1,3 +1,3 @@ ### Relevant Articles: -- [Introduction to Netflix Genie](https://www.baeldung.com/netflix-genie-intro) +- [Introduction to Netflix Genie](https://www.baeldung.com/ops/netflix-genie-intro) From e027dc115025d7f51dc99e8e4b7631aaaea67cfe Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 13:27:24 +0800 Subject: [PATCH 084/118] Update README.md --- testing-modules/gatling/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing-modules/gatling/README.md b/testing-modules/gatling/README.md index 7352479d1b..b99fafce15 100644 --- a/testing-modules/gatling/README.md +++ b/testing-modules/gatling/README.md @@ -1,6 +1,6 @@ ### Relevant Articles: - [Intro to Gatling](http://www.baeldung.com/introduction-to-gatling) -- [Run Gatling Tests From Jenkins](https://www.baeldung.com/jenkins-run-gatling-tests) +- [Run Gatling Tests From Jenkins](https://www.baeldung.com/ops/jenkins-run-gatling-tests) ### Running a simualtion - To run a simulation use "simulation" profile, command - `mvn install -Psimulation -Dgib.enabled=false` From 0c3bea1491c6c4d50117d436bcfd4d7027155e65 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 13:34:29 +0800 Subject: [PATCH 085/118] Update README.md --- docker/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/README.md b/docker/README.md index f1b4181e64..ab3ddd35b7 100644 --- a/docker/README.md +++ b/docker/README.md @@ -1,6 +1,6 @@ ## Relevant Articles: -- [Introduction to Docker Compose](https://www.baeldung.com/docker-compose) +- [Introduction to Docker Compose](https://www.baeldung.com/ops/docker-compose) - [Reusing Docker Layers with Spring Boot](https://www.baeldung.com/docker-layers-spring-boot) - [Running Spring Boot with PostgreSQL in Docker Compose](https://www.baeldung.com/spring-boot-postgresql-docker) - [How To Configure Java Heap Size Inside a Docker Container](https://www.baeldung.com/ops/docker-jvm-heap-size) From 94e70b0f36e39e81e577208e54121b4ae0fefacd Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 13:38:57 +0800 Subject: [PATCH 086/118] Update README.md --- spring-jenkins-pipeline/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-jenkins-pipeline/README.md b/spring-jenkins-pipeline/README.md index 9182823f52..d0eb726143 100644 --- a/spring-jenkins-pipeline/README.md +++ b/spring-jenkins-pipeline/README.md @@ -4,7 +4,7 @@ This module contains articles about Spring with Jenkins ### Relevant articles -- [Intro to Jenkins 2 and the Power of Pipelines](https://www.baeldung.com/jenkins-pipelines) +- [Intro to Jenkins 2 and the Power of Pipelines](https://www.baeldung.com/ops/jenkins-pipelines) ## Basic CRUD API with Spring Boot From 82dc81317315c0bacbc7c7693549744e1ff4ebd9 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 13:43:30 +0800 Subject: [PATCH 087/118] Update README.md --- jmeter/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jmeter/README.md b/jmeter/README.md index 53aca8d34b..6fdc79a2ce 100644 --- a/jmeter/README.md +++ b/jmeter/README.md @@ -51,5 +51,5 @@ Enjoy it :) ### Relevant Articles: - [Intro to Performance Testing using JMeter](https://www.baeldung.com/jmeter) -- [Configure Jenkins to Run and Show JMeter Tests](https://www.baeldung.com/jenkins-and-jmeter) +- [Configure Jenkins to Run and Show JMeter Tests](https://www.baeldung.com/ops/jenkins-and-jmeter) - [Write Extracted Data to a File Using JMeter](https://www.baeldung.com/jmeter-write-to-file) From 5b775d1f5d8a566f047875b2975fb258dcce8034 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 13:46:29 +0800 Subject: [PATCH 088/118] Update README.md --- mesos-marathon/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesos-marathon/README.md b/mesos-marathon/README.md index 8e5b8e4974..6fb3e7782b 100644 --- a/mesos-marathon/README.md +++ b/mesos-marathon/README.md @@ -4,6 +4,6 @@ This module contains articles about Marathon and Mesos. ### Relevant articles -- [Simple Jenkins Pipeline with Marathon and Mesos](https://www.baeldung.com/jenkins-pipeline-with-marathon-mesos) +- [Simple Jenkins Pipeline with Marathon and Mesos](https://www.baeldung.com/ops/jenkins-pipeline-with-marathon-mesos) To run the pipeline, please modify the dockerise.sh file with your own username and password for docker login. From efafe836c81874cf18dc6d451c94b0153c429d8d Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 14:09:33 +0800 Subject: [PATCH 089/118] Create README.md --- gradle/gradle-jacoco/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 gradle/gradle-jacoco/README.md diff --git a/gradle/gradle-jacoco/README.md b/gradle/gradle-jacoco/README.md new file mode 100644 index 0000000000..0da51cc539 --- /dev/null +++ b/gradle/gradle-jacoco/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Exclusions from Jacoco Report](https://www.baeldung.com/jacoco-report-exclude) From 23a5ad38767fde7e1a01425a57edb84f285082fc Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 14:53:08 +0800 Subject: [PATCH 090/118] Update README.md --- aws/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/README.md b/aws/README.md index fcb9735878..9006c2d190 100644 --- a/aws/README.md +++ b/aws/README.md @@ -6,7 +6,7 @@ This module contains articles about Amazon Web Services (AWS) - [AWS Lambda Using DynamoDB With Java](https://www.baeldung.com/aws-lambda-dynamodb-java) - [AWS S3 with Java](https://www.baeldung.com/aws-s3-java) -- [AWS Lambda With Java](https://www.baeldung.com/java-aws-lambda) +- [A Basic AWS Lambda Example With Java](https://www.baeldung.com/java-aws-lambda) - [Managing EC2 Instances in Java](https://www.baeldung.com/ec2-java) - [Multipart Uploads in Amazon S3 with Java](https://www.baeldung.com/aws-s3-multipart-upload) - [Integration Testing with a Local DynamoDB Instance](https://www.baeldung.com/dynamodb-local-integration-tests) From e2f9d8b6a50d35611af38483fe7c4ea2576d7a7f Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 15:05:35 +0800 Subject: [PATCH 091/118] Update README.md --- spring-boot-rest/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-rest/README.md b/spring-boot-rest/README.md index 861181c53e..365a21781c 100644 --- a/spring-boot-rest/README.md +++ b/spring-boot-rest/README.md @@ -10,7 +10,7 @@ This module contains articles about Spring Boot RESTful APIs. These articles are part of the Spring REST E-book: -1. [Bootstrap a Web Application with Spring 5](https://www.baeldung.com/bootstraping-a-web-application-with-spring-and-java-based-configuration) +1. [Creating a Web Application with Spring 5](https://www.baeldung.com/bootstraping-a-web-application-with-spring-and-java-based-configuration) 2. [Build a REST API with Spring and Java Config](https://www.baeldung.com/building-a-restful-web-service-with-spring-and-java-based-configuration) 3. [Http Message Converters with the Spring Framework](https://www.baeldung.com/spring-httpmessageconverter-rest) 4. [Spring’s RequestBody and ResponseBody Annotations](https://www.baeldung.com/spring-request-response-body) From 63d7cdbaf86363b75481e012440b1e724063e519 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 15:07:19 +0800 Subject: [PATCH 092/118] Update README.md --- core-java-modules/core-java-console/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-console/README.md b/core-java-modules/core-java-console/README.md index 0a31727588..9236e9cf99 100644 --- a/core-java-modules/core-java-console/README.md +++ b/core-java-modules/core-java-console/README.md @@ -3,6 +3,6 @@ ### Relevant Articles: - [Read and Write User Input in Java](http://www.baeldung.com/java-console-input-output) -- [Formatting with printf() in Java](https://www.baeldung.com/java-printstream-printf) +- [Formatting Output with printf() in Java](https://www.baeldung.com/java-printstream-printf) - [ASCII Art in Java](http://www.baeldung.com/ascii-art-in-java) - [System.console() vs. System.out](https://www.baeldung.com/java-system-console-vs-system-out) From 68ef34b2bff8c6e369ff4979ce46724b0fbcb634 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 15:11:04 +0800 Subject: [PATCH 093/118] Update README.md --- core-java-modules/core-java-collections-maps-3/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-collections-maps-3/README.md b/core-java-modules/core-java-collections-maps-3/README.md index 530a9310c2..68df2b9556 100644 --- a/core-java-modules/core-java-collections-maps-3/README.md +++ b/core-java-modules/core-java-collections-maps-3/README.md @@ -8,5 +8,5 @@ This module contains articles about Map data structures in Java. - [The Map.computeIfAbsent() Method](https://www.baeldung.com/java-map-computeifabsent) - [Collections.synchronizedMap vs. ConcurrentHashMap](https://www.baeldung.com/java-synchronizedmap-vs-concurrenthashmap) - [Java HashMap Load Factor](https://www.baeldung.com/java-hashmap-load-factor) -- [Converting java.util.Properties to HashMap](https://www.baeldung.com/java-convert-properties-to-hashmap) +- [Converting Java Properties to HashMap](https://www.baeldung.com/java-convert-properties-to-hashmap) - More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-2) From 1da22c097b68c03a52ffc5d82f3a5eec4bbe1a5f Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 15:14:53 +0800 Subject: [PATCH 094/118] Update README.md --- logging-modules/log4j/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logging-modules/log4j/README.md b/logging-modules/log4j/README.md index 371d0246ce..a47d0ae89b 100644 --- a/logging-modules/log4j/README.md +++ b/logging-modules/log4j/README.md @@ -3,4 +3,4 @@ - [Introduction to SLF4J](http://www.baeldung.com/slf4j-with-log4j2-logback) - [A Guide to Rolling File Appenders](http://www.baeldung.com/java-logging-rolling-file-appenders) - [Logging Exceptions Using SLF4J](https://www.baeldung.com/slf4j-log-exceptions) -- [Log4j Warning: "No Appenders Could Be Found for Logger"](https://www.baeldung.com/log4j-no-appenders-found) +- [Log4j Warning: “No Appenders Could Be Found for Logger”](https://www.baeldung.com/log4j-no-appenders-found) From 66e17f1c7e30a1d81f926a5ccc8f37e35a03a7ae Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 15:16:45 +0800 Subject: [PATCH 095/118] Update README.md --- apache-kafka/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apache-kafka/README.md b/apache-kafka/README.md index 3e817b2fa6..8a1d748482 100644 --- a/apache-kafka/README.md +++ b/apache-kafka/README.md @@ -3,7 +3,7 @@ This module contains articles about Apache Kafka. ### Relevant articles -- [Kafka Streams vs Kafka Consumer](https://www.baeldung.com/java-kafka-streams-vs-kafka-consumer) +- [Kafka Streams vs. Kafka Consumer](https://www.baeldung.com/java-kafka-streams-vs-kafka-consumer) - [Kafka Topic Creation Using Java](https://www.baeldung.com/kafka-topic-creation) - [Using Kafka MockConsumer](https://www.baeldung.com/kafka-mockconsumer) - [Using Kafka MockProducer](https://www.baeldung.com/kafka-mockproducer) From 46e43d23e0a38e0322ddfe915f53c58991c3000e Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 15:59:46 +0800 Subject: [PATCH 096/118] Update README.md --- spring-5/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-5/README.md b/spring-5/README.md index 2ddd9fa94f..952daa40d2 100644 --- a/spring-5/README.md +++ b/spring-5/README.md @@ -11,5 +11,4 @@ This module contains articles about Spring 5 - [Spring Assert Statements](https://www.baeldung.com/spring-assert) - [Difference between \ vs \](https://www.baeldung.com/spring-contextannotation-contextcomponentscan) - [Finding the Spring Version](https://www.baeldung.com/spring-find-version) -- [Spring 5 Testing with @EnabledIf Annotation](https://www.baeldung.com/spring-5-enabledIf) - [Configuring a Hikari Connection Pool with Spring Boot](https://www.baeldung.com/spring-boot-hikari) From 6dc9f39276a73845b8f9a8140bb9bbbf053d4b65 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 16:05:13 +0800 Subject: [PATCH 097/118] Update README.md --- spring-cloud/spring-cloud-config/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-cloud/spring-cloud-config/README.md b/spring-cloud/spring-cloud-config/README.md index b7c8c36e65..788889c8d6 100644 --- a/spring-cloud/spring-cloud-config/README.md +++ b/spring-cloud/spring-cloud-config/README.md @@ -1,3 +1,2 @@ ### Relevant Articles: - [Quick Intro to Spring Cloud Configuration](http://www.baeldung.com/spring-cloud-configuration) -- [Dockerizing a Spring Boot Application](http://www.baeldung.com/dockerizing-spring-boot-application) From 8b66e3ee289f986e7e11ec0bc62b305bcfca3837 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 24 Sep 2021 16:17:03 +0800 Subject: [PATCH 098/118] Update README.md --- patterns/enterprise-patterns/wire-tap/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/patterns/enterprise-patterns/wire-tap/README.md b/patterns/enterprise-patterns/wire-tap/README.md index 95cd1d585f..61d27f5c44 100644 --- a/patterns/enterprise-patterns/wire-tap/README.md +++ b/patterns/enterprise-patterns/wire-tap/README.md @@ -29,7 +29,5 @@ For more details, check out the AmqApplicationUnitTest.class. ### Relevant Articles: -- [Wire tap (Enterprise Integration Pattern)](https://drafts.baeldung.com/?p=103346&preview=true) -- [Intro to Apache camel](https://www.baeldung.com/apache-camel-intro) - [Wire Tap Enterprise Integration Pattern](https://www.baeldung.com/wiretap-pattern) From b0834a7c3f4f0a03f2efcf2dc51982e362c885e2 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Fri, 24 Sep 2021 16:49:31 +0300 Subject: [PATCH 099/118] BAEL-5065-v2 deleted files already moved to core-java-16 module --- core-java-modules/core-java-8/README.md | 1 - .../java_8_features/groupingby/BlogPost.java | 36 --- .../groupingby/BlogPostType.java | 5 - .../java_8_features/groupingby/Tuple.java | 41 ---- .../Java8GroupingByCollectorUnitTest.java | 215 ------------------ 5 files changed, 298 deletions(-) delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/Tuple.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/java_8_features/groupingby/Java8GroupingByCollectorUnitTest.java diff --git a/core-java-modules/core-java-8/README.md b/core-java-modules/core-java-8/README.md index 72bdafe5fa..ff4ceaf6db 100644 --- a/core-java-modules/core-java-8/README.md +++ b/core-java-modules/core-java-8/README.md @@ -4,7 +4,6 @@ This module contains articles about Java 8 core features ### Relevant Articles: - [New Features in Java 8](https://www.baeldung.com/java-8-new-features) -- [Guide to Java 8 groupingBy Collector](https://www.baeldung.com/java-groupingby-collector) - [Strategy Design Pattern in Java 8](https://www.baeldung.com/java-strategy-pattern) - [Guide to Java 8 Comparator.comparing()](https://www.baeldung.com/java-8-comparator-comparing) - [Guide to the Java 8 forEach](https://www.baeldung.com/foreach-java) diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java deleted file mode 100644 index afc05e356a..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.baeldung.java_8_features.groupingby; - -public class BlogPost { - private String title; - private String author; - private BlogPostType type; - private int likes; - - public BlogPost(String title, String author, BlogPostType type, int likes) { - this.title = title; - this.author = author; - this.type = type; - this.likes = likes; - } - - public String getTitle() { - return title; - } - - public String getAuthor() { - return author; - } - - public BlogPostType getType() { - return type; - } - - public int getLikes() { - return likes; - } - - @Override - public String toString() { - return "BlogPost{" + "title='" + title + '\'' + ", type=" + type + ", likes=" + likes + '}'; - } -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java deleted file mode 100644 index 2029784e91..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.baeldung.java_8_features.groupingby; - -public enum BlogPostType { - NEWS, REVIEW, GUIDE -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/Tuple.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/Tuple.java deleted file mode 100644 index 82a84bb2d6..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/Tuple.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.baeldung.java_8_features.groupingby; - -import java.util.Objects; - -public class Tuple { - private final BlogPostType type; - private final String author; - - public Tuple(BlogPostType type, String author) { - this.type = type; - this.author = author; - } - - public BlogPostType getType() { - return type; - } - - public String getAuthor() { - return author; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - Tuple tuple = (Tuple) o; - return type == tuple.type && author.equals(tuple.author); - } - - @Override - public int hashCode() { - return Objects.hash(type, author); - } - - @Override - public String toString() { - return "Tuple{" + "type=" + type + ", author='" + author + '\'' + '}'; - } -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/java_8_features/groupingby/Java8GroupingByCollectorUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/java_8_features/groupingby/Java8GroupingByCollectorUnitTest.java deleted file mode 100644 index 1da705294e..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/java_8_features/groupingby/Java8GroupingByCollectorUnitTest.java +++ /dev/null @@ -1,215 +0,0 @@ -package com.baeldung.java_8_features.groupingby; - -import static java.util.Comparator.comparingInt; -import static java.util.stream.Collectors.averagingInt; -import static java.util.stream.Collectors.counting; -import static java.util.stream.Collectors.groupingBy; -import static java.util.stream.Collectors.groupingByConcurrent; -import static java.util.stream.Collectors.joining; -import static java.util.stream.Collectors.mapping; -import static java.util.stream.Collectors.maxBy; -import static java.util.stream.Collectors.summarizingInt; -import static java.util.stream.Collectors.summingInt; -import static java.util.stream.Collectors.toList; -import static java.util.stream.Collectors.toSet; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.util.Arrays; -import java.util.EnumMap; -import java.util.IntSummaryStatistics; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.concurrent.ConcurrentMap; - -import org.junit.Test; - -public class Java8GroupingByCollectorUnitTest { - - private static final List posts = Arrays.asList(new BlogPost("News item 1", "Author 1", BlogPostType.NEWS, 15), new BlogPost("Tech review 1", "Author 2", BlogPostType.REVIEW, 5), - new BlogPost("Programming guide", "Author 1", BlogPostType.GUIDE, 20), new BlogPost("News item 2", "Author 2", BlogPostType.NEWS, 35), new BlogPost("Tech review 2", "Author 1", BlogPostType.REVIEW, 15)); - - @Test - public void givenAListOfPosts_whenGroupedByType_thenGetAMapBetweenTypeAndPosts() { - Map> postsPerType = posts.stream() - .collect(groupingBy(BlogPost::getType)); - - assertEquals(2, postsPerType.get(BlogPostType.NEWS) - .size()); - assertEquals(1, postsPerType.get(BlogPostType.GUIDE) - .size()); - assertEquals(2, postsPerType.get(BlogPostType.REVIEW) - .size()); - } - - @Test - public void givenAListOfPosts_whenGroupedByTypeAndTheirTitlesAreJoinedInAString_thenGetAMapBetweenTypeAndCsvTitles() { - Map postsPerType = posts.stream() - .collect(groupingBy(BlogPost::getType, mapping(BlogPost::getTitle, joining(", ", "Post titles: [", "]")))); - - assertEquals("Post titles: [News item 1, News item 2]", postsPerType.get(BlogPostType.NEWS)); - assertEquals("Post titles: [Programming guide]", postsPerType.get(BlogPostType.GUIDE)); - assertEquals("Post titles: [Tech review 1, Tech review 2]", postsPerType.get(BlogPostType.REVIEW)); - } - - @Test - public void givenAListOfPosts_whenGroupedByTypeAndSumTheLikes_thenGetAMapBetweenTypeAndPostLikes() { - Map likesPerType = posts.stream() - .collect(groupingBy(BlogPost::getType, summingInt(BlogPost::getLikes))); - - assertEquals(50, likesPerType.get(BlogPostType.NEWS) - .intValue()); - assertEquals(20, likesPerType.get(BlogPostType.REVIEW) - .intValue()); - assertEquals(20, likesPerType.get(BlogPostType.GUIDE) - .intValue()); - } - - @Test - public void givenAListOfPosts_whenGroupedByTypeInAnEnumMap_thenGetAnEnumMapBetweenTypeAndPosts() { - EnumMap> postsPerType = posts.stream() - .collect(groupingBy(BlogPost::getType, () -> new EnumMap<>(BlogPostType.class), toList())); - - assertEquals(2, postsPerType.get(BlogPostType.NEWS) - .size()); - assertEquals(1, postsPerType.get(BlogPostType.GUIDE) - .size()); - assertEquals(2, postsPerType.get(BlogPostType.REVIEW) - .size()); - } - - @Test - public void givenAListOfPosts_whenGroupedByTypeInSets_thenGetAMapBetweenTypesAndSetsOfPosts() { - Map> postsPerType = posts.stream() - .collect(groupingBy(BlogPost::getType, toSet())); - - assertEquals(2, postsPerType.get(BlogPostType.NEWS) - .size()); - assertEquals(1, postsPerType.get(BlogPostType.GUIDE) - .size()); - assertEquals(2, postsPerType.get(BlogPostType.REVIEW) - .size()); - } - - @Test - public void givenAListOfPosts_whenGroupedByTypeConcurrently_thenGetAMapBetweenTypeAndPosts() { - ConcurrentMap> postsPerType = posts.parallelStream() - .collect(groupingByConcurrent(BlogPost::getType)); - - assertEquals(2, postsPerType.get(BlogPostType.NEWS) - .size()); - assertEquals(1, postsPerType.get(BlogPostType.GUIDE) - .size()); - assertEquals(2, postsPerType.get(BlogPostType.REVIEW) - .size()); - } - - @Test - public void givenAListOfPosts_whenGroupedByTypeAndAveragingLikes_thenGetAMapBetweenTypeAndAverageNumberOfLikes() { - Map averageLikesPerType = posts.stream() - .collect(groupingBy(BlogPost::getType, averagingInt(BlogPost::getLikes))); - - assertEquals(25, averageLikesPerType.get(BlogPostType.NEWS) - .intValue()); - assertEquals(20, averageLikesPerType.get(BlogPostType.GUIDE) - .intValue()); - assertEquals(10, averageLikesPerType.get(BlogPostType.REVIEW) - .intValue()); - } - - @Test - public void givenAListOfPosts_whenGroupedByTypeAndCounted_thenGetAMapBetweenTypeAndNumberOfPosts() { - Map numberOfPostsPerType = posts.stream() - .collect(groupingBy(BlogPost::getType, counting())); - - assertEquals(2, numberOfPostsPerType.get(BlogPostType.NEWS) - .intValue()); - assertEquals(1, numberOfPostsPerType.get(BlogPostType.GUIDE) - .intValue()); - assertEquals(2, numberOfPostsPerType.get(BlogPostType.REVIEW) - .intValue()); - } - - @Test - public void givenAListOfPosts_whenGroupedByTypeAndMaxingLikes_thenGetAMapBetweenTypeAndMaximumNumberOfLikes() { - Map> maxLikesPerPostType = posts.stream() - .collect(groupingBy(BlogPost::getType, maxBy(comparingInt(BlogPost::getLikes)))); - - assertTrue(maxLikesPerPostType.get(BlogPostType.NEWS) - .isPresent()); - assertEquals(35, maxLikesPerPostType.get(BlogPostType.NEWS) - .get() - .getLikes()); - - assertTrue(maxLikesPerPostType.get(BlogPostType.GUIDE) - .isPresent()); - assertEquals(20, maxLikesPerPostType.get(BlogPostType.GUIDE) - .get() - .getLikes()); - - assertTrue(maxLikesPerPostType.get(BlogPostType.REVIEW) - .isPresent()); - assertEquals(15, maxLikesPerPostType.get(BlogPostType.REVIEW) - .get() - .getLikes()); - } - - @Test - public void givenAListOfPosts_whenGroupedByAuthorAndThenByType_thenGetAMapBetweenAuthorAndMapsBetweenTypeAndBlogPosts() { - Map>> map = posts.stream() - .collect(groupingBy(BlogPost::getAuthor, groupingBy(BlogPost::getType))); - - assertEquals(1, map.get("Author 1") - .get(BlogPostType.NEWS) - .size()); - assertEquals(1, map.get("Author 1") - .get(BlogPostType.GUIDE) - .size()); - assertEquals(1, map.get("Author 1") - .get(BlogPostType.REVIEW) - .size()); - - assertEquals(1, map.get("Author 2") - .get(BlogPostType.NEWS) - .size()); - assertEquals(1, map.get("Author 2") - .get(BlogPostType.REVIEW) - .size()); - assertNull(map.get("Author 2") - .get(BlogPostType.GUIDE)); - } - - @Test - public void givenAListOfPosts_whenGroupedByTypeAndSummarizingLikes_thenGetAMapBetweenTypeAndSummary() { - Map likeStatisticsPerType = posts.stream() - .collect(groupingBy(BlogPost::getType, summarizingInt(BlogPost::getLikes))); - - IntSummaryStatistics newsLikeStatistics = likeStatisticsPerType.get(BlogPostType.NEWS); - - assertEquals(2, newsLikeStatistics.getCount()); - assertEquals(50, newsLikeStatistics.getSum()); - assertEquals(25.0, newsLikeStatistics.getAverage(), 0.001); - assertEquals(35, newsLikeStatistics.getMax()); - assertEquals(15, newsLikeStatistics.getMin()); - } - - @Test - public void givenAListOfPosts_whenGroupedByComplexMapKeyType_thenGetAMapBetweenTupleAndList() { - Map> postsPerTypeAndAuthor = posts.stream() - .collect(groupingBy(post -> new Tuple(post.getType(), post.getAuthor()))); - - List result = postsPerTypeAndAuthor.get(new Tuple(BlogPostType.GUIDE, "Author 1")); - - assertThat(result.size()).isEqualTo(1); - - BlogPost blogPost = result.get(0); - - assertThat(blogPost.getTitle()).isEqualTo("Programming guide"); - assertThat(blogPost.getType()).isEqualTo(BlogPostType.GUIDE); - assertThat(blogPost.getAuthor()).isEqualTo("Author 1"); - } -} From c1fe739e37975d964186a45de94830628d8fad60 Mon Sep 17 00:00:00 2001 From: Steven van Beelen Date: Sat, 25 Sep 2021 15:13:54 +0200 Subject: [PATCH 100/118] [BAEL-4768] Snapshotting the Aggregate in Axon (#11103) * Add configuration class Add configuration class #BAEL-4768 * Add SnapshotTriggerDefinition bean creation method Add SnapshotTriggerDefinition bean creation method #BAEL-4768 * Add SnapshotTriggerDefinition bean name Add SnapshotTriggerDefinition bean name #BAEL-4768 * Add threshold behind property value Add threshold behind property value. Default to 250. #BAEL-4768 * Add link to the readme Add a link to the article in the readme #BAEL-4768 * Process review comments - Drop readme line - Adjust parameter indentation of config class #BAEL-4768 --- .../axon/OrderApplicationConfiguration.java | 18 ++++++++++++++++++ .../commandmodel/order/OrderAggregate.java | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 axon/src/main/java/com/baeldung/axon/OrderApplicationConfiguration.java diff --git a/axon/src/main/java/com/baeldung/axon/OrderApplicationConfiguration.java b/axon/src/main/java/com/baeldung/axon/OrderApplicationConfiguration.java new file mode 100644 index 0000000000..8b743144b4 --- /dev/null +++ b/axon/src/main/java/com/baeldung/axon/OrderApplicationConfiguration.java @@ -0,0 +1,18 @@ +package com.baeldung.axon; + +import org.axonframework.eventsourcing.EventCountSnapshotTriggerDefinition; +import org.axonframework.eventsourcing.SnapshotTriggerDefinition; +import org.axonframework.eventsourcing.Snapshotter; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class OrderApplicationConfiguration { + + @Bean + public SnapshotTriggerDefinition orderAggregateSnapshotTriggerDefinition(Snapshotter snapshotter, + @Value("${axon.aggregate.order.snapshot-threshold:250}") int threshold) { + return new EventCountSnapshotTriggerDefinition(snapshotter, threshold); + } +} diff --git a/axon/src/main/java/com/baeldung/axon/commandmodel/order/OrderAggregate.java b/axon/src/main/java/com/baeldung/axon/commandmodel/order/OrderAggregate.java index 97342bdb3a..1065e9d22b 100644 --- a/axon/src/main/java/com/baeldung/axon/commandmodel/order/OrderAggregate.java +++ b/axon/src/main/java/com/baeldung/axon/commandmodel/order/OrderAggregate.java @@ -23,7 +23,7 @@ import java.util.Map; import static org.axonframework.modelling.command.AggregateLifecycle.apply; -@Aggregate +@Aggregate(snapshotTriggerDefinition = "orderAggregateSnapshotTriggerDefinition") public class OrderAggregate { @AggregateIdentifier From 00026a8d5bccae1568967ce41a83a744b5b57ea4 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Sun, 26 Sep 2021 04:28:09 +0100 Subject: [PATCH 101/118] [JAVA-6005] Reduce logging (#11247) --- .../algorithms/prim/PrimUnitTest.java | 4 +- .../algorithms/suffixtree/SuffixTree.java | 10 +++-- .../algorithms/dfs/GraphUnitTest.java | 1 - .../quadtree/QuadTreeSearchUnitTest.java | 30 ++++++------- .../src/main/resources/log4j2.xml | 18 ++++++++ .../src/main/resources/log4j.properties | 42 +++++++++++++++++++ .../src/main/resources/logback.xml | 1 + core-java-modules/core-java-jar/pom.xml | 4 ++ jee-7/src/main/resources/logback.xml | 2 + .../src/main/resources/application.properties | 1 - .../BatchInsertIntegrationTest.java | 6 ++- .../application-batchinserts.properties | 4 +- .../resources/application-stats.properties | 5 +++ 13 files changed, 102 insertions(+), 26 deletions(-) create mode 100644 apache-libraries/src/main/resources/log4j2.xml create mode 100644 apache-spark/src/main/resources/log4j.properties create mode 100644 persistence-modules/spring-data-jpa-crud/src/test/resources/application-stats.properties diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/prim/PrimUnitTest.java b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/prim/PrimUnitTest.java index 41e53fc9f2..fac3b3a81f 100644 --- a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/prim/PrimUnitTest.java +++ b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/prim/PrimUnitTest.java @@ -1,10 +1,10 @@ package com.baeldung.algorithms.prim; +import org.junit.Test; + import java.util.ArrayList; import java.util.List; -import org.junit.Test; - public class PrimUnitTest { @Test diff --git a/algorithms-searching/src/main/java/com/baeldung/algorithms/suffixtree/SuffixTree.java b/algorithms-searching/src/main/java/com/baeldung/algorithms/suffixtree/SuffixTree.java index eb58c2bfab..b6b29ede78 100644 --- a/algorithms-searching/src/main/java/com/baeldung/algorithms/suffixtree/SuffixTree.java +++ b/algorithms-searching/src/main/java/com/baeldung/algorithms/suffixtree/SuffixTree.java @@ -8,7 +8,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class SuffixTree { + private static final Logger LOGGER = LoggerFactory.getLogger(SuffixTree.class); + private static final String WORD_TERMINATION = "$"; private static final int POSITION_UNDEFINED = -1; private Node root; @@ -23,7 +25,7 @@ public class SuffixTree { } public List searchText(String pattern) { - LOGGER.info("Searching for pattern \"{}\"", pattern); + LOGGER.debug("Searching for pattern \"{}\"", pattern); List result = new ArrayList<>(); List nodes = getAllNodesInTraversePath(pattern, root, false); @@ -41,11 +43,11 @@ public class SuffixTree { } private void addSuffix(String suffix, int position) { - LOGGER.info(">>>>>>>>>>>> Adding new suffix {}", suffix); + LOGGER.debug(">>>>>>>>>>>> Adding new suffix {}", suffix); List nodes = getAllNodesInTraversePath(suffix, root, true); if (nodes.size() == 0) { addChildNode(root, suffix, position); - LOGGER.info("{}", printTree()); + LOGGER.debug("{}", printTree()); } else { Node lastNode = nodes.remove(nodes.size() - 1); String newText = suffix; @@ -58,7 +60,7 @@ public class SuffixTree { newText = newText.substring(existingSuffixUptoLastNode.length()); } extendNode(lastNode, newText, position); - LOGGER.info("{}", printTree()); + LOGGER.debug("{}", printTree()); } } diff --git a/algorithms-searching/src/test/java/com/baeldung/algorithms/dfs/GraphUnitTest.java b/algorithms-searching/src/test/java/com/baeldung/algorithms/dfs/GraphUnitTest.java index 715bb55fcb..7af25a85ca 100644 --- a/algorithms-searching/src/test/java/com/baeldung/algorithms/dfs/GraphUnitTest.java +++ b/algorithms-searching/src/test/java/com/baeldung/algorithms/dfs/GraphUnitTest.java @@ -2,7 +2,6 @@ package com.baeldung.algorithms.dfs; import java.util.List; -import com.baeldung.algorithms.dfs.Graph; import org.junit.Test; public class GraphUnitTest { diff --git a/algorithms-searching/src/test/java/com/baeldung/algorithms/quadtree/QuadTreeSearchUnitTest.java b/algorithms-searching/src/test/java/com/baeldung/algorithms/quadtree/QuadTreeSearchUnitTest.java index 0b58ae9f14..faf06ced31 100644 --- a/algorithms-searching/src/test/java/com/baeldung/algorithms/quadtree/QuadTreeSearchUnitTest.java +++ b/algorithms-searching/src/test/java/com/baeldung/algorithms/quadtree/QuadTreeSearchUnitTest.java @@ -10,7 +10,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class QuadTreeSearchUnitTest { - + private static final Logger LOGGER = LoggerFactory.getLogger(QuadTreeSearchUnitTest.class); private static QuadTree quadTree; @@ -20,41 +20,41 @@ public class QuadTreeSearchUnitTest { Region area = new Region(0, 0, 400, 400); quadTree = new QuadTree(area); - float[][] points = new float[][] { { 21, 25 }, { 55, 53 }, { 70, 318 }, { 98, 302 }, + float[][] points = new float[][] { { 21, 25 }, { 55, 53 }, { 70, 318 }, { 98, 302 }, { 49, 229 }, { 135, 229 }, { 224, 292 }, { 206, 321 }, { 197, 258 }, { 245, 238 } }; for (int i = 0; i < points.length; i++) { Point point = new Point(points[i][0], points[i][1]); quadTree.addPoint(point); } - LOGGER.info("\n" + quadTree.printTree("")); - LOGGER.info("=============================================="); + LOGGER.debug("\n" + quadTree.printTree("")); + LOGGER.debug("=============================================="); } @Test public void givenQuadTree_whenSearchingForRange_thenReturn1MatchingItem() { Region searchArea = new Region(200, 200, 250, 250); List result = quadTree.search(searchArea, null, ""); - LOGGER.info(result.toString()); - LOGGER.info(quadTree.printSearchTraversePath()); - + LOGGER.debug(result.toString()); + LOGGER.debug(quadTree.printSearchTraversePath()); + Assert.assertEquals(1, result.size()); - Assert.assertArrayEquals(new float[] { 245, 238 }, + Assert.assertArrayEquals(new float[] { 245, 238 }, new float[]{result.get(0).getX(), result.get(0).getY() }, 0); } - + @Test public void givenQuadTree_whenSearchingForRange_thenReturn2MatchingItems() { Region searchArea = new Region(0, 0, 100, 100); List result = quadTree.search(searchArea, null, ""); - LOGGER.info(result.toString()); - LOGGER.info(quadTree.printSearchTraversePath()); - + LOGGER.debug(result.toString()); + LOGGER.debug(quadTree.printSearchTraversePath()); + Assert.assertEquals(2, result.size()); - Assert.assertArrayEquals(new float[] { 21, 25 }, + Assert.assertArrayEquals(new float[] { 21, 25 }, new float[]{result.get(0).getX(), result.get(0).getY() }, 0); - Assert.assertArrayEquals(new float[] { 55, 53 }, + Assert.assertArrayEquals(new float[] { 55, 53 }, new float[]{result.get(1).getX(), result.get(1).getY() }, 0); - + } } diff --git a/apache-libraries/src/main/resources/log4j2.xml b/apache-libraries/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..d1ea5173fa --- /dev/null +++ b/apache-libraries/src/main/resources/log4j2.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/apache-spark/src/main/resources/log4j.properties b/apache-spark/src/main/resources/log4j.properties new file mode 100644 index 0000000000..07a2943655 --- /dev/null +++ b/apache-spark/src/main/resources/log4j.properties @@ -0,0 +1,42 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# Set everything to be logged to the console +log4j.rootCategory=WARN, console +log4j.appender.console=org.apache.log4j.ConsoleAppender +log4j.appender.console.target=System.err +log4j.appender.console.layout=org.apache.log4j.PatternLayout +log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n + +# Set the default spark-shell log level to WARN. When running the spark-shell, the +# log level for this class is used to overwrite the root logger's log level, so that +# the user can have different defaults for the shell and regular Spark apps. +log4j.logger.org.apache.spark.repl.Main=WARN + +# Settings to quiet third party logs that are too verbose +log4j.logger.org.spark_project.jetty=WARN +log4j.logger.org.spark_project.jetty.util.component.AbstractLifeCycle=ERROR +log4j.logger.org.apache.spark.repl.SparkIMain$exprTyper=INFO +log4j.logger.org.apache.spark.repl.SparkILoop$SparkILoopInterpreter=INFO + +# SPARK-9183: Settings to avoid annoying messages when looking up nonexistent UDFs in SparkSQL with Hive support +log4j.logger.org.apache.hadoop.hive.metastore.RetryingHMSHandler=FATAL +log4j.logger.org.apache.hadoop.hive.ql.exec.FunctionRegistry=ERROR + +# Parquet related logging +log4j.logger.org.apache.parquet.CorruptStatistics=ERROR +log4j.logger.parquet.CorruptStatistics=ERROR diff --git a/core-java-modules/core-java-io/src/main/resources/logback.xml b/core-java-modules/core-java-io/src/main/resources/logback.xml index 56af2d397e..617917dca2 100644 --- a/core-java-modules/core-java-io/src/main/resources/logback.xml +++ b/core-java-modules/core-java-io/src/main/resources/logback.xml @@ -9,6 +9,7 @@ + diff --git a/core-java-modules/core-java-jar/pom.xml b/core-java-modules/core-java-jar/pom.xml index b638f81b0c..ae2333d721 100644 --- a/core-java-modules/core-java-jar/pom.xml +++ b/core-java-modules/core-java-jar/pom.xml @@ -76,6 +76,7 @@ org.apache.maven.plugins maven-dependency-plugin + ${maven-dependency-plugin.version} copy-dependencies @@ -106,6 +107,7 @@ org.apache.maven.plugins maven-assembly-plugin + ${maven-assembly-plugin.version} package @@ -383,6 +385,8 @@ 3.0.2 1.4.4 3.1.1 + 3.3.0 + 3.2.0 2.0.3.RELEASE 1.8 1.8 diff --git a/jee-7/src/main/resources/logback.xml b/jee-7/src/main/resources/logback.xml index 7d900d8ea8..5c1b7ec771 100644 --- a/jee-7/src/main/resources/logback.xml +++ b/jee-7/src/main/resources/logback.xml @@ -7,6 +7,8 @@ + + diff --git a/persistence-modules/spring-data-jpa-crud/src/main/resources/application.properties b/persistence-modules/spring-data-jpa-crud/src/main/resources/application.properties index 3829f676d3..d7fb13da08 100644 --- a/persistence-modules/spring-data-jpa-crud/src/main/resources/application.properties +++ b/persistence-modules/spring-data-jpa-crud/src/main/resources/application.properties @@ -1,7 +1,6 @@ spring.jpa.properties.hibernate.jdbc.batch_size=4 spring.jpa.properties.hibernate.order_inserts=true spring.jpa.properties.hibernate.order_updates=true -spring.jpa.properties.hibernate.generate_statistics=true spring.jpa.database-platform=org.hibernate.dialect.H2Dialect diff --git a/persistence-modules/spring-data-jpa-crud/src/test/java/com/baeldung/batchinserts/BatchInsertIntegrationTest.java b/persistence-modules/spring-data-jpa-crud/src/test/java/com/baeldung/batchinserts/BatchInsertIntegrationTest.java index 7ddf36d3f0..f91c966e94 100644 --- a/persistence-modules/spring-data-jpa-crud/src/test/java/com/baeldung/batchinserts/BatchInsertIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-crud/src/test/java/com/baeldung/batchinserts/BatchInsertIntegrationTest.java @@ -9,6 +9,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -20,6 +21,7 @@ import com.baeldung.boot.web.controllers.CustomerController; @RunWith(SpringRunner.class) @SpringBootTest(classes=Application.class) @AutoConfigureMockMvc +@ActiveProfiles("stats") public class BatchInsertIntegrationTest { @Autowired @@ -35,6 +37,6 @@ public class BatchInsertIntegrationTest { public void whenInsertingCustomers_thenCustomersAreCreated() throws Exception { this.mockMvc.perform(post("/customers")) .andExpect(status().isOk()); - } - + } + } \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-crud/src/test/resources/application-batchinserts.properties b/persistence-modules/spring-data-jpa-crud/src/test/resources/application-batchinserts.properties index 4141f5668e..bc9c9a832c 100644 --- a/persistence-modules/spring-data-jpa-crud/src/test/resources/application-batchinserts.properties +++ b/persistence-modules/spring-data-jpa-crud/src/test/resources/application-batchinserts.properties @@ -3,4 +3,6 @@ spring.jpa.show-sql=false spring.jpa.properties.hibernate.jdbc.batch_size=5 spring.jpa.properties.hibernate.order_inserts=true spring.jpa.properties.hibernate.order_updates=true -spring.jpa.properties.hibernate.batch_versioned_data=true \ No newline at end of file +spring.jpa.properties.hibernate.batch_versioned_data=true + +spring.jpa.properties.hibernate.generate_statistics=true \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-crud/src/test/resources/application-stats.properties b/persistence-modules/spring-data-jpa-crud/src/test/resources/application-stats.properties new file mode 100644 index 0000000000..97bc170ca0 --- /dev/null +++ b/persistence-modules/spring-data-jpa-crud/src/test/resources/application-stats.properties @@ -0,0 +1,5 @@ +spring.jpa.properties.hibernate.jdbc.batch_size=4 +spring.jpa.properties.hibernate.order_inserts=true +spring.jpa.properties.hibernate.order_updates=true + +spring.jpa.properties.hibernate.generate_statistics=true \ No newline at end of file From 82b38e998fdd67ef880edc8d02162d89253002a0 Mon Sep 17 00:00:00 2001 From: rvsathe <38076470+rvsathe@users.noreply.github.com> Date: Sun, 26 Sep 2021 22:00:14 +0530 Subject: [PATCH 102/118] BAEL 4293 correcting the unit test file ame. (#11258) --- .../example/soundapi/{AppTest.java => AppUnitTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/{AppTest.java => AppUnitTest.java} (98%) diff --git a/core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppTest.java b/core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppUnitTest.java similarity index 98% rename from core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppTest.java rename to core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppUnitTest.java index 77012fdf6e..38f21320e3 100644 --- a/core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppTest.java +++ b/core-java-modules/core-java-os/src/test/java/com/baeldung/example/soundapi/AppUnitTest.java @@ -11,7 +11,7 @@ import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.TargetDataLine; -public class AppTest { +public class AppUnitTest { AudioFormat af = App.buildAudioFormatInstance(); SoundRecorder soundRecorder = new SoundRecorder(); From 924fb8bbe292b9d8733511d1cc365d88443cb47f Mon Sep 17 00:00:00 2001 From: Rafael Lopez Date: Sun, 26 Sep 2021 12:57:40 -0400 Subject: [PATCH 103/118] JAVA-2801: Upgrade spring-data-cassandra-reactive to the latest Spring Boot version --- persistence-modules/spring-data-cassandra-reactive/pom.xml | 7 +++++-- .../src/main/resources/application.properties | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/persistence-modules/spring-data-cassandra-reactive/pom.xml b/persistence-modules/spring-data-cassandra-reactive/pom.xml index 5dd5ab4b69..cddb62186b 100644 --- a/persistence-modules/spring-data-cassandra-reactive/pom.xml +++ b/persistence-modules/spring-data-cassandra-reactive/pom.xml @@ -28,6 +28,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.projectlombok lombok @@ -52,8 +56,7 @@ - 2.2.6.RELEASE - 3.11.2.0 + 4.3.1.0 \ No newline at end of file diff --git a/persistence-modules/spring-data-cassandra-reactive/src/main/resources/application.properties b/persistence-modules/spring-data-cassandra-reactive/src/main/resources/application.properties index 7ed2f10131..5acbcdd5a3 100644 --- a/persistence-modules/spring-data-cassandra-reactive/src/main/resources/application.properties +++ b/persistence-modules/spring-data-cassandra-reactive/src/main/resources/application.properties @@ -1,2 +1,3 @@ spring.data.cassandra.keyspace-name=practice -spring.data.cassandra.port=9042 \ No newline at end of file +spring.data.cassandra.port=9042 +spring.data.cassandra.local-datacenter=datacenter1 \ No newline at end of file From 0e9d949beb715a15da86a4ab806f0c3cc0b3980e Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Mon, 27 Sep 2021 23:14:47 +0530 Subject: [PATCH 104/118] JAVA-6454 Changes done as per Spring Boot 2.5 release - Added spring.jpa.defer-datasource-initialization so that data creation can be enabled using data.sql files --- .../src/main/resources/persistence-generic-entity.properties | 2 ++ 1 file changed, 2 insertions(+) diff --git a/persistence-modules/spring-boot-persistence/src/main/resources/persistence-generic-entity.properties b/persistence-modules/spring-boot-persistence/src/main/resources/persistence-generic-entity.properties index b19304cb1f..3e018e9321 100644 --- a/persistence-modules/spring-boot-persistence/src/main/resources/persistence-generic-entity.properties +++ b/persistence-modules/spring-boot-persistence/src/main/resources/persistence-generic-entity.properties @@ -6,3 +6,5 @@ jdbc.pass=sa hibernate.dialect=org.hibernate.dialect.H2Dialect hibernate.show_sql=true hibernate.hbm2ddl.auto=create-drop + +spring.jpa.defer-datasource-initialization=true \ No newline at end of file From 7b47645746ff950f530087e5c776298501712191 Mon Sep 17 00:00:00 2001 From: Ralf Ueberfuhr <40685729+ueberfuhr@users.noreply.github.com> Date: Tue, 28 Sep 2021 05:58:14 +0200 Subject: [PATCH 105/118] BAEL-5167: add sample key implementations and test the correct functionality and the performance of those keys in a HashMap (#11260) --- .../core-java-collections-maps-4/README.md | 6 ++ .../core-java-collections-maps-4/pom.xml | 34 ++++++++ .../java/com/baeldung/maps/CoordinateKey.java | 39 +++++++++ .../baeldung/maps/CoordinateMutableKey.java | 45 +++++++++++ .../com/baeldung/maps/CoordinateSlowKey.java | 13 +++ .../baeldung/maps/CoordinateKeyUnitTest.java | 80 +++++++++++++++++++ .../maps/CoordinateMutableKeyUnitTest.java | 27 +++++++ core-java-modules/pom.xml | 3 +- 8 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-collections-maps-4/README.md create mode 100644 core-java-modules/core-java-collections-maps-4/pom.xml create mode 100644 core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/maps/CoordinateKey.java create mode 100644 core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/maps/CoordinateMutableKey.java create mode 100644 core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/maps/CoordinateSlowKey.java create mode 100644 core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/maps/CoordinateKeyUnitTest.java create mode 100644 core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/maps/CoordinateMutableKeyUnitTest.java diff --git a/core-java-modules/core-java-collections-maps-4/README.md b/core-java-modules/core-java-collections-maps-4/README.md new file mode 100644 index 0000000000..4449d3859b --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/README.md @@ -0,0 +1,6 @@ +## Java Collections Cookbooks and Examples + +This module contains articles about Map data structures in Java. + +### Relevant Articles: +- [Using a Custom Class as a Key in a Java HashMap](https://www.baeldung.com/custom-key-hashmap) diff --git a/core-java-modules/core-java-collections-maps-4/pom.xml b/core-java-modules/core-java-collections-maps-4/pom.xml new file mode 100644 index 0000000000..1835e3ceac --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + core-java-collections-maps-4 + 0.1.0-SNAPSHOT + core-java-collections-maps-4 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + ../pom.xml + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M5 + + + !performance + + + + + + + diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/maps/CoordinateKey.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/maps/CoordinateKey.java new file mode 100644 index 0000000000..34ae3d7952 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/maps/CoordinateKey.java @@ -0,0 +1,39 @@ +package com.baeldung.maps; + +import java.util.Objects; + +public class CoordinateKey { + + private final int x; + private final int y; + private final int hashCode; + + public CoordinateKey(int x, int y) { + this.x = x; + this.y = y; + this.hashCode = Objects.hash(x, y); + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + CoordinateKey that = (CoordinateKey) o; + return x == that.x && y == that.y; + } + + @Override + public int hashCode() { + return this.hashCode; + } +} diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/maps/CoordinateMutableKey.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/maps/CoordinateMutableKey.java new file mode 100644 index 0000000000..08b329de3d --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/maps/CoordinateMutableKey.java @@ -0,0 +1,45 @@ +package com.baeldung.maps; + +import java.util.Objects; + +public class CoordinateMutableKey { + + private int x; + private int y; + + public CoordinateMutableKey(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + CoordinateMutableKey that = (CoordinateMutableKey) o; + return x == that.x && y == that.y; + } + + @Override + public int hashCode() { + return Objects.hash(x, y); + } +} diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/maps/CoordinateSlowKey.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/maps/CoordinateSlowKey.java new file mode 100644 index 0000000000..9fe54bd9ea --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/maps/CoordinateSlowKey.java @@ -0,0 +1,13 @@ +package com.baeldung.maps; + +public class CoordinateSlowKey extends CoordinateKey { + + public CoordinateSlowKey(int x, int y) { + super(x, y); + } + + @Override + public int hashCode() { + return 1; + } +} diff --git a/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/maps/CoordinateKeyUnitTest.java b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/maps/CoordinateKeyUnitTest.java new file mode 100644 index 0000000000..8ee68c4a3d --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/maps/CoordinateKeyUnitTest.java @@ -0,0 +1,80 @@ +package com.baeldung.maps; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import java.awt.*; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class CoordinateKeyUnitTest { + + private Map pixels = new HashMap<>(); + + @Test + void testOptimalKey() { + // setup + CoordinateKey coord = new CoordinateKey(1, 2); + pixels.put(coord, Color.CYAN); + // read out color correctly + assertEquals(Color.CYAN, pixels.get(coord)); + } + + @Test + void testSlowKey() { + // setup + CoordinateKey coord = new CoordinateSlowKey(1, 2); + pixels.put(coord, Color.CYAN); + // read out color correctly + assertEquals(Color.CYAN, pixels.get(coord)); + } + + // Performance Test Parameters - change here + private static final int MAX_X = 100; + private static final int MAX_Y = 100; + private static final int COUNT_OF_QUERIES = 1000; + private static final int QUERY_X = 1; + private static final int QUERY_Y = 1; + + @Tag("performance") + @Test + void testKeyPerformance() { + // generate some sample keys and values + for (int x = 0; x < MAX_X; x++) { + for (int y = 0; y < MAX_Y; y++) { + pixels.put(new CoordinateKey(x, y), new Color(x % 255, y % 255, (x + y) % 255)); + } + } + // read out multiple times and measure time + CoordinateKey coord = new CoordinateKey(QUERY_X, QUERY_Y); + long t1 = System.currentTimeMillis(); + for (int i = 0; i < COUNT_OF_QUERIES; i++) { + assertNotNull(pixels.get(coord)); + } + long t2 = System.currentTimeMillis(); + System.out.printf("Optimal key performance: %d ms%n", t2 - t1); + } + + @Tag("performance") + @Test + void testSlowKeyPerformance() { + // generate some sample keys and values + for (int x = 0; x < MAX_X; x++) { + for (int y = 0; y < MAX_Y; y++) { + pixels.put(new CoordinateSlowKey(x, y), new Color(x % 255, y % 255, (x + y) % 255)); + } + } + // read out multiple times and measure time + CoordinateKey coord = new CoordinateSlowKey(QUERY_X, QUERY_Y); + long t1 = System.currentTimeMillis(); + for (int i = 0; i < COUNT_OF_QUERIES; i++) { + assertNotNull(pixels.get(coord)); + } + long t2 = System.currentTimeMillis(); + System.out.printf("Slow key performance: %d ms%n", t2 - t1); + } + +} diff --git a/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/maps/CoordinateMutableKeyUnitTest.java b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/maps/CoordinateMutableKeyUnitTest.java new file mode 100644 index 0000000000..54a61be230 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/maps/CoordinateMutableKeyUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.maps; + +import org.junit.jupiter.api.Test; + +import java.awt.*; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class CoordinateMutableKeyUnitTest { + + @Test + void testKeyMutable() { + // setup + Map pixels = new HashMap<>(); + CoordinateMutableKey coord = new CoordinateMutableKey(1, 2); + pixels.put(coord, Color.CYAN); + // read out color correctly + assertEquals(Color.CYAN, pixels.get(coord)); + // change key's hashcode should result in null value + coord.setX(10); + assertNull(pixels.get(coord)); + } + +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index b272d2aa13..5291c8c3ca 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -38,6 +38,7 @@ core-java-collections-maps core-java-collections-maps-2 core-java-collections-maps-3 + core-java-collections-maps-4 core-java-concurrency-2 core-java-concurrency-advanced core-java-concurrency-advanced-2 @@ -138,4 +139,4 @@ 5.6.2 - \ No newline at end of file + From 8235298f5bcfe333a334cd6d0eeae52950c4f497 Mon Sep 17 00:00:00 2001 From: lucaCambi77 Date: Wed, 29 Sep 2021 05:49:28 +0200 Subject: [PATCH 106/118] [ BAEL-4339 ] - How to get Junit 4 to ignore a Base Test Class? #11203 (#11204) * feat: add ignored tests and update run from java * Revert "feat: add ignored tests and update run from java" This reverts commit 0a56fa4bf9e29473d04091408c5ce381f2f55f42. * add ignore package * fix: remove abstract from rename and ignore * fix: removes run with and add missing tests --- .../test/java/com/baeldung/ignore/BaseUnitTest.java | 8 ++++++++ .../com/baeldung/ignore/BaseUnitTestHelper.java | 8 ++++++++ .../com/baeldung/ignore/ExtendedBaseUnitTest.java | 11 +++++++++++ .../com/baeldung/ignore/IgnoreClassUnitTest.java | 13 +++++++++++++ .../com/baeldung/ignore/IgnoreMethodUnitTest.java | 13 +++++++++++++ 5 files changed, 53 insertions(+) create mode 100644 testing-modules/junit-4/src/test/java/com/baeldung/ignore/BaseUnitTest.java create mode 100644 testing-modules/junit-4/src/test/java/com/baeldung/ignore/BaseUnitTestHelper.java create mode 100644 testing-modules/junit-4/src/test/java/com/baeldung/ignore/ExtendedBaseUnitTest.java create mode 100644 testing-modules/junit-4/src/test/java/com/baeldung/ignore/IgnoreClassUnitTest.java create mode 100644 testing-modules/junit-4/src/test/java/com/baeldung/ignore/IgnoreMethodUnitTest.java diff --git a/testing-modules/junit-4/src/test/java/com/baeldung/ignore/BaseUnitTest.java b/testing-modules/junit-4/src/test/java/com/baeldung/ignore/BaseUnitTest.java new file mode 100644 index 0000000000..0138184cc3 --- /dev/null +++ b/testing-modules/junit-4/src/test/java/com/baeldung/ignore/BaseUnitTest.java @@ -0,0 +1,8 @@ +package com.baeldung.ignore; + +public abstract class BaseUnitTest { + + public void helperMethod() { + + } +} diff --git a/testing-modules/junit-4/src/test/java/com/baeldung/ignore/BaseUnitTestHelper.java b/testing-modules/junit-4/src/test/java/com/baeldung/ignore/BaseUnitTestHelper.java new file mode 100644 index 0000000000..73ad19ed13 --- /dev/null +++ b/testing-modules/junit-4/src/test/java/com/baeldung/ignore/BaseUnitTestHelper.java @@ -0,0 +1,8 @@ +package com.baeldung.ignore; + +public class BaseUnitTestHelper { + + public void helperMethod() { + + } +} diff --git a/testing-modules/junit-4/src/test/java/com/baeldung/ignore/ExtendedBaseUnitTest.java b/testing-modules/junit-4/src/test/java/com/baeldung/ignore/ExtendedBaseUnitTest.java new file mode 100644 index 0000000000..3b88ff6ff9 --- /dev/null +++ b/testing-modules/junit-4/src/test/java/com/baeldung/ignore/ExtendedBaseUnitTest.java @@ -0,0 +1,11 @@ +package com.baeldung.ignore; + +import org.junit.Test; + +public class ExtendedBaseUnitTest extends BaseUnitTest { + + @Test + public void whenDoTest_thenAssert() { + + } +} diff --git a/testing-modules/junit-4/src/test/java/com/baeldung/ignore/IgnoreClassUnitTest.java b/testing-modules/junit-4/src/test/java/com/baeldung/ignore/IgnoreClassUnitTest.java new file mode 100644 index 0000000000..43ada14e8f --- /dev/null +++ b/testing-modules/junit-4/src/test/java/com/baeldung/ignore/IgnoreClassUnitTest.java @@ -0,0 +1,13 @@ +package com.baeldung.ignore; + +import org.junit.Ignore; +import org.junit.Test; + +@Ignore("Class not ready for tests") +public class IgnoreClassUnitTest { + + @Test + public void whenDoTest_thenAssert() { + + } +} diff --git a/testing-modules/junit-4/src/test/java/com/baeldung/ignore/IgnoreMethodUnitTest.java b/testing-modules/junit-4/src/test/java/com/baeldung/ignore/IgnoreMethodUnitTest.java new file mode 100644 index 0000000000..c49e95eed9 --- /dev/null +++ b/testing-modules/junit-4/src/test/java/com/baeldung/ignore/IgnoreMethodUnitTest.java @@ -0,0 +1,13 @@ +package com.baeldung.ignore; + +import org.junit.Ignore; +import org.junit.Test; + +public class IgnoreMethodUnitTest { + + @Ignore("This test method not ready yet") + @Test + public void whenMethodIsIgnored_thenTestsDoNotRun() { + + } +} From 7b36d4cd43d1ac4c6e73f685a080a99d8a726f74 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 29 Sep 2021 23:29:55 +0800 Subject: [PATCH 107/118] Update README.md --- grpc/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/grpc/README.md b/grpc/README.md index d65dc03e54..e162681810 100644 --- a/grpc/README.md +++ b/grpc/README.md @@ -3,4 +3,6 @@ This module contains articles about gRPC ### Relevant Articles: + - [Introduction to gRPC](https://www.baeldung.com/grpc-introduction) +- [Streaming with gRPC in Java](https://www.baeldung.com/java-grpc-streaming) From 508c2b61108d7af8cddd31b6cfa115b991ebedc5 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 29 Sep 2021 23:32:13 +0800 Subject: [PATCH 108/118] Update README.md --- axon/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/axon/README.md b/axon/README.md index 3b2fd0b5ff..18f5d568e6 100644 --- a/axon/README.md +++ b/axon/README.md @@ -6,3 +6,4 @@ This module contains articles about Axon - [A Guide to the Axon Framework](https://www.baeldung.com/axon-cqrs-event-sourcing) - [Multi-Entity Aggregates in Axon](https://www.baeldung.com/java-axon-multi-entity-aggregates) +- [Snapshotting Aggregates in Axon](https://www.baeldung.com/axon-snapshotting-aggregates) From 98a5c931b037d3f0338dc7d3f4ac80cee5b12dfe Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 29 Sep 2021 23:38:32 +0800 Subject: [PATCH 109/118] Update README.md --- core-java-modules/core-java-string-algorithms-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-string-algorithms-3/README.md b/core-java-modules/core-java-string-algorithms-3/README.md index c7b17a6720..8d515b9aea 100644 --- a/core-java-modules/core-java-string-algorithms-3/README.md +++ b/core-java-modules/core-java-string-algorithms-3/README.md @@ -5,3 +5,4 @@ This module contains articles about string-related algorithms. ### Relevant Articles: - [Check if Two Strings are Anagrams in Java](https://www.baeldung.com/java-strings-anagrams) +- [Email Validation in Java](https://www.baeldung.com/java-email-validation-regex) From 214c1c385f36d30cef8632749fa95ffad1f67d25 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 29 Sep 2021 23:40:27 +0800 Subject: [PATCH 110/118] 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 cf20c8da91..cb5def7144 100644 --- a/testing-modules/junit-4/README.md +++ b/testing-modules/junit-4/README.md @@ -6,3 +6,4 @@ - [Running JUnit Tests Programmatically, from a Java Application](https://www.baeldung.com/junit-tests-run-programmatically-from-java) - [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) From be966d6d62fb5b605e74b8bcf99e8b8c94e0c359 Mon Sep 17 00:00:00 2001 From: Bhaskara Date: Thu, 30 Sep 2021 03:19:24 +0530 Subject: [PATCH 111/118] BAEL-1918 Spring StrictHttpFirewall and RequestRejectedException (#11265) * BAEL-1918: Added code to for RequestRejectedException and StrictHttpFirewall tutorial * BAEL-1918: Added code to for RequestRejectedException and StrictHttpFirewall tutorial * BAEL:1918 - Modifed the code to accomodate comments from Michal * BAEL:1918 - Modifed the code to accomodate comments from Michal --- .../spring-security-web-boot-3/pom.xml | 18 +- .../httpfirewall/HttpFirewallApplication.java | 15 ++ .../HttpFirewallConfiguration.java | 48 ++++++ .../baeldung/httpfirewall/api/UserApi.java | 59 +++++++ .../httpfirewall/dao/InMemoryUserDao.java | 65 +++++++ .../baeldung/httpfirewall/model/Response.java | 40 +++++ .../com/baeldung/httpfirewall/model/User.java | 41 +++++ .../httpfirewall/service/UserServiceImpl.java | 51 ++++++ .../application-httpfirewall.properties | 2 + .../httpfirewall/api/UserApiLiveTest.java | 114 +++++++++++++ .../httpfirewall/api/UserApiUnitTest.java | 161 ++++++++++++++++++ .../service/UserServiceUnitTest.java | 92 ++++++++++ .../httpfirewall/utility/UserTestUtility.java | 33 ++++ 13 files changed, 737 insertions(+), 2 deletions(-) create mode 100644 spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/HttpFirewallApplication.java create mode 100644 spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/HttpFirewallConfiguration.java create mode 100644 spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/api/UserApi.java create mode 100644 spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/dao/InMemoryUserDao.java create mode 100644 spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/model/Response.java create mode 100644 spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/model/User.java create mode 100644 spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/service/UserServiceImpl.java create mode 100644 spring-security-modules/spring-security-web-boot-3/src/main/resources/application-httpfirewall.properties create mode 100644 spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/api/UserApiLiveTest.java create mode 100644 spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/api/UserApiUnitTest.java create mode 100644 spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/service/UserServiceUnitTest.java create mode 100644 spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/utility/UserTestUtility.java diff --git a/spring-security-modules/spring-security-web-boot-3/pom.xml b/spring-security-modules/spring-security-web-boot-3/pom.xml index 1fff259c16..9d09d60611 100644 --- a/spring-security-modules/spring-security-web-boot-3/pom.xml +++ b/spring-security-modules/spring-security-web-boot-3/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-security-web-boot-3 0.0.1-SNAPSHOT @@ -25,6 +25,20 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-test + test + + + org.apache.commons + commons-lang3 + + + org.springframework.security + spring-security-test + test + \ No newline at end of file diff --git a/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/HttpFirewallApplication.java b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/HttpFirewallApplication.java new file mode 100644 index 0000000000..e7d6d823a6 --- /dev/null +++ b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/HttpFirewallApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.httpfirewall; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class HttpFirewallApplication { + + public static void main(String[] args) { + SpringApplication application = new SpringApplication(HttpFirewallApplication.class); + application.setAdditionalProfiles("httpfirewall"); + application.run(args); + } + +} diff --git a/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/HttpFirewallConfiguration.java b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/HttpFirewallConfiguration.java new file mode 100644 index 0000000000..3147b962a3 --- /dev/null +++ b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/HttpFirewallConfiguration.java @@ -0,0 +1,48 @@ +package com.baeldung.httpfirewall; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.web.firewall.HttpFirewall; +import org.springframework.security.web.firewall.HttpStatusRequestRejectedHandler; +import org.springframework.security.web.firewall.RequestRejectedHandler; +import org.springframework.security.web.firewall.StrictHttpFirewall; + +import java.util.Arrays; + +@Configuration +public class HttpFirewallConfiguration extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception { + //@formatter:off + http + .csrf() + .disable() + .authorizeRequests() + .antMatchers("/error") + .permitAll() + .anyRequest() + .authenticated() + .and() + .httpBasic(); + //@formatter:on + } + + @Bean + public HttpFirewall configureFirewall() { + StrictHttpFirewall strictHttpFirewall = new StrictHttpFirewall(); + strictHttpFirewall.setAllowedHttpMethods(Arrays.asList("GET", "POST", "DELETE", "OPTIONS")); // Allow only HTTP GET, POST, DELETE and OPTIONS methods + return strictHttpFirewall; + } + + /* + Use this bean if you are using Spring Security 5.4 and above + */ + @Bean + public RequestRejectedHandler requestRejectedHandler() { + return new HttpStatusRequestRejectedHandler(); // Default status code is 400. Can be customized + } + +} diff --git a/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/api/UserApi.java b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/api/UserApi.java new file mode 100644 index 0000000000..9bed81bb8f --- /dev/null +++ b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/api/UserApi.java @@ -0,0 +1,59 @@ +package com.baeldung.httpfirewall.api; + +import com.baeldung.httpfirewall.model.Response; +import com.baeldung.httpfirewall.model.User; +import com.baeldung.httpfirewall.service.UserServiceImpl; +import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +@RestController +@RequestMapping("/api/v1/users") +public class UserApi { + + private final UserServiceImpl userServiceImpl; + + public UserApi(UserServiceImpl userServiceImpl) { + this.userServiceImpl = userServiceImpl; + } + + @PostMapping + public ResponseEntity createUser(@RequestBody User user) { + if (StringUtils.isBlank(user.getId())) { + user.setId(UUID.randomUUID().toString()); + } + userServiceImpl.saveUser(user); + Response response = new Response(HttpStatus.CREATED.value(), "User created successfully", System.currentTimeMillis()); + URI location = URI.create("/users/" + user.getId()); + return ResponseEntity.created(location).body(response); + } + + @GetMapping("/{userId}") + public User getUser(@PathVariable("userId") String userId) { + return userServiceImpl.findById(userId).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No user exists with the given Id")); + } + + @GetMapping() + public List getAllUsers() { + return userServiceImpl.findAll().orElse(new ArrayList<>()); + } + + @DeleteMapping("/{userId}") + public ResponseEntity deleteUser(@PathVariable("userId") String userId) { + userServiceImpl.deleteUser(userId); + return ResponseEntity.ok(new Response(200, "The user has been deleted successfully", System.currentTimeMillis())); + } +} diff --git a/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/dao/InMemoryUserDao.java b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/dao/InMemoryUserDao.java new file mode 100644 index 0000000000..fb89abf17d --- /dev/null +++ b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/dao/InMemoryUserDao.java @@ -0,0 +1,65 @@ +package com.baeldung.httpfirewall.dao; + +import com.baeldung.httpfirewall.model.User; +import org.springframework.stereotype.Repository; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +@Repository +public class InMemoryUserDao { + private Map map = new HashMap<>(); + + /** + * Persists the user. The user is store in an In-Memory store (a HashMap) + * The default implementation is an In-Memory persistence + * @param user The user that should be persisted + */ + + public void save(User user) { + map.put(user.getId(), user); + } + + /** + * Finds the user from the in-memory data store. + * The default implementation is an In-Memory persistence + * + * @param userId The ID of the user that has to be fetched + * @return An optional of the requested user + */ + public Optional findById(String userId) { + return Optional.ofNullable(map.get(userId)); + } + + /** + * Finds all the users from the in-memory data store + * The default implementation is an In-Memory persistence + */ + + public Optional> findAll() { + return Optional.of(new ArrayList<>(map.values())); + } + + /** + * Delete the user from the data store + * The default implementation is an In-Memory persistence + * @param userId The user that has to be deleted + */ + + public void delete(String userId) { + map.remove(userId); + } + + /** + * Checks if the user exists + * The default implementation is an In-Memory persistence + * @param userId The user that has to be checked for + */ + + public boolean isExists(String userId) { + return map.containsKey(userId); + } +} diff --git a/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/model/Response.java b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/model/Response.java new file mode 100644 index 0000000000..ba1c8d5dea --- /dev/null +++ b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/model/Response.java @@ -0,0 +1,40 @@ +package com.baeldung.httpfirewall.model; + +public class Response { + private int code; + private String message; + private long timestamp; + + public Response() { + } + + public Response(int code, String message, long timestamp) { + this.code = code; + this.message = message; + this.timestamp = timestamp; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } +} diff --git a/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/model/User.java b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/model/User.java new file mode 100644 index 0000000000..5807a990e7 --- /dev/null +++ b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/model/User.java @@ -0,0 +1,41 @@ +package com.baeldung.httpfirewall.model; + +public class User { + private String id; + private String username; + private String email; + + public User() { + } + + public User(String id, String username, String email) { + this.id = id; + this.username = username; + this.email = email; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + +} diff --git a/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/service/UserServiceImpl.java b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/service/UserServiceImpl.java new file mode 100644 index 0000000000..d3d3f20f3d --- /dev/null +++ b/spring-security-modules/spring-security-web-boot-3/src/main/java/com/baeldung/httpfirewall/service/UserServiceImpl.java @@ -0,0 +1,51 @@ +package com.baeldung.httpfirewall.service; + +import com.baeldung.httpfirewall.dao.InMemoryUserDao; +import com.baeldung.httpfirewall.model.User; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +public class UserServiceImpl { + + private final InMemoryUserDao inMemoryUserDao; + + public UserServiceImpl(InMemoryUserDao inMemoryUserDao) { + this.inMemoryUserDao = inMemoryUserDao; + } + + /** + * Creates a user. Checks if the user already exists and then persists the user + * @param user The user that is to be persisted into the store + */ + public void saveUser(User user) { + inMemoryUserDao.save(user); + } + + /** + * Get a user. Returns a user + * + * @param userId The user that has to be fetched form the repository + */ + public Optional findById(String userId) { + return inMemoryUserDao.findById(userId); + } + + /** + * Fetch all the users in the store + * @return A list of all the users + */ + public Optional> findAll() { + return inMemoryUserDao.findAll(); + } + + /** + * Delete the user with a given id + * @param userId The identifier of the user + */ + public void deleteUser(String userId) { + inMemoryUserDao.delete(userId); + } +} diff --git a/spring-security-modules/spring-security-web-boot-3/src/main/resources/application-httpfirewall.properties b/spring-security-modules/spring-security-web-boot-3/src/main/resources/application-httpfirewall.properties new file mode 100644 index 0000000000..cb7e159bf3 --- /dev/null +++ b/spring-security-modules/spring-security-web-boot-3/src/main/resources/application-httpfirewall.properties @@ -0,0 +1,2 @@ +spring.security.user.name=user +spring.security.user.password=password \ No newline at end of file diff --git a/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/api/UserApiLiveTest.java b/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/api/UserApiLiveTest.java new file mode 100644 index 0000000000..4b4a9a40ce --- /dev/null +++ b/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/api/UserApiLiveTest.java @@ -0,0 +1,114 @@ +package com.baeldung.httpfirewall.api; + +import com.baeldung.httpfirewall.model.User; + +import com.baeldung.httpfirewall.service.UserServiceImpl; +import com.baeldung.httpfirewall.utility.UserTestUtility; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpStatus; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; + +@SpringBootTest +@AutoConfigureMockMvc +@DisplayName("User API Live Tests") +class UserApiLiveTest { + + private final String userId = "1"; + + @Autowired + private MockMvc mockMvc; + @Autowired + private ObjectMapper objectMapper; + + @BeforeEach + void setup() throws Exception { + //@formatter:off + mockMvc + .perform(post("/api/v1/users") + .content(objectMapper.writeValueAsString(UserTestUtility.createUserWithId(userId))) + .contentType("application/json")); + //@formatter:on + } + + @Test + @WithMockUser + @DisplayName("LiveTest User Creation") + void givenCredentials_whenHttpPost_thenReturn201() throws Exception { + // @formatter:off + MvcResult result = mockMvc + .perform(post("/api/v1/users") + .content(objectMapper.writeValueAsString(UserTestUtility.createUserWithId("200"))) + .contentType("application/json")) + .andDo(print()) + .andExpect(header().exists("Location")).andReturn(); + + assertEquals(HttpStatus.CREATED.value(), result.getResponse().getStatus()); + // @formatter:on + mockMvc.perform(delete("/api/v1/users/" + 200).contentType("application/json")); + } + + @Test + @WithMockUser + @DisplayName("LiveTest Get User") + void givenCredentials_whenHttpGetById_thenReturnUser() throws Exception { + // @formatter:off + MvcResult result=mockMvc + .perform(get("/api/v1/users/"+userId) + .contentType("application/json")).andReturn(); + // @formatter:on + assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus()); + assertNotNull(result.getResponse()); + assertEquals(userId, objectMapper.readValue(result.getResponse().getContentAsString(), User.class).getId()); + } + + @Test + @WithMockUser + @DisplayName("LiveTest Get All Users") + void givenCredentials_whenHttpGet_thenReturnAllUsers() throws Exception { + // @formatter:off + MvcResult result=mockMvc + .perform(get("/api/v1/users/") + .contentType("application/json")).andReturn(); + // @formatter:on + assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus()); + assertNotNull(result.getResponse()); + assertNotNull(result.getResponse().getContentAsString()); + + List users = objectMapper.readValue(result.getResponse().getContentAsString(), objectMapper.getTypeFactory().constructCollectionType(List.class, User.class)); + + assertEquals(1, users.size()); + } + + @Test + @WithMockUser + @DisplayName("LiveTest Delete User") + void givenCredentials_whenHttpDelete_thenDeleteUser() throws Exception { + // @formatter:off + MvcResult result=mockMvc + .perform(delete("/api/v1/users/"+userId) + .contentType("application/json")).andReturn(); + // @formatter:on + assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus()); + assertNotNull(result.getResponse()); + assertNotNull(result.getResponse().getContentAsString()); + } + +} diff --git a/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/api/UserApiUnitTest.java b/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/api/UserApiUnitTest.java new file mode 100644 index 0000000000..4f6217ade2 --- /dev/null +++ b/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/api/UserApiUnitTest.java @@ -0,0 +1,161 @@ +package com.baeldung.httpfirewall.api; + +import com.baeldung.httpfirewall.model.User; +import com.baeldung.httpfirewall.service.UserServiceImpl; +import com.baeldung.httpfirewall.utility.UserTestUtility; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpStatus; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; + +@WebMvcTest +@AutoConfigureMockMvc +@DisplayName("User API Unit Tests") +class UserApiUnitTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private UserServiceImpl userService; + + @Autowired + private ObjectMapper objectMapper; + + @Test + @DisplayName("Test to Check Authentication") + void whenNoAuthentication_thenThrow401() throws Exception { + // @formatter:off + MvcResult result = mockMvc + .perform(post("/api/v1/users") + .content(objectMapper.writeValueAsString(UserTestUtility.createUser())) + .contentType("application/json")) + .andReturn(); + assertEquals(HttpStatus.UNAUTHORIZED.value(), result.getResponse().getStatus()); + // @formatter:off + } + + @Test + @WithMockUser + @DisplayName("Test Malicious URL") + void givenCredentials_whenMaliciousUrl_thenThrowRequestRejectedException() throws Exception { + // @formatter:off + MvcResult result = mockMvc + .perform(post("/api/v1\\users") + .content(objectMapper.writeValueAsString(UserTestUtility.createUser())) + .contentType("application/json")) + .andDo(print()) + .andReturn(); + assertEquals(HttpStatus.BAD_REQUEST.value(), result.getResponse().getStatus()); + // @formatter:on + } + + @Test + @WithMockUser + @DisplayName("Test User Create") + void givenCredentials_whenHttpPost_thenReturn201() throws Exception { + // @formatter:off + doNothing().when(userService).saveUser(new User()); + + MvcResult result=mockMvc + .perform(post("/api/v1/users") + .content(objectMapper.writeValueAsString(UserTestUtility.createUser())) + .contentType("application/json")) + .andDo(print()) + .andExpect(header().exists("Location")).andReturn(); + assertEquals(HttpStatus.CREATED.value(), result.getResponse().getStatus()); + // @formatter:on + } + + @Test + @WithMockUser + @DisplayName("Test User Create Without ID") + void givenCredentials_whenHttpPostWithId_thenReturn201() throws Exception { + // @formatter:off + doNothing().when(userService).saveUser(new User()); + + MvcResult result = mockMvc + .perform(post("/api/v1/users") + .content(objectMapper.writeValueAsString(UserTestUtility.createUserWithoutId())) + .contentType("application/json")) + .andDo(print()) + .andExpect(header().exists("Location")).andReturn(); + assertEquals(HttpStatus.CREATED.value(), result.getResponse().getStatus()); + // @formatter:on + } + + @Test + @WithMockUser + @DisplayName("Test Get User") + void givenCredentials_whenHttpGetWithId_thenReturnUser() throws Exception { + String userId = "1"; + // @formatter:off + when(userService.findById("1")).thenReturn(UserTestUtility.createUserWithId(userId)); + + MvcResult result = mockMvc + .perform(get("/api/v1/users/"+userId) + .accept("application/json")) + .andDo(print()) + .andReturn(); + assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus()); + assertNotNull(result.getResponse()); + assertEquals("jhondoe",objectMapper.readValue(result.getResponse().getContentAsString(), User.class).getUsername()); + + // @formatter:on + } + + @Test + @WithMockUser + @DisplayName("Test Get All Users") + void givenCredentials_whenHttpGetWithoutId_thenReturnAllUsers() throws Exception { + // @formatter:off + when(userService.findAll()).thenReturn(UserTestUtility.createUsers()); + + MvcResult result = mockMvc + .perform(get("/api/v1/users/") + .accept("application/json")) + .andDo(print()) + .andReturn(); + assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus()); + assertNotNull(result.getResponse()); + assertTrue(result.getResponse().getContentAsString().contains("jane.doe")); + // @formatter:on + } + + @Test + @WithMockUser + @DisplayName("Test Delete a User") + void givenCredentials_whenHttpDelete_thenDeleteUser() throws Exception { + String userId = "1"; + doNothing().when(userService).deleteUser(userId); + // @formatter:off + MvcResult result = mockMvc + .perform(delete("/api/v1/users/"+userId) + .accept("application/json")) + .andDo(print()) + .andReturn(); + assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus()); + assertNotNull(result.getResponse()); + assertTrue(result.getResponse().getContentAsString().contains("The user has been deleted successfully")); + // @formatter:on + } + +} diff --git a/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/service/UserServiceUnitTest.java b/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/service/UserServiceUnitTest.java new file mode 100644 index 0000000000..4a58312c0f --- /dev/null +++ b/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/service/UserServiceUnitTest.java @@ -0,0 +1,92 @@ +package com.baeldung.httpfirewall.service; + +import com.baeldung.httpfirewall.dao.InMemoryUserDao; + +import com.baeldung.httpfirewall.model.User; +import com.baeldung.httpfirewall.utility.UserTestUtility; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@DisplayName("UserService Unit Tests") +class UserServiceUnitTest { + + @InjectMocks + private UserServiceImpl userService; + + @Mock + private InMemoryUserDao userDao; + + @BeforeEach + void setup() { + MockitoAnnotations.openMocks(this); + } + + @Test + @DisplayName("Check Create User") + void whenCalledCreateUser_thenVerify() { + User user = UserTestUtility.createUser(); + doNothing().when(userDao).save(user); + + userService.saveUser(user); + verify(userDao, times(1)).save(user); + } + + + + @Test + @DisplayName("Check Get User") + void givenUserId_whenCalledFindById_thenReturnUser() { + User user = UserTestUtility.createUserWithId("1").orElse(new User("1", "jhondoe", "jhon.doe@gmail.com")); + + when(userDao.findById(user.getId())).thenReturn(Optional.of(user)); + + User actualUser = userService.findById("1").get(); + + assertNotNull(actualUser); + assertEquals("jhondoe", actualUser.getUsername()); + verify(userDao, times(1)).findById(user.getId()); + } + + @Test + @DisplayName("Check Get All Users") + void whenCalledFindAll_thenReturnAllUsers() { + List users = UserTestUtility.createUsers().orElse(new ArrayList<>()); + + when(userDao.findAll()).thenReturn(Optional.of(users)); + + Optional> actualUsers = userService.findAll(); + + assertNotNull(actualUsers); + assertEquals(2, users.size()); + verify(userDao, times(1)).findAll(); + } + + @Test + @DisplayName("Check Delete Users") + void givenId_whenCalledDeleteUser_thenDeleteUser() { + User user = UserTestUtility.createUserWithId("1").orElse(new User()); + + doNothing().when(userDao).delete(user.getId()); + userService.deleteUser(user.getId()); + verify(userDao, times(1)).delete(user.getId()); + } + +} diff --git a/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/utility/UserTestUtility.java b/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/utility/UserTestUtility.java new file mode 100644 index 0000000000..77953a5176 --- /dev/null +++ b/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/httpfirewall/utility/UserTestUtility.java @@ -0,0 +1,33 @@ +package com.baeldung.httpfirewall.utility; + +import com.baeldung.httpfirewall.model.User; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public class UserTestUtility { + public static User createUser() { + return new User(UUID.randomUUID().toString(),"jhondoe", "jhondoe@gmail.com"); + } + + public static User createUserWithoutId() { + return new User("","jhondoe", "jhondoe@gmail.com"); + } + + public static Optional createUserWithId(String id) { + // @formatter:off + return Optional.of(new User(id, "jhondoe", "jhon.doe@gmail.com")); + // @formatter:on + } + + public static Optional> createUsers() { + // @formatter:off + return Optional.of(Arrays.asList( + new User(UUID.randomUUID().toString(), "jhondoe","jhon.doe@gmail.com" ), + new User(UUID.randomUUID().toString(), "janedoe","jane.doe@gmail.com" )) + ); + // @formatter:on + } +} From a97ae4895830c633dcf0e8bb9125679791a61c6a Mon Sep 17 00:00:00 2001 From: mbarriola <85458535+mbarriola@users.noreply.github.com> Date: Fri, 1 Oct 2021 10:24:06 -0400 Subject: [PATCH 112/118] Bael 4466 error handling (#11269) * Commit source code to branch * BAEL-5065 improvement of groupBy with complex key * BAEL-4466 Implementation of error handling --- grpc/pom.xml | 6 + .../grpc/errorhandling/CommodityClient.java | 127 ++++++++++++ .../grpc/errorhandling/CommodityServer.java | 182 ++++++++++++++++++ .../baeldung/grpc/streaming/StockClient.java | 47 ++--- .../baeldung/grpc/streaming/StockServer.java | 11 +- grpc/src/main/proto/commodity_price.proto | 42 ++++ grpc/src/main/proto/google/protobuf/any.proto | 158 +++++++++++++++ grpc/src/main/proto/google/rpc/status.proto | 47 +++++ grpc/src/main/proto/stock_quote.proto | 2 - .../CommodityServerUnitTest.java | 102 ++++++++++ 10 files changed, 690 insertions(+), 34 deletions(-) create mode 100644 grpc/src/main/java/com/baeldung/grpc/errorhandling/CommodityClient.java create mode 100644 grpc/src/main/java/com/baeldung/grpc/errorhandling/CommodityServer.java create mode 100644 grpc/src/main/proto/commodity_price.proto create mode 100644 grpc/src/main/proto/google/protobuf/any.proto create mode 100644 grpc/src/main/proto/google/rpc/status.proto create mode 100644 grpc/src/test/java/com/baeldung/grpc/errorhandling/CommodityServerUnitTest.java diff --git a/grpc/pom.xml b/grpc/pom.xml index 77ec9be464..50700b0785 100644 --- a/grpc/pom.xml +++ b/grpc/pom.xml @@ -31,6 +31,12 @@ grpc-stub ${io.grpc.version} + + io.grpc + grpc-testing + ${io.grpc.version} + test + junit junit diff --git a/grpc/src/main/java/com/baeldung/grpc/errorhandling/CommodityClient.java b/grpc/src/main/java/com/baeldung/grpc/errorhandling/CommodityClient.java new file mode 100644 index 0000000000..9821df9182 --- /dev/null +++ b/grpc/src/main/java/com/baeldung/grpc/errorhandling/CommodityClient.java @@ -0,0 +1,127 @@ +package com.baeldung.grpc.errorhandling; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.baeldung.grpc.errorhandling.CommodityPriceProviderGrpc.CommodityPriceProviderStub; +import com.google.protobuf.Any; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.rpc.Code; +import com.google.rpc.ErrorInfo; + +import io.grpc.Channel; +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; +import io.grpc.Status; +import io.grpc.stub.StreamObserver; + +public class CommodityClient { + + private static final Logger logger = LoggerFactory.getLogger(CommodityClient.class.getName()); + + private final CommodityPriceProviderStub nonBlockingStub; + + public CommodityClient(Channel channel) { + + nonBlockingStub = CommodityPriceProviderGrpc.newStub(channel); + } + + + public void getBidirectionalCommodityPriceLists() throws InterruptedException { + + logger.info("#######START EXAMPLE#######: BidirectionalStreaming - getCommodityPriceLists from list of commodities"); + final CountDownLatch finishLatch = new CountDownLatch(1); + StreamObserver responseObserver = new StreamObserver() { + @Override +public void onNext(StreamingCommodityQuote streamingCommodityQuote) { + + switch (streamingCommodityQuote.getMessageCase()) { + case COMODITY_QUOTE: + CommodityQuote commodityQuote = streamingCommodityQuote.getComodityQuote(); + logger.info("RESPONSE producer:" + commodityQuote.getCommodityName() + " price:" + commodityQuote.getPrice()); + break; + case STATUS: + com.google.rpc.Status status = streamingCommodityQuote.getStatus(); + logger.info("RESPONSE status error:"); + logger.info("Status code:" + Code.forNumber(status.getCode())); + logger.info("Status message:" + status.getMessage()); + for (Any any : status.getDetailsList()) { + if (any.is(ErrorInfo.class)) { + ErrorInfo errorInfo; + try { + errorInfo = any.unpack(ErrorInfo.class); + logger.info("Reason:" + errorInfo.getReason()); + logger.info("Domain:" + errorInfo.getDomain()); + logger.info("Insert Token:" + errorInfo.getMetadataMap().get("insertToken")); + } catch (InvalidProtocolBufferException e) { + logger.error(e.getMessage()); + } + } + } + break; + default: + logger.info("Unknow message case"); + } +} + + @Override + public void onCompleted() { + logger.info("Finished getBidirectionalCommodityPriceListss"); + finishLatch.countDown(); + } + + @Override + public void onError(Throwable t) { + logger.error("getBidirectionalCommodityPriceLists Failed:" + Status.fromThrowable(t)); + finishLatch.countDown(); + } + }; + StreamObserver requestObserver = nonBlockingStub.bidirectionalListOfPrices(responseObserver); + try { + for (int i = 1; i <= 2; i++) { + Commodity request = Commodity.newBuilder() + .setCommodityName("Commodity" + i) + .setAccessToken(i + "23validToken") + .build(); + logger.info("REQUEST - commodity:" + request.getCommodityName()); + requestObserver.onNext(request); + Thread.sleep(200); + if (finishLatch.getCount() == 0) { + return; + } + } + } catch (RuntimeException e) { + requestObserver.onError(e); + throw e; + } + requestObserver.onCompleted(); + + if (!finishLatch.await(1, TimeUnit.MINUTES)) { + logger.info("getBidirectionalCommodityPriceLists can not finish within 1 minute"); + } + } + + public static void main(String[] args) throws InterruptedException, InvalidProtocolBufferException { + + String target = "localhost:8980"; + if (args.length > 0) { + target = args[0]; + } + + ManagedChannel channel = ManagedChannelBuilder.forTarget(target) + .usePlaintext() + .build(); + try { + CommodityClient client = new CommodityClient(channel); + + client.getBidirectionalCommodityPriceLists(); + + } finally { + channel.shutdownNow() + .awaitTermination(5, TimeUnit.SECONDS); + } + } +} diff --git a/grpc/src/main/java/com/baeldung/grpc/errorhandling/CommodityServer.java b/grpc/src/main/java/com/baeldung/grpc/errorhandling/CommodityServer.java new file mode 100644 index 0000000000..567c0815e5 --- /dev/null +++ b/grpc/src/main/java/com/baeldung/grpc/errorhandling/CommodityServer.java @@ -0,0 +1,182 @@ +package com.baeldung.grpc.errorhandling; + +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.protobuf.Any; +import com.google.rpc.Code; +import com.google.rpc.ErrorInfo; + +import io.grpc.Metadata; +import io.grpc.Server; +import io.grpc.ServerBuilder; +import io.grpc.Status; +import io.grpc.protobuf.ProtoUtils; +import io.grpc.protobuf.StatusProto; +import io.grpc.stub.StreamObserver; + +public class CommodityServer { + + private static final Logger logger = LoggerFactory.getLogger(CommodityServer.class.getName()); + private final int port; + private final Server server; + private static Map commodityLookupBasePrice; + static { + commodityLookupBasePrice = new ConcurrentHashMap<>(); + commodityLookupBasePrice.put("Commodity1", 5.0); + commodityLookupBasePrice.put("Commodity2", 6.0); + } + + public static void main(String[] args) throws Exception { + + CommodityServer commodityServer = new CommodityServer(8980); + commodityServer.start(); + if (commodityServer.server != null) { + commodityServer.server.awaitTermination(); + } + } + + public CommodityServer(int port) throws IOException { + this.port = port; + server = ServerBuilder.forPort(port) + .addService(new CommodityService()) + .build(); + } + + public void start() throws IOException { + server.start(); + logger.info("Server started, listening on {}", port); + Runtime.getRuntime() + .addShutdownHook(new Thread() { + @Override + public void run() { + System.err.println("shutting down server"); + try { + CommodityServer.this.stop(); + } catch (InterruptedException e) { + e.printStackTrace(System.err); + } + System.err.println("server shutted down"); + } + }); + } + + public void stop() throws InterruptedException { + if (server != null) { + server.shutdown() + .awaitTermination(30, TimeUnit.SECONDS); + } + } + + public static class CommodityService extends CommodityPriceProviderGrpc.CommodityPriceProviderImplBase { + + @Override + public void getBestCommodityPrice(Commodity request, StreamObserver responseObserver) { + + if (commodityLookupBasePrice.get(request.getCommodityName()) == null) { + + Metadata.Key errorResponseKey = ProtoUtils.keyForProto(ErrorResponse.getDefaultInstance()); + ErrorResponse errorResponse = ErrorResponse.newBuilder() + .setCommodityName(request.getCommodityName()) + .setAccessToken(request.getAccessToken()) + .setExpectedValue("Only Commodity1, Commodity2 are supported") + .build(); + Metadata metadata = new Metadata(); + metadata.put(errorResponseKey, errorResponse); + responseObserver.onError(Status.INVALID_ARGUMENT.withDescription("The commodity is not supported") + .asRuntimeException(metadata)); + } else if (request.getAccessToken().equals("123validToken") == false) { + + com.google.rpc.Status status = com.google.rpc.Status.newBuilder() + .setCode(Code.NOT_FOUND.getNumber()) + .setMessage("The access token not found") + .addDetails(Any.pack(ErrorInfo.newBuilder() + .setReason("Invalid Token") + .setDomain("com.baeldung.grpc.errorhandling") + .putMetadata("insertToken", "123validToken") + .build())) + .build(); + responseObserver.onError(StatusProto.toStatusRuntimeException(status)); + } else { + CommodityQuote commodityQuote = CommodityQuote.newBuilder() + .setPrice(fetchBestPriceBid(request)) + .setCommodityName(request.getCommodityName()) + .setProducerName("Best Producer with best price") + .build(); + responseObserver.onNext(commodityQuote); + responseObserver.onCompleted(); + } + } + + @Override + public StreamObserver bidirectionalListOfPrices(StreamObserver responseObserver) { + + return new StreamObserver() { + @Override + public void onNext(Commodity request) { + + logger.info("Access token:{}", request.getAccessToken()); + if (request.getAccessToken() + .equals("123validToken") == false) { + + com.google.rpc.Status status = com.google.rpc.Status.newBuilder() + .setCode(Code.NOT_FOUND.getNumber()) + .setMessage("The access token not found") + .addDetails(Any.pack(ErrorInfo.newBuilder() + .setReason("Invalid Token") + .setDomain("com.baeldung.grpc.errorhandling") + .putMetadata("insertToken", "123validToken") + .build())) + .build(); + StreamingCommodityQuote streamingCommodityQuote = StreamingCommodityQuote.newBuilder() + .setStatus(status) + .build(); + responseObserver.onNext(streamingCommodityQuote); + } else { + + for (int i = 1; i <= 5; i++) { + CommodityQuote commodityQuote = CommodityQuote.newBuilder() + .setPrice(fetchProviderPriceBid(request, "producer:" + i)) + .setCommodityName(request.getCommodityName()) + .setProducerName("producer:" + i) + .build(); + StreamingCommodityQuote streamingCommodityQuote = StreamingCommodityQuote.newBuilder() + .setComodityQuote(commodityQuote) + .build(); + responseObserver.onNext(streamingCommodityQuote); + } + } + } + + @Override + public void onCompleted() { + responseObserver.onCompleted(); + } + + @Override + public void onError(Throwable t) { + logger.info("error:{}", t.getMessage()); + } + }; + } + + } + + private static double fetchBestPriceBid(Commodity commodity) { + + return commodityLookupBasePrice.get(commodity.getCommodityName()) + ThreadLocalRandom.current() + .nextDouble(-0.2d, 0.2d); + } + + private static double fetchProviderPriceBid(Commodity commodity, String providerName) { + + return commodityLookupBasePrice.get(commodity.getCommodityName()) + providerName.length() + ThreadLocalRandom.current() + .nextDouble(-0.2d, 0.2d); + } +} diff --git a/grpc/src/main/java/com/baeldung/grpc/streaming/StockClient.java b/grpc/src/main/java/com/baeldung/grpc/streaming/StockClient.java index 1850c975a2..e2b670458e 100644 --- a/grpc/src/main/java/com/baeldung/grpc/streaming/StockClient.java +++ b/grpc/src/main/java/com/baeldung/grpc/streaming/StockClient.java @@ -5,8 +5,9 @@ import java.util.Iterator; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import java.util.logging.Level; -import java.util.logging.Logger; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.baeldung.grpc.streaming.StockQuoteProviderGrpc.StockQuoteProviderBlockingStub; import com.baeldung.grpc.streaming.StockQuoteProviderGrpc.StockQuoteProviderStub; @@ -19,7 +20,7 @@ import io.grpc.StatusRuntimeException; import io.grpc.stub.StreamObserver; public class StockClient { - private static final Logger logger = Logger.getLogger(StockClient.class.getName()); + private static final Logger logger = LoggerFactory.getLogger(StockClient.class.getName()); private final StockQuoteProviderBlockingStub blockingStub; private final StockQuoteProviderStub nonBlockingStub; @@ -34,7 +35,7 @@ public class StockClient { public void serverSideStreamingListOfStockPrices() { - logInfo("######START EXAMPLE######: ServerSideStreaming - list of Stock prices from a given stock"); + logger.info("######START EXAMPLE######: ServerSideStreaming - list of Stock prices from a given stock"); Stock request = Stock.newBuilder() .setTickerSymbol("AU") .setCompanyName("Austich") @@ -42,36 +43,36 @@ public class StockClient { .build(); Iterator stockQuotes; try { - logInfo("REQUEST - ticker symbol {0}", request.getTickerSymbol()); + logger.info("REQUEST - ticker symbol {}", request.getTickerSymbol()); stockQuotes = blockingStub.serverSideStreamingGetListStockQuotes(request); for (int i = 1; stockQuotes.hasNext(); i++) { StockQuote stockQuote = stockQuotes.next(); - logInfo("RESPONSE - Price #" + i + ": {0}", stockQuote.getPrice()); + logger.info("RESPONSE - Price #" + i + ": {}", stockQuote.getPrice()); } } catch (StatusRuntimeException e) { - logInfo("RPC failed: {0}", e.getStatus()); + logger.info("RPC failed: {}", e.getStatus()); } } public void clientSideStreamingGetStatisticsOfStocks() throws InterruptedException { - logInfo("######START EXAMPLE######: ClientSideStreaming - getStatisticsOfStocks from a list of stocks"); + logger.info("######START EXAMPLE######: ClientSideStreaming - getStatisticsOfStocks from a list of stocks"); final CountDownLatch finishLatch = new CountDownLatch(1); StreamObserver responseObserver = new StreamObserver() { @Override public void onNext(StockQuote summary) { - logInfo("RESPONSE, got stock statistics - Average Price: {0}, description: {1}", summary.getPrice(), summary.getDescription()); + logger.info("RESPONSE, got stock statistics - Average Price: {}, description: {}", summary.getPrice(), summary.getDescription()); } @Override public void onCompleted() { - logInfo("Finished clientSideStreamingGetStatisticsOfStocks"); + logger.info("Finished clientSideStreamingGetStatisticsOfStocks"); finishLatch.countDown(); } @Override public void onError(Throwable t) { - logWarning("Stock Statistics Failed: {0}", Status.fromThrowable(t)); + logger.warn("Stock Statistics Failed: {}", Status.fromThrowable(t)); finishLatch.countDown(); } }; @@ -80,7 +81,7 @@ public class StockClient { try { for (Stock stock : stocks) { - logInfo("REQUEST: {0}, {1}", stock.getTickerSymbol(), stock.getCompanyName()); + logger.info("REQUEST: {}, {}", stock.getTickerSymbol(), stock.getCompanyName()); requestObserver.onNext(stock); if (finishLatch.getCount() == 0) { return; @@ -92,36 +93,36 @@ public class StockClient { } requestObserver.onCompleted(); if (!finishLatch.await(1, TimeUnit.MINUTES)) { - logWarning("clientSideStreamingGetStatisticsOfStocks can not finish within 1 minutes"); + logger.warn("clientSideStreamingGetStatisticsOfStocks can not finish within 1 minutes"); } } public void bidirectionalStreamingGetListsStockQuotes() throws InterruptedException{ - logInfo("#######START EXAMPLE#######: BidirectionalStreaming - getListsStockQuotes from list of stocks"); + logger.info("#######START EXAMPLE#######: BidirectionalStreaming - getListsStockQuotes from list of stocks"); final CountDownLatch finishLatch = new CountDownLatch(1); StreamObserver responseObserver = new StreamObserver() { @Override public void onNext(StockQuote stockQuote) { - logInfo("RESPONSE price#{0} : {1}, description:{2}", stockQuote.getOfferNumber(), stockQuote.getPrice(), stockQuote.getDescription()); + logger.info("RESPONSE price#{} : {}, description:{}", stockQuote.getOfferNumber(), stockQuote.getPrice(), stockQuote.getDescription()); } @Override public void onCompleted() { - logInfo("Finished bidirectionalStreamingGetListsStockQuotes"); + logger.info("Finished bidirectionalStreamingGetListsStockQuotes"); finishLatch.countDown(); } @Override public void onError(Throwable t) { - logWarning("bidirectionalStreamingGetListsStockQuotes Failed: {0}", Status.fromThrowable(t)); + logger.warn("bidirectionalStreamingGetListsStockQuotes Failed: {0}", Status.fromThrowable(t)); finishLatch.countDown(); } }; StreamObserver requestObserver = nonBlockingStub.bidirectionalStreamingGetListsStockQuotes(responseObserver); try { for (Stock stock : stocks) { - logInfo("REQUEST: {0}, {1}", stock.getTickerSymbol(), stock.getCompanyName()); + logger.info("REQUEST: {}, {}", stock.getTickerSymbol(), stock.getCompanyName()); requestObserver.onNext(stock); Thread.sleep(200); if (finishLatch.getCount() == 0) { @@ -135,7 +136,7 @@ public class StockClient { requestObserver.onCompleted(); if (!finishLatch.await(1, TimeUnit.MINUTES)) { - logWarning("bidirectionalStreamingGetListsStockQuotes can not finish within 1 minute"); + logger.warn("bidirectionalStreamingGetListsStockQuotes can not finish within 1 minute"); } } @@ -172,12 +173,4 @@ public class StockClient { , Stock.newBuilder().setTickerSymbol("DIA").setCompanyName("Dialogic Corp").setDescription("Development Intel").build() , Stock.newBuilder().setTickerSymbol("EUS").setCompanyName("Euskaltel Corp").setDescription("English Intel").build()); } - - private void logInfo(String msg, Object... params) { - logger.log(Level.INFO, msg, params); - } - - private void logWarning(String msg, Object... params) { - logger.log(Level.WARNING, msg, params); - } } diff --git a/grpc/src/main/java/com/baeldung/grpc/streaming/StockServer.java b/grpc/src/main/java/com/baeldung/grpc/streaming/StockServer.java index f4dc6c39ac..952c9f26fd 100644 --- a/grpc/src/main/java/com/baeldung/grpc/streaming/StockServer.java +++ b/grpc/src/main/java/com/baeldung/grpc/streaming/StockServer.java @@ -3,8 +3,9 @@ package com.baeldung.grpc.streaming; import java.io.IOException; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; -import java.util.logging.Level; -import java.util.logging.Logger; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import io.grpc.Server; import io.grpc.ServerBuilder; @@ -12,7 +13,7 @@ import io.grpc.stub.StreamObserver; public class StockServer { - private static final Logger logger = Logger.getLogger(StockServer.class.getName()); + private static final Logger logger = LoggerFactory.getLogger(StockClient.class.getName()); private final int port; private final Server server; @@ -102,7 +103,7 @@ public class StockServer { @Override public void onError(Throwable t) { - logger.log(Level.WARNING, "error:{0}", t.getMessage()); + logger.warn("error:{}", t.getMessage()); } }; } @@ -131,7 +132,7 @@ public class StockServer { @Override public void onError(Throwable t) { - logger.log(Level.WARNING, "error:{0}", t.getMessage()); + logger.warn("error:{}", t.getMessage()); } }; } diff --git a/grpc/src/main/proto/commodity_price.proto b/grpc/src/main/proto/commodity_price.proto new file mode 100644 index 0000000000..c5d6247df5 --- /dev/null +++ b/grpc/src/main/proto/commodity_price.proto @@ -0,0 +1,42 @@ +syntax = "proto3"; + +import "google/rpc/status.proto"; + +package commodityprice; + +option java_multiple_files = true; +option java_package = "com.baeldung.grpc.errorhandling"; +option java_outer_classname = "CommodityPriceProto"; +option objc_class_prefix = "RTG"; + +service CommodityPriceProvider { + + rpc getBestCommodityPrice(Commodity) returns (CommodityQuote) {} + + rpc bidirectionalListOfPrices(stream Commodity) returns (stream StreamingCommodityQuote) {} +} + +message Commodity { + string access_token = 1; + string commodity_name = 2; +} + +message CommodityQuote { + string commodity_name = 1; + string producer_name = 2; + double price = 3; +} + +message ErrorResponse { + string commodity_name = 1; + string access_token = 2; + string expected_token = 3; + string expected_value = 4; +} + +message StreamingCommodityQuote{ + oneof message{ + CommodityQuote comodity_quote = 1; + google.rpc.Status status = 2; + } +} diff --git a/grpc/src/main/proto/google/protobuf/any.proto b/grpc/src/main/proto/google/protobuf/any.proto new file mode 100644 index 0000000000..1c6a465c72 --- /dev/null +++ b/grpc/src/main/proto/google/protobuf/any.proto @@ -0,0 +1,158 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto3"; + +package google.protobuf; + +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; +option go_package = "google.golang.org/protobuf/types/known/anypb"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "AnyProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; + +// `Any` contains an arbitrary serialized protocol buffer message along with a +// URL that describes the type of the serialized message. +// +// Protobuf library provides support to pack/unpack Any values in the form +// of utility functions or additional generated methods of the Any type. +// +// Example 1: Pack and unpack a message in C++. +// +// Foo foo = ...; +// Any any; +// any.PackFrom(foo); +// ... +// if (any.UnpackTo(&foo)) { +// ... +// } +// +// Example 2: Pack and unpack a message in Java. +// +// Foo foo = ...; +// Any any = Any.pack(foo); +// ... +// if (any.is(Foo.class)) { +// foo = any.unpack(Foo.class); +// } +// +// Example 3: Pack and unpack a message in Python. +// +// foo = Foo(...) +// any = Any() +// any.Pack(foo) +// ... +// if any.Is(Foo.DESCRIPTOR): +// any.Unpack(foo) +// ... +// +// Example 4: Pack and unpack a message in Go +// +// foo := &pb.Foo{...} +// any, err := anypb.New(foo) +// if err != nil { +// ... +// } +// ... +// foo := &pb.Foo{} +// if err := any.UnmarshalTo(foo); err != nil { +// ... +// } +// +// The pack methods provided by protobuf library will by default use +// 'type.googleapis.com/full.type.name' as the type URL and the unpack +// methods only use the fully qualified type name after the last '/' +// in the type URL, for example "foo.bar.com/x/y.z" will yield type +// name "y.z". +// +// +// JSON +// ==== +// The JSON representation of an `Any` value uses the regular +// representation of the deserialized, embedded message, with an +// additional field `@type` which contains the type URL. Example: +// +// package google.profile; +// message Person { +// string first_name = 1; +// string last_name = 2; +// } +// +// { +// "@type": "type.googleapis.com/google.profile.Person", +// "firstName": , +// "lastName": +// } +// +// If the embedded message type is well-known and has a custom JSON +// representation, that representation will be embedded adding a field +// `value` which holds the custom JSON in addition to the `@type` +// field. Example (for message [google.protobuf.Duration][]): +// +// { +// "@type": "type.googleapis.com/google.protobuf.Duration", +// "value": "1.212s" +// } +// +message Any { + // A URL/resource name that uniquely identifies the type of the serialized + // protocol buffer message. This string must contain at least + // one "/" character. The last segment of the URL's path must represent + // the fully qualified name of the type (as in + // `path/google.protobuf.Duration`). The name should be in a canonical form + // (e.g., leading "." is not accepted). + // + // In practice, teams usually precompile into the binary all types that they + // expect it to use in the context of Any. However, for URLs which use the + // scheme `http`, `https`, or no scheme, one can optionally set up a type + // server that maps type URLs to message definitions as follows: + // + // * If no scheme is provided, `https` is assumed. + // * An HTTP GET on the URL must yield a [google.protobuf.Type][] + // value in binary format, or produce an error. + // * Applications are allowed to cache lookup results based on the + // URL, or have them precompiled into a binary to avoid any + // lookup. Therefore, binary compatibility needs to be preserved + // on changes to types. (Use versioned type names to manage + // breaking changes.) + // + // Note: this functionality is not currently available in the official + // protobuf release, and it is not used for type URLs beginning with + // type.googleapis.com. + // + // Schemes other than `http`, `https` (or the empty scheme) might be + // used with implementation specific semantics. + // + string type_url = 1; + + // Must be a valid serialized protocol buffer of the above specified type. + bytes value = 2; +} \ No newline at end of file diff --git a/grpc/src/main/proto/google/rpc/status.proto b/grpc/src/main/proto/google/rpc/status.proto new file mode 100644 index 0000000000..5bd51aa2f3 --- /dev/null +++ b/grpc/src/main/proto/google/rpc/status.proto @@ -0,0 +1,47 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +syntax = "proto3"; + +package google.rpc; + +import "google/protobuf/any.proto"; + +option cc_enable_arenas = true; +option go_package = "google.golang.org/genproto/googleapis/rpc/status;status"; +option java_multiple_files = true; +option java_outer_classname = "StatusProto"; +option java_package = "com.google.rpc"; +option objc_class_prefix = "RPC"; + +// The `Status` type defines a logical error model that is suitable for +// different programming environments, including REST APIs and RPC APIs. It is +// used by [gRPC](https://github.com/grpc). Each `Status` message contains +// three pieces of data: error code, error message, and error details. +// +// You can find out more about this error model and how to work with it in the +// [API Design Guide](https://cloud.google.com/apis/design/errors). +message Status { + // The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]. + int32 code = 1; + + // A developer-facing error message, which should be in English. Any + // user-facing error message should be localized and sent in the + // [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client. + string message = 2; + + // A list of messages that carry the error details. There is a common set of + // message types for APIs to use. + repeated google.protobuf.Any details = 3; +} \ No newline at end of file diff --git a/grpc/src/main/proto/stock_quote.proto b/grpc/src/main/proto/stock_quote.proto index 66891a5008..b1197886ee 100644 --- a/grpc/src/main/proto/stock_quote.proto +++ b/grpc/src/main/proto/stock_quote.proto @@ -7,8 +7,6 @@ option java_package = "com.baeldung.grpc.streaming"; option java_outer_classname = "StockQuoteProto"; option objc_class_prefix = "RTG"; -//basic setup ... - service StockQuoteProvider { rpc serverSideStreamingGetListStockQuotes(Stock) returns (stream StockQuote) {} diff --git a/grpc/src/test/java/com/baeldung/grpc/errorhandling/CommodityServerUnitTest.java b/grpc/src/test/java/com/baeldung/grpc/errorhandling/CommodityServerUnitTest.java new file mode 100644 index 0000000000..7148831e51 --- /dev/null +++ b/grpc/src/test/java/com/baeldung/grpc/errorhandling/CommodityServerUnitTest.java @@ -0,0 +1,102 @@ +package com.baeldung.grpc.errorhandling; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.Rule; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.baeldung.grpc.errorhandling.CommodityServer.CommodityService; +import com.google.protobuf.Any; +import com.google.rpc.Code; +import com.google.rpc.ErrorInfo; + +import io.grpc.Metadata; +import io.grpc.Status; +import io.grpc.StatusRuntimeException; +import io.grpc.inprocess.InProcessChannelBuilder; +import io.grpc.inprocess.InProcessServerBuilder; +import io.grpc.protobuf.ProtoUtils; +import io.grpc.protobuf.StatusProto; +import io.grpc.testing.GrpcCleanupRule; + +public class CommodityServerUnitTest { + + CommodityPriceProviderGrpc.CommodityPriceProviderBlockingStub blockingStub; + + @Rule + public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); + + @BeforeEach + public void setup() throws Exception{ + + String serverName = InProcessServerBuilder.generateName(); + + grpcCleanup.register(InProcessServerBuilder.forName(serverName) + .directExecutor() + .addService(new CommodityService()) + .build() + .start()); + + blockingStub = CommodityPriceProviderGrpc.newBlockingStub(grpcCleanup.register(InProcessChannelBuilder.forName(serverName) + .directExecutor() + .build())); + } + + @Test + public void whenUsingValidRequest_thenReturnResponse() throws Exception { + + CommodityQuote reply = blockingStub.getBestCommodityPrice(Commodity.newBuilder() + .setCommodityName("Commodity1") + .setAccessToken("123validToken") + .build()); + + assertEquals("Commodity1", reply.getCommodityName()); + } + + @Test + public void whenUsingInvalidRequestToken_thenReturnExceptionGoogleRPCStatus() throws Exception { + + Commodity request = Commodity.newBuilder() + .setAccessToken("invalidToken") + .setCommodityName("Commodity1") + .build(); + + StatusRuntimeException thrown = Assertions.assertThrows(StatusRuntimeException.class, () -> blockingStub.getBestCommodityPrice(request)); + + com.google.rpc.Status status = StatusProto.fromThrowable(thrown); + assertNotNull(status); + assertEquals("NOT_FOUND", Code.forNumber(status.getCode()).toString()); + assertEquals("The access token not found", status.getMessage()); + for (Any any : status.getDetailsList()) { + if (any.is(ErrorInfo.class)) { + ErrorInfo errorInfo = any.unpack(ErrorInfo.class); + assertEquals("Invalid Token", errorInfo.getReason()); + assertEquals("com.baeldung.grpc.errorhandling", errorInfo.getDomain()); + assertEquals("123validToken", errorInfo.getMetadataMap().get("insertToken")); + } + } + } + + @Test + public void whenUsingInvalidCommodityName_thenReturnExceptionIoRpcStatus() throws Exception { + + Commodity request = Commodity.newBuilder() + .setAccessToken("123validToken") + .setCommodityName("Commodity5") + .build(); + + StatusRuntimeException thrown = Assertions.assertThrows(StatusRuntimeException.class, () -> blockingStub.getBestCommodityPrice(request)); + + assertEquals("INVALID_ARGUMENT", thrown.getStatus().getCode().toString()); + assertEquals("INVALID_ARGUMENT: The commodity is not supported", thrown.getMessage()); + Metadata metadata = Status.trailersFromThrowable(thrown); + ErrorResponse errorResponse = metadata.get(ProtoUtils.keyForProto(ErrorResponse.getDefaultInstance())); + assertEquals("Commodity5",errorResponse.getCommodityName()); + assertEquals("123validToken", errorResponse.getAccessToken()); + assertEquals("Only Commodity1, Commodity2 are supported", errorResponse.getExpectedValue()); + } + +} From b8a9ea2974f6c7fe7124cc73d4b6b551e7b4d48f Mon Sep 17 00:00:00 2001 From: Shashank Gowri Date: Fri, 1 Oct 2021 22:36:07 +0530 Subject: [PATCH 113/118] fix: updating database username to be in sync with the article (#11270) --- .../spring-data-arangodb/src/live-test/resources/Dockerfile | 4 ++++ .../src/live-test/resources/arangodb-setup.js | 4 ++++ .../src/live-test/resources/live-test-setup.sh | 2 ++ .../arangodb/configuration/ArangoDbConfiguration.java | 2 +- .../src/main/resources/arangodb.properties | 2 +- .../com/baeldung/arangodb/ArticleRepositoryLiveTest.java | 6 ++++++ 6 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 persistence-modules/spring-data-arangodb/src/live-test/resources/arangodb-setup.js diff --git a/persistence-modules/spring-data-arangodb/src/live-test/resources/Dockerfile b/persistence-modules/spring-data-arangodb/src/live-test/resources/Dockerfile index 8edb2bbbf6..6380d312a0 100644 --- a/persistence-modules/spring-data-arangodb/src/live-test/resources/Dockerfile +++ b/persistence-modules/spring-data-arangodb/src/live-test/resources/Dockerfile @@ -2,6 +2,10 @@ FROM arangodb:3.8.0 COPY init-session.js /docker-entrypoint-initdb.d/ +COPY arangodb-setup.js /setup/ + EXPOSE 8529 ENV ARANGO_ROOT_PASSWORD=password + +RUN chmod a+x /setup/arangodb-setup.js \ No newline at end of file diff --git a/persistence-modules/spring-data-arangodb/src/live-test/resources/arangodb-setup.js b/persistence-modules/spring-data-arangodb/src/live-test/resources/arangodb-setup.js new file mode 100644 index 0000000000..0c306ee5d5 --- /dev/null +++ b/persistence-modules/spring-data-arangodb/src/live-test/resources/arangodb-setup.js @@ -0,0 +1,4 @@ +#!/usr/bin/arangosh --javascript.execute + +print('creating database: baeldung-database & user: baeldung'); +db._createDatabase("baeldung-database", {}, [{ username: "baeldung", passwd: "password", active: true}]); \ No newline at end of file diff --git a/persistence-modules/spring-data-arangodb/src/live-test/resources/live-test-setup.sh b/persistence-modules/spring-data-arangodb/src/live-test/resources/live-test-setup.sh index b1a4cfb9d0..7f75fbd821 100644 --- a/persistence-modules/spring-data-arangodb/src/live-test/resources/live-test-setup.sh +++ b/persistence-modules/spring-data-arangodb/src/live-test/resources/live-test-setup.sh @@ -3,3 +3,5 @@ docker image build -t spring-data-arangodb:live-test . docker run -p 8529:8529 -e ARANGO_ROOT_PASSWORD=password --name spring-data-arangodb-live-test spring-data-arangodb:live-test + +docker exec spring-data-arangodb-live-test arangosh --server.password password --javascript.execute /setup/arangodb-setup.js \ No newline at end of file diff --git a/persistence-modules/spring-data-arangodb/src/main/java/com/baeldung/arangodb/configuration/ArangoDbConfiguration.java b/persistence-modules/spring-data-arangodb/src/main/java/com/baeldung/arangodb/configuration/ArangoDbConfiguration.java index 3aae10ce60..0042172ad6 100644 --- a/persistence-modules/spring-data-arangodb/src/main/java/com/baeldung/arangodb/configuration/ArangoDbConfiguration.java +++ b/persistence-modules/spring-data-arangodb/src/main/java/com/baeldung/arangodb/configuration/ArangoDbConfiguration.java @@ -13,7 +13,7 @@ public class ArangoDbConfiguration implements ArangoConfiguration { public ArangoDB.Builder arango() { return new ArangoDB.Builder() .host("127.0.0.1", 8529) - .user("root") + .user("baeldung") .password("password"); } diff --git a/persistence-modules/spring-data-arangodb/src/main/resources/arangodb.properties b/persistence-modules/spring-data-arangodb/src/main/resources/arangodb.properties index 7f8ded478c..2a6e667ecc 100644 --- a/persistence-modules/spring-data-arangodb/src/main/resources/arangodb.properties +++ b/persistence-modules/spring-data-arangodb/src/main/resources/arangodb.properties @@ -1,3 +1,3 @@ arangodb.hosts=127.0.0.1:8529 -arangodb.user=root +arangodb.user=baeldung arangodb.password=password \ No newline at end of file diff --git a/persistence-modules/spring-data-arangodb/src/test/java/com/baeldung/arangodb/ArticleRepositoryLiveTest.java b/persistence-modules/spring-data-arangodb/src/test/java/com/baeldung/arangodb/ArticleRepositoryLiveTest.java index 35232766ab..401e3393e8 100644 --- a/persistence-modules/spring-data-arangodb/src/test/java/com/baeldung/arangodb/ArticleRepositoryLiveTest.java +++ b/persistence-modules/spring-data-arangodb/src/test/java/com/baeldung/arangodb/ArticleRepositoryLiveTest.java @@ -2,6 +2,7 @@ package com.baeldung.arangodb; import com.baeldung.arangodb.model.Article; import com.baeldung.arangodb.repository.ArticleRepository; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -25,6 +26,11 @@ public class ArticleRepositoryLiveTest { @Autowired ArticleRepository articleRepository; + @AfterEach + public void tearDown(){ + articleRepository.deleteAll(); + } + @Test public void givenNewArticle_whenSaveInArangoDb_thenDataIsCorrect() { Article newArticle = new Article( From e41a8d5cc875250dc0e4271ca6085a3c0c45e7c9 Mon Sep 17 00:00:00 2001 From: Rafael Lopez Date: Fri, 1 Oct 2021 14:21:15 -0400 Subject: [PATCH 114/118] JAVA-6406: Update 'Exploring the New HTTP Client in Java' article --- .../core-java-9-new-features/README.md | 1 - .../httpclient/HttpClientExample.java | 84 ------- .../com.baeldung.httpclient/module-info.java | 3 - .../httpclient/HttpClientIntegrationTest.java | 218 ------------------ .../HttpRequestIntegrationTest.java | 171 -------------- .../HttpResponseIntegrationTest.java | 55 ----- 6 files changed, 532 deletions(-) delete mode 100644 core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java delete mode 100644 core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/module-info.java delete mode 100644 core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientIntegrationTest.java delete mode 100644 core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestIntegrationTest.java delete mode 100644 core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseIntegrationTest.java diff --git a/core-java-modules/core-java-9-new-features/README.md b/core-java-modules/core-java-9-new-features/README.md index 4045a37d9d..09890bb677 100644 --- a/core-java-modules/core-java-9-new-features/README.md +++ b/core-java-modules/core-java-9-new-features/README.md @@ -6,7 +6,6 @@ This module contains articles about core Java features that have been introduced - [New Features in Java 9](https://www.baeldung.com/new-java-9) - [Java 9 Variable Handles Demystified](http://www.baeldung.com/java-variable-handles) -- [Exploring the New HTTP Client in Java 9 and 11](http://www.baeldung.com/java-9-http-client) - [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar) - [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation) - [Introduction to Java 9 StackWalking API](https://www.baeldung.com/java-9-stackwalking-api) diff --git a/core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java b/core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java deleted file mode 100644 index de08c2164e..0000000000 --- a/core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package com.baeldung.httpclient; - -import java.io.File; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.stream.Collectors; -import jdk.incubator.http.HttpClient; -import jdk.incubator.http.HttpRequest; -import jdk.incubator.http.HttpRequest.BodyProcessor; -import jdk.incubator.http.HttpResponse; -import jdk.incubator.http.HttpResponse.BodyHandler; - -/** - * - * @author pkaria - */ -public class HttpClientExample { - - public static void main(String[] args) throws Exception { - httpGetRequest(); - httpPostRequest(); - asynchronousRequest(); - asynchronousMultipleRequests(); - } - - public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException { - HttpClient client = HttpClient.newHttpClient(); - URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1"); - HttpRequest request = HttpRequest.newBuilder(httpURI).GET() - .headers("Accept-Enconding", "gzip, deflate").build(); - HttpResponse response = client.send(request, HttpResponse.BodyHandler.asString()); - String responseBody = response.body(); - int responseStatusCode = response.statusCode(); - System.out.println(responseBody); - } - - public static void httpPostRequest() throws URISyntaxException, IOException, InterruptedException { - HttpClient client = HttpClient - .newBuilder() - .build(); - HttpRequest request = HttpRequest - .newBuilder(new URI("http://jsonplaceholder.typicode.com/posts")) - .POST(BodyProcessor.fromString("Sample Post Request")) - .build(); - HttpResponse response - = client.send(request, HttpResponse.BodyHandler.asString()); - String responseBody = response.body(); - System.out.println(responseBody); - } - - public static void asynchronousRequest() throws URISyntaxException { - HttpClient client = HttpClient.newHttpClient(); - URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1"); - HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build(); - CompletableFuture> futureResponse = client.sendAsync(request, - HttpResponse.BodyHandler.asString()); - } - - public static void asynchronousMultipleRequests() throws URISyntaxException { - List targets = Arrays.asList(new URI("http://jsonplaceholder.typicode.com/posts/1"), new URI("http://jsonplaceholder.typicode.com/posts/2")); - HttpClient client = HttpClient.newHttpClient(); - List> futures = targets - .stream() - .map(target -> client - .sendAsync( - HttpRequest.newBuilder(target) - .GET() - .build(), - BodyHandler.asFile(Paths.get("base", target.getPath()))) - .thenApply(response -> response.body()) - .thenApply(path -> path.toFile())) - .collect(Collectors.toList()); - } -} diff --git a/core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/module-info.java b/core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/module-info.java deleted file mode 100644 index 205c9ea725..0000000000 --- a/core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/module-info.java +++ /dev/null @@ -1,3 +0,0 @@ -module com.baeldung.httpclient { - requires jdk.incubator.httpclient; -} diff --git a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientIntegrationTest.java b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientIntegrationTest.java deleted file mode 100644 index fc59ae8d8d..0000000000 --- a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientIntegrationTest.java +++ /dev/null @@ -1,218 +0,0 @@ -package com.baeldung.java9.httpclient; - -import jdk.incubator.http.HttpClient; -import jdk.incubator.http.HttpRequest; -import jdk.incubator.http.HttpResponse; -import org.junit.Test; - -import java.io.IOException; -import java.net.*; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.stream.Collectors; - -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.collection.IsEmptyCollection.empty; -import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertThat; - -/** - * Created by adam. - */ -public class HttpClientIntegrationTest { - - @Test - public void shouldReturnSampleDataContentWhenConnectViaSystemProxy() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .headers("Content-Type", "text/plain;charset=UTF-8") - .POST(HttpRequest.BodyProcessor.fromString("Sample body")) - .build(); - - HttpResponse response = HttpClient.newBuilder() - .proxy(ProxySelector.getDefault()) - .build() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.body(), containsString("Sample body")); - } - - @Test - public void shouldNotFollowRedirectWhenSetToDefaultNever() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("http://stackoverflow.com")) - .version(HttpClient.Version.HTTP_1_1) - .GET() - .build(); - HttpResponse response = HttpClient.newBuilder() - .build() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_MOVED_PERM)); - assertThat(response.body(), containsString("")); - } - - @Test - public void shouldFollowRedirectWhenSetToAlways() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("http://stackoverflow.com")) - .version(HttpClient.Version.HTTP_1_1) - .GET() - .build(); - HttpResponse response = HttpClient.newBuilder() - .followRedirects(HttpClient.Redirect.ALWAYS) - .build() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.finalRequest() - .uri() - .toString(), equalTo("https://stackoverflow.com/")); - } - - @Test - public void shouldReturnOKStatusForAuthenticatedAccess() throws URISyntaxException, IOException, InterruptedException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/basic-auth")) - .GET() - .build(); - HttpResponse response = HttpClient.newBuilder() - .authenticator(new Authenticator() { - @Override - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication("postman", "password".toCharArray()); - } - }) - .build() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldSendRequestAsync() throws URISyntaxException, InterruptedException, ExecutionException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .headers("Content-Type", "text/plain;charset=UTF-8") - .POST(HttpRequest.BodyProcessor.fromString("Sample body")) - .build(); - CompletableFuture> response = HttpClient.newBuilder() - .build() - .sendAsync(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.get() - .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldUseJustTwoThreadWhenProcessingSendAsyncRequest() throws URISyntaxException, InterruptedException, ExecutionException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .GET() - .build(); - - ExecutorService executorService = Executors.newFixedThreadPool(2); - - CompletableFuture> response1 = HttpClient.newBuilder() - .executor(executorService) - .build() - .sendAsync(request, HttpResponse.BodyHandler.asString()); - - CompletableFuture> response2 = HttpClient.newBuilder() - .executor(executorService) - .build() - .sendAsync(request, HttpResponse.BodyHandler.asString()); - - CompletableFuture> response3 = HttpClient.newBuilder() - .executor(executorService) - .build() - .sendAsync(request, HttpResponse.BodyHandler.asString()); - - CompletableFuture.allOf(response1, response2, response3) - .join(); - - assertThat(response1.get() - .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response2.get() - .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response3.get() - .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldNotStoreCookieWhenPolicyAcceptNone() throws URISyntaxException, IOException, InterruptedException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .GET() - .build(); - - HttpClient httpClient = HttpClient.newBuilder() - .cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_NONE)) - .build(); - - httpClient.send(request, HttpResponse.BodyHandler.asString()); - - assertThat(httpClient.cookieManager() - .get() - .getCookieStore() - .getCookies(), empty()); - } - - @Test - public void shouldStoreCookieWhenPolicyAcceptAll() throws URISyntaxException, IOException, InterruptedException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .GET() - .build(); - - HttpClient httpClient = HttpClient.newBuilder() - .cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_ALL)) - .build(); - - httpClient.send(request, HttpResponse.BodyHandler.asString()); - - assertThat(httpClient.cookieManager() - .get() - .getCookieStore() - .getCookies(), not(empty())); - } - - @Test - public void shouldProcessMultipleRequestViaStream() throws URISyntaxException, ExecutionException, InterruptedException { - List targets = Arrays.asList(new URI("https://postman-echo.com/get?foo1=bar1"), new URI("https://postman-echo.com/get?foo2=bar2")); - - HttpClient client = HttpClient.newHttpClient(); - - List> futures = targets.stream() - .map(target -> client.sendAsync(HttpRequest.newBuilder(target) - .GET() - .build(), HttpResponse.BodyHandler.asString()) - .thenApply(response -> response.body())) - .collect(Collectors.toList()); - - CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) - .join(); - - if (futures.get(0) - .get() - .contains("foo1")) { - assertThat(futures.get(0) - .get(), containsString("bar1")); - assertThat(futures.get(1) - .get(), containsString("bar2")); - } else { - assertThat(futures.get(1) - .get(), containsString("bar2")); - assertThat(futures.get(1) - .get(), containsString("bar1")); - } - - } - -} diff --git a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestIntegrationTest.java b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestIntegrationTest.java deleted file mode 100644 index 17af7bd8ba..0000000000 --- a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestIntegrationTest.java +++ /dev/null @@ -1,171 +0,0 @@ -package com.baeldung.java9.httpclient; - -import jdk.incubator.http.HttpClient; -import jdk.incubator.http.HttpRequest; -import jdk.incubator.http.HttpResponse; -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.URI; -import java.net.URISyntaxException; -import java.nio.file.Paths; -import java.security.NoSuchAlgorithmException; -import java.time.Duration; - -import static java.time.temporal.ChronoUnit.SECONDS; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; - -/** - * Created by adam. - */ -public class HttpRequestIntegrationTest { - - @Test - public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .GET() - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldUseHttp2WhenWebsiteUsesHttp2() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://stackoverflow.com")) - .version(HttpClient.Version.HTTP_2) - .GET() - .build(); - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.version(), equalTo(HttpClient.Version.HTTP_2)); - } - - @Test - public void shouldFallbackToHttp1_1WhenWebsiteDoesNotUseHttp2() throws IOException, InterruptedException, URISyntaxException, NoSuchAlgorithmException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .version(HttpClient.Version.HTTP_2) - .GET() - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.version(), equalTo(HttpClient.Version.HTTP_1_1)); - } - - @Test - public void shouldReturnStatusOKWhenSendGetRequestWithDummyHeaders() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .headers("key1", "value1", "key2", "value2") - .GET() - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldReturnStatusOKWhenSendGetRequestTimeoutSet() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .timeout(Duration.of(10, SECONDS)) - .GET() - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldReturnNoContentWhenPostWithNoBody() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .POST(HttpRequest.noBody()) - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldReturnSampleDataContentWhenPostWithBodyText() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .headers("Content-Type", "text/plain;charset=UTF-8") - .POST(HttpRequest.BodyProcessor.fromString("Sample request body")) - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.body(), containsString("Sample request body")); - } - - @Test - public void shouldReturnSampleDataContentWhenPostWithInputStream() throws IOException, InterruptedException, URISyntaxException { - byte[] sampleData = "Sample request body".getBytes(); - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .headers("Content-Type", "text/plain;charset=UTF-8") - .POST(HttpRequest.BodyProcessor.fromInputStream(() -> new ByteArrayInputStream(sampleData))) - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.body(), containsString("Sample request body")); - } - - @Test - public void shouldReturnSampleDataContentWhenPostWithByteArrayProcessorStream() throws IOException, InterruptedException, URISyntaxException { - byte[] sampleData = "Sample request body".getBytes(); - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .headers("Content-Type", "text/plain;charset=UTF-8") - .POST(HttpRequest.BodyProcessor.fromByteArray(sampleData)) - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.body(), containsString("Sample request body")); - } - - @Test - public void shouldReturnSampleDataContentWhenPostWithFileProcessorStream() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .headers("Content-Type", "text/plain;charset=UTF-8") - .POST(HttpRequest.BodyProcessor.fromFile(Paths.get("src/test/resources/sample.txt"))) - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.body(), containsString("Sample file content")); - } - -} diff --git a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseIntegrationTest.java b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseIntegrationTest.java deleted file mode 100644 index 5c6f9c8a52..0000000000 --- a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseIntegrationTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.baeldung.java9.httpclient; - -import jdk.incubator.http.HttpClient; -import jdk.incubator.http.HttpRequest; -import jdk.incubator.http.HttpResponse; -import org.junit.Test; - -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.URI; -import java.net.URISyntaxException; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.Matchers.isEmptyString; -import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertThat; - -/** - * Created by adam. - */ -public class HttpResponseIntegrationTest { - - @Test - public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .GET() - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.body(), not(isEmptyString())); - } - - @Test - public void shouldResponseURIDifferentThanRequestUIRWhenRedirect() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("http://stackoverflow.com")) - .version(HttpClient.Version.HTTP_1_1) - .GET() - .build(); - HttpResponse response = HttpClient.newBuilder() - .followRedirects(HttpClient.Redirect.ALWAYS) - .build() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(request.uri() - .toString(), equalTo("http://stackoverflow.com")); - assertThat(response.uri() - .toString(), equalTo("https://stackoverflow.com/")); - } - -} From 3fce5f424d492d1003bc16485d55dd3caa7092e1 Mon Sep 17 00:00:00 2001 From: Shashank Date: Sat, 2 Oct 2021 21:33:59 +0530 Subject: [PATCH 115/118] feat: removing deprecated view resolver classes and configuring BeanNameViewResolver --- spring-web-modules/spring-mvc-basics/pom.xml | 1 - .../baeldung/spring/web/config/WebConfig.java | 35 +++++++++++-------- .../src/main/resources/views.properties | 3 -- .../src/main/resources/views.xml | 10 ------ 4 files changed, 20 insertions(+), 29 deletions(-) delete mode 100644 spring-web-modules/spring-mvc-basics/src/main/resources/views.properties delete mode 100644 spring-web-modules/spring-mvc-basics/src/main/resources/views.xml diff --git a/spring-web-modules/spring-mvc-basics/pom.xml b/spring-web-modules/spring-mvc-basics/pom.xml index 9fe4494393..a91f4004ff 100644 --- a/spring-web-modules/spring-mvc-basics/pom.xml +++ b/spring-web-modules/spring-mvc-basics/pom.xml @@ -33,7 +33,6 @@ org.apache.tomcat.embed tomcat-embed-jasper - provided javax.servlet diff --git a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/spring/web/config/WebConfig.java b/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/spring/web/config/WebConfig.java index ac917018b0..e1987f8345 100644 --- a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/spring/web/config/WebConfig.java +++ b/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/spring/web/config/WebConfig.java @@ -1,13 +1,11 @@ package com.baeldung.spring.web.config; -import java.io.IOException; - import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.ClassPathResource; import org.springframework.http.MediaType; import org.springframework.ui.context.support.ResourceBundleThemeSource; import org.springframework.web.multipart.commons.CommonsMultipartResolver; +import org.springframework.web.servlet.View; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; @@ -17,10 +15,11 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.resource.PathResourceResolver; import org.springframework.web.servlet.theme.CookieThemeResolver; import org.springframework.web.servlet.theme.ThemeChangeInterceptor; +import org.springframework.web.servlet.view.BeanNameViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; -import org.springframework.web.servlet.view.ResourceBundleViewResolver; -import org.springframework.web.servlet.view.XmlViewResolver; + +import java.io.IOException; //@EnableWebMvc //@ComponentScan(basePackages = { "com.baeldung.web.controller" }) @@ -93,19 +92,25 @@ public class WebConfig implements WebMvcConfigurer { /** END theme configuration */ @Bean - public ViewResolver resourceBundleViewResolver() { - final ResourceBundleViewResolver bean = new ResourceBundleViewResolver(); - bean.setBasename("views"); - bean.setOrder(0); - return bean; + public BeanNameViewResolver beanNameViewResolver(){ + BeanNameViewResolver beanNameViewResolver = new BeanNameViewResolver(); + beanNameViewResolver.setOrder(1); + return beanNameViewResolver; } @Bean - public ViewResolver xmlViewResolver() { - final XmlViewResolver bean = new XmlViewResolver(); - bean.setLocation(new ClassPathResource("views.xml")); - bean.setOrder(1); - return bean; + public View sample() { + return new JstlView("/WEB-INF/view/sample.jsp"); + } + + @Bean + public View sample2() { + return new JstlView("/WEB-INF/view2/sample2.jsp"); + } + + @Bean + public View sample3(){ + return new JstlView("/WEB-INF/view3/sample3.jsp"); } /** diff --git a/spring-web-modules/spring-mvc-basics/src/main/resources/views.properties b/spring-web-modules/spring-mvc-basics/src/main/resources/views.properties deleted file mode 100644 index 06d042b446..0000000000 --- a/spring-web-modules/spring-mvc-basics/src/main/resources/views.properties +++ /dev/null @@ -1,3 +0,0 @@ -sample2.(class)=org.springframework.web.servlet.view.JstlView -sample2.url=/WEB-INF/view2/sample2.jsp - diff --git a/spring-web-modules/spring-mvc-basics/src/main/resources/views.xml b/spring-web-modules/spring-mvc-basics/src/main/resources/views.xml deleted file mode 100644 index a44d3deae4..0000000000 --- a/spring-web-modules/spring-mvc-basics/src/main/resources/views.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - \ No newline at end of file From 6781cea471bb66c2634423e36f4a1fca78b84286 Mon Sep 17 00:00:00 2001 From: mbarriola <85458535+mbarriola@users.noreply.github.com> Date: Sun, 3 Oct 2021 22:57:34 -0400 Subject: [PATCH 116/118] Bael 5121 error handling (#11281) * Commit source code to branch * BAEL-5065 improvement of groupBy with complex key * Fixed indentation --- .../grpc/errorhandling/CommodityClient.java | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/grpc/src/main/java/com/baeldung/grpc/errorhandling/CommodityClient.java b/grpc/src/main/java/com/baeldung/grpc/errorhandling/CommodityClient.java index 9821df9182..8680abe6f3 100644 --- a/grpc/src/main/java/com/baeldung/grpc/errorhandling/CommodityClient.java +++ b/grpc/src/main/java/com/baeldung/grpc/errorhandling/CommodityClient.java @@ -29,47 +29,47 @@ public class CommodityClient { nonBlockingStub = CommodityPriceProviderGrpc.newStub(channel); } - public void getBidirectionalCommodityPriceLists() throws InterruptedException { logger.info("#######START EXAMPLE#######: BidirectionalStreaming - getCommodityPriceLists from list of commodities"); final CountDownLatch finishLatch = new CountDownLatch(1); StreamObserver responseObserver = new StreamObserver() { @Override -public void onNext(StreamingCommodityQuote streamingCommodityQuote) { + public void onNext(StreamingCommodityQuote streamingCommodityQuote) { - switch (streamingCommodityQuote.getMessageCase()) { - case COMODITY_QUOTE: - CommodityQuote commodityQuote = streamingCommodityQuote.getComodityQuote(); - logger.info("RESPONSE producer:" + commodityQuote.getCommodityName() + " price:" + commodityQuote.getPrice()); - break; - case STATUS: - com.google.rpc.Status status = streamingCommodityQuote.getStatus(); - logger.info("RESPONSE status error:"); - logger.info("Status code:" + Code.forNumber(status.getCode())); - logger.info("Status message:" + status.getMessage()); - for (Any any : status.getDetailsList()) { - if (any.is(ErrorInfo.class)) { - ErrorInfo errorInfo; - try { - errorInfo = any.unpack(ErrorInfo.class); - logger.info("Reason:" + errorInfo.getReason()); - logger.info("Domain:" + errorInfo.getDomain()); - logger.info("Insert Token:" + errorInfo.getMetadataMap().get("insertToken")); - } catch (InvalidProtocolBufferException e) { - logger.error(e.getMessage()); + switch (streamingCommodityQuote.getMessageCase()) { + case COMODITY_QUOTE: + CommodityQuote commodityQuote = streamingCommodityQuote.getComodityQuote(); + logger.info("RESPONSE producer:" + commodityQuote.getCommodityName() + " price:" + commodityQuote.getPrice()); + break; + case STATUS: + com.google.rpc.Status status = streamingCommodityQuote.getStatus(); + logger.info("RESPONSE status error:"); + logger.info("Status code:" + Code.forNumber(status.getCode())); + logger.info("Status message:" + status.getMessage()); + for (Any any : status.getDetailsList()) { + if (any.is(ErrorInfo.class)) { + ErrorInfo errorInfo; + try { + errorInfo = any.unpack(ErrorInfo.class); + logger.info("Reason:" + errorInfo.getReason()); + logger.info("Domain:" + errorInfo.getDomain()); + logger.info("Insert Token:" + errorInfo.getMetadataMap() + .get("insertToken")); + } catch (InvalidProtocolBufferException e) { + logger.error(e.getMessage()); + } + } + } + break; + default: + logger.info("Unknow message case"); } } - } - break; - default: - logger.info("Unknow message case"); - } -} @Override public void onCompleted() { - logger.info("Finished getBidirectionalCommodityPriceListss"); + logger.info("Finished getBidirectionalCommodityPriceLists"); finishLatch.countDown(); } From a5bcd85b16d73cedf348abb9a02ccb45075b0599 Mon Sep 17 00:00:00 2001 From: freelansam <79205526+freelansam@users.noreply.github.com> Date: Tue, 5 Oct 2021 01:08:39 +0530 Subject: [PATCH 117/118] JAVA-7433: Fix references to parents (#11275) --- annotations/annotation-processing/pom.xml | 1 - annotations/annotation-user/pom.xml | 1 - core-java-modules/core-java-8-2/pom.xml | 1 - core-java-modules/core-java-8-datetime-2/pom.xml | 1 - core-java-modules/core-java-8-datetime/pom.xml | 1 - core-java-modules/core-java-8/pom.xml | 1 - core-java-modules/core-java-annotations/pom.xml | 1 - core-java-modules/core-java-arrays-sorting/pom.xml | 1 - core-java-modules/core-java-char/pom.xml | 1 - core-java-modules/core-java-collections-2/pom.xml | 1 - core-java-modules/core-java-collections-3/pom.xml | 1 - core-java-modules/core-java-collections-4/pom.xml | 1 - core-java-modules/core-java-collections-array-list/pom.xml | 1 - core-java-modules/core-java-collections-list-2/pom.xml | 1 - core-java-modules/core-java-collections-list-3/pom.xml | 1 - core-java-modules/core-java-collections-list/pom.xml | 1 - core-java-modules/core-java-collections-maps-2/pom.xml | 1 - core-java-modules/core-java-collections-maps-3/pom.xml | 1 - core-java-modules/core-java-collections-maps/pom.xml | 1 - core-java-modules/core-java-collections/pom.xml | 1 - core-java-modules/core-java-concurrency-2/pom.xml | 1 - core-java-modules/core-java-concurrency-advanced-2/pom.xml | 1 - core-java-modules/core-java-concurrency-advanced-3/pom.xml | 1 - core-java-modules/core-java-concurrency-advanced-4/pom.xml | 1 - core-java-modules/core-java-concurrency-advanced/pom.xml | 1 - core-java-modules/core-java-concurrency-basic-2/pom.xml | 1 - core-java-modules/core-java-concurrency-basic/pom.xml | 1 - core-java-modules/core-java-concurrency-collections-2/pom.xml | 1 - core-java-modules/core-java-concurrency-collections/pom.xml | 1 - core-java-modules/core-java-console/pom.xml | 1 - core-java-modules/core-java-date-operations-2/pom.xml | 1 - core-java-modules/core-java-exceptions-2/pom.xml | 1 - core-java-modules/core-java-exceptions-3/pom.xml | 1 - core-java-modules/core-java-exceptions/pom.xml | 1 - core-java-modules/core-java-function/pom.xml | 1 - core-java-modules/core-java-functional/pom.xml | 1 - core-java-modules/core-java-io-2/pom.xml | 1 - core-java-modules/core-java-io-3/pom.xml | 1 - core-java-modules/core-java-io-4/pom.xml | 1 - core-java-modules/core-java-io-apis/pom.xml | 1 - core-java-modules/core-java-io-conversions/pom.xml | 1 - core-java-modules/core-java-jar/pom.xml | 1 - core-java-modules/core-java-jvm-2/pom.xml | 1 - core-java-modules/core-java-lambdas/pom.xml | 1 - core-java-modules/core-java-lang-2/pom.xml | 1 - core-java-modules/core-java-lang-3/pom.xml | 1 - core-java-modules/core-java-lang-4/pom.xml | 1 - core-java-modules/core-java-lang-math-2/pom.xml | 1 - core-java-modules/core-java-lang-math-3/pom.xml | 1 - core-java-modules/core-java-lang-math/pom.xml | 1 - core-java-modules/core-java-lang-operators-2/pom.xml | 1 - core-java-modules/core-java-lang-operators/pom.xml | 1 - core-java-modules/core-java-lang-syntax-2/pom.xml | 1 - core-java-modules/core-java-lang-syntax/pom.xml | 1 - core-java-modules/core-java-lang/pom.xml | 1 - core-java-modules/core-java-networking-2/pom.xml | 1 - core-java-modules/core-java-networking-3/pom.xml | 1 - core-java-modules/core-java-networking/pom.xml | 1 - core-java-modules/core-java-nio-2/pom.xml | 1 - core-java-modules/core-java-nio/pom.xml | 1 - core-java-modules/core-java-perf/pom.xml | 1 - core-java-modules/core-java-reflection-2/pom.xml | 1 - core-java-modules/core-java-reflection/pom.xml | 1 - core-java-modules/core-java-regex-2/pom.xml | 1 - core-java-modules/core-java-regex/pom.xml | 1 - core-java-modules/core-java-security-2/pom.xml | 1 - core-java-modules/core-java-security/pom.xml | 1 - core-java-modules/core-java-streams-2/pom.xml | 1 - core-java-modules/core-java-streams-3/pom.xml | 1 - core-java-modules/core-java-streams-4/pom.xml | 1 - core-java-modules/core-java-streams/pom.xml | 1 - core-java-modules/core-java-string-algorithms-2/pom.xml | 1 - core-java-modules/core-java-string-algorithms-3/pom.xml | 1 - core-java-modules/core-java-string-algorithms/pom.xml | 1 - core-java-modules/core-java-string-apis/pom.xml | 1 - core-java-modules/core-java-string-conversions-2/pom.xml | 1 - core-java-modules/core-java-string-conversions/pom.xml | 1 - core-java-modules/core-java-string-operations-2/pom.xml | 1 - core-java-modules/core-java-string-operations/pom.xml | 1 - core-java-modules/core-java/pom.xml | 1 - 80 files changed, 80 deletions(-) diff --git a/annotations/annotation-processing/pom.xml b/annotations/annotation-processing/pom.xml index 59dffb963d..14bbc409e5 100644 --- a/annotations/annotation-processing/pom.xml +++ b/annotations/annotation-processing/pom.xml @@ -10,7 +10,6 @@ com.baeldung 1.0.0-SNAPSHOT annotations - ../ diff --git a/annotations/annotation-user/pom.xml b/annotations/annotation-user/pom.xml index 4743f22e95..37a2e36f61 100644 --- a/annotations/annotation-user/pom.xml +++ b/annotations/annotation-user/pom.xml @@ -10,7 +10,6 @@ com.baeldung annotations 1.0.0-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-8-2/pom.xml b/core-java-modules/core-java-8-2/pom.xml index f3e60f8d5f..af26289db8 100644 --- a/core-java-modules/core-java-8-2/pom.xml +++ b/core-java-modules/core-java-8-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-8-datetime-2/pom.xml b/core-java-modules/core-java-8-datetime-2/pom.xml index a5a0417374..e662ba400c 100644 --- a/core-java-modules/core-java-8-datetime-2/pom.xml +++ b/core-java-modules/core-java-8-datetime-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-8-datetime/pom.xml b/core-java-modules/core-java-8-datetime/pom.xml index a79d5d089b..f58557df7f 100644 --- a/core-java-modules/core-java-8-datetime/pom.xml +++ b/core-java-modules/core-java-8-datetime/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-8/pom.xml b/core-java-modules/core-java-8/pom.xml index a7a2a1a0f8..987ba2e568 100644 --- a/core-java-modules/core-java-8/pom.xml +++ b/core-java-modules/core-java-8/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-annotations/pom.xml b/core-java-modules/core-java-annotations/pom.xml index 6c35aea668..a718c43eef 100644 --- a/core-java-modules/core-java-annotations/pom.xml +++ b/core-java-modules/core-java-annotations/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-arrays-sorting/pom.xml b/core-java-modules/core-java-arrays-sorting/pom.xml index 8c55204347..e9946d46ed 100644 --- a/core-java-modules/core-java-arrays-sorting/pom.xml +++ b/core-java-modules/core-java-arrays-sorting/pom.xml @@ -11,7 +11,6 @@ core-java-modules com.baeldung.core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-char/pom.xml b/core-java-modules/core-java-char/pom.xml index 23156b5a22..009197a1d0 100644 --- a/core-java-modules/core-java-char/pom.xml +++ b/core-java-modules/core-java-char/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-collections-2/pom.xml b/core-java-modules/core-java-collections-2/pom.xml index ad76990af3..4e171eed48 100644 --- a/core-java-modules/core-java-collections-2/pom.xml +++ b/core-java-modules/core-java-collections-2/pom.xml @@ -11,7 +11,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-collections-3/pom.xml b/core-java-modules/core-java-collections-3/pom.xml index 40f1867738..4ca4bda1ee 100644 --- a/core-java-modules/core-java-collections-3/pom.xml +++ b/core-java-modules/core-java-collections-3/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-collections-4/pom.xml b/core-java-modules/core-java-collections-4/pom.xml index 9dc26cd5d5..d86b04644c 100644 --- a/core-java-modules/core-java-collections-4/pom.xml +++ b/core-java-modules/core-java-collections-4/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../pom.xml diff --git a/core-java-modules/core-java-collections-array-list/pom.xml b/core-java-modules/core-java-collections-array-list/pom.xml index 2e4ab30b8a..c14e59bac0 100644 --- a/core-java-modules/core-java-collections-array-list/pom.xml +++ b/core-java-modules/core-java-collections-array-list/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-collections-list-2/pom.xml b/core-java-modules/core-java-collections-list-2/pom.xml index abddeb9014..51e66fc0c2 100644 --- a/core-java-modules/core-java-collections-list-2/pom.xml +++ b/core-java-modules/core-java-collections-list-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-collections-list-3/pom.xml b/core-java-modules/core-java-collections-list-3/pom.xml index b05fc46928..efe79509c1 100644 --- a/core-java-modules/core-java-collections-list-3/pom.xml +++ b/core-java-modules/core-java-collections-list-3/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-collections-list/pom.xml b/core-java-modules/core-java-collections-list/pom.xml index f1b6bc3965..ae1e1561c6 100644 --- a/core-java-modules/core-java-collections-list/pom.xml +++ b/core-java-modules/core-java-collections-list/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-collections-maps-2/pom.xml b/core-java-modules/core-java-collections-maps-2/pom.xml index 3ae1437054..772cf30416 100644 --- a/core-java-modules/core-java-collections-maps-2/pom.xml +++ b/core-java-modules/core-java-collections-maps-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-collections-maps-3/pom.xml b/core-java-modules/core-java-collections-maps-3/pom.xml index 892d0443c5..c534c1d5ca 100644 --- a/core-java-modules/core-java-collections-maps-3/pom.xml +++ b/core-java-modules/core-java-collections-maps-3/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-collections-maps/pom.xml b/core-java-modules/core-java-collections-maps/pom.xml index 855f40c304..245c4b04bb 100644 --- a/core-java-modules/core-java-collections-maps/pom.xml +++ b/core-java-modules/core-java-collections-maps/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-collections/pom.xml b/core-java-modules/core-java-collections/pom.xml index 38513c889a..8fbc6e8de7 100644 --- a/core-java-modules/core-java-collections/pom.xml +++ b/core-java-modules/core-java-collections/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-concurrency-2/pom.xml b/core-java-modules/core-java-concurrency-2/pom.xml index 71c73f5dff..5196872e21 100644 --- a/core-java-modules/core-java-concurrency-2/pom.xml +++ b/core-java-modules/core-java-concurrency-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-concurrency-advanced-2/pom.xml b/core-java-modules/core-java-concurrency-advanced-2/pom.xml index f975dd41a4..93c23ccae7 100644 --- a/core-java-modules/core-java-concurrency-advanced-2/pom.xml +++ b/core-java-modules/core-java-concurrency-advanced-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-concurrency-advanced-3/pom.xml b/core-java-modules/core-java-concurrency-advanced-3/pom.xml index 7a85672779..915aa8d912 100644 --- a/core-java-modules/core-java-concurrency-advanced-3/pom.xml +++ b/core-java-modules/core-java-concurrency-advanced-3/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-concurrency-advanced-4/pom.xml b/core-java-modules/core-java-concurrency-advanced-4/pom.xml index 7f4a573664..5bd7ccfa07 100644 --- a/core-java-modules/core-java-concurrency-advanced-4/pom.xml +++ b/core-java-modules/core-java-concurrency-advanced-4/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-concurrency-advanced/pom.xml b/core-java-modules/core-java-concurrency-advanced/pom.xml index 52a2e67e70..3c21b49ae5 100644 --- a/core-java-modules/core-java-concurrency-advanced/pom.xml +++ b/core-java-modules/core-java-concurrency-advanced/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-concurrency-basic-2/pom.xml b/core-java-modules/core-java-concurrency-basic-2/pom.xml index 99c8cee0f2..9ca12da7fb 100644 --- a/core-java-modules/core-java-concurrency-basic-2/pom.xml +++ b/core-java-modules/core-java-concurrency-basic-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-concurrency-basic/pom.xml b/core-java-modules/core-java-concurrency-basic/pom.xml index 5df379efb2..7212a2dcb1 100644 --- a/core-java-modules/core-java-concurrency-basic/pom.xml +++ b/core-java-modules/core-java-concurrency-basic/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-concurrency-collections-2/pom.xml b/core-java-modules/core-java-concurrency-collections-2/pom.xml index 0a31c64728..8de0e1bef7 100644 --- a/core-java-modules/core-java-concurrency-collections-2/pom.xml +++ b/core-java-modules/core-java-concurrency-collections-2/pom.xml @@ -11,7 +11,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-concurrency-collections/pom.xml b/core-java-modules/core-java-concurrency-collections/pom.xml index edb5a71e39..f22da1c848 100644 --- a/core-java-modules/core-java-concurrency-collections/pom.xml +++ b/core-java-modules/core-java-concurrency-collections/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-console/pom.xml b/core-java-modules/core-java-console/pom.xml index 6f9288f1a5..673c5d4dff 100644 --- a/core-java-modules/core-java-console/pom.xml +++ b/core-java-modules/core-java-console/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-date-operations-2/pom.xml b/core-java-modules/core-java-date-operations-2/pom.xml index bb0435a92a..1d283851ca 100644 --- a/core-java-modules/core-java-date-operations-2/pom.xml +++ b/core-java-modules/core-java-date-operations-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-exceptions-2/pom.xml b/core-java-modules/core-java-exceptions-2/pom.xml index ce95553021..af7a778b23 100644 --- a/core-java-modules/core-java-exceptions-2/pom.xml +++ b/core-java-modules/core-java-exceptions-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-exceptions-3/pom.xml b/core-java-modules/core-java-exceptions-3/pom.xml index c4c3c00b56..bdee998e8d 100644 --- a/core-java-modules/core-java-exceptions-3/pom.xml +++ b/core-java-modules/core-java-exceptions-3/pom.xml @@ -13,7 +13,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-exceptions/pom.xml b/core-java-modules/core-java-exceptions/pom.xml index f2c6d001be..1f15dabe36 100644 --- a/core-java-modules/core-java-exceptions/pom.xml +++ b/core-java-modules/core-java-exceptions/pom.xml @@ -13,7 +13,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-function/pom.xml b/core-java-modules/core-java-function/pom.xml index 1cdcfe47db..cc44ba5a7c 100644 --- a/core-java-modules/core-java-function/pom.xml +++ b/core-java-modules/core-java-function/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-functional/pom.xml b/core-java-modules/core-java-functional/pom.xml index 3eeec71a5a..52d74035a5 100644 --- a/core-java-modules/core-java-functional/pom.xml +++ b/core-java-modules/core-java-functional/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ \ No newline at end of file diff --git a/core-java-modules/core-java-io-2/pom.xml b/core-java-modules/core-java-io-2/pom.xml index 19f34d6da1..924248f4f9 100644 --- a/core-java-modules/core-java-io-2/pom.xml +++ b/core-java-modules/core-java-io-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-io-3/pom.xml b/core-java-modules/core-java-io-3/pom.xml index f1b6412249..017b56f03f 100644 --- a/core-java-modules/core-java-io-3/pom.xml +++ b/core-java-modules/core-java-io-3/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-io-4/pom.xml b/core-java-modules/core-java-io-4/pom.xml index 8327764c9f..0501bb4a66 100644 --- a/core-java-modules/core-java-io-4/pom.xml +++ b/core-java-modules/core-java-io-4/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-io-apis/pom.xml b/core-java-modules/core-java-io-apis/pom.xml index 9d0f3196ab..f2a574ed89 100644 --- a/core-java-modules/core-java-io-apis/pom.xml +++ b/core-java-modules/core-java-io-apis/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-io-conversions/pom.xml b/core-java-modules/core-java-io-conversions/pom.xml index 2dd21a80e8..8d5a47a1ac 100644 --- a/core-java-modules/core-java-io-conversions/pom.xml +++ b/core-java-modules/core-java-io-conversions/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-jar/pom.xml b/core-java-modules/core-java-jar/pom.xml index ae2333d721..3c5a1b35bf 100644 --- a/core-java-modules/core-java-jar/pom.xml +++ b/core-java-modules/core-java-jar/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-jvm-2/pom.xml b/core-java-modules/core-java-jvm-2/pom.xml index f1d0a292c6..5bc5b5e3a5 100644 --- a/core-java-modules/core-java-jvm-2/pom.xml +++ b/core-java-modules/core-java-jvm-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-lambdas/pom.xml b/core-java-modules/core-java-lambdas/pom.xml index 87b315490a..f1e61ab8bf 100644 --- a/core-java-modules/core-java-lambdas/pom.xml +++ b/core-java-modules/core-java-lambdas/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-lang-2/pom.xml b/core-java-modules/core-java-lang-2/pom.xml index d32f382506..d1a8d68075 100644 --- a/core-java-modules/core-java-lang-2/pom.xml +++ b/core-java-modules/core-java-lang-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-lang-3/pom.xml b/core-java-modules/core-java-lang-3/pom.xml index c9005e51ae..0eda5dd16b 100644 --- a/core-java-modules/core-java-lang-3/pom.xml +++ b/core-java-modules/core-java-lang-3/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-lang-4/pom.xml b/core-java-modules/core-java-lang-4/pom.xml index 824194cc07..de67ddf55a 100644 --- a/core-java-modules/core-java-lang-4/pom.xml +++ b/core-java-modules/core-java-lang-4/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-lang-math-2/pom.xml b/core-java-modules/core-java-lang-math-2/pom.xml index c825ecdef8..4411d313db 100644 --- a/core-java-modules/core-java-lang-math-2/pom.xml +++ b/core-java-modules/core-java-lang-math-2/pom.xml @@ -11,7 +11,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-lang-math-3/pom.xml b/core-java-modules/core-java-lang-math-3/pom.xml index e1a650a18f..b9d09dba8c 100644 --- a/core-java-modules/core-java-lang-math-3/pom.xml +++ b/core-java-modules/core-java-lang-math-3/pom.xml @@ -11,7 +11,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-lang-math/pom.xml b/core-java-modules/core-java-lang-math/pom.xml index 0facdf2d8b..2cc9b90fa4 100644 --- a/core-java-modules/core-java-lang-math/pom.xml +++ b/core-java-modules/core-java-lang-math/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-lang-operators-2/pom.xml b/core-java-modules/core-java-lang-operators-2/pom.xml index 1e7e659e6d..1779b7384c 100644 --- a/core-java-modules/core-java-lang-operators-2/pom.xml +++ b/core-java-modules/core-java-lang-operators-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-lang-operators/pom.xml b/core-java-modules/core-java-lang-operators/pom.xml index 2d669bfa55..63f42917b8 100644 --- a/core-java-modules/core-java-lang-operators/pom.xml +++ b/core-java-modules/core-java-lang-operators/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-lang-syntax-2/pom.xml b/core-java-modules/core-java-lang-syntax-2/pom.xml index 9ca06e8fba..fdb0503174 100644 --- a/core-java-modules/core-java-lang-syntax-2/pom.xml +++ b/core-java-modules/core-java-lang-syntax-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-lang-syntax/pom.xml b/core-java-modules/core-java-lang-syntax/pom.xml index 70b0600580..da7d56de7b 100644 --- a/core-java-modules/core-java-lang-syntax/pom.xml +++ b/core-java-modules/core-java-lang-syntax/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-lang/pom.xml b/core-java-modules/core-java-lang/pom.xml index 6a14cab242..4989c6a5a7 100644 --- a/core-java-modules/core-java-lang/pom.xml +++ b/core-java-modules/core-java-lang/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-networking-2/pom.xml b/core-java-modules/core-java-networking-2/pom.xml index 893db2d85c..0035b9c0b7 100644 --- a/core-java-modules/core-java-networking-2/pom.xml +++ b/core-java-modules/core-java-networking-2/pom.xml @@ -11,7 +11,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-networking-3/pom.xml b/core-java-modules/core-java-networking-3/pom.xml index d3b2398b75..de2408ec0d 100644 --- a/core-java-modules/core-java-networking-3/pom.xml +++ b/core-java-modules/core-java-networking-3/pom.xml @@ -11,7 +11,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../pom.xml diff --git a/core-java-modules/core-java-networking/pom.xml b/core-java-modules/core-java-networking/pom.xml index 3e69e03dcb..9974134eae 100644 --- a/core-java-modules/core-java-networking/pom.xml +++ b/core-java-modules/core-java-networking/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-nio-2/pom.xml b/core-java-modules/core-java-nio-2/pom.xml index 0f4107d536..f9cf1f3060 100644 --- a/core-java-modules/core-java-nio-2/pom.xml +++ b/core-java-modules/core-java-nio-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-nio/pom.xml b/core-java-modules/core-java-nio/pom.xml index 1d267245a9..9e1c529a65 100644 --- a/core-java-modules/core-java-nio/pom.xml +++ b/core-java-modules/core-java-nio/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ \ No newline at end of file diff --git a/core-java-modules/core-java-perf/pom.xml b/core-java-modules/core-java-perf/pom.xml index 636de00d8f..f6f3ef795c 100644 --- a/core-java-modules/core-java-perf/pom.xml +++ b/core-java-modules/core-java-perf/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ \ No newline at end of file diff --git a/core-java-modules/core-java-reflection-2/pom.xml b/core-java-modules/core-java-reflection-2/pom.xml index 5b04b8e8db..75168936a7 100644 --- a/core-java-modules/core-java-reflection-2/pom.xml +++ b/core-java-modules/core-java-reflection-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-reflection/pom.xml b/core-java-modules/core-java-reflection/pom.xml index 67922eec5b..b28ecccb80 100644 --- a/core-java-modules/core-java-reflection/pom.xml +++ b/core-java-modules/core-java-reflection/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-regex-2/pom.xml b/core-java-modules/core-java-regex-2/pom.xml index 9e081c2228..a47c0ff357 100644 --- a/core-java-modules/core-java-regex-2/pom.xml +++ b/core-java-modules/core-java-regex-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-regex/pom.xml b/core-java-modules/core-java-regex/pom.xml index fc8c1a8385..3fb63fb42a 100644 --- a/core-java-modules/core-java-regex/pom.xml +++ b/core-java-modules/core-java-regex/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-security-2/pom.xml b/core-java-modules/core-java-security-2/pom.xml index 2520cee7f8..8de12c0c46 100644 --- a/core-java-modules/core-java-security-2/pom.xml +++ b/core-java-modules/core-java-security-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-security/pom.xml b/core-java-modules/core-java-security/pom.xml index 7b49d08a37..daba990776 100644 --- a/core-java-modules/core-java-security/pom.xml +++ b/core-java-modules/core-java-security/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-streams-2/pom.xml b/core-java-modules/core-java-streams-2/pom.xml index f875e910db..5f25a2d9fb 100644 --- a/core-java-modules/core-java-streams-2/pom.xml +++ b/core-java-modules/core-java-streams-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-streams-3/pom.xml b/core-java-modules/core-java-streams-3/pom.xml index 01b83f229a..b384ef7e0e 100644 --- a/core-java-modules/core-java-streams-3/pom.xml +++ b/core-java-modules/core-java-streams-3/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-streams-4/pom.xml b/core-java-modules/core-java-streams-4/pom.xml index 8cb3786776..110ec50b8c 100644 --- a/core-java-modules/core-java-streams-4/pom.xml +++ b/core-java-modules/core-java-streams-4/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-streams/pom.xml b/core-java-modules/core-java-streams/pom.xml index 3d5de7cebe..4862672c8a 100644 --- a/core-java-modules/core-java-streams/pom.xml +++ b/core-java-modules/core-java-streams/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-string-algorithms-2/pom.xml b/core-java-modules/core-java-string-algorithms-2/pom.xml index e263c1c79c..4b0d55508f 100644 --- a/core-java-modules/core-java-string-algorithms-2/pom.xml +++ b/core-java-modules/core-java-string-algorithms-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-string-algorithms-3/pom.xml b/core-java-modules/core-java-string-algorithms-3/pom.xml index 6d1c0b0e48..6376bfa677 100644 --- a/core-java-modules/core-java-string-algorithms-3/pom.xml +++ b/core-java-modules/core-java-string-algorithms-3/pom.xml @@ -11,7 +11,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-string-algorithms/pom.xml b/core-java-modules/core-java-string-algorithms/pom.xml index 0ad9ec4c66..9fea569b29 100644 --- a/core-java-modules/core-java-string-algorithms/pom.xml +++ b/core-java-modules/core-java-string-algorithms/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-string-apis/pom.xml b/core-java-modules/core-java-string-apis/pom.xml index b09d59fc10..6a382c3c57 100644 --- a/core-java-modules/core-java-string-apis/pom.xml +++ b/core-java-modules/core-java-string-apis/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-string-conversions-2/pom.xml b/core-java-modules/core-java-string-conversions-2/pom.xml index 700b4394c6..44968678f2 100644 --- a/core-java-modules/core-java-string-conversions-2/pom.xml +++ b/core-java-modules/core-java-string-conversions-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-string-conversions/pom.xml b/core-java-modules/core-java-string-conversions/pom.xml index ccbeb69768..1047e3e7c3 100644 --- a/core-java-modules/core-java-string-conversions/pom.xml +++ b/core-java-modules/core-java-string-conversions/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-string-operations-2/pom.xml b/core-java-modules/core-java-string-operations-2/pom.xml index b7187d2fdf..db8f78da70 100644 --- a/core-java-modules/core-java-string-operations-2/pom.xml +++ b/core-java-modules/core-java-string-operations-2/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java-string-operations/pom.xml b/core-java-modules/core-java-string-operations/pom.xml index 17b8562caf..67ce43277e 100644 --- a/core-java-modules/core-java-string-operations/pom.xml +++ b/core-java-modules/core-java-string-operations/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ diff --git a/core-java-modules/core-java/pom.xml b/core-java-modules/core-java/pom.xml index 504ec321f2..db2b1edc70 100644 --- a/core-java-modules/core-java/pom.xml +++ b/core-java-modules/core-java/pom.xml @@ -12,7 +12,6 @@ com.baeldung.core-java-modules core-java-modules 0.0.1-SNAPSHOT - ../ From 5cb50132f757cd4869a28eace6863fd6c948baa9 Mon Sep 17 00:00:00 2001 From: Mansoor Ali Date: Tue, 5 Oct 2021 07:00:20 +0500 Subject: [PATCH 118/118] BAEL-5118 (#11238) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * BAEL-5118 How to solve Hibernate “object references an unsaved transient instance” error * BAEL-5118 How to solve Hibernate “object references an unsaved transient instance” error - refactoring --- .../transientobject/HibernateUtil.java | 51 +++++++++++ .../transientobject/entity/Address.java | 74 ++++++++++++++++ .../transientobject/entity/Author.java | 71 ++++++++++++++++ .../transientobject/entity/Book.java | 72 ++++++++++++++++ .../transientobject/entity/Department.java | 70 ++++++++++++++++ .../transientobject/entity/Employee.java | 66 +++++++++++++++ .../transientobject/entity/User.java | 76 +++++++++++++++++ .../HibernateTransientObjectUnitTest.java | 84 +++++++++++++++++++ 8 files changed, 564 insertions(+) create mode 100644 persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/HibernateUtil.java create mode 100644 persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Address.java create mode 100644 persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Author.java create mode 100644 persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Book.java create mode 100644 persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Department.java create mode 100644 persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Employee.java create mode 100644 persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/User.java create mode 100644 persistence-modules/hibernate-exceptions/src/test/java/com/baeldung/hibernate/exception/transientobject/HibernateTransientObjectUnitTest.java diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/HibernateUtil.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/HibernateUtil.java new file mode 100644 index 0000000000..a40279661f --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/HibernateUtil.java @@ -0,0 +1,51 @@ +package com.baeldung.hibernate.exception.transientobject; + +import java.util.Properties; + +import com.baeldung.hibernate.exception.transientobject.entity.Address; +import com.baeldung.hibernate.exception.transientobject.entity.Department; +import com.baeldung.hibernate.exception.transientobject.entity.Author; +import com.baeldung.hibernate.exception.transientobject.entity.Book; +import com.baeldung.hibernate.exception.transientobject.entity.Employee; +import com.baeldung.hibernate.exception.transientobject.entity.User; +import org.hibernate.SessionFactory; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.Environment; +import org.hibernate.service.ServiceRegistry; + +public class HibernateUtil { + private static SessionFactory sessionFactory; + + public static SessionFactory getSessionFactory() { + if (sessionFactory == null) { + try { + Configuration configuration = new Configuration(); + Properties settings = new Properties(); + settings.put(Environment.DRIVER, "org.hsqldb.jdbcDriver"); + settings.put(Environment.URL, "jdbc:hsqldb:mem:transient"); + settings.put(Environment.USER, "sa"); + settings.put(Environment.PASS, ""); + settings.put(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect"); + settings.put(Environment.SHOW_SQL, "true"); + settings.put(Environment.USE_SQL_COMMENTS, "true"); + settings.put(Environment.HBM2DDL_AUTO, "update"); + configuration.setProperties(settings); + configuration.addAnnotatedClass(User.class); + configuration.addAnnotatedClass(Address.class); + configuration.addAnnotatedClass(Department.class); + configuration.addAnnotatedClass(Employee.class); + configuration.addAnnotatedClass(Book.class); + configuration.addAnnotatedClass(Author.class); + + + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() + .applySettings(configuration.getProperties()).build(); + sessionFactory = configuration.buildSessionFactory(serviceRegistry); + } catch (Exception e) { + e.printStackTrace(); + } + } + return sessionFactory; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Address.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Address.java new file mode 100644 index 0000000000..e450fc3c18 --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Address.java @@ -0,0 +1,74 @@ +package com.baeldung.hibernate.exception.transientobject.entity; + +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "address") +public class Address { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @Column(name = "city") + private String city; + + @Column(name = "street") + private String street; + + @OneToOne(mappedBy = "address") + private User user; + + public Address() { + } + + public Address(String city, String street) { + this.city = city; + this.street = street; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getStreet() { + return street; + } + + public void setStreet(String street) { + this.street = street; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Author.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Author.java new file mode 100644 index 0000000000..f1a88daa36 --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Author.java @@ -0,0 +1,71 @@ +package com.baeldung.hibernate.exception.transientobject.entity; + +import org.hibernate.annotations.Cascade; +import org.hibernate.annotations.CascadeType; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import java.util.HashSet; +import java.util.Set; + +@Entity +@Table(name = "author") +public class Author { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private int id; + + @Column(name = "name") + private String name; + + @ManyToMany + @Cascade({ CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.PERSIST}) + @JoinColumn(name = "book_id") + private Set books = new HashSet<>(); + + public void addBook(Book book) { + books.add(book); + } + + // standard getters and setters + + public Author() { + } + + public Author(String name) { + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } +} diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Book.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Book.java new file mode 100644 index 0000000000..91728430ea --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Book.java @@ -0,0 +1,72 @@ +package com.baeldung.hibernate.exception.transientobject.entity; + + +import org.hibernate.annotations.Cascade; +import org.hibernate.annotations.CascadeType; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToMany; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import java.util.HashSet; +import java.util.Set; + +@Entity +@Table(name = "book") +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private int id; + + @Column(name = "title") + private String title; + + @ManyToMany + @Cascade({ CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.PERSIST}) + @JoinColumn(name = "author_id") + private Set authors = new HashSet<>(); + + public void addAuthor(Author author) { + authors.add(author); + } + + // standard getters and setters + + public Book() { + } + + public Book(String title) { + this.title = title; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } +} diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Department.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Department.java new file mode 100644 index 0000000000..4b8fa69964 --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Department.java @@ -0,0 +1,70 @@ +package com.baeldung.hibernate.exception.transientobject.entity; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import java.util.HashSet; +import java.util.Set; + +@Entity +@Table(name = "department") +public class Department { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private int id; + + @Column(name = "name") + private String name; + + @OneToMany(mappedBy = "department", cascade = CascadeType.ALL, orphanRemoval = true) + private Set employees = new HashSet<>(); + + public void addEmployee(Employee employee) { + employees.add(employee); + } + + // standard getters and setters + + public Department() { + } + + public Department(int id) { + this.id = id; + } + + public Department(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Set getEmployees() { + return employees; + } + + public void setEmployees(Set employees) { + this.employees = employees; + } +} diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Employee.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Employee.java new file mode 100644 index 0000000000..56443cce70 --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/Employee.java @@ -0,0 +1,66 @@ +package com.baeldung.hibernate.exception.transientobject.entity; + + +import org.hibernate.annotations.Cascade; +import org.hibernate.annotations.CascadeType; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "employee") +public class Employee { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private int id; + + @Column(name = "name") + private String name; + + @ManyToOne + @Cascade(CascadeType.SAVE_UPDATE) + @JoinColumn(name = "department_id") + private Department department; + + // standard getters and setters + + public Employee() { + } + + public Employee(String name) { + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Department getDepartment() { + return department; + } + + public void setDepartment(Department department) { + this.department = department; + } +} diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/User.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/User.java new file mode 100644 index 0000000000..eff1a88b51 --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/transientobject/entity/User.java @@ -0,0 +1,76 @@ +package com.baeldung.hibernate.exception.transientobject.entity; + +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "user") +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private int id; + + @Column(name = "first_name") + private String firstName; + + @Column(name = "last_name") + private String lastName; + + @OneToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "address_id", referencedColumnName = "id") + private Address address; + + public User() { + } + + public User(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } +} diff --git a/persistence-modules/hibernate-exceptions/src/test/java/com/baeldung/hibernate/exception/transientobject/HibernateTransientObjectUnitTest.java b/persistence-modules/hibernate-exceptions/src/test/java/com/baeldung/hibernate/exception/transientobject/HibernateTransientObjectUnitTest.java new file mode 100644 index 0000000000..3f6d0effa9 --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/test/java/com/baeldung/hibernate/exception/transientobject/HibernateTransientObjectUnitTest.java @@ -0,0 +1,84 @@ +package com.baeldung.hibernate.exception.transientobject; + +import com.baeldung.hibernate.exception.transientobject.entity.Address; +import com.baeldung.hibernate.exception.transientobject.entity.Department; +import com.baeldung.hibernate.exception.transientobject.entity.Author; +import com.baeldung.hibernate.exception.transientobject.entity.Book; +import com.baeldung.hibernate.exception.transientobject.entity.Employee; +import com.baeldung.hibernate.exception.transientobject.entity.User; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class HibernateTransientObjectUnitTest { + + private static SessionFactory sessionFactory; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @BeforeClass + public static void init() { + sessionFactory = HibernateUtil.getSessionFactory(); + } + + @Test + public void whenSaveEntitiesWithOneToOneAssociation_thenSuccess() { + User user = new User("Bob", "Smith"); + Address address = new Address("London", "221b Baker Street"); + user.setAddress(address); + Session session = sessionFactory.openSession(); + session.beginTransaction(); + session.save(user); + session.getTransaction().commit(); + session.close(); + } + + @Test + public void whenSaveEntitiesWithOneToManyAssociation_thenSuccess() { + Department department = new Department(); + department.setName("IT Support"); + Employee employee = new Employee("John Doe"); + employee.setDepartment(department); + Session session = sessionFactory.openSession(); + session.beginTransaction(); + session.save(employee); + session.getTransaction().commit(); + session.close(); + } + + @Test + public void whenSaveEntitiesWithManyToManyAssociation_thenSuccess_1() { + Book book = new Book("Design Patterns: Elements of Reusable Object-Oriented Software"); + book.addAuthor(new Author("Erich Gamma")); + book.addAuthor(new Author("John Vlissides")); + book.addAuthor(new Author("Richard Helm")); + book.addAuthor(new Author("Ralph Johnson")); + Session session = sessionFactory.openSession(); + session.beginTransaction(); + session.save(book); + session.getTransaction().commit(); + session.close(); + } + + @Test + public void whenSaveEntitiesWithManyToManyAssociation_thenSuccess_2() { + Author author = new Author("Erich Gamma"); + author.addBook(new Book("Design Patterns: Elements of Reusable Object-Oriented Software")); + author.addBook(new Book("Introduction to Object Orient Design in C")); + Session session = sessionFactory.openSession(); + session.beginTransaction(); + session.save(author); + session.getTransaction().commit(); + session.close(); + } + + @AfterClass + public static void cleanUp() { + sessionFactory.close(); + } +}