Merge branch 'master' into BAEL-7142-spring-ai
This commit is contained in:
@@ -94,7 +94,6 @@
|
||||
<properties>
|
||||
<spring.version>2.2.1.RELEASE</spring.version>
|
||||
<awssdk.version>2.17.283</awssdk.version>
|
||||
<lombok.version>1.18.20</lombok.version>
|
||||
<reactor.version>3.6.0</reactor.version>
|
||||
</properties>
|
||||
|
||||
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
package com.baeldung.iteratorvsforloop;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.List;
|
||||
import java.util.Iterator;
|
||||
import java.util.Collections;
|
||||
import java.util.ListIterator;
|
||||
|
||||
public class IteratorForLoopUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenEmptyCollection_whenUsingForLoop_thenNoElementsAreIterated() {
|
||||
List<String> names = Collections.emptyList();
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < names.size(); i++) {
|
||||
stringBuilder.append(names.get(i));
|
||||
}
|
||||
|
||||
assertEquals("", stringBuilder.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyCollection_whenUsingIterator_thenNoElementsAreIterated() {
|
||||
List<String> names = Collections.emptyList();
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
Iterator<String> iterator = names.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
stringBuilder.append(iterator.next());
|
||||
}
|
||||
|
||||
assertEquals("", stringBuilder.toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenUsingForLoop_thenAllElementsAreIterated() {
|
||||
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < names.size(); i++) {
|
||||
stringBuilder.append(names.get(i));
|
||||
}
|
||||
|
||||
assertEquals("AliceBobCharlie", stringBuilder.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenUsingIterator_thenAllElementsAreIterated() {
|
||||
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
Iterator<String> iterator = names.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
stringBuilder.append(iterator.next());
|
||||
}
|
||||
|
||||
assertEquals("AliceBobCharlie", stringBuilder.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenUsingForLoop_thenAllElementsAreIteratedReverseOrder() {
|
||||
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
for (int i = names.size() - 1; i >= 0; i--) {
|
||||
stringBuilder.append(names.get(i));
|
||||
}
|
||||
|
||||
assertEquals("CharlieBobAlice", stringBuilder.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenUsingListIterator_thenAllElementsAreIteratedInReverseOrder() {
|
||||
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
ListIterator<String> listIterator = names.listIterator(names.size());
|
||||
while (listIterator.hasPrevious()) {
|
||||
stringBuilder.append(listIterator.previous());
|
||||
}
|
||||
|
||||
assertEquals("CharlieBobAlice", stringBuilder.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenRemovingElementDuringForLoopIteration_thenConcurrentModificationExceptionIsThrown() {
|
||||
List<String> names = new ArrayList<>(List.of("Alice", "Bob", "Charlie"));
|
||||
|
||||
assertThrows(ConcurrentModificationException.class, () -> {
|
||||
for (String name : names) {
|
||||
names.remove("Bob");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenRemovingElementUsingIterator_thenElementIsRemovedSafely() {
|
||||
List<String> names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Charlie"));
|
||||
Iterator<String> iterator = names.iterator();
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
String name = iterator.next();
|
||||
if (name.equals("Bob")) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
List<String> expected = Arrays.asList("Alice", "Charlie");
|
||||
assertIterableEquals(expected, names);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenModifyingElementToLowerCaseDuringForLoopIteration_thenElementsAreModifiedToLowerCase() {
|
||||
List<String> names = new ArrayList<>(List.of("Alice", "Bob", "Charlie"));
|
||||
|
||||
for (int i = 0; i < names.size(); i++) {
|
||||
names.set(i, names.get(i).toLowerCase());
|
||||
}
|
||||
|
||||
List<String> expected = Arrays.asList("alice","bob", "charlie");
|
||||
assertIterableEquals(expected, names);
|
||||
}
|
||||
}
|
||||
@@ -10,4 +10,5 @@ This module contains articles about Map data structures in Java.
|
||||
- [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)
|
||||
- [Remove Duplicate Values From HashMap in Java](https://www.baeldung.com/java-hashmap-delete-duplicates)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-2)[[next -->]](/core-java-modules/core-java-collections-maps-4)
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.map.removeuplicate;
|
||||
package com.baeldung.map.removeduplicate;
|
||||
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -9,3 +9,4 @@ This module contains articles about Map data structures in Java.
|
||||
- [Difference Between Map and HashMap in Java](https://www.baeldung.com/java-map-vs-hashmap)
|
||||
- [How to Create a New Entry in a Map](https://www.baeldung.com/java-map-new-entry)
|
||||
- [Difference Between Map and MultivaluedMap in Java](https://www.baeldung.com/java-map-vs-multivaluedmap)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-3)[[next -->]](/core-java-modules/core-java-collections-maps-5)
|
||||
|
||||
@@ -10,4 +10,5 @@
|
||||
- [How to Invert a Map in Java](https://www.baeldung.com/java-invert-map)
|
||||
- [Implementing a Map with Multiple Keys in Java](https://www.baeldung.com/java-multiple-keys-map)
|
||||
- [Difference Between Map.ofEntries() and Map.of()](https://www.baeldung.com/map-ofentries-and-map-of)
|
||||
- More articles: [[<-- prev]](../core-java-collections-maps-4)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-4)[[next -->]](/core-java-modules/core-java-collections-maps-6)
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
- [Converting JsonNode Object to Map](https://www.baeldung.com/jackson-jsonnode-map)
|
||||
- [How to Modify a Key in a HashMap?](https://www.baeldung.com/java-hashmap-modify-key)
|
||||
- [Converting String or String Array to Map in Java](https://www.baeldung.com/java-convert-string-to-map)
|
||||
- [Remove Duplicate Values From HashMap in Java](https://www.baeldung.com/java-hashmap-delete-duplicates)
|
||||
- [Sorting Java Map in Descending Order](https://www.baeldung.com/java-sort-map-descending)
|
||||
- [Convert HashMap.toString() to HashMap in Java](https://www.baeldung.com/hashmap-from-tostring)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-5)[[next -->]](/core-java-modules/core-java-collections-maps-7)
|
||||
|
||||
|
||||
@@ -11,4 +11,4 @@ This module contains articles about the Stream API in Java.
|
||||
- [Should We Close a Java Stream?](https://www.baeldung.com/java-stream-close)
|
||||
- [Returning Stream vs. Collection](https://www.baeldung.com/java-return-stream-collection)
|
||||
- [Convert a Java Enumeration Into a Stream](https://www.baeldung.com/java-enumeration-to-stream)
|
||||
- More articles: [[<-- prev>]](/../core-java-streams-2)
|
||||
- More articles: [[<-- prev>]](/../core-java-streams-2) [[next -->]](/../core-java-streams-4)
|
||||
|
||||
@@ -10,3 +10,4 @@
|
||||
- [Understanding the Difference Between Stream.of() and IntStream.range()](https://www.baeldung.com/java-stream-of-and-intstream-range)
|
||||
- [Check if Object Is an Array in Java](https://www.baeldung.com/java-check-if-object-is-an-array)
|
||||
- [Mapping an Array of Integers to Strings Using Java Streams](https://www.baeldung.com/java-stream-integer-array-to-strings)
|
||||
- More articles: [[<-- prev>]](/../core-java-streams-3) [[next -->]](/../core-java-streams-5)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
- [Partition a Stream in Java](https://www.baeldung.com/java-partition-stream)
|
||||
- [Taking Every N-th Element from Finite and Infinite Streams in Java](https://www.baeldung.com/java-nth-element-finite-infinite-streams)
|
||||
- [Modifying Objects Within Stream While Iterating](https://www.baeldung.com/java-stream-modify-objects-during-iteration)
|
||||
- [Convert a Stream into a Map or Multimap in Java](https://www.baeldung.com/java-convert-stream-map-multimap)
|
||||
- [How to Avoid NoSuchElementException in Stream API](https://www.baeldung.com/java-streams-api-avoid-nosuchelementexception)
|
||||
- [Get Index of First Element Matching Boolean Using Java Streams](https://www.baeldung.com/java-streams-find-first-match-index)
|
||||
- [Handling NullPointerException in findFirst() When the First Element Is Null](https://www.baeldung.com/java-handle-nullpointerexception-findfirst-first-null)
|
||||
- More articles: [[<-- prev>]](/../core-java-streams-4)
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
## Relevant Articles:
|
||||
- [Handle Duplicate Keys When Producing Map Using Java Stream](https://www.baeldung.com/java-duplicate-keys-when-producing-map-using-stream)
|
||||
- [Convert a Stream into a Map or Multimap in Java](https://www.baeldung.com/java-convert-stream-map-multimap)
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package com.baeldung.streams.streamtomapandmultimap;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.ListMultimap;
|
||||
|
||||
public class StreamToMapAndMultiMapUnitTest {
|
||||
@Test
|
||||
public void givenStringStream_whenConvertingToMapWithMerge_thenExpectedMapIsGenerated() {
|
||||
Stream<String> stringStream = Stream.of("one", "two", "three", "two");
|
||||
|
||||
Map<String, String> mergedMap = stringStream.collect(
|
||||
Collectors.toMap(s -> s, s -> s, (s1, s2) -> s1 + ", " + s2)
|
||||
);
|
||||
|
||||
// Define the expected map
|
||||
Map<String, String> expectedMap = Map.of(
|
||||
"one", "one",
|
||||
"two", "two, two",
|
||||
"three", "three"
|
||||
);
|
||||
|
||||
assertEquals(expectedMap, mergedMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringStream_whenConvertingToMultimap_thenExpectedMultimapIsGenerated() {
|
||||
Stream<String> stringStream = Stream.of("one", "two", "three", "two");
|
||||
|
||||
ListMultimap<String, String> multimap = stringStream.collect(
|
||||
ArrayListMultimap::create,
|
||||
(map, element) -> map.put(element, element),
|
||||
ArrayListMultimap::putAll
|
||||
);
|
||||
|
||||
ListMultimap<String, String> expectedMultimap = ArrayListMultimap.create();
|
||||
expectedMultimap.put("one", "one");
|
||||
expectedMultimap.put("two", "two");
|
||||
expectedMultimap.put("two", "two");
|
||||
expectedMultimap.put("three", "three");
|
||||
|
||||
assertEquals(expectedMultimap, multimap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringStream_whenConvertingToMultimapWithStreamReduce_thenExpectedMultimapIsGenerated() {
|
||||
Stream<String> stringStream = Stream.of("one", "two", "three", "two");
|
||||
|
||||
Map<String, List<String>> multimap = stringStream.reduce(
|
||||
new HashMap<>(),
|
||||
(map, element) -> {
|
||||
map.computeIfAbsent(element, k -> new ArrayList<>()).add(element);
|
||||
return map;
|
||||
},
|
||||
(map1, map2) -> {
|
||||
map2.forEach((key, value) -> map1.merge(key, value, (list1, list2) -> {
|
||||
list1.addAll(list2);
|
||||
return list1;
|
||||
}));
|
||||
return map1;
|
||||
}
|
||||
);
|
||||
|
||||
Map<String, List<String>> expectedMultimap = new HashMap<>();
|
||||
expectedMultimap.put("one", Collections.singletonList("one"));
|
||||
expectedMultimap.put("two", Arrays.asList("two", "two"));
|
||||
expectedMultimap.put("three", Collections.singletonList("three"));
|
||||
|
||||
assertEquals(expectedMultimap, multimap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringStream_whenConvertingToMapWithStreamReduce_thenExpectedMapIsGenerated() {
|
||||
Stream<String> stringStream = Stream.of("one", "two", "three", "two");
|
||||
|
||||
Map<String, String> resultMap = stringStream.reduce(
|
||||
new HashMap<>(),
|
||||
(map, element) -> {
|
||||
map.put(element, element);
|
||||
return map;
|
||||
},
|
||||
(map1, map2) -> {
|
||||
map1.putAll(map2);
|
||||
return map1;
|
||||
}
|
||||
);
|
||||
|
||||
Map<String, String> expectedMap = new HashMap<>();
|
||||
expectedMap.put("one", "one");
|
||||
expectedMap.put("two", "two");
|
||||
expectedMap.put("three", "three");
|
||||
|
||||
assertEquals(expectedMap, resultMap);
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.annotation</groupId>
|
||||
<artifactId>javax.annotation-api</artifactId>
|
||||
|
||||
+2
-2
@@ -9,7 +9,7 @@ import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class JsonNodeToJsonObjectUnitTest {
|
||||
public class JsonNodeToObjectNodeUnitTest {
|
||||
|
||||
public static String jsonString = "{\"name\": \"John\", \"gender\": \"male\", \"company\": \"Baeldung\", \"isEmployee\": true, \"age\": 30}";
|
||||
|
||||
@@ -18,7 +18,7 @@ public class JsonNodeToJsonObjectUnitTest {
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode jsonNode = objectMapper.readTree(jsonString);
|
||||
ObjectNode objectNode = objectMapper.createObjectNode().setAll((ObjectNode) jsonNode);
|
||||
ObjectNode objectNode = (ObjectNode) jsonNode;
|
||||
|
||||
assertEquals("John", objectNode.get("name").asText());
|
||||
assertEquals("male", objectNode.get("gender").asText());
|
||||
@@ -3,17 +3,19 @@
|
||||
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>jkube-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>jkube-demo</name>
|
||||
<description>jkube-demo</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>jkube-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>jkube-demo</name>
|
||||
<description>jkube-demo</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@@ -2,14 +2,13 @@
|
||||
<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">
|
||||
<parent>
|
||||
<artifactId>microservices-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>rest-express</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>rest-express</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>A Basic, MongoDB-backed Service Suite</description>
|
||||
<url>https://github.com/RestExpress/RestExpress-Scaffold</url>
|
||||
<!--
|
||||
To run the project: mvn clean package exec:java
|
||||
* mongod must be running.
|
||||
@@ -18,11 +17,12 @@
|
||||
mvn clean package
|
||||
mvn assembly:single
|
||||
-->
|
||||
<description>A Basic, MongoDB-backed Service Suite</description>
|
||||
<url>https://github.com/RestExpress/RestExpress-Scaffold</url>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>rest-express</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>microservices-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring.version>6.0.12</spring.version>
|
||||
<spring.version>6.1.2</spring.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
<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>
|
||||
<groupId>com.baeldung.core-java-persistence-3</groupId>
|
||||
<artifactId>core-java-persistence-3</artifactId>
|
||||
<name>core-java-persistence-3</name>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-persistence-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
@@ -22,6 +26,7 @@
|
||||
<version>${commons-dbutils.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<h2.version>2.1.214</h2.version>
|
||||
<commons-dbutils.version>1.8.1</commons-dbutils.version>
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -74,7 +74,10 @@
|
||||
|
||||
<properties>
|
||||
<flyway.configFiles>src/main/resources/application-${spring-boot.run.profiles}.properties</flyway.configFiles>
|
||||
<flyway-maven-plugin.version>10.2.0</flyway-maven-plugin.version>
|
||||
<flyway-maven-plugin.version>10.4.0</flyway-maven-plugin.version>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,38 @@
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.flywaycallbacks.FlywayApplication;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = { FlywayApplication.class })
|
||||
public class FlywayAppIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Test
|
||||
public void testAllMigrationsExecuted() throws SQLException {
|
||||
DatabaseMetaData metadata = dataSource.getConnection()
|
||||
.getMetaData();
|
||||
ResultSet resultSet = metadata.getTables(null, null, null, new String[] { "TABLE" });
|
||||
Set<String> tables = Set.of("TABLE_ONE", "TABLE_TWO", "TABLE_THREE", "TABLE_FOUR");
|
||||
int migrations = 0;
|
||||
while (resultSet.next()) {
|
||||
if (tables.contains(resultSet.getString("TABLE_NAME"))) {
|
||||
migrations++;
|
||||
}
|
||||
}
|
||||
Assert.assertEquals(migrations, 4);
|
||||
}
|
||||
}
|
||||
@@ -31,20 +31,34 @@
|
||||
<artifactId>jooq-codegen</artifactId>
|
||||
<version>${jooq.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.15.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${postgresql.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<jooq.version>3.18.7</jooq.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<java.version>17</java.version>
|
||||
<jooq.version>3.19.0</jooq.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package com.baeldung.jooq;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.codegen.GenerationTool;
|
||||
import org.jooq.impl.DSL;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jooq.model.tables.Article;
|
||||
import com.baeldung.jooq.model.tables.Author;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
public class CodeGenerationIntegrationTest {
|
||||
|
||||
static DSLContext context;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws SQLException {
|
||||
Connection conn = DriverManager.getConnection("jdbc:h2:mem:tes;INIT=CREATE SCHEMA IF NOT EXISTS \"public\"");
|
||||
context = DSL.using(conn, SQLDialect.H2);
|
||||
|
||||
context.createTable(Author.AUTHOR)
|
||||
.columns(
|
||||
Author.AUTHOR.ID,
|
||||
Author.AUTHOR.FIRST_NAME,
|
||||
Author.AUTHOR.LAST_NAME,
|
||||
Author.AUTHOR.AGE
|
||||
)
|
||||
.execute();
|
||||
context.createTable(Article.ARTICLE)
|
||||
.columns(
|
||||
Article.ARTICLE.ID,
|
||||
Article.ARTICLE.TITLE,
|
||||
Article.ARTICLE.DESCRIPTION,
|
||||
Article.ARTICLE.AUTHOR_ID
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() throws IOException {
|
||||
File generatedDirectory = new File("src/main/java/com/baeldung/jooq/generated");
|
||||
FileUtils.deleteDirectory(generatedDirectory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassGenerationFromExistingDatabase() throws Exception {
|
||||
|
||||
File generatedDirectory = new File("src/main/java/com/baeldung/jooq/generated");
|
||||
|
||||
assertFalse(generatedDirectory.exists());
|
||||
|
||||
URL jooqConfigURL = getClass().getClassLoader().getResource("jooq-config.xml");
|
||||
assertNotNull(jooqConfigURL);
|
||||
File file = new File(jooqConfigURL.getFile());
|
||||
GenerationTool.generate(Files.readString(file.toPath()));
|
||||
|
||||
assertTrue(generatedDirectory.exists());
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -28,7 +28,7 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CrudLiveTest {
|
||||
public class CrudIntegrationTest {
|
||||
|
||||
static DSLContext context;
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.13.0.xsd">
|
||||
|
||||
<jdbc>
|
||||
<driver>org.h2.Driver</driver>
|
||||
<url>jdbc:h2:mem:tes</url>
|
||||
<user></user>
|
||||
<password></password>
|
||||
</jdbc>
|
||||
|
||||
<generator>
|
||||
<name>org.jooq.codegen.JavaGenerator</name>
|
||||
|
||||
<database>
|
||||
<name>org.jooq.meta.h2.H2Database</name>
|
||||
<inputSchema>public</inputSchema>
|
||||
<includes>.*</includes>
|
||||
<excludes></excludes>
|
||||
</database>
|
||||
|
||||
<target>
|
||||
<packageName>com.baeldung.jooq.generated</packageName>
|
||||
<directory>src/main/java</directory>
|
||||
</target>
|
||||
</generator>
|
||||
</configuration>
|
||||
@@ -108,7 +108,7 @@
|
||||
<module>spring-jpa</module>
|
||||
<module>spring-jpa-2</module>
|
||||
<module>spring-jdbc</module>
|
||||
<module>spring-jdbc-2</module>
|
||||
<module>spring-jdbc-2</module>
|
||||
<!--<module>spring-jooq</module>--><!-- failing after upgrading to jdk17 -->
|
||||
<module>spring-mybatis</module>
|
||||
<module>spring-persistence-simple</module>
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -36,4 +36,8 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring-boot.repackage.skip>true</spring-boot.repackage.skip>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
-1
@@ -3,7 +3,6 @@ package com.baeldung.springdatajdbcintro.repository;
|
||||
import com.baeldung.springdatajdbcintro.entity.Person;
|
||||
import org.springframework.data.jdbc.repository.query.Modifying;
|
||||
import org.springframework.data.jdbc.repository.query.Query;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
+4
-4
@@ -1,9 +1,9 @@
|
||||
package com.baeldung.springmultipledatasources.todos;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Todo {
|
||||
|
||||
+4
-4
@@ -1,9 +1,9 @@
|
||||
package com.baeldung.springmultipledatasources.topics;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Topic {
|
||||
|
||||
@@ -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>spring-data-jpa-query-2</artifactId>
|
||||
<name>spring-data-jpa-query-2</name>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user