Merge branch 'eugenp:master' into PR-6865

This commit is contained in:
parthiv39731 2023-08-28 23:14:13 +05:30 committed by GitHub
commit e521083c14
163 changed files with 1453 additions and 952 deletions

View File

@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory;
public class KadaneAlgorithm {
private Logger logger = LoggerFactory.getLogger(BruteForceAlgorithm.class.getName());
private Logger logger = LoggerFactory.getLogger(KadaneAlgorithm.class.getName());
public int maxSubArraySum(int[] arr) {
@ -14,15 +14,15 @@ public class KadaneAlgorithm {
int end = 0;
int maxSoFar = arr[0], maxEndingHere = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > maxEndingHere + arr[i]) {
start = i;
maxEndingHere = maxEndingHere + arr[i];
if (arr[i] > maxEndingHere) {
maxEndingHere = arr[i];
} else {
maxEndingHere = maxEndingHere + arr[i];
if (maxSoFar < maxEndingHere) {
start = i;
}
}
if (maxSoFar < maxEndingHere) {
maxSoFar = maxEndingHere;
end = i;

View File

@ -7,7 +7,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
class KadaneAlgorithmUnitTest {
@Test
void givenArrayWithNegativeNumberWhenMaximumSubarrayThenReturns6() {
void givenArrayWithNegativeNumberWhenMaximumSubarrayThenReturnsExpectedResult() {
//given
int[] arr = new int[] { -3, 1, -8, 4, -1, 2, 1, -5, 5 };
//when
@ -27,7 +27,7 @@ class KadaneAlgorithmUnitTest {
//then
assertEquals(-1, maxSum);
}
@Test
void givenArrayWithAllPosiitveNumbersWhenMaximumSubarrayThenReturnsExpectedResult() {
//given
@ -39,4 +39,15 @@ class KadaneAlgorithmUnitTest {
assertEquals(10, maxSum);
}
@Test
void givenArrayToTestStartIndexWhenMaximumSubarrayThenReturnsExpectedResult() {
//given
int[] arr = new int[] { 1, 2, -1, 3, -6, -2 };
//when
KadaneAlgorithm algorithm = new KadaneAlgorithm();
int maxSum = algorithm.maxSubArraySum(arr);
//then
assertEquals(5, maxSum);
}
}

View File

@ -3,4 +3,5 @@
- [Algorithm to Identify and Validate a Credit Card Number](https://www.baeldung.com/java-validate-cc-number)
- [Find the N Most Frequent Elements in a Java Array](https://www.baeldung.com/java-n-most-frequent-elements-array)
- [Getting Pixel Array From Image in Java](https://www.baeldung.com/java-getting-pixel-array-from-image)
- [Calculate Distance Between Two Coordinates in Java](https://www.baeldung.com/java-find-distance-between-points)
- More articles: [[<-- prev]](/algorithms-miscellaneous-6)

View File

@ -43,19 +43,19 @@
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-ri</artifactId>
<version>2.3.3</version>
<version>${jaxws-ri.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<version>${javax.servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<version>${jstl.version}</version>
</dependency>
</dependencies>
@ -117,7 +117,9 @@
<properties>
<spring.version>5.3.25</spring.version>
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
<jstl.version>1.2</jstl.version>
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
<jaxws-ri.version>2.3.3</jaxws-ri.version>
</properties>
</project>

View File

@ -241,8 +241,6 @@
<httpcore.version>4.4.16</httpcore.version>
<httpclient.version>4.5.14</httpclient.version>
<mockserver.version>5.11.2</mockserver.version>
<!-- maven plugins -->
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
</properties>
</project>

View File

@ -21,7 +21,6 @@
</dependency>
</dependencies>
<properties>
<javax.validation.validation-api.version>2.0.1.Final</javax.validation.validation-api.version>
</properties>

View File

@ -63,7 +63,6 @@
<org.apache.httpcomponents.version>4.5.2</org.apache.httpcomponents.version>
<velocity-version>1.7</velocity-version>
<velocity-tools-version>2.0</velocity-tools-version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
</properties>
</project>

View File

@ -49,7 +49,6 @@
<properties>
<asm.version>5.2</asm.version>
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
</properties>
</project>

View File

@ -1,6 +1,7 @@
<?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 https://maven.apache.org/xsd/maven-4.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>aws-s3-update-object</artifactId>
<version>0.0.1-SNAPSHOT</version>

View File

@ -8,4 +8,5 @@ This module contains articles about Simple Storage Service (S3) on AWS
- [Multipart Uploads in Amazon S3 with Java](https://www.baeldung.com/aws-s3-multipart-upload)
- [Using the JetS3t Java Client With Amazon S3](https://www.baeldung.com/jets3t-amazon-s3)
- [Check if a Specified Key Exists in a Given S3 Bucket Using Java](https://www.baeldung.com/java-aws-s3-check-specified-key-exists)
- [Listing All AWS S3 Objects in a Bucket Using Java](https://www.baeldung.com/java-aws-s3-list-bucket-objects)
- [Listing All AWS S3 Objects in a Bucket Using Java](https://www.baeldung.com/java-aws-s3-list-bucket-objects)
- [Update an Existing Amazon S3 Object Using Java](https://www.baeldung.com/java-update-amazon-s3-object)

View File

@ -1,7 +1,7 @@
<?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/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>aws-s3</artifactId>
<version>0.1.0-SNAPSHOT</version>

View File

@ -122,7 +122,6 @@
<docker.image.prefix>${azure.containerRegistry}.azurecr.io</docker.image.prefix>
<docker-maven-plugin.version>1.1.0</docker-maven-plugin.version>
<azure-webapp-maven-plugin.version>1.1.0</azure-webapp-maven-plugin.version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
</properties>
</project>

View File

@ -27,6 +27,7 @@
<hsqldb.version>2.7.1</hsqldb.version>
<spock-core.version>2.3-groovy-3.0</spock-core.version>
<gmavenplus-plugin.version>2.1.0</gmavenplus-plugin.version>
<maven-failsafe-plugin.version>2.21.0</maven-failsafe-plugin.version>
</properties>
</project>

View File

@ -48,7 +48,6 @@
<properties>
<maven.compiler.release>14</maven.compiler.release>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
</properties>

View File

@ -53,7 +53,6 @@
<properties>
<maven.compiler.release>15</maven.compiler.release>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
</properties>

View File

@ -0,0 +1,126 @@
package com.baeldung.multipleorwithif;
import static java.time.Month.DECEMBER;
import static java.time.Month.NOVEMBER;
import static java.time.Month.OCTOBER;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.in;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Month;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.function.Predicate;
import org.junit.jupiter.api.Test;
class MultipleOrWithIfUnitTest {
private final Random rand = new Random();
final Set<Month> months = Set.of(OCTOBER, NOVEMBER, DECEMBER);
@Test
public void givenIfStatement_whenMultipleOrOperator_thenAssert() {
assertTrue(multipleOrOperatorIf(monthIn()));
assertFalse(multipleOrOperatorIf(monthNotIn()));
}
boolean multipleOrOperatorIf(Month month) {
if (month == OCTOBER || month == NOVEMBER || month == DECEMBER) {
return true;
}
return false;
}
@Test
public void givenSwitch_whenMultipleCase_thenBreakAndAssert() {
assertTrue(switchMonth(monthIn()));
assertFalse(switchMonth(monthNotIn()));
}
boolean switchMonth(Month month) {
return switch (month) {
case OCTOBER, NOVEMBER, DECEMBER -> true;
default -> false;
};
}
@Test
public void givenAllowedValuesList_whenContains_thenAssert() {
assertTrue(contains(monthIn()));
assertFalse(contains(monthNotIn()));
}
@Test
public void givenPredicates_whenTestMultipleOr_thenAssert() {
assertTrue(predicateWithIf(monthIn()));
assertFalse(predicateWithIf(monthNotIn()));
}
@Test
public void givenInputList_whenFilterWithPredicate_thenAssert() {
List<Month> list = List.of(monthIn(), monthIn(), monthNotIn());
list.stream()
.filter(this::predicateWithIf)
.forEach(m -> assertThat(m, is(in(months))));
}
Predicate<Month> orPredicate() {
Predicate<Month> predicate = x -> x == OCTOBER;
Predicate<Month> predicate1 = x -> x == NOVEMBER;
Predicate<Month> predicate2 = x -> x == DECEMBER;
return predicate.or(predicate1)
.or(predicate2);
}
boolean predicateWithIf(Month month) {
if (orPredicate().test(month)) {
return true;
}
return false;
}
@Test
public void givenContainsInSetPredicate_whenTestPredicate_thenAssert() {
Predicate<Month> collectionPredicate = this::contains;
assertTrue(collectionPredicate.test(monthIn()));
assertFalse(collectionPredicate.test(monthNotIn()));
}
@Test
public void givenInputList_whenFilterWithContains_thenAssert() {
List<Month> monthList = List.of(monthIn(), monthIn(), monthNotIn());
monthList.stream()
.filter(this::contains)
.forEach(m -> assertThat(m, is(in(months))));
}
private boolean contains(Month month) {
if (months.contains(month)) {
return true;
}
return false;
}
private Month monthIn() {
return Month.of(rand.ints(10, 13)
.findFirst()
.orElse(10));
}
private Month monthNotIn() {
return Month.of(rand.ints(1, 10)
.findFirst()
.orElse(1));
}
}

View File

@ -152,7 +152,6 @@
<awaitility.version>4.0.2</awaitility.version>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
</properties>
</project>

View File

@ -13,3 +13,4 @@ This module contains articles about advanced operations on arrays in Java. They
- [Performance of System.arraycopy() vs. Arrays.copyOf()](https://www.baeldung.com/java-system-arraycopy-arrays-copyof-performance)
- [Slicing Arrays in Java](https://www.baeldung.com/java-slicing-arrays)
- [Combining Two or More Byte Arrays](https://www.baeldung.com/java-concatenate-byte-arrays)
- [Calculating the Sum of Two Arrays in Java](https://www.baeldung.com/java-sum-arrays-element-wise)

View File

@ -13,4 +13,5 @@ This module contains articles about conversions among Collection types and array
- [Combining Two Lists Into a Map in Java](https://www.baeldung.com/java-combine-two-lists-into-map)
- [Convert a List of Strings to a List of Integers](https://www.baeldung.com/java-convert-list-strings-to-integers)
- [Convert List to Long[] Array in Java](https://www.baeldung.com/java-convert-list-object-to-long-array)
- [Get the First n Elements of a List Into an Array](https://www.baeldung.com/java-take-start-elements-list-array)
- More articles: [[<-- prev]](../core-java-collections-conversions)

View File

@ -1,7 +1,7 @@
<?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/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-list-5</artifactId>
<name>core-java-collections-list-5</name>

View File

@ -9,4 +9,5 @@ This module contains articles about Map data structures in Java.
- [Collections.synchronizedMap vs. ConcurrentHashMap](https://www.baeldung.com/java-synchronizedmap-vs-concurrenthashmap)
- [Java HashMap Load Factor](https://www.baeldung.com/java-hashmap-load-factor)
- [Converting Java Properties to HashMap](https://www.baeldung.com/java-convert-properties-to-hashmap)
- [Get Values and Keys as ArrayList From a HashMap](https://www.baeldung.com/java-values-keys-arraylists-hashmap)
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-2)

View File

@ -58,4 +58,8 @@ public class DataQueue {
return queue.poll();
}
}
public Integer getSize() {
return queue.size();
}
}

View File

@ -1,12 +1,13 @@
package com.baeldung.producerconsumer;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
public class Producer implements Runnable {
private static final Logger log = Logger.getLogger(Producer.class.getCanonicalName());
private final DataQueue dataQueue;
private static int idSequence = 0;
final ReentrantLock lock = new ReentrantLock();
public Producer(DataQueue dataQueue) {
this.dataQueue = dataQueue;
@ -19,22 +20,38 @@ public class Producer implements Runnable {
public void produce() {
while (dataQueue.runFlag) {
while (dataQueue.isFull() && dataQueue.runFlag) {
try {
dataQueue.waitOnFull();
} catch (InterruptedException e) {
e.printStackTrace();
try {
lock.lock();
while (dataQueue.isFull() && dataQueue.runFlag) {
try {
dataQueue.waitOnFull();
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
if (!dataQueue.runFlag) {
break;
}
}
if (!dataQueue.runFlag) {
break;
}
Message message = generateMessage();
dataQueue.add(message);
dataQueue.notifyAllForEmpty();
Message message = generateMessage();
dataQueue.add(message);
dataQueue.notifyAllForEmpty();
log.info("Size of the queue is: " + dataQueue.getSize());
}
finally{
lock.unlock();
}
//Sleeping on random time to make it realistic
ThreadUtil.sleep((long) (Math.random() * 100));
}
log.info("Producer Stopped");
}
@ -43,9 +60,6 @@ public class Producer implements Runnable {
log.info(String.format("[%s] Generated Message. Id: %d, Data: %f%n",
Thread.currentThread().getName(), message.getId(), message.getData()));
//Sleeping on random time to make it realistic
ThreadUtil.sleep((long) (message.getData() * 100));
return message;
}

View File

@ -36,8 +36,8 @@ public class ProducerConsumerDemonstrator {
public static void demoMultipleProducersAndMultipleConsumers() {
DataQueue dataQueue = new DataQueue(MAX_QUEUE_CAPACITY);
int producerCount = 3;
int consumerCount = 3;
int producerCount = 5;
int consumerCount = 5;
List<Thread> threads = new ArrayList<>();
Producer producer = new Producer(dataQueue);
for(int i = 0; i < producerCount; i++) {
@ -45,6 +45,7 @@ public class ProducerConsumerDemonstrator {
producerThread.start();
threads.add(producerThread);
}
Consumer consumer = new Consumer(dataQueue);
for(int i = 0; i < consumerCount; i++) {
Thread consumerThread = new Thread(consumer);
@ -52,8 +53,8 @@ public class ProducerConsumerDemonstrator {
threads.add(consumerThread);
}
// let threads run for two seconds
sleep(2000);
// let threads run for ten seconds
sleep(10000);
// Stop threads
producer.stop();

View File

@ -11,4 +11,5 @@ This module contains articles about basic Java concurrency
- [Runnable vs. Callable in Java](https://www.baeldung.com/java-runnable-callable)
- [What Is Thread-Safety and How to Achieve It?](https://www.baeldung.com/java-thread-safety)
- [How to Get Notified When a Task Completes in Java Executors](https://www.baeldung.com/java-executors-task-completed-notification)
- [Difference Between Future, CompletableFuture, and Rxjavas Observable](https://www.baeldung.com/java-future-completablefuture-rxjavas-observable)
- [[Next -->]](/core-java-modules/core-java-concurrency-basic-2)

View File

@ -37,20 +37,19 @@
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source.version>14</maven.compiler.source.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven.compiler.target.version>14</maven.compiler.target.version>
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
</properties>

View File

@ -12,7 +12,7 @@
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
@ -25,7 +25,7 @@
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<properties>
<joda-time.version>2.12.5</joda-time.version>
<commons-lang3.version>3.12.0</commons-lang3.version>

View File

@ -9,3 +9,4 @@ This module contains articles about converting between Java date and time object
- [Convert Between java.time.Instant and java.sql.Timestamp](https://www.baeldung.com/java-time-instant-to-java-sql-timestamp)
- [Convert Between LocalDateTime and ZonedDateTime](https://www.baeldung.com/java-localdatetime-zoneddatetime)
- [Conversion From 12-Hour Time to 24-Hour Time in Java](https://www.baeldung.com/java-convert-time-format)
- [Convert Epoch Time to LocalDate and LocalDateTime](https://www.baeldung.com/java-convert-epoch-localdate)

View File

@ -132,7 +132,6 @@
<!-- Mime Type Libraries -->
<tika.version>2.8.0</tika.version>
<jmime-magic.version>0.1.5</jmime-magic.version>
<maven-jar-plugin.version>3.3.0</maven-jar-plugin.version>
<fscontext.version>4.4.2</fscontext.version>
<jakarta-activation-api.version>2.1.2</jakarta-activation-api.version>
<angus-activation.version>2.0.1</angus-activation.version>

View File

@ -275,7 +275,6 @@
<!-- maven plugins -->
<javamoney.moneta.version>1.1</javamoney.moneta.version>
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
<onejar-maven-plugin.version>1.4.4</onejar-maven-plugin.version>
<maven-shade-plugin.version>3.1.1</maven-shade-plugin.version>
<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -12,3 +12,4 @@ This module contains articles about inheritance in Java
- [Guide to Inheritance in Java](https://www.baeldung.com/java-inheritance)
- [Object Type Casting in Java](https://www.baeldung.com/java-type-casting)
- [Variable and Method Hiding in Java](https://www.baeldung.com/java-variable-method-hiding)
- [Inner Classes Vs. Subclasses in Java](https://www.baeldung.com/java-inner-classes-vs-subclasses)

View File

@ -1,54 +1,77 @@
package com.baeldung.modulo;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import static org.assertj.core.api.Java6Assertions.*;
public class ModuloUnitTest {
@Test
public void whenIntegerDivision_thenLosesRemainder(){
assertThat(11 / 4).isEqualTo(2);
}
@Test
public void whenDoubleDivision_thenKeepsRemainder(){
assertThat(11 / 4.0).isEqualTo(2.75);
}
@Test
public void whenModulo_thenReturnsRemainder(){
assertThat(11 % 4).isEqualTo(3);
}
@Test(expected = ArithmeticException.class)
public void whenDivisionByZero_thenArithmeticException(){
double result = 1 / 0;
}
@Test(expected = ArithmeticException.class)
public void whenModuloByZero_thenArithmeticException(){
double result = 1 % 0;
}
@Test
public void whenDivisorIsOddAndModulusIs2_thenResultIs1(){
assertThat(3 % 2).isEqualTo(1);
}
@Test
public void whenDivisorIsEvenAndModulusIs2_thenResultIs0(){
assertThat(4 % 2).isEqualTo(0);
}
@Test
public void whenItemsIsAddedToCircularQueue_thenNoArrayIndexOutOfBounds(){
int QUEUE_CAPACITY= 10;
int[] circularQueue = new int[QUEUE_CAPACITY];
int itemsInserted = 0;
for (int value = 0; value < 1000; value++) {
int writeIndex = ++itemsInserted % QUEUE_CAPACITY;
circularQueue[writeIndex] = value;
@Test
public void whenIntegerDivision_thenLosesRemainder() {
assertThat(11 / 4).isEqualTo(2);
}
@Test
public void whenDoubleDivision_thenKeepsRemainder() {
assertThat(11 / 4.0).isEqualTo(2.75);
}
@Test
public void whenModulo_thenReturnsRemainder() {
assertThat(11 % 4).isEqualTo(3);
}
@Test(expected = ArithmeticException.class)
public void whenDivisionByZero_thenArithmeticException() {
double result = 1 / 0;
}
@Test(expected = ArithmeticException.class)
public void whenModuloByZero_thenArithmeticException() {
double result = 1 % 0;
}
@Test
public void whenDivisorIsOddAndModulusIs2_thenResultIs1() {
assertThat(3 % 2).isEqualTo(1);
}
@Test
public void whenDivisorIsEvenAndModulusIs2_thenResultIs0() {
assertThat(4 % 2).isEqualTo(0);
}
@Test
public void whenDividendIsNegativeAndModulusIs2_thenResultIsNegative() {
assertEquals(-1, -9 % 2);
}
@Test
public void whenDividendIsNegativeAndRemainderIsCheckedForNegativeValue_thenResultIsPositive() {
int remainder = -9 % 2;
if (remainder < 0) {
remainder += 2;
}
assertEquals(1, remainder);
}
@Test
public void whenDividendIsNegativeAndUsesMathClass_thenResultIsPositive() {
int remainder = Math.floorMod(-9, 2);
assertEquals(1, remainder);
}
@Test
public void whenItemsIsAddedToCircularQueue_thenNoArrayIndexOutOfBounds() {
int QUEUE_CAPACITY = 10;
int[] circularQueue = new int[QUEUE_CAPACITY];
int itemsInserted = 0;
for (int value = 0; value < 1000; value++) {
int writeIndex = ++itemsInserted % QUEUE_CAPACITY;
circularQueue[writeIndex] = value;
}
}
}
}

View File

@ -0,0 +1,75 @@
package com.baeldung.regex.indexesofmatches;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
public class IndexesOfMatchesUnitTest {
private static final String INPUT = "This line contains <the first value>, <the second value>, and <the third value>.";
@Test
void whenUsingNorCharClass_thenGetExpectedTexts() {
Pattern pattern = Pattern.compile("<[^>]*>");
Matcher matcher = pattern.matcher(INPUT);
List<String> result = new ArrayList<>();
while (matcher.find()) {
result.add(matcher.group());
}
assertThat(result).containsExactly("<the first value>", "<the second value>", "<the third value>");
}
@Test
void whenCallingMatcherEnd_thenGetIndexesAfterTheMatchSequence() {
Pattern pattern = Pattern.compile("456");
Matcher matcher = pattern.matcher("0123456789");
String result = null;
int startIdx = -1;
int endIdx = -1;
if (matcher.find()) {
result = matcher.group();
startIdx = matcher.start();
endIdx = matcher.end();
}
assertThat(result).isEqualTo("456");
assertThat(startIdx).isEqualTo(4);
assertThat(endIdx).isEqualTo(7);
}
@Test
void whenUsingMatcherStartAndEnd_thenGetIndexesOfMatches() {
Pattern pattern = Pattern.compile("<[^>]*>");
Matcher matcher = pattern.matcher(INPUT);
List<String> result = new ArrayList<>();
Map<Integer, Integer> indexesOfMatches = new LinkedHashMap<>();
while (matcher.find()) {
result.add(matcher.group());
indexesOfMatches.put(matcher.start(), matcher.end());
}
assertThat(result).containsExactly("<the first value>", "<the second value>", "<the third value>");
assertThat(indexesOfMatches.entrySet()).map(entry -> INPUT.substring(entry.getKey(), entry.getValue()))
.containsExactly("<the first value>", "<the second value>", "<the third value>");
}
@Test
void whenUsingMatcherStartAndEndWithGroupIdx_thenGetIndexesOfMatches() {
Pattern pattern = Pattern.compile("<([^>]*)>");
Matcher matcher = pattern.matcher(INPUT);
List<String> result = new ArrayList<>();
Map<Integer, Integer> indexesOfMatches = new LinkedHashMap<>();
while (matcher.find()) {
result.add(matcher.group(1));
indexesOfMatches.put(matcher.start(1), matcher.end(1));
}
assertThat(result).containsExactly("the first value", "the second value", "the third value");
assertThat(indexesOfMatches.entrySet()).map(entry -> INPUT.substring(entry.getKey(), entry.getValue()))
.containsExactly("the first value", "the second value", "the third value");
}
}

View File

@ -0,0 +1,98 @@
package com.baeldung.regex.squarebrackets;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
import com.google.common.collect.Lists;
public class ExtractTextBetweenSquareBracketsUnitTest {
static final String INPUT1 = "some text [THE IMPORTANT MESSAGE] something else";
static final String EXPECTED1 = "THE IMPORTANT MESSAGE";
static final String INPUT2 = "[La La Land], [The last Emperor], and [Life of Pi] are all great movies.";
static final List<String> EXPECTED2 = Lists.newArrayList("La La Land", "The last Emperor", "Life of Pi");
@Test
void whenUsingDotStarOnInput1_thenGetExpectedResult() {
String result = null;
String rePattern = "\\[(.*)]";
Pattern p = Pattern.compile(rePattern);
Matcher m = p.matcher(INPUT1);
if (m.find()) {
result = m.group(1);
}
assertThat(result).isEqualTo(EXPECTED1);
}
@Test
void whenUsingCharClassOnInput1_thenGetExpectedResult() {
String result = null;
String rePattern = "\\[([^]]*)";
Pattern p = Pattern.compile(rePattern);
Matcher m = p.matcher(INPUT1);
if (m.find()) {
result = m.group(1);
}
assertThat(result).isEqualTo(EXPECTED1);
}
@Test
void whenUsingSplitOnInput1_thenGetExpectedResult() {
String[] strArray = INPUT1.split("[\\[\\]]", -1);
String result = strArray.length == 3 ? strArray[1] : null;
assertThat(result).isEqualTo(EXPECTED1);
}
@Test
void whenUsingSplitWithLimit_thenGetExpectedResult() {
String[] strArray = "[THE IMPORTANT MESSAGE]".split("[\\[\\]]");
assertThat(strArray).hasSize(2)
.containsExactly("", "THE IMPORTANT MESSAGE");
strArray = "[THE IMPORTANT MESSAGE]".split("[\\[\\]]", -1);
assertThat(strArray).hasSize(3)
.containsExactly("", "THE IMPORTANT MESSAGE", "");
}
@Test
void whenUsingNonGreedyOnInput2_thenGetExpectedResult() {
List<String> result = new ArrayList<>();
String rePattern = "\\[(.*?)]";
Pattern p = Pattern.compile(rePattern);
Matcher m = p.matcher(INPUT2);
while (m.find()) {
result.add(m.group(1));
}
assertThat(result).isEqualTo(EXPECTED2);
}
@Test
void whenUsingCharClassOnInput2_thenGetExpectedResult() {
List<String> result = new ArrayList<>();
String rePattern = "\\[([^]]*)";
Pattern p = Pattern.compile(rePattern);
Matcher m = p.matcher(INPUT2);
while (m.find()) {
result.add(m.group(1));
}
assertThat(result).isEqualTo(EXPECTED2);
}
@Test
void whenUsingSplitInput2_thenGetExpectedResult() {
List<String> result = new ArrayList<>();
String[] strArray = INPUT2.split("[\\[\\]]", -1);
for (int i = 1; i < strArray.length; i += 2) {
result.add(strArray[i]);
}
assertThat(result).isEqualTo(EXPECTED2);
}
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-scanner</artifactId>
<name>core-java-scanner</name>

View File

@ -115,7 +115,6 @@
<properties>
<!-- testing -->
<assertj.version>3.23.1</assertj.version>
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
<rx.java.version>1.2.5</rx.java.version>

View File

@ -69,7 +69,6 @@
<properties>
<!-- testing -->
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
<vavr.version>0.10.2</vavr.version>

View File

@ -58,7 +58,6 @@
<properties>
<!-- testing -->
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

View File

@ -1,7 +1,7 @@
<?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/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-operations-6</artifactId>
<name>core-java-string-operations-6</name>

View File

@ -15,30 +15,20 @@
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.4.0-b180725.0427</version>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>${jakarta.xml.bind-api.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
@ -57,7 +47,8 @@
</build>
<properties>
<commons-codec.version>1.15</commons-codec.version>
<commons-codec.version>1.16.0</commons-codec.version>
<jakarta.xml.bind-api.version>4.0.0</jakarta.xml.bind-api.version>
</properties>
</project>

View File

@ -2,7 +2,7 @@ package com.baeldung.base64encodinganddecoding;
import org.junit.Test;
import javax.xml.bind.DatatypeConverter;
import jakarta.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

View File

@ -1,37 +1,37 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>inject-intro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>avaje-inject-intro</name>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<avaje.inject.version>9.5</avaje.inject.version>
</properties>
<dependencies>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
<version>${avaje.inject.version}</version>
</dependency>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>inject-intro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>avaje-inject-intro</name>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<avaje.inject.version>9.5</avaje.inject.version>
</properties>
<dependencies>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
<version>${avaje.inject.version}</version>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-test</artifactId>
<version>${avaje.inject.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-test</artifactId>
<version>${avaje.inject.version}</version>
<scope>test</scope>
</dependency>
<!-- Annotation processors -->
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
<version>${avaje.inject.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
<!-- Annotation processors -->
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
<version>${avaje.inject.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -119,7 +119,6 @@
<disruptor.version>3.3.6</disruptor.version>
<!-- testing -->
<maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
<onejar-maven-plugin.version>1.4.4</onejar-maven-plugin.version>
</properties>

View File

@ -20,6 +20,7 @@ import org.joda.time.DateTimeZone;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.StdDateFormat;
@ -67,6 +68,8 @@ public class JacksonDateUnitTest {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID);
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
String converted = objectMapper.writeValueAsString(now);

View File

@ -1,7 +1,7 @@
<?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/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jackson-polymorphic-deserialization</artifactId>
<name>jackson-polymorphic-deserialization</name>

View File

@ -30,7 +30,6 @@
<properties>
<javax.websocket-api.version>1.1</javax.websocket-api.version>
<gson.version>2.8.0</gson.version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
</properties>
</project>

View File

@ -3,7 +3,7 @@
This module contains articles about Bean Validation.
### Relevant Articles:
- [Java Bean Validation Basics](https://www.baeldung.com/javax-validation)
- [Java Bean Validation Basics](https://www.baeldung.com/java-validation)
- [Validating Container Elements with Jakarta Bean Validation 3.0](https://www.baeldung.com/bean-validation-container-elements)
- [Validations for Enum Types](https://www.baeldung.com/javax-validations-enums)
- [Javax BigDecimal Validation](https://www.baeldung.com/javax-bigdecimal-validation)

View File

@ -100,7 +100,6 @@
<properties>
<jersey.version>3.1.1</jersey.version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
</properties>
</project>

View File

@ -1,5 +1,7 @@
<?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>
@ -45,8 +47,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>
@ -440,7 +442,7 @@
</goals>
</pluginExecutionFilter>
<action>
<ignore />
<ignore/>
</action>
</pluginExecution>
</pluginExecutions>
@ -634,9 +636,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&amp;hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&amp;hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy</referenceUrl>
<verbose>true</verbose>
<logging>debug</logging>
@ -683,7 +685,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration />
<configuration/>
</plugin>
</plugins>
</build>
@ -722,7 +724,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration />
<configuration/>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>

View File

@ -1,5 +1,7 @@
<?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>
@ -44,8 +46,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>
@ -439,7 +441,7 @@
</goals>
</pluginExecutionFilter>
<action>
<ignore />
<ignore/>
</action>
</pluginExecution>
</pluginExecutions>
@ -633,9 +635,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&amp;hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&amp;hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy</referenceUrl>
<verbose>true</verbose>
<logging>debug</logging>
@ -682,7 +684,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration />
<configuration/>
</plugin>
</plugins>
</build>
@ -721,7 +723,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration />
<configuration/>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>

View File

@ -1,5 +1,7 @@
<?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>
@ -48,8 +50,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>
@ -481,7 +483,7 @@
</goals>
</pluginExecutionFilter>
<action>
<ignore />
<ignore/>
</action>
</pluginExecution>
<pluginExecution>
@ -497,7 +499,7 @@
</goals>
</pluginExecutionFilter>
<action>
<ignore />
<ignore/>
</action>
</pluginExecution>
</pluginExecutions>
@ -691,9 +693,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&amp;hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&amp;hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy</referenceUrl>
<verbose>true</verbose>
<logging>debug</logging>

View File

@ -83,10 +83,8 @@
</build>
<properties>
<maven-jar-plugin.version>3.3.0</maven-jar-plugin.version>
<jol-core.version>0.17</jol-core.version>
<maven-assembly-plugin.version>3.5.0</maven-assembly-plugin.version>
<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
</properties>
</project>

View File

@ -76,8 +76,6 @@
<!-- JSF -->
<com.sun.faces.version>2.2.14</com.sun.faces.version>
<javax.el.version>3.0.0</javax.el.version>
<!-- Maven War plugin -->
<maven-war-plugin.version>3.3.1</maven-war-plugin.version>
<!-- Other -->
<javax.annotation-api.version>1.3.1</javax.annotation-api.version>
</properties>

View File

@ -13,5 +13,6 @@ This module contains articles about JSON.
- [Getting a Value in JSONObject](https://www.baeldung.com/java-jsonobject-get-value)
- [Pretty-Print a JSON in Java](https://www.baeldung.com/java-json-pretty-print)
- [Remove Whitespaces From a JSON in Java](https://www.baeldung.com/java-json-minify-remove-whitespaces)
- [Programmatic Generation of JSON Schemas in Java](https://www.baeldung.com/java-json-schema-create-automatically)
- More Articles: [[<-- prev]](/json-modules/json)

View File

@ -0,0 +1,2 @@
## Relevant Articles
- [How to Check if a Value Exists in a JSON Array for a Particular Key](https://www.baeldung.com/java-json-array-check-key-value-pair)

View File

@ -1,7 +1,7 @@
<?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/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>json-conversion</artifactId>

View File

@ -61,8 +61,4 @@
</plugins>
</build>
<properties>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
</properties>
</project>

View File

@ -1,6 +1,7 @@
<?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 https://maven.apache.org/xsd/maven-4.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -92,7 +92,7 @@
<artifactId>suanshu</artifactId>
<version>${suanshu.version}</version>
</dependency>
-->
-->
<dependency>
<groupId>org.derive4j</groupId>
<artifactId>derive4j</artifactId>
@ -162,7 +162,7 @@
</plugin>
</plugins>
</build>
<!-- JAVA-24004
<repositories>
<repository>

View File

@ -73,7 +73,6 @@
<properties>
<jetty.version>9.4.27.v20200227</jetty.version>
<alpn.version>8.1.11.v20170118</alpn.version>
<maven-war-plugin.version>3.2.2</maven-war-plugin.version>
</properties>
</project>

View File

@ -291,7 +291,6 @@
<quartz.version>2.3.0</quartz.version>
<javaassist.version>3.29.2-GA</javaassist.version>
<jool.version>0.9.12</jool.version>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
<commons-net.version>3.6</commons-net.version>
<commonsio.version>2.6</commonsio.version>
</properties>

View File

@ -71,11 +71,11 @@
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
<version>${lombok.version}</version>
</dependency>
</dependency>
</dependencies>
<profiles>

View File

@ -7,8 +7,8 @@
<version>0.0.1-SNAPSHOT</version>
<name>maven-build-optimization</name>
<packaging>pom</packaging>
<parent>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>maven-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
@ -53,10 +53,10 @@
</build>
</profile>
</profiles>
<properties>
<failsafe.version>3.1.2</failsafe.version>
<profiler.version>1.7</profiler.version>
</properties>
<profiler.version>1.7</profiler.version>
</properties>
</project>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>business</artifactId>
<version>1.0-SNAPSHOT</version>
@ -24,5 +24,4 @@
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>resume-from</artifactId>

View File

@ -22,7 +22,7 @@
<module>spring-amqp</module>
<module>spring-apache-camel</module>
<module>spring-jms</module>
<module>postgres-notify</module>
<module>postgres-notify</module>
</modules>
</project>

View File

@ -42,37 +42,36 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>instance1</id>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Dserver.port=8081</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<profiles>
<profile>
<id>instance1</id>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Dserver.port=8081</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -22,8 +22,4 @@
<module>rest-express</module>
</modules>
<properties>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
</properties>
</project>

View File

@ -144,7 +144,6 @@
<Syntaxe.version>1.0</Syntaxe.version>
<repoexpress-mongodb.version>0.4.8</repoexpress-mongodb.version>
<junit4.version>4.11</junit4.version>
<exec-maven-plugin.version>1.2.1</exec-maven-plugin.version>
<maven-shade-plugin.version>2.4.1</maven-shade-plugin.version>
<versions-maven-plugin.version>2.0</versions-maven-plugin.version>
</properties>

View File

@ -217,6 +217,8 @@
<munit.version>1.3.6</munit.version>
<build-helper-maven-plugin.version>1.10</build-helper-maven-plugin.version>
<mule-maven-plugin.version>2.2.1</mule-maven-plugin.version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
<maven-install-plugin.version>2.5.2</maven-install-plugin.version>
</properties>
</project>

View File

@ -9,3 +9,4 @@
- [Get the Number of Rows in a ResultSet](https://www.baeldung.com/java-resultset-number-of-rows)
- [Converting a JDBC ResultSet to JSON in Java](https://www.baeldung.com/java-jdbc-convert-resultset-to-json)
- [Guide to MicroStream](https://www.baeldung.com/microstream-intro)
- [Executing SQL Script File in Java](https://www.baeldung.com/java-run-sql-script)

View File

@ -166,7 +166,6 @@
<javassist.version>3.27.0-GA</javassist.version>
<jaxb.version>2.3.1</jaxb.version>
<log4jdbc.version>2.0.0</log4jdbc.version>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
<maven.compiler.version>3.8.1</maven.compiler.version>
<maven.version>3.8.1</maven.version>
<mysql.version>8.0.19</mysql.version>

View File

@ -156,7 +156,6 @@
<properties>
<eclipselink.version>4.0.1</eclipselink.version>
<javax.persistence-api.version>2.2</javax.persistence-api.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-processor-plugin.version>3.3.3</maven-processor-plugin.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
<querydsl.version>5.0.0</querydsl.version>

View File

@ -88,7 +88,6 @@
<eclipselink.version>2.7.4</eclipselink.version>
<mysql.version>8.0.21</mysql.version>
<javax.persistence-api.version>2.2</javax.persistence-api.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-processor-plugin.version>3.3.3</maven-processor-plugin.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
<h2.version>2.1.214</h2.version>

View File

@ -85,7 +85,7 @@
<module>spring-data-jpa-query-3</module>
<module>spring-data-jpa-repo</module>
<module>spring-data-jpa-repo-2</module>
<module>spring-data-jpa-repo-4</module>
<module>spring-data-jpa-repo-4</module>
<module>spring-data-jdbc</module>
<module>spring-data-keyvalue</module>
<module>spring-data-mongodb</module>

View File

@ -1,7 +1,7 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.boot.persistence</groupId>
<artifactId>spring-boot-persistence-4</artifactId>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-shardingsphere</artifactId>
<version>1.0</version>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-yugabytedb</artifactId>
<version>1.0</version>

22
pom.xml
View File

@ -474,6 +474,7 @@
<module>apache-spark</module>
<module>jenkins-modules</module>
<module>jhipster-6</module>
<module>jhipster-modules</module>
</modules>
</profile>
@ -632,6 +633,7 @@
<module>apache-spark</module>
<module>jenkins-modules</module>
<module>jhipster-6</module>
<module>jhipster-modules</module>
</modules>
@ -786,7 +788,6 @@
<module>guava-modules</module>
<module>kubernetes-modules</module>
<module>libraries-concurrency</module>
<module>jhipster-6</module>
<module>libraries-testing</module>
<module>maven-modules</module>
<module>optaplanner</module>
@ -1058,7 +1059,6 @@
<module>guava-modules</module>
<module>kubernetes-modules</module>
<module>libraries-concurrency</module>
<module>jhipster-6</module>
<module>libraries-testing</module>
<module>maven-modules</module>
<module>optaplanner</module>
@ -1270,19 +1270,19 @@
<!-- plugins -->
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<exec-maven-plugin.version>3.0.0</exec-maven-plugin.version>
<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
<exec-maven-plugin.version>3.1.0</exec-maven-plugin.version>
<java.version>1.8</java.version>
<log4j.version>1.2.17</log4j.version>
<jmh-core.version>1.36</jmh-core.version>
<jmh-generator.version>1.36</jmh-generator.version>
<maven-failsafe-plugin.version>2.21.0</maven-failsafe-plugin.version>
<maven-failsafe-plugin.version>3.1.2</maven-failsafe-plugin.version>
<commons-collections4.version>4.4</commons-collections4.version>
<commons-io.version>2.13.0</commons-io.version>
<commons-lang.version>2.6</commons-lang.version>
<commons-lang3.version>3.13.0</commons-lang3.version>
<commons-cli.version>1.5.0</commons-cli.version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
<maven-war-plugin.version>3.4.0</maven-war-plugin.version>
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
<jstl-api.version>1.2</jstl-api.version>
<javax.servlet.jsp-api.version>2.3.3</javax.servlet.jsp-api.version>
@ -1292,16 +1292,16 @@
<junit-platform.version>1.9.2</junit-platform.version>
<junit-jupiter.version>5.9.2</junit-jupiter.version>
<junit-platform-surefire-provider.version>1.3.2</junit-platform-surefire-provider.version>
<directory-maven-plugin.version>0.3.1</directory-maven-plugin.version>
<maven-install-plugin.version>2.5.2</maven-install-plugin.version>
<directory-maven-plugin.version>1.0</directory-maven-plugin.version>
<maven-install-plugin.version>3.1.1</maven-install-plugin.version>
<custom-pmd.version>0.0.1</custom-pmd.version>
<gitflow-incremental-builder.version>3.12.2</gitflow-incremental-builder.version>
<maven-jxr-plugin.version>3.0.0</maven-jxr-plugin.version>
<maven-pmd-plugin.version>3.19.0</maven-pmd-plugin.version>
<maven-jxr-plugin.version>3.3.0</maven-jxr-plugin.version>
<maven-pmd-plugin.version>3.21.0</maven-pmd-plugin.version>
<lombok.version>1.18.28</lombok.version>
<h2.version>2.1.214</h2.version>
<guava.version>32.1.2-jre</guava.version>
<maven-jar-plugin.version>3.2.2</maven-jar-plugin.version>
<maven-jar-plugin.version>3.3.0</maven-jar-plugin.version>
</properties>
</project>

View File

@ -255,7 +255,6 @@
<testcontainers-bom.version>1.17.2</testcontainers-bom.version>
<java.version>11</java.version>
<spring-native.version>0.12.1</spring-native.version>
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
<maven.compiler.source.version>11</maven.compiler.source.version>
<maven.compiler.target.version>11</maven.compiler.target.version>
<maven-surefire-plugin.version>3.1.0</maven-surefire-plugin.version>

View File

@ -46,6 +46,5 @@
<properties>
<sentry.version>6.11.0</sentry.version>
<cargo-maven3-plugin.version>1.10.4</cargo-maven3-plugin.version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
</properties>
</project>

View File

@ -55,7 +55,6 @@
<properties>
<slack.version>1.4</slack.version>
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
</properties>
</project>

View File

@ -25,8 +25,4 @@
<module>sql-injection-samples</module>
</modules>
<properties>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
</properties>
</project>

View File

@ -46,7 +46,6 @@
<properties>
<undertow-servlet.version>1.4.18.Final</undertow-servlet.version>
<!--<maven-shade-plugin.version>2.4.3</maven-shade-plugin.version> -->
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
</properties>
</project>

View File

@ -44,7 +44,7 @@
<module>spring-boot-graphql</module>
<module>spring-boot-groovy</module>
<!-- <module>spring-boot-gradle</module> --> <!-- Not a maven project -->
<!--<module>spring-boot-jasypt</module>--> <!-- Fixing in JAVA-24011 -->
<module>spring-boot-jasypt</module>
<module>spring-boot-jsp</module>
<module>spring-boot-keycloak</module>
<module>spring-boot-keycloak-2</module>
@ -91,6 +91,7 @@
<module>spring-boot-3-native</module>
<module>spring-boot-3-observation</module>
<module>spring-boot-3-test-pitfalls</module>
<module>spring-boot-3-testcontainers</module>
<module>spring-boot-resilience4j</module>
<module>spring-boot-properties</module>
<module>spring-boot-properties-2</module>
@ -119,8 +120,4 @@
</dependencies>
</dependencyManagement>
<properties>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
</properties>
</project>

View File

@ -0,0 +1 @@
## Relevant Articles

View File

@ -0,0 +1,102 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-3-testcontainers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-3-testcontainers</name>
<description>Testcontainer Improvements in Spring Boot 3</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-3</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
<testcontainers.version>1.18.3</testcontainers.version>
<rest-assured.version>5.3.1</rest-assured.version>
</properties>
</project>

View File

@ -0,0 +1,14 @@
package com.baeldung.testcontainers;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.testcontainers.support;
import java.util.UUID;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document("characters")
public record MiddleEarthCharacter(@Id String id, String name, String race) {
public MiddleEarthCharacter(String name, String race) {
this(UUID.randomUUID().toString(), name, race);
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.testcontainers.support;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("characters")
public class MiddleEarthCharactersController {
private final MiddleEarthCharactersRepository repository;
public MiddleEarthCharactersController(MiddleEarthCharactersRepository repository) {
this.repository = repository;
}
@GetMapping
public List<MiddleEarthCharacter> findByRace(@RequestParam String race) {
return repository.findAllByRace(race);
}
@PostMapping
public MiddleEarthCharacter save(@RequestBody MiddleEarthCharacter character) {
return repository.save(character);
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.testcontainers.support;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MiddleEarthCharactersRepository extends MongoRepository<MiddleEarthCharacter, String> {
List<MiddleEarthCharacter> findAllByRace(String race);
}

View File

@ -0,0 +1,59 @@
package com.baeldung.testcontainers;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.hasItems;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import com.baeldung.testcontainers.support.MiddleEarthCharacter;
import com.baeldung.testcontainers.support.MiddleEarthCharactersRepository;
@Testcontainers
@SpringBootTest(webEnvironment = DEFINED_PORT)
@DirtiesContext(classMode = AFTER_CLASS)
class DynamicPropertiesIntegrationTest {
@Container
static MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10"));
@DynamicPropertySource
static void setProperties(DynamicPropertyRegistry registry) {
registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl);
}
@Autowired
private MiddleEarthCharactersRepository repository;
@BeforeEach
void beforeEach() {
repository.deleteAll();
}
@Test
void whenRequestingHobbits_thenReturnFrodoAndSam() {
repository.saveAll(List.of(
new MiddleEarthCharacter("Frodo", "hobbit"),
new MiddleEarthCharacter("Samwise", "hobbit"),
new MiddleEarthCharacter("Aragon", "human"),
new MiddleEarthCharacter("Gandalf", "wizzard")
));
when().get("/characters?race=hobbit")
.then().statusCode(200)
.and().body("name", hasItems("Frodo", "Samwise"));
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.testcontainers;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.devtools.restart.RestartScope;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.utility.DockerImageName;
class LocalDevApplication {
public static void main(String[] args) {
SpringApplication.from(Application::main)
.with(LocalDevTestcontainersConfig.class)
.run(args);
}
@TestConfiguration(proxyBeanMethods = false)
static class LocalDevTestcontainersConfig {
@Bean
@RestartScope
@ServiceConnection
public MongoDBContainer mongoDBContainer() {
return new MongoDBContainer(DockerImageName.parse("mongo:4.0.10"));
}
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.testcontainers;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.hasItems;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.test.annotation.DirtiesContext;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import com.baeldung.testcontainers.support.MiddleEarthCharacter;
import com.baeldung.testcontainers.support.MiddleEarthCharactersRepository;
@Testcontainers
@SpringBootTest(webEnvironment = DEFINED_PORT)
@DirtiesContext(classMode = AFTER_CLASS)
class ServiceConnectionIntegrationTest {
@Container
@ServiceConnection
static MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10"));
@Autowired
private MiddleEarthCharactersRepository repository;
@BeforeEach
void beforeEach() {
repository.deleteAll();
}
@Test
void whenRequestingHobbits_thenReturnFrodoAndSam() {
repository.saveAll(List.of(
new MiddleEarthCharacter("Frodo", "hobbit"),
new MiddleEarthCharacter("Samwise", "hobbit"),
new MiddleEarthCharacter("Aragon", "human"),
new MiddleEarthCharacter("Gandalf", "wizzard")
));
when().get("/characters?race=hobbit")
.then().statusCode(200)
.and().body("name", hasItems("Frodo", "Samwise"));
}
}

View File

@ -1,7 +1,7 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>

View File

@ -193,7 +193,6 @@
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
<subethasmtp.version>3.1.7</subethasmtp.version>
<httpclient.version>4.5.8</httpclient.version>
<maven-failsafe-plugin.version>2.18</maven-failsafe-plugin.version>
</properties>
</project>

View File

@ -86,5 +86,4 @@
<start-class>com.baeldung.changeport.CustomApplication</start-class>
</properties>
</project>

View File

@ -92,7 +92,6 @@
</build>
<properties>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
<swagger-core.version>2.2.11</swagger-core.version>
<springwolf-kafka.version>0.12.1</springwolf-kafka.version>
<springwolf-ui.version>0.8.0</springwolf-ui.version>

View File

@ -50,4 +50,11 @@
<jasypt.version>2.0.0</jasypt.version>
</properties>
<repositories>
<repository>
<id>spring-milestone</id>
<name>Spring Milestone</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>

Some files were not shown because too many files have changed in this diff Show More