Merge branch 'eugenp:master' into danielmcnally285_last_n_characters

This commit is contained in:
danielmcnally285 2024-03-19 19:59:07 +00:00 committed by GitHub
commit ab35aeabf7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
199 changed files with 1933 additions and 437 deletions

View File

@ -4,8 +4,8 @@
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>akka-modules</artifactId>
<name>akka-modules</name>
<packaging>pom</packaging>
<name>akka-modules</name>
<parent>
<artifactId>parent-modules</artifactId>

View File

@ -4,8 +4,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>cxf-spring</artifactId>
<name>cxf-spring</name>
<packaging>war</packaging>
<name>cxf-spring</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,9 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-cxf-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>apache-cxf-modules</name>
<packaging>pom</packaging>
<name>apache-cxf-modules</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>sse-jaxrs</artifactId>
<name>sse-jaxrs</name>
<packaging>pom</packaging>
<name>sse-jaxrs</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>sse-jaxrs-server</artifactId>
<name>sse-jaxrs-server</name>
<packaging>war</packaging>
<name>sse-jaxrs-server</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-httpclient4</artifactId>
<version>0.1-SNAPSHOT</version>
<name>apache-httpclient4</name>
<packaging>war</packaging>
<name>apache-httpclient4</name>
<parent>
<groupId>com.baeldung</groupId>
@ -193,7 +193,6 @@
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>

View File

@ -24,14 +24,12 @@
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-junit5</artifactId>
<version>${camel.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-main</artifactId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-spark</artifactId>
<version>1.0-SNAPSHOT</version>
<name>apache-spark</name>
<packaging>jar</packaging>
<name>apache-spark</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-thrift</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>apache-thrift</name>
<packaging>pom</packaging>
<name>apache-thrift</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-velocity</artifactId>
<version>0.1-SNAPSHOT</version>
<name>apache-velocity</name>
<packaging>war</packaging>
<name>apache-velocity</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>aws-dynamodb</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>aws-dynamodb</name>
<packaging>jar</packaging>
<name>aws-dynamodb</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>lambda-function</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>lambda-function</name>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>

View File

@ -4,8 +4,8 @@
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-lambda-modules</artifactId>
<name>aws-lambda-modules</name>
<packaging>pom</packaging>
<name>aws-lambda-modules</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -3,8 +3,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>ShippingFunction</artifactId>
<version>1.0</version>
<name>ShippingFunction</name>
<version>1.0</version>
<packaging>jar</packaging>
<parent>

View File

@ -4,8 +4,8 @@
<modelVersion>4.0.0</modelVersion>
<groupId>helloworld</groupId>
<artifactId>ToDoFunction</artifactId>
<version>1.0</version>
<name>ToDoFunction</name>
<version>1.0</version>
<packaging>jar</packaging>
<parent>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>aws-miscellaneous</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>aws-miscellaneous</name>
<packaging>jar</packaging>
<name>aws-miscellaneous</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>aws-s3</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>aws-s3</name>
<packaging>jar</packaging>
<name>aws-s3</name>
<parent>
<groupId>com.baeldung</groupId>
@ -26,7 +26,6 @@
<version>${aws.java.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
@ -56,7 +55,6 @@
<version>${com.adobe.testing.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>

View File

@ -4,6 +4,7 @@
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-modules</artifactId>
<packaging>pom</packaging>
<name>aws-modules</name>
<parent>
@ -12,16 +13,6 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.12.523</version>
<scope>compile</scope>
</dependency>
</dependencies>
<packaging>pom</packaging>
<modules>
<module>aws-app-sync</module>
<module>aws-dynamodb</module>
@ -31,6 +22,15 @@
<module>aws-s3</module>
</modules>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.12.523</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<aws-java-sdk.version>1.12.331</aws-java-sdk.version>
<aws-java-sdk-v2.version>2.24.9</aws-java-sdk-v2.version>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>azure</artifactId>
<version>0.1</version>
<name>azure</name>
<packaging>war</packaging>
<name>azure</name>
<description>Demo project for Spring Boot on Azure</description>
<parent>

View File

@ -0,0 +1,45 @@
package com.baeldung.checkiftimebetweentwotimes;
import org.junit.Test;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import static org.junit.Assert.assertTrue;
public class CheckIfTimeBetweenTwoTimesUnitTest {
private LocalTime startTime = LocalTime.parse("09:00:00");
private LocalTime endTime = LocalTime.parse("17:00:00");
private LocalTime targetTime = LocalTime.parse("12:30:00");
@Test
public void givenLocalTime_whenUsingIsAfterIsBefore_thenTimeIsBetween() {
assertTrue(!targetTime.isBefore(startTime) && !targetTime.isAfter(endTime));
}
@Test
public void givenLocalTime_whenUsingCompareTo_thenTimeIsBetween() {
assertTrue(targetTime.compareTo(startTime) >= 0 && targetTime.compareTo(endTime) <= 0);
}
@Test
public void givenDate_whenUsingAfterBefore_thenTimeIsBetween() {
Calendar startCalendar = Calendar.getInstance();
startCalendar.set(Calendar.HOUR_OF_DAY, 9);
startCalendar.set(Calendar.MINUTE, 0);
Date startTime = startCalendar.getTime();
Calendar endCalendar = Calendar.getInstance();
endCalendar.set(Calendar.HOUR_OF_DAY, 17);
endCalendar.set(Calendar.MINUTE, 0);
Date endTime = endCalendar.getTime();
Calendar targetCalendar = Calendar.getInstance();
targetCalendar.set(Calendar.HOUR_OF_DAY, 12);
targetCalendar.set(Calendar.MINUTE, 30);
Date targetTime = targetCalendar.getTime();
assertTrue(!targetTime.before(startTime) && !targetTime.after(endTime));
}
}

View File

@ -0,0 +1,3 @@
=========
### Relevant articles:

View File

@ -0,0 +1,15 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-lang-math-4</artifactId>
<name>core-java-lang-math-4</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,78 @@
package com.baeldung.percentile;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
public class CalculatePercentileUnitTest {
public static <T extends Comparable<T>> T getPercentile(Collection<T> input, double percentile) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("The input dataset cannot be null or empty.");
}
if (percentile < 0 || percentile > 100) {
throw new IllegalArgumentException("Percentile must be between 0 and 100(exclusive)");
}
List<T> sortedList = input.stream()
.sorted()
.collect(Collectors.toList());
int rank = percentile == 0 ? 1 : (int) Math.ceil(percentile / 100.0 * input.size());
return sortedList.get(rank - 1);
}
@Test
void whenCallingGetPercentileWithAList_thenGetExpectedResult() {
assertThrows(IllegalArgumentException.class, () -> getPercentile(List.of(1, 2, 3), -1));
assertThrows(IllegalArgumentException.class, () -> getPercentile(List.of(1, 2, 3), 101));
List<Integer> list100 = IntStream.rangeClosed(1, 100)
.boxed()
.collect(Collectors.toList());
Collections.shuffle(list100);
assertEquals(1, getPercentile(list100, 0));
assertEquals(10, getPercentile(list100, 10));
assertEquals(25, getPercentile(list100, 25));
assertEquals(50, getPercentile(list100, 50));
assertEquals(76, getPercentile(list100, 75.3));
assertEquals(100, getPercentile(list100, 100));
List<Integer> list8 = IntStream.of(-1, 200, 30, 42, -5, 7, 8, 92)
.boxed()
.collect(Collectors.toList());
assertEquals(-5, getPercentile(list8, 0));
assertEquals(-5, getPercentile(list8, 10));
assertEquals(-1, getPercentile(list8, 25));
assertEquals(8, getPercentile(list8, 50));
assertEquals(92, getPercentile(list8, 75.3));
assertEquals(200, getPercentile(list8, 100));
}
@Test
void whenCallingGetPercentileWithAnArray_thenGetExpectedResult() {
long[] theArray = new long[] { -1, 200, 30, 42, -5, 7, 8, 92 };
//convert the long[] array to a List<Long>
List<Long> list8 = Arrays.stream(theArray)
.boxed()
.toList();
assertEquals(-5, getPercentile(list8, 0));
assertEquals(-5, getPercentile(list8, 10));
assertEquals(-1, getPercentile(list8, 25));
assertEquals(8, getPercentile(list8, 50));
assertEquals(92, getPercentile(list8, 75.3));
assertEquals(200, getPercentile(list8, 100));
}
}

View File

@ -0,0 +1,66 @@
package com.baeldung.integersatbitlevel;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
class IntegersBitLevelUnitTest {
@Test
void givenNumbers_whenBitwiseAND_thenResultIsExpected() {
int result = 0b1100 & 0b0111;
assertEquals(0b0100, result);
}
@Test
void givenNumbers_whenBitwiseOR_thenResultIsExpected() {
int result = 0b1100 | 0b0111;
assertEquals(0b1111, result);
}
@Test
void givenNumbers_whenBitwiseXOR_thenResultIsExpected() {
int result = 0b1100 ^ 0b0111;
assertEquals(0b1011, result);
}
@Test
void givenNumber_whenBitwiseNOT_thenResultIsExpected() {
int result = ~0b0101;
assertEquals(-0b0110, result);
}
@Test
void givenNumber_whenBitwiseLeftShift_thenResultIsExpected() {
int result = 0b0101 << 2;
assertEquals(0b10100, result);
}
@Test
void givenNumber_whenBitwiseRightShift_thenResultIsExpected() {
int result = 0b0101 >> 1;
assertEquals(0b10, result);
}
@Test
void givenOriginalColor_whenApplyingMask_thenObtainModifiedColor() {
int originalColor = 0xFF336699;
int alphaMask = 0xFF000000;
int redMask = 0x00FF0000;
int greenMask = 0x0000FF00;
int blueMask = 0x000000FF;
int alpha = (originalColor & alphaMask) >>> 24;
int red = (originalColor & redMask) >>> 16;
int green = (originalColor & greenMask) >>> 8;
int blue = originalColor & blueMask;
red = Math.min(255, red + 50);
green = Math.min(255, green + 30);
int modifiedColor = (alpha << 24) | (red << 16) | (green << 8) | blue;
assertEquals(-10124135, modifiedColor);
}
}

View File

@ -55,7 +55,7 @@
</build>
<properties>
<validator.version>1.7</validator.version>
<validator.version>1.8.0</validator.version>
<emoji-java.version>5.1.1</emoji-java.version>
<apache-commons-text.version>1.10.0</apache-commons-text.version>
</properties>

View File

@ -144,6 +144,7 @@
<module>core-java-lang-6</module>
<module>core-java-lang-math</module>
<module>core-java-lang-math-2</module>
<module>core-java-lang-math-4</module>
<module>core-java-lang-oop-constructors</module>
<module>core-java-lang-oop-patterns</module>
<module>core-java-lang-oop-generics</module>

View File

@ -4,8 +4,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>deeplearning4j</artifactId>
<name>deeplearning4j</name>
<packaging>jar</packaging>
<name>deeplearning4j</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>guice</artifactId>
<name>guice</name>
<packaging>jar</packaging>
<name>guice</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>di-modules</artifactId>
<name>di-modules</name>
<packaging>pom</packaging>
<name>di-modules</name>
<parent>
<artifactId>parent-modules</artifactId>

View File

@ -4,8 +4,8 @@
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>disruptor</artifactId>
<name>disruptor</name>
<packaging>jar</packaging>
<name>disruptor</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -6,8 +6,8 @@
<groupId>com.baeldung</groupId>
<artifactId>multi-module-caching</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Multi-module Maven caching example</description>
<packaging>pom</packaging>
<description>Multi-module Maven caching example</description>
<modules>
<module>runner-module</module>

View File

@ -4,8 +4,8 @@
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>docker-caching</artifactId>
<name>docker-caching</name>
<packaging>pom</packaging>
<name>docker-caching</name>
<parent>
<artifactId>docker-modules</artifactId>

View File

@ -4,8 +4,8 @@
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>docker-compose-2</artifactId>
<description>Demo project for Spring Boot and Docker - Module docker-compose-2</description>
<packaging>pom</packaging>
<description>Demo project for Spring Boot and Docker - Module docker-compose-2</description>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>geotools</artifactId>
<name>geotools</name>
<packaging>jar</packaging>
<name>geotools</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>google-auto-project</artifactId>
<version>1.0</version>
<name>google-auto-project</name>
<packaging>pom</packaging>
<name>google-auto-project</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>google-cloud</artifactId>
<name>google-cloud</name>
<packaging>jar</packaging>
<name>google-cloud</name>
<description>Google Cloud Tutorials</description>
<parent>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.graphql</groupId>
<artifactId>graphql-modules</artifactId>
<name>graphql-modules</name>
<packaging>pom</packaging>
<name>graphql-modules</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>grpc</artifactId>
<name>grpc</name>
<packaging>jar</packaging>
<name>grpc</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,9 +4,9 @@
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>guava-modules</artifactId>
<name>guava-modules</name>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>guava-modules</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>httpclient-simple</artifactId>
<name>httpclient-simple</name>
<packaging>war</packaging>
<name>httpclient-simple</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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-jr</artifactId>
<name>jackson-jr</name>
<packaging>pom</packaging>
<name>jackson-jr</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,9 +4,9 @@
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-modules</artifactId>
<name>jackson-modules</name>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>jackson-modules</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.blockchain</groupId>
<artifactId>java-blockchain</artifactId>
<name>java-blockchain</name>
<packaging>jar</packaging>
<name>java-blockchain</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>java-jdi</artifactId>
<name>java-jdi</name>
<packaging>jar</packaging>
<name>java-jdi</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<groupId>com.baeldung.java.panama</groupId>
<artifactId>java-panama</artifactId>
<version>1.0</version>
<name>java-panama</name>
<packaging>jar</packaging>
<name>java-panama</name>
<dependencies>
<dependency>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.javax-sound</groupId>
<artifactId>javax-sound</artifactId>
<name>javax-sound</name>
<packaging>jar</packaging>
<name>javax-sound</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>jenkins-modules</artifactId>
<name>jenkins-modules</name>
<packaging>pom</packaging>
<name>jenkins-modules</name>
<parent>
<artifactId>parent-modules</artifactId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>jetbrains</artifactId>
<version>1.0-SNAPSHOT</version>
<name>jetbrains</name>
<packaging>jar</packaging>
<name>jetbrains</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>jgit</artifactId>
<name>jgit</name>
<packaging>jar</packaging>
<name>jgit</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<groupId>com.baeldung.jhipster</groupId>
<artifactId>jhipster-6</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>jhipster-6</name>
<packaging>pom</packaging>
<name>jhipster-6</name>
<parent>
<artifactId>parent-boot-2</artifactId>

View File

@ -4,8 +4,8 @@
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>jhipster-uaa</artifactId>
<name>jhipster-uaa</name>
<packaging>pom</packaging>
<name>jhipster-uaa</name>
<parent>
<artifactId>jhipster-modules</artifactId>

View File

@ -6,8 +6,8 @@
<groupId>com.baeldung.jhipster</groupId>
<artifactId>jhipster-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>jhipster-modules</name>
<packaging>pom</packaging>
<name>jhipster-modules</name>
<parent>
<!-- Jhipster project is autogenerated and upgrading it will need a newly generated project.

View File

@ -4,8 +4,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jmh</artifactId>
<name>jmh</name>
<packaging>jar</packaging>
<name>jmh</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -29,6 +29,11 @@
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>

View File

@ -0,0 +1,84 @@
package com.baeldung.exceltojson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONArray;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static org.junit.Assert.assertEquals;
public class ExcelToJsonUnitTest {
public static String filePath = Objects.requireNonNull(ExcelToJsonUnitTest.class.getClassLoader().getResource("Book1.xlsx")).getFile();
public String expectedJson = "[[\"C1\",\"C2\",\"C3\",\"C4\",\"C5\"]," +
"[\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"]," +
"[\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"]," +
"[\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"]," +
"[\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"]]";
private Workbook workbook;
private Sheet sheet;
private InputStream inputStream;
public ExcelToJsonUnitTest() throws IOException {
inputStream = new FileInputStream(filePath);
workbook = new XSSFWorkbook(inputStream);
sheet = workbook.getSheetAt(0);
}
@Test
public void givenExcelFile_whenUsingApachePOIConversion_thenConvertToJson() {
JSONArray jsonArray = new JSONArray();
Row headerRow = sheet.getRow(0);
List<String> headers = new ArrayList<>();
for (Cell cell : headerRow) {
headers.add(cell.toString());
}
jsonArray.put(headers);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
List<String> rowData = new ArrayList<>();
for (Cell cell : row) {
rowData.add(cell.toString());
}
jsonArray.put(rowData);
}
assertEquals(expectedJson, jsonArray.toString());
}
@Test
public void givenExcelFile_whenUsingJacksonConversion_thenConvertToJson() throws JsonProcessingException {
List<List<String>> data = new ArrayList<>();
Row headerRow = sheet.getRow(0);
List<String> headers = new ArrayList<>();
for (Cell cell : headerRow) {
headers.add(cell.toString());
}
data.add(headers);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
List<String> rowData = new ArrayList<>();
for (Cell cell : row) {
rowData.add(cell.toString());
}
data.add(rowData);
}
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(data);
assertEquals(expectedJson, json);
}
}

