Merge remote-tracking branch 'origin/PR-7005' into PR-7005
This commit is contained in:
commit
0507b0417e
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Task> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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<List<Integer>> 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<Integer> 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<Integer> list = input.randomNumbers.get();
|
||||
Collections.sort(list);
|
||||
blackhole.consume(list);
|
||||
}
|
||||
}
|
|
@ -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<int[]> randomNumbersSupplier = randomNumbers::clone;
|
||||
public static final Supplier<int[]> 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());
|
||||
}
|
||||
}
|
|
@ -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<Task> 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<Task> tasks;
|
||||
public static final Supplier<List<Task>> 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);
|
||||
}
|
||||
}
|
|
@ -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++];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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 <T> void sort(List<T> list, Comparator<T> comparator) {
|
||||
sort(list, 0, list.size() - 1, comparator);
|
||||
}
|
||||
public static <T> void sort(List<T> list, int low, int high, Comparator<T> 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 <T> int partition(List<T> list, int begin, int end, Comparator<T> 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;
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -23,6 +23,11 @@
|
|||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${apache-commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.vdurmont</groupId>
|
||||
<artifactId>emoji-java</artifactId>
|
||||
<version>${emoji-java.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -52,6 +57,7 @@
|
|||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<validator.version>1.7</validator.version>
|
||||
<apache-commons-lang3.version>3.12.0</apache-commons-lang3.version>
|
||||
<emoji-java.version>5.1.1</emoji-java.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
allprojects {
|
||||
apply plugin: "nebula.lint"
|
||||
gradleLint {
|
||||
rules = ['dependency-parenthesis']
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
/build/
|
|
@ -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')
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -2,4 +2,5 @@ rootProject.name='gradle-5'
|
|||
include 'java-exec'
|
||||
include 'unused-dependencies'
|
||||
include 'source-sets'
|
||||
include 'cmd-line-args'
|
||||
include 'cmd-line-args'
|
||||
include 'gradle-lint-intro'
|
|
@ -1,7 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
|
@ -47,8 +45,8 @@
|
|||
<metrics-spring.version>3.1.3</metrics-spring.version>
|
||||
<node.version>v6.10.0</node.version>
|
||||
<!-- These remain empty unless the corresponding profile is active -->
|
||||
<profile.no-liquibase/>
|
||||
<profile.swagger/>
|
||||
<profile.no-liquibase />
|
||||
<profile.swagger />
|
||||
<!-- Sonar properties -->
|
||||
<project.testresult.directory>${project.build.directory}/test-results</project.testresult.directory>
|
||||
<prometheus-simpleclient.version>0.0.20</prometheus-simpleclient.version>
|
||||
|
@ -442,7 +440,7 @@
|
|||
</goals>
|
||||
</pluginExecutionFilter>
|
||||
<action>
|
||||
<ignore/>
|
||||
<ignore />
|
||||
</action>
|
||||
</pluginExecution>
|
||||
</pluginExecutions>
|
||||
|
@ -636,9 +634,9 @@
|
|||
<diffChangeLogFile>src/main/resources/config/liquibase/changelog/${maven.build.timestamp}_changelog.xml</diffChangeLogFile>
|
||||
<driver>org.h2.Driver</driver>
|
||||
<url>jdbc:h2:file:./target/h2db/db/carapp</url>
|
||||
<defaultSchemaName/>
|
||||
<defaultSchemaName />
|
||||
<username>carapp</username>
|
||||
<password/>
|
||||
<password />
|
||||
<referenceUrl>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</referenceUrl>
|
||||
<verbose>true</verbose>
|
||||
<logging>debug</logging>
|
||||
|
@ -685,7 +683,7 @@
|
|||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<configuration/>
|
||||
<configuration />
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
@ -724,7 +722,7 @@
|
|||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<configuration/>
|
||||
<configuration />
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
|
@ -46,8 +44,8 @@
|
|||
<metrics-spring.version>3.1.3</metrics-spring.version>
|
||||
<node.version>v6.10.0</node.version>
|
||||
<!-- These remain empty unless the corresponding profile is active -->
|
||||
<profile.no-liquibase/>
|
||||
<profile.swagger/>
|
||||
<profile.no-liquibase />
|
||||
<profile.swagger />
|
||||
<!-- Sonar properties -->
|
||||
<project.testresult.directory>${project.build.directory}/test-results</project.testresult.directory>
|
||||
<prometheus-simpleclient.version>0.0.20</prometheus-simpleclient.version>
|
||||
|
@ -441,7 +439,7 @@
|
|||
</goals>
|
||||
</pluginExecutionFilter>
|
||||
<action>
|
||||
<ignore/>
|
||||
<ignore />
|
||||
</action>
|
||||
</pluginExecution>
|
||||
</pluginExecutions>
|
||||
|
@ -635,9 +633,9 @@
|
|||
<diffChangeLogFile>src/main/resources/config/liquibase/changelog/${maven.build.timestamp}_changelog.xml</diffChangeLogFile>
|
||||
<driver>org.h2.Driver</driver>
|
||||
<url>jdbc:h2:file:./target/h2db/db/dealerapp</url>
|
||||
<defaultSchemaName/>
|
||||
<defaultSchemaName />
|
||||
<username>dealerapp</username>
|
||||
<password/>
|
||||
<password />
|
||||
<referenceUrl>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</referenceUrl>
|
||||
<verbose>true</verbose>
|
||||
<logging>debug</logging>
|
||||
|
@ -684,7 +682,7 @@
|
|||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<configuration/>
|
||||
<configuration />
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
@ -723,7 +721,7 @@
|
|||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<configuration/>
|
||||
<configuration />
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
|
@ -50,8 +48,8 @@
|
|||
<metrics-spring.version>3.1.3</metrics-spring.version>
|
||||
<node.version>v6.10.0</node.version>
|
||||
<!-- These remain empty unless the corresponding profile is active -->
|
||||
<profile.no-liquibase/>
|
||||
<profile.swagger/>
|
||||
<profile.no-liquibase />
|
||||
<profile.swagger />
|
||||
<!-- Sonar properties -->
|
||||
<project.testresult.directory>${project.build.directory}/test-results</project.testresult.directory>
|
||||
<prometheus-simpleclient.version>0.0.20</prometheus-simpleclient.version>
|
||||
|
@ -483,7 +481,7 @@
|
|||
</goals>
|
||||
</pluginExecutionFilter>
|
||||
<action>
|
||||
<ignore/>
|
||||
<ignore />
|
||||
</action>
|
||||
</pluginExecution>
|
||||
<pluginExecution>
|
||||
|
@ -499,7 +497,7 @@
|
|||
</goals>
|
||||
</pluginExecutionFilter>
|
||||
<action>
|
||||
<ignore/>
|
||||
<ignore />
|
||||
</action>
|
||||
</pluginExecution>
|
||||
</pluginExecutions>
|
||||
|
@ -693,9 +691,9 @@
|
|||
<diffChangeLogFile>src/main/resources/config/liquibase/changelog/${maven.build.timestamp}_changelog.xml</diffChangeLogFile>
|
||||
<driver>org.h2.Driver</driver>
|
||||
<url>jdbc:h2:file:./target/h2db/db/gateway</url>
|
||||
<defaultSchemaName/>
|
||||
<defaultSchemaName />
|
||||
<username>gateway</username>
|
||||
<password/>
|
||||
<password />
|
||||
<referenceUrl>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</referenceUrl>
|
||||
<verbose>true</verbose>
|
||||
<logging>debug</logging>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<Student> 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<Student> 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<Student> 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();
|
||||
}
|
||||
}
|
|
@ -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<Student> 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<Student> 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<Student> 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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -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<List<Student>> {
|
||||
@Override
|
||||
public List<Student> extractData(ResultSet rs) throws SQLException {
|
||||
List<Student> students = new ArrayList<Student>();
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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<Student> {
|
||||
@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;
|
||||
}
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1 @@
|
|||
DROP TABLE student;
|
|
@ -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');
|
|
@ -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<Student> 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<Student> 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<Student> 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<Student> 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<Student> 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<Student> 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);
|
||||
}
|
||||
}
|
1
pom.xml
1
pom.xml
|
@ -1210,7 +1210,6 @@
|
|||
<module>gradle-modules/gradle/maven-to-gradle</module>
|
||||
<module>persistence-modules/spring-data-neo4j</module>
|
||||
<module>spring-actuator</module>
|
||||
<module>spring-cloud-modules/spring-cloud-azure</module>
|
||||
<module>spring-cloud-modules/spring-cloud-contract</module>
|
||||
</modules>
|
||||
|
||||
|
|
|
@ -9,10 +9,11 @@
|
|||
<name>spring-boot-aws</name>
|
||||
<description>spring-boot-aws</description>
|
||||
|
||||
<!-- "sam build" needs all the dependencies including its parent project. Hence, we cannot use parent-boot in this case -->
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.11</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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$")));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue