diff --git a/algorithms-miscellaneous-1/README.md b/algorithms-miscellaneous-1/README.md index ea6d6f379b..479c2792f6 100644 --- a/algorithms-miscellaneous-1/README.md +++ b/algorithms-miscellaneous-1/README.md @@ -14,7 +14,5 @@ - [Calculate Factorial in Java](https://www.baeldung.com/java-calculate-factorial) - [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings) - [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters) -- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique) - [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations) -- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine) - [Generate Combinations in Java](https://www.baeldung.com/java-combinations-algorithm) diff --git a/algorithms-miscellaneous-2/README.md b/algorithms-miscellaneous-2/README.md index d693a44f66..de054566ed 100644 --- a/algorithms-miscellaneous-2/README.md +++ b/algorithms-miscellaneous-2/README.md @@ -8,9 +8,6 @@ - [Create a Sudoku Solver in Java](http://www.baeldung.com/java-sudoku) - [Displaying Money Amounts in Words](http://www.baeldung.com/java-money-into-words) - [A Collaborative Filtering Recommendation System in Java](http://www.baeldung.com/java-collaborative-filtering-recommendations) -- [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic) -- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity) -- [An Introduction to the Theory of Big-O Notation](http://www.baeldung.com/big-o-notation) - [Check If Two Rectangles Overlap In Java](https://www.baeldung.com/java-check-if-two-rectangles-overlap) - [Calculate the Distance Between Two Points in Java](https://www.baeldung.com/java-distance-between-two-points) - [Find the Intersection of Two Lines in Java](https://www.baeldung.com/java-intersection-of-two-lines) diff --git a/algorithms-miscellaneous-3/.gitignore b/algorithms-miscellaneous-3/.gitignore new file mode 100644 index 0000000000..30b2b7442c --- /dev/null +++ b/algorithms-miscellaneous-3/.gitignore @@ -0,0 +1,4 @@ +/target/ +.settings/ +.classpath +.project \ No newline at end of file diff --git a/algorithms-miscellaneous-3/README.md b/algorithms-miscellaneous-3/README.md new file mode 100644 index 0000000000..f299492d9c --- /dev/null +++ b/algorithms-miscellaneous-3/README.md @@ -0,0 +1,7 @@ +## Relevant articles: + +- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique) +- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine) +- [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic) +- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity) +- [An Introduction to the Theory of Big-O Notation](http://www.baeldung.com/big-o-notation) \ No newline at end of file diff --git a/algorithms-miscellaneous-3/pom.xml b/algorithms-miscellaneous-3/pom.xml new file mode 100644 index 0000000000..c4017144c8 --- /dev/null +++ b/algorithms-miscellaneous-3/pom.xml @@ -0,0 +1,39 @@ + + 4.0.0 + algorithms-miscellaneous-3 + 0.0.1-SNAPSHOT + algorithms-miscellaneous-3 + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.assertj + assertj-core + ${org.assertj.core.version} + test + + + + + + + + org.codehaus.mojo + exec-maven-plugin + ${exec-maven-plugin.version} + + + + + + + 3.9.0 + + + \ No newline at end of file diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/enumstatemachine/LeaveRequestState.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/enumstatemachine/LeaveRequestState.java similarity index 100% rename from algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/enumstatemachine/LeaveRequestState.java rename to algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/enumstatemachine/LeaveRequestState.java diff --git a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/romannumerals/RomanArabicConverter.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/romannumerals/RomanArabicConverter.java similarity index 100% rename from algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/romannumerals/RomanArabicConverter.java rename to algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/romannumerals/RomanArabicConverter.java diff --git a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/romannumerals/RomanNumeral.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/romannumerals/RomanNumeral.java similarity index 100% rename from algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/romannumerals/RomanNumeral.java rename to algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/romannumerals/RomanNumeral.java diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/twopointertechnique/LinkedListFindMiddle.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/twopointertechnique/LinkedListFindMiddle.java similarity index 100% rename from algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/twopointertechnique/LinkedListFindMiddle.java rename to algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/twopointertechnique/LinkedListFindMiddle.java diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/twopointertechnique/MyNode.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/twopointertechnique/MyNode.java similarity index 100% rename from algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/twopointertechnique/MyNode.java rename to algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/twopointertechnique/MyNode.java diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/twopointertechnique/RotateArray.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/twopointertechnique/RotateArray.java similarity index 100% rename from algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/twopointertechnique/RotateArray.java rename to algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/twopointertechnique/RotateArray.java diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/twopointertechnique/TwoSum.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/twopointertechnique/TwoSum.java similarity index 100% rename from algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/twopointertechnique/TwoSum.java rename to algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/twopointertechnique/TwoSum.java diff --git a/algorithms-miscellaneous-3/src/main/resources/logback.xml b/algorithms-miscellaneous-3/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/algorithms-miscellaneous-3/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/analysis/AnalysisRunnerLiveTest.java b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/analysis/AnalysisRunnerLiveTest.java similarity index 100% rename from algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/analysis/AnalysisRunnerLiveTest.java rename to algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/analysis/AnalysisRunnerLiveTest.java diff --git a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/enumstatemachine/LeaveRequestStateUnitTest.java b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/enumstatemachine/LeaveRequestStateUnitTest.java similarity index 100% rename from algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/enumstatemachine/LeaveRequestStateUnitTest.java rename to algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/enumstatemachine/LeaveRequestStateUnitTest.java diff --git a/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/romannumerals/RomanArabicConverterUnitTest.java b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/romannumerals/RomanArabicConverterUnitTest.java similarity index 100% rename from algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/romannumerals/RomanArabicConverterUnitTest.java rename to algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/romannumerals/RomanArabicConverterUnitTest.java diff --git a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/twopointertechnique/LinkedListFindMiddleUnitTest.java b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/twopointertechnique/LinkedListFindMiddleUnitTest.java similarity index 100% rename from algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/twopointertechnique/LinkedListFindMiddleUnitTest.java rename to algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/twopointertechnique/LinkedListFindMiddleUnitTest.java diff --git a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/twopointertechnique/RotateArrayUnitTest.java b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/twopointertechnique/RotateArrayUnitTest.java similarity index 100% rename from algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/twopointertechnique/RotateArrayUnitTest.java rename to algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/twopointertechnique/RotateArrayUnitTest.java diff --git a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/twopointertechnique/TwoSumUnitTest.java b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/twopointertechnique/TwoSumUnitTest.java similarity index 100% rename from algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/twopointertechnique/TwoSumUnitTest.java rename to algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/twopointertechnique/TwoSumUnitTest.java diff --git a/core-java-8-2/.gitignore b/core-java-8-2/.gitignore new file mode 100644 index 0000000000..374c8bf907 --- /dev/null +++ b/core-java-8-2/.gitignore @@ -0,0 +1,25 @@ +*.class + +0.* + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* +.resourceCache + +# Packaged files # +*.jar +*.war +*.ear + +# Files generated by integration tests +backup-pom.xml +/bin/ +/temp + +#IntelliJ specific +.idea/ +*.iml \ No newline at end of file diff --git a/core-java-8-2/pom.xml b/core-java-8-2/pom.xml index 7035f12fb7..4692a0fca6 100644 --- a/core-java-8-2/pom.xml +++ b/core-java-8-2/pom.xml @@ -21,9 +21,16 @@ UTF-8 1.8 1.8 + 64.2 + + com.ibm.icu + icu4j + ${icu.version} + + diff --git a/core-java-8-2/src/main/java/com/baeldung/jarArguments/JarExample.java b/core-java-8-2/src/main/java/com/baeldung/jarArguments/JarExample.java new file mode 100644 index 0000000000..c2fb809790 --- /dev/null +++ b/core-java-8-2/src/main/java/com/baeldung/jarArguments/JarExample.java @@ -0,0 +1,18 @@ +package com.baeldung.jarArguments; + +public class JarExample { + + public static void main(String[] args) { + System.out.println("Hello Baeldung Reader in JarExample!"); + + if(args == null) { + System.out.println("You have not provided any arguments!"); + }else { + System.out.println("There are "+args.length+" argument(s)!"); + for(int i=0; i locales = Arrays.asList(new Locale[] { Locale.UK, Locale.ITALY, Locale.FRANCE, Locale.forLanguageTag("pl-PL") }); + Localization.run(locales); + JavaSEFormat.run(locales); + ICUFormat.run(locales); + } + +} diff --git a/core-java-8-2/src/main/java/com/baeldung/localization/ICUFormat.java b/core-java-8-2/src/main/java/com/baeldung/localization/ICUFormat.java new file mode 100644 index 0000000000..f7bc357933 --- /dev/null +++ b/core-java-8-2/src/main/java/com/baeldung/localization/ICUFormat.java @@ -0,0 +1,29 @@ +package com.baeldung.localization; + +import java.util.List; +import java.util.Locale; +import java.util.ResourceBundle; + +import com.ibm.icu.text.MessageFormat; + +public class ICUFormat { + + public static String getLabel(Locale locale, Object[] data) { + ResourceBundle bundle = ResourceBundle.getBundle("formats", locale); + String format = bundle.getString("label-icu"); + MessageFormat formatter = new MessageFormat(format, locale); + return formatter.format(data); + } + + public static void run(List locales) { + System.out.println("ICU formatter"); + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 0 }))); + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 1 }))); + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 2 }))); + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 3 }))); + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 0 }))); + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 1 }))); + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 2 }))); + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 3 }))); + } +} diff --git a/core-java-8-2/src/main/java/com/baeldung/localization/JavaSEFormat.java b/core-java-8-2/src/main/java/com/baeldung/localization/JavaSEFormat.java new file mode 100644 index 0000000000..c95dfffa13 --- /dev/null +++ b/core-java-8-2/src/main/java/com/baeldung/localization/JavaSEFormat.java @@ -0,0 +1,24 @@ +package com.baeldung.localization; + +import java.text.MessageFormat; +import java.util.Date; +import java.util.List; +import java.util.Locale; +import java.util.ResourceBundle; + +public class JavaSEFormat { + + public static String getLabel(Locale locale, Object[] data) { + ResourceBundle bundle = ResourceBundle.getBundle("formats", locale); + final String pattern = bundle.getString("label"); + final MessageFormat formatter = new MessageFormat(pattern, locale); + return formatter.format(data); + } + + public static void run(List locales) { + System.out.println("Java formatter"); + final Date date = new Date(System.currentTimeMillis()); + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 0 }))); + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 2 }))); + } +} diff --git a/core-java-8-2/src/main/java/com/baeldung/localization/Localization.java b/core-java-8-2/src/main/java/com/baeldung/localization/Localization.java new file mode 100644 index 0000000000..17a6598ce0 --- /dev/null +++ b/core-java-8-2/src/main/java/com/baeldung/localization/Localization.java @@ -0,0 +1,18 @@ +package com.baeldung.localization; + +import java.util.List; +import java.util.Locale; +import java.util.ResourceBundle; + +public class Localization { + + public static String getLabel(Locale locale) { + final ResourceBundle bundle = ResourceBundle.getBundle("messages", locale); + return bundle.getString("label"); + } + + public static void run(List locales) { + locales.forEach(locale -> System.out.println(getLabel(locale))); + } + +} diff --git a/core-java-8-2/src/main/resources/example_manifest.txt b/core-java-8-2/src/main/resources/example_manifest.txt new file mode 100644 index 0000000000..71abcb05fb --- /dev/null +++ b/core-java-8-2/src/main/resources/example_manifest.txt @@ -0,0 +1 @@ +Main-Class: com.baeldung.jarArguments.JarExample diff --git a/core-java-8-2/src/main/resources/formats_en.properties b/core-java-8-2/src/main/resources/formats_en.properties new file mode 100644 index 0000000000..41e0e00119 --- /dev/null +++ b/core-java-8-2/src/main/resources/formats_en.properties @@ -0,0 +1,2 @@ +label=On {0, date, short} {1} has sent you {2, choice, 0#no messages|1#a message|2#two messages|2<{2,number,integer} messages}. +label-icu={0} has sent you {2, plural, =0 {no messages} =1 {a message} other {{2, number, integer} messages}}. \ No newline at end of file diff --git a/core-java-8-2/src/main/resources/formats_fr.properties b/core-java-8-2/src/main/resources/formats_fr.properties new file mode 100644 index 0000000000..c2d5159b32 --- /dev/null +++ b/core-java-8-2/src/main/resources/formats_fr.properties @@ -0,0 +1,2 @@ +label={0, date, short}, {1}{2, choice, 0# ne|0<} vous a envoyé {2, choice, 0#aucun message|1#un message|2#deux messages|2<{2,number,integer} messages}. +label-icu={0} {2, plural, =0 {ne } other {}}vous a envoyé {2, plural, =0 {aucun message} =1 {un message} other {{2, number, integer} messages}}. \ No newline at end of file diff --git a/core-java-8-2/src/main/resources/formats_it.properties b/core-java-8-2/src/main/resources/formats_it.properties new file mode 100644 index 0000000000..43fd1eee1c --- /dev/null +++ b/core-java-8-2/src/main/resources/formats_it.properties @@ -0,0 +1,2 @@ +label={0, date, short} {1} ti ha inviato {2, choice, 0#nessun messagio|1#un messaggio|2#due messaggi|2<{2, number, integer} messaggi}. +label-icu={0} {2, plural, =0 {non } other {}}ti ha inviato {2, plural, =0 {nessun messaggio} =1 {un messaggio} other {{2, number, integer} messaggi}}. \ No newline at end of file diff --git a/core-java-8-2/src/main/resources/formats_pl.properties b/core-java-8-2/src/main/resources/formats_pl.properties new file mode 100644 index 0000000000..9333ec3396 --- /dev/null +++ b/core-java-8-2/src/main/resources/formats_pl.properties @@ -0,0 +1,2 @@ +label=W {0, date, short} {1}{2, choice, 0# nie|0<} wys\u0142a\u0142a ci {2, choice, 0#\u017Cadnych wiadomo\u015Bci|1#wiadomo\u015B\u0107|2#dwie wiadomo\u015Bci|2<{2, number, integer} wiadomo\u015Bci}. +label-icu={0} {2, plural, =0 {nie } other {}}{1, select, male {wys\u0142a\u0142} female {wys\u0142a\u0142a} other {wys\u0142a\u0142o}} ci {2, plural, =0 {\u017Cadnej wiadomo\u015Bci} =1 {wiadomo\u015B\u0107} other {{2, number, integer} wiadomo\u015Bci}}. diff --git a/core-java-8-2/src/main/resources/messages_en.properties b/core-java-8-2/src/main/resources/messages_en.properties new file mode 100644 index 0000000000..bcbca9483c --- /dev/null +++ b/core-java-8-2/src/main/resources/messages_en.properties @@ -0,0 +1 @@ +label=Alice has sent you a message. diff --git a/core-java-8-2/src/main/resources/messages_fr.properties b/core-java-8-2/src/main/resources/messages_fr.properties new file mode 100644 index 0000000000..6716102568 --- /dev/null +++ b/core-java-8-2/src/main/resources/messages_fr.properties @@ -0,0 +1 @@ +label=Alice vous a envoyé un message. \ No newline at end of file diff --git a/core-java-8-2/src/main/resources/messages_it.properties b/core-java-8-2/src/main/resources/messages_it.properties new file mode 100644 index 0000000000..6929a8c091 --- /dev/null +++ b/core-java-8-2/src/main/resources/messages_it.properties @@ -0,0 +1 @@ +label=Alice ti ha inviato un messaggio. \ No newline at end of file diff --git a/core-java-8-2/src/main/resources/messages_pl.properties b/core-java-8-2/src/main/resources/messages_pl.properties new file mode 100644 index 0000000000..5515a9920e --- /dev/null +++ b/core-java-8-2/src/main/resources/messages_pl.properties @@ -0,0 +1 @@ +label=Alice wys\u0142a\u0142a ci wiadomo\u015B\u0107. \ No newline at end of file diff --git a/core-java-8-2/src/test/java/com/baeldung/localization/ICUFormatUnitTest.java b/core-java-8-2/src/test/java/com/baeldung/localization/ICUFormatUnitTest.java new file mode 100644 index 0000000000..2c8f9b47f3 --- /dev/null +++ b/core-java-8-2/src/test/java/com/baeldung/localization/ICUFormatUnitTest.java @@ -0,0 +1,74 @@ +package com.baeldung.localization; + +import static org.junit.Assert.assertEquals; + +import java.util.Locale; + +import org.junit.Test; + +import com.baeldung.localization.ICUFormat; + +public class ICUFormatUnitTest { + + @Test + public void givenInUK_whenAliceSendsNothing_thenCorrectMessage() { + assertEquals("Alice has sent you no messages.", ICUFormat.getLabel(Locale.UK, new Object[] { "Alice", "female", 0 })); + } + + @Test + public void givenInUK_whenAliceSendsOneMessage_thenCorrectMessage() { + assertEquals("Alice has sent you a message.", ICUFormat.getLabel(Locale.UK, new Object[] { "Alice", "female", 1 })); + } + + @Test + public void givenInUK_whenBobSendsSixMessages_thenCorrectMessage() { + assertEquals("Bob has sent you 6 messages.", ICUFormat.getLabel(Locale.UK, new Object[] { "Bob", "male", 6 })); + } + + @Test + public void givenInItaly_whenAliceSendsNothing_thenCorrectMessage() { + assertEquals("Alice non ti ha inviato nessun messaggio.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Alice", "female", 0 })); + } + + @Test + public void givenInItaly_whenAliceSendsOneMessage_thenCorrectMessage() { + assertEquals("Alice ti ha inviato un messaggio.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Alice", "female", 1 })); + } + + @Test + public void givenInItaly_whenBobSendsSixMessages_thenCorrectMessage() { + assertEquals("Bob ti ha inviato 6 messaggi.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Bob", "male", 6 })); + } + + @Test + public void givenInFrance_whenAliceSendsNothing_thenCorrectMessage() { + assertEquals("Alice ne vous a envoyé aucun message.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Alice", "female", 0 })); + } + + @Test + public void givenInFrance_whenAliceSendsOneMessage_thenCorrectMessage() { + assertEquals("Alice vous a envoyé un message.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Alice", "female", 1 })); + } + + @Test + public void givenInFrance_whenBobSendsSixMessages_thenCorrectMessage() { + assertEquals("Bob vous a envoyé 6 messages.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Bob", "male", 6 })); + } + + + @Test + public void givenInPoland_whenAliceSendsNothing_thenCorrectMessage() { + assertEquals("Alice nie wysÅ‚aÅ‚a ci żadnej wiadomoÅ›ci.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Alice", "female", 0 })); + } + + @Test + public void givenInPoland_whenAliceSendsOneMessage_thenCorrectMessage() { + assertEquals("Alice wysÅ‚aÅ‚a ci wiadomość.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Alice", "female", 1 })); + } + + @Test + public void givenInPoland_whenBobSendsSixMessages_thenCorrectMessage() { + assertEquals("Bob wysÅ‚aÅ‚ ci 6 wiadomoÅ›ci.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Bob", "male", 6 })); + } + +} diff --git a/core-java-collections-set/README.md b/core-java-collections-set/README.md index 710484a2e1..217e9ee6a5 100644 --- a/core-java-collections-set/README.md +++ b/core-java-collections-set/README.md @@ -4,3 +4,8 @@ ### Relevant Articles: - [Set Operations in Java](http://www.baeldung.com/set-operations-in-java) +- [HashSet and TreeSet Comparison](http://www.baeldung.com/java-hashset-vs-treeset) +- [A Guide to HashSet in Java](http://www.baeldung.com/java-hashset) +- [A Guide to TreeSet in Java](http://www.baeldung.com/java-tree-set) +- [Initializing HashSet at the Time of Construction](http://www.baeldung.com/java-initialize-hashset) +- [Guide to EnumSet](https://www.baeldung.com/java-enumset) \ No newline at end of file diff --git a/core-java-collections/src/main/java/com/baeldung/enumset/EnumSets.java b/core-java-collections-set/src/main/java/com/baeldung/enumset/EnumSets.java similarity index 100% rename from core-java-collections/src/main/java/com/baeldung/enumset/EnumSets.java rename to core-java-collections-set/src/main/java/com/baeldung/enumset/EnumSets.java diff --git a/core-java-collections/src/test/java/com/baeldung/collection/WhenUsingHashSet.java b/core-java-collections-set/src/test/java/com/baeldung/collection/WhenUsingHashSet.java similarity index 100% rename from core-java-collections/src/test/java/com/baeldung/collection/WhenUsingHashSet.java rename to core-java-collections-set/src/test/java/com/baeldung/collection/WhenUsingHashSet.java diff --git a/core-java-collections/src/test/java/com/baeldung/collection/WhenUsingTreeSet.java b/core-java-collections-set/src/test/java/com/baeldung/collection/WhenUsingTreeSet.java similarity index 100% rename from core-java-collections/src/test/java/com/baeldung/collection/WhenUsingTreeSet.java rename to core-java-collections-set/src/test/java/com/baeldung/collection/WhenUsingTreeSet.java diff --git a/core-java-collections/src/test/java/com/baeldung/java/set/HashSetInitalizingUnitTest.java b/core-java-collections-set/src/test/java/com/baeldung/java/set/HashSetInitalizingUnitTest.java similarity index 100% rename from core-java-collections/src/test/java/com/baeldung/java/set/HashSetInitalizingUnitTest.java rename to core-java-collections-set/src/test/java/com/baeldung/java/set/HashSetInitalizingUnitTest.java diff --git a/core-java/src/test/java/com/baeldung/java/set/SetUnitTest.java b/core-java-collections-set/src/test/java/com/baeldung/java/set/SetUnitTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/java/set/SetUnitTest.java rename to core-java-collections-set/src/test/java/com/baeldung/java/set/SetUnitTest.java diff --git a/core-java-collections/README.md b/core-java-collections/README.md index 43f5bfc384..b34293769d 100644 --- a/core-java-collections/README.md +++ b/core-java-collections/README.md @@ -4,14 +4,10 @@ ### Relevant Articles: - [Java – Combine Multiple Collections](http://www.baeldung.com/java-combine-multiple-collections) -- [HashSet and TreeSet Comparison](http://www.baeldung.com/java-hashset-vs-treeset) - [Collect a Java Stream to an Immutable Collection](http://www.baeldung.com/java-stream-immutable-collection) - [Introduction to the Java ArrayDeque](http://www.baeldung.com/java-array-deque) -- [A Guide to HashSet in Java](http://www.baeldung.com/java-hashset) -- [A Guide to TreeSet in Java](http://www.baeldung.com/java-tree-set) - [Getting the Size of an Iterable in Java](http://www.baeldung.com/java-iterable-size) - [How to Filter a Collection in Java](http://www.baeldung.com/java-collection-filtering) -- [Initializing HashSet at the Time of Construction](http://www.baeldung.com/java-initialize-hashset) - [Removing the First Element of an Array](https://www.baeldung.com/java-array-remove-first-element) - [Fail-Safe Iterator vs Fail-Fast Iterator](http://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator) - [Shuffling Collections In Java](http://www.baeldung.com/java-shuffle-collection) @@ -23,7 +19,6 @@ - [Time Complexity of Java Collections](https://www.baeldung.com/java-collections-complexity) - [Operating on and Removing an Item from Stream](https://www.baeldung.com/java-use-remove-item-stream) - [An Introduction to Synchronized Java Collections](https://www.baeldung.com/java-synchronized-collections) -- [Guide to EnumSet](https://www.baeldung.com/java-enumset) - [Removing Elements from Java Collections](https://www.baeldung.com/java-collection-remove-elements) - [Combining Different Types of Collections in Java](https://www.baeldung.com/java-combine-collections) - [Sorting in Java](http://www.baeldung.com/java-sorting) diff --git a/core-java-modules/multimodulemavenproject/daomodule/pom.xml b/core-java-modules/multimodulemavenproject/daomodule/pom.xml new file mode 100644 index 0000000000..a260e15e6d --- /dev/null +++ b/core-java-modules/multimodulemavenproject/daomodule/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + com.baeldung.multimodulemavenproject + multimodulemavenproject + 1.0 + + com.baeldung.daomodule + daomodule + 1.0 + jar + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + + + 9 + 9 + + \ No newline at end of file diff --git a/core-java-modules/multimodulemavenproject/daomodule/src/main/java/com/baeldung/daomodule/Dao.java b/core-java-modules/multimodulemavenproject/daomodule/src/main/java/com/baeldung/daomodule/Dao.java new file mode 100644 index 0000000000..9568bae9d9 --- /dev/null +++ b/core-java-modules/multimodulemavenproject/daomodule/src/main/java/com/baeldung/daomodule/Dao.java @@ -0,0 +1,11 @@ +package com.baeldung.daomodule; +import java.util.List; +import java.util.Optional; + +public interface Dao { + + Optional findById(int id); + + List findAll(); + +} diff --git a/core-java-modules/multimodulemavenproject/daomodule/src/main/java/module-info.java b/core-java-modules/multimodulemavenproject/daomodule/src/main/java/module-info.java new file mode 100644 index 0000000000..20c51d316a --- /dev/null +++ b/core-java-modules/multimodulemavenproject/daomodule/src/main/java/module-info.java @@ -0,0 +1,3 @@ +module com.baeldung.daomodule { + exports com.baeldung.daomodule; +} diff --git a/core-java-modules/multimodulemavenproject/entitymodule/pom.xml b/core-java-modules/multimodulemavenproject/entitymodule/pom.xml new file mode 100644 index 0000000000..1fd672d03e --- /dev/null +++ b/core-java-modules/multimodulemavenproject/entitymodule/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + com.baeldung.multimodulemavenproject + multimodulemavenproject + 1.0 + + com.baeldung.entitymodule + entitymodule + 1.0 + jar + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + + + 9 + 9 + + + \ No newline at end of file diff --git a/core-java-modules/multimodulemavenproject/entitymodule/src/main/java/com/baeldung/entitymodule/User.java b/core-java-modules/multimodulemavenproject/entitymodule/src/main/java/com/baeldung/entitymodule/User.java new file mode 100644 index 0000000000..c025bffd87 --- /dev/null +++ b/core-java-modules/multimodulemavenproject/entitymodule/src/main/java/com/baeldung/entitymodule/User.java @@ -0,0 +1,19 @@ +package com.baeldung.entitymodule; + +public class User { + + private final String name; + + public User(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return "User{" + "name=" + name + '}'; + } +} \ No newline at end of file diff --git a/core-java-modules/multimodulemavenproject/entitymodule/src/main/java/module-info.java b/core-java-modules/multimodulemavenproject/entitymodule/src/main/java/module-info.java new file mode 100644 index 0000000000..b2c4553357 --- /dev/null +++ b/core-java-modules/multimodulemavenproject/entitymodule/src/main/java/module-info.java @@ -0,0 +1,3 @@ +module com.baeldung.entitymodule { + exports com.baeldung.entitymodule; +} diff --git a/core-java-modules/multimodulemavenproject/mainappmodule/pom.xml b/core-java-modules/multimodulemavenproject/mainappmodule/pom.xml new file mode 100644 index 0000000000..26e6a15b1e --- /dev/null +++ b/core-java-modules/multimodulemavenproject/mainappmodule/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.baeldung.multimodulemavenproject + multimodulemavenproject + 1.0 + + com.baeldung.mainappmodule + mainappmodule + 1.0 + jar + + + + com.baeldung.entitymodule + entitymodule + 1.0 + + + com.baeldung.daomodule + daomodule + 1.0 + + + com.baeldung.userdaomodule + userdaomodule + 1.0 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + + + 9 + 9 + + \ No newline at end of file diff --git a/core-java-modules/multimodulemavenproject/mainappmodule/src/main/java/com/baeldung/mainappmodule/Application.java b/core-java-modules/multimodulemavenproject/mainappmodule/src/main/java/com/baeldung/mainappmodule/Application.java new file mode 100644 index 0000000000..cf0ba1d9bc --- /dev/null +++ b/core-java-modules/multimodulemavenproject/mainappmodule/src/main/java/com/baeldung/mainappmodule/Application.java @@ -0,0 +1,19 @@ +package com.baeldung.mainappmodule; + +import com.baeldung.daomodule.Dao; +import com.baeldung.entitymodule.User; +import com.baeldung.userdaomodule.UserDao; +import java.util.HashMap; +import java.util.Map; + +public class Application { + + public static void main(String args[]) { + Map users = new HashMap<>(); + users.put(1, new User("Julie")); + users.put(2, new User("David")); + Dao userDao = new UserDao(users); + userDao.findAll().forEach(System.out::println); + } + +} diff --git a/core-java-modules/multimodulemavenproject/mainappmodule/src/main/java/module-info.java b/core-java-modules/multimodulemavenproject/mainappmodule/src/main/java/module-info.java new file mode 100644 index 0000000000..dd7620c7bf --- /dev/null +++ b/core-java-modules/multimodulemavenproject/mainappmodule/src/main/java/module-info.java @@ -0,0 +1,8 @@ +module com.baeldung.mainppmodule { + + requires com.baeldung.entitymodule; + requires com.baeldung.daomodule; + requires com.baeldung.userdaomodule; + uses com.baeldung.userdaomodule.UserDao; + +} diff --git a/core-java-modules/multimodulemavenproject/pom.xml b/core-java-modules/multimodulemavenproject/pom.xml new file mode 100644 index 0000000000..3d56f1356b --- /dev/null +++ b/core-java-modules/multimodulemavenproject/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + com.baeldung.multimodulemavenproject + multimodulemavenproject + 1.0 + pom + multimodulemavenproject + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + ../../ + + + + + + junit + junit + 4.12 + test + + + org.assertj + assertj-core + 3.12.2 + test + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + + 1.9 + 1.9 + + + + + + + + + entitymodule + daomodule + userdaomodule + mainappmodule + + + + UTF-8 + + diff --git a/core-java-modules/multimodulemavenproject/userdaomodule/pom.xml b/core-java-modules/multimodulemavenproject/userdaomodule/pom.xml new file mode 100644 index 0000000000..63968452db --- /dev/null +++ b/core-java-modules/multimodulemavenproject/userdaomodule/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + com.baeldung.multimodulemavenproject + multimodulemavenproject + 1.0 + + com.baeldung.userdaomodule + userdaomodule + 1.0 + jar + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + + + + com.baeldung.entitymodule + entitymodule + 1.0 + + + com.baeldung.daomodule + daomodule + 1.0 + + + + + 9 + 9 + + + \ No newline at end of file diff --git a/core-java-modules/multimodulemavenproject/userdaomodule/src/main/java/com/baeldung/userdaomodule/UserDao.java b/core-java-modules/multimodulemavenproject/userdaomodule/src/main/java/com/baeldung/userdaomodule/UserDao.java new file mode 100644 index 0000000000..aba157b431 --- /dev/null +++ b/core-java-modules/multimodulemavenproject/userdaomodule/src/main/java/com/baeldung/userdaomodule/UserDao.java @@ -0,0 +1,32 @@ +package com.baeldung.userdaomodule; + +import com.baeldung.daomodule.Dao; +import com.baeldung.entitymodule.User; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class UserDao implements Dao { + + private final Map users; + + public UserDao() { + users = new HashMap<>(); + } + + public UserDao(Map users) { + this.users = users; + } + + @Override + public List findAll() { + return new ArrayList<>(users.values()); + } + + @Override + public Optional findById(int id) { + return Optional.ofNullable(users.get(id)); + } +} \ No newline at end of file diff --git a/core-java-modules/multimodulemavenproject/userdaomodule/src/main/java/module-info.java b/core-java-modules/multimodulemavenproject/userdaomodule/src/main/java/module-info.java new file mode 100644 index 0000000000..6dd81dabe5 --- /dev/null +++ b/core-java-modules/multimodulemavenproject/userdaomodule/src/main/java/module-info.java @@ -0,0 +1,6 @@ +module com.baeldung.userdaomodule { + requires com.baeldung.entitymodule; + requires com.baeldung.daomodule; + provides com.baeldung.daomodule.Dao with com.baeldung.userdaomodule.UserDao; + exports com.baeldung.userdaomodule; +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index ad49cceb9d..ed6a056448 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -2,6 +2,7 @@ 4.0.0 + com.baeldung.core-java-modules core-java-modules core-java-modules pom @@ -15,5 +16,5 @@ pre-jpms - - \ No newline at end of file + + diff --git a/core-java/src/main/java/com/baeldung/graph/Graph.java b/core-java/src/main/java/com/baeldung/graph/Graph.java index 43b5c0aa08..3f2e17c43c 100644 --- a/core-java/src/main/java/com/baeldung/graph/Graph.java +++ b/core-java/src/main/java/com/baeldung/graph/Graph.java @@ -59,18 +59,43 @@ public class Graph { Vertex(String label) { this.label = label; } - @Override - public boolean equals(Object obj) { - Vertex vertex = (Vertex) obj; - return vertex.label == label; - } + @Override public int hashCode() { - return label.hashCode(); + final int prime = 31; + int result = 1; + result = prime * result + getOuterType().hashCode(); + result = prime * result + ((label == null) ? 0 : label.hashCode()); + return result; } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Vertex other = (Vertex) obj; + if (!getOuterType().equals(other.getOuterType())) + return false; + if (label == null) { + if (other.label != null) + return false; + } else if (!label.equals(other.label)) + return false; + return true; + } + @Override public String toString() { return label; } + + + private Graph getOuterType() { + return Graph.this; + } } } \ No newline at end of file diff --git a/core-kotlin-2/src/main/kotlin/com/baeldung/jvmannotations/Document.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/jvmannotations/Document.kt new file mode 100644 index 0000000000..3f9922b88b --- /dev/null +++ b/core-kotlin-2/src/main/kotlin/com/baeldung/jvmannotations/Document.kt @@ -0,0 +1,31 @@ +package com.baeldung.jvmannotations + +import java.util.* + +interface Document { + + @JvmDefault + fun getType() = "document" +} + +class TextDocument : Document { + override fun getType() = "text" + + fun transformList(list : List) : List { + return list.filter { n -> n.toInt() > 1 } + } + + fun transformListInverseWildcards(list : List<@JvmSuppressWildcards Number>) : List<@JvmWildcard Number> { + return list.filter { n -> n.toInt() > 1 } + } + + var list : List<@JvmWildcard Any> = ArrayList() +} + +class XmlDocument(d : Document) : Document by d + +fun main() { + val myDocument = TextDocument() + val myTextDocument = XmlDocument(myDocument) + println("${myDocument.getType()} ${myTextDocument.getType()}") +} diff --git a/core-kotlin-2/src/main/kotlin/com/baeldung/jvmannotations/HtmlDocument.java b/core-kotlin-2/src/main/kotlin/com/baeldung/jvmannotations/HtmlDocument.java new file mode 100644 index 0000000000..feb71772cb --- /dev/null +++ b/core-kotlin-2/src/main/kotlin/com/baeldung/jvmannotations/HtmlDocument.java @@ -0,0 +1,9 @@ +package com.baeldung.jvmannotations; + +public class HtmlDocument implements Document { + + @Override + public String getType() { + return "HTML"; + } +} \ No newline at end of file diff --git a/core-kotlin-2/src/main/kotlin/com/baeldung/jvmannotations/Message.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/jvmannotations/Message.kt new file mode 100644 index 0000000000..80180bd924 --- /dev/null +++ b/core-kotlin-2/src/main/kotlin/com/baeldung/jvmannotations/Message.kt @@ -0,0 +1,66 @@ +@file:JvmName("MessageHelper") +@file:JvmMultifileClass //used +package com.baeldung.jvmannotations + +import java.util.* + +@JvmName("getMyUsername") +fun getMyName() : String { + return "myUserId" +} + +object MessageBroker { + @JvmStatic + var totalMessagesSent = 0 + + const val maxMessageLength = 0 + + @JvmStatic + fun clearAllMessages() { + } + + @JvmStatic + @JvmOverloads + @Throws(Exception::class) + fun findMessages(sender : String, type : String = "text", maxResults : Int = 10) : List { + if(sender.isEmpty()) { + throw Exception() + } + return ArrayList() + } +} + +class Message { + + // this would cause a compilation error since sender is immutable + // @set:JvmName("setSender") + val sender = "myself" + + // this works as name is overridden + @JvmName("getSenderName") + fun getSender() : String = "from:$sender" + + @get:JvmName("getReceiverName") + @set:JvmName("setReceiverName") + var receiver : String = "" + + @get:JvmName("getContent") + @set:JvmName("setContent") + var text = "" + + // generates a warning + @get:JvmName("getId") + private val id = 0 + + @get:JvmName("hasAttachment") + var hasAttachment = true + + var isEncrypted = true + + fun setReceivers(receiverNames : List) { + } + + @JvmName("setReceiverIds") + fun setReceivers(receiverNames : List) { + } +} \ No newline at end of file diff --git a/core-kotlin-2/src/main/kotlin/com/baeldung/jvmannotations/MessageConverter.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/jvmannotations/MessageConverter.kt new file mode 100644 index 0000000000..3b19b12e10 --- /dev/null +++ b/core-kotlin-2/src/main/kotlin/com/baeldung/jvmannotations/MessageConverter.kt @@ -0,0 +1,6 @@ +@file:JvmMultifileClass +@file:JvmName("MessageHelper") //applies to all top level functions / variables / constants +package com.baeldung.jvmannotations + +fun convert(message: Message) { +} diff --git a/core-kotlin-io/README.md b/core-kotlin-io/README.md index c085c0653f..cb4dac7e4d 100644 --- a/core-kotlin-io/README.md +++ b/core-kotlin-io/README.md @@ -1,3 +1,5 @@ ## Relevant articles: - [InputStream to String in Kotlin](https://www.baeldung.com/kotlin-inputstream-to-string) +- [Console I/O in Kotlin](https://www.baeldung.com/kotlin-console-io) + diff --git a/core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt b/core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt index 8539378c91..886a3fc51e 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt @@ -17,4 +17,8 @@ class FileReader { File(fileName).inputStream().readBytes().toString(Charsets.UTF_8) fun readFileDirectlyAsText(fileName: String): String = File(fileName).readText(Charsets.UTF_8) + + fun readFileUsingGetResource(fileName: String) = this::class.java.getResource(fileName).readText(Charsets.UTF_8) + + fun readFileAsLinesUsingGetResourceAsStream(fileName: String) = this::class.java.getResourceAsStream(fileName).bufferedReader().readLines() } \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/filesystem/FileReaderTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/filesystem/FileReaderTest.kt index 67795dda14..ad541c446e 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/filesystem/FileReaderTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/filesystem/FileReaderTest.kt @@ -48,4 +48,20 @@ internal class FileReaderTest { assertTrue { text.contains("Hello to Kotlin") } } + + @Test + fun whenReadFileAsTextUsingGetResource_thenCorrect() { + val text = fileReader.readFileUsingGetResource("/Kotlin.in") + + assertTrue { text.contains("1. Concise") } + } + + @Test + fun whenReadFileUsingGetResourceAsStream_thenCorrect() { + val lines = fileReader.readFileAsLinesUsingGetResourceAsStream("/Kotlin.in") + + assertTrue { lines.contains("3. Interoperable") } + } + + } \ No newline at end of file diff --git a/fastUtil/pom.xml b/fastUtil/pom.xml deleted file mode 100644 index fcd9020747..0000000000 --- a/fastUtil/pom.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - 4.0.0 - - com.baeldung - fastUtil - 1.0-SNAPSHOT - - - - - it.unimi.dsi - fastutil - 8.2.2 - - - - junit - junit - 4.12 - test - - - - org.openjdk.jmh - jmh-core - 1.19 - test - - - org.openjdk.jmh - jmh-generator-annprocess - 1.19 - test - - - - - - - - \ No newline at end of file diff --git a/httpclient-simple/README.md b/httpclient-simple/README.md index 96d9e9ec0e..492f3bc5b4 100644 --- a/httpclient-simple/README.md +++ b/httpclient-simple/README.md @@ -9,4 +9,8 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [HttpClient 4 – Get the Status Code](http://www.baeldung.com/httpclient-status-code) - [HttpClient with SSL](http://www.baeldung.com/httpclient-ssl) +- [HttpClient Timeout](http://www.baeldung.com/httpclient-timeout) +- [HttpClient 4 – Send Custom Cookie](http://www.baeldung.com/httpclient-4-cookies) +- [Custom HTTP Header with the HttpClient](http://www.baeldung.com/httpclient-custom-http-header) - [HttpClient Basic Authentication](http://www.baeldung.com/httpclient-4-basic-authentication) +- [Posting with HttpClient](https://www.baeldung.com/httpclient-post-http-request) diff --git a/httpclient-simple/src/test/java/org/baeldung/client/RestClientLiveManualTest.java b/httpclient-simple/src/test/java/org/baeldung/client/RestClientLiveManualTest.java index 53f259c21d..696d414ae7 100644 --- a/httpclient-simple/src/test/java/org/baeldung/client/RestClientLiveManualTest.java +++ b/httpclient-simple/src/test/java/org/baeldung/client/RestClientLiveManualTest.java @@ -46,16 +46,11 @@ public class RestClientLiveManualTest { // old httpClient will throw UnsupportedOperationException @Ignore @Test - public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException_1() throws GeneralSecurityException { + public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk_1() throws GeneralSecurityException { final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); final CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient(); - final TrustStrategy acceptingTrustStrategy = new TrustStrategy() { - @Override - public final boolean isTrusted(final X509Certificate[] certificate, final String authType) { - return true; - } - }; + final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER); httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf)); @@ -65,7 +60,7 @@ public class RestClientLiveManualTest { // new httpClient : 4.4 and above @Test - public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException_2() throws GeneralSecurityException { + public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk_2() throws GeneralSecurityException { final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientHeadersLiveTest.java b/httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientHeadersLiveTest.java similarity index 100% rename from httpclient/src/test/java/org/baeldung/httpclient/HttpClientHeadersLiveTest.java rename to httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientHeadersLiveTest.java diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientPostingLiveTest.java b/httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientPostingLiveTest.java similarity index 100% rename from httpclient/src/test/java/org/baeldung/httpclient/HttpClientPostingLiveTest.java rename to httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientPostingLiveTest.java diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java b/httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java similarity index 60% rename from httpclient/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java rename to httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java index 74255e416c..8041080b3d 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java +++ b/httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java @@ -1,20 +1,27 @@ package org.baeldung.httpclient; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.config.SocketConfig; -import org.apache.http.conn.HttpHostConnectException; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.junit.After; -import org.junit.Test; - -import java.io.IOException; - import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; +import java.io.IOException; +import java.util.Timer; +import java.util.TimerTask; + +import org.apache.http.HttpResponse; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.params.ClientPNames; +import org.apache.http.config.SocketConfig; +import org.apache.http.conn.ConnectTimeoutException; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.params.CoreConnectionPNames; +import org.apache.http.params.HttpParams; +import org.junit.After; +import org.junit.Test; + public class HttpClientTimeoutLiveTest { private CloseableHttpResponse response; @@ -25,6 +32,20 @@ public class HttpClientTimeoutLiveTest { } // tests + @Test + public final void givenUsingOldApi_whenSettingTimeoutViaParameter_thenCorrect() throws IOException { + + DefaultHttpClient httpClient = new DefaultHttpClient(); + int timeout = 5; // seconds + HttpParams httpParams = httpClient.getParams(); + httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000); + httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000); + httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000)); + + final HttpGet request = new HttpGet("http://www.github.com"); + HttpResponse execute = httpClient.execute(request); + assertThat(execute.getStatusLine().getStatusCode(), equalTo(200)); + } @Test public final void givenUsingNewApi_whenSettingTimeoutViaRequestConfig_thenCorrect() throws IOException { @@ -33,8 +54,6 @@ public class HttpClientTimeoutLiveTest { final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); final HttpGet request = new HttpGet("http://www.github.com"); - // httpParams.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000)); // https://issues.apache.org/jira/browse/HTTPCLIENT-1418 - response = client.execute(request); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); @@ -71,7 +90,7 @@ public class HttpClientTimeoutLiveTest { /** * This simulates a timeout against a domain with multiple routes/IPs to it (not a single raw IP) */ - @Test(expected = HttpHostConnectException.class) + @Test(expected = ConnectTimeoutException.class) public final void givenTimeoutIsConfigured_whenTimingOut_thenTimeoutException() throws IOException { final int timeout = 3; @@ -81,5 +100,28 @@ public class HttpClientTimeoutLiveTest { final HttpGet request = new HttpGet("http://www.google.com:81"); client.execute(request); } + + @Test + public void whenSecuredRestApiIsConsumed_then200OK() throws IOException { + CloseableHttpClient httpClient = HttpClientBuilder.create().build(); + int timeout = 20; // seconds + RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout * 1000) + .setConnectTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build(); + HttpGet getMethod = new HttpGet("http://localhost:8082/httpclient-simple/api/bars/1"); + getMethod.setConfig(requestConfig); + + int hardTimeout = 5; // seconds + TimerTask task = new TimerTask() { + @Override + public void run() { + getMethod.abort(); + } + }; + new Timer(true).schedule(task, hardTimeout * 1000); + + HttpResponse response = httpClient.execute(getMethod); + System.out.println("HTTP Status of response: " + response.getStatusLine().getStatusCode()); + } + } diff --git a/httpclient/src/test/java/org/baeldung/httpclient/ProgressEntityWrapper.java b/httpclient-simple/src/test/java/org/baeldung/httpclient/ProgressEntityWrapper.java similarity index 100% rename from httpclient/src/test/java/org/baeldung/httpclient/ProgressEntityWrapper.java rename to httpclient-simple/src/test/java/org/baeldung/httpclient/ProgressEntityWrapper.java diff --git a/httpclient/src/test/java/org/baeldung/httpclient/sec/HttpClientCookieLiveTest.java b/httpclient-simple/src/test/java/org/baeldung/httpclient/sec/HttpClientCookieLiveTest.java similarity index 100% rename from httpclient/src/test/java/org/baeldung/httpclient/sec/HttpClientCookieLiveTest.java rename to httpclient-simple/src/test/java/org/baeldung/httpclient/sec/HttpClientCookieLiveTest.java diff --git a/httpclient/README.md b/httpclient/README.md index cab538be85..a5fc29b089 100644 --- a/httpclient/README.md +++ b/httpclient/README.md @@ -7,12 +7,10 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: -- [HttpClient 4 – Send Custom Cookie](http://www.baeldung.com/httpclient-4-cookies) - [HttpClient 4 – Cancel Request](http://www.baeldung.com/httpclient-cancel-request) - [HttpClient 4 Cookbook](http://www.baeldung.com/httpclient4) - [Unshorten URLs with HttpClient](http://www.baeldung.com/unshorten-url-httpclient) - [HttpClient 4 – Follow Redirects for POST](http://www.baeldung.com/httpclient-redirect-on-http-post) -- [Custom HTTP Header with the HttpClient](http://www.baeldung.com/httpclient-custom-http-header) - [Multipart Upload with HttpClient 4](http://www.baeldung.com/httpclient-multipart-upload) - [HttpAsyncClient Tutorial](http://www.baeldung.com/httpasyncclient-tutorial) - [HttpClient 4 Tutorial](http://www.baeldung.com/httpclient-guide) diff --git a/java-dates-2/pom.xml b/java-dates-2/pom.xml index c2464ed47f..9307a794b9 100644 --- a/java-dates-2/pom.xml +++ b/java-dates-2/pom.xml @@ -15,6 +15,12 @@ + + joda-time + joda-time + ${joda-time.version} + + org.assertj @@ -49,6 +55,7 @@ 3.6.1 + 2.10 1.9 1.9 diff --git a/java-dates-2/src/test/java/com/baeldung/convert/ConvertDateTimeUnitTest.java b/java-dates-2/src/test/java/com/baeldung/convert/ConvertDateTimeUnitTest.java new file mode 100644 index 0000000000..cd31ccc799 --- /dev/null +++ b/java-dates-2/src/test/java/com/baeldung/convert/ConvertDateTimeUnitTest.java @@ -0,0 +1,59 @@ +package com.baeldung.convert; + +import org.joda.time.Instant; +import org.junit.Assert; +import org.junit.Test; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Calendar; +import java.util.Date; + +public class ConvertDateTimeUnitTest { + + public final long millis = 1556175797428L; + + @Test + public void givenLocalDateTime_WhenToEpochMillis_ThenMillis() { + ZoneId id = ZoneId.systemDefault(); + + LocalDateTime localDateTime = + LocalDateTime.ofInstant(java.time.Instant.ofEpochMilli(millis), id); + + ZonedDateTime zdt = ZonedDateTime.of(localDateTime, id); + + Assert.assertEquals(millis, zdt.toInstant().toEpochMilli()); + } + + @Test + public void givenJava8Instant_WhenGToEpochMillis_ThenMillis() { + java.time.Instant instant = java.time.Instant.ofEpochMilli(millis); + Assert.assertEquals(millis, instant.toEpochMilli()); + } + + @Test + public void givenDate_WhenGetTime_ThenMillis() { + Date date = new Date(millis); + Assert.assertEquals(millis, date.getTime()); + } + + @Test + public void givenCalendar_WhenGetTimeInMillis_ThenMillis() { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(new Date(millis)); + Assert.assertEquals(millis, calendar.getTimeInMillis()); + } + + @Test + public void givenJodaInstant_WhenGetMillis_ThenMillis() { + Instant jodaInstant = Instant.ofEpochMilli(millis); + Assert.assertEquals(millis, jodaInstant.getMillis()); + } + + @Test + public void givenJODADateTime_WhenGetMillis_ThenMillis() { + org.joda.time.DateTime jodaDateTime = new org.joda.time.DateTime(millis); + Assert.assertEquals(millis, jodaDateTime.getMillis()); + } +} diff --git a/java-strings-2/pom.xml b/java-strings-2/pom.xml new file mode 100755 index 0000000000..c314cd8cad --- /dev/null +++ b/java-strings-2/pom.xml @@ -0,0 +1,150 @@ + + 4.0.0 + java-strings-2 + 0.1.0-SNAPSHOT + jar + java-strings-2 + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + commons-io + commons-io + ${commons-io.version} + + + log4j + log4j + ${log4j.version} + + + commons-codec + commons-codec + ${commons-codec.version} + + + + org.assertj + assertj-core + ${assertj.version} + test + + + org.openjdk.jmh + jmh-core + ${jmh-core.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh-core.version} + + + com.ibm.icu + icu4j + ${icu4j.version} + + + com.google.guava + guava + ${guava.version} + + + com.vdurmont + emoji-java + ${emoji-java.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + junit + junit + ${junit.version} + test + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter-api.version} + test + + + + org.hamcrest + hamcrest-library + ${hamcrest-library.version} + test + + + + + org.passay + passay + ${passay.version} + + + org.apache.commons + commons-text + ${commons-text.version} + + + + org.ahocorasick + ahocorasick + ${ahocorasick.version} + + + + + + java-strings-2 + + + src/main/resources + true + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${java.version} + ${java.version} + -parameters + + + + + + + + 3.8.1 + 1.10 + + 3.6.1 + 1.19 + 61.1 + 27.0.1-jre + 4.0.0 + 5.3.1 + 1.3 + 1.3.1 + 1.4 + 0.4.0 + + + \ No newline at end of file diff --git a/java-strings-2/src/main/java/com/baeldung/string/search/performance/SubstringSearchPerformanceComparison.java b/java-strings-2/src/main/java/com/baeldung/string/search/performance/SubstringSearchPerformanceComparison.java new file mode 100644 index 0000000000..bf33c47a7e --- /dev/null +++ b/java-strings-2/src/main/java/com/baeldung/string/search/performance/SubstringSearchPerformanceComparison.java @@ -0,0 +1,58 @@ +package com.baeldung.string.search.performance; + +import java.util.concurrent.TimeUnit; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; + +/** + * Based on https://github.com/tedyoung/indexof-contains-benchmark + */ +@Fork(5) +@State(Scope.Benchmark) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +public class SubstringSearchPerformanceComparison { + + private String message; + + private Pattern pattern; + + public static void main(String[] args) throws Exception { + org.openjdk.jmh.Main.main(args); + } + + @Setup + public void setup() { + message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"; + pattern = Pattern.compile("(? + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/java-strings-2/src/test/java/com/baeldung/string/search/SubstringSearch.java b/java-strings-2/src/test/java/com/baeldung/string/search/SubstringSearch.java new file mode 100644 index 0000000000..4a5adb45ef --- /dev/null +++ b/java-strings-2/src/test/java/com/baeldung/string/search/SubstringSearch.java @@ -0,0 +1,70 @@ +package com.baeldung.string.search; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Assert; +import org.junit.Test; + +/** + * BAEL-2832: Different ways to check if a Substring could be found in a String. + */ +public class SubstringSearch { + + @Test + public void searchSubstringWithIndexOf() { + Assert.assertEquals(9, "Bohemian Rhapsodyan".indexOf("Rhap")); + + // indexOf will return -1, because it's case sensitive + Assert.assertEquals(-1, "Bohemian Rhapsodyan".indexOf("rhap")); + + // indexOf will return 9, because it's all lowercase + Assert.assertEquals(9, "Bohemian Rhapsodyan".toLowerCase() + .indexOf("rhap")); + + // it will return 6, because it's the first occurrence. Sorry Queen for being blasphemic + Assert.assertEquals(6, "Bohemian Rhapsodyan".indexOf("an")); + } + + @Test + public void searchSubstringWithContains() { + Assert.assertTrue("Hey Ho, let's go".contains("Hey")); + + // contains will return false, because it's case sensitive + Assert.assertFalse("Hey Ho, let's go".contains("hey")); + + // contains will return true, because it's all lowercase + Assert.assertTrue("Hey Ho, let's go".toLowerCase().contains("hey")); + + // contains will return false, because 'jey' can't be found + Assert.assertFalse("Hey Ho, let's go".contains("jey")); + } + + @Test + public void searchSubstringWithStringUtils() { + Assert.assertTrue(StringUtils.containsIgnoreCase("Runaway train", "train")); + + // it will also be true, because ignores case ;) + Assert.assertTrue(StringUtils.containsIgnoreCase("Runaway train", "Train")); + } + + @Test + public void searchUsingPattern() { + + // We create the Pattern first + Pattern pattern = Pattern.compile("(? + + 4.0.0 + kotlin-libraries-2 + kotlin-libraries-2 + jar + + + com.baeldung + parent-kotlin + 1.0.0-SNAPSHOT + ../parent-kotlin + + + + + com.fasterxml.jackson.module + jackson-module-kotlin + + + junit + junit + test + + + + + + + + + + diff --git a/kotlin-libraries-2/resources/logback.xml b/kotlin-libraries-2/resources/logback.xml new file mode 100644 index 0000000000..9452207268 --- /dev/null +++ b/kotlin-libraries-2/resources/logback.xml @@ -0,0 +1,11 @@ + + + + %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + \ No newline at end of file diff --git a/kotlin-libraries-2/src/main/kotlin/com/baeldung/kotlin/jackson/Book.kt b/kotlin-libraries-2/src/main/kotlin/com/baeldung/kotlin/jackson/Book.kt new file mode 100644 index 0000000000..4ff47ea987 --- /dev/null +++ b/kotlin-libraries-2/src/main/kotlin/com/baeldung/kotlin/jackson/Book.kt @@ -0,0 +1,8 @@ +package com.baeldung.kotlin.jackson + +import com.fasterxml.jackson.annotation.* + +@JsonInclude(JsonInclude.Include.NON_EMPTY) +data class Book(var title: String, @JsonProperty("author") var authorName: String) { + var genres: List? = emptyList() +} \ No newline at end of file diff --git a/kotlin-libraries-2/src/main/kotlin/com/baeldung/kotlin/jackson/Movie.kt b/kotlin-libraries-2/src/main/kotlin/com/baeldung/kotlin/jackson/Movie.kt new file mode 100644 index 0000000000..445b6013d5 --- /dev/null +++ b/kotlin-libraries-2/src/main/kotlin/com/baeldung/kotlin/jackson/Movie.kt @@ -0,0 +1,3 @@ +package com.baeldung.kotlin.jackson + +data class Movie(var name: String, var studio: String, var rating: Float? = 1f) \ No newline at end of file diff --git a/kotlin-libraries-2/src/test/kotlin/com/baeldung/kotlin/jackson/JacksonUnitTest.kt b/kotlin-libraries-2/src/test/kotlin/com/baeldung/kotlin/jackson/JacksonUnitTest.kt new file mode 100644 index 0000000000..84171d9019 --- /dev/null +++ b/kotlin-libraries-2/src/test/kotlin/com/baeldung/kotlin/jackson/JacksonUnitTest.kt @@ -0,0 +1,114 @@ +package com.baeldung.kotlin.jackson + +import org.junit.Test +import kotlin.test.assertTrue +import kotlin.test.assertFalse +import kotlin.test.assertEquals +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.KotlinModule + +class JacksonUnitTest { + //val mapper = jacksonObjectMapper() + val mapper = ObjectMapper().registerModule(KotlinModule()) + + + @Test + fun whenSerializeMovie_thenSuccess() { + val movie = Movie("Endgame", "Marvel", 9.2f) + val serialized = mapper.writeValueAsString(movie) + + val json = """{"name":"Endgame","studio":"Marvel","rating":9.2}""" + assertEquals(serialized, json) + } + + @Test + fun whenDeserializeMovie_thenSuccess() { + val json = """{"name":"Endgame","studio":"Marvel","rating":9.2}""" + // val movie: Movie = mapper.readValue(json) + val movie = mapper.readValue(json) + + assertEquals(movie.name, "Endgame") + assertEquals(movie.studio, "Marvel") + assertEquals(movie.rating, 9.2f) + } + + @Test + fun whenDeserializeMovieWithMissingValue_thenUseDefaultValue() { + val json = """{"name":"Endgame","studio":"Marvel"}""" + val movie: Movie = mapper.readValue(json) + + assertEquals(movie.name, "Endgame") + assertEquals(movie.studio, "Marvel") + assertEquals(movie.rating, 1f) + } + + @Test + fun whenSerializeMap_thenSuccess() { + val map = mapOf(1 to "one", 2 to "two") + val serialized = mapper.writeValueAsString(map) + + val json = """{"1":"one","2":"two"}""" + assertEquals(serialized, json) + } + + @Test + fun whenDeserializeMap_thenSuccess() { + val json = """{"1":"one","2":"two"}""" + val aMap: Map = mapper.readValue(json) + + assertEquals(aMap[1], "one") + assertEquals(aMap[2], "two") + } + + @Test + fun whenSerializeList_thenSuccess() { + val movie1 = Movie("Endgame", "Marvel", 9.2f) + val movie2 = Movie("Shazam", "Warner Bros", 7.6f) + val movieList = listOf(movie1, movie2) + val serialized = mapper.writeValueAsString(movieList) + + val json = """[{"name":"Endgame","studio":"Marvel","rating":9.2},{"name":"Shazam","studio":"Warner Bros","rating":7.6}]""" + assertEquals(serialized, json) + } + + @Test + fun whenDeserializeList_thenSuccess() { + val json = """[{"name":"Endgame","studio":"Marvel","rating":9.2},{"name":"Shazam","studio":"Warner Bros","rating":7.6}]""" + val movieList: List = mapper.readValue(json) + + val movie1 = Movie("Endgame", "Marvel", 9.2f) + val movie2 = Movie("Shazam", "Warner Bros", 7.6f) + assertTrue(movieList.contains(movie1)) + assertTrue(movieList.contains(movie2)) + } + + @Test + fun whenSerializeBook_thenSuccess() { + val book = Book("Oliver Twist", "Charles Dickens") + val serialized = mapper.writeValueAsString(book) + + val json = """{"title":"Oliver Twist","author":"Charles Dickens"}""" + assertEquals(serialized, json) + } + + @Test + fun whenDeserializeBook_thenSuccess() { + val json = """{"title":"Oliver Twist","author":"Charles Dickens"}""" + val book: Book = mapper.readValue(json) + + assertEquals(book.title, "Oliver Twist") + assertEquals(book.authorName, "Charles Dickens") + } + + @Test + fun givenJsonInclude_whenSerializeBook_thenEmptyFieldExcluded() { + val book = Book("Oliver Twist", "Charles Dickens") + val serialized = mapper.writeValueAsString(book) + + val json = """{"title":"Oliver Twist","author":"Charles Dickens"}""" + assertEquals(serialized, json) + } + +} \ No newline at end of file diff --git a/libraries-primitive/pom.xml b/libraries-primitive/pom.xml new file mode 100644 index 0000000000..9bb58470b2 --- /dev/null +++ b/libraries-primitive/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + com.baeldung + libraries-primitive + 1.0-SNAPSHOT + + + + + it.unimi.dsi + fastutil + 8.2.2 + + + + junit + junit + 4.12 + test + + + + org.openjdk.jmh + jmh-core + 1.19 + test + + + org.openjdk.jmh + jmh-generator-annprocess + 1.19 + test + + + diff --git a/fastUtil/src/test/java/com/baeldung/BigArraysUnitTest.java b/libraries-primitive/src/test/java/com/baeldung/BigArraysUnitTest.java similarity index 100% rename from fastUtil/src/test/java/com/baeldung/BigArraysUnitTest.java rename to libraries-primitive/src/test/java/com/baeldung/BigArraysUnitTest.java diff --git a/fastUtil/src/test/java/com/baeldung/FastUtilTypeSpecificBenchmarkUnitTest.java b/libraries-primitive/src/test/java/com/baeldung/FastUtilTypeSpecificBenchmarkUnitTest.java similarity index 100% rename from fastUtil/src/test/java/com/baeldung/FastUtilTypeSpecificBenchmarkUnitTest.java rename to libraries-primitive/src/test/java/com/baeldung/FastUtilTypeSpecificBenchmarkUnitTest.java diff --git a/fastUtil/src/test/java/com/baeldung/FastUtilTypeSpecificUnitTest.java b/libraries-primitive/src/test/java/com/baeldung/FastUtilTypeSpecificUnitTest.java similarity index 100% rename from fastUtil/src/test/java/com/baeldung/FastUtilTypeSpecificUnitTest.java rename to libraries-primitive/src/test/java/com/baeldung/FastUtilTypeSpecificUnitTest.java diff --git a/maven/profiles/pom.xml b/maven/profiles/pom.xml new file mode 100644 index 0000000000..110016f3a2 --- /dev/null +++ b/maven/profiles/pom.xml @@ -0,0 +1,88 @@ + + 4.0.0 + com.baeldung + profiles + 0.0.1-SNAPSHOT + profiles + + + + no-tests + + true + + + + integration-tests + + true + + + + mutation-tests + + + active-on-jdk-11 + + 11 + + + + active-on-windows-10 + + + windows 10 + Windows + amd64 + 10.0 + + + + + active-on-property-environment + + + environment + !test + + + + + active-on-missing-file + + + target/testreport.html + + + + + active-on-present-file + + + target/artifact.jar + + + + + + + + + org.apache.maven.plugins + maven-help-plugin + 3.2.0 + + + show-profiles + compile + + active-profiles + + + + + + + \ No newline at end of file diff --git a/parent-kotlin/pom.xml b/parent-kotlin/pom.xml index 86ab45ecb1..6f7fe6c102 100644 --- a/parent-kotlin/pom.xml +++ b/parent-kotlin/pom.xml @@ -108,6 +108,9 @@ ${project.basedir}/src/main/java ${java.version} + + -Xjvm-default=enable + diff --git a/persistence-modules/hibernate-mapping/pom.xml b/persistence-modules/hibernate-mapping/pom.xml index 11caf67bf1..fdc82a5275 100644 --- a/persistence-modules/hibernate-mapping/pom.xml +++ b/persistence-modules/hibernate-mapping/pom.xml @@ -31,6 +31,22 @@ h2 ${h2database.version} + + + org.hibernate + hibernate-validator + ${hibernate-validator.version} + + + javax.el + javax.el-api + ${javax.el-api.version} + + + org.glassfish + javax.el + ${org.glassfish.javax.el.version} + @@ -47,6 +63,9 @@ 5.3.7.Final 1.4.196 3.8.0 + 5.3.3.Final + 2.2.5 + 3.0.1-b08 \ No newline at end of file diff --git a/persistence-modules/hibernate-mapping/src/main/java/com/baeldung/hibernate/Strategy.java b/persistence-modules/hibernate-mapping/src/main/java/com/baeldung/hibernate/Strategy.java index 75f10ed583..78434fd2a2 100644 --- a/persistence-modules/hibernate-mapping/src/main/java/com/baeldung/hibernate/Strategy.java +++ b/persistence-modules/hibernate-mapping/src/main/java/com/baeldung/hibernate/Strategy.java @@ -9,7 +9,7 @@ public enum Strategy { //See that the classes belongs to different packages MAP_KEY_COLUMN_BASED(Collections.singletonList(com.baeldung.hibernate.persistmaps.mapkeycolumn.Order.class)), MAP_KEY_BASED(Arrays.asList(com.baeldung.hibernate.persistmaps.mapkey.Item.class, - com.baeldung.hibernate.persistmaps.mapkey.Order.class)), + com.baeldung.hibernate.persistmaps.mapkey.Order.class,com.baeldung.hibernate.persistmaps.mapkey.User.class)), MAP_KEY_JOIN_COLUMN_BASED(Arrays.asList(com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Seller.class, com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Item.class, com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Order.class)), diff --git a/persistence-modules/hibernate-mapping/src/main/java/com/baeldung/hibernate/persistmaps/mapkey/User.java b/persistence-modules/hibernate-mapping/src/main/java/com/baeldung/hibernate/persistmaps/mapkey/User.java new file mode 100644 index 0000000000..f6e8f1cdd6 --- /dev/null +++ b/persistence-modules/hibernate-mapping/src/main/java/com/baeldung/hibernate/persistmaps/mapkey/User.java @@ -0,0 +1,66 @@ +package com.baeldung.hibernate.persistmaps.mapkey; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.validation.constraints.Size; + +import org.hibernate.validator.constraints.Length; + +@Entity +public class User { + @Id + @Column(length = 3) + private String firstName; + + @Size(min = 3, max = 15) + private String middleName; + + @Length(min = 3, max = 15) + private String lastName; + + @Column(length = 5) + @Size(min = 3, max = 5) + private String city; + + public User(String firstName, String middleName, String lastName, String city) { + super(); + this.firstName = firstName; + this.middleName = middleName; + this.lastName = lastName; + this.city = city; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getMiddleName() { + return middleName; + } + + public void setMiddleName(String middletName) { + this.middleName = middletName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + +} diff --git a/persistence-modules/hibernate-mapping/src/test/java/com/baeldung/hibernate/validation/UserValidationUnitTest.java b/persistence-modules/hibernate-mapping/src/test/java/com/baeldung/hibernate/validation/UserValidationUnitTest.java new file mode 100644 index 0000000000..e39f324856 --- /dev/null +++ b/persistence-modules/hibernate-mapping/src/test/java/com/baeldung/hibernate/validation/UserValidationUnitTest.java @@ -0,0 +1,81 @@ +package com.baeldung.hibernate.validation; + +import static org.junit.Assert.assertEquals; +import java.util.Set; +import javax.persistence.PersistenceException; +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import com.baeldung.hibernate.HibernateUtil; +import com.baeldung.hibernate.Strategy; +import com.baeldung.hibernate.persistmaps.mapkey.User; + +public class UserValidationUnitTest { + + private static Validator validator; + private static SessionFactory sessionFactory; + private Session session; + private Set> constraintViolations; + + @BeforeClass + public static void before() { + ValidatorFactory config = Validation.buildDefaultValidatorFactory(); + validator = config.getValidator(); + sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_BASED); + } + + @Before + public void setUp() { + session = sessionFactory.openSession(); + session.beginTransaction(); + } + + @Test + public void whenValidationWithSizeAndInvalidMiddleName_thenConstraintViolation() { + User user = new User("john", "m", "butler", "japan"); + constraintViolations = validator.validateProperty(user, "middleName"); + assertEquals(constraintViolations.size(), 1); + } + + @Test + public void whenValidationWithSizeAndValidMiddleName_thenNoConstraintViolation() { + User user = new User("john", "mathis", "butler", "japan"); + constraintViolations = validator.validateProperty(user, "middleName"); + assertEquals(constraintViolations.size(), 0); + } + + @Test + public void whenValidationWithLengthAndInvalidLastName_thenConstraintViolation() { + User user = new User("john", "m", "b", "japan"); + constraintViolations = validator.validateProperty(user, "lastName"); + assertEquals(constraintViolations.size(), 1); + } + + @Test + public void whenValidationWithLengthAndValidLastName_thenNoConstraintViolation() { + User user = new User("john", "mathis", "butler", "japan"); + constraintViolations = validator.validateProperty(user, "lastName"); + assertEquals(constraintViolations.size(), 0); + } + + @Test(expected = PersistenceException.class) + public void whenSavingFirstNameWithInvalidFirstName_thenPersistenceException() { + User user = new User("john", "mathis", "butler", "japan"); + session.save(user); + session.getTransaction() + .commit(); + } + + @Test + public void whenValidationWithSizeAndColumnWithValidCity_thenNoConstraintViolation() { + User user = new User("john", "mathis", "butler", "japan"); + constraintViolations = validator.validateProperty(user, "city"); + assertEquals(constraintViolations.size(), 0); + } +} diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/Application.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/Application.java new file mode 100644 index 0000000000..3b96f68583 --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/Application.java @@ -0,0 +1,14 @@ +package com.baeldung.jpadefaultvalues.application; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; + +@SpringBootApplication +public class Application { + private static ApplicationContext applicationContext; + + public static void main(String[] args) { + applicationContext = SpringApplication.run(Application.class, args); + } +} diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/User.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/User.java new file mode 100644 index 0000000000..e271c01815 --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/User.java @@ -0,0 +1,55 @@ +package com.baeldung.jpadefaultvalues.application; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class User { + + @Id + @GeneratedValue + private Long id; + + @Column(columnDefinition = "varchar(255) default 'John Snow'") + private String name = "John Snow"; + + @Column(columnDefinition = "integer default 25") + private Integer age = 25; + + @Column(columnDefinition = "boolean default false") + private Boolean locked = false; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public Boolean getLocked() { + return locked; + } + + public void setLocked(Boolean locked) { + this.locked = locked; + } +} diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/UserRepository.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/UserRepository.java new file mode 100644 index 0000000000..ca0e4c4d71 --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/UserRepository.java @@ -0,0 +1,8 @@ +package com.baeldung.jpadefaultvalues.application; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface UserRepository extends JpaRepository { +} diff --git a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/jpadefaultvalues/application/UserDefaultValuesUnitTest.java b/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/jpadefaultvalues/application/UserDefaultValuesUnitTest.java new file mode 100644 index 0000000000..52a5c4dcd6 --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/jpadefaultvalues/application/UserDefaultValuesUnitTest.java @@ -0,0 +1,56 @@ +package com.baeldung.jpadefaultvalues.application; + +import com.baeldung.jpadefaultvalues.application.User; +import com.baeldung.jpadefaultvalues.application.UserRepository; + +import org.junit.Test; +import org.junit.Ignore; +import org.junit.runner.RunWith; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.beans.factory.annotation.Autowired; + +import static org.junit.Assert.*; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class UserDefaultValuesUnitTest { + + @Autowired + private UserRepository userRepository; + + @Test + @Ignore // SQL default values are also defined + public void saveUser_shouldSaveWithDefaultFieldValues() { + User user = new User(); + user = userRepository.save(user); + + assertEquals(user.getName(), "John Snow"); + assertEquals(25, (int) user.getAge()); + assertFalse(user.getLocked()); + } + + @Test + @Ignore // SQL default values are also defined + public void saveUser_shouldSaveWithNullName() { + User user = new User(); + user.setName(null); + user = userRepository.save(user); + + assertNull(user.getName()); + assertEquals(25, (int) user.getAge()); + assertFalse(user.getLocked()); + } + + @Test + public void saveUser_shouldSaveWithDefaultSqlValues() { + User user = new User(); + user = userRepository.save(user); + + assertEquals(user.getName(), "John Snow"); + assertEquals(25, (int) user.getAge()); + assertFalse(user.getLocked()); + } + +} diff --git a/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entitygraph/model/Characteristic.java b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entitygraph/model/Characteristic.java new file mode 100644 index 0000000000..ae20375572 --- /dev/null +++ b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entitygraph/model/Characteristic.java @@ -0,0 +1,43 @@ +package com.baeldung.entitygraph.model; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +@Entity +public class Characteristic { + + @Id + private Long id; + private String type; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn + private Item item; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public Item getItem() { + return item; + } + + public void setItem(Item item) { + this.item = item; + } +} diff --git a/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entitygraph/model/Item.java b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entitygraph/model/Item.java new file mode 100644 index 0000000000..e90a22ef62 --- /dev/null +++ b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entitygraph/model/Item.java @@ -0,0 +1,48 @@ +package com.baeldung.entitygraph.model; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.NamedAttributeNode; +import javax.persistence.NamedEntityGraph; +import javax.persistence.OneToMany; + +@Entity +@NamedEntityGraph(name = "Item.characteristics", + attributeNodes = @NamedAttributeNode("characteristics") +) +public class Item { + + @Id + private Long id; + private String name; + + @OneToMany(mappedBy = "item") + private List characteristics = new ArrayList<>(); + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getCharacteristics() { + return characteristics; + } + + public void setCharacteristics(List characteristics) { + this.characteristics = characteristics; + } +} diff --git a/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entitygraph/repository/CharacteristicsRepository.java b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entitygraph/repository/CharacteristicsRepository.java new file mode 100644 index 0000000000..9f923ab241 --- /dev/null +++ b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entitygraph/repository/CharacteristicsRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.entitygraph.repository; + +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.JpaRepository; + +import com.baeldung.entitygraph.model.Characteristic; + +public interface CharacteristicsRepository extends JpaRepository { + + @EntityGraph(attributePaths = {"item"}) + Characteristic findByType(String type); + +} diff --git a/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entitygraph/repository/ItemRepository.java b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entitygraph/repository/ItemRepository.java new file mode 100644 index 0000000000..b2a5f223b3 --- /dev/null +++ b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entitygraph/repository/ItemRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.entitygraph.repository; + +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType; +import org.springframework.data.jpa.repository.JpaRepository; + +import com.baeldung.entitygraph.model.Item; + +public interface ItemRepository extends JpaRepository { + + @EntityGraph(value = "Item.characteristics", type = EntityGraphType.FETCH) + Item findByName(String name); +} diff --git a/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/entitygraph/EntityGraphIntegrationTest.java b/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/entitygraph/EntityGraphIntegrationTest.java new file mode 100644 index 0000000000..24880a5dff --- /dev/null +++ b/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/entitygraph/EntityGraphIntegrationTest.java @@ -0,0 +1,39 @@ +package com.baeldung.entitygraph; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.entitygraph.model.Characteristic; +import com.baeldung.entitygraph.model.Item; +import com.baeldung.entitygraph.repository.CharacteristicsRepository; +import com.baeldung.entitygraph.repository.ItemRepository; + +@DataJpaTest +@RunWith(SpringRunner.class) +@Sql(scripts = "/entitygraph-data.sql") +public class EntityGraphIntegrationTest { + + @Autowired + private ItemRepository itemRepo; + + @Autowired + private CharacteristicsRepository characteristicsRepo; + + @Test + public void givenEntityGraph_whenCalled_shouldRetrunDefinedFields() { + Item item = itemRepo.findByName("Table"); + assertThat(item.getId()).isEqualTo(1L); + } + + @Test + public void givenAdhocEntityGraph_whenCalled_shouldRetrunDefinedFields() { + Characteristic characteristic = characteristicsRepo.findByType("Rigid"); + assertThat(characteristic.getId()).isEqualTo(1L); + } +} diff --git a/persistence-modules/spring-data-jpa-2/src/test/resources/entitygraph-data.sql b/persistence-modules/spring-data-jpa-2/src/test/resources/entitygraph-data.sql new file mode 100644 index 0000000000..685ec2c605 --- /dev/null +++ b/persistence-modules/spring-data-jpa-2/src/test/resources/entitygraph-data.sql @@ -0,0 +1,7 @@ +INSERT INTO Item(id,name) VALUES (1,'Table'); +INSERT INTO Item(id,name) VALUES (2,'Bottle'); + +INSERT INTO Characteristic(id,item_id, type) VALUES (1,1,'Rigid'); +INSERT INTO Characteristic(id,item_id,type) VALUES (2,1,'Big'); +INSERT INTO Characteristic(id,item_id,type) VALUES (3,2,'Fragile'); +INSERT INTO Characteristic(id,item_id,type) VALUES (4,2,'Small'); \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa/pom.xml b/persistence-modules/spring-data-jpa/pom.xml index c512994931..a7788065c9 100644 --- a/persistence-modules/spring-data-jpa/pom.xml +++ b/persistence-modules/spring-data-jpa/pom.xml @@ -89,5 +89,9 @@ test + + + com.baeldung.boot.Application + \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/Application.java similarity index 75% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/Application.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/Application.java index 72d29d9fa5..1f078801e2 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/Application.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/Application.java @@ -1,10 +1,11 @@ -package com.baeldung; +package com.baeldung.boot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; -import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl; +import com.baeldung.boot.daos.impl.ExtendedRepositoryImpl; +import com.baeldung.multipledb.MultipleDbApplication; @SpringBootApplication @EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class) diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/config/PersistenceConfiguration.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/config/PersistenceConfiguration.java new file mode 100644 index 0000000000..35a603d9f4 --- /dev/null +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/config/PersistenceConfiguration.java @@ -0,0 +1,21 @@ +package com.baeldung.boot.config; + +import com.baeldung.boot.services.IBarService; +import com.baeldung.boot.services.impl.BarSpringDataJpaService; +import org.springframework.context.annotation.*; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@Configuration +@Profile("!tc") +@EnableTransactionManagement +@EnableJpaAuditing +public class PersistenceConfiguration { + + @Bean + public IBarService barSpringDataJpaService() { + return new BarSpringDataJpaService(); + } + +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ArticleRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ArticleRepository.java similarity index 90% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ArticleRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ArticleRepository.java index 8402c099d9..73397ad42e 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ArticleRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ArticleRepository.java @@ -1,10 +1,11 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; -import com.baeldung.domain.Article; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import com.baeldung.boot.domain.Article; + import java.util.Date; import java.util.List; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/CustomItemRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/CustomItemRepository.java similarity index 74% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/CustomItemRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/CustomItemRepository.java index f5b94e507c..0aebe34921 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/CustomItemRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/CustomItemRepository.java @@ -1,8 +1,8 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; import org.springframework.stereotype.Repository; -import com.baeldung.domain.Item; +import com.baeldung.boot.domain.Item; @Repository public interface CustomItemRepository { diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/CustomItemTypeRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/CustomItemTypeRepository.java similarity index 71% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/CustomItemTypeRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/CustomItemTypeRepository.java index 81ebdf3fda..832d61408c 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/CustomItemTypeRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/CustomItemTypeRepository.java @@ -1,8 +1,8 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; import org.springframework.stereotype.Repository; -import com.baeldung.domain.ItemType; +import com.baeldung.boot.domain.ItemType; @Repository public interface CustomItemTypeRepository { diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/batchinserts/repository/CustomerRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/CustomerRepository.java similarity index 68% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/batchinserts/repository/CustomerRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/CustomerRepository.java index ab0214bade..2f1af6ac55 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/batchinserts/repository/CustomerRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/CustomerRepository.java @@ -1,8 +1,8 @@ -package com.baeldung.batchinserts.repository; +package com.baeldung.boot.daos; import org.springframework.data.repository.CrudRepository; -import com.baeldung.batchinserts.model.Customer; +import com.baeldung.boot.domain.Customer; /** * JPA CrudRepository interface diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ExtendedRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ExtendedRepository.java similarity index 90% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ExtendedRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ExtendedRepository.java index 9e82f02fa6..adb2af4320 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ExtendedRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ExtendedRepository.java @@ -1,4 +1,4 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; import java.io.Serializable; import java.util.List; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ExtendedStudentRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ExtendedStudentRepository.java similarity index 54% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ExtendedStudentRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ExtendedStudentRepository.java index 199e4e5ff6..c9b0192536 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ExtendedStudentRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ExtendedStudentRepository.java @@ -1,6 +1,6 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; -import com.baeldung.domain.Student; +import com.baeldung.boot.domain.Student; public interface ExtendedStudentRepository extends ExtendedRepository { } diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/IBarCrudRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/IBarCrudRepository.java similarity index 71% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/IBarCrudRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/IBarCrudRepository.java index 54a7d77691..921fabe3fb 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/IBarCrudRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/IBarCrudRepository.java @@ -1,8 +1,9 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; -import com.baeldung.domain.Bar; import org.springframework.data.repository.CrudRepository; +import com.baeldung.boot.domain.Bar; + import java.io.Serializable; public interface IBarCrudRepository extends CrudRepository { diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/IFooDao.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/IFooDao.java similarity index 83% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/IFooDao.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/IFooDao.java index bb3c229945..d537772076 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/IFooDao.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/IFooDao.java @@ -1,10 +1,11 @@ -package com.baeldung.dao; +package com.baeldung.boot.daos; -import com.baeldung.domain.Foo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import com.baeldung.boot.domain.Foo; + public interface IFooDao extends JpaRepository { @Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)") diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/InventoryRepository.java similarity index 63% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/InventoryRepository.java index a575f0b915..606f3993d5 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/InventoryRepository.java @@ -1,7 +1,8 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; -import com.baeldung.domain.MerchandiseEntity; import org.springframework.data.repository.CrudRepository; +import com.baeldung.boot.domain.MerchandiseEntity; + public interface InventoryRepository extends CrudRepository { } diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ItemTypeRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ItemTypeRepository.java similarity index 76% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ItemTypeRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ItemTypeRepository.java index 2af83bc322..413c09e968 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ItemTypeRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ItemTypeRepository.java @@ -1,9 +1,9 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import com.baeldung.domain.ItemType; +import com.baeldung.boot.domain.ItemType; @Repository public interface ItemTypeRepository extends JpaRepository, CustomItemTypeRepository, CustomItemRepository { diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/LocationRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/LocationRepository.java similarity index 72% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/LocationRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/LocationRepository.java index 27bbe27af0..697ce295d0 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/LocationRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/LocationRepository.java @@ -1,9 +1,9 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import com.baeldung.domain.Location; +import com.baeldung.boot.domain.Location; @Repository public interface LocationRepository extends JpaRepository { diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ReadOnlyLocationRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ReadOnlyLocationRepository.java similarity index 79% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ReadOnlyLocationRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ReadOnlyLocationRepository.java index 8f68cdbbe5..3a2ea3cda5 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/ReadOnlyLocationRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/ReadOnlyLocationRepository.java @@ -1,10 +1,10 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; import java.util.Optional; import org.springframework.data.repository.Repository; -import com.baeldung.domain.Location; +import com.baeldung.boot.domain.Location; @org.springframework.stereotype.Repository public interface ReadOnlyLocationRepository extends Repository { diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/StoreRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/StoreRepository.java similarity index 79% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/StoreRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/StoreRepository.java index 9318c32ee9..ae13f75f66 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/StoreRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/StoreRepository.java @@ -1,11 +1,11 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import com.baeldung.domain.Store; +import com.baeldung.boot.domain.Store; @Repository public interface StoreRepository extends JpaRepository { diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/CustomItemRepositoryImpl.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/impl/CustomItemRepositoryImpl.java similarity index 83% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/CustomItemRepositoryImpl.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/impl/CustomItemRepositoryImpl.java index 5538448c49..820a2cdd41 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/CustomItemRepositoryImpl.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/impl/CustomItemRepositoryImpl.java @@ -1,12 +1,12 @@ -package com.baeldung.dao.repositories.impl; +package com.baeldung.boot.daos.impl; import javax.persistence.EntityManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; -import com.baeldung.dao.repositories.CustomItemRepository; -import com.baeldung.domain.Item; +import com.baeldung.boot.daos.CustomItemRepository; +import com.baeldung.boot.domain.Item; @Repository public class CustomItemRepositoryImpl implements CustomItemRepository { diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/CustomItemTypeRepositoryImpl.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/impl/CustomItemTypeRepositoryImpl.java similarity index 84% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/CustomItemTypeRepositoryImpl.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/impl/CustomItemTypeRepositoryImpl.java index 2b49f2380c..d7cba7c2c6 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/CustomItemTypeRepositoryImpl.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/impl/CustomItemTypeRepositoryImpl.java @@ -1,4 +1,4 @@ -package com.baeldung.dao.repositories.impl; +package com.baeldung.boot.daos.impl; import javax.persistence.EntityManager; @@ -7,8 +7,8 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; -import com.baeldung.domain.ItemType; -import com.baeldung.dao.repositories.CustomItemTypeRepository; +import com.baeldung.boot.daos.CustomItemTypeRepository; +import com.baeldung.boot.domain.ItemType; @Repository public class CustomItemTypeRepositoryImpl implements CustomItemTypeRepository { diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/ExtendedRepositoryImpl.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/impl/ExtendedRepositoryImpl.java similarity index 93% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/ExtendedRepositoryImpl.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/impl/ExtendedRepositoryImpl.java index f6f06efb51..fbe6695844 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/ExtendedRepositoryImpl.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/impl/ExtendedRepositoryImpl.java @@ -1,4 +1,4 @@ -package com.baeldung.dao.repositories.impl; +package com.baeldung.boot.daos.impl; import java.io.Serializable; import java.util.List; @@ -10,10 +10,11 @@ import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import javax.transaction.Transactional; -import com.baeldung.dao.repositories.ExtendedRepository; import org.springframework.data.jpa.repository.support.JpaEntityInformation; import org.springframework.data.jpa.repository.support.SimpleJpaRepository; +import com.baeldung.boot.daos.ExtendedRepository; + public class ExtendedRepositoryImpl extends SimpleJpaRepository implements ExtendedRepository { private EntityManager entityManager; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/PersonInsertRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/impl/PersonInsertRepository.java similarity index 90% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/PersonInsertRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/impl/PersonInsertRepository.java index d809385456..373532e1c3 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/PersonInsertRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/impl/PersonInsertRepository.java @@ -1,8 +1,9 @@ -package com.baeldung.dao.repositories.impl; +package com.baeldung.boot.daos.impl; -import com.baeldung.domain.Person; import org.springframework.stereotype.Repository; +import com.baeldung.boot.domain.Person; + import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.transaction.Transactional; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/user/PossessionRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/user/PossessionRepository.java similarity index 62% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/user/PossessionRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/user/PossessionRepository.java index f0eeb475c1..e102754c18 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/user/PossessionRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/user/PossessionRepository.java @@ -1,8 +1,9 @@ -package com.baeldung.dao.repositories.user; +package com.baeldung.boot.daos.user; -import com.baeldung.domain.user.Possession; import org.springframework.data.jpa.repository.JpaRepository; +import com.baeldung.boot.domain.Possession; + public interface PossessionRepository extends JpaRepository { } diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/user/UserRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/user/UserRepository.java similarity index 92% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/user/UserRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/user/UserRepository.java index 1d05a17414..d6a060740d 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/user/UserRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/user/UserRepository.java @@ -1,6 +1,5 @@ -package com.baeldung.dao.repositories.user; +package com.baeldung.boot.daos.user; -import com.baeldung.domain.user.User; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; @@ -9,6 +8,8 @@ import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import com.baeldung.boot.domain.User; + import java.time.LocalDate; import java.util.Collection; import java.util.List; @@ -18,11 +19,11 @@ public interface UserRepository extends JpaRepository , UserRepos Stream findAllByName(String name); - @Query("select u from User u where u.email like '%@gmail.com'") - List findUsersWithGmailAddress(); - @Query("SELECT u FROM User u WHERE u.status = 1") Collection findAllActiveUsers(); + + @Query("select u from User u where u.email like '%@gmail.com'") + List findUsersWithGmailAddress(); @Query(value = "SELECT * FROM Users u WHERE u.status = 1", nativeQuery = true) Collection findAllActiveUsersNative(); @@ -74,14 +75,14 @@ public interface UserRepository extends JpaRepository , UserRepos @Query(value = "INSERT INTO Users (name, age, email, status, active) VALUES (:name, :age, :email, :status, :active)", nativeQuery = true) @Modifying void insertUser(@Param("name") String name, @Param("age") Integer age, @Param("email") String email, @Param("status") Integer status, @Param("active") boolean active); - + @Modifying @Query(value = "UPDATE Users u SET status = ? WHERE u.name = ?", nativeQuery = true) int updateUserSetStatusForNameNativePostgres(Integer status, String name); - + @Query(value = "SELECT u FROM User u WHERE u.name IN :names") - List findUserByNameList(@Param("names") Collection names); - + List findUserByNameList(@Param("names") Collection names); + void deleteAllByCreationDateAfter(LocalDate date); @Modifying(clearAutomatically = true, flushAutomatically = true) @@ -89,11 +90,10 @@ public interface UserRepository extends JpaRepository , UserRepos void deactivateUsersNotLoggedInSince(@Param("date") LocalDate date); @Modifying(clearAutomatically = true, flushAutomatically = true) - @Query("delete from User u where u.active = false") + @Query("delete User u where u.active = false") int deleteDeactivatedUsers(); @Modifying(clearAutomatically = true, flushAutomatically = true) - @Query(value = "alter table Users add column deleted int(1) not null default 0", nativeQuery = true) + @Query(value = "alter table USERS.USERS add column deleted int(1) not null default 0", nativeQuery = true) void addDeletedColumn(); - } diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/user/UserRepositoryCustom.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/user/UserRepositoryCustom.java similarity index 77% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/user/UserRepositoryCustom.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/user/UserRepositoryCustom.java index 0f29cd656e..c586b54027 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/user/UserRepositoryCustom.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/user/UserRepositoryCustom.java @@ -1,14 +1,14 @@ -package com.baeldung.dao.repositories.user; +package com.baeldung.boot.daos.user; import java.util.Collection; import java.util.List; import java.util.Set; import java.util.function.Predicate; -import com.baeldung.domain.user.User; +import com.baeldung.boot.domain.User; public interface UserRepositoryCustom { List findUserByEmails(Set emails); - + List findAllUsersByPredicates(Collection> predicates); } diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/user/UserRepositoryCustomImpl.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/user/UserRepositoryCustomImpl.java similarity index 95% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/user/UserRepositoryCustomImpl.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/user/UserRepositoryCustomImpl.java index 7b2a7ab123..63a743b6b5 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/user/UserRepositoryCustomImpl.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/daos/user/UserRepositoryCustomImpl.java @@ -1,4 +1,4 @@ -package com.baeldung.dao.repositories.user; +package com.baeldung.boot.daos.user; import java.util.ArrayList; import java.util.Collection; @@ -15,7 +15,7 @@ import javax.persistence.criteria.Path; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; -import com.baeldung.domain.user.User; +import com.baeldung.boot.domain.User; public class UserRepositoryCustomImpl implements UserRepositoryCustom { @@ -42,7 +42,7 @@ public class UserRepositoryCustomImpl implements UserRepositoryCustom { return entityManager.createQuery(query) .getResultList(); } - + @Override public List findAllUsersByPredicates(Collection> predicates) { List allUsers = entityManager.createQuery("select u from User u", User.class).getResultList(); diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate.java similarity index 95% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate.java index bf6ff0a0b9..e435f4c85c 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.ddd.event; +package com.baeldung.boot.ddd.event; import javax.persistence.Entity; import javax.persistence.Id; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate2.java similarity index 95% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate2.java index 3d2816299a..08f61db812 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate2.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.ddd.event; +package com.baeldung.boot.ddd.event; import java.util.ArrayList; import java.util.Collection; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2Repository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate2Repository.java similarity index 80% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2Repository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate2Repository.java index 2a95abe347..7f09c410fc 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2Repository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate2Repository.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.ddd.event; +package com.baeldung.boot.ddd.event; import org.springframework.data.repository.CrudRepository; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate3.java similarity index 91% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate3.java index e0c3131b06..f664322a59 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate3.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.ddd.event; +package com.baeldung.boot.ddd.event; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3Repository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate3Repository.java similarity index 83% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3Repository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate3Repository.java index e442bdb210..93f50bb5cf 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3Repository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/Aggregate3Repository.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.ddd.event; +package com.baeldung.boot.ddd.event; import org.springframework.data.repository.CrudRepository; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/AggregateRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/AggregateRepository.java similarity index 80% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/AggregateRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/AggregateRepository.java index 5a15156d03..1c2d3884bf 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/AggregateRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/AggregateRepository.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.ddd.event; +package com.baeldung.boot.ddd.event; import org.springframework.data.repository.CrudRepository; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DddConfig.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/DddConfig.java similarity index 89% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DddConfig.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/DddConfig.java index 1315b11875..34cf21463b 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DddConfig.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/DddConfig.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.ddd.event; +package com.baeldung.boot.ddd.event; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/DomainEvent.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/DomainEvent.java new file mode 100644 index 0000000000..e7626e742d --- /dev/null +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/DomainEvent.java @@ -0,0 +1,8 @@ +/** + * + */ +package com.baeldung.boot.ddd.event; + +class DomainEvent { + +} diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainService.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/DomainService.java similarity index 95% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainService.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/DomainService.java index 082c9bd88e..80aa5ca6cb 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainService.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/ddd/event/DomainService.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.ddd.event; +package com.baeldung.boot.ddd.event; import javax.transaction.Transactional; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Article.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Article.java similarity index 92% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Article.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Article.java index 3b5a8be088..de4dbed1a0 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Article.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Article.java @@ -1,4 +1,4 @@ -package com.baeldung.domain; +package com.baeldung.boot.domain; import javax.persistence.*; import java.util.Date; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Bar.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Bar.java similarity index 99% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Bar.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Bar.java index efd297bafc..35d1903801 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Bar.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Bar.java @@ -1,4 +1,4 @@ -package com.baeldung.domain; +package com.baeldung.boot.domain; import com.google.common.collect.Sets; import org.hibernate.annotations.OrderBy; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/batchinserts/model/Customer.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Customer.java similarity index 96% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/batchinserts/model/Customer.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Customer.java index 4d82cf12a2..af88be0be6 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/batchinserts/model/Customer.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Customer.java @@ -1,4 +1,4 @@ -package com.baeldung.batchinserts.model; +package com.baeldung.boot.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Foo.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Foo.java similarity index 98% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Foo.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Foo.java index ef88840746..5030e5600b 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Foo.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Foo.java @@ -1,4 +1,4 @@ -package com.baeldung.domain; +package com.baeldung.boot.domain; import org.hibernate.envers.Audited; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Item.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Item.java similarity index 97% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Item.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Item.java index 1e58fb25ba..8ac06af15a 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Item.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Item.java @@ -1,4 +1,4 @@ -package com.baeldung.domain; +package com.baeldung.boot.domain; import java.math.BigDecimal; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/ItemType.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/ItemType.java similarity index 96% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/ItemType.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/ItemType.java index b0349e0471..8a52a9847c 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/ItemType.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/ItemType.java @@ -1,4 +1,4 @@ -package com.baeldung.domain; +package com.baeldung.boot.domain; import java.util.ArrayList; import java.util.List; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/KVTag.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/KVTag.java similarity index 94% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/KVTag.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/KVTag.java index b3e7d78b30..1901f43c0a 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/KVTag.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/KVTag.java @@ -1,4 +1,4 @@ -package com.baeldung.domain; +package com.baeldung.boot.domain; import javax.persistence.Embeddable; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Location.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Location.java similarity index 96% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Location.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Location.java index 4ca7295986..9c1b93d551 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Location.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Location.java @@ -1,4 +1,4 @@ -package com.baeldung.domain; +package com.baeldung.boot.domain; import java.util.ArrayList; import java.util.List; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/MerchandiseEntity.java similarity index 97% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/MerchandiseEntity.java index bfc690e0e2..e94c23de86 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/MerchandiseEntity.java @@ -1,4 +1,4 @@ -package com.baeldung.domain; +package com.baeldung.boot.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Person.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Person.java similarity index 95% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Person.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Person.java index 30d7370982..88894ccc72 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Person.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Person.java @@ -1,4 +1,4 @@ -package com.baeldung.domain; +package com.baeldung.boot.domain; import javax.persistence.Entity; import javax.persistence.Id; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/user/Possession.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Possession.java similarity index 95% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/user/Possession.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Possession.java index b1427c0270..f13491ad82 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/user/Possession.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Possession.java @@ -1,6 +1,7 @@ -package com.baeldung.domain.user; +package com.baeldung.boot.domain; import javax.persistence.*; +import com.baeldung.boot.domain.Possession; @Entity @Table diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/SkillTag.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/SkillTag.java similarity index 93% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/SkillTag.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/SkillTag.java index 1f2778c589..0933a3e6af 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/SkillTag.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/SkillTag.java @@ -1,4 +1,4 @@ -package com.baeldung.domain; +package com.baeldung.boot.domain; import javax.persistence.Embeddable; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Store.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Store.java similarity index 97% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Store.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Store.java index 4172051c71..5b4b831cc7 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Store.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Store.java @@ -1,4 +1,4 @@ -package com.baeldung.domain; +package com.baeldung.boot.domain; import java.util.ArrayList; import java.util.List; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Student.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Student.java similarity index 97% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Student.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Student.java index bd7eaeb24b..1003167cc7 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/Student.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/Student.java @@ -1,4 +1,4 @@ -package com.baeldung.domain; +package com.baeldung.boot.domain; import javax.persistence.ElementCollection; import javax.persistence.Entity; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/user/User.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/User.java similarity index 96% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/user/User.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/User.java index 7109271eeb..cca00e52a2 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/user/User.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/domain/User.java @@ -1,6 +1,7 @@ -package com.baeldung.domain.user; +package com.baeldung.boot.domain; import javax.persistence.*; + import java.time.LocalDate; import java.util.List; import java.util.Objects; @@ -27,7 +28,7 @@ public class User { super(); } - public User(String name, LocalDate creationDate, String email, Integer status) { + public User(String name, LocalDate creationDate,String email, Integer status) { this.name = name; this.creationDate = creationDate; this.email = email; @@ -51,10 +52,6 @@ public class User { this.name = name; } - public LocalDate getCreationDate() { - return creationDate; - } - public String getEmail() { return email; } @@ -78,6 +75,10 @@ public class User { public void setAge(final int age) { this.age = age; } + + public LocalDate getCreationDate() { + return creationDate; + } public List getPossessionList() { return possessionList; @@ -93,7 +94,7 @@ public class User { builder.append("User [name=").append(name).append(", id=").append(id).append("]"); return builder.toString(); } - + @Override public boolean equals(Object o) { if (this == o) return true; @@ -127,4 +128,5 @@ public class User { public void setActive(boolean active) { this.active = active; } + } \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/CustomPassengerRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/passenger/CustomPassengerRepository.java similarity index 77% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/CustomPassengerRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/passenger/CustomPassengerRepository.java index 7ae44bfbda..7152286c83 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/CustomPassengerRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/passenger/CustomPassengerRepository.java @@ -1,4 +1,4 @@ -package com.baeldung.passenger; +package com.baeldung.boot.passenger; import java.util.List; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/Passenger.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/passenger/Passenger.java similarity index 98% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/Passenger.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/passenger/Passenger.java index a96b1edb20..c75107a783 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/Passenger.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/passenger/Passenger.java @@ -1,4 +1,4 @@ -package com.baeldung.passenger; +package com.baeldung.boot.passenger; import javax.persistence.Basic; import javax.persistence.Column; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/PassengerRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/passenger/PassengerRepository.java similarity index 94% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/PassengerRepository.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/passenger/PassengerRepository.java index 6c6c145942..cdb8cc98fb 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/PassengerRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/passenger/PassengerRepository.java @@ -1,4 +1,4 @@ -package com.baeldung.passenger; +package com.baeldung.boot.passenger; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.repository.JpaRepository; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/PassengerRepositoryImpl.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/passenger/PassengerRepositoryImpl.java similarity index 93% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/PassengerRepositoryImpl.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/passenger/PassengerRepositoryImpl.java index bd6e535e3e..508c669066 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/PassengerRepositoryImpl.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/passenger/PassengerRepositoryImpl.java @@ -1,4 +1,4 @@ -package com.baeldung.passenger; +package com.baeldung.boot.passenger; import org.springframework.stereotype.Repository; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/IBarService.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/IBarService.java new file mode 100644 index 0000000000..8054cbba59 --- /dev/null +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/IBarService.java @@ -0,0 +1,7 @@ +package com.baeldung.boot.services; + +import com.baeldung.boot.domain.Bar; + +public interface IBarService extends IOperations { + // +} diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/IFooService.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/IFooService.java similarity index 76% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/IFooService.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/IFooService.java index 7e16ace5b6..871cccdd45 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/IFooService.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/IFooService.java @@ -1,9 +1,10 @@ -package com.baeldung.services; +package com.baeldung.boot.services; -import com.baeldung.domain.Foo; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import com.baeldung.boot.domain.Foo; + public interface IFooService extends IOperations { Foo retrieveByName(String name); diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/IOperations.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/IOperations.java similarity index 92% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/IOperations.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/IOperations.java index d50d465639..ec2b866b6f 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/IOperations.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/IOperations.java @@ -1,4 +1,4 @@ -package com.baeldung.services; +package com.baeldung.boot.services; import org.springframework.data.domain.Page; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/impl/AbstractService.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/impl/AbstractService.java similarity index 94% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/impl/AbstractService.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/impl/AbstractService.java index 708524225b..8e4f643adc 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/impl/AbstractService.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/impl/AbstractService.java @@ -1,6 +1,6 @@ -package com.baeldung.services.impl; +package com.baeldung.boot.services.impl; -import com.baeldung.services.IOperations; +import com.baeldung.boot.services.IOperations; import com.google.common.collect.Lists; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/impl/AbstractSpringDataJpaService.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/impl/AbstractSpringDataJpaService.java similarity index 92% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/impl/AbstractSpringDataJpaService.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/impl/AbstractSpringDataJpaService.java index 28c86bee28..a73a6bd7fc 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/impl/AbstractSpringDataJpaService.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/impl/AbstractSpringDataJpaService.java @@ -1,6 +1,6 @@ -package com.baeldung.services.impl; +package com.baeldung.boot.services.impl; -import com.baeldung.services.IOperations; +import com.baeldung.boot.services.IOperations; import com.google.common.collect.Lists; import org.springframework.data.repository.CrudRepository; import org.springframework.transaction.annotation.Transactional; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/impl/BarSpringDataJpaService.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/impl/BarSpringDataJpaService.java similarity index 79% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/impl/BarSpringDataJpaService.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/impl/BarSpringDataJpaService.java index ca3e5f868d..80568f2fd4 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/impl/BarSpringDataJpaService.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/impl/BarSpringDataJpaService.java @@ -1,8 +1,9 @@ -package com.baeldung.services.impl; +package com.baeldung.boot.services.impl; + +import com.baeldung.boot.daos.IBarCrudRepository; +import com.baeldung.boot.domain.Bar; +import com.baeldung.boot.services.IBarService; -import com.baeldung.domain.Bar; -import com.baeldung.dao.repositories.IBarCrudRepository; -import com.baeldung.services.IBarService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.repository.CrudRepository; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/impl/FooService.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/impl/FooService.java similarity index 87% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/impl/FooService.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/impl/FooService.java index 319ab3a825..04eec63854 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/impl/FooService.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/services/impl/FooService.java @@ -1,9 +1,10 @@ -package com.baeldung.services.impl; +package com.baeldung.boot.services.impl; import com.google.common.collect.Lists; -import com.baeldung.dao.IFooDao; -import com.baeldung.domain.Foo; -import com.baeldung.services.IFooService; +import com.baeldung.boot.daos.IFooDao; +import com.baeldung.boot.domain.Foo; +import com.baeldung.boot.services.IFooService; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/batchinserts/CustomerController.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/web/controllers/CustomerController.java similarity index 88% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/batchinserts/CustomerController.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/web/controllers/CustomerController.java index 7623d4d166..69a1e5507e 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/batchinserts/CustomerController.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/boot/web/controllers/CustomerController.java @@ -1,4 +1,4 @@ -package com.baeldung.batchinserts; +package com.baeldung.boot.web.controllers; import java.net.URISyntaxException; import java.util.Arrays; @@ -9,8 +9,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; -import com.baeldung.batchinserts.model.Customer; -import com.baeldung.batchinserts.repository.CustomerRepository; +import com.baeldung.boot.daos.CustomerRepository; +import com.baeldung.boot.domain.Customer; /** * A simple controller to test the JPA CrudRepository operations diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/config/PersistenceConfiguration.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/config/PersistenceConfiguration.java deleted file mode 100644 index 891624443b..0000000000 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/config/PersistenceConfiguration.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.baeldung.config; - -import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl; -import com.baeldung.services.IBarService; -import com.baeldung.services.impl.BarSpringDataJpaService; -import com.google.common.base.Preconditions; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.*; -import org.springframework.core.env.Environment; -import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; -import org.springframework.data.jpa.repository.config.EnableJpaAuditing; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; -import org.springframework.jdbc.datasource.DriverManagerDataSource; -import org.springframework.orm.jpa.JpaTransactionManager; -import org.springframework.orm.jpa.JpaVendorAdapter; -import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; -import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -import javax.sql.DataSource; -import java.util.Properties; - -@Configuration -@ComponentScan({ "com.baeldung.dao", "com.baeldung.services" }) -@EnableTransactionManagement -@EnableJpaRepositories(basePackages = { "com.baeldung.dao" }, repositoryBaseClass = ExtendedRepositoryImpl.class) -@EnableJpaAuditing -@PropertySource("classpath:persistence.properties") -@Profile("!tc") -public class PersistenceConfiguration { - - @Autowired - private Environment env; - - public PersistenceConfiguration() { - super(); - } - - @Bean - public LocalContainerEntityManagerFactoryBean entityManagerFactory() { - final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); - emf.setDataSource(dataSource()); - emf.setPackagesToScan("com.baeldung.domain"); - - final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); - emf.setJpaVendorAdapter(vendorAdapter); - emf.setJpaProperties(hibernateProperties()); - - return emf; - } - - @Bean - public DataSource dataSource() { - final DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); - dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url"))); - dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user"))); - dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass"))); - - return dataSource; - } - - @Bean - public PlatformTransactionManager transactionManager() { - final JpaTransactionManager transactionManager = new JpaTransactionManager(); - transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); - return transactionManager; - } - - @Bean - public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { - return new PersistenceExceptionTranslationPostProcessor(); - } - - @Bean - public IBarService barSpringDataJpaService() { - return new BarSpringDataJpaService(); - } - - private final Properties hibernateProperties() { - final Properties hibernateProperties = new Properties(); - hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); - hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); - - hibernateProperties.setProperty("hibernate.show_sql", "true"); - // hibernateProperties.setProperty("hibernate.format_sql", "true"); - // hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true"); - - // Envers properties - hibernateProperties.setProperty("org.hibernate.envers.audit_table_suffix", - env.getProperty("envers.audit_table_suffix")); - - return hibernateProperties; - } - -} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/product/ProductRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/product/ProductRepository.java deleted file mode 100755 index 1f9f5f9195..0000000000 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/product/ProductRepository.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.dao.repositories.product; - -import com.baeldung.domain.product.Product; - -import java.util.List; - -import org.springframework.data.domain.Pageable; -import org.springframework.data.repository.PagingAndSortingRepository; - -public interface ProductRepository extends PagingAndSortingRepository { - - List findAllByPrice(double price, Pageable pageable); -} diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainEvent.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainEvent.java deleted file mode 100644 index 1e6479d4fc..0000000000 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainEvent.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -package com.baeldung.ddd.event; - -class DomainEvent { - -} diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/MultipleDbApplication.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/MultipleDbApplication.java new file mode 100644 index 0000000000..8ff6799e31 --- /dev/null +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/MultipleDbApplication.java @@ -0,0 +1,14 @@ +package com.baeldung.multipledb; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MultipleDbApplication { + + public static void main(String[] args) { + SpringApplication.run(MultipleDbApplication.class, args); + } + +} + diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/config/PersistenceProductConfiguration.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/PersistenceProductConfiguration.java similarity index 90% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/config/PersistenceProductConfiguration.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/PersistenceProductConfiguration.java index ecaee82ae5..bcf2cd84eb 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/config/PersistenceProductConfiguration.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/PersistenceProductConfiguration.java @@ -1,4 +1,4 @@ -package com.baeldung.config; +package com.baeldung.multipledb; import com.google.common.base.Preconditions; import org.springframework.beans.factory.annotation.Autowired; @@ -19,7 +19,7 @@ import java.util.HashMap; @Configuration @PropertySource({"classpath:persistence-multiple-db.properties"}) -@EnableJpaRepositories(basePackages = "com.baeldung.dao.repositories.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager") +@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager") @Profile("!tc") public class PersistenceProductConfiguration { @Autowired @@ -35,7 +35,7 @@ public class PersistenceProductConfiguration { public LocalContainerEntityManagerFactoryBean productEntityManager() { final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(productDataSource()); - em.setPackagesToScan("com.baeldung.domain.product"); + em.setPackagesToScan("com.baeldung.multipledb.model.product"); final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/config/PersistenceUserConfiguration.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/PersistenceUserConfiguration.java similarity index 88% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/config/PersistenceUserConfiguration.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/PersistenceUserConfiguration.java index 6893d889e6..6b48455c0c 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/config/PersistenceUserConfiguration.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/PersistenceUserConfiguration.java @@ -1,4 +1,4 @@ -package com.baeldung.config; +package com.baeldung.multipledb; import com.google.common.base.Preconditions; import org.springframework.beans.factory.annotation.Autowired; @@ -16,7 +16,7 @@ import java.util.HashMap; @Configuration @PropertySource({"classpath:persistence-multiple-db.properties"}) -@EnableJpaRepositories(basePackages = "com.baeldung.dao.repositories.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager") +@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager") @Profile("!tc") public class PersistenceUserConfiguration { @Autowired @@ -31,9 +31,10 @@ public class PersistenceUserConfiguration { @Primary @Bean public LocalContainerEntityManagerFactoryBean userEntityManager() { + System.out.println("loading config"); final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(userDataSource()); - em.setPackagesToScan("com.baeldung.domain.user"); + em.setPackagesToScan("com.baeldung.multipledb.model.user"); final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/dao/product/ProductRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/dao/product/ProductRepository.java new file mode 100755 index 0000000000..022099eed0 --- /dev/null +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/dao/product/ProductRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.multipledb.dao.product; + +import java.util.List; + +import org.springframework.data.domain.Pageable; +import org.springframework.data.repository.PagingAndSortingRepository; + +import com.baeldung.multipledb.model.product.ProductMultipleDB; + +public interface ProductRepository extends PagingAndSortingRepository { + + List findAllByPrice(double price, Pageable pageable); +} diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/dao/user/PossessionRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/dao/user/PossessionRepository.java new file mode 100644 index 0000000000..ae37fde20d --- /dev/null +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/dao/user/PossessionRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.multipledb.dao.user; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.baeldung.multipledb.model.user.PossessionMultipleDB; + +public interface PossessionRepository extends JpaRepository { + +} diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/dao/user/UserRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/dao/user/UserRepository.java new file mode 100644 index 0000000000..267a61a93f --- /dev/null +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/dao/user/UserRepository.java @@ -0,0 +1,8 @@ +package com.baeldung.multipledb.dao.user; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.baeldung.multipledb.model.user.UserMultipleDB; + +public interface UserRepository extends JpaRepository { +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/product/Product.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/model/product/ProductMultipleDB.java similarity index 76% rename from persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/product/Product.java rename to persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/model/product/ProductMultipleDB.java index 2f82e3e318..8bdff340ac 100755 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/product/Product.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/model/product/ProductMultipleDB.java @@ -1,4 +1,4 @@ -package com.baeldung.domain.product; +package com.baeldung.multipledb.model.product; import javax.persistence.Entity; import javax.persistence.Id; @@ -6,7 +6,7 @@ import javax.persistence.Table; @Entity @Table(schema = "products") -public class Product { +public class ProductMultipleDB { @Id private int id; @@ -15,19 +15,19 @@ public class Product { private double price; - public Product() { + public ProductMultipleDB() { super(); } - private Product(int id, String name, double price) { + private ProductMultipleDB(int id, String name, double price) { super(); this.id = id; this.name = name; this.price = price; } - public static Product from(int id, String name, double price) { - return new Product(id, name, price); + public static ProductMultipleDB from(int id, String name, double price) { + return new ProductMultipleDB(id, name, price); } public int getId() { diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/model/user/PossessionMultipleDB.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/model/user/PossessionMultipleDB.java new file mode 100644 index 0000000000..a6a3c88bd0 --- /dev/null +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/model/user/PossessionMultipleDB.java @@ -0,0 +1,82 @@ +package com.baeldung.multipledb.model.user; + +import javax.persistence.*; + +@Entity +@Table +public class PossessionMultipleDB { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + private String name; + + public PossessionMultipleDB() { + super(); + } + + public PossessionMultipleDB(final String name) { + super(); + + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(final int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = (prime * result) + (int) (id ^ (id >>> 32)); + result = (prime * result) + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final PossessionMultipleDB other = (PossessionMultipleDB) obj; + if (id != other.id) { + return false; + } + if (name == null) { + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + return false; + } + return true; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Possesion [id=").append(id).append(", name=").append(name).append("]"); + return builder.toString(); + } + +} diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/model/user/UserMultipleDB.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/model/user/UserMultipleDB.java new file mode 100644 index 0000000000..c7cd07f7a1 --- /dev/null +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/multipledb/model/user/UserMultipleDB.java @@ -0,0 +1,88 @@ +package com.baeldung.multipledb.model.user; + +import javax.persistence.*; + +import java.util.List; + +@Entity +@Table(name = "users") +public class UserMultipleDB { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private int id; + private String name; + private int age; + @Column(unique = true, nullable = false) + private String email; + private Integer status; + + @OneToMany + List possessionList; + + public UserMultipleDB() { + super(); + } + + public UserMultipleDB(String name, String email, Integer status) { + this.name = name; + this.email = email; + this.status = status; + } + + public int getId() { + return id; + } + + public void setId(final int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(final String email) { + this.email = email; + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } + + public int getAge() { + return age; + } + + public void setAge(final int age) { + this.age = age; + } + + public List getPossessionList() { + return possessionList; + } + + public void setPossessionList(List possessionList) { + this.possessionList = possessionList; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("User [name=").append(name).append(", id=").append(id).append("]"); + return builder.toString(); + } + +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/IBarService.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/IBarService.java deleted file mode 100644 index 7e127488db..0000000000 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/services/IBarService.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.baeldung.services; - -import com.baeldung.domain.Bar; - -public interface IBarService extends IOperations { - // -} diff --git a/persistence-modules/spring-data-jpa/src/main/resources/application.properties b/persistence-modules/spring-data-jpa/src/main/resources/application.properties index 239f81db7b..f127dd5e50 100644 --- a/persistence-modules/spring-data-jpa/src/main/resources/application.properties +++ b/persistence-modules/spring-data-jpa/src/main/resources/application.properties @@ -1,22 +1,6 @@ -# spring.datasource.x -spring.datasource.driver-class-name=org.h2.Driver -spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 -spring.datasource.username=sa -spring.datasource.password=sa - -# hibernate.X -hibernate.dialect=org.hibernate.dialect.H2Dialect -hibernate.show_sql=true -hibernate.hbm2ddl.auto=create-drop -hibernate.cache.use_second_level_cache=true -hibernate.cache.use_query_cache=true -hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory - -spring.datasource.data=import_entities.sql - spring.main.allow-bean-definition-overriding=true 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 +spring.jpa.properties.hibernate.generate_statistics=true diff --git a/persistence-modules/spring-data-jpa/src/main/resources/persistence.properties b/persistence-modules/spring-data-jpa/src/main/resources/persistence.properties index 3543e1b52b..6bc83edf34 100644 --- a/persistence-modules/spring-data-jpa/src/main/resources/persistence.properties +++ b/persistence-modules/spring-data-jpa/src/main/resources/persistence.properties @@ -12,5 +12,3 @@ hibernate.cache.use_second_level_cache=true hibernate.cache.use_query_cache=true hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory -# envers.X -envers.audit_table_suffix=_audit_log \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/batchinserts/BatchInsertIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/batchinserts/BatchInsertIntegrationTest.java index f60e0d21bf..7ddf36d3f0 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/batchinserts/BatchInsertIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/batchinserts/BatchInsertIntegrationTest.java @@ -9,21 +9,17 @@ 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.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import com.baeldung.batchinserts.CustomerController; -import com.baeldung.batchinserts.repository.CustomerRepository; -import com.baeldung.config.PersistenceConfiguration; -import com.baeldung.config.PersistenceProductConfiguration; -import com.baeldung.config.PersistenceUserConfiguration; +import com.baeldung.boot.Application; +import com.baeldung.boot.daos.CustomerRepository; +import com.baeldung.boot.web.controllers.CustomerController; @RunWith(SpringRunner.class) -@SpringBootTest +@SpringBootTest(classes=Application.class) @AutoConfigureMockMvc -@ContextConfiguration(classes = { PersistenceConfiguration.class, PersistenceProductConfiguration.class, PersistenceUserConfiguration.class }) public class BatchInsertIntegrationTest { @Autowired diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/ArticleRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/ArticleRepositoryIntegrationTest.java similarity index 82% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/ArticleRepositoryIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/ArticleRepositoryIntegrationTest.java index 093e744003..dfb04b3dfb 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/ArticleRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/ArticleRepositoryIntegrationTest.java @@ -1,13 +1,16 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; + +import com.baeldung.boot.Application; +import com.baeldung.boot.config.PersistenceConfiguration; +import com.baeldung.boot.daos.ArticleRepository; +import com.baeldung.boot.domain.Article; -import com.baeldung.config.PersistenceConfiguration; -import com.baeldung.config.PersistenceProductConfiguration; -import com.baeldung.config.PersistenceUserConfiguration; -import com.baeldung.domain.Article; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; import org.springframework.test.context.junit4.SpringRunner; import java.text.SimpleDateFormat; @@ -18,7 +21,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @RunWith(SpringRunner.class) -@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class}) +@DataJpaTest(properties="spring.datasource.data=classpath:import_entities.sql") public class ArticleRepositoryIntegrationTest { @Autowired diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/ExtendedStudentRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/ExtendedStudentRepositoryIntegrationTest.java similarity index 80% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/ExtendedStudentRepositoryIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/ExtendedStudentRepositoryIntegrationTest.java index b19a34df82..66de5911db 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/ExtendedStudentRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/ExtendedStudentRepositoryIntegrationTest.java @@ -1,7 +1,10 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; + +import com.baeldung.boot.Application; +import com.baeldung.boot.config.PersistenceConfiguration; +import com.baeldung.boot.daos.ExtendedStudentRepository; +import com.baeldung.boot.domain.Student; -import com.baeldung.config.PersistenceConfiguration; -import com.baeldung.domain.Student; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -15,7 +18,7 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) -@ContextConfiguration(classes = {PersistenceConfiguration.class}) +@ContextConfiguration(classes = {Application.class}) @DirtiesContext public class ExtendedStudentRepositoryIntegrationTest { @Resource diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/InventoryRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/InventoryRepositoryIntegrationTest.java similarity index 85% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/InventoryRepositoryIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/InventoryRepositoryIntegrationTest.java index 9d6334445c..877e59d5a2 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/InventoryRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/InventoryRepositoryIntegrationTest.java @@ -1,11 +1,15 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; + +import com.baeldung.boot.Application; +import com.baeldung.boot.config.PersistenceConfiguration; +import com.baeldung.boot.daos.InventoryRepository; +import com.baeldung.boot.domain.MerchandiseEntity; -import com.baeldung.config.PersistenceConfiguration; -import com.baeldung.domain.MerchandiseEntity; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.math.BigDecimal; @@ -15,7 +19,7 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; @RunWith(SpringRunner.class) -@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class}) +@SpringBootTest(classes=Application.class) public class InventoryRepositoryIntegrationTest { private static final String ORIGINAL_TITLE = "Pair of Pants"; diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/JpaRepositoriesIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/JpaRepositoriesIntegrationTest.java similarity index 80% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/JpaRepositoriesIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/JpaRepositoriesIntegrationTest.java index 01405c0b8a..30925d9b68 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/JpaRepositoriesIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/JpaRepositoriesIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertFalse; @@ -13,18 +13,24 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; -import com.baeldung.config.PersistenceConfiguration; -import com.baeldung.config.PersistenceProductConfiguration; -import com.baeldung.config.PersistenceUserConfiguration; -import com.baeldung.domain.Item; -import com.baeldung.domain.ItemType; -import com.baeldung.domain.Location; -import com.baeldung.domain.Store; +import com.baeldung.boot.Application; +import com.baeldung.boot.config.PersistenceConfiguration; +import com.baeldung.boot.daos.ItemTypeRepository; +import com.baeldung.boot.daos.LocationRepository; +import com.baeldung.boot.daos.ReadOnlyLocationRepository; +import com.baeldung.boot.daos.StoreRepository; +import com.baeldung.boot.domain.Item; +import com.baeldung.boot.domain.ItemType; +import com.baeldung.boot.domain.Location; +import com.baeldung.boot.domain.Store; +import com.baeldung.multipledb.PersistenceProductConfiguration; +import com.baeldung.multipledb.PersistenceUserConfiguration; @RunWith(SpringRunner.class) -@DataJpaTest(excludeAutoConfiguration = { PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class }) +@DataJpaTest(properties="spring.datasource.data=classpath:import_entities.sql") public class JpaRepositoriesIntegrationTest { @Autowired private LocationRepository locationRepository; diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/PersonInsertRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/PersonInsertRepositoryIntegrationTest.java similarity index 94% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/PersonInsertRepositoryIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/PersonInsertRepositoryIntegrationTest.java index b248cf8bf1..9d45c17035 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/PersonInsertRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/PersonInsertRepositoryIntegrationTest.java @@ -1,7 +1,8 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; + +import com.baeldung.boot.daos.impl.PersonInsertRepository; +import com.baeldung.boot.domain.Person; -import com.baeldung.dao.repositories.impl.PersonInsertRepository; -import com.baeldung.domain.Person; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/UserRepositoryCommon.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/UserRepositoryCommon.java similarity index 99% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/UserRepositoryCommon.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/UserRepositoryCommon.java index abd758cec2..4aeeaf5209 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/UserRepositoryCommon.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/UserRepositoryCommon.java @@ -1,7 +1,5 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; -import com.baeldung.dao.repositories.user.UserRepository; -import com.baeldung.domain.user.User; import org.junit.After; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -12,6 +10,9 @@ import org.springframework.data.jpa.domain.JpaSort; import org.springframework.data.mapping.PropertyReferenceException; import org.springframework.transaction.annotation.Transactional; +import com.baeldung.boot.daos.user.UserRepository; +import com.baeldung.boot.domain.User; + import javax.persistence.EntityManager; import javax.persistence.Query; import java.time.LocalDate; diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/UserRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/UserRepositoryIntegrationTest.java similarity index 86% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/UserRepositoryIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/UserRepositoryIntegrationTest.java index 09f937c8f2..1b1d264574 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/UserRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/UserRepositoryIntegrationTest.java @@ -1,7 +1,7 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; -import com.baeldung.config.PersistenceConfiguration; -import com.baeldung.domain.user.User; +import com.baeldung.boot.Application; +import com.baeldung.boot.domain.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; @@ -17,7 +17,7 @@ import static org.assertj.core.api.Assertions.assertThat; * Created by adam. */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = PersistenceConfiguration.class) +@SpringBootTest(classes = Application.class) @DirtiesContext public class UserRepositoryIntegrationTest extends UserRepositoryCommon { diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/UserRepositoryTCAutoIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/UserRepositoryTCAutoIntegrationTest.java similarity index 91% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/UserRepositoryTCAutoIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/UserRepositoryTCAutoIntegrationTest.java index 6a851823e3..2f3e9c9032 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/UserRepositoryTCAutoIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/UserRepositoryTCAutoIntegrationTest.java @@ -1,6 +1,7 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; -import com.baeldung.domain.user.User; +import com.baeldung.boot.Application; +import com.baeldung.boot.domain.User; import com.baeldung.util.BaeldungPostgresqlContainer; import org.junit.ClassRule; import org.junit.Test; @@ -19,7 +20,7 @@ import static org.assertj.core.api.Assertions.assertThat; * Created by adam. */ @RunWith(SpringRunner.class) -@SpringBootTest +@SpringBootTest(classes = Application.class) @ActiveProfiles({"tc", "tc-auto"}) public class UserRepositoryTCAutoIntegrationTest extends UserRepositoryCommon { diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/UserRepositoryTCIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/UserRepositoryTCIntegrationTest.java similarity index 94% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/UserRepositoryTCIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/UserRepositoryTCIntegrationTest.java index c300a07ab9..afdf60cc81 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/UserRepositoryTCIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/daos/UserRepositoryTCIntegrationTest.java @@ -1,6 +1,7 @@ -package com.baeldung.dao.repositories; +package com.baeldung.boot.daos; -import com.baeldung.domain.user.User; +import com.baeldung.boot.Application; +import com.baeldung.boot.domain.User; import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; @@ -19,7 +20,7 @@ import java.time.LocalDate; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) -@SpringBootTest +@SpringBootTest(classes = Application.class) @ActiveProfiles("tc") @ContextConfiguration(initializers = {UserRepositoryTCIntegrationTest.Initializer.class}) public class UserRepositoryTCIntegrationTest extends UserRepositoryCommon { diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate2EventsIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/ddd/event/Aggregate2EventsIntegrationTest.java similarity index 90% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate2EventsIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/ddd/event/Aggregate2EventsIntegrationTest.java index 3f650d4d63..e76b932cb9 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate2EventsIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/ddd/event/Aggregate2EventsIntegrationTest.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.ddd.event; +package com.baeldung.boot.ddd.event; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.times; @@ -15,6 +15,10 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import com.baeldung.boot.ddd.event.Aggregate2; +import com.baeldung.boot.ddd.event.Aggregate2Repository; +import com.baeldung.boot.ddd.event.DomainEvent; + @SpringJUnitConfig @SpringBootTest class Aggregate2EventsIntegrationTest { diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate3EventsIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/ddd/event/Aggregate3EventsIntegrationTest.java similarity index 90% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate3EventsIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/ddd/event/Aggregate3EventsIntegrationTest.java index 893dcac3f8..4193e932ee 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate3EventsIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/ddd/event/Aggregate3EventsIntegrationTest.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.ddd.event; +package com.baeldung.boot.ddd.event; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.times; @@ -14,6 +14,10 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import com.baeldung.boot.ddd.event.Aggregate3; +import com.baeldung.boot.ddd.event.Aggregate3Repository; +import com.baeldung.boot.ddd.event.DomainEvent; + @SpringJUnitConfig @SpringBootTest class Aggregate3EventsIntegrationTest { diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/ddd/event/AggregateEventsIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/ddd/event/AggregateEventsIntegrationTest.java similarity index 91% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/ddd/event/AggregateEventsIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/ddd/event/AggregateEventsIntegrationTest.java index f0e1147245..ac607063b2 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/ddd/event/AggregateEventsIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/ddd/event/AggregateEventsIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.ddd.event; +package com.baeldung.boot.ddd.event; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.times; @@ -16,6 +16,11 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Bean; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import com.baeldung.boot.ddd.event.Aggregate; +import com.baeldung.boot.ddd.event.AggregateRepository; +import com.baeldung.boot.ddd.event.DomainEvent; +import com.baeldung.boot.ddd.event.DomainService; + @SpringJUnitConfig @SpringBootTest class AggregateEventsIntegrationTest { diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/ddd/event/TestEventHandler.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/ddd/event/TestEventHandler.java similarity index 68% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/ddd/event/TestEventHandler.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/ddd/event/TestEventHandler.java index 721402c17a..0f499834eb 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/ddd/event/TestEventHandler.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/ddd/event/TestEventHandler.java @@ -1,10 +1,12 @@ /** * */ -package com.baeldung.ddd.event; +package com.baeldung.boot.ddd.event; import org.springframework.transaction.event.TransactionalEventListener; +import com.baeldung.boot.ddd.event.DomainEvent; + interface TestEventHandler { @TransactionalEventListener void handleEvent(DomainEvent event); diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/passenger/PassengerRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/passenger/PassengerRepositoryIntegrationTest.java similarity index 98% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/passenger/PassengerRepositoryIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/passenger/PassengerRepositoryIntegrationTest.java index 2b303604b6..f082350019 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/passenger/PassengerRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/passenger/PassengerRepositoryIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.passenger; +package com.baeldung.boot.passenger; import org.junit.Before; import org.junit.Test; @@ -12,6 +12,9 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner; +import com.baeldung.boot.passenger.Passenger; +import com.baeldung.boot.passenger.PassengerRepository; + import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import java.util.List; @@ -23,6 +26,7 @@ import static org.hamcrest.core.IsNot.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; + @DataJpaTest @RunWith(SpringRunner.class) public class PassengerRepositoryIntegrationTest { diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/services/AbstractServicePersistenceIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/services/AbstractServicePersistenceIntegrationTest.java similarity index 98% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/services/AbstractServicePersistenceIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/services/AbstractServicePersistenceIntegrationTest.java index acac66f2f7..bf0c85fca6 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/services/AbstractServicePersistenceIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/services/AbstractServicePersistenceIntegrationTest.java @@ -1,6 +1,7 @@ -package com.baeldung.services; +package com.baeldung.boot.services; -import com.baeldung.domain.Foo; +import com.baeldung.boot.domain.Foo; +import com.baeldung.boot.services.IOperations; import com.baeldung.util.IDUtil; import org.hamcrest.Matchers; import org.junit.Ignore; diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/services/FooServicePersistenceIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/services/FooServicePersistenceIntegrationTest.java similarity index 85% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/services/FooServicePersistenceIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/services/FooServicePersistenceIntegrationTest.java index fd17d033e1..f0e4aa7317 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/services/FooServicePersistenceIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/services/FooServicePersistenceIntegrationTest.java @@ -1,11 +1,16 @@ -package com.baeldung.services; +package com.baeldung.boot.services; + +import com.baeldung.boot.Application; +import com.baeldung.boot.config.PersistenceConfiguration; +import com.baeldung.boot.domain.Foo; +import com.baeldung.boot.services.IFooService; +import com.baeldung.boot.services.IOperations; -import com.baeldung.config.PersistenceConfiguration; -import com.baeldung.domain.Foo; import org.junit.Ignore; 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.dao.DataIntegrityViolationException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.test.context.ContextConfiguration; @@ -16,7 +21,7 @@ import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.junit.Assert.assertNotNull; @RunWith(SpringRunner.class) -@ContextConfiguration(classes = {PersistenceConfiguration.class}, loader = AnnotationConfigContextLoader.class) +@SpringBootTest(classes=Application.class) public class FooServicePersistenceIntegrationTest extends AbstractServicePersistenceIntegrationTest { @Autowired diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/services/SpringDataJPABarAuditIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/services/SpringDataJPABarAuditIntegrationTest.java similarity index 87% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/services/SpringDataJPABarAuditIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/services/SpringDataJPABarAuditIntegrationTest.java index f3b857c73d..810cf70769 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/services/SpringDataJPABarAuditIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/boot/services/SpringDataJPABarAuditIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.services; +package com.baeldung.boot.services; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -16,16 +16,19 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; -import com.baeldung.config.PersistenceConfiguration; -import com.baeldung.domain.Bar; +import com.baeldung.boot.Application; +import com.baeldung.boot.config.PersistenceConfiguration; +import com.baeldung.boot.domain.Bar; +import com.baeldung.boot.services.IBarService; @RunWith(SpringRunner.class) -@ContextConfiguration(classes = { PersistenceConfiguration.class }, loader = AnnotationConfigContextLoader.class) +@SpringBootTest(classes=Application.class) public class SpringDataJPABarAuditIntegrationTest { private static Logger logger = LoggerFactory.getLogger(SpringDataJPABarAuditIntegrationTest.class); diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/services/JpaMultipleDBIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/multipledb/JpaMultipleDBIntegrationTest.java similarity index 74% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/services/JpaMultipleDBIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/multipledb/JpaMultipleDBIntegrationTest.java index 71a3fb0b44..bcc4103d08 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/services/JpaMultipleDBIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/multipledb/JpaMultipleDBIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.services; +package com.baeldung.multipledb; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -10,26 +10,23 @@ 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.dao.DataIntegrityViolationException; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; -import com.baeldung.config.PersistenceProductConfiguration; -import com.baeldung.config.PersistenceUserConfiguration; -import com.baeldung.dao.repositories.product.ProductRepository; -import com.baeldung.dao.repositories.user.PossessionRepository; -import com.baeldung.dao.repositories.user.UserRepository; -import com.baeldung.domain.product.Product; -import com.baeldung.domain.user.Possession; -import com.baeldung.domain.user.User; +import com.baeldung.multipledb.dao.product.ProductRepository; +import com.baeldung.multipledb.dao.user.PossessionRepository; +import com.baeldung.multipledb.dao.user.UserRepository; +import com.baeldung.multipledb.model.product.ProductMultipleDB; +import com.baeldung.multipledb.model.user.PossessionMultipleDB; +import com.baeldung.multipledb.model.user.UserMultipleDB; @RunWith(SpringRunner.class) -@ContextConfiguration(classes = { PersistenceUserConfiguration.class, PersistenceProductConfiguration.class }) +@SpringBootTest(classes=MultipleDbApplication.class) @EnableTransactionManagement -@DirtiesContext public class JpaMultipleDBIntegrationTest { @Autowired @@ -46,15 +43,15 @@ public class JpaMultipleDBIntegrationTest { @Test @Transactional("userTransactionManager") public void whenCreatingUser_thenCreated() { - User user = new User(); + UserMultipleDB user = new UserMultipleDB(); user.setName("John"); user.setEmail("john@test.com"); user.setAge(20); - Possession p = new Possession("sample"); + PossessionMultipleDB p = new PossessionMultipleDB("sample"); p = possessionRepository.save(p); user.setPossessionList(Collections.singletonList(p)); user = userRepository.save(user); - final Optional result = userRepository.findById(user.getId()); + final Optional result = userRepository.findById(user.getId()); assertTrue(result.isPresent()); System.out.println(result.get().getPossessionList()); assertEquals(1, result.get().getPossessionList().size()); @@ -63,14 +60,14 @@ public class JpaMultipleDBIntegrationTest { @Test @Transactional("userTransactionManager") public void whenCreatingUsersWithSameEmail_thenRollback() { - User user1 = new User(); + UserMultipleDB user1 = new UserMultipleDB(); user1.setName("John"); user1.setEmail("john@test.com"); user1.setAge(20); user1 = userRepository.save(user1); assertTrue(userRepository.findById(user1.getId()).isPresent()); - User user2 = new User(); + UserMultipleDB user2 = new UserMultipleDB(); user2.setName("Tom"); user2.setEmail("john@test.com"); user2.setAge(10); @@ -88,7 +85,7 @@ public class JpaMultipleDBIntegrationTest { @Test @Transactional("productTransactionManager") public void whenCreatingProduct_thenCreated() { - Product product = new Product(); + ProductMultipleDB product = new ProductMultipleDB(); product.setName("Book"); product.setId(2); product.setPrice(20); diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/product/ProductRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/multipledb/ProductRepositoryIntegrationTest.java similarity index 69% rename from persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/product/ProductRepositoryIntegrationTest.java rename to persistence-modules/spring-data-jpa/src/test/java/com/baeldung/multipledb/ProductRepositoryIntegrationTest.java index 4caa0f0ca4..2c965f72f3 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/product/ProductRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/multipledb/ProductRepositoryIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.dao.repositories.product; +package com.baeldung.multipledb; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; @@ -13,6 +13,7 @@ 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.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -22,11 +23,12 @@ import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; -import com.baeldung.config.PersistenceProductConfiguration; -import com.baeldung.domain.product.Product; +import com.baeldung.multipledb.PersistenceProductConfiguration; +import com.baeldung.multipledb.dao.product.ProductRepository; +import com.baeldung.multipledb.model.product.ProductMultipleDB; @RunWith(SpringRunner.class) -@ContextConfiguration(classes = { PersistenceProductConfiguration.class }) +@SpringBootTest(classes=MultipleDbApplication.class) @EnableTransactionManagement public class ProductRepositoryIntegrationTest { @@ -36,22 +38,22 @@ public class ProductRepositoryIntegrationTest { @Before @Transactional("productTransactionManager") public void setUp() { - productRepository.save(Product.from(1001, "Book", 21)); - productRepository.save(Product.from(1002, "Coffee", 10)); - productRepository.save(Product.from(1003, "Jeans", 30)); - productRepository.save(Product.from(1004, "Shirt", 32)); - productRepository.save(Product.from(1005, "Bacon", 10)); + productRepository.save(ProductMultipleDB.from(1001, "Book", 21)); + productRepository.save(ProductMultipleDB.from(1002, "Coffee", 10)); + productRepository.save(ProductMultipleDB.from(1003, "Jeans", 30)); + productRepository.save(ProductMultipleDB.from(1004, "Shirt", 32)); + productRepository.save(ProductMultipleDB.from(1005, "Bacon", 10)); } @Test public void whenRequestingFirstPageOfSizeTwo_ThenReturnFirstPage() { Pageable pageRequest = PageRequest.of(0, 2); - Page result = productRepository.findAll(pageRequest); + Page result = productRepository.findAll(pageRequest); assertThat(result.getContent(), hasSize(2)); assertTrue(result.stream() - .map(Product::getId) + .map(ProductMultipleDB::getId) .allMatch(id -> Arrays.asList(1001, 1002) .contains(id))); } @@ -60,11 +62,11 @@ public class ProductRepositoryIntegrationTest { public void whenRequestingSecondPageOfSizeTwo_ThenReturnSecondPage() { Pageable pageRequest = PageRequest.of(1, 2); - Page result = productRepository.findAll(pageRequest); + Page result = productRepository.findAll(pageRequest); assertThat(result.getContent(), hasSize(2)); assertTrue(result.stream() - .map(Product::getId) + .map(ProductMultipleDB::getId) .allMatch(id -> Arrays.asList(1003, 1004) .contains(id))); } @@ -73,11 +75,11 @@ public class ProductRepositoryIntegrationTest { public void whenRequestingLastPage_ThenReturnLastPageWithRemData() { Pageable pageRequest = PageRequest.of(2, 2); - Page result = productRepository.findAll(pageRequest); + Page result = productRepository.findAll(pageRequest); assertThat(result.getContent(), hasSize(1)); assertTrue(result.stream() - .map(Product::getId) + .map(ProductMultipleDB::getId) .allMatch(id -> Arrays.asList(1005) .contains(id))); } @@ -86,12 +88,12 @@ public class ProductRepositoryIntegrationTest { public void whenSortingByNameAscAndPaging_ThenReturnSortedPagedResult() { Pageable pageRequest = PageRequest.of(0, 3, Sort.by("name")); - Page result = productRepository.findAll(pageRequest); + Page result = productRepository.findAll(pageRequest); assertThat(result.getContent(), hasSize(3)); assertThat(result.getContent() .stream() - .map(Product::getId) + .map(ProductMultipleDB::getId) .collect(Collectors.toList()), equalTo(Arrays.asList(1005, 1001, 1002))); } @@ -101,12 +103,12 @@ public class ProductRepositoryIntegrationTest { Pageable pageRequest = PageRequest.of(0, 3, Sort.by("price") .descending()); - Page result = productRepository.findAll(pageRequest); + Page result = productRepository.findAll(pageRequest); assertThat(result.getContent(), hasSize(3)); assertThat(result.getContent() .stream() - .map(Product::getId) + .map(ProductMultipleDB::getId) .collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001))); } @@ -117,12 +119,12 @@ public class ProductRepositoryIntegrationTest { .descending() .and(Sort.by("name"))); - Page result = productRepository.findAll(pageRequest); + Page result = productRepository.findAll(pageRequest); assertThat(result.getContent(), hasSize(5)); assertThat(result.getContent() .stream() - .map(Product::getId) + .map(ProductMultipleDB::getId) .collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001, 1005, 1002))); } @@ -131,11 +133,11 @@ public class ProductRepositoryIntegrationTest { public void whenRequestingFirstPageOfSizeTwoUsingCustomMethod_ThenReturnFirstPage() { Pageable pageRequest = PageRequest.of(0, 2); - List result = productRepository.findAllByPrice(10, pageRequest); + List result = productRepository.findAllByPrice(10, pageRequest); assertThat(result, hasSize(2)); assertTrue(result.stream() - .map(Product::getId) + .map(ProductMultipleDB::getId) .allMatch(id -> Arrays.asList(1002, 1005) .contains(id))); } diff --git a/persistence-modules/spring-data-jpa/src/test/java/org/baeldung/SpringContextIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/org/baeldung/SpringContextIntegrationTest.java index 7f906bdbcd..e885d0fe51 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/org/baeldung/SpringContextIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/org/baeldung/SpringContextIntegrationTest.java @@ -5,7 +5,7 @@ import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; -import com.baeldung.Application; +import com.baeldung.boot.Application; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) diff --git a/persistence-modules/spring-data-jpa/src/test/java/org/baeldung/SpringJpaContextIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/org/baeldung/SpringJpaContextIntegrationTest.java index 66b5b20b97..4a36407884 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/org/baeldung/SpringJpaContextIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/org/baeldung/SpringJpaContextIntegrationTest.java @@ -6,10 +6,10 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; -import com.baeldung.Application; -import com.baeldung.config.PersistenceConfiguration; -import com.baeldung.config.PersistenceProductConfiguration; -import com.baeldung.config.PersistenceUserConfiguration; +import com.baeldung.boot.Application; +import com.baeldung.boot.config.PersistenceConfiguration; +import com.baeldung.multipledb.PersistenceProductConfiguration; +import com.baeldung.multipledb.PersistenceUserConfiguration; @RunWith(SpringRunner.class) @DataJpaTest(excludeAutoConfiguration = { diff --git a/pom.xml b/pom.xml index de6f47d71d..b896514b3a 100644 --- a/pom.xml +++ b/pom.xml @@ -339,6 +339,7 @@ algorithms-genetic algorithms-miscellaneous-1 algorithms-miscellaneous-2 + algorithms-miscellaneous-3 algorithms-sorting animal-sniffer-mvn-plugin annotations @@ -462,6 +463,7 @@ java-streams java-streams-2 java-strings + java-strings-2 java-vavr-stream java-websocket javafx @@ -490,12 +492,14 @@ kotlin-libraries + kotlin-libraries-2 libraries libraries-2 libraries-data libraries-apache-commons + libraries-primitive libraries-security libraries-server linkrest @@ -1017,6 +1021,7 @@ algorithms-genetic algorithms-miscellaneous-1 algorithms-miscellaneous-2 + algorithms-miscellaneous-3 algorithms-sorting animal-sniffer-mvn-plugin annotations @@ -1133,6 +1138,7 @@ java-streams java-streams-2 java-strings + java-strings-2 java-vavr-stream java-websocket javafx diff --git a/spring-5-webflux/README.md b/spring-5-webflux/README.md index 84c3d5a4ca..4345c8746b 100644 --- a/spring-5-webflux/README.md +++ b/spring-5-webflux/README.md @@ -2,3 +2,4 @@ - [Spring Boot Reactor Netty Configuration](https://www.baeldung.com/spring-boot-reactor-netty) - [How to Return 404 with Spring WebFlux](https://www.baeldung.com/spring-webflux-404) +- [WebClient request with parameters](https://www.baeldung.com/webclient-request-with-parameters) diff --git a/spring-5-webflux/src/main/java/com/baeldung/spring/webclientrequests/SpringWebClientRequestsApp.java b/spring-5-webflux/src/main/java/com/baeldung/spring/webclientrequests/SpringWebClientRequestsApp.java new file mode 100644 index 0000000000..314fe2fdf9 --- /dev/null +++ b/spring-5-webflux/src/main/java/com/baeldung/spring/webclientrequests/SpringWebClientRequestsApp.java @@ -0,0 +1,12 @@ +package com.baeldung.spring.webclientrequests; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringWebClientRequestsApp { + + public static void main(String[] args) { + SpringApplication.run(SpringWebClientRequestsApp.class, args); + } +} diff --git a/spring-5-webflux/src/test/java/com/baeldung/spring/webclientrequests/WebClientRequestsUnitTest.java b/spring-5-webflux/src/test/java/com/baeldung/spring/webclientrequests/WebClientRequestsUnitTest.java new file mode 100644 index 0000000000..8919daa9fb --- /dev/null +++ b/spring-5-webflux/src/test/java/com/baeldung/spring/webclientrequests/WebClientRequestsUnitTest.java @@ -0,0 +1,164 @@ +package com.baeldung.spring.webclientrequests; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.reactive.function.client.ClientRequest; +import org.springframework.web.reactive.function.client.ClientResponse; +import org.springframework.web.reactive.function.client.ExchangeFunction; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.DefaultUriBuilderFactory; +import reactor.core.publisher.Mono; + +import static org.mockito.Mockito.*; + +@RunWith(SpringRunner.class) +@WebFluxTest +public class WebClientRequestsUnitTest { + + private static final String BASE_URL = "https://example.com"; + + private WebClient webClient; + + @Captor + private ArgumentCaptor argumentCaptor; + + private ExchangeFunction exchangeFunction; + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + this.exchangeFunction = mock(ExchangeFunction.class); + ClientResponse mockResponse = mock(ClientResponse.class); + when(this.exchangeFunction.exchange(this.argumentCaptor.capture())).thenReturn(Mono.just(mockResponse)); + this.webClient = WebClient + .builder() + .baseUrl(BASE_URL) + .exchangeFunction(exchangeFunction) + .build(); + } + + + @Test + public void whenCallSimpleURI_thenURIMatched() { + this.webClient.get() + .uri("/products") + .retrieve(); + verifyCalledUrl("/products"); + } + + @Test + public void whenCallSinglePathSegmentUri_thenURIMatched() { + this.webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/{id}") + .build(2)) + .retrieve(); + verifyCalledUrl("/products/2"); + } + + @Test + public void whenCallMultiplePathSegmentsUri_thenURIMatched() { + this.webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/{id}/attributes/{attributeId}") + .build(2, 13)) + .retrieve(); + verifyCalledUrl("/products/2/attributes/13"); + } + + @Test + public void whenCallSingleQueryParams_thenURIMatched() { + this.webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/") + .queryParam("name", "AndroidPhone") + .queryParam("color", "black") + .queryParam("deliveryDate", "13/04/2019") + .build()) + .retrieve(); + verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019"); + } + + @Test + public void whenCallSingleQueryParamsPlaceholders_thenURIMatched() { + this.webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/") + .queryParam("name", "{title}") + .queryParam("color", "{authorId}") + .queryParam("deliveryDate", "{date}") + .build("AndroidPhone", "black", "13/04/2019")) + .retrieve(); + verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13%2F04%2F2019"); + } + + @Test + public void whenCallArrayQueryParamsBrackets_thenURIMatched() { + this.webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/") + .queryParam("tag[]", "Snapdragon", "NFC") + .build()) + .retrieve(); + verifyCalledUrl("/products/?tag%5B%5D=Snapdragon&tag%5B%5D=NFC"); + } + + + @Test + public void whenCallArrayQueryParams_thenURIMatched() { + this.webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/") + .queryParam("category", "Phones", "Tablets") + .build()) + .retrieve(); + verifyCalledUrl("/products/?category=Phones&category=Tablets"); + } + + @Test + public void whenCallArrayQueryParamsComma_thenURIMatched() { + this.webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/") + .queryParam("category", String.join(",", "Phones", "Tablets")) + .build()) + .retrieve(); + verifyCalledUrl("/products/?category=Phones,Tablets"); + } + + @Test + public void whenUriComponentEncoding_thenQueryParamsNotEscaped() { + DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(BASE_URL); + factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.URI_COMPONENT); + this.webClient = WebClient + .builder() + .uriBuilderFactory(factory) + .baseUrl(BASE_URL) + .exchangeFunction(exchangeFunction) + .build(); + this.webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/") + .queryParam("name", "AndroidPhone") + .queryParam("color", "black") + .queryParam("deliveryDate", "13/04/2019") + .build()) + .retrieve(); + verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019"); + } + + private void verifyCalledUrl(String relativeUrl) { + ClientRequest request = this.argumentCaptor.getValue(); + Assert.assertEquals(String.format("%s%s", BASE_URL, relativeUrl), request.url().toString()); + Mockito.verify(this.exchangeFunction).exchange(request); + verifyNoMoreInteractions(this.exchangeFunction); + } +} diff --git a/spring-security-mvc-digest-auth/src/test/java/org/baeldung/client/RawClientLiveTest.java b/spring-security-mvc-digest-auth/src/test/java/org/baeldung/client/RawClientLiveTest.java index 90c2a29968..e8dcf82ebc 100644 --- a/spring-security-mvc-digest-auth/src/test/java/org/baeldung/client/RawClientLiveTest.java +++ b/spring-security-mvc-digest-auth/src/test/java/org/baeldung/client/RawClientLiveTest.java @@ -1,7 +1,8 @@ package org.baeldung.client; +import java.io.IOException; + import org.apache.http.HttpResponse; -import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; @@ -12,10 +13,6 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; -import java.io.IOException; -import java.util.Timer; -import java.util.TimerTask; - @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {ClientConfig.class}, loader = AnnotationConfigContextLoader.class) public class RawClientLiveTest { @@ -25,22 +22,7 @@ public class RawClientLiveTest { @Test public void whenSecuredRestApiIsConsumed_then200OK() throws IOException { CloseableHttpClient httpClient = HttpClientBuilder.create().build(); - - int timeout = 20; // seconds - RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout) - .setConnectTimeout(timeout).setSocketTimeout(timeout).build(); HttpGet getMethod = new HttpGet("http://localhost:8082/spring-security-rest-basic-auth/api/bars/1"); - getMethod.setConfig(requestConfig); - - int hardTimeout = 5; // seconds - TimerTask task = new TimerTask() { - @Override - public void run() { - getMethod.abort(); - } - }; - new Timer(true).schedule(task, hardTimeout * 1000); - HttpResponse response = httpClient.execute(getMethod); System.out.println("HTTP Status of response: " + response.getStatusLine().getStatusCode()); } diff --git a/spring-security-rest-basic-auth/README.md b/spring-security-rest-basic-auth/README.md index 0a48a747c2..b905824161 100644 --- a/spring-security-rest-basic-auth/README.md +++ b/spring-security-rest-basic-auth/README.md @@ -7,6 +7,5 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com ### Relevant Articles: - [Basic Authentication with the RestTemplate](http://www.baeldung.com/how-to-use-resttemplate-with-basic-authentication-in-spring) -- [HttpClient Timeout](http://www.baeldung.com/httpclient-timeout) - [A Custom Filter in the Spring Security Filter Chain](http://www.baeldung.com/spring-security-custom-filter) - [Spring Security Basic Authentication](http://www.baeldung.com/spring-security-basic-authentication) diff --git a/testing-modules/junit-5-configuration/README.md b/testing-modules/junit-5-configuration/README.md new file mode 100644 index 0000000000..c274391766 --- /dev/null +++ b/testing-modules/junit-5-configuration/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- diff --git a/testing-modules/junit-5-configuration/pom.xml b/testing-modules/junit-5-configuration/pom.xml new file mode 100644 index 0000000000..aa488ebb8b --- /dev/null +++ b/testing-modules/junit-5-configuration/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + junit-5-configuration + 1.0-SNAPSHOT + junit-5-configuration + Intro to JUnit 5 configuration + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + org.junit.jupiter + junit-jupiter-params + ${junit.jupiter.version} + + + + + + + src/test/resources + true + + + + + + 5.3.1 + 1.2.0 + 5.2.0 + + + diff --git a/testing-modules/junit-5-configuration/src/test/java/com/baeldung/resourcedirectory/ReadResourceDirectoryUnitTest.java b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/resourcedirectory/ReadResourceDirectoryUnitTest.java new file mode 100644 index 0000000000..20fa372abd --- /dev/null +++ b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/resourcedirectory/ReadResourceDirectoryUnitTest.java @@ -0,0 +1,45 @@ +package com.baeldung.resourcedirectory; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class ReadResourceDirectoryUnitTest { + + @Test + public void givenResourcePath_whenReadAbsolutePathWithFile_thenAbsolutePathEndsWithDirectory() { + String path = "src/test/resources"; + + File file = new File(path); + String absolutePath = file.getAbsolutePath(); + + System.out.println(absolutePath); + Assert.assertTrue(absolutePath.endsWith("src/test/resources")); + } + + @Test + public void givenResourcePath_whenReadAbsolutePathWithPaths_thenAbsolutePathEndsWithDirectory() { + Path resourceDirectory = Paths.get("src", "test", "resources"); + + String absolutePath = resourceDirectory.toFile().getAbsolutePath(); + + System.out.println(absolutePath); + Assert.assertTrue(absolutePath.endsWith("src/test/resources")); + } + + @Test + public void givenResourceFile_whenReadResourceWithClassLoader_thenPathEndWithFilename() { + String resourceName = "example_resource.txt"; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource(resourceName).getFile()); + String absolutePath = file.getAbsolutePath(); + + System.out.println(absolutePath); + Assert.assertTrue(absolutePath.endsWith("/example_resource.txt")); + } + +} diff --git a/testing-modules/junit-5-configuration/src/test/resources/example_resource.txt b/testing-modules/junit-5-configuration/src/test/resources/example_resource.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testing-modules/junit-5/pom.xml b/testing-modules/junit-5/pom.xml index a5a1ddaf0b..1d1b0bc574 100644 --- a/testing-modules/junit-5/pom.xml +++ b/testing-modules/junit-5/pom.xml @@ -103,13 +103,6 @@ maven-surefire-plugin ${maven-surefire-plugin.version} - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - - org.codehaus.mojo @@ -130,10 +123,10 @@ - 5.3.1 + 5.4.2 2.23.0 - 1.2.0 - 5.2.0 + 1.4.2 + 5.4.2 2.8.2 1.4.196 2.0.0-RC.1 diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/parameterized/StringsUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/parameterized/StringsUnitTest.java index 7d02a5a74b..6aea7668f1 100644 --- a/testing-modules/junit-5/src/test/java/com/baeldung/parameterized/StringsUnitTest.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/parameterized/StringsUnitTest.java @@ -86,7 +86,30 @@ class StringsUnitTest { assertEquals(expected, actualValue); } + @ParameterizedTest + @NullSource + void isBlank_ShouldReturnTrueForNullInputs(String input) { + assertTrue(Strings.isBlank(input)); + } + @ParameterizedTest + @EmptySource + void isBlank_ShouldReturnTrueForEmptyStrings(String input) { + assertTrue(Strings.isBlank(input)); + } + + @ParameterizedTest + @NullAndEmptySource + void isBlank_ShouldReturnTrueForNullAndEmptyStrings(String input) { + assertTrue(Strings.isBlank(input)); + } + + @ParameterizedTest + @NullAndEmptySource + @ValueSource(strings = {" ", "\t", "\n"}) + void isBlank_ShouldReturnTrueForAllTypesOfBlankStrings(String input) { + assertTrue(Strings.isBlank(input)); + } private static Stream provideStringsForIsBlank() { return Stream.of( diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index 39047fb756..40ed63bc12 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -32,5 +32,6 @@ test-containers testing testng + junit-5-configuration