View File

@ -4,8 +4,8 @@
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>json-modules</artifactId>
<name>json-modules</name>
<packaging>pom</packaging>
<name>json-modules</name>
<parent>
<artifactId>parent-modules</artifactId>

View File

@ -4,8 +4,8 @@
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>jsoup</artifactId>
<name>jsoup</name>
<packaging>jar</packaging>
<name>jsoup</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jws</artifactId>
<name>jws</name>
<packaging>war</packaging>
<name>jws</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>k8s-operator</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>k8s-operator</name>
<packaging>jar</packaging>
<name>k8s-operator</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries-bytecode</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>libraries-bytecode</name>
<packaging>jar</packaging>
<name>libraries-bytecode</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries-cli</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>libraries-cli</name>
<packaging>jar</packaging>
<name>libraries-cli</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -121,7 +121,6 @@
<artifactId>flexy-micrometer-metrics</artifactId>
<version>${flexy-pool.version}</version>
</dependency>
<dependency>
<groupId>com.vladmihalcea.flexy-pool</groupId>
<artifactId>flexy-hikaricp</artifactId>

View File

@ -3,15 +3,15 @@
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>libraries-data-io-2</artifactId>
<name>libraries-data-io-3</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>libraries-data-io-2</artifactId>
<name>libraries-data-io-3</name>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>

