diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/knapsack/Knapsack.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/knapsack/Knapsack.java new file mode 100644 index 0000000000..61c3f05a95 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/knapsack/Knapsack.java @@ -0,0 +1,36 @@ +package com.baeldung.algorithms.knapsack; + +public class Knapsack { + + public int knapsackRec(int[] w, int[] v, int n, int W) { + if (n <= 0) { + return 0; + } else if (w[n - 1] > W) { + return knapsackRec(w, v, n - 1, W); + } else { + return Math.max(knapsackRec(w, v, n - 1, W), v[n - 1] + knapsackRec(w, v, n - 1, W - w[n - 1])); + } + } + + public int knapsackDP(int[] w, int[] v, int n, int W) { + if (n <= 0 || W <= 0) { + return 0; + } + + int[][] m = new int[n + 1][W + 1]; + for (int j = 0; j <= W; j++) { + m[0][j] = 0; + } + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= W; j++) { + if (w[i - 1] > j) { + m[i][j] = m[i - 1][j]; + } else { + m[i][j] = Math.max(m[i - 1][j], m[i - 1][j - w[i - 1]] + v[i - 1]); + } + } + } + return m[n][W]; + } +} diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/knapsack/KnapsackUnitTest.java b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/knapsack/KnapsackUnitTest.java new file mode 100644 index 0000000000..b168e6b1eb --- /dev/null +++ b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/knapsack/KnapsackUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.algorithms.knapsack; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class KnapsackUnitTest { + + @Test + public void givenWeightsandValues_whenCalculateMax_thenOutputCorrectResult() { + final int[] w = new int[] { 23, 26, 20, 18, 32, 27, 29, 26, 30, 27 }; + final int[] v = new int[] { 505, 352, 458, 220, 354, 414, 498, 545, 473, 543 }; + final int n = 10; + final int W = 67; + final Knapsack knapsack = new Knapsack(); + + assertEquals(1270, knapsack.knapsackRec(w, v, n, W)); + assertEquals(1270, knapsack.knapsackDP(w, v, n, W)); + } + + @Test + public void givenZeroItems_whenCalculateMax_thenOutputZero() { + final int[] w = new int[] {}; + final int[] v = new int[] {}; + final int n = 0; + final int W = 67; + final Knapsack knapsack = new Knapsack(); + + assertEquals(0, knapsack.knapsackRec(w, v, n, W)); + assertEquals(0, knapsack.knapsackDP(w, v, n, W)); + } + + @Test + public void givenZeroWeightLimit_whenCalculateMax_thenOutputZero() { + final int[] w = new int[] { 23, 26, 20, 18, 32, 27, 29, 26, 30, 27 }; + final int[] v = new int[] { 505, 352, 458, 220, 354, 414, 498, 545, 473, 543 }; + final int n = 10; + final int W = 0; + final Knapsack knapsack = new Knapsack(); + + assertEquals(0, knapsack.knapsackRec(w, v, n, W)); + assertEquals(0, knapsack.knapsackDP(w, v, n, W)); + } +} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/Tuple.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/Tuple.java index 7a9f62341e..82a84bb2d6 100644 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/Tuple.java +++ b/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/groupingby/Tuple.java @@ -1,12 +1,12 @@ package com.baeldung.java_8_features.groupingby; -public class Tuple { +import java.util.Objects; + +public class Tuple { + private final BlogPostType type; + private final String author; - private BlogPostType type; - private String author; - public Tuple(BlogPostType type, String author) { - super(); this.type = type; this.author = author; } @@ -15,20 +15,27 @@ public class Tuple { return type; } - public void setType(BlogPostType type) { - this.type = type; - } - public String getAuthor() { return author; } - public void setAuthor(String author) { - this.author = author; + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + Tuple tuple = (Tuple) o; + return type == tuple.type && author.equals(tuple.author); + } + + @Override + public int hashCode() { + return Objects.hash(type, author); } @Override public String toString() { - return "Tuple [type=" + type + ", author=" + author + ", getType()=" + getType() + ", getAuthor()=" + getAuthor() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]"; + return "Tuple{" + "type=" + type + ", author='" + author + '\'' + '}'; } } diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/java_8_features/groupingby/Java8GroupingByCollectorUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/java_8_features/groupingby/Java8GroupingByCollectorUnitTest.java index 323586b85f..1da705294e 100644 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/java_8_features/groupingby/Java8GroupingByCollectorUnitTest.java +++ b/core-java-modules/core-java-8/src/test/java/com/baeldung/java_8_features/groupingby/Java8GroupingByCollectorUnitTest.java @@ -1,15 +1,32 @@ package com.baeldung.java_8_features.groupingby; -import com.baeldung.java_8_features.groupingby.BlogPost; -import com.baeldung.java_8_features.groupingby.BlogPostType; -import org.junit.Test; +import static java.util.Comparator.comparingInt; +import static java.util.stream.Collectors.averagingInt; +import static java.util.stream.Collectors.counting; +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.groupingByConcurrent; +import static java.util.stream.Collectors.joining; +import static java.util.stream.Collectors.mapping; +import static java.util.stream.Collectors.maxBy; +import static java.util.stream.Collectors.summarizingInt; +import static java.util.stream.Collectors.summingInt; +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toSet; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; -import java.util.*; +import java.util.Arrays; +import java.util.EnumMap; +import java.util.IntSummaryStatistics; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; import java.util.concurrent.ConcurrentMap; -import static java.util.Comparator.comparingInt; -import static java.util.stream.Collectors.*; -import static org.junit.Assert.*; +import org.junit.Test; public class Java8GroupingByCollectorUnitTest { @@ -180,4 +197,19 @@ public class Java8GroupingByCollectorUnitTest { assertEquals(15, newsLikeStatistics.getMin()); } + @Test + public void givenAListOfPosts_whenGroupedByComplexMapKeyType_thenGetAMapBetweenTupleAndList() { + Map> postsPerTypeAndAuthor = posts.stream() + .collect(groupingBy(post -> new Tuple(post.getType(), post.getAuthor()))); + + List result = postsPerTypeAndAuthor.get(new Tuple(BlogPostType.GUIDE, "Author 1")); + + assertThat(result.size()).isEqualTo(1); + + BlogPost blogPost = result.get(0); + + assertThat(blogPost.getTitle()).isEqualTo("Programming guide"); + assertThat(blogPost.getType()).isEqualTo(BlogPostType.GUIDE); + assertThat(blogPost.getAuthor()).isEqualTo("Author 1"); + } } diff --git a/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/array/operations/ArrayOperations.java b/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/array/operations/ArrayOperations.java index 94a00f7aba..4513dbf899 100644 --- a/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/array/operations/ArrayOperations.java +++ b/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/array/operations/ArrayOperations.java @@ -192,15 +192,48 @@ public class ArrayOperations { return array[new Random().nextInt(array.length)]; } - public static Integer[] intersectionSimple(final Integer[] a, final Integer[] b){ - return Stream.of(a).filter(Arrays.asList(b)::contains).toArray(Integer[]::new); + public static Integer[] intersectionSimple(final Integer[] a, final Integer[] b) { + return Stream.of(a) + .filter(Arrays.asList(b)::contains) + .toArray(Integer[]::new); } - public static Integer[] intersectionSet(final Integer[] a, final Integer[] b){ - return Stream.of(a).filter(Arrays.asList(b)::contains).distinct().toArray(Integer[]::new); + public static Integer[] intersectionSet(final Integer[] a, final Integer[] b) { + return Stream.of(a) + .filter(Arrays.asList(b)::contains) + .distinct() + .toArray(Integer[]::new); } - public static Integer[] intersectionMultiSet(final Integer[] a, final Integer[] b){ - return Stream.of(a).filter(new LinkedList<>(Arrays.asList(b))::remove).toArray(Integer[]::new); + public static Integer[] intersectionMultiSet(final Integer[] a, final Integer[] b) { + return Stream.of(a) + .filter(new LinkedList<>(Arrays.asList(b))::remove) + .toArray(Integer[]::new); + } + + public static Integer[] addElementUsingPureJava(Integer[] srcArray, int elementToAdd) { + Integer[] destArray = new Integer[srcArray.length + 1]; + + for (int i = 0; i < srcArray.length; i++) { + destArray[i] = srcArray[i]; + } + + destArray[destArray.length - 1] = elementToAdd; + return destArray; + } + + public static int[] insertAnElementAtAGivenIndex(final int[] srcArray, int index, int newElement) { + int[] destArray = new int[srcArray.length + 1]; + int j = 0; + for (int i = 0; i < destArray.length - 1; i++) { + + if (i == index) { + destArray[i] = newElement; + } else { + destArray[i] = srcArray[j]; + j++; + } + } + return destArray; } } diff --git a/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/arraylist/operations/ArrayListOperations.java b/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/arraylist/operations/ArrayListOperations.java new file mode 100644 index 0000000000..b2aed553da --- /dev/null +++ b/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/arraylist/operations/ArrayListOperations.java @@ -0,0 +1,23 @@ +package com.baeldung.arraylist.operations; + +import java.util.ArrayList; + +public class ArrayListOperations { + + public static Integer getAnIntegerElement(ArrayList anArrayList, int index) { + return anArrayList.get(index); + } + + public static void modifyAnIntegerElement(ArrayList anArrayList, int index, Integer newElement) { + anArrayList.set(index, newElement); + } + + public static void appendAnIntegerElement(ArrayList anArrayList, Integer newElement) { + anArrayList.add(newElement); + } + + public static void insertAnIntegerElementAtIndex(ArrayList anArrayList, int index, Integer newElement) { + anArrayList.add(index, newElement); + } + +} diff --git a/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/array/operations/ArrayOperationsUnitTest.java b/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/array/operations/ArrayOperationsUnitTest.java index d72681ca05..ea9bf2c5a9 100644 --- a/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/array/operations/ArrayOperationsUnitTest.java +++ b/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/array/operations/ArrayOperationsUnitTest.java @@ -1,12 +1,11 @@ package com.baeldung.array.operations; +import java.util.Arrays; import org.assertj.core.api.Condition; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - -import java.util.Arrays; - import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertArrayEquals; public class ArrayOperationsUnitTest { @@ -262,8 +261,7 @@ public class ArrayOperationsUnitTest { @Test public void whenMapIntArrayToString_thenReturnArray() { - String[] expectedArray = new String[] { "Value: 3", "Value: 5", "Value: 2", "Value: 5", "Value: 14", - "Value: 4" }; + String[] expectedArray = new String[] { "Value: 3", "Value: 5", "Value: 2", "Value: 5", "Value: 14", "Value: 4" }; String[] output = ArrayOperations.mapIntArrayToString(defaultIntArray); assertThat(output).containsExactly(expectedArray); @@ -313,13 +311,10 @@ public class ArrayOperationsUnitTest { int[] output5 = ArrayOperations.shuffleIntArray(defaultIntArray); int[] output6 = ArrayOperations.shuffleIntArray(defaultIntArray); - Condition atLeastOneArraysIsNotEqual = new Condition( - "at least one output should be different (order-wise)") { + Condition atLeastOneArraysIsNotEqual = new Condition("at least one output should be different (order-wise)") { @Override public boolean matches(int[] value) { - return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3) - || !Arrays.equals(value, output4) || !Arrays.equals(value, output5) - || !Arrays.equals(value, output6); + return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3) || !Arrays.equals(value, output4) || !Arrays.equals(value, output5) || !Arrays.equals(value, output6); } }; @@ -335,13 +330,10 @@ public class ArrayOperationsUnitTest { Integer[] output5 = ArrayOperations.shuffleObjectArray(defaultObjectArray); Integer[] output6 = ArrayOperations.shuffleObjectArray(defaultObjectArray); - Condition atLeastOneArraysIsNotEqual = new Condition( - "at least one output should be different (order-wise)") { + Condition atLeastOneArraysIsNotEqual = new Condition("at least one output should be different (order-wise)") { @Override public boolean matches(Integer[] value) { - return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3) - || !Arrays.equals(value, output4) || !Arrays.equals(value, output5) - || !Arrays.equals(value, output6); + return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3) || !Arrays.equals(value, output4) || !Arrays.equals(value, output5) || !Arrays.equals(value, output6); } }; @@ -362,4 +354,27 @@ public class ArrayOperationsUnitTest { assertThat(defaultObjectArray).contains(output); } + + @Test + public void givenSourceArrayAndElement_whenAddElementUsingPureJavaIsInvoked_thenNewElementMustBeAdded() { + Integer[] sourceArray = { 1, 2, 3, 4 }; + int elementToAdd = 5; + + Integer[] destArray = ArrayOperations.addElementUsingPureJava(sourceArray, elementToAdd); + + Integer[] expectedArray = { 1, 2, 3, 4, 5 }; + assertArrayEquals(expectedArray, destArray); + } + + @Test + public void whenInsertAnElementAtAGivenIndexCalled_thenShiftTheFollowingElementsAndInsertTheElementInArray() { + int[] expectedArray = { 1, 4, 2, 3, 0 }; + int[] anArray = new int[4]; + anArray[0] = 1; + anArray[1] = 2; + anArray[2] = 3; + int[] outputArray = ArrayOperations.insertAnElementAtAGivenIndex(anArray, 1, 4); + + assertThat(outputArray).containsExactly(expectedArray); + } } diff --git a/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/arraylist/operations/ArrayListOperationsUnitTest.java b/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/arraylist/operations/ArrayListOperationsUnitTest.java new file mode 100644 index 0000000000..1ec7645d8f --- /dev/null +++ b/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/arraylist/operations/ArrayListOperationsUnitTest.java @@ -0,0 +1,50 @@ +package com.baeldung.arraylist.operations; + +import java.util.ArrayList; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; + +public class ArrayListOperationsUnitTest { + + private ArrayList anArrayList; + + @BeforeEach + public void setupDefaults() { + anArrayList = new ArrayList<>(); + anArrayList.add(2); + anArrayList.add(3); + anArrayList.add(4); + } + + @Test + public void whenGetAnIntegerElementCalled_thenReturnTheIntegerElement() { + Integer output = ArrayListOperations.getAnIntegerElement(anArrayList, 1); + + assertThat(output).isEqualTo(3); + } + + @Test + public void whenModifyAnIntegerElementCalled_thenModifyTheIntegerElement() { + ArrayListOperations.modifyAnIntegerElement(anArrayList, 2, 5); + Integer output = ArrayListOperations.getAnIntegerElement(anArrayList, 2); + + assertThat(output).isEqualTo(5); + } + + @Test + public void whenAppendAnIntegerElementCalled_thenTheIntegerElementIsAppendedToArrayList() { + ArrayListOperations.appendAnIntegerElement(anArrayList, 6); + Integer output = ArrayListOperations.getAnIntegerElement(anArrayList, anArrayList.size() - 1); + + assertThat(output).isEqualTo(6); + } + + @Test + public void whenInsertAnIntegerAtIndexCalled_thenTheIntegerElementIsInseredToArrayList() { + ArrayListOperations.insertAnIntegerElementAtIndex(anArrayList, 1, 10); + Integer output = ArrayListOperations.getAnIntegerElement(anArrayList, 1); + + assertThat(output).isEqualTo(10); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/DuplicatesCounter.java b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/DuplicatesCounter.java new file mode 100644 index 0000000000..04e24e5fa1 --- /dev/null +++ b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/DuplicatesCounter.java @@ -0,0 +1,42 @@ +package com.baeldung.list; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * Demo different approaches to get count of duplicated elements in an + * arrayList + */ +public class DuplicatesCounter { + + public static Map countByClassicalLoop(List inputList) { + Map resultMap = new HashMap<>(); + for (T element : inputList) { + if (resultMap.containsKey(element)) { + resultMap.put(element, resultMap.get(element) + 1L); + } else { + resultMap.put(element, 1L); + } + } + return resultMap; + } + + public static Map countByClassicalLoopWithMapCompute(List inputList) { + Map resultMap = new HashMap<>(); + for (T element : inputList) { + resultMap.compute(element, (k, v) -> v == null ? 1 : v + 1); + } + return resultMap; + } + + public static Map countByStreamToMap(List inputList) { + return inputList.stream().collect(Collectors.toMap(Function.identity(), v -> 1L, Long::sum)); + } + + public static Map countByStreamGroupBy(List inputList) { + return inputList.stream().collect(Collectors.groupingBy(k -> k, Collectors.counting())); + } +} diff --git a/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/DuplicatesCounterUnitTest.java b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/DuplicatesCounterUnitTest.java new file mode 100644 index 0000000000..4b6a03aaef --- /dev/null +++ b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/DuplicatesCounterUnitTest.java @@ -0,0 +1,55 @@ +package com.baeldung.list; + +import org.assertj.core.util.Lists; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.data.MapEntry.entry; + +class DuplicatesCounterUnitTest { + + + private static List INPUT_LIST = Lists.list( + "expect1", + "expect2", "expect2", + "expect3", "expect3", "expect3", + "expect4", "expect4", "expect4", "expect4"); + + @Test + void givenInput_whenCountByClassicalLoop_thenGetResultMap() { + Map result = DuplicatesCounter.countByClassicalLoop(INPUT_LIST); + verifyResult(result); + } + + + @Test + void givenInput_whenCountByClassicalLoopWithMapCompute_thenGetResultMap() { + Map result = DuplicatesCounter.countByClassicalLoopWithMapCompute(INPUT_LIST); + verifyResult(result); + } + + @Test + void givenInput_whenCountByStreamToMap_thenGetResultMap() { + Map result = DuplicatesCounter.countByStreamToMap(INPUT_LIST); + verifyResult(result); + } + + @Test + void givenInput_whenCountByStreamGroupBy_thenGetResultMap() { + Map result = DuplicatesCounter.countByStreamGroupBy(INPUT_LIST); + verifyResult(result); + } + + private void verifyResult(Map resultMap) { + assertThat(resultMap) + .isNotEmpty().hasSize(4) + .containsExactly( + entry("expect1", 1L), + entry("expect2", 2L), + entry("expect3", 3L), + entry("expect4", 4L)); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-datetime-computations/src/test/java/com/baeldung/date/DateDiffUnitTest.java b/core-java-modules/core-java-datetime-computations/src/test/java/com/baeldung/date/DateDiffUnitTest.java index a35699e469..226556d4bb 100644 --- a/core-java-modules/core-java-datetime-computations/src/test/java/com/baeldung/date/DateDiffUnitTest.java +++ b/core-java-modules/core-java-datetime-computations/src/test/java/com/baeldung/date/DateDiffUnitTest.java @@ -1,6 +1,6 @@ package com.baeldung.date; -import org.junit.Test; +import static org.junit.Assert.assertEquals; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -16,7 +16,7 @@ import java.util.Locale; import java.util.TimeZone; import java.util.concurrent.TimeUnit; -import static org.junit.Assert.assertEquals; +import org.junit.Test; public class DateDiffUnitTest { @@ -31,14 +31,14 @@ public class DateDiffUnitTest { assertEquals(diff, 6); } - + @Test public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() { LocalDate now = LocalDate.now(); LocalDate sixDaysBehind = now.minusDays(6); Period period = Period.between(now, sixDaysBehind); - int diff = period.getDays(); + int diff = Math.abs(period.getDays()); assertEquals(diff, 6); } @@ -68,7 +68,8 @@ public class DateDiffUnitTest { public void givenTwoZonedDateTimesInJava8_whenDifferentiating_thenWeGetSix() { LocalDateTime ldt = LocalDateTime.now(); ZonedDateTime now = ldt.atZone(ZoneId.of("America/Montreal")); - ZonedDateTime sixDaysBehind = now.withZoneSameInstant(ZoneId.of("Asia/Singapore")).minusDays(6); + ZonedDateTime sixDaysBehind = now.withZoneSameInstant(ZoneId.of("Asia/Singapore")) + .minusDays(6); long diff = ChronoUnit.DAYS.between(sixDaysBehind, now); assertEquals(diff, 6); } diff --git a/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseLocalDate.java b/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseLocalDate.java index b380c04fc2..ec8dfa2fc4 100644 --- a/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseLocalDate.java +++ b/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseLocalDate.java @@ -36,10 +36,15 @@ class UseLocalDate { } LocalDate getFirstDayOfMonth() { - LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()); + LocalDate firstDayOfMonth = LocalDate.now() + .with(TemporalAdjusters.firstDayOfMonth()); return firstDayOfMonth; } + boolean isLeapYear(LocalDate localDate) { + return localDate.isLeapYear(); + } + LocalDateTime getStartOfDay(LocalDate localDate) { LocalDateTime startofDay = localDate.atStartOfDay(); return startofDay; diff --git a/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseLocalDateTime.java b/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseLocalDateTime.java index b2ff11ba16..267a9412eb 100644 --- a/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseLocalDateTime.java +++ b/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseLocalDateTime.java @@ -2,6 +2,7 @@ package com.baeldung.datetime; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.ZoneOffset; import java.time.temporal.ChronoField; public class UseLocalDateTime { @@ -21,4 +22,7 @@ public class UseLocalDateTime { return endOfDate; } + LocalDateTime ofEpochSecond(int epochSecond, ZoneOffset zoneOffset) { + return LocalDateTime.ofEpochSecond(epochSecond, 0, zoneOffset); + } } diff --git a/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseLocalTime.java b/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseLocalTime.java index 8d166c413f..876516e365 100644 --- a/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseLocalTime.java +++ b/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseLocalTime.java @@ -9,6 +9,10 @@ public class UseLocalTime { return LocalTime.of(hour, min, seconds); } + LocalTime getLocalTimeUsingFactoryOfMethod(int hour, int min) { + return LocalTime.of(hour, min); + } + LocalTime getLocalTimeUsingParseMethod(String timeRepresentation) { return LocalTime.parse(timeRepresentation); } diff --git a/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseZonedDateTime.java b/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseZonedDateTime.java index 505bfa741f..a8948e5ce1 100644 --- a/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseZonedDateTime.java +++ b/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datetime/UseZonedDateTime.java @@ -12,31 +12,35 @@ class UseZonedDateTime { return ZonedDateTime.of(localDateTime, zoneId); } + ZonedDateTime getZonedDateTimeUsingParseMethod(String parsableString) { + return ZonedDateTime.parse(parsableString); + } + ZonedDateTime getStartOfDay(LocalDate localDate, ZoneId zone) { - ZonedDateTime startofDay = localDate.atStartOfDay() + ZonedDateTime startOfDay = localDate.atStartOfDay() .atZone(zone); - return startofDay; + return startOfDay; } ZonedDateTime getStartOfDayShorthand(LocalDate localDate, ZoneId zone) { - ZonedDateTime startofDay = localDate.atStartOfDay(zone); - return startofDay; + ZonedDateTime startOfDay = localDate.atStartOfDay(zone); + return startOfDay; } ZonedDateTime getStartOfDayFromZonedDateTime(ZonedDateTime zonedDateTime) { - ZonedDateTime startofDay = zonedDateTime.toLocalDateTime() + ZonedDateTime startOfDay = zonedDateTime.toLocalDateTime() .toLocalDate() .atStartOfDay(zonedDateTime.getZone()); - return startofDay; + return startOfDay; } ZonedDateTime getStartOfDayAtMinTime(ZonedDateTime zonedDateTime) { - ZonedDateTime startofDay = zonedDateTime.with(ChronoField.HOUR_OF_DAY, 0); - return startofDay; + ZonedDateTime startOfDay = zonedDateTime.with(ChronoField.HOUR_OF_DAY, 0); + return startOfDay; } ZonedDateTime getStartOfDayAtMidnightTime(ZonedDateTime zonedDateTime) { - ZonedDateTime startofDay = zonedDateTime.with(ChronoField.NANO_OF_DAY, 0); - return startofDay; + ZonedDateTime startOfDay = zonedDateTime.with(ChronoField.NANO_OF_DAY, 0); + return startOfDay; } } diff --git a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/date/comparison/Java8DateTimeApiGeneralComparisonsUnitTest.java b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/date/comparison/Java8DateTimeApiGeneralComparisonsUnitTest.java index ff51476e7c..af72f9e58a 100644 --- a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/date/comparison/Java8DateTimeApiGeneralComparisonsUnitTest.java +++ b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/date/comparison/Java8DateTimeApiGeneralComparisonsUnitTest.java @@ -1,12 +1,16 @@ package com.baeldung.date.comparison; -import org.junit.Test; - -import java.time.*; - import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; + +import org.junit.Test; + public class Java8DateTimeApiGeneralComparisonsUnitTest { @Test @@ -54,10 +58,8 @@ public class Java8DateTimeApiGeneralComparisonsUnitTest { @Test public void givenZonedDateTimes_whenComparing_thenAssertsPass() { - ZonedDateTime timeInNewYork = ZonedDateTime.of(2019, 8, 10, 8, 0, 0, 0, - ZoneId.of("America/New_York")); - ZonedDateTime timeInBerlin = ZonedDateTime.of(2019, 8, 10, 14, 0, 0, 0, - ZoneId.of("Europe/Berlin")); + ZonedDateTime timeInNewYork = ZonedDateTime.of(2019, 8, 10, 8, 0, 0, 0, ZoneId.of("America/New_York")); + ZonedDateTime timeInBerlin = ZonedDateTime.of(2019, 8, 10, 14, 0, 0, 0, ZoneId.of("Europe/Berlin")); assertThat(timeInNewYork.isAfter(timeInBerlin), is(false)); assertThat(timeInNewYork.isBefore(timeInBerlin), is(false)); @@ -80,4 +82,14 @@ public class Java8DateTimeApiGeneralComparisonsUnitTest { assertThat(firstTime.compareTo(secondTime), is(-1)); } + + @Test + public void givenMinMaxLocalTimes_whenComparing_thenAssertsPass() { + LocalTime minTime = LocalTime.MIN; + LocalTime time = LocalTime.of(8, 30); + LocalTime maxTime = LocalTime.MAX; + + assertThat(minTime.isBefore(time), is(true)); + assertThat(time.isBefore(maxTime), is(true)); + } } \ No newline at end of file diff --git a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/dateapi/JavaDurationUnitTest.java b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/dateapi/JavaDurationUnitTest.java index db6b353ba6..be43fb609a 100644 --- a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/dateapi/JavaDurationUnitTest.java +++ b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/dateapi/JavaDurationUnitTest.java @@ -1,16 +1,39 @@ package com.baeldung.dateapi; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import java.time.Duration; import java.time.Instant; +import java.time.LocalTime; import java.time.temporal.ChronoUnit; import org.junit.Test; public class JavaDurationUnitTest { + @Test + public void givenATimePlus30Seconds_whenRequestingDuration_thenExpect30() { + LocalTime initialTime = LocalTime.of(6, 30, 0); + LocalTime finalTime = initialTime.plus(Duration.ofSeconds(30)); + + long seconds = Duration.between(initialTime, finalTime) + .getSeconds(); + + assertThat(seconds).isEqualTo(30); + } + + @Test + public void givenATimePlus30Seconds_whenRequestingSecondsBetween_thenExpect30() { + LocalTime initialTime = LocalTime.of(6, 30, 0); + LocalTime finalTime = initialTime.plus(Duration.ofSeconds(30)); + + long seconds = ChronoUnit.SECONDS.between(initialTime, finalTime); + + assertThat(seconds).isEqualTo(30); + } + @Test public void test2() { Instant start = Instant.parse("2017-10-03T10:15:30.00Z"); @@ -29,11 +52,15 @@ public class JavaDurationUnitTest { Duration fromMinutes = Duration.ofMinutes(60); assertEquals(1, fromMinutes.toHours()); - assertEquals(120, duration.plusSeconds(60).getSeconds()); - assertEquals(30, duration.minusSeconds(30).getSeconds()); + assertEquals(120, duration.plusSeconds(60) + .getSeconds()); + assertEquals(30, duration.minusSeconds(30) + .getSeconds()); - assertEquals(120, duration.plus(60, ChronoUnit.SECONDS).getSeconds()); - assertEquals(30, duration.minus(30, ChronoUnit.SECONDS).getSeconds()); + assertEquals(120, duration.plus(60, ChronoUnit.SECONDS) + .getSeconds()); + assertEquals(30, duration.minus(30, ChronoUnit.SECONDS) + .getSeconds()); Duration fromChar1 = Duration.parse("P1DT1H10M10.5S"); Duration fromChar2 = Duration.parse("PT10M"); diff --git a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/dateapi/JavaPeriodUnitTest.java b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/dateapi/JavaPeriodUnitTest.java index 81e9153a51..6dfbbe8c24 100644 --- a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/dateapi/JavaPeriodUnitTest.java +++ b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/dateapi/JavaPeriodUnitTest.java @@ -1,18 +1,41 @@ package com.baeldung.dateapi; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import java.time.LocalDate; +import java.time.Period; +import java.time.temporal.ChronoUnit; + import org.apache.log4j.Logger; import org.junit.Test; -import java.time.LocalDate; -import java.time.Period; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; - public class JavaPeriodUnitTest { private static final Logger LOG = Logger.getLogger(JavaPeriodUnitTest.class); + @Test + public void givenADatePlus5Days_whenRequestingPeriod_thenExpectFive() { + LocalDate initialDate = LocalDate.parse("2007-05-10"); + LocalDate finalDate = initialDate.plus(Period.ofDays(5)); + + int days = Period.between(initialDate, finalDate) + .getDays(); + + assertThat(days).isEqualTo(5); + } + + @Test + public void givenADatePlus5Days_whenRequestingDaysBetween_thenExpectFive() { + LocalDate initialDate = LocalDate.parse("2007-05-10"); + LocalDate finalDate = initialDate.plus(Period.ofDays(5)); + + long days = ChronoUnit.DAYS.between(initialDate, finalDate); + + assertThat(days).isEqualTo(5); + } + @Test public void whenTestPeriod_thenOk() { @@ -24,8 +47,10 @@ public class JavaPeriodUnitTest { LOG.info(String.format("Years:%d months:%d days:%d", period.getYears(), period.getMonths(), period.getDays())); assertFalse(period.isNegative()); - assertEquals(56, period.plusDays(50).getDays()); - assertEquals(9, period.minusMonths(2).getMonths()); + assertEquals(56, period.plusDays(50) + .getDays()); + assertEquals(9, period.minusMonths(2) + .getMonths()); Period fromUnits = Period.of(3, 10, 10); Period fromDays = Period.ofDays(50); diff --git a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseLocalDateTimeUnitTest.java b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseLocalDateTimeUnitTest.java index 5709fc7209..2bbee16e25 100644 --- a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseLocalDateTimeUnitTest.java +++ b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseLocalDateTimeUnitTest.java @@ -7,12 +7,13 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Month; +import java.time.ZoneOffset; import org.junit.Test; public class UseLocalDateTimeUnitTest { - UseLocalDateTime useLocalDateTime = new UseLocalDateTime(); + private UseLocalDateTime useLocalDateTime = new UseLocalDateTime(); @Test public void givenString_whenUsingParse_thenLocalDateTime() { @@ -33,4 +34,28 @@ public class UseLocalDateTimeUnitTest { assertThat(endOfDayFromGivenDirectly.toLocalTime()).isEqualTo(LocalTime.MAX); assertThat(endOfDayFromGivenDirectly.toString()).isEqualTo("2018-06-23T23:59:59.999999999"); } + + @Test + public void givenLocalDateTimeInFebruary_whenRequestingMonth_thenMonthIsFebruary() { + LocalDateTime givenLocalDateTime = LocalDateTime.of(2015, Month.FEBRUARY, 20, 6, 30); + + assertThat(givenLocalDateTime.getMonth()).isEqualTo(Month.FEBRUARY); + } + + @Test + public void givenLocalDateTime_whenManipulating_thenResultIsAsExpected() { + LocalDateTime givenLocalDateTime = LocalDateTime.parse("2015-02-20T06:30:00"); + + LocalDateTime manipulatedLocalDateTime = givenLocalDateTime.plusDays(1); + manipulatedLocalDateTime = manipulatedLocalDateTime.minusHours(2); + + assertThat(manipulatedLocalDateTime).isEqualTo(LocalDateTime.of(2015, Month.FEBRUARY, 21, 4, 30)); + } + + @Test + public void whenRequestTimeFromEpoch_thenResultIsAsExpected() { + LocalDateTime result = useLocalDateTime.ofEpochSecond(1465817690, ZoneOffset.UTC); + + assertThat(result.toString()).isEqualTo("2016-06-13T11:34:50"); + } } diff --git a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseLocalDateUnitTest.java b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseLocalDateUnitTest.java index bb9b60956d..fd22be9260 100644 --- a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseLocalDateUnitTest.java +++ b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseLocalDateUnitTest.java @@ -12,7 +12,7 @@ import org.junit.Test; public class UseLocalDateUnitTest { - UseLocalDate useLocalDate = new UseLocalDate(); + private UseLocalDate useLocalDate = new UseLocalDate(); @Test public void givenValues_whenUsingFactoryOf_thenLocalDate() { @@ -88,4 +88,31 @@ public class UseLocalDateUnitTest { assertThat(endOfDayWithMax.toString()).isEqualTo("2018-06-23T23:59:59.999999999"); } + @Test + public void givenTheYear2000_whenCheckingForLeapYear_thenReturnTrue() { + LocalDate given = LocalDate.parse("2000-06-23"); + + boolean leapYear = useLocalDate.isLeapYear(given); + + assertThat(leapYear).isEqualTo(true); + } + + @Test + public void givenTheYear2004_whenCheckingForLeapYear_thenReturnTrue() { + LocalDate given = LocalDate.parse("2004-06-23"); + + boolean leapYear = useLocalDate.isLeapYear(given); + + assertThat(leapYear).isEqualTo(true); + } + + @Test + public void givenTheYear2019_whenCheckingForLeapYear_thenReturnFalse() { + LocalDate given = LocalDate.parse("2019-06-23"); + + boolean leapYear = useLocalDate.isLeapYear(given); + + assertThat(leapYear).isEqualTo(false); + } + } diff --git a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseLocalTimeUnitTest.java b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseLocalTimeUnitTest.java index 996e200ae9..afee9126f9 100644 --- a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseLocalTimeUnitTest.java +++ b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseLocalTimeUnitTest.java @@ -7,21 +7,30 @@ import org.junit.Test; public class UseLocalTimeUnitTest { - UseLocalTime useLocalTime = new UseLocalTime(); + private UseLocalTime useLocalTime = new UseLocalTime(); @Test public void givenValues_whenUsingFactoryOf_thenLocalTime() { - Assert.assertEquals("07:07:07", useLocalTime.getLocalTimeUsingFactoryOfMethod(7, 7, 7).toString()); + Assert.assertEquals("07:07:07", useLocalTime.getLocalTimeUsingFactoryOfMethod(7, 7, 7) + .toString()); + } + + @Test + public void givenValues_whenUsingFactoryOfWithoutSeconds_thenLocalTime() { + Assert.assertEquals("07:07", useLocalTime.getLocalTimeUsingFactoryOfMethod(7, 7) + .toString()); } @Test public void givenString_whenUsingParse_thenLocalTime() { - Assert.assertEquals("06:30", useLocalTime.getLocalTimeUsingParseMethod("06:30").toString()); + Assert.assertEquals("06:30", useLocalTime.getLocalTimeUsingParseMethod("06:30") + .toString()); } @Test public void givenTime_whenAddHour_thenLocalTime() { - Assert.assertEquals("07:30", useLocalTime.addAnHour(LocalTime.of(6, 30)).toString()); + Assert.assertEquals("07:30", useLocalTime.addAnHour(LocalTime.of(6, 30)) + .toString()); } @Test diff --git a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseZonedDateTimeUnitTest.java b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseZonedDateTimeUnitTest.java index f9b4008888..4a39f6056e 100644 --- a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseZonedDateTimeUnitTest.java +++ b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datetime/UseZonedDateTimeUnitTest.java @@ -7,13 +7,14 @@ import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; import java.time.ZonedDateTime; +import java.util.Set; import org.junit.Assert; import org.junit.Test; public class UseZonedDateTimeUnitTest { - UseZonedDateTime zonedDateTime = new UseZonedDateTime(); + private UseZonedDateTime zonedDateTime = new UseZonedDateTime(); @Test public void givenZoneId_thenZonedDateTime() { @@ -22,6 +23,13 @@ public class UseZonedDateTimeUnitTest { Assert.assertEquals(zoneId, ZoneId.from(zonedDatetime)); } + @Test + public void whenRequestingZones_thenAtLeastOneIsReturned() { + Set allZoneIds = ZoneId.getAvailableZoneIds(); + + assertThat(allZoneIds.size()).isGreaterThan(1); + } + @Test public void givenLocalDateOrZoned_whenSettingStartOfDay_thenReturnMidnightInAllCases() { LocalDate given = LocalDate.parse("2018-06-23"); @@ -42,4 +50,15 @@ public class UseZonedDateTimeUnitTest { assertThat(startOfOfDayWithMethod.toLocalTime() .toString()).isEqualTo("00:00"); } + + @Test + public void givenAStringWithTimeZone_whenParsing_thenEqualsExpected() { + ZonedDateTime resultFromString = zonedDateTime.getZonedDateTimeUsingParseMethod("2015-05-03T10:15:30+01:00[Europe/Paris]"); + ZonedDateTime resultFromLocalDateTime = ZonedDateTime.of(2015, 5, 3, 11, 15, 30, 0, ZoneId.of("Europe/Paris")); + + assertThat(resultFromString.getZone()).isEqualTo(ZoneId.of("Europe/Paris")); + assertThat(resultFromLocalDateTime.getZone()).isEqualTo(ZoneId.of("Europe/Paris")); + + assertThat(resultFromString).isEqualTo(resultFromLocalDateTime); + } } diff --git a/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/modifiers/FirstClass.java b/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/modifiers/FirstClass.java new file mode 100644 index 0000000000..a80387d61a --- /dev/null +++ b/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/modifiers/FirstClass.java @@ -0,0 +1,18 @@ +package com.baeldung.core.modifiers; + +public class FirstClass { + + protected String name; + + protected FirstClass(String name) { + this.name = name; + } + + protected String getName() { + return name; + } + + protected static class InnerClass { + public InnerClass() {} + } +} diff --git a/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/modifiers/GenericClass.java b/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/modifiers/GenericClass.java new file mode 100644 index 0000000000..83381931d0 --- /dev/null +++ b/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/modifiers/GenericClass.java @@ -0,0 +1,15 @@ +package com.baeldung.core.modifiers; + +public class GenericClass { + + public static void main(String[] args) { + // accessing protected constructor + FirstClass first = new FirstClass("random name"); + // using protected method + System.out.println("FirstClass name is " + first.getName()); + // accessing a protected field + first.name = "new name"; + // instantiating protected inner class + FirstClass.InnerClass innerClass = new FirstClass.InnerClass(); + } +} diff --git a/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/modifiers/otherpackage/SecondClass.java b/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/modifiers/otherpackage/SecondClass.java new file mode 100644 index 0000000000..528f97539a --- /dev/null +++ b/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/modifiers/otherpackage/SecondClass.java @@ -0,0 +1,18 @@ +package com.baeldung.core.modifiers.otherpackage; + +import com.baeldung.core.modifiers.FirstClass; + +public class SecondClass extends FirstClass { + + public SecondClass(String name) { + // accessing protected constructor + super(name); + // using protected method + System.out.println("SecondClass name is " + this.getName()); + // accessing a protected field + this.name = "new name"; + // instantiating protected inner class -> add public constructor to InnerClass + FirstClass.InnerClass innerClass = new FirstClass.InnerClass(); + } + +} diff --git a/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/modifiers/otherpackage/SecondGenericClass.java b/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/modifiers/otherpackage/SecondGenericClass.java new file mode 100644 index 0000000000..d29533edef --- /dev/null +++ b/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/modifiers/otherpackage/SecondGenericClass.java @@ -0,0 +1,20 @@ +package com.baeldung.core.modifiers.otherpackage; + +import com.baeldung.core.modifiers.FirstClass; +//import com.baeldung.core.modifiers.FirstClass.InnerClass; + +public class SecondGenericClass { + + // uncomment the following lines to see the errors + public static void main(String[] args) { + // accessing protected constructor + // FirstClass first = new FirstClass("random name"); + // using protected method + // System.out.println("FirstClass name is " + first.getName()); + // accessing a protected field + // first.name = "new name"; + // instantiating protected inner class + // FirstClass.InnerClass innerClass = new FirstClass.InnerClass(); + } + +} diff --git a/ddd/pom.xml b/ddd/pom.xml index 4ac65ea841..c249007ba4 100644 --- a/ddd/pom.xml +++ b/ddd/pom.xml @@ -3,7 +3,6 @@ 4.0.0 com.baeldung.ddd ddd - 0.0.1-SNAPSHOT ddd jar DDD series examples @@ -35,7 +34,6 @@ org.junit.platform junit-platform-launcher - ${junit-platform.version} test @@ -85,8 +83,6 @@ 1.0.1 - 2.22.0 - 2.0.6.RELEASE \ No newline at end of file diff --git a/disruptor/pom.xml b/disruptor/pom.xml index e1d78e7ed6..213331f25c 100644 --- a/disruptor/pom.xml +++ b/disruptor/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung disruptor 0.1.0-SNAPSHOT disruptor @@ -116,10 +115,7 @@ 3.3.6 - - 6.10 - 3.6.1 - + 2.4.3 3.0.2 1.4.4 diff --git a/dozer/pom.xml b/dozer/pom.xml index 8234eb4c79..c781962d83 100644 --- a/dozer/pom.xml +++ b/dozer/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung dozer 1.0 dozer diff --git a/dubbo/pom.xml b/dubbo/pom.xml index 9fe228bf89..947175b6a0 100644 --- a/dubbo/pom.xml +++ b/dubbo/pom.xml @@ -32,7 +32,6 @@ 2.5.7 3.4.11 0.10 - 2.19.1 diff --git a/ethereum/pom.xml b/ethereum/pom.xml index 80e1c04676..f4850df844 100644 --- a/ethereum/pom.xml +++ b/ethereum/pom.xml @@ -3,7 +3,6 @@ 4.0.0 com.baeldung.ethereum ethereum - 0.0.1-SNAPSHOT ethereum @@ -207,8 +206,6 @@ - UTF-8 - 8.5.4 1.5.0-RELEASE 3.3.1 1.5.6.RELEASE diff --git a/flyway-cdi-extension/pom.xml b/flyway-cdi-extension/pom.xml index e8b327e35f..49bd6bd9fd 100644 --- a/flyway-cdi-extension/pom.xml +++ b/flyway-cdi-extension/pom.xml @@ -3,7 +3,6 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung flyway-cdi-extension 1.0-SNAPSHOT flyway-cdi-extension @@ -51,8 +50,6 @@ - 1.8 - 1.8 2.0.SP1 3.0.5.Final 5.1.4 diff --git a/google-web-toolkit/pom.xml b/google-web-toolkit/pom.xml index 0723cd3be2..8a7cb87701 100644 --- a/google-web-toolkit/pom.xml +++ b/google-web-toolkit/pom.xml @@ -5,7 +5,6 @@ 4.0.0 - com.baeldung google-web-toolkit 1.0-SNAPSHOT google-web-toolkit diff --git a/gson/pom.xml b/gson/pom.xml index 9d2cf630d0..4aa6c00458 100644 --- a/gson/pom.xml +++ b/gson/pom.xml @@ -2,7 +2,6 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung gson 0.1-SNAPSHOT gson diff --git a/guava-collections-map/pom.xml b/guava-collections-map/pom.xml index 45bb6b8caa..21597d0e28 100644 --- a/guava-collections-map/pom.xml +++ b/guava-collections-map/pom.xml @@ -13,9 +13,6 @@ ../parent-java - - - guava-collections-map @@ -26,7 +23,4 @@ - - - \ No newline at end of file diff --git a/guava-collections-set/pom.xml b/guava-collections-set/pom.xml index 46dcae492d..640a28c4c1 100644 --- a/guava-collections-set/pom.xml +++ b/guava-collections-set/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung guava-collections-set 0.1.0-SNAPSHOT guava-collections-set @@ -28,8 +27,6 @@ - - 27.1-jre 3.6.1 diff --git a/guava-collections/pom.xml b/guava-collections/pom.xml index 8cdb086029..9dfcceaab8 100644 --- a/guava-collections/pom.xml +++ b/guava-collections/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung guava-collections 0.1.0-SNAPSHOT guava-collections @@ -54,7 +53,6 @@ - 24.0-jre 4.1 diff --git a/guava-io/pom.xml b/guava-io/pom.xml index 2a66baba80..aaaf7edd4e 100644 --- a/guava-io/pom.xml +++ b/guava-io/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung guava-io 0.1.0-SNAPSHOT guava-io diff --git a/guava/pom.xml b/guava/pom.xml index 17c930cdd2..4ca9918455 100644 --- a/guava/pom.xml +++ b/guava/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung guava 0.1.0-SNAPSHOT guava @@ -39,9 +38,6 @@ - - 24.0-jre - 3.6.1 diff --git a/hazelcast/pom.xml b/hazelcast/pom.xml index 705792ad05..9e0b0671d0 100644 --- a/hazelcast/pom.xml +++ b/hazelcast/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung hazelcast 0.0.1-SNAPSHOT hazelcast diff --git a/httpclient-simple/pom.xml b/httpclient-simple/pom.xml index b2b0f98dd1..183c4438de 100644 --- a/httpclient-simple/pom.xml +++ b/httpclient-simple/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung httpclient-simple 0.1-SNAPSHOT httpclient-simple diff --git a/httpclient/pom.xml b/httpclient/pom.xml index 8cd483cfc6..6316ee4eed 100644 --- a/httpclient/pom.xml +++ b/httpclient/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung httpclient 0.1-SNAPSHOT httpclient @@ -48,11 +47,6 @@ httpmime ${httpclient.version} - - commons-codec - commons-codec - ${commons-codec.version} - org.apache.httpcomponents httpasyncclient @@ -82,51 +76,12 @@ - - - live - - - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - **/*ManualTest.java - - - **/*LiveTest.java - - - - - - - json - - - - - - - - - 19.0 - 1.10 4.1.4 2.5.1 4.5.8 - - 1.6.1 \ No newline at end of file diff --git a/hystrix/pom.xml b/hystrix/pom.xml index e08af2c40f..4aeb47f095 100644 --- a/hystrix/pom.xml +++ b/hystrix/pom.xml @@ -41,23 +41,11 @@ hystrix-metrics-event-stream ${hystrix-metrics-event-stream.version} - com.netflix.rxjava rxjava-core ${rxjava-core.version} - - org.hamcrest - hamcrest-all - ${hamcrest-all.version} - test - - - org.springframework - spring-test - test - @@ -65,9 +53,7 @@ 1.5.8 0.20.7 - 2.7 1.5.8 - 1.5.8 diff --git a/jackson-simple/README.md b/jackson-simple/README.md index 302fcb5b05..9d24a20e4a 100644 --- a/jackson-simple/README.md +++ b/jackson-simple/README.md @@ -2,7 +2,8 @@ This module contains articles about Jackson that are also part of the Jackson Ebook. -###The Course +### The Course + The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java b/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java index 2745e4f767..a339ddf2c5 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java +++ b/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java @@ -1,20 +1,29 @@ package com.baeldung.jackson.objectmapper; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +import java.io.File; +import java.net.URL; +import java.util.List; +import java.util.Map; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + import com.baeldung.jackson.objectmapper.dto.Car; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Test; - -import java.util.List; -import java.util.Map; - -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; public class JavaReadWriteJsonExampleUnitTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + final String EXAMPLE_JSON = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; final String LOCAL_JSON = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"BMW\" }]"; @@ -27,6 +36,19 @@ public class JavaReadWriteJsonExampleUnitTest { assertThat(carAsString, containsString("renault")); } + @Test + public void whenWriteToFile_thanCorrect() throws Exception { + File resultFile = folder.newFile("car.json"); + + ObjectMapper objectMapper = new ObjectMapper(); + Car car = new Car("yellow", "renault"); + objectMapper.writeValue(resultFile, car); + + Car fromFile = objectMapper.readValue(resultFile, Car.class); + assertEquals(car.getType(), fromFile.getType()); + assertEquals(car.getColor(), fromFile.getColor()); + } + @Test public void whenReadJsonToJava_thanCorrect() throws Exception { final ObjectMapper objectMapper = new ObjectMapper(); @@ -66,4 +88,26 @@ public class JavaReadWriteJsonExampleUnitTest { assertNotNull(key); } } + + @Test + public void wheReadFromFile_thanCorrect() throws Exception { + File resource = new File("src/test/resources/json_car.json"); + + ObjectMapper objectMapper = new ObjectMapper(); + Car fromFile = objectMapper.readValue(resource, Car.class); + + assertEquals("BMW", fromFile.getType()); + assertEquals("Black", fromFile.getColor()); + } + + @Test + public void wheReadFromUrl_thanCorrect() throws Exception { + URL resource = new URL("file:src/test/resources/json_car.json"); + + ObjectMapper objectMapper = new ObjectMapper(); + Car fromFile = objectMapper.readValue(resource, Car.class); + + assertEquals("BMW", fromFile.getType()); + assertEquals("Black", fromFile.getColor()); + } } diff --git a/jackson-simple/src/test/resources/.gitignore b/jackson-simple/src/test/resources/.gitignore deleted file mode 100644 index 83c05e60c8..0000000000 --- a/jackson-simple/src/test/resources/.gitignore +++ /dev/null @@ -1,13 +0,0 @@ -*.class - -#folders# -/target -/neoDb* -/data -/src/main/webapp/WEB-INF/classes -*/META-INF/* - -# Packaged files # -*.jar -*.war -*.ear \ No newline at end of file diff --git a/jackson-simple/src/test/resources/json_car.json b/jackson-simple/src/test/resources/json_car.json new file mode 100644 index 0000000000..2f6b35b523 --- /dev/null +++ b/jackson-simple/src/test/resources/json_car.json @@ -0,0 +1,4 @@ +{ + "color": "Black", + "type": "BMW" +} \ No newline at end of file diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/City.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/City.java new file mode 100644 index 0000000000..2bbef534c3 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/City.java @@ -0,0 +1,15 @@ +package com.baeldung.jackson.deserialization.enums; + +public class City { + + private Distance distance; + + public Distance getDistance() { + return distance; + } + + public void setDistance(Distance distance) { + this.distance = distance; + } + +} diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/Distance.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/Distance.java new file mode 100644 index 0000000000..2c96b6eb80 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/Distance.java @@ -0,0 +1,31 @@ +package com.baeldung.jackson.deserialization.enums; + +public enum Distance { + + KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001); + + private String unit; + private double meters; + + private Distance(String unit, double meters) { + this.unit = unit; + this.meters = meters; + } + + public void setMeters(double meters) { + this.meters = meters; + } + + public double getMeters() { + return meters; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } +} + diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/City.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/City.java new file mode 100644 index 0000000000..36ef7e244a --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/City.java @@ -0,0 +1,15 @@ +package com.baeldung.jackson.deserialization.enums.customdeserializer; + +public class City { + + private Distance distance; + + public Distance getDistance() { + return distance; + } + + public void setDistance(Distance distance) { + this.distance = distance; + } + +} diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/CustomEnumDeserializer.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/CustomEnumDeserializer.java new file mode 100644 index 0000000000..bae0c0df34 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/CustomEnumDeserializer.java @@ -0,0 +1,42 @@ +package com.baeldung.jackson.deserialization.enums.customdeserializer; + +import java.io.IOException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + +public class CustomEnumDeserializer extends StdDeserializer { + + private static final long serialVersionUID = -1166032307856492833L; + + public CustomEnumDeserializer() { + this(null); + } + + public CustomEnumDeserializer(Class c) { + super(c); + } + + @Override + public Distance deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException { + + JsonNode node = jsonParser.getCodec().readTree(jsonParser); + + String unit = node.get("unit").asText(); + double meters = node.get("meters").asDouble(); + + for (Distance distance : Distance.values()) { + + if (distance.getUnit().equals(unit) && + Double.compare(distance.getMeters(), meters) == 0) { + + return distance; + } + } + + return null; + } + +} diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/Distance.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/Distance.java new file mode 100644 index 0000000000..9cb3a0c391 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/Distance.java @@ -0,0 +1,33 @@ +package com.baeldung.jackson.deserialization.enums.customdeserializer; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +@JsonDeserialize(using = CustomEnumDeserializer.class) +public enum Distance { + + KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001); + + private String unit; + private double meters; + + private Distance(String unit, double meters) { + this.unit = unit; + this.meters = meters; + } + + public double getMeters() { + return meters; + } + + public void setMeters(double meters) { + this.meters = meters; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } +} \ No newline at end of file diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsoncreator/City.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsoncreator/City.java new file mode 100644 index 0000000000..6a478dd263 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsoncreator/City.java @@ -0,0 +1,15 @@ +package com.baeldung.jackson.deserialization.enums.jsoncreator; + +public class City { + + private Distance distance; + + public Distance getDistance() { + return distance; + } + + public void setDistance(Distance distance) { + this.distance = distance; + } + +} diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsoncreator/Distance.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsoncreator/Distance.java new file mode 100644 index 0000000000..18235fb5c6 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsoncreator/Distance.java @@ -0,0 +1,48 @@ +package com.baeldung.jackson.deserialization.enums.jsoncreator; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +public enum Distance { + + KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001); + + private String unit; + private double meters; + + private Distance(String unit, double meters) { + this.unit = unit; + this.meters = meters; + } + + public void setMeters(double meters) { + this.meters = meters; + } + + public double getMeters() { + return meters; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + + @JsonCreator + public static Distance forValues(@JsonProperty("unit") String unit, @JsonProperty("meters") double meters) { + + for (Distance distance : Distance.values()) { + if (distance.unit.equals(unit) && Double.compare(distance.meters, meters) == 0) { + + return distance; + } + } + + return null; + + } +} + diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonproperty/City.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonproperty/City.java new file mode 100644 index 0000000000..7f763f97c4 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonproperty/City.java @@ -0,0 +1,15 @@ +package com.baeldung.jackson.deserialization.enums.jsonproperty; + +public class City { + + private Distance distance; + + public Distance getDistance() { + return distance; + } + + public void setDistance(Distance distance) { + this.distance = distance; + } + +} diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonproperty/Distance.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonproperty/Distance.java new file mode 100644 index 0000000000..e671a9ae5b --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonproperty/Distance.java @@ -0,0 +1,51 @@ +package com.baeldung.jackson.deserialization.enums.jsonproperty; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public enum Distance { + + @JsonProperty("distance-in-km") + KILOMETER("km", 1000), + + @JsonProperty("distance-in-miles") + MILE("miles", 1609.34), + + @JsonProperty("distance-in-meters") + METER("meters", 1), + + @JsonProperty("distance-in-inches") + INCH("inches", 0.0254), + + @JsonProperty("distance-in-cm") + CENTIMETER("cm", 0.01), + + @JsonProperty("distance-in-mm") + MILLIMETER("mm", 0.001); + + private String unit; + private double meters; + + private Distance(String unit, double meters) { + this.unit = unit; + this.meters = meters; + } + + public void setMeters(double meters) { + this.meters = meters; + } + + public double getMeters() { + return meters; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + +} + + diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonvalue/City.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonvalue/City.java new file mode 100644 index 0000000000..014668ea8c --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonvalue/City.java @@ -0,0 +1,15 @@ +package com.baeldung.jackson.deserialization.enums.jsonvalue; + +public class City { + + private Distance distance; + + public Distance getDistance() { + return distance; + } + + public void setDistance(Distance distance) { + this.distance = distance; + } + +} diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonvalue/Distance.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonvalue/Distance.java new file mode 100644 index 0000000000..44bb5f20c3 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonvalue/Distance.java @@ -0,0 +1,35 @@ +package com.baeldung.jackson.deserialization.enums.jsonvalue; + +import com.fasterxml.jackson.annotation.JsonValue; + +public enum Distance { + + KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001); + + private String unit; + private double meters; + + private Distance(String unit, double meters) { + this.unit = unit; + this.meters = meters; + } + + public void setMeters(double meters) { + this.meters = meters; + } + + @JsonValue + public double getMeters() { + return meters; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + +} + diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/DefaultEnumDeserializationUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/DefaultEnumDeserializationUnitTest.java new file mode 100644 index 0000000000..c7ce96e013 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/DefaultEnumDeserializationUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.jackson.deserialization.enums; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.io.IOException; +import org.junit.Test; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class DefaultEnumDeserializationUnitTest { + + @Test + public void givenEnum_whenDeserializingJson_thenCorrectRepresentation() throws JsonParseException, IOException { + String json = "{\"distance\":\"KILOMETER\"}"; + City city = new ObjectMapper().readValue(json, City.class); + + assertEquals(Distance.KILOMETER, city.getDistance()); + } + +} diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/customdeserializer/EnumCustomDeserializationUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/customdeserializer/EnumCustomDeserializationUnitTest.java new file mode 100644 index 0000000000..e8dbfa8df8 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/customdeserializer/EnumCustomDeserializationUnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung.jackson.deserialization.enums.customdeserializer; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.io.IOException; +import org.junit.Test; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class EnumCustomDeserializationUnitTest { + + @Test + public void givenEnumWithCustomDeserializer_whenDeserializingJson_thenCorrectRepresentation() throws JsonParseException, IOException { + String json = "{\"distance\": {\"unit\":\"miles\",\"meters\":1609.34}}"; + + City city = new ObjectMapper().readValue(json, City.class); + assertEquals(Distance.MILE, city.getDistance()); + } +} diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsoncreator/EnumDeserializationUsingJsonCreatorUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsoncreator/EnumDeserializationUsingJsonCreatorUnitTest.java new file mode 100644 index 0000000000..d778cbe26b --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsoncreator/EnumDeserializationUsingJsonCreatorUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.jackson.deserialization.enums.jsoncreator; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.io.IOException; +import org.junit.Test; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class EnumDeserializationUsingJsonCreatorUnitTest { + + @Test + public void givenEnumWithJsonCreator_whenDeserializingJson_thenCorrectRepresentation() throws JsonParseException, IOException { + String json = "{\"distance\": {\"unit\":\"miles\",\"meters\":1609.34}}"; + + City city = new ObjectMapper().readValue(json, City.class); + assertEquals(Distance.MILE, city.getDistance()); + } + +} diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsonproperty/EnumDeserializationUsingJsonPropertyUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsonproperty/EnumDeserializationUsingJsonPropertyUnitTest.java new file mode 100644 index 0000000000..134f4a29cc --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsonproperty/EnumDeserializationUsingJsonPropertyUnitTest.java @@ -0,0 +1,20 @@ +package com.baeldung.jackson.deserialization.enums.jsonproperty; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.io.IOException; +import org.junit.Test; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class EnumDeserializationUsingJsonPropertyUnitTest { + + @Test + public void givenEnumWithJsonProperty_whenDeserializingJson_thenCorrectRepresentation() throws JsonParseException, IOException { + String json = "{\"distance\": \"distance-in-km\"}"; + + City city = new ObjectMapper().readValue(json, City.class); + assertEquals(Distance.KILOMETER, city.getDistance()); + + } + +} diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsonvalue/EnumDeserializationUsingJsonValueUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsonvalue/EnumDeserializationUsingJsonValueUnitTest.java new file mode 100644 index 0000000000..85afcb9a69 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsonvalue/EnumDeserializationUsingJsonValueUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.jackson.deserialization.enums.jsonvalue; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.io.IOException; +import org.junit.Test; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class EnumDeserializationUsingJsonValueUnitTest { + + @Test + public void givenEnumWithJsonValue_whenDeserializingJson_thenCorrectRepresentation() throws JsonParseException, IOException { + String json = "{\"distance\": \"0.0254\"}"; + + City city = new ObjectMapper().readValue(json, City.class); + assertEquals(Distance.INCH, city.getDistance()); + } + +} diff --git a/jackson/src/test/java/com/baeldung/jackson/enums/JacksonEnumSerializationUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/enums/JacksonEnumSerializationUnitTest.java index 45c0ba1382..d4fb2401ed 100644 --- a/jackson/src/test/java/com/baeldung/jackson/enums/JacksonEnumSerializationUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/enums/JacksonEnumSerializationUnitTest.java @@ -2,11 +2,8 @@ package com.baeldung.jackson.enums; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertThat; - import java.io.IOException; - import org.junit.Test; - import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -18,5 +15,5 @@ public class JacksonEnumSerializationUnitTest { assertThat(dtoAsString, containsString("1609.34")); } - + } diff --git a/java-dates/src/main/java/com/baeldung/datetime/UseDateTimeFormatter.java b/java-dates/src/main/java/com/baeldung/datetime/UseDateTimeFormatter.java new file mode 100644 index 0000000000..13a2ba6a1a --- /dev/null +++ b/java-dates/src/main/java/com/baeldung/datetime/UseDateTimeFormatter.java @@ -0,0 +1,21 @@ +package com.baeldung.datetime; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.util.Locale; + +public class UseDateTimeFormatter { + public String formatAsIsoDate(LocalDateTime localDateTime) { + return localDateTime.format(DateTimeFormatter.ISO_DATE); + } + + public String formatCustom(LocalDateTime localDateTime, String pattern) { + return localDateTime.format(DateTimeFormatter.ofPattern(pattern)); + } + + public String formatWithStyleAndLocale(LocalDateTime localDateTime, FormatStyle formatStyle, Locale locale) { + return localDateTime.format(DateTimeFormatter.ofLocalizedDateTime(formatStyle) + .withLocale(locale)); + } +} diff --git a/java-dates/src/main/java/com/baeldung/datetime/UseOffsetDateTime.java b/java-dates/src/main/java/com/baeldung/datetime/UseOffsetDateTime.java new file mode 100644 index 0000000000..ed8499d6e0 --- /dev/null +++ b/java-dates/src/main/java/com/baeldung/datetime/UseOffsetDateTime.java @@ -0,0 +1,11 @@ +package com.baeldung.datetime; + +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; + +public class UseOffsetDateTime { + public OffsetDateTime offsetOfLocalDateTimeAndOffset(LocalDateTime localDateTime, ZoneOffset offset) { + return OffsetDateTime.of(localDateTime, offset); + } +} diff --git a/java-dates/src/test/java/com/baeldung/datetime/UseDateTimeFormatterUnitTest.java b/java-dates/src/test/java/com/baeldung/datetime/UseDateTimeFormatterUnitTest.java new file mode 100644 index 0000000000..797e0b954a --- /dev/null +++ b/java-dates/src/test/java/com/baeldung/datetime/UseDateTimeFormatterUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.datetime; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.LocalDateTime; +import java.time.Month; +import java.time.format.FormatStyle; +import java.util.Locale; + +import org.junit.Test; + +public class UseDateTimeFormatterUnitTest { + private final UseDateTimeFormatter subject = new UseDateTimeFormatter(); + private final LocalDateTime localDateTime = LocalDateTime.of(2015, Month.JANUARY, 25, 6, 30); + + @Test + public void givenALocalDate_whenFormattingAsIso_thenPass() { + String result = subject.formatAsIsoDate(localDateTime); + + assertThat(result).isEqualTo("2015-01-25"); + } + + @Test + public void givenALocalDate_whenFormattingWithPattern_thenPass() { + String result = subject.formatCustom(localDateTime, "yyyy/MM/dd"); + + assertThat(result).isEqualTo("2015/01/25"); + } + + @Test + public void givenALocalDate_whenFormattingWithStyleAndLocale_thenPass() { + String result = subject.formatWithStyleAndLocale(localDateTime, FormatStyle.MEDIUM, Locale.UK); + + assertThat(result).isEqualTo("25 Jan 2015, 06:30:00"); + } +} \ No newline at end of file diff --git a/java-dates/src/test/java/com/baeldung/datetime/UseOffsetDateTimeUnitTest.java b/java-dates/src/test/java/com/baeldung/datetime/UseOffsetDateTimeUnitTest.java new file mode 100644 index 0000000000..5b58dd3848 --- /dev/null +++ b/java-dates/src/test/java/com/baeldung/datetime/UseOffsetDateTimeUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.datetime; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.LocalDateTime; +import java.time.Month; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; + +import org.junit.Test; + +public class UseOffsetDateTimeUnitTest { + private final UseOffsetDateTime subject = new UseOffsetDateTime(); + + @Test + public void givenAZoneOffSetAndLocalDateTime_whenCombing_thenValidResult() { + ZoneOffset offset = ZoneOffset.of("+02:00"); + LocalDateTime localDateTime = LocalDateTime.of(2015, Month.FEBRUARY, 20, 6, 30); + + OffsetDateTime result = subject.offsetOfLocalDateTimeAndOffset(localDateTime, offset); + + assertThat(result.toString()).isEqualTo("2015-02-20T06:30+02:00"); + } +} \ No newline at end of file diff --git a/java-dates/src/test/java/com/baeldung/datetime/UseToInstantUnitTest.java b/java-dates/src/test/java/com/baeldung/datetime/UseToInstantUnitTest.java new file mode 100644 index 0000000000..78d9a647fe --- /dev/null +++ b/java-dates/src/test/java/com/baeldung/datetime/UseToInstantUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.datetime; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.LocalDateTime; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +import org.junit.Test; + +public class UseToInstantUnitTest { + + private UseToInstant subject = new UseToInstant(); + + @Test + public void givenAGregorianCalenderDate_whenConvertingToLocalDate_thenAsExpected() { + GregorianCalendar givenCalender = new GregorianCalendar(2018, Calendar.JULY, 28); + + LocalDateTime localDateTime = subject.convertDateToLocalDate(givenCalender); + + assertThat(localDateTime).isEqualTo("2018-07-28T00:00:00"); + } + + @Test + public void givenADate_whenConvertingToLocalDate_thenAsExpected() { + Date givenDate = new Date(1465817690000L); + + LocalDateTime localDateTime = subject.convertDateToLocalDate(givenDate); + + assertThat(localDateTime).isEqualTo("2016-06-13T13:34:50"); + } +} \ No newline at end of file diff --git a/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/Position.java b/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/Position.java new file mode 100644 index 0000000000..dd7694feb6 --- /dev/null +++ b/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/Position.java @@ -0,0 +1,51 @@ +package com.baeldung.prototype; + +public final class Position { + + private final int x; + private final int y; + + public Position(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + x; + result = prime * result + y; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Position other = (Position) obj; + if (x != other.x) + return false; + if (y != other.y) + return false; + return true; + } + + @Override + public String toString() { + return "Position [x=" + x + ", y=" + y + "]"; + } + +} diff --git a/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/Tree.java b/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/Tree.java new file mode 100644 index 0000000000..f95d568647 --- /dev/null +++ b/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/Tree.java @@ -0,0 +1,54 @@ +package com.baeldung.prototype; + +public class Tree implements Cloneable { + + private double mass; + private double height; + private Position position; + + public Tree(double mass, double height) { + this.mass = mass; + this.height = height; + } + + public void setMass(double mass) { + this.mass = mass; + } + + public void setHeight(double height) { + this.height = height; + } + + public void setPosition(Position position) { + this.position = position; + } + + public double getMass() { + return mass; + } + + public double getHeight() { + return height; + } + + public Position getPosition() { + return position; + } + + @Override + public Tree clone() { + Tree tree = null; + try { + tree = (Tree) super.clone(); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + return tree; + } + + @Override + public String toString() { + return "Tree [mass=" + mass + ", height=" + height + ", position=" + position + "]"; + } + +} diff --git a/patterns/design-patterns-creational/src/test/java/com/baeldung/prototype/TreePrototypeUnitTest.java b/patterns/design-patterns-creational/src/test/java/com/baeldung/prototype/TreePrototypeUnitTest.java new file mode 100644 index 0000000000..0d06da53d6 --- /dev/null +++ b/patterns/design-patterns-creational/src/test/java/com/baeldung/prototype/TreePrototypeUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.prototype; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class TreePrototypeUnitTest { + + @Test + public void givenATreePrototypeWhenClonedThenCreateA_Clone() { + double mass = 10.0; + double height = 3.7; + Position position = new Position(3, 7); + Position otherPosition = new Position(4, 8); + + Tree tree = new Tree(mass, height); + tree.setPosition(position); + Tree anotherTree = tree.clone(); + anotherTree.setPosition(otherPosition); + + assertEquals(position, tree.getPosition()); + assertEquals(otherPosition, anotherTree.getPosition()); + } +} diff --git a/persistence-modules/hibernate5-2/pom.xml b/persistence-modules/hibernate5-2/pom.xml new file mode 100644 index 0000000000..ef091c331e --- /dev/null +++ b/persistence-modules/hibernate5-2/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + com.baeldung + hibernate5-2 + 0.1-SNAPSHOT + hibernate5-2 + jar + Hibernate tutorial illustrating the use of named parameters + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + org.hibernate + hibernate-core + 5.4.7.Final + + + + com.h2database + h2 + 1.4.200 + + + + + true + + + diff --git a/persistence-modules/hibernate5-2/src/main/java/com/baeldung/hibernateparameters/Event.java b/persistence-modules/hibernate5-2/src/main/java/com/baeldung/hibernateparameters/Event.java new file mode 100644 index 0000000000..7912659a09 --- /dev/null +++ b/persistence-modules/hibernate5-2/src/main/java/com/baeldung/hibernateparameters/Event.java @@ -0,0 +1,30 @@ +package com.baeldung.hibernateparameters; + +public class Event { + private Long id; + + private String title; + + public Event() { + } + + public Event(String title) { + this.title = title; + } + + public Long getId() { + return id; + } + + private void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate5-2/src/main/resources/com/baeldung/hibernateparameters/Event.hbm.xml b/persistence-modules/hibernate5-2/src/main/resources/com/baeldung/hibernateparameters/Event.hbm.xml new file mode 100644 index 0000000000..c485080879 --- /dev/null +++ b/persistence-modules/hibernate5-2/src/main/resources/com/baeldung/hibernateparameters/Event.hbm.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + diff --git a/persistence-modules/hibernate5-2/src/main/resources/hibernate.cfg.xml b/persistence-modules/hibernate5-2/src/main/resources/hibernate.cfg.xml new file mode 100644 index 0000000000..be564aaf5a --- /dev/null +++ b/persistence-modules/hibernate5-2/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,28 @@ + + + + + + + org.h2.Driver + jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1 + sa + + + 1 + + org.hibernate.dialect.H2Dialect + + org.hibernate.cache.internal.NoCacheProvider + + true + + create + + + + + + \ No newline at end of file diff --git a/persistence-modules/hibernate5-2/src/test/java/com/baeldung/hibernateparameters/NamedParameterUnitTest.java b/persistence-modules/hibernate5-2/src/test/java/com/baeldung/hibernateparameters/NamedParameterUnitTest.java new file mode 100644 index 0000000000..4efa1e1f68 --- /dev/null +++ b/persistence-modules/hibernate5-2/src/test/java/com/baeldung/hibernateparameters/NamedParameterUnitTest.java @@ -0,0 +1,73 @@ +package com.baeldung.hibernateparameters; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.query.Query; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +public class NamedParameterUnitTest { + private SessionFactory sessionFactory; + + @Before + public void setUp() throws Exception { + final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() + .configure() + .build(); + try { + sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); + Session session = sessionFactory.openSession(); + session.beginTransaction(); + session.save(new Event("Event 1")); + session.save(new Event("Event 2")); + session.getTransaction().commit(); + session.close(); + } catch (Exception e) { + fail(e); + StandardServiceRegistryBuilder.destroy(registry); + } + } + + @After + public void tearDown() throws Exception { + if (sessionFactory != null) { + sessionFactory.close(); + } + } + + @Test + public void whenNamedParameterProvided_thenCorrect() { + Session session = sessionFactory.openSession(); + session.beginTransaction(); + Query query = session.createQuery("from Event E WHERE E.title = :eventTitle", Event.class); + + // This binds the value "Event1" to the parameter :eventTitle + query.setParameter("eventTitle", "Event 1"); + + assertEquals(1, query.list().size()); + session.getTransaction().commit(); + session.close(); + } + + @Test(expected = org.hibernate.QueryException.class) + public void whenNamedParameterMissing_thenThrowsQueryException() { + Session session = sessionFactory.openSession(); + session.beginTransaction(); + Query query = session.createQuery("from Event E WHERE E.title = :eventTitle", Event.class); + + try { + query.list(); + fail("We are expecting an exception!"); + } finally { + session.getTransaction().commit(); + session.close(); + } + } +} diff --git a/persistence-modules/java-jpa-2/pom.xml b/persistence-modules/java-jpa-2/pom.xml index 790c4a07df..2488c5ccdf 100644 --- a/persistence-modules/java-jpa-2/pom.xml +++ b/persistence-modules/java-jpa-2/pom.xml @@ -1,5 +1,4 @@ - 4.0.0 java-jpa-2 @@ -49,6 +48,13 @@ ${postgres.version} runtime + + + org.assertj + assertj-core + ${assertj.version} + test + @@ -108,6 +114,7 @@ 2.7.4-RC1 42.2.5 2.2 + 3.11.1 \ No newline at end of file diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/multipleentities/AllergensAsEntity.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/multipleentities/AllergensAsEntity.java new file mode 100644 index 0000000000..e5e228d013 --- /dev/null +++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/multipleentities/AllergensAsEntity.java @@ -0,0 +1,73 @@ +package com.baeldung.jpa.multipletables.multipleentities; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; +import javax.persistence.PrimaryKeyJoinColumn; +import javax.persistence.Table; + +import com.baeldung.jpa.multipletables.secondarytable.MealAsSingleEntity; + +@Entity +@Table(name = "allergens") +public class AllergensAsEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "meal_id") + private Long mealId; + + @OneToOne + @PrimaryKeyJoinColumn(name = "meal_id") + private MealAsSingleEntity meal; + + @Column(name = "peanuts") + private boolean peanuts; + + @Column(name = "celery") + private boolean celery; + + @Column(name = "sesame_seeds") + private boolean sesameSeeds; + + public MealAsSingleEntity getMeal() { + return meal; + } + + public void setMeal(MealAsSingleEntity meal) { + this.meal = meal; + } + + public boolean isPeanuts() { + return peanuts; + } + + public void setPeanuts(boolean peanuts) { + this.peanuts = peanuts; + } + + public boolean isCelery() { + return celery; + } + + public void setCelery(boolean celery) { + this.celery = celery; + } + + public boolean isSesameSeeds() { + return sesameSeeds; + } + + public void setSesameSeeds(boolean sesameSeeds) { + this.sesameSeeds = sesameSeeds; + } + + @Override + public String toString() { + return "AllergensAsEntity [peanuts=" + peanuts + ", celery=" + celery + ", sesameSeeds=" + sesameSeeds + "]"; + } + +} diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/multipleentities/MealWithMultipleEntities.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/multipleentities/MealWithMultipleEntities.java new file mode 100644 index 0000000000..74105f8f1f --- /dev/null +++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/multipleentities/MealWithMultipleEntities.java @@ -0,0 +1,75 @@ +package com.baeldung.jpa.multipletables.multipleentities; + +import java.math.BigDecimal; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "meal") +public class MealWithMultipleEntities { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @Column(name = "name") + private String name; + + @Column(name = "description") + private String description; + + @Column(name = "price") + private BigDecimal price; + + @OneToOne(mappedBy = "meal") + private AllergensAsEntity allergens; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public AllergensAsEntity getAllergens() { + return allergens; + } + + public void setAllergens(AllergensAsEntity allergens) { + this.allergens = allergens; + } + + public Long getId() { + return id; + } + + @Override + public String toString() { + return "MealWithMultipleEntities [id=" + id + ", name=" + name + ", description=" + description + ", price=" + price + ", allergens=" + allergens + "]"; + } + +} diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/secondarytable/MealAsSingleEntity.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/secondarytable/MealAsSingleEntity.java new file mode 100644 index 0000000000..2929f391a4 --- /dev/null +++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/secondarytable/MealAsSingleEntity.java @@ -0,0 +1,99 @@ +package com.baeldung.jpa.multipletables.secondarytable; + +import java.math.BigDecimal; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.PrimaryKeyJoinColumn; +import javax.persistence.SecondaryTable; +import javax.persistence.Table; + +@Entity +@Table(name = "meal") +@SecondaryTable(name = "allergens", pkJoinColumns = @PrimaryKeyJoinColumn(name = "meal_id")) +public class MealAsSingleEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @Column(name = "name") + private String name; + + @Column(name = "description") + private String description; + + @Column(name = "price") + private BigDecimal price; + + @Column(name = "peanuts", table = "allergens") + private boolean peanuts; + + @Column(name = "celery", table = "allergens") + private boolean celery; + + @Column(name = "sesame_seeds", table = "allergens") + private boolean sesameSeeds; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public boolean isPeanuts() { + return peanuts; + } + + public void setPeanuts(boolean peanuts) { + this.peanuts = peanuts; + } + + public boolean isCelery() { + return celery; + } + + public void setCelery(boolean celery) { + this.celery = celery; + } + + public boolean isSesameSeeds() { + return sesameSeeds; + } + + public void setSesameSeeds(boolean sesameSeeds) { + this.sesameSeeds = sesameSeeds; + } + + public Long getId() { + return id; + } + + @Override + public String toString() { + return "MealAsSingleEntity [id=" + id + ", name=" + name + ", description=" + description + ", price=" + price + ", peanuts=" + peanuts + ", celery=" + celery + ", sesameSeeds=" + sesameSeeds + "]"; + } + +} diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/secondarytable/embeddable/AllergensAsEmbeddable.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/secondarytable/embeddable/AllergensAsEmbeddable.java new file mode 100644 index 0000000000..1c1f05890b --- /dev/null +++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/secondarytable/embeddable/AllergensAsEmbeddable.java @@ -0,0 +1,47 @@ +package com.baeldung.jpa.multipletables.secondarytable.embeddable; + +import javax.persistence.Column; +import javax.persistence.Embeddable; + +@Embeddable +public class AllergensAsEmbeddable { + + @Column(name = "peanuts", table = "allergens") + private boolean peanuts; + + @Column(name = "celery", table = "allergens") + private boolean celery; + + @Column(name = "sesame_seeds", table = "allergens") + private boolean sesameSeeds; + + public boolean isPeanuts() { + return peanuts; + } + + public void setPeanuts(boolean peanuts) { + this.peanuts = peanuts; + } + + public boolean isCelery() { + return celery; + } + + public void setCelery(boolean celery) { + this.celery = celery; + } + + public boolean isSesameSeeds() { + return sesameSeeds; + } + + public void setSesameSeeds(boolean sesameSeeds) { + this.sesameSeeds = sesameSeeds; + } + + @Override + public String toString() { + return "AllergensAsEmbeddable [peanuts=" + peanuts + ", celery=" + celery + ", sesameSeeds=" + sesameSeeds + "]"; + } + +} diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/secondarytable/embeddable/MealWithEmbeddedAllergens.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/secondarytable/embeddable/MealWithEmbeddedAllergens.java new file mode 100644 index 0000000000..87a83157d7 --- /dev/null +++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/multipletables/secondarytable/embeddable/MealWithEmbeddedAllergens.java @@ -0,0 +1,78 @@ +package com.baeldung.jpa.multipletables.secondarytable.embeddable; + +import java.math.BigDecimal; + +import javax.persistence.Column; +import javax.persistence.Embedded; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.PrimaryKeyJoinColumn; +import javax.persistence.SecondaryTable; +import javax.persistence.Table; + +@Entity +@Table(name = "meal") +@SecondaryTable(name = "allergens", pkJoinColumns = @PrimaryKeyJoinColumn(name = "meal_id")) +public class MealWithEmbeddedAllergens { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @Column(name = "name") + private String name; + + @Column(name = "description") + private String description; + + @Column(name = "price") + private BigDecimal price; + + @Embedded + private AllergensAsEmbeddable allergens; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public AllergensAsEmbeddable getAllergens() { + return allergens; + } + + public void setAllergens(AllergensAsEmbeddable allergens) { + this.allergens = allergens; + } + + public Long getId() { + return id; + } + + @Override + public String toString() { + return "MealWithEmbeddedAllergens [id=" + id + ", name=" + name + ", description=" + description + ", price=" + price + ", allergens=" + allergens + "]"; + } + +} diff --git a/persistence-modules/java-jpa-2/src/main/resources/META-INF/persistence.xml b/persistence-modules/java-jpa-2/src/main/resources/META-INF/persistence.xml index cd46901792..11e007973f 100644 --- a/persistence-modules/java-jpa-2/src/main/resources/META-INF/persistence.xml +++ b/persistence-modules/java-jpa-2/src/main/resources/META-INF/persistence.xml @@ -135,4 +135,32 @@ + + org.hibernate.jpa.HibernatePersistenceProvider + com.baeldung.jpa.multipletables.multipleentities.MealWithMultipleEntities + com.baeldung.jpa.multipletables.multipleentities.AllergensAsEntity + + com.baeldung.jpa.multipletables.secondarytable.MealAsSingleEntity + + com.baeldung.jpa.multipletables.secondarytable.embeddable.MealWithEmbeddedAllergens + com.baeldung.jpa.multipletables.secondarytable.embeddable.AllergensAsEmbeddable + + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/java-jpa-2/src/test/java/com/baeldung/jpa/multipletables/MultipleTablesIntegrationTest.java b/persistence-modules/java-jpa-2/src/test/java/com/baeldung/jpa/multipletables/MultipleTablesIntegrationTest.java new file mode 100644 index 0000000000..99b2cd69ee --- /dev/null +++ b/persistence-modules/java-jpa-2/src/test/java/com/baeldung/jpa/multipletables/MultipleTablesIntegrationTest.java @@ -0,0 +1,79 @@ +package com.baeldung.jpa.multipletables; + +import static org.assertj.core.api.Assertions.*; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.baeldung.jpa.multipletables.multipleentities.MealWithMultipleEntities; +import com.baeldung.jpa.multipletables.secondarytable.MealAsSingleEntity; +import com.baeldung.jpa.multipletables.secondarytable.embeddable.MealWithEmbeddedAllergens; + +public class MultipleTablesIntegrationTest { + + private static EntityManagerFactory emf; + private static EntityManager em; + + @BeforeClass + public static void setup() { + emf = Persistence.createEntityManagerFactory("jpa-h2-multipltables"); + em = emf.createEntityManager(); + } + + @Test + public void entityManager_shouldLoadMealAsSingleEntity() { + // given + + // when + MealAsSingleEntity meal = em.find(MealAsSingleEntity.class, 1L); + + // then + assertThat(meal).isNotNull(); + assertThat(meal.getId()).isEqualTo(1L); + assertThat(meal.isPeanuts()).isFalse(); + assertThat(meal.isCelery()).isTrue(); + } + + @Test + public void entityManager_shouldLoadMealWithEmbeddedAllergens() { + // given + + // when + MealWithEmbeddedAllergens meal = em.find(MealWithEmbeddedAllergens.class, 1L); + + // then + assertThat(meal).isNotNull(); + assertThat(meal.getId()).isEqualTo(1L); + assertThat(meal.getAllergens()).isNotNull(); + assertThat(meal.getAllergens().isPeanuts()).isFalse(); + assertThat(meal.getAllergens().isCelery()).isTrue(); + } + + @Test + public void entityManager_shouldLoadMealWithAllergensEntity() { + // given + + // when + MealWithMultipleEntities meal = em.find(MealWithMultipleEntities.class, 1L); + + // then + assertThat(meal).isNotNull(); + assertThat(meal.getId()).isEqualTo(1L); + assertThat(meal.getAllergens()).isNotNull(); + assertThat(meal.getAllergens().isPeanuts()).isFalse(); + assertThat(meal.getAllergens().isCelery()).isTrue(); + } + + @AfterClass + public static void teardown() { + if (emf != null) { + emf.close(); + } + } + +} diff --git a/persistence-modules/java-jpa-2/src/test/resources/multipletables.sql b/persistence-modules/java-jpa-2/src/test/resources/multipletables.sql new file mode 100644 index 0000000000..226e63258b --- /dev/null +++ b/persistence-modules/java-jpa-2/src/test/resources/multipletables.sql @@ -0,0 +1,8 @@ +drop table if exists allergens; +drop table if exists meal; + +create table meal (id bigint auto_increment, name varchar(255) not null, description varchar(255) not null, price decimal(19, 2) not null, primary key (id)); +create table allergens (meal_id bigint auto_increment, peanuts number(1) not null, celery number(1) not null, sesame_seeds number(1) not null, primary key (meal_id)); + +insert into meal (id, name, description, price) values (1, 'Pizza', 'Delicious', 5); +insert into allergens (meal_id, peanuts, celery, sesame_seeds) values (1, 0, 1, 0); diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 932b62a235..3dac8baa60 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -23,7 +23,7 @@ hibernate5 hibernate-ogm hibernate-mapping - hibernate-parameters + hibernate5-2 influxdb java-cassandra java-cockroachdb @@ -49,6 +49,7 @@ spring-data-elasticsearch spring-data-gemfire spring-data-jpa + spring-data-jpa-3 spring-data-keyvalue spring-data-mongodb spring-data-neo4j @@ -60,7 +61,7 @@ spring-jpa spring-persistence-simple jpa-hibernate-cascade-type - r2dbc + r2dbc spring-boot-jdbi diff --git a/persistence-modules/spring-data-jpa-3/pom.xml b/persistence-modules/spring-data-jpa-3/pom.xml new file mode 100644 index 0000000000..f743fce2a3 --- /dev/null +++ b/persistence-modules/spring-data-jpa-3/pom.xml @@ -0,0 +1,78 @@ + + + + 4.0.0 + spring-data-jpa-3 + spring-data-jpa-3 + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.postgresql + postgresql + ${postgresql.version} + + + + + org.testcontainers + junit-jupiter + ${testcontainers.version} + test + + + org.testcontainers + postgresql + ${testcontainers.version} + test + + + org.springframework.boot + spring-boot-starter-test + test + + + junit + junit + + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.junit.platform + junit-platform-launcher + ${junit-platform.version} + test + + + + + 1.12.2 + 42.2.8 + + + diff --git a/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..c0490d50c6 --- /dev/null +++ b/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/Application.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/tx/Payment.java b/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/tx/Payment.java new file mode 100644 index 0000000000..37f3d09381 --- /dev/null +++ b/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/tx/Payment.java @@ -0,0 +1,55 @@ +package com.baeldung.tx; + +import javax.persistence.*; + +@Entity +public class Payment { + + @Id + @GeneratedValue + private Long id; + + private Long amount; + + @Column(unique = true) + private String referenceNumber; + + @Enumerated(EnumType.STRING) + private State state; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getAmount() { + return amount; + } + + public void setAmount(Long amount) { + this.amount = amount; + } + + public String getReferenceNumber() { + return referenceNumber; + } + + public void setReferenceNumber(String referenceNumber) { + this.referenceNumber = referenceNumber; + } + + public State getState() { + return state; + } + + public void setState(State state) { + this.state = state; + } + + public enum State { + STARTED, FAILED, SUCCESSFUL + } +} diff --git a/persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/tx/ManualTransactionIntegrationTest.java b/persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/tx/ManualTransactionIntegrationTest.java new file mode 100644 index 0000000000..62d64d4372 --- /dev/null +++ b/persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/tx/ManualTransactionIntegrationTest.java @@ -0,0 +1,171 @@ +package com.baeldung.tx; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.support.DefaultTransactionDefinition; +import org.springframework.transaction.support.TransactionCallbackWithoutResult; +import org.springframework.transaction.support.TransactionTemplate; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import javax.persistence.EntityManager; + +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace.NONE; +import static org.springframework.transaction.annotation.Propagation.NOT_SUPPORTED; + +@DataJpaTest +@Testcontainers +@ActiveProfiles("test") +@AutoConfigureTestDatabase(replace = NONE) +@Transactional(propagation = NOT_SUPPORTED) +class ManualTransactionIntegrationTest { + + @Container + private static PostgreSQLContainer pg = initPostgres(); + + @Autowired + private PlatformTransactionManager transactionManager; + + @Autowired + private EntityManager entityManager; + + private TransactionTemplate transactionTemplate; + + @BeforeEach + void setUp() { + transactionTemplate = new TransactionTemplate(transactionManager); + } + + @AfterEach + void flushDb() { + transactionTemplate.execute(status -> entityManager + .createQuery("delete from Payment") + .executeUpdate()); + } + + @Test + void givenAPayment_WhenNotDuplicate_ThenShouldCommit() { + Long id = transactionTemplate.execute(status -> { + Payment payment = new Payment(); + payment.setAmount(1000L); + payment.setReferenceNumber("Ref-1"); + payment.setState(Payment.State.SUCCESSFUL); + + entityManager.persist(payment); + + return payment.getId(); + }); + + Payment payment = entityManager.find(Payment.class, id); + assertThat(payment).isNotNull(); + } + + @Test + void givenAPayment_WhenMarkAsRollback_ThenShouldRollback() { + transactionTemplate.execute(status -> { + Payment payment = new Payment(); + payment.setAmount(1000L); + payment.setReferenceNumber("Ref-1"); + payment.setState(Payment.State.SUCCESSFUL); + + entityManager.persist(payment); + status.setRollbackOnly(); + + return payment.getId(); + }); + + assertThat(entityManager + .createQuery("select p from Payment p") + .getResultList()).isEmpty(); + } + + @Test + void givenTwoPayments_WhenRefIsDuplicate_ThenShouldRollback() { + try { + transactionTemplate.execute(s -> { + Payment first = new Payment(); + first.setAmount(1000L); + first.setReferenceNumber("Ref-1"); + first.setState(Payment.State.SUCCESSFUL); + + Payment second = new Payment(); + second.setAmount(2000L); + second.setReferenceNumber("Ref-1"); + second.setState(Payment.State.SUCCESSFUL); + + entityManager.persist(first); + entityManager.persist(second); + + return "Ref-1"; + }); + } catch (Exception ignored) { + } + + assertThat(entityManager + .createQuery("select p from Payment p") + .getResultList()).isEmpty(); + } + + @Test + void givenAPayment_WhenNotExpectingAnyResult_ThenShouldCommit() { + transactionTemplate.execute(new TransactionCallbackWithoutResult() { + @Override + protected void doInTransactionWithoutResult(TransactionStatus status) { + Payment payment = new Payment(); + payment.setReferenceNumber("Ref-1"); + payment.setState(Payment.State.SUCCESSFUL); + + entityManager.persist(payment); + } + }); + + assertThat(entityManager + .createQuery("select p from Payment p") + .getResultList()).hasSize(1); + } + + @Test + void givenAPayment_WhenUsingTxManager_ThenShouldCommit() { + DefaultTransactionDefinition definition = new DefaultTransactionDefinition(); + definition.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ); + definition.setTimeout(3); + + TransactionStatus status = transactionManager.getTransaction(definition); + try { + Payment payment = new Payment(); + payment.setReferenceNumber("Ref-1"); + payment.setState(Payment.State.SUCCESSFUL); + + entityManager.persist(payment); + transactionManager.commit(status); + } catch (Exception ex) { + transactionManager.rollback(status); + } + + assertThat(entityManager + .createQuery("select p from Payment p") + .getResultList()).hasSize(1); + } + + private static PostgreSQLContainer initPostgres() { + PostgreSQLContainer pg = new PostgreSQLContainer<>("postgres:11.1") + .withDatabaseName("baeldung") + .withUsername("test") + .withPassword("test"); + pg.setPortBindings(singletonList("54320:5432")); + + return pg; + } +} diff --git a/persistence-modules/spring-data-jpa-3/src/test/resources/application-test.properties b/persistence-modules/spring-data-jpa-3/src/test/resources/application-test.properties new file mode 100644 index 0000000000..aa7093f751 --- /dev/null +++ b/persistence-modules/spring-data-jpa-3/src/test/resources/application-test.properties @@ -0,0 +1,4 @@ +spring.jpa.hibernate.ddl-auto=update +spring.datasource.url=jdbc:postgresql://localhost:54320/baeldung +spring.datasource.username=test +spring.datasource.password=test diff --git a/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/persistence/service/transactional/FooTransactionalUnitTest.java b/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/persistence/service/transactional/FooTransactionalUnitTest.java new file mode 100644 index 0000000000..6f2a499bc5 --- /dev/null +++ b/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/persistence/service/transactional/FooTransactionalUnitTest.java @@ -0,0 +1,250 @@ +package com.baeldung.persistence.service.transactional; + +import com.baeldung.persistence.model.Foo; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; +import org.springframework.stereotype.Service; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; +import org.springframework.transaction.IllegalTransactionStateException; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.support.TransactionTemplate; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceTransactionalTestConfig.class }, loader = AnnotationConfigContextLoader.class) +@DirtiesContext +public class FooTransactionalUnitTest { + + static abstract class BasicFooDao { + @PersistenceContext private EntityManager entityManager; + + public Foo findOne(final long id) { + return entityManager.find(Foo.class, id); + } + + public Foo create(final Foo entity) { + entityManager.persist(entity); + return entity; + } + } + + @Repository + static class RequiredTransactionalFooDao extends BasicFooDao { + @Override + @Transactional(propagation = Propagation.REQUIRED) + public Foo create(Foo entity) { + return super.create(entity); + } + } + + @Repository + static class RequiresNewTransactionalFooDao extends BasicFooDao { + @Override + @Transactional(propagation = Propagation.REQUIRES_NEW) + public Foo create(Foo entity) { + return super.create(entity); + } + } + + @Repository + static class SupportTransactionalFooDao extends BasicFooDao { + @Override + @Transactional(propagation = Propagation.SUPPORTS) + public Foo create(Foo entity) { + return super.create(entity); + } + } + + @Repository + static class MandatoryTransactionalFooDao extends BasicFooDao { + @Override + @Transactional(propagation = Propagation.MANDATORY) + public Foo create(Foo entity) { + return super.create(entity); + } + } + + @Repository + static class SupportTransactionalFooService { + @Transactional(propagation = Propagation.SUPPORTS) + public Foo identity(Foo entity) { + return entity; + } + } + + @Service + static class MandatoryTransactionalFooService { + @Transactional(propagation = Propagation.MANDATORY) + public Foo identity(Foo entity) { + return entity; + } + } + + @Service + static class NotSupportedTransactionalFooService { + @Transactional(propagation = Propagation.NOT_SUPPORTED) + public Foo identity(Foo entity) { + return entity; + } + } + + @Service + static class NeverTransactionalFooService { + @Transactional(propagation = Propagation.NEVER) + public Foo identity(Foo entity) { + return entity; + } + } + + @Autowired private TransactionTemplate transactionTemplate; + + @Autowired private RequiredTransactionalFooDao requiredTransactionalFooDao; + + @Autowired private RequiresNewTransactionalFooDao requiresNewTransactionalFooDao; + + @Autowired private SupportTransactionalFooDao supportTransactionalFooDao; + + @Autowired private MandatoryTransactionalFooDao mandatoryTransactionalFooDao; + + @Autowired private MandatoryTransactionalFooService mandatoryTransactionalFooService; + + @Autowired private NeverTransactionalFooService neverTransactionalFooService; + + @Autowired private NotSupportedTransactionalFooService notSupportedTransactionalFooService; + + @Autowired private SupportTransactionalFooService supportTransactionalFooService; + + @After + public void tearDown(){ + PersistenceTransactionalTestConfig.clearSpy(); + } + + @Test + public void givenRequiredWithNoActiveTransaction_whenCallCreate_thenExpect1NewAnd0Suspend() { + requiredTransactionalFooDao.create(new Foo("baeldung")); + PersistenceTransactionalTestConfig.TransactionSynchronizationAdapterSpy transactionSpy = PersistenceTransactionalTestConfig.getSpy(); + Assert.assertEquals(0, transactionSpy.getSuspend()); + Assert.assertEquals(1, transactionSpy.getCreate()); + } + + + + @Test + public void givenRequiresNewWithNoActiveTransaction_whenCallCreate_thenExpect1NewAnd0Suspend() { + requiresNewTransactionalFooDao.create(new Foo("baeldung")); + PersistenceTransactionalTestConfig.TransactionSynchronizationAdapterSpy transactionSpy = PersistenceTransactionalTestConfig.getSpy(); + Assert.assertEquals(0, transactionSpy.getSuspend()); + Assert.assertEquals(1, transactionSpy.getCreate()); + } + + @Test + public void givenSupportWithNoActiveTransaction_whenCallService_thenExpect0NewAnd0Suspend() { + supportTransactionalFooService.identity(new Foo("baeldung")); + PersistenceTransactionalTestConfig.TransactionSynchronizationAdapterSpy transactionSpy = PersistenceTransactionalTestConfig.getSpy(); + Assert.assertEquals(0, transactionSpy.getSuspend()); + Assert.assertEquals(0, transactionSpy.getCreate()); + } + + @Test(expected = IllegalTransactionStateException.class) + public void givenMandatoryWithNoActiveTransaction_whenCallService_thenExpectIllegalTransactionStateExceptionWith0NewAnd0Suspend() { + mandatoryTransactionalFooService.identity(new Foo("baeldung")); + PersistenceTransactionalTestConfig.TransactionSynchronizationAdapterSpy transactionSpy = PersistenceTransactionalTestConfig.getSpy(); + Assert.assertEquals(0, transactionSpy.getSuspend()); + Assert.assertEquals(0, transactionSpy.getCreate()); + } + + @Test + public void givenNotSupportWithNoActiveTransaction_whenCallService_thenExpect0NewAnd0Suspend() { + notSupportedTransactionalFooService.identity(new Foo("baeldung")); + PersistenceTransactionalTestConfig.TransactionSynchronizationAdapterSpy transactionSpy = PersistenceTransactionalTestConfig.getSpy(); + Assert.assertEquals(0, transactionSpy.getSuspend()); + Assert.assertEquals(0, transactionSpy.getCreate()); + } + + @Test + public void givenNeverWithNoActiveTransaction_whenCallService_thenExpect0NewAnd0Suspend() { + neverTransactionalFooService.identity(new Foo("baeldung")); + PersistenceTransactionalTestConfig.TransactionSynchronizationAdapterSpy transactionSpy = PersistenceTransactionalTestConfig.getSpy(); + Assert.assertEquals(0, transactionSpy.getSuspend()); + Assert.assertEquals(0, transactionSpy.getCreate()); + } + + @Test + public void givenRequiredWithActiveTransaction_whenCallCreate_thenExpect0NewAnd0Suspend() { + transactionTemplate.execute(status -> { + Foo foo = new Foo("baeldung"); + return requiredTransactionalFooDao.create(foo); + }); + PersistenceTransactionalTestConfig.TransactionSynchronizationAdapterSpy transactionSpy = PersistenceTransactionalTestConfig.getSpy(); + Assert.assertEquals(0, transactionSpy.getSuspend()); + Assert.assertEquals(1, transactionSpy.getCreate()); + } + + @Test + public void givenRequiresNewWithActiveTransaction_whenCallCreate_thenExpect1NewAnd1Suspend() { + transactionTemplate.execute(status -> { + Foo foo = new Foo("baeldung"); + return requiresNewTransactionalFooDao.create(foo); + }); + PersistenceTransactionalTestConfig.TransactionSynchronizationAdapterSpy transactionSpy = PersistenceTransactionalTestConfig.getSpy(); + Assert.assertEquals(1, transactionSpy.getSuspend()); + Assert.assertEquals(2, transactionSpy.getCreate()); + } + + @Test + public void givenSupportWithActiveTransaction_whenCallCreate_thenExpect0NewAnd0Suspend() { + transactionTemplate.execute(status -> { + Foo foo = new Foo("baeldung"); + return supportTransactionalFooDao.create(foo); + }); + PersistenceTransactionalTestConfig.TransactionSynchronizationAdapterSpy transactionSpy = PersistenceTransactionalTestConfig.getSpy(); + Assert.assertEquals(0, transactionSpy.getSuspend()); + Assert.assertEquals(1, transactionSpy.getCreate()); + } + + @Test + public void givenMandatoryWithActiveTransaction_whenCallCreate_thenExpect0NewAnd0Suspend() { + + transactionTemplate.execute(status -> { + Foo foo = new Foo("baeldung"); + return mandatoryTransactionalFooDao.create(foo); + }); + + PersistenceTransactionalTestConfig.TransactionSynchronizationAdapterSpy transactionSpy = PersistenceTransactionalTestConfig.getSpy(); + Assert.assertEquals(0, transactionSpy.getSuspend()); + Assert.assertEquals(1, transactionSpy.getCreate()); + } + + @Test + public void givenNotSupportWithActiveTransaction_whenCallCreate_thenExpect0NewAnd1Suspend() { + transactionTemplate.execute(status -> { + Foo foo = new Foo("baeldung"); + return notSupportedTransactionalFooService.identity(foo); + }); + + PersistenceTransactionalTestConfig.TransactionSynchronizationAdapterSpy transactionSpy = PersistenceTransactionalTestConfig.getSpy(); + Assert.assertEquals(1, transactionSpy.getSuspend()); + Assert.assertEquals(1, transactionSpy.getCreate()); + } + + @Test(expected = IllegalTransactionStateException.class) + public void givenNeverWithActiveTransaction_whenCallCreate_thenExpectIllegalTransactionStateExceptionWith0NewAnd0Suspend() { + transactionTemplate.execute(status -> { + Foo foo = new Foo("baeldung"); + return neverTransactionalFooService.identity(foo); + }); + PersistenceTransactionalTestConfig.TransactionSynchronizationAdapterSpy transactionSpy = PersistenceTransactionalTestConfig.getSpy(); + Assert.assertEquals(0, transactionSpy.getSuspend()); + Assert.assertEquals(1, transactionSpy.getCreate()); + } + +} diff --git a/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/persistence/service/transactional/PersistenceTransactionalTestConfig.java b/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/persistence/service/transactional/PersistenceTransactionalTestConfig.java new file mode 100644 index 0000000000..fde1857ca2 --- /dev/null +++ b/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/persistence/service/transactional/PersistenceTransactionalTestConfig.java @@ -0,0 +1,148 @@ +package com.baeldung.persistence.service.transactional; + +import com.google.common.base.Preconditions; +import java.util.Properties; +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; +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.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.transaction.support.DefaultTransactionStatus; +import org.springframework.transaction.support.TransactionSynchronizationAdapter; +import org.springframework.transaction.support.TransactionSynchronizationManager; +import org.springframework.transaction.support.TransactionTemplate; + +@Configuration +@EnableTransactionManagement +@PropertySource({ "classpath:persistence-h2.properties" }) +@ComponentScan({ "com.baeldung.persistence","com.baeldung.jpa.dao" }) +@EnableJpaRepositories(basePackages = "com.baeldung.jpa.dao") +public class PersistenceTransactionalTestConfig { + + public static class TransactionSynchronizationAdapterSpy extends TransactionSynchronizationAdapter { + private int create, suspend; + + public int getSuspend() { + return suspend; + } + + public int getCreate() { + return create; + } + + public void create() { + create++; + } + + @Override + public void suspend() { + suspend++; + super.suspend(); + } + } + + + public static class JpaTransactionManagerSpy extends JpaTransactionManager { + @Override + protected void prepareSynchronization(DefaultTransactionStatus status, TransactionDefinition definition) { + super.prepareSynchronization(status, definition); + if (status.isNewTransaction()) { + if ( adapterSpyThreadLocal.get() == null ){ + TransactionSynchronizationAdapterSpy spy = new TransactionSynchronizationAdapterSpy(); + TransactionSynchronizationManager.registerSynchronization(spy); + adapterSpyThreadLocal.set(spy); + } + adapterSpyThreadLocal.get().create(); + } + } + } + + private static ThreadLocal adapterSpyThreadLocal = new ThreadLocal<>(); + + @Autowired + private Environment env; + + public PersistenceTransactionalTestConfig() { + super(); + } + + public static TransactionSynchronizationAdapterSpy getSpy(){ + if ( adapterSpyThreadLocal.get() == null ) + return new TransactionSynchronizationAdapterSpy(); + return adapterSpyThreadLocal.get(); + } + + public static void clearSpy(){ + adapterSpyThreadLocal.set(null); + } + + // beans + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() { + final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); + em.setDataSource(dataSource()); + em.setPackagesToScan(new String[] { "com.baeldung.persistence.model" }); + + final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); + em.setJpaVendorAdapter(vendorAdapter); + em.setJpaProperties(additionalProperties()); + + return em; + } + + @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 EntityManagerFactory emf) { + final JpaTransactionManagerSpy transactionManager = new JpaTransactionManagerSpy(); + transactionManager.setEntityManagerFactory(emf); + return transactionManager; + } + + @Bean + public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { + return new PersistenceExceptionTranslationPostProcessor(); + } + + final Properties additionalProperties() { + 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.cache.use_second_level_cache", "false"); + return hibernateProperties; + } + + + @Bean + public TransactionTemplate transactionTemplate(PlatformTransactionManager transactionManager){ + TransactionTemplate template = new TransactionTemplate(transactionManager); + template.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); + return template; + } + + +} \ No newline at end of file diff --git a/spring-cloud/pom.xml b/spring-cloud/pom.xml index baf86a4386..7138efc128 100644 --- a/spring-cloud/pom.xml +++ b/spring-cloud/pom.xml @@ -25,7 +25,7 @@ spring-cloud-zookeeper spring-cloud-gateway spring-cloud-stream - spring-cloud-stream-starters/twitterhdfs + spring-cloud-stream-starters/twitterhdfs spring-cloud-connectors-heroku spring-cloud-aws spring-cloud-consul @@ -35,9 +35,10 @@ spring-cloud-archaius spring-cloud-functions spring-cloud-vault - - spring-cloud-task + + spring-cloud-task spring-cloud-zuul + spring-cloud-zuul-fallback diff --git a/spring-cloud/spring-cloud-zuul-fallback/README.md b/spring-cloud/spring-cloud-zuul-fallback/README.md new file mode 100644 index 0000000000..2f13c1923b --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Fallback for Zuul Route](TODO) diff --git a/spring-cloud/spring-cloud-zuul-fallback/api-gateway/pom.xml b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/pom.xml new file mode 100644 index 0000000000..ee0f607d8a --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + api-gateway + api-gateway + API Gateway using Zuul + jar + + + com.baeldung.spring.cloud + spring-cloud-zuul-fallback + 1.0.0-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-netflix-zuul + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.cloud + spring-cloud-starter-parent + ${spring-cloud-dependencies.version} + pom + import + + + + diff --git a/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/java/com/baeldung/spring/cloud/apigateway/ApiGatewayApplication.java b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/java/com/baeldung/spring/cloud/apigateway/ApiGatewayApplication.java new file mode 100644 index 0000000000..78f489f2bb --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/java/com/baeldung/spring/cloud/apigateway/ApiGatewayApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.cloud.apigateway; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; + +@SpringBootApplication +@EnableZuulProxy +public class ApiGatewayApplication { + + public static void main(String[] args) { + SpringApplication.run(ApiGatewayApplication.class, args); + } + +} diff --git a/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/java/com/baeldung/spring/cloud/apigateway/fallback/GatewayClientResponse.java b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/java/com/baeldung/spring/cloud/apigateway/fallback/GatewayClientResponse.java new file mode 100644 index 0000000000..ce0c7819f1 --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/java/com/baeldung/spring/cloud/apigateway/fallback/GatewayClientResponse.java @@ -0,0 +1,69 @@ +package com.baeldung.spring.cloud.apigateway.fallback; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.client.ClientHttpResponse; + +public class GatewayClientResponse implements ClientHttpResponse { + + private HttpStatus status; + private String message; + + public GatewayClientResponse(HttpStatus status, String message) { + this.status = status; + this.message = message; + } + + @Override + public HttpStatus getStatusCode() throws IOException { + return status; + } + + @Override + public int getRawStatusCode() throws IOException { + return status.value(); + } + + @Override + public String getStatusText() throws IOException { + return status.getReasonPhrase(); + } + + @Override + public void close() { + } + + @Override + public InputStream getBody() throws IOException { + return new ByteArrayInputStream(message.getBytes()); + } + + @Override + public HttpHeaders getHeaders() { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + return headers; + } + + public HttpStatus getStatus() { + return status; + } + + public void setStatus(HttpStatus status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + +} diff --git a/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/java/com/baeldung/spring/cloud/apigateway/fallback/GatewayServiceFallback.java b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/java/com/baeldung/spring/cloud/apigateway/fallback/GatewayServiceFallback.java new file mode 100644 index 0000000000..73f72492c9 --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/java/com/baeldung/spring/cloud/apigateway/fallback/GatewayServiceFallback.java @@ -0,0 +1,29 @@ +package com.baeldung.spring.cloud.apigateway.fallback; + +import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider; +import org.springframework.http.HttpStatus; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.stereotype.Component; + +import com.netflix.hystrix.exception.HystrixTimeoutException; + +@Component +class GatewayServiceFallback implements FallbackProvider { + + private static final String DEFAULT_MESSAGE = "Service not available."; + + @Override + public String getRoute() { + return "*"; // or return null; + } + + @Override + public ClientHttpResponse fallbackResponse(String route, Throwable cause) { + if (cause instanceof HystrixTimeoutException) { + return new GatewayClientResponse(HttpStatus.GATEWAY_TIMEOUT, DEFAULT_MESSAGE); + } else { + return new GatewayClientResponse(HttpStatus.INTERNAL_SERVER_ERROR, DEFAULT_MESSAGE); + } + } + +} diff --git a/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/java/com/baeldung/spring/cloud/apigateway/fallback/WeatherServiceFallback.java b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/java/com/baeldung/spring/cloud/apigateway/fallback/WeatherServiceFallback.java new file mode 100644 index 0000000000..28fb6fb6e5 --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/java/com/baeldung/spring/cloud/apigateway/fallback/WeatherServiceFallback.java @@ -0,0 +1,29 @@ +package com.baeldung.spring.cloud.apigateway.fallback; + +import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider; +import org.springframework.http.HttpStatus; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.stereotype.Component; + +import com.netflix.hystrix.exception.HystrixTimeoutException; + +@Component +class WeatherServiceFallback implements FallbackProvider { + + private static final String DEFAULT_MESSAGE = "Weather information is not available."; + + @Override + public String getRoute() { + return "weather-service"; + } + + @Override + public ClientHttpResponse fallbackResponse(String route, Throwable cause) { + if (cause instanceof HystrixTimeoutException) { + return new GatewayClientResponse(HttpStatus.GATEWAY_TIMEOUT, DEFAULT_MESSAGE); + } else { + return new GatewayClientResponse(HttpStatus.INTERNAL_SERVER_ERROR, DEFAULT_MESSAGE); + } + } + +} diff --git a/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/resources/application.yml b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/resources/application.yml new file mode 100644 index 0000000000..4a0662a960 --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/main/resources/application.yml @@ -0,0 +1,22 @@ +spring: + application: + name: api-gateway +server: + port: 7070 + +zuul: + igoredServices: '*' + routes: + weather-service: + path: /weather/** + serviceId: weather-service + strip-prefix: false + +ribbon: + eureka: + enabled: false + +weather-service: + ribbon: + listOfServers: localhost:8080 + diff --git a/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/test/java/com/baeldung/spring/cloud/apigateway/ApiGatewayApplicationIntegrationTest.java b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/test/java/com/baeldung/spring/cloud/apigateway/ApiGatewayApplicationIntegrationTest.java new file mode 100644 index 0000000000..4052edc1f3 --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/test/java/com/baeldung/spring/cloud/apigateway/ApiGatewayApplicationIntegrationTest.java @@ -0,0 +1,16 @@ +package com.baeldung.spring.cloud.apigateway; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ApiGatewayApplicationIntegrationTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } + +} diff --git a/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/test/java/com/baeldung/spring/cloud/apigateway/fallback/GatewayServiceFallbackUnitTest.java b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/test/java/com/baeldung/spring/cloud/apigateway/fallback/GatewayServiceFallbackUnitTest.java new file mode 100644 index 0000000000..db2f703084 --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/test/java/com/baeldung/spring/cloud/apigateway/fallback/GatewayServiceFallbackUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.spring.cloud.apigateway.fallback; + +import static org.junit.Assert.assertEquals; + +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.http.HttpStatus; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.test.context.junit4.SpringRunner; + +import com.netflix.hystrix.exception.HystrixTimeoutException; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class GatewayServiceFallbackUnitTest { + + private static final String ROUTE = "*"; + + @Autowired + private GatewayServiceFallback fallback; + + @Test + public void testWhenGetRouteThenReturnWeatherServiceRoute() { + assertEquals(ROUTE, fallback.getRoute()); + + } + + @Test + public void testFallbackResponse_whenHystrixException_thenGatewayTimeout() throws Exception { + HystrixTimeoutException exception = new HystrixTimeoutException(); + ClientHttpResponse response = fallback.fallbackResponse(ROUTE, exception); + + assertEquals(HttpStatus.GATEWAY_TIMEOUT, response.getStatusCode()); + } + + @Test + public void testFallbackResponse_whenNonHystrixException_thenInternalServerError() throws Exception { + RuntimeException exception = new RuntimeException("Test exception"); + ClientHttpResponse response = fallback.fallbackResponse(ROUTE, exception); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + +} diff --git a/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/test/java/com/baeldung/spring/cloud/apigateway/fallback/WeatherServiceFallbackUnitTest.java b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/test/java/com/baeldung/spring/cloud/apigateway/fallback/WeatherServiceFallbackUnitTest.java new file mode 100644 index 0000000000..ccd59531b5 --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/api-gateway/src/test/java/com/baeldung/spring/cloud/apigateway/fallback/WeatherServiceFallbackUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.spring.cloud.apigateway.fallback; + +import static org.junit.Assert.assertEquals; + +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.http.HttpStatus; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.test.context.junit4.SpringRunner; + +import com.netflix.hystrix.exception.HystrixTimeoutException; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class WeatherServiceFallbackUnitTest { + + private static final String ROUTE = "weather-service"; + + @Autowired + private WeatherServiceFallback fallback; + + @Test + public void testWhenGetRouteThenReturnWeatherServiceRoute() { + assertEquals(ROUTE, fallback.getRoute()); + + } + + @Test + public void testFallbackResponse_whenHystrixException_thenGatewayTimeout() throws Exception { + HystrixTimeoutException exception = new HystrixTimeoutException(); + ClientHttpResponse response = fallback.fallbackResponse(ROUTE, exception); + + assertEquals(HttpStatus.GATEWAY_TIMEOUT, response.getStatusCode()); + } + + @Test + public void testFallbackResponse_whenNonHystrixException_thenInternalServerError() throws Exception { + RuntimeException exception = new RuntimeException("Test exception"); + ClientHttpResponse response = fallback.fallbackResponse(ROUTE, exception); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + +} diff --git a/spring-cloud/spring-cloud-zuul-fallback/pom.xml b/spring-cloud/spring-cloud-zuul-fallback/pom.xml new file mode 100644 index 0000000000..5bdd1d49ca --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + spring-cloud-zuul-fallback + pom + spring-cloud-zuul-fallback + Spring Cloud Zuul Fallback + + + com.baeldung.spring.cloud + spring-cloud + 1.0.0-SNAPSHOT + .. + + + + api-gateway + weather-service + + + + 1.8 + Finchley.SR2 + 3.1.1 + + diff --git a/spring-cloud/spring-cloud-zuul-fallback/weather-service/pom.xml b/spring-cloud/spring-cloud-zuul-fallback/weather-service/pom.xml new file mode 100644 index 0000000000..611f1dcd4f --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/weather-service/pom.xml @@ -0,0 +1,39 @@ + + 4.0.0 + weather-service + Weather Service + Weather Service for Zuul Fallback Test + + + com.baeldung.spring.cloud + spring-cloud-zuul-fallback + 1.0.0-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.cloud + spring-cloud-starter-parent + ${spring-cloud-dependencies.version} + pom + import + + + + diff --git a/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/main/java/com/baeldung/spring/cloud/weatherservice/WeatherController.java b/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/main/java/com/baeldung/spring/cloud/weatherservice/WeatherController.java new file mode 100644 index 0000000000..3cf451dcf7 --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/main/java/com/baeldung/spring/cloud/weatherservice/WeatherController.java @@ -0,0 +1,16 @@ +package com.baeldung.spring.cloud.weatherservice; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/weather") +public class WeatherController { + + @GetMapping("/today") + public String getMessage() { + return "It's a bright sunny day today!"; + } + +} diff --git a/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/main/java/com/baeldung/spring/cloud/weatherservice/WeatherServiceApplication.java b/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/main/java/com/baeldung/spring/cloud/weatherservice/WeatherServiceApplication.java new file mode 100644 index 0000000000..3a6d85e8d1 --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/main/java/com/baeldung/spring/cloud/weatherservice/WeatherServiceApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.spring.cloud.weatherservice; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class WeatherServiceApplication { + + public static void main(String[] args) { + SpringApplication.run(WeatherServiceApplication.class, args); + } + +} diff --git a/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/main/resources/application.yml b/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/main/resources/application.yml new file mode 100644 index 0000000000..242f842f44 --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/main/resources/application.yml @@ -0,0 +1,4 @@ +spring: + application: + name: weather-service + diff --git a/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/test/java/com/baeldung/spring/cloud/weatherservice/WeatherControllerIntegrationTest.java b/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/test/java/com/baeldung/spring/cloud/weatherservice/WeatherControllerIntegrationTest.java new file mode 100644 index 0000000000..3ffd2b0e7a --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/test/java/com/baeldung/spring/cloud/weatherservice/WeatherControllerIntegrationTest.java @@ -0,0 +1,31 @@ +package com.baeldung.spring.cloud.weatherservice; + +import static org.hamcrest.Matchers.containsString; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +@RunWith(SpringRunner.class) +@WebMvcTest(WeatherController.class) +public class WeatherControllerIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void whenWeatherControllerInvoked_thenReturnWeatherInformation() throws Exception { + this.mockMvc.perform(get("/weather/today")) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("bright sunny day"))); + } + +} diff --git a/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/test/java/com/baeldung/spring/cloud/weatherservice/WeatherServiceApplicationIntegrationTest.java b/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/test/java/com/baeldung/spring/cloud/weatherservice/WeatherServiceApplicationIntegrationTest.java new file mode 100644 index 0000000000..fec7ec1560 --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-fallback/weather-service/src/test/java/com/baeldung/spring/cloud/weatherservice/WeatherServiceApplicationIntegrationTest.java @@ -0,0 +1,16 @@ +package com.baeldung.spring.cloud.weatherservice; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class WeatherServiceApplicationIntegrationTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } + +} diff --git a/spring-integration/src/main/java/com/baeldung/dsl/JavaDSLFileCopyConfig.java b/spring-integration/src/main/java/com/baeldung/dsl/JavaDSLFileCopyConfig.java index 7e91345f04..e79eec3e83 100644 --- a/spring-integration/src/main/java/com/baeldung/dsl/JavaDSLFileCopyConfig.java +++ b/spring-integration/src/main/java/com/baeldung/dsl/JavaDSLFileCopyConfig.java @@ -16,8 +16,10 @@ import org.springframework.integration.core.MessageSource; import org.springframework.integration.dsl.IntegrationFlow; import org.springframework.integration.dsl.IntegrationFlows; import org.springframework.integration.dsl.Pollers; +import org.springframework.integration.dsl.channel.MessageChannels; import org.springframework.integration.file.FileReadingMessageSource; import org.springframework.integration.file.FileWritingMessageHandler; +import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHandler; /** @@ -104,9 +106,14 @@ public class JavaDSLFileCopyConfig { return handler; } + @Bean + public MessageChannel holdingTank() { + return MessageChannels.queue().get(); + } + // @Bean public IntegrationFlow fileReader() { - return IntegrationFlows.from(sourceDirectory()) + return IntegrationFlows.from(sourceDirectory(), configurer -> configurer.poller(Pollers.fixedDelay(10))) .filter(onlyJpgs()) .channel("holdingTank") .get(); diff --git a/spring-scheduling/README.md b/spring-scheduling/README.md index 72d5a7dc83..2e3bb2b5e5 100644 --- a/spring-scheduling/README.md +++ b/spring-scheduling/README.md @@ -1,5 +1,6 @@ ### Relevant articles: - [A Guide to the Spring Task Scheduler](http://www.baeldung.com/spring-task-scheduler) +- [The @Scheduled Annotation in Spring](https://www.baeldung.com/spring-scheduled-tasks) - [Guide to Spring Retry](http://www.baeldung.com/spring-retry) - [How To Do @Async in Spring](http://www.baeldung.com/spring-async) diff --git a/spring-scheduling/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java b/spring-scheduling/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java index 284bcf5e6a..23bbee3bc3 100644 --- a/spring-scheduling/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java +++ b/spring-scheduling/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java @@ -31,9 +31,10 @@ public class ScheduledAnnotationExample { System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000); } - @Scheduled(fixedDelay = 1000, initialDelay = 100) + @Scheduled(fixedDelay = 1000, initialDelay = 1000) public void scheduleFixedRateWithInitialDelayTask() { - System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000); + long now = System.currentTimeMillis() / 1000; + System.out.println("Fixed rate task with one second initial delay - " + now); } /** @@ -41,7 +42,8 @@ public class ScheduledAnnotationExample { */ @Scheduled(cron = "0 15 10 15 * ?") public void scheduleTaskUsingCronExpression() { - System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000); + long now = System.currentTimeMillis() / 1000; + System.out.println("schedule tasks using cron jobs - " + now); } @Scheduled(cron = "${cron.expression}") diff --git a/spring-scheduling/src/main/resources/springScheduled-config.xml b/spring-scheduling/src/main/resources/springScheduled-config.xml index d1ff9dc028..4078f535da 100644 --- a/spring-scheduling/src/main/resources/springScheduled-config.xml +++ b/spring-scheduling/src/main/resources/springScheduled-config.xml @@ -15,7 +15,7 @@ - + diff --git a/spring-security-mvc/src/main/java/com/baeldung/session/bean/Constants.java b/spring-security-mvc/src/main/java/com/baeldung/session/bean/Constants.java new file mode 100644 index 0000000000..bf204c3b99 --- /dev/null +++ b/spring-security-mvc/src/main/java/com/baeldung/session/bean/Constants.java @@ -0,0 +1,5 @@ +package com.baeldung.session.bean; + +public class Constants { + public static final String FOO = "foo"; +} diff --git a/spring-security-mvc/src/main/java/com/baeldung/session/bean/Foo.java b/spring-security-mvc/src/main/java/com/baeldung/session/bean/Foo.java new file mode 100644 index 0000000000..c9c9c011d4 --- /dev/null +++ b/spring-security-mvc/src/main/java/com/baeldung/session/bean/Foo.java @@ -0,0 +1,29 @@ +package com.baeldung.session.bean; + +import static org.springframework.context.annotation.ScopedProxyMode.TARGET_CLASS; +import static org.springframework.web.context.WebApplicationContext.SCOPE_SESSION; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +@Component +@Scope(value = SCOPE_SESSION, proxyMode = TARGET_CLASS) +public class Foo { + private final String created; + + public Foo() { + this.created = LocalDateTime.now() + .format(DateTimeFormatter.ISO_DATE_TIME); + } + + public Foo(Foo theFoo) { + this.created = theFoo.created; + } + + public String getCreated() { + return created; + } +} diff --git a/spring-security-mvc/src/main/java/com/baeldung/session/security/config/SecSecurityConfig.java b/spring-security-mvc/src/main/java/com/baeldung/session/security/config/SecSecurityConfig.java index 35b53a0e7f..9a4978c27e 100644 --- a/spring-security-mvc/src/main/java/com/baeldung/session/security/config/SecSecurityConfig.java +++ b/spring-security-mvc/src/main/java/com/baeldung/session/security/config/SecSecurityConfig.java @@ -38,7 +38,7 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter { .csrf().disable() .authorizeRequests() .antMatchers("/anonymous*").anonymous() - .antMatchers("/login*","/invalidSession*", "/sessionExpired*").permitAll() + .antMatchers("/login*","/invalidSession*", "/sessionExpired*", "/foo/**").permitAll() .anyRequest().authenticated() .and() .formLogin() diff --git a/spring-security-mvc/src/main/java/com/baeldung/session/web/FooController.java b/spring-security-mvc/src/main/java/com/baeldung/session/web/FooController.java new file mode 100644 index 0000000000..7c3385dcbd --- /dev/null +++ b/spring-security-mvc/src/main/java/com/baeldung/session/web/FooController.java @@ -0,0 +1,44 @@ +package com.baeldung.session.web; + +import javax.servlet.http.HttpSession; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import com.baeldung.session.bean.Constants; +import com.baeldung.session.bean.Foo; + +@RestController +@RequestMapping(path = "/foo") +public class FooController { + + @Autowired + private Foo theFoo; + + @GetMapping(path = "set") + public void fooSet(HttpSession session) { + session.setAttribute(Constants.FOO, new Foo()); + } + + @GetMapping(path = "autowired") + public Foo getAutowired() { + return new Foo(theFoo); + } + + @GetMapping(path = "inject") + public Foo fooInject(HttpSession session) { + return (Foo) session.getAttribute(Constants.FOO); + } + + @GetMapping(path = "raw") + public Foo fromRaw() { + ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); + HttpSession session = attr.getRequest() + .getSession(true); + return (Foo) session.getAttribute(Constants.FOO); + } +} diff --git a/spring-security-mvc/src/main/java/com/baeldung/session/web/SessionRestController.java b/spring-security-mvc/src/main/java/com/baeldung/session/web/SessionRestController.java index 82199a9e4e..79f57246a9 100644 --- a/spring-security-mvc/src/main/java/com/baeldung/session/web/SessionRestController.java +++ b/spring-security-mvc/src/main/java/com/baeldung/session/web/SessionRestController.java @@ -3,15 +3,13 @@ package com.baeldung.session.web; import javax.servlet.http.HttpSession; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class SessionRestController { @GetMapping("/session-max-interval") - @ResponseBody - public String retrieveMaxSessionIncativeInterval(HttpSession session) { + public String retrieveMaxSessionInactiveInterval(HttpSession session) { return "Max Inactive Interval before Session expires: " + session.getMaxInactiveInterval(); } } diff --git a/spring-security-mvc/src/main/resources/application.properties b/spring-security-mvc/src/main/resources/application.properties index 56b2b7b123..6f0d0519ef 100644 --- a/spring-security-mvc/src/main/resources/application.properties +++ b/spring-security-mvc/src/main/resources/application.properties @@ -1,3 +1,5 @@ +spring.jackson.serialization.fail-on-empty-beans=false + server.servlet.session.timeout=65s spring.mvc.view.prefix=/WEB-INF/view/ diff --git a/spring-security-mvc/src/main/resources/webSecurityConfig.xml b/spring-security-mvc/src/main/resources/webSecurityConfig.xml index 42ff4c2186..e91755d394 100644 --- a/spring-security-mvc/src/main/resources/webSecurityConfig.xml +++ b/spring-security-mvc/src/main/resources/webSecurityConfig.xml @@ -7,7 +7,7 @@ http://www.springframework.org/schema/beans/spring-beans.xsd" > - + @@ -22,10 +22,9 @@ - - + diff --git a/spring-security-mvc/src/main/webapp/WEB-INF/web.xml b/spring-security-mvc/src/main/webapp/WEB-INF/web.xml index 2ef734441b..88087c92ed 100644 --- a/spring-security-mvc/src/main/webapp/WEB-INF/web.xml +++ b/spring-security-mvc/src/main/webapp/WEB-INF/web.xml @@ -8,13 +8,14 @@ 1 + COOKIE - org.baeldung.web.SessionListenerWithMetrics + com.baeldung.web.SessionListenerWithMetrics 2.0.0.0 3.1.6 - 5.4.0 + 5.5.0 + 1.5.2 5.1.4.RELEASE 4.0.1 2.1.1 diff --git a/testing-modules/spring-testing/src/test/java/com/baeldung/dirtiescontext/DirtiesContextIntegrationTest.java b/testing-modules/spring-testing/src/test/java/com/baeldung/dirtiescontext/DirtiesContextIntegrationTest.java index f3e7b8c228..7afd4a22d4 100644 --- a/testing-modules/spring-testing/src/test/java/com/baeldung/dirtiescontext/DirtiesContextIntegrationTest.java +++ b/testing-modules/spring-testing/src/test/java/com/baeldung/dirtiescontext/DirtiesContextIntegrationTest.java @@ -10,10 +10,12 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext.MethodMode; import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; @TestMethodOrder(OrderAnnotation.class) @ExtendWith(SpringExtension.class) @SpringBootTest(classes = SpringDataRestApplication.class) +@EnableWebMvc class DirtiesContextIntegrationTest { @Autowired diff --git a/testing-modules/spring-testing/src/test/java/com/baeldung/overrideproperties/ProfilePropertySourceResolverIntegrationTest.java b/testing-modules/spring-testing/src/test/java/com/baeldung/overrideproperties/ProfilePropertySourceResolverIntegrationTest.java index 95d83420b7..815b628f0a 100644 --- a/testing-modules/spring-testing/src/test/java/com/baeldung/overrideproperties/ProfilePropertySourceResolverIntegrationTest.java +++ b/testing-modules/spring-testing/src/test/java/com/baeldung/overrideproperties/ProfilePropertySourceResolverIntegrationTest.java @@ -1,21 +1,25 @@ package com.baeldung.overrideproperties; -import com.baeldung.overrideproperties.resolver.PropertySourceResolver; +import static org.junit.Assert.assertEquals; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import static org.junit.Assert.assertEquals; +import com.baeldung.overrideproperties.resolver.PropertySourceResolver; @RunWith(SpringRunner.class) @SpringBootTest @ActiveProfiles("test") +@EnableWebMvc public class ProfilePropertySourceResolverIntegrationTest { - @Autowired private PropertySourceResolver propertySourceResolver; + @Autowired + private PropertySourceResolver propertySourceResolver; @Test public void shouldProfiledProperty_overridePropertyValues() { diff --git a/testing-modules/spring-testing/src/test/java/com/baeldung/overrideproperties/SpringBootPropertySourceResolverIntegrationTest.java b/testing-modules/spring-testing/src/test/java/com/baeldung/overrideproperties/SpringBootPropertySourceResolverIntegrationTest.java index 573a46dd5f..d00aa51e6c 100644 --- a/testing-modules/spring-testing/src/test/java/com/baeldung/overrideproperties/SpringBootPropertySourceResolverIntegrationTest.java +++ b/testing-modules/spring-testing/src/test/java/com/baeldung/overrideproperties/SpringBootPropertySourceResolverIntegrationTest.java @@ -1,18 +1,22 @@ package com.baeldung.overrideproperties; -import com.baeldung.overrideproperties.resolver.PropertySourceResolver; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +import com.baeldung.overrideproperties.resolver.PropertySourceResolver; @RunWith(SpringRunner.class) @SpringBootTest(properties = { "example.firstProperty=annotation" }) +@EnableWebMvc public class SpringBootPropertySourceResolverIntegrationTest { - @Autowired private PropertySourceResolver propertySourceResolver; + @Autowired + private PropertySourceResolver propertySourceResolver; @Test public void shouldSpringBootTestAnnotation_overridePropertyValues() { diff --git a/testing-modules/spring-testing/src/test/java/com/baeldung/overrideproperties/TestResourcePropertySourceResolverIntegrationTest.java b/testing-modules/spring-testing/src/test/java/com/baeldung/overrideproperties/TestResourcePropertySourceResolverIntegrationTest.java index c724713854..dc15851277 100644 --- a/testing-modules/spring-testing/src/test/java/com/baeldung/overrideproperties/TestResourcePropertySourceResolverIntegrationTest.java +++ b/testing-modules/spring-testing/src/test/java/com/baeldung/overrideproperties/TestResourcePropertySourceResolverIntegrationTest.java @@ -1,19 +1,23 @@ package com.baeldung.overrideproperties; -import com.baeldung.overrideproperties.resolver.PropertySourceResolver; +import static org.junit.Assert.assertEquals; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import static org.junit.Assert.assertEquals; +import com.baeldung.overrideproperties.resolver.PropertySourceResolver; @RunWith(SpringRunner.class) @SpringBootTest +@EnableWebMvc public class TestResourcePropertySourceResolverIntegrationTest { - @Autowired private PropertySourceResolver propertySourceResolver; + @Autowired + private PropertySourceResolver propertySourceResolver; @Test public void shouldTestResourceFile_overridePropertyValues() {