diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/value_based_class/Point.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/value_based_class/Point.java new file mode 100644 index 0000000000..13e2238274 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/value_based_class/Point.java @@ -0,0 +1,49 @@ +package com.baeldung.value_based_class; + +import java.util.Objects; + +import jdk.internal.ValueBased; + +/** + * This class is written with the intention that it can serve as an example of + * what a Value-based class could be. + */ + +@ValueBased +public final class Point { + private final int x; + private final int y; + private final int z; + + private static Point ORIGIN = new Point(0, 0, 0); + + private Point(int x, int y, int z) { + this.x = x; + this.y = y; + this.z = z; + } + + public static Point valueOfPoint(int x, int y, int z) { + // returns a cached instance if it is origin, or a new instance + if (isOrigin(x, y, z)) + return ORIGIN; + return new Point(x, y, z); + } + + @Override + public boolean equals(Object other) { + if (other == null || getClass() != other.getClass()) + return false; + Point point = (Point) other; + return x == point.x && y == point.y && z == point.z; + } + + @Override + public int hashCode() { + return Objects.hash(x, y, z); + } + + private static boolean isOrigin(int x, int y, int z) { + return x == 0 && y == 0 && z == 0; + } +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/value_based_class/ValueBasedClassUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/value_based_class/ValueBasedClassUnitTest.java new file mode 100644 index 0000000000..781f368982 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/value_based_class/ValueBasedClassUnitTest.java @@ -0,0 +1,41 @@ +package com.baeldung.value_based_class; + +import org.junit.Assert; +import org.junit.Test; + +public class ValueBasedClassUnitTest { + @Test + public void givenAutoboxedAndPrimitive_whenCompared_thenReturnEquals() { + int primitive_a = 125; + Integer obj_a = 125; // this is autoboxed + Assert.assertSame(primitive_a, obj_a); + } + + @Test + public void givenValueBasedPoint_whenCreated_thenReturnsObjects() { + Point p1 = Point.valueOfPoint(1, 2, 3); + Point p2 = Point.valueOfPoint(2, 3, 4); + + Assert.assertNotEquals(p1, p2); + } + + @Test + public void givenValueBasedPoint_whenCompared_thenReturnEquals() { + Point p1 = Point.valueOfPoint(1, 2, 3); + Point p2 = Point.valueOfPoint(1, 2, 3); + + Assert.assertEquals(p1, p2); + } + + @Test + public void givenValueBasedPoint_whenOrigin_thenReturnCachedInstance() { + Point p1 = Point.valueOfPoint(0, 0, 0); + Point p2 = Point.valueOfPoint(0, 0, 0); + Point p3 = Point.valueOfPoint(1, 2, 3); + + // the following should not be assumed for value-based classes + + Assert.assertTrue(p1 == p2); + Assert.assertFalse(p1 == p3); + } +} diff --git a/core-java-modules/core-java-8-datetime-2/src/test/java/com/baeldung/stringtime/AddMinuteStringTimeUnitTest.java b/core-java-modules/core-java-8-datetime-2/src/test/java/com/baeldung/stringtime/AddMinuteStringTimeUnitTest.java new file mode 100644 index 0000000000..15b430cc6a --- /dev/null +++ b/core-java-modules/core-java-8-datetime-2/src/test/java/com/baeldung/stringtime/AddMinuteStringTimeUnitTest.java @@ -0,0 +1,37 @@ +package com.baeldung.stringtime; + +import static org.junit.jupiter.api.Assertions.*; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalTime; +import java.util.Calendar; +import java.util.Date; +import org.junit.jupiter.api.Test; + +public class AddMinuteStringTimeUnitTest { + + @Test + public void givenTimeStringUsingSimpleDateFormat_whenIncrementedWith10Minutes_thenResultShouldBeCorrect() throws ParseException { + String timeString = "23:45"; + SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); + Date date = timeFormat.parse(timeString); + + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + cal.add(Calendar.MINUTE, 10); + String result = timeFormat.format(cal.getTime()); + assertEquals("23:55", result); + + } + + @Test + public void givenTimeStringUsingLocalTime_whenIncrementedWith10Minutes_thenResultShouldBeCorrect() { + String timeString = "23:45"; + LocalTime time = LocalTime.parse(timeString); + LocalTime newTime = time.plusMinutes(10); + String result = newTime.toString(); + assertEquals("23:55", result); + } + +} diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/NonStableSortExample.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/NonStableSortExample.java new file mode 100644 index 0000000000..cd754489a6 --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/NonStableSortExample.java @@ -0,0 +1,26 @@ +package com.baeldung.collectionsvsarrays; + +import com.baeldung.collectionsvsarrays.sorting.Quicksort; +import java.util.Comparator; +import java.util.List; + +public class NonStableSortExample { + + + + public static void main(String[] args) { + List tasks = Tasks.supplier.get(); + Quicksort.sort(tasks, Comparator.comparingInt(Task::getPriority)); + System.out.println("After sorting by priority:"); + for (Task task : tasks) { + System.out.println(task); + } + Quicksort.sort(tasks, Comparator.comparing(Task::getDueDate)); + System.out.println("\nAfter sorting by due date:"); + for (Task task : tasks) { + System.out.println(task); + } + } + + +} diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/ObjectOverheadBenchmark.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/ObjectOverheadBenchmark.java new file mode 100644 index 0000000000..9a8adff507 --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/ObjectOverheadBenchmark.java @@ -0,0 +1,56 @@ +package com.baeldung.collectionsvsarrays; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; + +@Measurement(iterations = 2, time = 10, timeUnit = TimeUnit.MINUTES) +@Warmup(iterations = 5, time = 10) +@Fork(value = 2) +public class ObjectOverheadBenchmark { + + private static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current(); + + @State(Scope.Benchmark) + public static class Input { + public Supplier> randomNumbers = () -> RANDOM.ints().limit(10000).boxed().collect(Collectors.toList()); + } + + @Benchmark + @BenchmarkMode(Mode.Throughput) + public void sortingPrimitiveArray(Input input, Blackhole blackhole) { + final int[] array = input.randomNumbers.get().stream().mapToInt(Integer::intValue).toArray(); + Arrays.sort(array); + final List result = Arrays.stream(array).boxed().collect(Collectors.toList()); + blackhole.consume(result); + } + + @Benchmark + @BenchmarkMode(Mode.Throughput) + public void sortingObjectArray(Input input, Blackhole blackhole) { + final Integer[] array = input.randomNumbers.get().toArray(new Integer[0]); + Arrays.sort(array); + blackhole.consume(array); + } + + @Benchmark + @BenchmarkMode(Mode.Throughput) + public void sortingObjects(Input input, Blackhole blackhole) { + final List list = input.randomNumbers.get(); + Collections.sort(list); + blackhole.consume(list); + } +} diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/PerformanceBenchmark.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/PerformanceBenchmark.java new file mode 100644 index 0000000000..6e645b0fb9 --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/PerformanceBenchmark.java @@ -0,0 +1,51 @@ +package com.baeldung.collectionsvsarrays; + +import com.baeldung.collectionsvsarrays.sorting.MergeSort; +import com.baeldung.collectionsvsarrays.sorting.Quicksort; +import java.util.Random; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; +import java.util.stream.IntStream; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.Warmup; + +@Measurement(iterations = 2, time = 10, timeUnit = TimeUnit.MINUTES) +@Warmup(iterations = 5, time = 10) +public class PerformanceBenchmark { + + private static final Random RANDOM = new Random(); + private static final int ARRAY_SIZE = 10000; + private static final int[] randomNumbers = RANDOM.ints(ARRAY_SIZE).toArray(); + private static final int[] sameNumbers = IntStream.generate(() -> 42).limit(ARRAY_SIZE).toArray(); + public static final Supplier randomNumbersSupplier = randomNumbers::clone; + public static final Supplier sameNumbersSupplier = sameNumbers::clone; + + @Benchmark + @BenchmarkMode(Mode.Throughput) + @Fork(value = 1, jvmArgs = {"-Xlog:gc:file=gc-logs-quick-sort-same-number-%t.txt,filesize=900m -Xmx6gb -Xms6gb"}) + public void quickSortSameNumber() { + Quicksort.sort(sameNumbersSupplier.get()); + } + @Benchmark + @BenchmarkMode(Mode.Throughput) + @Fork(value = 1, jvmArgs = {"-Xlog:gc:file=gc-logs-quick-sort-random-number-%t.txt,filesize=900m -Xmx6gb -Xms6gb"}) + public void quickSortRandomNumber() { + Quicksort.sort(randomNumbersSupplier.get()); + } + @Benchmark + @BenchmarkMode(Mode.Throughput) + @Fork(value = 1, jvmArgs = {"-Xlog:gc:file=gc-logs-merge-sort-same-number-%t.txt,filesize=900m -Xmx6gb -Xms6gb"}) + public void mergeSortSameNumber() { + MergeSort.sort(sameNumbersSupplier.get()); + } + @Benchmark + @BenchmarkMode(Mode.Throughput) + @Fork(value = 1, jvmArgs = {"-Xlog:gc:file=gc-logs-merge-sort-random-number-%t.txt,filesize=900m -Xmx6gb -Xms6gb"}) + public void mergeSortRandomNumber() { + MergeSort.sort(randomNumbersSupplier.get()); + } +} diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/StableSortExample.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/StableSortExample.java new file mode 100644 index 0000000000..336673bac8 --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/StableSortExample.java @@ -0,0 +1,23 @@ +package com.baeldung.collectionsvsarrays; + + +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class StableSortExample { + + public static void main(String[] args) { + final List tasks = Tasks.supplier.get(); + Collections.sort(tasks, Comparator.comparingInt(Task::getPriority)); + System.out.println("After sorting by priority:"); + for (Task task : tasks) { + System.out.println(task); + } + Collections.sort(tasks, Comparator.comparing(Task::getDueDate)); + System.out.println("\nAfter sorting by due date:"); + for (Task task : tasks) { + System.out.println(task); + } + } +} diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/Task.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/Task.java new file mode 100644 index 0000000000..133c39ccd2 --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/Task.java @@ -0,0 +1,30 @@ +package com.baeldung.collectionsvsarrays; + +public class Task { + private final long id; + private final int priority; + private final String dueDate; + + public Task(final long id, int priority, String dueDate) { + this.id = id; + this.priority = priority; + this.dueDate = dueDate; + } + + @Override + public String toString() { + return String.format("Task: #%-2d | Priority: %d | Due Date: %s", getId(), getPriority(), getDueDate()); + } + + public int getPriority() { + return priority; + } + + public String getDueDate() { + return dueDate; + } + + private long getId() { + return id; + } +} diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/Tasks.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/Tasks.java new file mode 100644 index 0000000000..fc413f549f --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/Tasks.java @@ -0,0 +1,30 @@ +package com.baeldung.collectionsvsarrays; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Supplier; + +public class Tasks { + + private static final List tasks; + public static final Supplier> supplier; + + static { + tasks = new ArrayList<>(); + tasks.add(new Task(1, 1, "2023-09-01")); + tasks.add(new Task(2, 2, "2023-08-30")); + tasks.add(new Task(3, 1, "2023-08-29")); + tasks.add(new Task(4, 2, "2023-09-02")); + tasks.add(new Task(5, 3, "2023-09-05")); + tasks.add(new Task(6, 1, "2023-09-03")); + tasks.add(new Task(7, 3, "2023-08-28")); + tasks.add(new Task(8, 2, "2023-09-01")); + tasks.add(new Task(9, 1, "2023-08-28")); + tasks.add(new Task(10, 2, "2023-09-04")); + tasks.add(new Task(11, 3, "2023-08-31")); + tasks.add(new Task(12, 1, "2023-08-30")); + tasks.add(new Task(13, 3, "2023-09-02")); + + supplier = () -> new ArrayList<>(tasks); + } +} diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/sorting/MergeSort.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/sorting/MergeSort.java new file mode 100644 index 0000000000..6078db50e0 --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/sorting/MergeSort.java @@ -0,0 +1,47 @@ +package com.baeldung.collectionsvsarrays.sorting; + +public class MergeSort { + + public static void sort(int[] a) { + sort(a, a.length); + } + public static void sort(int[] a, int n) { + if (n < 2) { + return; + } + int mid = n / 2; + int[] l = new int[mid]; + int[] r = new int[n - mid]; + + for (int i = 0; i < mid; i++) { + l[i] = a[i]; + } + for (int i = mid; i < n; i++) { + r[i - mid] = a[i]; + } + sort(l, mid); + sort(r, n - mid); + + merge(a, l, r, mid, n - mid); + } + private static void merge( + int[] a, int[] l, int[] r, int left, int right) { + + int i = 0, j = 0, k = 0; + while (i < left && j < right) { + if (l[i] <= r[j]) { + a[k++] = l[i++]; + } + else { + a[k++] = r[j++]; + } + } + while (i < left) { + a[k++] = l[i++]; + } + while (j < right) { + a[k++] = r[j++]; + } + } + +} diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/sorting/Quicksort.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/sorting/Quicksort.java new file mode 100644 index 0000000000..eab47b901b --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/collectionsvsarrays/sorting/Quicksort.java @@ -0,0 +1,72 @@ +package com.baeldung.collectionsvsarrays.sorting; + +import java.util.Comparator; +import java.util.List; + +public class Quicksort { + + public static void sort(int arr[]) { + sort(arr, 0, arr.length - 1); + } + public static void sort(int arr[], int begin, int end) { + if (begin < end) { + int partitionIndex = partition(arr, begin, end); + + sort(arr, begin, partitionIndex-1); + sort(arr, partitionIndex+1, end); + } + } + private static int partition(int arr[], int begin, int end) { + int pivot = arr[end]; + int i = (begin-1); + + for (int j = begin; j < end; j++) { + if (arr[j] <= pivot) { + i++; + + int swapTemp = arr[i]; + arr[i] = arr[j]; + arr[j] = swapTemp; + } + } + + int swapTemp = arr[i+1]; + arr[i+1] = arr[end]; + arr[end] = swapTemp; + + return i+1; + } + + public static void sort(List list, Comparator comparator) { + sort(list, 0, list.size() - 1, comparator); + } + public static void sort(List list, int low, int high, Comparator comparator) { + if (low < high) { + int partitionIndex = partition(list, low, high, comparator); + + sort(list, low, partitionIndex - 1, comparator); + sort(list, partitionIndex + 1, high, comparator); + } + } + + private static int partition(List list, int begin, int end, Comparator comparator) { + T pivot = list.get(end); + int i = (begin-1); + + for (int j = begin; j < end; j++) { + if (comparator.compare(list.get(j), pivot) <= 0) { + i++; + + T swapTemp = list.get(i); + list.set(i, list.get(j)); + list.set(j, swapTemp); + } + } + + T swapTemp = list.get(i+1); + list.set(i+1,list.get(end)); + list.set(end, swapTemp); + + return i+1; + } +} diff --git a/core-java-modules/core-java-io/README.md b/core-java-modules/core-java-io/README.md index f6b7787960..aed0ff41c2 100644 --- a/core-java-modules/core-java-io/README.md +++ b/core-java-modules/core-java-io/README.md @@ -12,4 +12,5 @@ This module contains articles about core Java input and output (IO) - [Getting a File’s Mime Type in Java](https://www.baeldung.com/java-file-mime-type) - [How to Avoid the Java FileNotFoundException When Loading Resources](https://www.baeldung.com/java-classpath-resource-cannot-be-opened) - [Java – Rename or Move a File](https://www.baeldung.com/java-how-to-rename-or-move-a-file) +- [Closing Java IO Streams](https://www.baeldung.com/java-io-streams-closing) - [[More -->]](/core-java-modules/core-java-io-2) diff --git a/core-java-modules/core-java-numbers-6/README.md b/core-java-modules/core-java-numbers-6/README.md index 67f574652c..254b2b58d7 100644 --- a/core-java-modules/core-java-numbers-6/README.md +++ b/core-java-modules/core-java-numbers-6/README.md @@ -3,4 +3,5 @@ - [Convert Integer to Hexadecimal in Java](https://www.baeldung.com/java-convert-int-to-hex) - [Integer.class vs Integer.TYPE vs int.class](https://www.baeldung.com/java-integer-class-vs-type-vs-int) - [Does Java Read Integers in Little Endian or Big Endian?](https://www.baeldung.com/java-integers-little-big-endian) +- [How to Split an Integer Number Into Digits in Java](https://www.baeldung.com/java-integer-individual-digits) - More articles: [[<-- prev]](../core-java-numbers-5) diff --git a/core-java-modules/core-java-perf-2/README.md b/core-java-modules/core-java-perf-2/README.md index 8718060645..aea10fa69c 100644 --- a/core-java-modules/core-java-perf-2/README.md +++ b/core-java-modules/core-java-perf-2/README.md @@ -5,3 +5,4 @@ This module contains articles about performance of Java applications ### Relevant Articles: - [External Debugging With JMXTerm](https://www.baeldung.com/java-jmxterm-external-debugging) - [Create and Detect Memory Leaks in Java](https://www.baeldung.com/java-create-detect-memory-leaks) +- [Differences Between Heap Dump, Thread Dump and Core Dump](https://www.baeldung.com/java-heap-thread-core-dumps) diff --git a/core-java-modules/core-java-reflection-2/README.md b/core-java-modules/core-java-reflection-2/README.md index 4918b1fe98..dfd6bf28e6 100644 --- a/core-java-modules/core-java-reflection-2/README.md +++ b/core-java-modules/core-java-reflection-2/README.md @@ -9,3 +9,4 @@ - [Invoke a Static Method Using Java Reflection API](https://www.baeldung.com/java-invoke-static-method-reflection) - [What Is the JDK com.sun.proxy.$Proxy Class?](https://www.baeldung.com/jdk-com-sun-proxy) - [Unit Test Private Methods in Java](https://www.baeldung.com/java-unit-test-private-methods) +- [Constructing Java Objects From Only the Class Name](https://www.baeldung.com/java-objects-make-using-class-name) diff --git a/core-java-modules/core-java-security-4/README.md b/core-java-modules/core-java-security-4/README.md index edb29167ef..236715713b 100644 --- a/core-java-modules/core-java-security-4/README.md +++ b/core-java-modules/core-java-security-4/README.md @@ -4,4 +4,5 @@ This module contains articles about core Java Security ### Relevant Articles: - [Check if Certificate Is Self-Signed or CA-Signed With Java](https://www.baeldung.com/java-check-certificate-sign) +- [Extract CN From X509 Certificate in Java](https://www.baeldung.com/java-extract-common-name-x509-certificate) - More articles: [[<-- prev]](/core-java-modules/core-java-security-3) diff --git a/core-java-modules/core-java-string-algorithms-3/pom.xml b/core-java-modules/core-java-string-algorithms-3/pom.xml index 7d4adeba92..536175acc2 100644 --- a/core-java-modules/core-java-string-algorithms-3/pom.xml +++ b/core-java-modules/core-java-string-algorithms-3/pom.xml @@ -23,6 +23,11 @@ commons-lang3 ${apache-commons-lang3.version} + + com.vdurmont + emoji-java + ${emoji-java.version} + @@ -52,6 +57,7 @@ 11 1.7 3.12.0 + 5.1.1 \ No newline at end of file diff --git a/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/findemojis/FindEmojisUnitTest.java b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/findemojis/FindEmojisUnitTest.java new file mode 100644 index 0000000000..3cd5b1ba97 --- /dev/null +++ b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/findemojis/FindEmojisUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.findemojis; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.vdurmont.emoji.EmojiManager; + +public class FindEmojisUnitTest { + + @Test + public void givenAWord_whenUsingEmojiJava_thenDetectEmoji() { + boolean emoji = EmojiManager.isEmoji("\uD83D\uDC3B"); + assertTrue(emoji); + + boolean notEmoji = EmojiManager.isEmoji("w"); + assertFalse(notEmoji); + } + + @Test + public void givenAWord_whenUsingRegex_thenDetectEmoji() { + String regexPattern = "[\uD800-\uDBFF\uDC00-\uDFFF]+"; + String emojiString = "\uD83D\uDC3B"; + boolean emoji = emojiString.matches(regexPattern); + assertTrue(emoji); + + String notEmojiString = "w"; + boolean notEmoji = notEmojiString.matches(regexPattern); + assertFalse(notEmoji); + } + +} diff --git a/gradle-modules/gradle-5/build.gradle b/gradle-modules/gradle-5/build.gradle index 84cf05bad6..bc3f233f99 100644 --- a/gradle-modules/gradle-5/build.gradle +++ b/gradle-modules/gradle-5/build.gradle @@ -1,14 +1,21 @@ -plugins{ - id "nebula.lint" version "16.9.0" +plugins { + id "nebula.lint" version "18.1.0" } + description = "Gradle 5 root project" allprojects { - apply plugin :"java" - apply plugin :"nebula.lint" + apply plugin: "java" + apply plugin: "nebula.lint" gradleLint { - rules=['unused-dependency'] + rules = [ + // 'unused-dependency', + // 'dependency-parentheses', + // 'undeclared-dependency', + // 'minimum-dependency-version' + ] reportFormat = 'text' + reportOnlyFixableViolations = true } group = "com.baeldung" version = "0.0.1" @@ -16,6 +23,6 @@ allprojects { targetCompatibility = "1.8" repositories { - jcenter() + mavenCentral() } } \ No newline at end of file diff --git a/gradle-modules/gradle-5/gradle-lint-intro.gradle b/gradle-modules/gradle-5/gradle-lint-intro.gradle new file mode 100644 index 0000000000..4b919c21e8 --- /dev/null +++ b/gradle-modules/gradle-5/gradle-lint-intro.gradle @@ -0,0 +1,6 @@ +allprojects { + apply plugin: "nebula.lint" + gradleLint { + rules = ['dependency-parenthesis'] + } +} \ No newline at end of file diff --git a/gradle-modules/gradle-5/gradle-lint-intro/.gitignore b/gradle-modules/gradle-5/gradle-lint-intro/.gitignore new file mode 100644 index 0000000000..84c048a73c --- /dev/null +++ b/gradle-modules/gradle-5/gradle-lint-intro/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/gradle-modules/gradle-5/gradle-lint-intro/build.gradle b/gradle-modules/gradle-5/gradle-lint-intro/build.gradle new file mode 100644 index 0000000000..fab83bfeec --- /dev/null +++ b/gradle-modules/gradle-5/gradle-lint-intro/build.gradle @@ -0,0 +1,15 @@ + +description = "Introduction to Gradle Lint Plugin" + +ext { + awsVersion = '2.20.83' +} + + +dependencies { + implementation platform("software.amazon.awssdk:bom:$awsVersion") + testImplementation('junit:junit:4.13.1') + gradleLint.ignore('unused-dependency', 'dependency-parentheses') { + implementation('software.amazon.awssdk:sts') + } +} diff --git a/gradle-modules/gradle-5/gradle/wrapper/gradle-wrapper.properties b/gradle-modules/gradle-5/gradle/wrapper/gradle-wrapper.properties index 4c46317507..7666e22b54 100644 --- a/gradle-modules/gradle-5/gradle/wrapper/gradle-wrapper.properties +++ b/gradle-modules/gradle-5/gradle/wrapper/gradle-wrapper.properties @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip diff --git a/gradle-modules/gradle-5/lint.gradle b/gradle-modules/gradle-5/lint.gradle new file mode 100644 index 0000000000..7fc8ca780e --- /dev/null +++ b/gradle-modules/gradle-5/lint.gradle @@ -0,0 +1,16 @@ +import com.netflix.nebula.lint.plugin.GradleLintPlugin + +initscript { + repositories { mavenCentral() } + dependencies { + classpath 'com.netflix.nebula:gradle-lint-plugin:18.1.0' + } +} + +allprojects { + apply plugin: GradleLintPlugin + gradleLint { + rules=[] + alwaysRun= false + } +} \ No newline at end of file diff --git a/gradle-modules/gradle-5/settings.gradle b/gradle-modules/gradle-5/settings.gradle index c73af319a3..3173a23d5c 100644 --- a/gradle-modules/gradle-5/settings.gradle +++ b/gradle-modules/gradle-5/settings.gradle @@ -2,4 +2,5 @@ rootProject.name='gradle-5' include 'java-exec' include 'unused-dependencies' include 'source-sets' -include 'cmd-line-args' \ No newline at end of file +include 'cmd-line-args' +include 'gradle-lint-intro' \ No newline at end of file diff --git a/jhipster-modules/jhipster-microservice/car-app/pom.xml b/jhipster-modules/jhipster-microservice/car-app/pom.xml index 9ecf471bc2..f345688939 100644 --- a/jhipster-modules/jhipster-microservice/car-app/pom.xml +++ b/jhipster-modules/jhipster-microservice/car-app/pom.xml @@ -1,7 +1,5 @@ - + 4.0.0 @@ -47,8 +45,8 @@ 3.1.3 v6.10.0 - - + + ${project.build.directory}/test-results 0.0.20 @@ -442,7 +440,7 @@ - + @@ -636,9 +634,9 @@ src/main/resources/config/liquibase/changelog/${maven.build.timestamp}_changelog.xml org.h2.Driver jdbc:h2:file:./target/h2db/db/carapp - + carapp - + hibernate:spring:com.car.app.domain?dialect=org.hibernate.dialect.H2Dialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy true debug @@ -685,7 +683,7 @@ org.apache.maven.plugins maven-war-plugin - + @@ -724,7 +722,7 @@ org.apache.maven.plugins maven-war-plugin - + org.springframework.boot diff --git a/jhipster-modules/jhipster-microservice/dealer-app/pom.xml b/jhipster-modules/jhipster-microservice/dealer-app/pom.xml index 33a238c96c..056bd60f33 100644 --- a/jhipster-modules/jhipster-microservice/dealer-app/pom.xml +++ b/jhipster-modules/jhipster-microservice/dealer-app/pom.xml @@ -1,7 +1,5 @@ - + 4.0.0 @@ -46,8 +44,8 @@ 3.1.3 v6.10.0 - - + + ${project.build.directory}/test-results 0.0.20 @@ -441,7 +439,7 @@ - + @@ -635,9 +633,9 @@ src/main/resources/config/liquibase/changelog/${maven.build.timestamp}_changelog.xml org.h2.Driver jdbc:h2:file:./target/h2db/db/dealerapp - + dealerapp - + hibernate:spring:com.dealer.app.domain?dialect=org.hibernate.dialect.H2Dialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy true debug @@ -684,7 +682,7 @@ org.apache.maven.plugins maven-war-plugin - + @@ -723,7 +721,7 @@ org.apache.maven.plugins maven-war-plugin - + org.springframework.boot diff --git a/jhipster-modules/jhipster-microservice/gateway-app/pom.xml b/jhipster-modules/jhipster-microservice/gateway-app/pom.xml index 3956475380..b90f22f009 100644 --- a/jhipster-modules/jhipster-microservice/gateway-app/pom.xml +++ b/jhipster-modules/jhipster-microservice/gateway-app/pom.xml @@ -1,7 +1,5 @@ - + 4.0.0 @@ -50,8 +48,8 @@ 3.1.3 v6.10.0 - - + + ${project.build.directory}/test-results 0.0.20 @@ -483,7 +481,7 @@ - + @@ -499,7 +497,7 @@ - + @@ -693,9 +691,9 @@ src/main/resources/config/liquibase/changelog/${maven.build.timestamp}_changelog.xml org.h2.Driver jdbc:h2:file:./target/h2db/db/gateway - + gateway - + hibernate:spring:com.gateway.domain?dialect=org.hibernate.dialect.H2Dialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy true debug diff --git a/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/ReplaceDeprecatedApplication.java b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/ReplaceDeprecatedApplication.java new file mode 100644 index 0000000000..f1f50dfbf9 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/ReplaceDeprecatedApplication.java @@ -0,0 +1,14 @@ +package com.baeldung.spring.jdbc.replacedeprecated; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan(basePackages = "com.baeldung.spring.jdbc.replacedeprecated") +public class ReplaceDeprecatedApplication { + + public static void main(String[] args) { + SpringApplication.run(ReplaceDeprecatedApplication.class, args); + } +} diff --git a/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithDeprecatedJdbcTemplateMethods.java b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithDeprecatedJdbcTemplateMethods.java new file mode 100644 index 0000000000..74e1ea6a0a --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithDeprecatedJdbcTemplateMethods.java @@ -0,0 +1,54 @@ +package com.baeldung.spring.jdbc.replacedeprecated; + +import com.baeldung.spring.jdbc.replacedeprecated.model.Student; +import com.baeldung.spring.jdbc.replacedeprecated.model.StudentResultExtractor; +import com.baeldung.spring.jdbc.replacedeprecated.model.StudentRowMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowCountCallbackHandler; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public class StudentDaoWithDeprecatedJdbcTemplateMethods { + JdbcTemplate jdbcTemplate; + + @Autowired + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public List getStudentsOfAgeAndGender(Integer age, String gender) { + String sql = "select student_id, student_name, age, gender, grade, state from student where age= ? and gender = ?"; + Object[] args = {age, gender}; + return jdbcTemplate.query(sql, args, new StudentRowMapper()); + } + + public List getStudentsOfAgeGenderAndGrade(Integer age, String gender, Integer grade) { + String sql = "select student_id, student_name, age, gender, grade, state from student where age= ? and gender = ? and grade = ?"; + Object[] args = {age, gender, grade}; + return jdbcTemplate.query(sql, args, new StudentRowMapper()); + } + + public List getStudentsOfGradeAndState(Integer grade, String state) { + String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?"; + Object[] args = {grade, state}; + return jdbcTemplate.query(sql, args, new StudentResultExtractor()); + } + + public Student getStudentOfStudentIDAndGrade(Integer studentID, Integer grade) { + String sql = "select student_id, student_name, age, gender, grade, state from student where student_id = ? and grade = ?"; + Object[] args = {studentID, grade}; + + return jdbcTemplate.queryForObject(sql, args, new StudentRowMapper()); + } + + public Integer getCountOfStudentsInAGradeFromAState(Integer grade, String state) { + String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?"; + Object[] args = {grade, state}; + RowCountCallbackHandler countCallbackHandler = new RowCountCallbackHandler(); + jdbcTemplate.query(sql, args, countCallbackHandler); + return countCallbackHandler.getRowCount(); + } +} diff --git a/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithPreferredJdbcTemplateMethods.java b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithPreferredJdbcTemplateMethods.java new file mode 100644 index 0000000000..afe8a8f2bd --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithPreferredJdbcTemplateMethods.java @@ -0,0 +1,56 @@ +package com.baeldung.spring.jdbc.replacedeprecated; + +import com.baeldung.spring.jdbc.replacedeprecated.model.Student; +import com.baeldung.spring.jdbc.replacedeprecated.model.StudentResultExtractor; +import com.baeldung.spring.jdbc.replacedeprecated.model.StudentRowMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowCountCallbackHandler; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public class StudentDaoWithPreferredJdbcTemplateMethods { + JdbcTemplate jdbcTemplate; + + @Autowired + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public List getStudentsOfAgeAndGender(Integer age, String gender) { + String sql = "select student_id, student_name, age, gender, grade, state from student where age= ? and gender = ?"; + return jdbcTemplate.query(sql, new StudentRowMapper(), age, gender); + } + + public List getStudentsOfAgeGenderAndGrade(Integer age, String gender, Integer grade) { + String sql = "select student_id, student_name, age, gender, grade, state from student where age= ? and gender = ? and grade = ?"; + return jdbcTemplate.query(sql, new StudentRowMapper(), age, gender, grade); + } + + public List getStudentsOfGradeAndState(Integer grade, String state) { + String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?"; + return jdbcTemplate.query(sql, new StudentResultExtractor(), grade, state); + } + + public Student getStudentOfStudentIDAndGrade(Integer studentID, Integer grade) { + String sql = "select student_id, student_name, age, gender, grade, state from student where student_id = ? and grade = ?"; + + return jdbcTemplate.queryForObject(sql, new StudentRowMapper(), studentID, grade); + } + + public Integer getCountOfGenderInAGrade(String gender, Integer grade) { + String sql = "select count(1) as total from student where gender = ? and grade = ?"; + + return jdbcTemplate.queryForObject(sql, Integer.class, gender, grade); + } + + public Integer getCountOfStudentsInAGradeFromAState(Integer grade, String state) { + String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?"; + + RowCountCallbackHandler countCallbackHandler = new RowCountCallbackHandler(); + jdbcTemplate.query(sql, countCallbackHandler, grade, state); + return countCallbackHandler.getRowCount(); + } +} diff --git a/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/Student.java b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/Student.java new file mode 100644 index 0000000000..001089d830 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/Student.java @@ -0,0 +1,60 @@ +package com.baeldung.spring.jdbc.replacedeprecated.model; + +public class Student { + private Integer studentId; + private String studentName; + private String studentGender; + private Integer age; + private Integer grade; + + public Integer getStudentId() { + return studentId; + } + + public void setStudentId(Integer studentId) { + this.studentId = studentId; + } + + public String getStudentName() { + return studentName; + } + + public void setStudentName(String studentName) { + this.studentName = studentName; + } + + public String getStudentGender() { + return studentGender; + } + + public void setStudentGender(String studentGender) { + this.studentGender = studentGender; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public Integer getGrade() { + return grade; + } + + public void setGrade(Integer grade) { + this.grade = grade; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + private String state; + +} diff --git a/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentResultExtractor.java b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentResultExtractor.java new file mode 100644 index 0000000000..d2da59e5f9 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentResultExtractor.java @@ -0,0 +1,26 @@ +package com.baeldung.spring.jdbc.replacedeprecated.model; + +import org.springframework.jdbc.core.ResultSetExtractor; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class StudentResultExtractor implements ResultSetExtractor> { + @Override + public List extractData(ResultSet rs) throws SQLException { + List students = new ArrayList(); + while(rs.next()) { + Student student = new Student(); + student.setStudentId(rs.getInt("student_id")); + student.setStudentName(rs.getString("student_name")); + student.setAge(rs.getInt("age")); + student.setStudentGender(rs.getString("gender")); + student.setGrade(rs.getInt("grade")); + student.setState(rs.getString("state")); + students.add(student); + } + return students; + } +} diff --git a/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentRowMapper.java b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentRowMapper.java new file mode 100644 index 0000000000..bf2419e663 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentRowMapper.java @@ -0,0 +1,20 @@ +package com.baeldung.spring.jdbc.replacedeprecated.model; + +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class StudentRowMapper implements RowMapper { + @Override + public Student mapRow(ResultSet rs, int rowNum) throws SQLException { + Student student = new Student(); + student.setStudentId(rs.getInt("student_id")); + student.setStudentName(rs.getString("student_name")); + student.setAge(rs.getInt("age")); + student.setStudentGender(rs.getString("gender")); + student.setGrade(rs.getInt("grade")); + student.setState(rs.getString("state")); + return student; + } +} diff --git a/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/application.properties b/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/application.properties new file mode 100644 index 0000000000..04c963ebf4 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/application.properties @@ -0,0 +1,5 @@ +# DataSource Configuration +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=user +spring.datasource.password= # Leave this empty \ No newline at end of file diff --git a/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/drop_student.sql b/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/drop_student.sql new file mode 100644 index 0000000000..954545a862 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/drop_student.sql @@ -0,0 +1 @@ +DROP TABLE student; \ No newline at end of file diff --git a/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/student.sql b/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/student.sql new file mode 100644 index 0000000000..0e137f5445 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/student.sql @@ -0,0 +1,98 @@ + +CREATE TABLE student ( + student_id INT AUTO_INCREMENT PRIMARY KEY, + student_name VARCHAR(255) NOT NULL, + age INT, + grade INT NOT NULL, + gender VARCHAR(10) NOT NULL, + state VARCHAR(100) NOT NULL +); +-- Student 1 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('John Smith', 18, 3, 'Male', 'California'); + +-- Student 2 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Emily Johnson', 17, 2, 'Female', 'New York'); + +-- Student 3 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Michael Davis', 4, 1, 'Male', 'Texas'); + +-- Student 4 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Sophia Martinez', 2, 1, 'Female', 'Florida'); + +-- Student 5 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('William Brown', 5, 5, 'Male', 'California'); + +-- Student 6 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Olivia Garcia', 4, 2, 'Female', 'Texas'); + +-- Student 7 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Ethan Rodriguez', 3, 1, 'Male', 'New York'); + +-- Student 8 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Ava Hernandez', 2, 1, 'Female', 'Florida'); + +-- Student 9 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('James Wilson', 5, 4, 'Male', 'Texas'); + +-- Student 10 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Emma Miller', 3, 1, 'Female', 'California'); + +-- Student 11 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Benjamin Brown', 4, 1, 'Male', 'New York'); + +-- Student 12 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Mia Smith', 2, 1, 'Female', 'Florida'); + +-- Student 13 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Daniel Johnson', 5, 4, 'Male', 'California'); + +-- Student 14 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Ava Davis', 4, 2, 'Female', 'Texas'); + +-- Student 15 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Matthew Martinez', 3, 1, 'Male', 'New York'); + +-- Student 16 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Sophia Taylor', 2, 1, 'Female', 'Florida'); + +-- Student 17 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Alexander White', 5, 4, 'Male', 'California'); + +-- Student 18 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Olivia Johnson', 4, 2, 'Female', 'Texas'); + +-- Student 19 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Christopher Lee', 3, 1, 'Male', 'New York'); + +-- Student 20 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Emma Wilson', 2, 1, 'Female', 'Florida'); + +-- Student 21 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Elijah Smith', 5, 3, 'Male', 'Texas'); + +-- Student 22 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Isabella Davis', 4, 2, 'Female', 'California'); + +-- Student 23 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Liam Johnson', 3, 1, 'Male', 'New York'); + +-- Student 24 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Olivia Garcia', 2, 1, 'Female', 'Florida'); + +-- Student 25 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Noah Rodriguez', 5, 3, 'Male', 'Texas'); + +-- Student 26 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Sophia Hernandez', 4, 2, 'Female', 'California'); + +-- Student 27 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Mason Smith', 3, 1, 'Male', 'New York'); + +-- Student 28 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Ava Taylor', 2, 1, 'Female', 'Florida'); + +-- Student 29 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('William Brown', 5, 5, 'Male', 'Texas'); + +-- Student 30 +INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Olivia Martinez', 4, 4, 'Female', 'California'); diff --git a/persistence-modules/spring-jdbc/src/test/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithJdbcTemplateMethodsUnitTest.java b/persistence-modules/spring-jdbc/src/test/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithJdbcTemplateMethodsUnitTest.java new file mode 100644 index 0000000000..ea1a4a8659 --- /dev/null +++ b/persistence-modules/spring-jdbc/src/test/java/com/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithJdbcTemplateMethodsUnitTest.java @@ -0,0 +1,131 @@ +package com.baeldung.spring.jdbc.replacedeprecated; + +import com.baeldung.spring.jdbc.replacedeprecated.model.Student; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; +import org.springframework.test.context.jdbc.Sql; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@JdbcTest +@Sql(value = "student.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) +@Sql(value = "drop_student.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) +public class StudentDaoWithJdbcTemplateMethodsUnitTest { + private static final Logger logger = LoggerFactory.getLogger(StudentDaoWithJdbcTemplateMethodsUnitTest.class); + @Autowired + StudentDaoWithDeprecatedJdbcTemplateMethods studentDaoWithDeprecatedJdbcTemplateMethods; + @Autowired + StudentDaoWithPreferredJdbcTemplateMethods studentDaoWithPreferredJdbcTemplateMethods; + + @Test + public void givenPreferredMethodQuery_whenArgsAgeAndGender_thenReturnStudents() { + List students = studentDaoWithPreferredJdbcTemplateMethods.getStudentsOfAgeAndGender(4, "Female"); + + for (Student student: students) { + logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender()); + } + assertEquals(6, students.size()); + } + + @Test + public void givenPreferredMethodQuery_whenArgsAgeGenderAndGrade_thenReturnStudents() { + List students = studentDaoWithPreferredJdbcTemplateMethods.getStudentsOfAgeGenderAndGrade(4, "Female", 2); + + for (Student student: students) { + logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender() + + " Student grade: " + student.getGrade()); + } + assertEquals(5, students.size()); + } + + @Test + public void givenDeprecatedMethodQuery_whenArgsAgeGenderAndGrade_thenReturnStudents() { + List students = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentsOfAgeGenderAndGrade(4, "Female", 2); + + for (Student student: students) { + logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender() + + " Student grade: " + student.getGrade()); + } + assertEquals(5, students.size()); + } + + @Test + public void givenDeprecatedMethodQuery_whenArgsAgeAndGender_thenReturnStudents() { + List students = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentsOfAgeAndGender(4, "Female"); + for (Student student: students) { + logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender()); + } + assertEquals(6, students.size()); + } + + @Test + public void givenDeprecatedMethodQuery_whenArgsGradeAndState_thenReturnStudents() { + List students = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentsOfGradeAndState(1, "New York"); + for (Student student: students) { + logger.info("Student Name: " + student.getStudentName() + + " Student grade: " + student.getStudentGender() + + " Student State: " + student.getState()); + } + assertEquals(6, students.size()); + } + + @Test + public void givenPreferredMethodQuery_whenArgsGradeAndState_thenReturnStudents() { + List students = studentDaoWithPreferredJdbcTemplateMethods.getStudentsOfGradeAndState(1, "New York"); + for (Student student: students) { + logger.info("Student Name: " + student.getStudentName() + + " Student grade: " + student.getStudentGender() + + " Student State: " + student.getState()); + } + assertEquals(6, students.size()); + } + + @Test + public void givenDeprecatedMethodQuery_whenArgsGradeAndState_thenReturnCount() { + Integer count = studentDaoWithDeprecatedJdbcTemplateMethods.getCountOfStudentsInAGradeFromAState(1, "New York"); + logger.info("Total students of grade 1 from New York: " + count); + assertEquals(6, count); + } + @Test + public void givenPreferredMethodQuery_whenArgsGradeAndState_thenReturnCount() { + Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfStudentsInAGradeFromAState(1, "New York"); + logger.info("Total students of grade 1 from New York: " + count); + assertEquals(6, count); + } + @Test + public void givenDeprecatedMethodQueryForObject_whenArgsStudentIDAndGrade_thenReturnStudent() { + Student student = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentOfStudentIDAndGrade(4, 1); + assertEquals(1, student.getGrade()); + assertEquals(4, student.getStudentId()); + logger.info("Student ID: " + student.getStudentId() + + " Student Name: " + student.getStudentName() + " Student grade: " + student.getGrade()); + } + + @Test + public void givenPreferredMethodQueryForObject_whenArgsStudentIDAndGrade_thenReturnStudent() { + Student student = studentDaoWithPreferredJdbcTemplateMethods.getStudentOfStudentIDAndGrade(4, 1); + assertEquals(1, student.getGrade()); + assertEquals(4, student.getStudentId()); + logger.info("Student ID: " + student.getStudentId() + + " Student Name: " + student.getStudentName() + " Student grade: " + student.getGrade()); + } + + @Test + public void givenPreferredMethodQueryForObject_whenArgsGenderAndGrade_thenReturnCount() { + Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfGenderInAGrade("Female", 2); + assertEquals(6, count); + logger.info("Total number of Female Students: " + count); + } + + @Test + public void givenDeprecatedMethodQueryForObject_whenArgsGenderAndGrade_thenReturnCount() { + Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfGenderInAGrade("Female", 2); + assertEquals(6, count); + logger.info("Total number of Female Students: " + count); + } +} diff --git a/pom.xml b/pom.xml index cb794fe211..c65f6ce62d 100644 --- a/pom.xml +++ b/pom.xml @@ -1210,7 +1210,6 @@ gradle-modules/gradle/maven-to-gradle persistence-modules/spring-data-neo4j spring-actuator - spring-cloud-modules/spring-cloud-azure spring-cloud-modules/spring-cloud-contract diff --git a/spring-boot-modules/spring-boot-aws/pom.xml b/spring-boot-modules/spring-boot-aws/pom.xml index 815766a846..460acae247 100644 --- a/spring-boot-modules/spring-boot-aws/pom.xml +++ b/spring-boot-modules/spring-boot-aws/pom.xml @@ -9,10 +9,11 @@ spring-boot-aws spring-boot-aws + - com.baeldung.spring-boot-modules - spring-boot-modules - 1.0.0-SNAPSHOT + org.springframework.boot + spring-boot-starter-parent + 2.7.11 diff --git a/testing-modules/mockito-2/README.md b/testing-modules/mockito-2/README.md index ac931a108f..82c97305f6 100644 --- a/testing-modules/mockito-2/README.md +++ b/testing-modules/mockito-2/README.md @@ -6,3 +6,4 @@ This module contains articles about Mockito - [Mocking a Singleton With Mockito](https://www.baeldung.com/java-mockito-singleton) - [Resolving Mockito Exception: Wanted But Not Invoked](https://www.baeldung.com/mockito-exception-wanted-but-not-invoked) - [Matching Null With Mockito](https://www.baeldung.com/mockito-match-null) +- [Mock Same Method with Different Parameters](https://www.baeldung.com/java-mock-same-method-other-parameters) diff --git a/testing-modules/testing-assertions/src/test/java/com/baeldung/assertregexmatch/AssetRegexMatchUnitTest.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/assertregexmatch/AssetRegexMatchUnitTest.java new file mode 100644 index 0000000000..e5456130e9 --- /dev/null +++ b/testing-modules/testing-assertions/src/test/java/com/baeldung/assertregexmatch/AssetRegexMatchUnitTest.java @@ -0,0 +1,42 @@ +package com.baeldung.assertregexmatch; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.hamcrest.Matchers.matchesPattern; +import static org.hamcrest.Matchers.not; +import static org.junit.jupiter.api.Assertions.*; + +public class AssetRegexMatchUnitTest { + + @Test + void whenUsingJunit5assertTrue_thenGetExpectedResult() { + assertTrue("Java at Baeldung".matches(".* at Baeldung$")); + assertFalse("something else".matches(".* at Baeldung$")); + } + + @Test + void whenUsingJunit5assertLinesMatch_thenGetExpectedResult() { + assertLinesMatch(List.of(".* at Baeldung$"), List.of("Kotlin at Baeldung")); + } + + @Test + void whenUsingJunit5assertLinesMatch_thenEqualsIsCheckedFirst() { + assertFalse(".* at Baeldung$".matches(".* at Baeldung$")); + assertLinesMatch(List.of(".* at Baeldung$"), List.of(".* at Baeldung$")); + } + + @Test + void whenUsingAssertJMatches_thenGetExpectedResult() { + org.assertj.core.api.Assertions.assertThat("Linux at Baeldung").matches(".* at Baeldung$"); + org.assertj.core.api.Assertions.assertThat("something unrelated").doesNotMatch(".* at Baeldung$"); + } + + @Test + void whenUsingHamcrestMatches_thenGetExpectedResult() { + org.hamcrest.MatcherAssert.assertThat("Computer science at Baeldung", matchesPattern(".* at Baeldung$")); + org.hamcrest.MatcherAssert.assertThat("something unrelated", not(matchesPattern(".* at Baeldung$"))); + } + +} \ No newline at end of file