View File

@ -165,7 +165,6 @@
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<build>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries-reporting</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>libraries-reporting</name>
<packaging>jar</packaging>
<name>libraries-reporting</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries-rpc</artifactId>
<name>libraries-rpc</name>
<packaging>jar</packaging>
<name>libraries-rpc</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>libraries-security</artifactId>
<name>libraries-security</name>
<packaging>jar</packaging>
<name>libraries-security</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries-server-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>libraries-server-2</name>
<packaging>war</packaging>
<name>libraries-server-2</name>
<parent>
<groupId>com.baeldung</groupId>
@ -35,7 +35,6 @@
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</dependency>
</dependencies>
<build>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries-stream</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>libraries-stream</name>
<packaging>jar</packaging>
<name>libraries-stream</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -17,7 +17,6 @@
<artifactId>spoon-core</artifactId>
<version>${spoon-core.version}</version>
</dependency>
</dependencies>
<properties>

View File

@ -6,9 +6,9 @@
<groupId>com.baelduung</groupId>
<artifactId>lightrun</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>lightrun</name>
<description>Services for LightRun Article</description>
<packaging>pom</packaging>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>log-mdc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>log-mdc</name>
<packaging>war</packaging>
<name>log-mdc</name>
<description>tutorial on logging with MDC and NDC</description>
<parent>

View File

@ -4,8 +4,8 @@
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>lombok-custom</artifactId>
<version>0.1-SNAPSHOT</version>
<name>lombok-custom</name>
<version>0.1-SNAPSHOT</version>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>lombok-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>lombok-modules</name>
<packaging>pom</packaging>
<name>lombok-modules</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>mapstruct</artifactId>
<version>1.0</version>
<name>mapstruct</name>
<packaging>jar</packaging>
<name>mapstruct</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>animal-sniffer-mvn-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<name>animal-sniffer-mvn-plugin</name>
<packaging>jar</packaging>
<name>animal-sniffer-mvn-plugin</name>
<url>http://maven.apache.org</url>
<parent>

View File

@ -4,8 +4,8 @@
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-exclusions</artifactId>
<name>core-java-exclusions</name>
<packaging>jar</packaging>
<name>core-java-exclusions</name>
<parent>
<groupId>com.baeldung.dependency-exclusion</groupId>
@ -19,7 +19,6 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.dependency-exclusion</groupId>
<artifactId>dependency-exclusion</artifactId>
<name>dependency-exclusion</name>
<packaging>pom</packaging>
<name>dependency-exclusion</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.jacoco-coverage-aggregation</groupId>
<artifactId>jacoco-coverage-aggregate-report</artifactId>
<name>jacoco-coverage-aggregate-report</name>
<packaging>pom</packaging>
<name>jacoco-coverage-aggregate-report</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.jacoco-coverage-aggregation</groupId>
<artifactId>jacoco-coverage-controllers-example</artifactId>
<name>jacoco-coverage-controllers-example</name>
<packaging>jar</packaging>
<name>jacoco-coverage-controllers-example</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.jacoco-coverage-aggregation</groupId>
<artifactId>jacoco-coverage-services-example</artifactId>
<name>jacoco-coverage-services-example</name>
<packaging>jar</packaging>
<name>jacoco-coverage-services-example</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>jacoco-coverage-aggregation</artifactId>
<version>1.0</version>
<name>jacoco-coverage-aggregation</name>
<packaging>pom</packaging>
<name>jacoco-coverage-aggregation</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -6,8 +6,8 @@
<groupId>com.baeldung.archetypes</groupId>
<artifactId>maven-archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<name>maven-archetype</name>
<packaging>maven-archetype</packaging>
<name>maven-archetype</name>
<description>Archetype used to generate rest application based on jaxrs 2.1</description>
<build>

View File

@ -3,19 +3,14 @@
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>maven-build-lifecycle</artifactId>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>maven-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>maven-build-lifecycle</artifactId>
<properties>
<maven.surefire.version>3.1.2</maven.surefire.version>
<maven.failsafe.version>3.1.2</maven.failsafe.version>
<junit.jupiter.version>5.3.1</junit.jupiter.version>
</properties>
<build>
<plugins>
<!-- Maven Surefire Plugin for unit tests -->
@ -49,4 +44,10 @@
</plugins>
</build>
<properties>
<maven.surefire.version>3.1.2</maven.surefire.version>
<maven.failsafe.version>3.1.2</maven.failsafe.version>
<junit.jupiter.version>5.3.1</junit.jupiter.version>
</properties>
</project>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-build-optimization</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maven-build-optimization</name>
<packaging>pom</packaging>
<name>maven-build-optimization</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-copy-files</artifactId>
<version>1.0-SNAPSHOT</version>
<name>maven-copy-files</name>
<packaging>pom</packaging>
<name>maven-copy-files</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

View File

@ -6,8 +6,8 @@
<groupId>com.baeldung</groupId>
<artifactId>counter-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>counter-maven-plugin</name>
<packaging>maven-plugin</packaging>
<name>counter-maven-plugin</name>
<url>http://maven.apache.org</url>
<organization>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-custom-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maven-custom-plugin</name>
<packaging>pom</packaging>
<name>maven-custom-plugin</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-generate-war</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maven-generate-war</name>
<packaging>war</packaging>
<name>maven-generate-war</name>
<description>Spring boot project to demonstrate war file generation</description>
<parent>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-integration-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maven-integration-test</name>
<packaging>war</packaging>
<name>maven-integration-test</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-polyglot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maven-polyglot</name>
<packaging>pom</packaging>
<name>maven-polyglot</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -6,11 +6,13 @@
<artifactId>patient-web</artifactId>
<version>1.0-SNAPSHOT</version>
<name>patient-web</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>maven-reactor</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.baeldung</groupId>

View File

@ -85,7 +85,7 @@
<properties>
<dropwizard.metrics.version>4.2.17</dropwizard.metrics.version>
<netflix.servo.version>0.13.2</netflix.servo.version>
<micrometer-registry-atlas.version>1.11.0</micrometer-registry-atlas.version>
<micrometer-registry-atlas.version>1.12.3</micrometer-registry-atlas.version>
<spring-boot-starter-web.version>3.1.0</spring-boot-starter-web.version>
<metrics-aspectj-deps.version>1.2.0</metrics-aspectj-deps.version>
<spectator-api.version>1.7.7</spectator-api.version>

View File

@ -9,7 +9,9 @@ import static org.junit.Assert.assertSame;
import jakarta.persistence.PersistenceException;
import org.h2.tools.RunScript;
import org.hibernate.HibernateException;
import org.hibernate.ReplicationMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
@ -24,35 +26,58 @@ import org.junit.Test;
import com.baeldung.persistence.model.Person;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* Testing specific implementation details for different methods:
* persist, save, merge, update, saveOrUpdate.
* persist, save, merge, update, saveOrUpdate, refresh, replicate
*/
public class SaveMethodsIntegrationTest {
private static SessionFactory sessionFactory;
private static SessionFactory sessionFactory1;
private Session session;
private static SessionFactory sessionFactory2;
private Session session1;
private Session session2;
private boolean doNotCommit = false;
@BeforeClass
public static void beforeTests() {
private static SessionFactory createSessionFactoryAndInitializeDBs(String dbUrl) throws Exception {
Configuration configuration = new Configuration().addAnnotatedClass(Person.class)
.setProperty("hibernate.dialect", HSQLDialect.class.getName())
.setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName())
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test")
.setProperty("hibernate.connection.url", dbUrl)
.setProperty("hibernate.connection.username", "sa")
.setProperty("hibernate.connection.password", "")
.setProperty("hibernate.hbm2ddl.auto", "update");
Connection connection = DriverManager.getConnection(dbUrl, "sa", "");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
.build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
SessionFactory sf = configuration.buildSessionFactory(serviceRegistry);
try (InputStream h2InitStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("h2-trigger.sql")) {
assert h2InitStream != null;
try (InputStreamReader h2InitReader = new InputStreamReader(h2InitStream)) {
RunScript.execute(connection, h2InitReader);
}
}
return sf;
}
@BeforeClass
public static void beforeTests() throws Exception{
sessionFactory1 = createSessionFactoryAndInitializeDBs("jdbc:hsqldb:mem:test");
sessionFactory2 = createSessionFactoryAndInitializeDBs("jdbc:hsqldb:mem:test2");
}
@Before
public void setUp() {
session = sessionFactory.openSession();
session.beginTransaction();
public void setUp() throws Exception {
session1 = sessionFactory1.openSession();
session1.beginTransaction();
doNotCommit = false;
}
@ -61,16 +86,16 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
session.persist(person);
session1.persist(person);
session.getTransaction()
session1.getTransaction()
.commit();
session.close();
session1.close();
session = sessionFactory.openSession();
session.beginTransaction();
session1 = sessionFactory1.openSession();
session1.beginTransaction();
assertNotNull(session.get(Person.class, person.getId()));
assertNotNull(session1.get(Person.class, person.getId()));
}
@ -80,10 +105,10 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
session.persist(person);
session1.persist(person);
Long id1 = person.getId();
session.persist(person);
session1.persist(person);
Long id2 = person.getId();
assertEquals(id1, id2);
@ -96,10 +121,10 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
session.persist(person);
session.evict(person);
session1.persist(person);
session1.evict(person);
session.persist(person);
session1.persist(person);
}
@Test
@ -107,12 +132,12 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
session.save(person);
session.flush();
session.evict(person);
session1.save(person);
session1.flush();
session1.evict(person);
person.setName("Mary");
Person mergedPerson = (Person) session.merge(person);
Person mergedPerson = (Person) session1.merge(person);
assertNotSame(person, mergedPerson);
assertEquals("Mary", mergedPerson.getName());
@ -127,20 +152,20 @@ public class SaveMethodsIntegrationTest {
assertNull(person.getId());
Long id = (Long) session.save(person);
Long id = (Long) session1.save(person);
assertNotNull(id);
session.getTransaction()
session1.getTransaction()
.commit();
session.close();
session1.close();
assertEquals(id, person.getId());
session = sessionFactory.openSession();
session.beginTransaction();
session1 = sessionFactory1.openSession();
session1.beginTransaction();
assertNotNull(session.get(Person.class, person.getId()));
assertNotNull(session1.get(Person.class, person.getId()));
}
@ -149,8 +174,8 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
Long id1 = (Long) session.save(person);
Long id2 = (Long) session.save(person);
Long id1 = (Long) session1.save(person);
Long id2 = (Long) session1.save(person);
assertEquals(id1, id2);
}
@ -160,10 +185,10 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
Long id1 = (Long) session.save(person);
session.evict(person);
Long id1 = (Long) session1.save(person);
session1.evict(person);
Long id2 = (Long) session.save(person);
Long id2 = (Long) session1.save(person);
assertNotEquals(id1, id2);
}
@ -173,11 +198,11 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
Person mergedPerson = (Person) session.merge(person);
Person mergedPerson = (Person) session1.merge(person);
session.getTransaction()
session1.getTransaction()
.commit();
session.beginTransaction();
session1.beginTransaction();
assertNotNull(person.getId());
assertNotNull(mergedPerson.getId());
@ -189,9 +214,9 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
session.save(person);
session1.save(person);
Person mergedPerson = (Person) session.merge(person);
Person mergedPerson = (Person) session1.merge(person);
assertSame(person, mergedPerson);
@ -202,11 +227,11 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
session.save(person);
session.evict(person);
session1.save(person);
session1.evict(person);
person.setName("Mary");
session.update(person);
session1.update(person);
assertEquals("Mary", person.getName());
}
@ -216,7 +241,7 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
session.update(person);
session1.update(person);
}
@ -225,9 +250,9 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
session.save(person);
session1.save(person);
session.update(person);
session1.update(person);
}
@ -236,11 +261,11 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
session.save(person);
session.evict(person);
session1.save(person);
session1.evict(person);
person.setName("Mary");
session.saveOrUpdate(person);
session1.saveOrUpdate(person);
assertEquals("Mary", person.getName());
}
@ -250,16 +275,75 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
session.saveOrUpdate(person);
session1.saveOrUpdate(person);
session.getTransaction()
session1.getTransaction()
.commit();
session.close();
session1.close();
session = sessionFactory.openSession();
session.beginTransaction();
session1 = sessionFactory1.openSession();
session1.beginTransaction();
assertNotNull(session.get(Person.class, person.getId()));
assertNotNull(session1.get(Person.class, person.getId()));
}
@Test
public void whenSaveAndTriggerUpdatedAndRefresh_thenRefreshPersistentEntity() {
Person person = new Person();
person.setName("Zeeshan Arif");
session1.persist(person);
session1.getTransaction()
.commit();
session1.close();
session1 = sessionFactory1.openSession();
session1.beginTransaction();
session1.refresh(person);
session1.getTransaction()
.commit();
session1.close();
session1 = sessionFactory1.openSession();
session1.beginTransaction();
assertEquals(person.getName(), "Neymar Santos");
}
@Test
public void whenReplicate_thenRefreshPersistentEntity() {
Person p = new Person();
p.setName("Ronaldinho Gaucho");
session1.persist(p);
session1.getTransaction()
.commit();
session1.close();
Session session2 = sessionFactory2.openSession();
session2.beginTransaction();
session2.replicate(p, ReplicationMode.LATEST_VERSION);
session2.getTransaction()
.commit();
session2.close();
session2 = sessionFactory2.openSession();
session2.beginTransaction();
Person actual = session2.get(Person.class, p.getId());
session2.getTransaction().commit();
session2.close();
session1 = sessionFactory1.openSession();
session1.beginTransaction();
Person expected = session1.get(Person.class, p.getId());
session1.getTransaction().commit();
session1.close();
session1 = sessionFactory1.openSession();
session1.beginTransaction();
assertEquals(expected.getId(), actual.getId());
assertEquals(expected.getName(), actual.getName());
}
@ -268,24 +352,25 @@ public class SaveMethodsIntegrationTest {
Person person = new Person();
person.setName("John");
session.save(person);
session1.save(person);
session.saveOrUpdate(person);
session1.saveOrUpdate(person);
}
@After
public void tearDown() {
if (!doNotCommit) {
session.getTransaction()
session1.getTransaction()
.commit();
}
session.close();
session1.close();
}
@AfterClass
public static void afterTests() {
sessionFactory.close();
sessionFactory1.close();
sessionFactory2.close();
}
}

View File

@ -0,0 +1,5 @@
CREATE TRIGGER IF NOT EXISTS TGR_UpdatePersonName AFTER INSERT
ON Person FOR EACH ROW UPDATE PERSON SET NAME = 'Neymar Santos' where NAME = 'Zeeshan Arif'

View File

@ -15,6 +15,18 @@
</parent>
<dependencies>
<!-- Spring Data -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>${spring-boot.version}</version>
</dependency>
<!-- QueryDSL -->
<dependency>
<groupId>com.querydsl</groupId>
@ -36,6 +48,16 @@
<version>${hibernate-core.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate-core.version}</version>
</dependency>
<dependency>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>5.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
@ -103,24 +125,36 @@
<version>${maven-compiler-plugin.version}</version>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<!-- QueryDSL plugin -->
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>${apt-maven-plugin.version}</version>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>5.0</version>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate-core.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
@ -132,6 +166,7 @@
<commons-dbcp.version>1.4</commons-dbcp.version>
<apt-maven-plugin.version>1.1.3</apt-maven-plugin.version>
<hibernate-core.version>6.4.2.Final</hibernate-core.version>
<spring-boot.version>3.2.3</spring-boot.version>
</properties>
</project>

View File

@ -0,0 +1,59 @@
package com.baeldung.querydslvsjpacriteria.entities;
import java.util.HashSet;
import java.util.Set;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.OneToMany;
@Entity
public class GroupUser {
@Id
@GeneratedValue
private Long id;
private String login;
@ManyToMany(mappedBy = "groupUsers", cascade = CascadeType.PERSIST)
private Set<UserGroup> userGroups = new HashSet<>();
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "groupUser")
private Set<Task> tasks = new HashSet<>(0);
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public Set<UserGroup> getUserGroups() {
return userGroups;
}
public void setUserGroups(Set<UserGroup> userGroups) {
this.userGroups = userGroups;
}
public Set<Task> getTasks() {
return tasks;
}
public void setTasks(Set<Task> tasks) {
this.tasks = tasks;
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.querydslvsjpacriteria.entities;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
@Entity
public class Task {
@Id
@GeneratedValue
private Long id;
private String description;
@ManyToOne
private GroupUser groupUser;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public GroupUser getUser() {
return groupUser;
}
public void setUser(GroupUser groupUser) {
this.groupUser = groupUser;
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.querydslvsjpacriteria.entities;
import java.util.HashSet;
import java.util.Set;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
@Entity
public class UserGroup {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToMany(cascade = CascadeType.PERSIST)
private Set<GroupUser> groupUsers = new HashSet<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<GroupUser> getGroupUsers() {
return groupUsers;
}
public void setGroupUsers(Set<GroupUser> groupUsers) {
this.groupUsers = groupUsers;
}
}

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