Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
20f2d3a923
|
@ -62,3 +62,5 @@ jmeter/src/main/resources/*-JMeter.csv
|
|||
**/dist
|
||||
**/tmp
|
||||
**/out-tsc
|
||||
**/nbproject/
|
||||
**/nb-configuration.xml
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.algorithms.insertionsort;
|
||||
|
||||
public class InsertionSort {
|
||||
|
||||
public static void insertionSortImperative(int[] input) {
|
||||
for (int i = 1; i < input.length; i++) {
|
||||
int key = input[i];
|
||||
int j = i - 1;
|
||||
while (j >= 0 && input[j] > key) {
|
||||
input[j + 1] = input[j];
|
||||
j = j - 1;
|
||||
}
|
||||
input[j + 1] = key;
|
||||
}
|
||||
}
|
||||
|
||||
public static void insertionSortRecursive(int[] input) {
|
||||
insertionSortRecursive(input, input.length);
|
||||
}
|
||||
|
||||
private static void insertionSortRecursive(int[] input, int i) {
|
||||
// base case
|
||||
if (i <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// sort the first i - 1 elements of the array
|
||||
insertionSortRecursive(input, i - 1);
|
||||
|
||||
// then find the correct position of the element at position i
|
||||
int key = input[i - 1];
|
||||
int j = i - 2;
|
||||
// shifting the elements from their position by 1
|
||||
while (j >= 0 && input[j] > key) {
|
||||
input[j + 1] = input[j];
|
||||
j = j - 1;
|
||||
}
|
||||
// inserting the key at the appropriate position
|
||||
input[j + 1] = key;
|
||||
}
|
||||
}
|
|
@ -98,7 +98,7 @@ public class FindKthLargest {
|
|||
|
||||
private int randomPartition(Integer arr[], int left, int right) {
|
||||
int n = right - left + 1;
|
||||
int pivot = (int) (Math.random()) % n;
|
||||
int pivot = (int) (Math.random() * n);
|
||||
swap(arr, left + pivot, right);
|
||||
return partition(arr, left, right);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.algorithms.insertionsort;
|
||||
|
||||
import com.baeldung.algorithms.insertionsort.InsertionSort;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
public class InsertionSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUnsortedArray_whenInsertionSortImperative_thenSortedAsc() {
|
||||
int[] input = {6, 2, 3, 4, 5, 1};
|
||||
InsertionSort.insertionSortImperative(input);
|
||||
int[] expected = {1, 2, 3, 4, 5, 6};
|
||||
assertArrayEquals("the two arrays are not equal", expected, input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedArray_whenInsertionSortRecursive_thenSortedAsc() {
|
||||
int[] input = {6, 4, 5, 2, 3, 1};
|
||||
InsertionSort.insertionSortRecursive(input);
|
||||
int[] expected = {1, 2, 3, 4, 5, 6};
|
||||
assertArrayEquals("the two arrays are not equal", expected, input);
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
package com.baeldung.internationalization;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.FormatStyle;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class DateTimeFormatterUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDefaultUsLocaleAndDateTimeAndPattern_whenFormatWithDifferentLocales_thenGettingLocalizedDateTimes() {
|
||||
Locale.setDefault(Locale.US);
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 1, 10, 15, 50, 500);
|
||||
String pattern = "dd-MMMM-yyyy HH:mm:ss.SSS";
|
||||
|
||||
DateTimeFormatter defaultTimeFormatter = DateTimeFormatter.ofPattern(pattern);
|
||||
DateTimeFormatter plTimeFormatter = DateTimeFormatter.ofPattern(pattern, new Locale("pl", "PL"));
|
||||
DateTimeFormatter deTimeFormatter = DateTimeFormatter.ofPattern(pattern).withLocale(Locale.GERMANY);
|
||||
|
||||
Assert.assertEquals("01-January-2018 10:15:50.000", defaultTimeFormatter.format(localDateTime));
|
||||
Assert.assertEquals("01-stycznia-2018 10:15:50.000", plTimeFormatter.format(localDateTime));
|
||||
Assert.assertEquals("01-Januar-2018 10:15:50.000", deTimeFormatter.format(localDateTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateTimeAndTimeZone_whenFormatWithDifferentLocales_thenGettingLocalizedZonedDateTimes() {
|
||||
Locale.setDefault(Locale.US);
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 1, 10, 15, 50, 500);
|
||||
ZoneId losAngelesTimeZone = TimeZone.getTimeZone("America/Los_Angeles").toZoneId();
|
||||
|
||||
DateTimeFormatter localizedFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
|
||||
DateTimeFormatter frLocalizedFormatter =
|
||||
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(Locale.FRANCE);
|
||||
String formattedDateTime = localizedFormatter.format(ZonedDateTime.of(localDateTime, losAngelesTimeZone));
|
||||
String frFormattedDateTime = frLocalizedFormatter.format(ZonedDateTime.of(localDateTime, losAngelesTimeZone));
|
||||
|
||||
Assert.assertEquals("Monday, January 1, 2018 10:15:50 AM PST", formattedDateTime);
|
||||
Assert.assertEquals("lundi 1 janvier 2018 10 h 15 PST", frFormattedDateTime);
|
||||
}
|
||||
}
|
|
@ -48,6 +48,7 @@
|
|||
- [Thread Safe LIFO Data Structure Implementations](https://www.baeldung.com/java-lifo-thread-safe)
|
||||
- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list)
|
||||
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
||||
- [Differences Between Collection.clear() and Collection.removeAll()](https://www.baeldung.com/java-collection-clear-vs-removeall)
|
||||
- [Performance of contains() in a HashSet vs ArrayList](https://www.baeldung.com/java-hashset-arraylist-contains-performance)
|
||||
- [Get the Key for a Value from a Java Map](https://www.baeldung.com/java-map-key-from-value)
|
||||
- [Time Complexity of Java Collections](https://www.baeldung.com/java-collections-complexity)
|
||||
|
|
|
@ -1,85 +1,83 @@
|
|||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-collections</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>core-java-collections</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.collections</groupId>
|
||||
<artifactId>collections-generic</artifactId>
|
||||
<version>${collections-generic.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>${avaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.collections</groupId>
|
||||
<artifactId>eclipse-collections</artifactId>
|
||||
<version>${eclipse.collections.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${openjdk.jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${openjdk.jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-exec</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
<properties>
|
||||
<openjdk.jmh.version>1.19</openjdk.jmh.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<eclipse.collections.version>7.1.0</eclipse.collections.version>
|
||||
</properties>
|
||||
</project>
|
||||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-collections</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>core-java-collections</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.collections</groupId>
|
||||
<artifactId>collections-generic</artifactId>
|
||||
<version>${collections-generic.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>${avaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.collections</groupId>
|
||||
<artifactId>eclipse-collections</artifactId>
|
||||
<version>${eclipse.collections.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${openjdk.jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${openjdk.jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-exec</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<openjdk.jmh.version>1.19</openjdk.jmh.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<eclipse.collections.version>7.1.0</eclipse.collections.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.convertcollectiontoarraylist;
|
||||
|
||||
/**
|
||||
* This POJO is the element type of our collection. It has a deepCopy() method.
|
||||
*
|
||||
* @author chris
|
||||
*/
|
||||
public class Foo {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
private Foo parent;
|
||||
|
||||
public Foo() {
|
||||
}
|
||||
|
||||
public Foo(int id, String name, Foo parent) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Foo getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(Foo parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public Foo deepCopy() {
|
||||
return new Foo(
|
||||
this.id, this.name, this.parent != null ? this.parent.deepCopy() : null);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
package com.baeldung.map.java_8;
|
||||
|
||||
import com.baeldung.sort.Employee;
|
||||
import one.util.streamex.EntryStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class MergeMaps {
|
||||
|
||||
private static Map<String, Employee> map1 = new HashMap<>();
|
||||
private static Map<String, Employee> map2 = new HashMap<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
initialize();
|
||||
|
||||
mergeFunction();
|
||||
|
||||
streamConcat();
|
||||
|
||||
streamOf();
|
||||
|
||||
streamEx();
|
||||
|
||||
streamMerge();
|
||||
}
|
||||
|
||||
private static void streamMerge() {
|
||||
|
||||
Map<String, Employee> map3 = map2.entrySet()
|
||||
.stream()
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue,
|
||||
(v1, v2) -> new Employee(v1.getId(), v2.getName()),
|
||||
() -> new HashMap<>(map1)
|
||||
)
|
||||
);
|
||||
|
||||
System.out.println(map3);
|
||||
}
|
||||
|
||||
private static void streamEx() {
|
||||
Map<String, Employee> map3 = EntryStream.of(map1)
|
||||
.append(EntryStream.of(map2))
|
||||
.toMap((e1, e2) -> e1);
|
||||
|
||||
System.out.println(map3);
|
||||
|
||||
}
|
||||
|
||||
private static void streamOf() {
|
||||
Map<String, Employee> map3 = Stream.of(map1, map2)
|
||||
.flatMap(map -> map.entrySet().stream())
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue,
|
||||
(v1, v2) -> new Employee(v1.getId(), v2.getName())
|
||||
)
|
||||
);
|
||||
|
||||
map3.entrySet().forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static void streamConcat() {
|
||||
Map<String, Employee> result = Stream.concat(map1.entrySet().stream(), map2.entrySet().stream()).collect(Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue,
|
||||
(value1, value2) -> new Employee(value2.getId(), value1.getName())
|
||||
));
|
||||
|
||||
result.entrySet().forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static void mergeFunction() {
|
||||
Map<String, Employee> map3 = new HashMap<>(map1);
|
||||
|
||||
map2.forEach(
|
||||
(key, value) -> map3.merge(key, value, (v1, v2) ->
|
||||
new Employee(v1.getId(), v2.getName()))
|
||||
);
|
||||
|
||||
map3.entrySet().forEach(System.out::println);
|
||||
}
|
||||
|
||||
|
||||
private static void initialize() {
|
||||
Employee employee1 = new Employee(1L, "Henry");
|
||||
map1.put(employee1.getName(), employee1);
|
||||
Employee employee2 = new Employee(22L, "Annie");
|
||||
map1.put(employee2.getName(), employee2);
|
||||
Employee employee3 = new Employee(8L, "John");
|
||||
map1.put(employee3.getName(), employee3);
|
||||
|
||||
Employee employee4 = new Employee(2L, "George");
|
||||
map2.put(employee4.getName(), employee4);
|
||||
Employee employee5 = new Employee(3L, "Henry");
|
||||
map2.put(employee5.getName(), employee5);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.synchronizedcollections.application;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Application {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(Application.class.getName());
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
List<Integer> syncCollection = Collections.synchronizedList(Arrays.asList(1, 2, 3, 4, 5, 6));
|
||||
synchronized (syncCollection) {
|
||||
syncCollection.forEach((e) -> {LOGGER.info(e.toString());});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,148 @@
|
|||
package com.baeldung.convertcollectiontoarraylist;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import static java.util.stream.Collectors.toCollection;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chris
|
||||
*/
|
||||
public class FooUnitTest {
|
||||
private static Collection<Foo> srcCollection = new HashSet<>();
|
||||
|
||||
public FooUnitTest() {
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() {
|
||||
int i = 0;
|
||||
Foo john = new Foo(i++, "John", null);
|
||||
Foo mary = new Foo(i++, "Mary", null);
|
||||
Foo sam = new Foo(i++, "Sam", john);
|
||||
Foo alice = new Foo(i++, "Alice", john);
|
||||
Foo buffy = new Foo(i++, "Buffy", sam);
|
||||
srcCollection.add(john);
|
||||
srcCollection.add(mary);
|
||||
srcCollection.add(sam);
|
||||
srcCollection.add(alice);
|
||||
srcCollection.add(buffy);
|
||||
|
||||
// make sure the collection isn't sorted accidentally
|
||||
assertFalse("Oops: source collection is already sorted!", isSorted(srcCollection));
|
||||
}
|
||||
|
||||
/**
|
||||
* Section 3. Using the ArrayList Constructor
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingConstructor_thenVerifyShallowCopy() {
|
||||
ArrayList<Foo> newList = new ArrayList<>(srcCollection);
|
||||
verifyShallowCopy(srcCollection, newList);
|
||||
}
|
||||
|
||||
/**
|
||||
* Section 4. Using the Streams API
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingStream_thenVerifyShallowCopy() {
|
||||
ArrayList<Foo> newList = srcCollection.stream().collect(toCollection(ArrayList::new));
|
||||
|
||||
verifyShallowCopy(srcCollection, newList);
|
||||
}
|
||||
|
||||
/**
|
||||
* Section 5. Deep Copy
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingDeepCopy_thenVerifyDeepCopy() {
|
||||
ArrayList<Foo> newList = srcCollection.stream()
|
||||
.map(foo -> foo.deepCopy())
|
||||
.collect(toCollection(ArrayList::new));
|
||||
|
||||
verifyDeepCopy(srcCollection, newList);
|
||||
}
|
||||
|
||||
/**
|
||||
* Section 6. Controlling the List Order
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingSortedStream_thenVerifySortOrder() {
|
||||
ArrayList<Foo> newList = srcCollection.stream()
|
||||
.sorted(Comparator.comparing(Foo::getName))
|
||||
.collect(toCollection(ArrayList::new));
|
||||
|
||||
assertTrue("ArrayList is not sorted by name", isSorted(newList));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the contents of the two collections are the same
|
||||
* @param a
|
||||
* @param b
|
||||
*/
|
||||
private void verifyShallowCopy(Collection a, Collection b) {
|
||||
assertEquals("Collections have different lengths", a.size(), b.size());
|
||||
Iterator<Foo> iterA = a.iterator();
|
||||
Iterator<Foo> iterB = b.iterator();
|
||||
while (iterA.hasNext()) {
|
||||
// use '==' to test instance identity
|
||||
assertTrue("Foo instances differ!", iterA.next() == iterB.next());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the contents of the two collections are the same
|
||||
* @param a
|
||||
* @param b
|
||||
*/
|
||||
private void verifyDeepCopy(Collection a, Collection b) {
|
||||
assertEquals("Collections have different lengths", a.size(), b.size());
|
||||
Iterator<Foo> iterA = a.iterator();
|
||||
Iterator<Foo> iterB = b.iterator();
|
||||
while (iterA.hasNext()) {
|
||||
Foo nextA = iterA.next();
|
||||
Foo nextB = iterB.next();
|
||||
// should not be same instance
|
||||
assertFalse("Foo instances are the same!", nextA == nextB);
|
||||
// but should have same content
|
||||
assertFalse("Foo instances have different content!", fooDiff(nextA, nextB));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the contents of a and b differ. Test parent recursively
|
||||
* @param a
|
||||
* @param b
|
||||
* @return False if the two items are the same
|
||||
*/
|
||||
private boolean fooDiff(Foo a, Foo b) {
|
||||
if (a != null && b != null) {
|
||||
return a.getId() != b.getId()
|
||||
|| !a.getName().equals(b.getName())
|
||||
|| fooDiff(a.getParent(), b.getParent());
|
||||
}
|
||||
return !(a == null && b == null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param c collection of Foo
|
||||
* @return true if the collection is sorted by name
|
||||
*/
|
||||
private static boolean isSorted(Collection<Foo> c) {
|
||||
String prevName = null;
|
||||
for (Foo foo : c) {
|
||||
if (prevName == null || foo.getName().compareTo(prevName) > 0) {
|
||||
prevName = foo.getName();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.java.map;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class KeyCheckUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenKeyIsPresent_thenContainsKeyReturnsTrue() {
|
||||
Map<String, String> map = Collections.singletonMap("key", "value");
|
||||
|
||||
assertTrue(map.containsKey("key"));
|
||||
assertFalse(map.containsKey("missing"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenKeyHasNullValue_thenGetStillWorks() {
|
||||
Map<String, String> map = Collections.singletonMap("nothing", null);
|
||||
|
||||
assertTrue(map.containsKey("nothing"));
|
||||
assertNull(map.get("nothing"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.synchronizedcollections.test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SynchronizedCollectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedCollection_whenTwoThreadsAddElements_thenCorrectCollectionSize() throws InterruptedException {
|
||||
Collection<Integer> syncCollection = Collections.synchronizedCollection(new ArrayList<>());
|
||||
|
||||
Runnable listOperations = () -> {
|
||||
syncCollection.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));
|
||||
};
|
||||
Thread thread1 = new Thread(listOperations);
|
||||
Thread thread2 = new Thread(listOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(syncCollection.size()).isEqualTo(12);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.synchronizedcollections.test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class SynchronizedListUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedList_whenTwoThreadsAddElements_thenCorrectListSize() throws InterruptedException {
|
||||
List<Integer> syncList = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
Runnable listOperations = () -> {
|
||||
syncList.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));
|
||||
};
|
||||
Thread thread1 = new Thread(listOperations);
|
||||
Thread thread2 = new Thread(listOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(syncList.size()).isEqualTo(12);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringList_whenTwoThreadsIterateOnSynchronizedList_thenCorrectResult() throws InterruptedException {
|
||||
List<String> syncCollection = Collections.synchronizedList(Arrays.asList("a", "b", "c"));
|
||||
List<String> uppercasedCollection = new ArrayList<>();
|
||||
|
||||
Runnable listOperations = () -> {
|
||||
synchronized (syncCollection) {
|
||||
syncCollection.forEach((e) -> {
|
||||
uppercasedCollection.add(e.toUpperCase());
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Thread thread1 = new Thread(listOperations);
|
||||
Thread thread2 = new Thread(listOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(uppercasedCollection.get(0)).isEqualTo("A");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.synchronizedcollections.test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class SynchronizedMapUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedMap_whenTwoThreadsAddElements_thenCorrectMapSize() throws InterruptedException {
|
||||
Map<Integer, String> syncMap = Collections.synchronizedMap(new HashMap<>());
|
||||
|
||||
Runnable mapOperations = () -> {
|
||||
syncMap.put(1, "one");
|
||||
syncMap.put(2, "two");
|
||||
syncMap.put(3, "three");
|
||||
|
||||
};
|
||||
Thread thread1 = new Thread(mapOperations);
|
||||
Thread thread2 = new Thread(mapOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(syncMap.size()).isEqualTo(3);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.synchronizedcollections.test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class SynchronizedSetUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedSet_whenTwoThreadsAddElements_thenCorrectSetSize() throws InterruptedException {
|
||||
Set<Integer> syncSet = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
Runnable setOperations = () -> {syncSet.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));};
|
||||
Thread thread1 = new Thread(setOperations);
|
||||
Thread thread2 = new Thread(setOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(syncSet.size()).isEqualTo(6);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.synchronizedcollections.test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SynchronizedSortedMapUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedSorteMap_whenTwoThreadsAddElements_thenCorrectSortedMapSize() throws InterruptedException {
|
||||
Map<Integer, String> syncSortedMap = Collections.synchronizedSortedMap(new TreeMap<>());
|
||||
|
||||
Runnable sortedMapOperations = () -> {
|
||||
syncSortedMap.put(1, "One");
|
||||
syncSortedMap.put(2, "Two");
|
||||
syncSortedMap.put(3, "Three");
|
||||
};
|
||||
Thread thread1 = new Thread(sortedMapOperations);
|
||||
Thread thread2 = new Thread(sortedMapOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(syncSortedMap.size()).isEqualTo(3);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.synchronizedcollections.test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SynchronizedSortedSetUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedSortedSet_whenTwoThreadsAddElements_thenCorrectSortedSetSize() throws InterruptedException {
|
||||
SortedSet<Integer> syncSortedSet = Collections.synchronizedSortedSet(new TreeSet<>());
|
||||
|
||||
Runnable sortedSetOperations = () -> {syncSortedSet.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));};
|
||||
sortedSetOperations.run();
|
||||
sortedSetOperations.run();
|
||||
Thread thread1 = new Thread(sortedSetOperations);
|
||||
Thread thread2 = new Thread(sortedSetOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(syncSortedSet.size()).isEqualTo(6);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.stream;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
public class OutputStreamExamples {
|
||||
|
||||
public void fileOutputStreamByteSequence(String file, String data) throws IOException {
|
||||
byte[] bytes = data.getBytes();
|
||||
try (OutputStream out = new FileOutputStream(file)) {
|
||||
out.write(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
public void fileOutputStreamByteSubSequence(String file, String data) throws IOException {
|
||||
byte[] bytes = data.getBytes();
|
||||
try (OutputStream out = new FileOutputStream(file)) {
|
||||
out.write(bytes, 6, 5);
|
||||
}
|
||||
}
|
||||
|
||||
public void fileOutputStreamByteSingle(String file, String data) throws IOException {
|
||||
byte[] bytes = data.getBytes();
|
||||
try (OutputStream out = new FileOutputStream(file)) {
|
||||
out.write(bytes[6]);
|
||||
}
|
||||
}
|
||||
|
||||
public void bufferedOutputStream(String file, String... data) throws IOException {
|
||||
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
|
||||
for (String s : data) {
|
||||
out.write(s.getBytes());
|
||||
out.write(" ".getBytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void outputStreamWriter(String file, String data) throws IOException {
|
||||
try (OutputStream out = new FileOutputStream(file); Writer writer = new OutputStreamWriter(out, "UTF-8")) {
|
||||
writer.write(data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package com.baeldung.stream;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class OutputStreamExamplesTest {
|
||||
|
||||
StringBuilder filePath = new StringBuilder();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
filePath.append("src");
|
||||
filePath.append(File.separator);
|
||||
filePath.append("test");
|
||||
filePath.append(File.separator);
|
||||
filePath.append("resources");
|
||||
filePath.append(File.separator);
|
||||
filePath.append("output_file.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutputStream_whenWriteSingleByteCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.fileOutputStreamByteSingle(filePath.toString(), "Hello World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutputStream_whenWriteByteSequenceCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.fileOutputStreamByteSequence(filePath.toString(), "Hello World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutputStream_whenWriteByteSubSequenceCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.fileOutputStreamByteSubSequence(filePath.toString(), "Hello World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBufferedOutputStream_whenCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.bufferedOutputStream(filePath.toString(), "Hello", "World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutputStreamWriter_whenCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.outputStreamWriter(filePath.toString(), "Hello World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
}
|
|
@ -25,7 +25,6 @@
|
|||
- [The Traveling Salesman Problem in Java](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
|
||||
- [How to Create an Executable JAR with Maven](http://www.baeldung.com/executable-jar-with-maven)
|
||||
- [How to Design a Genetic Algorithm in Java](http://www.baeldung.com/java-genetic-algorithm)
|
||||
- [Spring Security – Cache Control Headers](http://www.baeldung.com/spring-security-cache-control-headers)
|
||||
- [Basic Introduction to JMX](http://www.baeldung.com/java-management-extensions)
|
||||
- [AWS Lambda With Java](http://www.baeldung.com/java-aws-lambda)
|
||||
- [Introduction to Nashorn](http://www.baeldung.com/java-nashorn)
|
||||
|
@ -37,7 +36,6 @@
|
|||
- [Guide to sun.misc.Unsafe](http://www.baeldung.com/java-unsafe)
|
||||
- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
|
||||
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
|
||||
- [Guide to UUID in JAVA](http://www.baeldung.com/guide-to-uuid-in-java)
|
||||
- [How to Add a Single Element to a Stream](http://www.baeldung.com/java-stream-append-prepend)
|
||||
- [Iterating Over Enum Values in Java](http://www.baeldung.com/java-enum-iteration)
|
||||
- [Kotlin Java Interoperability](http://www.baeldung.com/kotlin-java-interoperability)
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
package com.baeldung.classloader;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class CustomClassLoader extends ClassLoader {
|
||||
|
||||
|
||||
public Class getClass(String name) throws ClassNotFoundException {
|
||||
@Override
|
||||
public Class findClass(String name) throws ClassNotFoundException {
|
||||
byte[] b = loadClassFromFile(name);
|
||||
return defineClass(name, b, 0, b.length);
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ public class CustomClassLoaderUnitTest {
|
|||
public void customLoader() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
|
||||
|
||||
CustomClassLoader customClassLoader = new CustomClassLoader();
|
||||
Class<?> c = customClassLoader.getClass(PrintClassLoader.class.getName());
|
||||
Class<?> c = customClassLoader.findClass(PrintClassLoader.class.getName());
|
||||
|
||||
Object ob = c.newInstance();
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant articles
|
||||
|
||||
- [CDI Portable Extension and Flyway](https://www.baeldung.com/cdi-portable-extension)
|
|
@ -14,4 +14,5 @@
|
|||
- [Bootstrapping JPA Programmatically in Java](http://www.baeldung.com/java-bootstrap-jpa)
|
||||
- [Optimistic Locking in JPA](http://www.baeldung.com/jpa-optimistic-locking)
|
||||
- [Hibernate Entity Lifecycle](https://www.baeldung.com/hibernate-entity-lifecycle)
|
||||
- [Mapping A Hibernate Query to a Custom Class](https://www.baeldung.com/hibernate-query-to-custom-class)
|
||||
- [@JoinColumn Annotation Explained](https://www.baeldung.com/jpa-join-column)
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.hibernate.namingstrategy;
|
||||
|
||||
import org.hibernate.boot.model.naming.Identifier;
|
||||
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
|
||||
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
|
||||
|
||||
public class CustomPhysicalNamingStrategy implements PhysicalNamingStrategy {
|
||||
|
||||
@Override
|
||||
public Identifier toPhysicalCatalogName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
|
||||
return convertToSnakeCase(identifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Identifier toPhysicalColumnName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
|
||||
return convertToSnakeCase(identifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Identifier toPhysicalSchemaName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
|
||||
return convertToSnakeCase(identifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Identifier toPhysicalSequenceName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
|
||||
return convertToSnakeCase(identifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
|
||||
return convertToSnakeCase(identifier);
|
||||
}
|
||||
|
||||
private Identifier convertToSnakeCase(final Identifier identifier) {
|
||||
if (identifier == null) {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
final String regex = "([a-z])([A-Z])";
|
||||
final String replacement = "$1_$2";
|
||||
final String newName = identifier.getText()
|
||||
.replaceAll(regex, replacement)
|
||||
.toLowerCase();
|
||||
return Identifier.toIdentifier(newName);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package com.baeldung.hibernate.namingstrategy;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Customers")
|
||||
public class Customer {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
@Column(name = "email")
|
||||
private String emailAddress;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getEmailAddress() {
|
||||
return emailAddress;
|
||||
}
|
||||
|
||||
public void setEmailAddress(String emailAddress) {
|
||||
this.emailAddress = emailAddress;
|
||||
}
|
||||
|
||||
}
|
|
@ -4,13 +4,23 @@ import javax.persistence.Entity;
|
|||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.SequenceGenerator;
|
||||
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
import org.hibernate.annotations.Parameter;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence-generator")
|
||||
@SequenceGenerator(name = "sequence-generator", sequenceName = "user_sequence", initialValue = 4)
|
||||
@GeneratedValue(generator = "sequence-generator")
|
||||
@GenericGenerator(
|
||||
name = "sequence-generator",
|
||||
strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
|
||||
parameters = {
|
||||
@Parameter(name = "sequence_name", value = "user_sequence"),
|
||||
@Parameter(name = "initial_value", value = "4"),
|
||||
@Parameter(name = "increment_size", value = "1")
|
||||
}
|
||||
)
|
||||
private long userId;
|
||||
|
||||
public long getUserId() {
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
package com.baeldung.hibernate.namingstrategy;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NamingStrategyLiveTest {
|
||||
|
||||
private Session session;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
try {
|
||||
Configuration configuration = new Configuration();
|
||||
|
||||
Properties properties = new Properties();
|
||||
properties.load(Thread.currentThread()
|
||||
.getContextClassLoader()
|
||||
.getResourceAsStream("hibernate-namingstrategy.properties"));
|
||||
|
||||
configuration.setProperties(properties);
|
||||
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
|
||||
.build();
|
||||
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
|
||||
metadataSources.addAnnotatedClass(Customer.class);
|
||||
|
||||
SessionFactory factory = metadataSources.buildMetadata()
|
||||
.buildSessionFactory();
|
||||
|
||||
session = factory.openSession();
|
||||
} catch (HibernateException | IOException e) {
|
||||
fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (session != null)
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomPhysicalNamingStrategy() {
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setFirstName("first name");
|
||||
customer.setLastName("last name");
|
||||
customer.setEmailAddress("customer@example.com");
|
||||
|
||||
session.beginTransaction();
|
||||
|
||||
Long id = (Long) session.save(customer);
|
||||
|
||||
session.flush();
|
||||
session.clear();
|
||||
|
||||
Object[] result = (Object[]) session.createNativeQuery("select c.first_name, c.last_name, c.email from customers c where c.id = :id")
|
||||
.setParameter("id", id)
|
||||
.getSingleResult();
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
hibernate.connection.driver_class=org.h2.Driver
|
||||
hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1
|
||||
hibernate.connection.username=sa
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
|
||||
hibernate.show_sql=true
|
||||
hibernate.hbm2ddl.auto=create-drop
|
||||
|
||||
hibernate.physical_naming_strategy=com.baeldung.hibernate.namingstrategy.CustomPhysicalNamingStrategy
|
||||
hibernate.implicit_naming_strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
|
|
@ -118,7 +118,7 @@
|
|||
|
||||
<properties>
|
||||
<!-- marshalling -->
|
||||
<jackson.version>2.9.6</jackson.version>
|
||||
<jackson.version>2.9.7</jackson.version>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.8</commons-lang3.version>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.jackson.xmlToJson;
|
||||
|
||||
public enum Color {
|
||||
PINK, BLUE, YELLOW, RED;
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.jackson.xmlToJson;
|
||||
|
||||
public class Flower {
|
||||
|
||||
private String name;
|
||||
|
||||
private Color color;
|
||||
|
||||
private Integer petals;
|
||||
|
||||
public Flower() { }
|
||||
|
||||
public Flower(String name, Color color, Integer petals) {
|
||||
this.name = name;
|
||||
this.color = color;
|
||||
this.petals = petals;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public Integer getPetals() {
|
||||
return petals;
|
||||
}
|
||||
|
||||
public void setPetals(Integer petals) {
|
||||
this.petals = petals;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package com.baeldung.jackson.xmlToJson;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class XmlToJsonUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() {
|
||||
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
|
||||
|
||||
try {
|
||||
XmlMapper xmlMapper = new XmlMapper();
|
||||
Flower poppy = xmlMapper.readValue(flowerXML, Flower.class);
|
||||
|
||||
assertEquals(poppy.getName(), "Poppy");
|
||||
assertEquals(poppy.getColor(), Color.RED);
|
||||
assertEquals(poppy.getPetals(), new Integer(9));
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String json = mapper.writeValueAsString(poppy);
|
||||
|
||||
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}");
|
||||
System.out.println(json);
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() {
|
||||
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
|
||||
|
||||
try {
|
||||
XmlMapper xmlMapper = new XmlMapper();
|
||||
JsonNode node = xmlMapper.readTree(flowerXML.getBytes());
|
||||
|
||||
ObjectMapper jsonMapper = new ObjectMapper();
|
||||
String json = jsonMapper.writeValueAsString(node);
|
||||
|
||||
System.out.println(json);
|
||||
|
||||
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}");
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,7 +5,9 @@ import org.junit.Test;
|
|||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Period;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
@ -26,6 +28,17 @@ public class DateDiffUnitTest {
|
|||
|
||||
assertEquals(diff, 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() {
|
||||
LocalDate now = LocalDate.now();
|
||||
LocalDate sixDaysBehind = now.minusDays(6);
|
||||
|
||||
Period period = Period.between(now, sixDaysBehind);
|
||||
int diff = period.getDays();
|
||||
|
||||
assertEquals(diff, 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoDateTimesInJava8_whenDifferentiating_thenWeGetSix() {
|
||||
|
|
|
@ -0,0 +1,158 @@
|
|||
package com.baeldung.datetime;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.time.format.FormatStyle;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class DateTimeFormatterUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDefaultUsLocaleAndDateTimeAndPattern_whenFormatWithDifferentLocales_thenGettingLocalizedDateTimes() {
|
||||
Locale.setDefault(Locale.US);
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 1, 10, 15, 50, 500);
|
||||
String pattern = "dd-MMMM-yyyy HH:mm:ss.SSS";
|
||||
|
||||
DateTimeFormatter defaultTimeFormatter = DateTimeFormatter.ofPattern(pattern);
|
||||
DateTimeFormatter plTimeFormatter = DateTimeFormatter.ofPattern(pattern, new Locale("pl", "PL"));
|
||||
DateTimeFormatter deTimeFormatter = DateTimeFormatter.ofPattern(pattern).withLocale(Locale.GERMANY);
|
||||
|
||||
Assert.assertEquals("01-January-2018 10:15:50.000", defaultTimeFormatter.format(localDateTime));
|
||||
Assert.assertEquals("01-stycznia-2018 10:15:50.000", plTimeFormatter.format(localDateTime));
|
||||
Assert.assertEquals("01-Januar-2018 10:15:50.000", deTimeFormatter.format(localDateTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateTimeAndTimeZone_whenFormatWithDifferentLocales_thenGettingLocalizedZonedDateTimes() {
|
||||
Locale.setDefault(Locale.US);
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 1, 10, 15, 50, 500);
|
||||
ZoneId losAngelesTimeZone = TimeZone.getTimeZone("America/Los_Angeles").toZoneId();
|
||||
|
||||
DateTimeFormatter localizedFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
|
||||
DateTimeFormatter frLocalizedFormatter =
|
||||
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(Locale.FRANCE);
|
||||
String formattedDateTime = localizedFormatter.format(ZonedDateTime.of(localDateTime, losAngelesTimeZone));
|
||||
String frFormattedDateTime = frLocalizedFormatter.format(ZonedDateTime.of(localDateTime, losAngelesTimeZone));
|
||||
|
||||
Assert.assertEquals("Monday, January 1, 2018 10:15:50 AM PST", formattedDateTime);
|
||||
Assert.assertEquals("lundi 1 janvier 2018 10 h 15 PST", frFormattedDateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedDate() {
|
||||
String europeanDatePattern = "dd.MM.yyyy";
|
||||
DateTimeFormatter europeanDateFormatter = DateTimeFormatter.ofPattern(europeanDatePattern);
|
||||
LocalDate summerDay = LocalDate.of(2016, 7, 31);
|
||||
Assert.assertEquals("31.07.2016", europeanDateFormatter.format(summerDay));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedTime24() {
|
||||
String timeColonPattern = "HH:mm:ss";
|
||||
DateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern);
|
||||
LocalTime colonTime = LocalTime.of(17, 35, 50);
|
||||
Assert.assertEquals("17:35:50", timeColonFormatter.format(colonTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedTimeWithMillis() {
|
||||
String timeColonPattern = "HH:mm:ss SSS";
|
||||
DateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern);
|
||||
LocalTime colonTime = LocalTime.of(17, 35, 50).plus(329, ChronoUnit.MILLIS);
|
||||
Assert.assertEquals("17:35:50 329", timeColonFormatter.format(colonTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedTimePM() {
|
||||
String timeColonPattern = "hh:mm:ss a";
|
||||
DateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern);
|
||||
LocalTime colonTime = LocalTime.of(17, 35, 50);
|
||||
Assert.assertEquals("05:35:50 PM", timeColonFormatter.format(colonTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedUTCRelatedZonedDateTime() {
|
||||
String newYorkDateTimePattern = "dd.MM.yyyy HH:mm z";
|
||||
DateTimeFormatter newYorkDateFormatter = DateTimeFormatter.ofPattern(newYorkDateTimePattern);
|
||||
LocalDateTime summerDay = LocalDateTime.of(2016, 7, 31, 14, 15);
|
||||
Assert.assertEquals("31.07.2016 14:15 UTC-04:00", newYorkDateFormatter.format(ZonedDateTime.of(summerDay, ZoneId.of("UTC-4"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedNewYorkZonedDateTime() {
|
||||
String newYorkDateTimePattern = "dd.MM.yyyy HH:mm z";
|
||||
DateTimeFormatter newYorkDateFormatter = DateTimeFormatter.ofPattern(newYorkDateTimePattern);
|
||||
LocalDateTime summerDay = LocalDateTime.of(2016, 7, 31, 14, 15);
|
||||
Assert.assertEquals("31.07.2016 14:15 EDT", newYorkDateFormatter.format(ZonedDateTime.of(summerDay, ZoneId.of("America/New_York"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintStyledDate() {
|
||||
LocalDate anotherSummerDay = LocalDate.of(2016, 8, 23);
|
||||
Assert.assertEquals("Tuesday, August 23, 2016", DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(anotherSummerDay));
|
||||
Assert.assertEquals("August 23, 2016", DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).format(anotherSummerDay));
|
||||
Assert.assertEquals("Aug 23, 2016", DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(anotherSummerDay));
|
||||
Assert.assertEquals("8/23/16", DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(anotherSummerDay));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintStyledDateTime() {
|
||||
LocalDateTime anotherSummerDay = LocalDateTime.of(2016, 8, 23, 13, 12, 45);
|
||||
Assert.assertEquals("Tuesday, August 23, 2016 1:12:45 PM EET", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
|
||||
Assert.assertEquals("August 23, 2016 1:12:45 PM EET", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
|
||||
Assert.assertEquals("Aug 23, 2016 1:12:45 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
|
||||
Assert.assertEquals("8/23/16 1:12 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedDateTimeWithPredefined() {
|
||||
Assert.assertEquals("2018-03-09", DateTimeFormatter.ISO_LOCAL_DATE.format(LocalDate.of(2018, 3, 9)));
|
||||
Assert.assertEquals("2018-03-09-03:00", DateTimeFormatter.ISO_OFFSET_DATE.format(LocalDate.of(2018, 3, 9).atStartOfDay(ZoneId.of("UTC-3"))));
|
||||
Assert.assertEquals("Fri, 9 Mar 2018 00:00:00 -0300", DateTimeFormatter.RFC_1123_DATE_TIME.format(LocalDate.of(2018, 3, 9).atStartOfDay(ZoneId.of("UTC-3"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseDateTime() {
|
||||
Assert.assertEquals(LocalDate.of(2018, 3, 12), LocalDate.from(DateTimeFormatter.ISO_LOCAL_DATE.parse("2018-03-09")).plusDays(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseFormatStyleFull() {
|
||||
ZonedDateTime dateTime = ZonedDateTime.from(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).parse("Tuesday, August 23, 2016 1:12:45 PM EET"));
|
||||
Assert.assertEquals(ZonedDateTime.of(LocalDateTime.of(2016, 8, 23, 22, 12, 45), ZoneId.of("Europe/Bucharest")), dateTime.plusHours(9));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseDateWithCustomFormatter() {
|
||||
DateTimeFormatter europeanDateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
|
||||
Assert.assertFalse(LocalDate.from(europeanDateFormatter.parse("15.08.2014")).isLeapYear());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseTimeWithCustomFormatter() {
|
||||
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
|
||||
Assert.assertTrue(LocalTime.from(timeFormatter.parse("12:25:30 AM")).isBefore(LocalTime.NOON));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseZonedDateTimeWithCustomFormatter() {
|
||||
DateTimeFormatter zonedFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm z");
|
||||
Assert.assertEquals(7200, ZonedDateTime.from(zonedFormatter.parse("31.07.2016 14:15 GMT+02:00")).getOffset().getTotalSeconds());
|
||||
}
|
||||
|
||||
@Test(expected = DateTimeParseException.class)
|
||||
public void shouldExpectAnExceptionIfDateTimeStringNotMatchPattern() {
|
||||
DateTimeFormatter zonedFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm z");
|
||||
ZonedDateTime.from(zonedFormatter.parse("31.07.2016 14:15"));
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
## Relevant articles:
|
||||
|
||||
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
|
||||
- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro)
|
||||
- [Migrating to the New Java 8 Date Time API](http://www.baeldung.com/migrating-to-java-8-date-time-api)
|
|
@ -1,36 +0,0 @@
|
|||
<?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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>java-difference-date</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>java-difference-date</name>
|
||||
<description>Difference between two dates in java</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.darwinsys</groupId>
|
||||
<artifactId>hirondelle-date4j</artifactId>
|
||||
<version>${hirondelle-date4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<joda-time.version>2.9.9</joda-time.version>
|
||||
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,61 +0,0 @@
|
|||
package com.baeldung;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Duration;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class DateDiffUnitTest {
|
||||
@Test
|
||||
public void givenTwoDatesBeforeJava8_whenDifferentiating_thenWeGetSix() throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
|
||||
Date firstDate = sdf.parse("06/24/2017");
|
||||
Date secondDate = sdf.parse("06/30/2017");
|
||||
|
||||
long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
|
||||
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
|
||||
|
||||
assertEquals(diff, 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() {
|
||||
ZonedDateTime now = ZonedDateTime.now();
|
||||
ZonedDateTime sixDaysBehind = now.minusDays(6);
|
||||
|
||||
Duration duration = Duration.between(now, sixDaysBehind);
|
||||
long diff = Math.abs(duration.toDays());
|
||||
|
||||
assertEquals(diff, 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() {
|
||||
DateTime now = DateTime.now();
|
||||
DateTime sixDaysBehind = now.minusDays(6);
|
||||
|
||||
org.joda.time.Duration duration = new org.joda.time.Duration(now, sixDaysBehind);
|
||||
long diff = Math.abs(duration.getStandardDays());
|
||||
|
||||
assertEquals(diff, 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() {
|
||||
hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault());
|
||||
hirondelle.date4j.DateTime sixDaysBehind = now.minusDays(6);
|
||||
|
||||
long diff = Math.abs(now.numDaysFrom(sixDaysBehind));
|
||||
|
||||
assertEquals(diff, 6);
|
||||
}
|
||||
}
|
|
@ -27,6 +27,7 @@
|
|||
- [Compact Strings in Java 9](http://www.baeldung.com/java-9-compact-string)
|
||||
- [Java Check a String for Lowercase/Uppercase Letter, Special Character and Digit](https://www.baeldung.com/java-lowercase-uppercase-special-character-digit-regex)
|
||||
- [Convert java.util.Date to String](https://www.baeldung.com/java-util-date-to-string)
|
||||
- [Get Substring from String in Java](https://www.baeldung.com/java-substring)
|
||||
- [Converting a Stack Trace to a String in Java](https://www.baeldung.com/java-stacktrace-to-string)
|
||||
- [Sorting a String Alphabetically in Java](https://www.baeldung.com/java-sort-string-alphabetically)
|
||||
- [Remove Emojis from a Java String](https://www.baeldung.com/java-string-remove-emojis)
|
||||
|
|
|
@ -63,11 +63,11 @@
|
|||
|
||||
<properties>
|
||||
<validation-api.version>2.0.1.Final</validation-api.version>
|
||||
<hibernate-validator.version>6.0.7.Final</hibernate-validator.version>
|
||||
<hibernate-validator.version>6.0.13.Final</hibernate-validator.version>
|
||||
<javax.el-api.version>3.0.0</javax.el-api.version>
|
||||
<javax.el.version>2.2.6</javax.el.version>
|
||||
<javax.el.version>3.0.0</javax.el.version>
|
||||
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
</properties>
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
- [Jersey Filters and Interceptors](http://www.baeldung.com/jersey-filters-interceptors)
|
||||
- [Jersey MVC Support](https://www.baeldung.com/jersey-mvc)
|
||||
- [Bean Validation in Jersey](https://www.baeldung.com/jersey-bean-validation)
|
||||
- [Set a Response Body in JAX-RS](https://www.baeldung.com/jax-rs-response)
|
||||
|
|
|
@ -11,3 +11,4 @@
|
|||
- [Apache Ignite with Spring Data](http://www.baeldung.com/apache-ignite-spring-data)
|
||||
- [Guide to JMapper](https://www.baeldung.com/jmapper)
|
||||
- [A Guide to Apache Crunch](https://www.baeldung.com/apache-crunch)
|
||||
- [Building a Data Pipeline with Flink and Kafka](https://www.baeldung.com/kafka-flink-data-pipeline)
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>versions-maven-plugin-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<commons-compress-version>1.15</commons-compress-version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>4.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-compress</artifactId>
|
||||
<version>${commons-compress-version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-beanutils</groupId>
|
||||
<artifactId>commons-beanutils</artifactId>
|
||||
<version>1.9.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>versions-maven-plugin</artifactId>
|
||||
<version>2.7</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>org.apache.commons:commons-collections4</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>apache.snapshots</id>
|
||||
<name>Apache Development Snapshot Repository</name>
|
||||
<url>https://repository.apache.org/content/repositories/snapshots/</url>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,76 @@
|
|||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>versions-maven-plugin-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<commons-compress-version>1.15</commons-compress-version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>4.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-compress</artifactId>
|
||||
<version>${commons-compress-version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-beanutils</groupId>
|
||||
<artifactId>commons-beanutils</artifactId>
|
||||
<version>1.9.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>versions-maven-plugin</artifactId>
|
||||
<version>2.7</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>org.apache.commons:commons-collections4</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>apache.snapshots</id>
|
||||
<name>Apache Development Snapshot Repository</name>
|
||||
<url>https://repository.apache.org/content/repositories/snapshots/</url>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,74 @@
|
|||
#!/bin/bash
|
||||
#function to display commands
|
||||
exe() { echo -e "\$ $@\n" ; "$@" ; }
|
||||
|
||||
TEXT_COLOR='\033[1;33m' #Yellow
|
||||
NO_COLOR='\033[0m' # No Color
|
||||
|
||||
clear
|
||||
|
||||
echo -e "======================================================================================"
|
||||
echo -e " Showcase for the BAELDUNG tutorial \"Use the latest version of a dependency in Maven\""
|
||||
echo -e " Author: Andrea Ligios"
|
||||
echo -e "======================================================================================"
|
||||
|
||||
echo -e "${TEXT_COLOR}\n--------------------------------------------------------------------------------------"
|
||||
echo -e " Resetting the demo environment (which will be altered during the run): "
|
||||
echo -e "--------------------------------------------------------------------------------------${NO_COLOR}"
|
||||
rm -f pom.xml.versionsBackup
|
||||
cp original/pom.xml pom.xml
|
||||
ls -lt pom.*
|
||||
echo -e "${TEXT_COLOR}\n--------------------------------------------------------------------------------------"
|
||||
echo -e " Checking for newer versions of the Maven dependencies:"
|
||||
echo -e "--------------------------------------------------------------------------------------${NO_COLOR}"
|
||||
exe mvn versions:display-dependency-updates
|
||||
echo
|
||||
read -p "Press enter to continue"
|
||||
|
||||
echo -e "${TEXT_COLOR}\n--------------------------------------------------------------------------------------"
|
||||
echo -e " Updating SNAPSHOT dependencies to their RELEASE version, if any:"
|
||||
echo -e "--------------------------------------------------------------------------------------${NO_COLOR}"
|
||||
exe mvn versions:use-releases
|
||||
echo -e "${TEXT_COLOR}\n--------------------------------------------------------------------------------------"
|
||||
echo -e " A backup has been created automatically:"
|
||||
echo -e "--------------------------------------------------------------------------------------${NO_COLOR}"
|
||||
ls -lt pom.*
|
||||
echo
|
||||
read -p "Press enter to continue"
|
||||
|
||||
echo -e "${TEXT_COLOR}\n--------------------------------------------------------------------------------------"
|
||||
echo -e " Updating RELEASE dependencies to their *next* RELEASE version:"
|
||||
echo -e "--------------------------------------------------------------------------------------${NO_COLOR}"
|
||||
exe mvn versions:use-next-releases
|
||||
echo
|
||||
read -p "Press enter to continue"
|
||||
|
||||
echo -e "${TEXT_COLOR}\n--------------------------------------------------------------------------------------"
|
||||
echo -e " Reverting every modification made since the beginning:"
|
||||
echo -e "--------------------------------------------------------------------------------------${NO_COLOR}"
|
||||
exe mvn versions:revert
|
||||
echo -e "${TEXT_COLOR}\n--------------------------------------------------------------------------------------"
|
||||
echo -e " The backup is gone, and the pom.xml contains the initial dependencies:"
|
||||
echo -e "--------------------------------------------------------------------------------------${NO_COLOR}"
|
||||
ls -lt pom.*
|
||||
echo
|
||||
read -p "Press enter to continue"
|
||||
|
||||
echo -e "${TEXT_COLOR}\n--------------------------------------------------------------------------------------"
|
||||
echo -e " Updating RELEASE dependencies to their *latest* RELEASE version:"
|
||||
echo -e "--------------------------------------------------------------------------------------${NO_COLOR}"
|
||||
exe mvn versions:use-latest-releases
|
||||
echo
|
||||
read -p "Press enter to continue"
|
||||
|
||||
echo -e "${TEXT_COLOR}\n--------------------------------------------------------------------------------------"
|
||||
echo -e " Committing the modifications to pom.xml:"
|
||||
echo -e "--------------------------------------------------------------------------------------${NO_COLOR}"
|
||||
exe mvn versions:commit
|
||||
echo -e "${TEXT_COLOR}\n--------------------------------------------------------------------------------------"
|
||||
echo -e " The backup is gone, and the pom.xml contains the latest dependencies:"
|
||||
echo -e "--------------------------------------------------------------------------------------${NO_COLOR}"
|
||||
ls -lt pom.*
|
||||
echo
|
||||
|
||||
echo -e "${TEXT_COLOR}\nThat's all folks!${NO_COLOR}\n"
|
2
pom.xml
2
pom.xml
|
@ -665,7 +665,6 @@
|
|||
<module>dubbo</module>
|
||||
<module>flyway</module>
|
||||
<!-- <module>grpc</module> --><!-- protobuf-maven-plugin filecopy failure -->
|
||||
<module>java-difference-date</module>
|
||||
<!-- <module>JGit</module> --><!-- Unit test failure -->
|
||||
<module>jni</module>
|
||||
<module>jooby</module>
|
||||
|
@ -1138,7 +1137,6 @@
|
|||
<module>dubbo</module>
|
||||
<module>flyway</module>
|
||||
<!-- <module>grpc</module> --><!-- protobuf-maven-plugin filecopy failure -->
|
||||
<module>java-difference-date</module>
|
||||
<!-- <module>JGit</module> --><!-- Unit test failure -->
|
||||
<module>jni</module>
|
||||
<module>jooby</module>
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
# Overview
|
||||
This is an example of a ETL stream pipeline, mixing a starter application with custom transform and sink.
|
||||
|
||||
# Applications
|
||||
JDBC Source - Application Starter distributed by default
|
||||
|
||||
customer-transform - Custom application to transform the data
|
||||
|
||||
customer-mongodb-sink - Custom application to sink the data
|
|
@ -0,0 +1,75 @@
|
|||
<?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>
|
||||
|
||||
<groupId>com.customer</groupId>
|
||||
<artifactId>customer-mongodb-sink</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>customer-mongodb-sink</name>
|
||||
<description>Example ETL Load Project</description>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-binder-rabbit</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-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-test-support</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
|
@ -0,0 +1,27 @@
|
|||
package com.customer.customermongodbsink;
|
||||
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
@Document(collection = "customer")
|
||||
public class Customer {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.customer.customermongodbsink;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Sink;
|
||||
|
||||
@EnableBinding(Sink.class)
|
||||
public class CustomerListener {
|
||||
|
||||
@Autowired
|
||||
private CustomerRepository repository;
|
||||
|
||||
@StreamListener(Sink.INPUT)
|
||||
public void save(Customer customer) {
|
||||
repository.save(customer);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.customer.customermongodbsink;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class CustomerMongodbSinkApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CustomerMongodbSinkApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.customer.customermongodbsink;
|
||||
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CustomerRepository extends MongoRepository<Customer, Long> {
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
<?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>
|
||||
|
||||
<groupId>com.customer</groupId>
|
||||
<artifactId>customer-transform</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>customer-transform</name>
|
||||
<description>Example transform ETL step</description>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-test-support</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
|
@ -0,0 +1,29 @@
|
|||
package com.customer.customertransform;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@JsonProperty("customer_name")
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@JsonProperty("name")
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.customer.customertransform;
|
||||
|
||||
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.messaging.Processor;
|
||||
import org.springframework.integration.annotation.Transformer;
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
public class CustomerProcessorConfiguration {
|
||||
|
||||
@Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
|
||||
public Customer convertToPojo(Customer payload) {
|
||||
|
||||
return payload;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.customer.customertransform;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class CustomerTransformApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CustomerTransformApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<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>
|
||||
<groupId>org.baeldung.spring.cloud</groupId>
|
||||
<artifactId>etl-spring-cloud-data-flow</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>org.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-data-flow</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>customer-mongodb-sink</module>
|
||||
<module>customer-transform</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
|
@ -19,6 +19,7 @@
|
|||
<module>time-processor</module>
|
||||
<module>log-sink</module>
|
||||
<module>batch-job</module>
|
||||
<module>etl</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -8,9 +8,9 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-4</artifactId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-spring-4</relativePath>
|
||||
<relativePath>../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -19,6 +19,28 @@
|
|||
<artifactId>spring-data-mongodb</artifactId>
|
||||
<version>${org.springframework.data.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-releasetrain</artifactId>
|
||||
<version>Lovelace-M3</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-reactivestreams</artifactId>
|
||||
<version>${mongodb-reactivestreams.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-test</artifactId>
|
||||
<version>${projectreactor.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
|
@ -48,6 +70,17 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
@ -70,10 +103,12 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<org.springframework.data.version>1.10.4.RELEASE</org.springframework.data.version>
|
||||
<rest-assured.version>2.9.0</rest-assured.version>
|
||||
<org.springframework.data.version>2.1.0.RELEASE</org.springframework.data.version>
|
||||
<querydsl.version>4.1.4</querydsl.version>
|
||||
<mysema.maven.version>1.1.3</mysema.maven.version>
|
||||
<spring.version>5.1.0.RELEASE</spring.version>
|
||||
<mongodb-reactivestreams.version>1.9.2</mongodb-reactivestreams.version>
|
||||
<projectreactor.version>3.2.0.RELEASE</projectreactor.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -1,21 +1,23 @@
|
|||
package com.baeldung.config;
|
||||
|
||||
import com.mongodb.Mongo;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.baeldung.converter.UserWriterConverter;
|
||||
import com.baeldung.event.CascadeSaveMongoEventListener;
|
||||
import com.baeldung.event.UserCascadeSaveMongoEventListener;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
|
||||
import org.springframework.data.mongodb.core.convert.CustomConversions;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.MongoTransactionManager;
|
||||
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
|
||||
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import com.baeldung.converter.UserWriterConverter;
|
||||
import com.baeldung.event.CascadeSaveMongoEventListener;
|
||||
import com.baeldung.event.UserCascadeSaveMongoEventListener;
|
||||
import com.mongodb.MongoClient;
|
||||
|
||||
@Configuration
|
||||
@EnableMongoRepositories(basePackages = "com.baeldung.repository")
|
||||
public class MongoConfig extends AbstractMongoConfiguration {
|
||||
|
@ -28,7 +30,7 @@ public class MongoConfig extends AbstractMongoConfiguration {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Mongo mongo() throws Exception {
|
||||
public MongoClient mongoClient() {
|
||||
return new MongoClient("127.0.0.1", 27017);
|
||||
}
|
||||
|
||||
|
@ -48,13 +50,19 @@ public class MongoConfig extends AbstractMongoConfiguration {
|
|||
}
|
||||
|
||||
@Override
|
||||
public CustomConversions customConversions() {
|
||||
public MongoCustomConversions customConversions() {
|
||||
converters.add(new UserWriterConverter());
|
||||
return new CustomConversions(converters);
|
||||
return new MongoCustomConversions(converters);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GridFsTemplate gridFsTemplate() throws Exception {
|
||||
return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter());
|
||||
}
|
||||
|
||||
@Bean
|
||||
MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
|
||||
return new MongoTransactionManager(dbFactory);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration;
|
||||
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
|
||||
|
||||
import com.mongodb.reactivestreams.client.MongoClient;
|
||||
import com.mongodb.reactivestreams.client.MongoClients;
|
||||
|
||||
@Configuration
|
||||
@EnableReactiveMongoRepositories(basePackages = "com.baeldung.reactive.repository")
|
||||
public class MongoReactiveConfig extends AbstractReactiveMongoConfiguration {
|
||||
|
||||
@Override
|
||||
public MongoClient reactiveMongoClient() {
|
||||
return MongoClients.create();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseName() {
|
||||
return "reactive";
|
||||
}
|
||||
}
|
|
@ -1,18 +1,18 @@
|
|||
package com.baeldung.config;
|
||||
|
||||
import com.mongodb.Mongo;
|
||||
import com.mongodb.MongoClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
|
||||
@Configuration
|
||||
@EnableMongoRepositories(basePackages = "com.baeldung.repository")
|
||||
public class SimpleMongoConfig {
|
||||
|
||||
@Bean
|
||||
public Mongo mongo() throws Exception {
|
||||
public MongoClient mongo() throws Exception {
|
||||
return new MongoClient("localhost");
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.reactive.repository;
|
||||
|
||||
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
|
||||
|
||||
import com.baeldung.model.User;
|
||||
|
||||
|
||||
public interface UserRepository extends ReactiveMongoRepository<User, String> {
|
||||
}
|
|
@ -1,13 +1,14 @@
|
|||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.model.User;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.data.mongodb.repository.Query;
|
||||
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserRepository extends MongoRepository<User, String>, QueryDslPredicateExecutor<User> {
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.data.mongodb.repository.Query;
|
||||
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
||||
|
||||
import com.baeldung.model.User;
|
||||
|
||||
public interface UserRepository extends MongoRepository<User, String>, QuerydslPredicateExecutor<User> {
|
||||
@Query("{ 'name' : ?0 }")
|
||||
List<User> findUsersByName(String name);
|
||||
|
||||
|
|
|
@ -3,17 +3,17 @@
|
|||
xmlns:p="http://www.springframework.org/schema/p" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/data/mongo
|
||||
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
|
||||
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
|
||||
http://www.springframework.org/schema/context/spring-context.xsd"
|
||||
>
|
||||
<context:annotation-config/>
|
||||
|
||||
<mongo:mongo id="mongo" host="localhost" />
|
||||
<mongo:mongo-client id="mongoClient" host="localhost" />
|
||||
|
||||
<mongo:db-factory id="mongoDbFactory" dbname="test" mongo-ref="mongo" />
|
||||
<mongo:db-factory id="mongoDbFactory" dbname="test" mongo-ref="mongoClient" />
|
||||
|
||||
<bean id="gridFsTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
|
||||
<constructor-arg ref="mongoDbFactory" />
|
||||
|
@ -27,12 +27,12 @@
|
|||
|
||||
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
|
||||
|
||||
<mongo:repositories base-package="org.baeldung.repository" mongo-template-ref="mongoTemplate"/>
|
||||
<mongo:repositories base-package="com.baeldung.repository" mongo-template-ref="mongoTemplate"/>
|
||||
|
||||
<bean class="org.baeldung.event.CascadeSaveMongoEventListener">
|
||||
<bean class="com.baeldung.event.CascadeSaveMongoEventListener">
|
||||
</bean>
|
||||
|
||||
<mongo:mapping-converter id="mongoConverter" base-package="org.baeldung.converter">
|
||||
<mongo:mapping-converter id="mongoConverter" base-package="com.baeldung.converter">
|
||||
<mongo:custom-converters base-package="org.baeldung.converter"/>
|
||||
</mongo:mapping-converter>
|
||||
|
||||
|
|
|
@ -1,12 +1,24 @@
|
|||
package com.baeldung.aggregation;
|
||||
|
||||
import com.mongodb.DB;
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.util.JSON;
|
||||
import com.baeldung.aggregation.model.StatePopulation;
|
||||
import com.baeldung.config.MongoConfig;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.limit;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.sort;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
@ -26,23 +38,11 @@ import org.springframework.data.mongodb.core.query.Criteria;
|
|||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.limit;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.sort;
|
||||
import com.baeldung.aggregation.model.StatePopulation;
|
||||
import com.baeldung.config.MongoConfig;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = MongoConfig.class)
|
||||
|
@ -56,23 +56,22 @@ public class ZipsAggregationLiveTest {
|
|||
@BeforeClass
|
||||
public static void setupTests() throws Exception {
|
||||
client = new MongoClient();
|
||||
DB testDB = client.getDB("test");
|
||||
DBCollection zipsCollection = testDB.getCollection("zips");
|
||||
MongoDatabase testDB = client.getDatabase("test");
|
||||
MongoCollection<Document> zipsCollection = testDB.getCollection("zips");
|
||||
zipsCollection.drop();
|
||||
|
||||
InputStream zipsJsonStream = ZipsAggregationLiveTest.class.getResourceAsStream("/zips.json");
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(zipsJsonStream));
|
||||
reader.lines()
|
||||
.forEach(line -> zipsCollection.insert((DBObject) JSON.parse(line)));
|
||||
.forEach(line -> zipsCollection.insertOne(Document.parse(line)));
|
||||
reader.close();
|
||||
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() throws Exception {
|
||||
client = new MongoClient();
|
||||
DB testDB = client.getDB("test");
|
||||
DBCollection zipsCollection = testDB.getCollection("zips");
|
||||
MongoDatabase testDB = client.getDatabase("test");
|
||||
MongoCollection<Document> zipsCollection = testDB.getCollection("zips");
|
||||
zipsCollection.drop();
|
||||
client.close();
|
||||
}
|
||||
|
@ -140,13 +139,13 @@ public class ZipsAggregationLiveTest {
|
|||
|
||||
Aggregation aggregation = newAggregation(sumZips, sortByCount, groupFirstAndLast);
|
||||
|
||||
AggregationResults<DBObject> result = mongoTemplate.aggregate(aggregation, "zips", DBObject.class);
|
||||
DBObject dbObject = result.getUniqueMappedResult();
|
||||
AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, "zips", Document.class);
|
||||
Document document = result.getUniqueMappedResult();
|
||||
|
||||
assertEquals("DC", dbObject.get("minZipState"));
|
||||
assertEquals(24, dbObject.get("minZipCount"));
|
||||
assertEquals("TX", dbObject.get("maxZipState"));
|
||||
assertEquals(1671, dbObject.get("maxZipCount"));
|
||||
assertEquals("DC", document.get("minZipState"));
|
||||
assertEquals(24, document.get("minZipCount"));
|
||||
assertEquals("TX", document.get("maxZipState"));
|
||||
assertEquals(1671, document.get("maxZipCount"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,8 +1,19 @@
|
|||
package com.baeldung.gridfs;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.gridfs.GridFSDBFile;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
@ -16,18 +27,9 @@ import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
|||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.client.gridfs.model.GridFSFile;
|
||||
|
||||
@ContextConfiguration("file:src/main/resources/mongoConfig.xml")
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
|
@ -40,8 +42,9 @@ public class GridFSLiveTest {
|
|||
|
||||
@After
|
||||
public void tearDown() {
|
||||
List<GridFSDBFile> fileList = gridFsTemplate.find(null);
|
||||
for (GridFSDBFile file : fileList) {
|
||||
List<GridFSFile> fileList = new ArrayList<GridFSFile>();
|
||||
gridFsTemplate.find(new Query()).into(fileList);
|
||||
for (GridFSFile file : fileList) {
|
||||
gridFsTemplate.delete(new Query(Criteria.where("filename").is(file.getFilename())));
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +57,7 @@ public class GridFSLiveTest {
|
|||
String id = "";
|
||||
try {
|
||||
inputStream = new FileInputStream("src/main/resources/test.png");
|
||||
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
|
||||
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).toString();
|
||||
} catch (FileNotFoundException ex) {
|
||||
logger.error("File not found", ex);
|
||||
} finally {
|
||||
|
@ -75,10 +78,10 @@ public class GridFSLiveTest {
|
|||
DBObject metaData = new BasicDBObject();
|
||||
metaData.put("user", "alex");
|
||||
InputStream inputStream = null;
|
||||
String id = "";
|
||||
ObjectId id = null;
|
||||
try {
|
||||
inputStream = new FileInputStream("src/main/resources/test.png");
|
||||
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
|
||||
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData);
|
||||
} catch (FileNotFoundException ex) {
|
||||
logger.error("File not found", ex);
|
||||
} finally {
|
||||
|
@ -91,22 +94,22 @@ public class GridFSLiveTest {
|
|||
}
|
||||
}
|
||||
|
||||
GridFSDBFile gridFSDBFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id)));
|
||||
GridFSFile gridFSFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id)));
|
||||
|
||||
assertNotNull(gridFSDBFile);
|
||||
assertNotNull(gridFSDBFile.getInputStream());
|
||||
assertThat(gridFSDBFile.numChunks(), is(1));
|
||||
assertThat(gridFSDBFile.containsField("filename"), is(true));
|
||||
assertThat(gridFSDBFile.get("filename"), is("test.png"));
|
||||
assertThat(gridFSDBFile.getId(), is(id));
|
||||
assertThat(gridFSDBFile.keySet().size(), is(9));
|
||||
assertNotNull(gridFSDBFile.getMD5());
|
||||
assertNotNull(gridFSDBFile.getUploadDate());
|
||||
assertNull(gridFSDBFile.getAliases());
|
||||
assertNotNull(gridFSDBFile.getChunkSize());
|
||||
assertThat(gridFSDBFile.getContentType(), is("image/png"));
|
||||
assertThat(gridFSDBFile.getFilename(), is("test.png"));
|
||||
assertThat(gridFSDBFile.getMetaData().get("user"), is("alex"));
|
||||
assertNotNull(gridFSFile);
|
||||
// assertNotNull(gridFSFile.getInputStream());
|
||||
// assertThat(gridFSFile.numChunks(), is(1));
|
||||
// assertThat(gridFSFile.containsField("filename"), is(true));
|
||||
assertThat(gridFSFile.getFilename(), is("test.png"));
|
||||
assertThat(gridFSFile.getObjectId(), is(id));
|
||||
// assertThat(gridFSFile.keySet().size(), is(9));
|
||||
// assertNotNull(gridFSFile.getMD5());
|
||||
assertNotNull(gridFSFile.getUploadDate());
|
||||
// assertNull(gridFSFile.getAliases());
|
||||
assertNotNull(gridFSFile.getChunkSize());
|
||||
assertThat(gridFSFile.getMetadata().get("_contentType"), is("image/png"));
|
||||
assertThat(gridFSFile.getFilename(), is("test.png"));
|
||||
assertThat(gridFSFile.getMetadata().get("user"), is("alex"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -133,10 +136,11 @@ public class GridFSLiveTest {
|
|||
}
|
||||
}
|
||||
|
||||
List<GridFSDBFile> gridFSDBFiles = gridFsTemplate.find(null);
|
||||
List<GridFSFile> gridFSFiles = new ArrayList<GridFSFile>();
|
||||
gridFsTemplate.find(new Query()).into(gridFSFiles);
|
||||
|
||||
assertNotNull(gridFSDBFiles);
|
||||
assertThat(gridFSDBFiles.size(), is(2));
|
||||
assertNotNull(gridFSFiles);
|
||||
assertThat(gridFSFiles.size(), is(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -163,10 +167,11 @@ public class GridFSLiveTest {
|
|||
}
|
||||
}
|
||||
|
||||
List<GridFSDBFile> gridFSDBFiles = gridFsTemplate.find(new Query(Criteria.where("metadata.user").is("alex")));
|
||||
List<GridFSFile> gridFSFiles = new ArrayList<GridFSFile>();
|
||||
gridFsTemplate.find(new Query(Criteria.where("metadata.user").is("alex"))).into(gridFSFiles);
|
||||
|
||||
assertNotNull(gridFSDBFiles);
|
||||
assertThat(gridFSDBFiles.size(), is(1));
|
||||
assertNotNull(gridFSFiles);
|
||||
assertThat(gridFSFiles.size(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -177,7 +182,7 @@ public class GridFSLiveTest {
|
|||
String id = "";
|
||||
try {
|
||||
inputStream = new FileInputStream("src/main/resources/test.png");
|
||||
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
|
||||
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).toString();
|
||||
} catch (FileNotFoundException ex) {
|
||||
logger.error("File not found", ex);
|
||||
} finally {
|
||||
|
@ -200,10 +205,9 @@ public class GridFSLiveTest {
|
|||
DBObject metaData = new BasicDBObject();
|
||||
metaData.put("user", "alex");
|
||||
InputStream inputStream = null;
|
||||
String id = "";
|
||||
try {
|
||||
inputStream = new FileInputStream("src/main/resources/test.png");
|
||||
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
|
||||
gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).toString();
|
||||
} catch (FileNotFoundException ex) {
|
||||
logger.error("File not found", ex);
|
||||
} finally {
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package com.baeldung.mongotemplate;
|
||||
|
||||
import com.baeldung.config.MongoConfig;
|
||||
import com.baeldung.model.EmailAddress;
|
||||
import com.baeldung.model.User;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -17,11 +20,9 @@ import org.springframework.data.mongodb.core.query.Query;
|
|||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import com.baeldung.config.MongoConfig;
|
||||
import com.baeldung.model.EmailAddress;
|
||||
import com.baeldung.model.User;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = MongoConfig.class)
|
||||
|
@ -152,7 +153,7 @@ public class DocumentQueryLiveTest {
|
|||
user.setAge(35);
|
||||
mongoTemplate.insert(user);
|
||||
|
||||
final Pageable pageableRequest = new PageRequest(0, 2);
|
||||
final Pageable pageableRequest = PageRequest.of(0, 2);
|
||||
Query query = new Query();
|
||||
query.with(pageableRequest);
|
||||
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package com.baeldung.mongotemplate;
|
||||
|
||||
import com.baeldung.config.MongoConfig;
|
||||
import com.baeldung.model.EmailAddress;
|
||||
import com.baeldung.model.User;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -20,11 +23,9 @@ import org.springframework.data.mongodb.core.query.Query;
|
|||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import com.baeldung.config.MongoConfig;
|
||||
import com.baeldung.model.EmailAddress;
|
||||
import com.baeldung.model.User;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = MongoConfig.class)
|
||||
|
@ -104,7 +105,7 @@ public class MongoTemplateQueryLiveTest {
|
|||
user.setAge(35);
|
||||
mongoTemplate.insert(user);
|
||||
|
||||
final Pageable pageableRequest = new PageRequest(0, 2);
|
||||
final Pageable pageableRequest = PageRequest.of(0, 2);
|
||||
Query query = new Query();
|
||||
query.with(pageableRequest);
|
||||
|
||||
|
|
|
@ -5,8 +5,6 @@ import static org.junit.Assert.assertThat;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.config.MongoConfig;
|
||||
import com.baeldung.model.User;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -22,6 +20,9 @@ import org.springframework.data.mongodb.core.query.Query;
|
|||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.config.MongoConfig;
|
||||
import com.baeldung.model.User;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = MongoConfig.class)
|
||||
public class UserRepositoryLiveTest {
|
||||
|
@ -72,8 +73,7 @@ public class UserRepositoryLiveTest {
|
|||
|
||||
user.setName("Jim");
|
||||
userRepository.save(user);
|
||||
|
||||
assertThat(mongoOps.findAll(User.class).size(), is(2));
|
||||
assertThat(mongoOps.findAll(User.class).size(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -94,7 +94,7 @@ public class UserRepositoryLiveTest {
|
|||
mongoOps.insert(user);
|
||||
|
||||
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Chris")), User.class);
|
||||
final User foundUser = userRepository.findOne(user.getId());
|
||||
final User foundUser = userRepository.findById(user.getId()).get();
|
||||
|
||||
assertThat(user.getName(), is(foundUser.getName()));
|
||||
}
|
||||
|
@ -106,7 +106,7 @@ public class UserRepositoryLiveTest {
|
|||
mongoOps.insert(user);
|
||||
|
||||
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Harris")), User.class);
|
||||
final boolean isExists = userRepository.exists(user.getId());
|
||||
final boolean isExists = userRepository.existsById(user.getId());
|
||||
|
||||
assertThat(isExists, is(true));
|
||||
}
|
||||
|
@ -138,7 +138,7 @@ public class UserRepositoryLiveTest {
|
|||
user.setName("Adam");
|
||||
mongoOps.insert(user);
|
||||
|
||||
final Pageable pageableRequest = new PageRequest(0, 1);
|
||||
final Pageable pageableRequest = PageRequest.of(0, 1);
|
||||
|
||||
final Page<User> page = userRepository.findAll(pageableRequest);
|
||||
List<User> users = page.getContent();
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.transaction;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.config.MongoReactiveConfig;
|
||||
import com.baeldung.model.User;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = MongoReactiveConfig.class)
|
||||
public class MongoTransactionReactiveIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ReactiveMongoOperations reactiveOps;
|
||||
|
||||
@Before
|
||||
public void testSetup() {
|
||||
if (!reactiveOps.collectionExists(User.class)
|
||||
.block()) {
|
||||
reactiveOps.createCollection(User.class);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
System.out.println(reactiveOps.findAll(User.class)
|
||||
.count()
|
||||
.block());
|
||||
reactiveOps.dropCollection(User.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPerformTransaction_thenSuccess() {
|
||||
User user1 = new User("Jane", 23);
|
||||
User user2 = new User("John", 34);
|
||||
reactiveOps.inTransaction()
|
||||
.execute(action -> action.insert(user1)
|
||||
.then(action.insert(user2)));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.transaction;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.MongoTransactionManager;
|
||||
import org.springframework.data.mongodb.SessionSynchronization;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import com.baeldung.config.MongoConfig;
|
||||
import com.baeldung.model.User;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = MongoConfig.class)
|
||||
public class MongoTransactionTemplateIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MongoTemplate mongoTemplate;
|
||||
|
||||
@Autowired
|
||||
private MongoTransactionManager mongoTransactionManager;
|
||||
|
||||
@Before
|
||||
public void testSetup() {
|
||||
if (!mongoTemplate.collectionExists(User.class)) {
|
||||
mongoTemplate.createCollection(User.class);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
mongoTemplate.dropCollection(User.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTransactionTemplate_whenPerformTransaction_thenSuccess() {
|
||||
mongoTemplate.setSessionSynchronization(SessionSynchronization.ALWAYS);
|
||||
TransactionTemplate transactionTemplate = new TransactionTemplate(mongoTransactionManager);
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
mongoTemplate.insert(new User("Kim", 20));
|
||||
mongoTemplate.insert(new User("Jack", 45));
|
||||
};
|
||||
});
|
||||
|
||||
Query query = new Query().addCriteria(Criteria.where("name")
|
||||
.is("Jack"));
|
||||
List<User> users = mongoTemplate.find(query, User.class);
|
||||
|
||||
assertThat(users.size(), is(1));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
package com.baeldung.transaction;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.MongoTransactionException;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baeldung.config.MongoConfig;
|
||||
import com.baeldung.model.User;
|
||||
import com.baeldung.repository.UserRepository;
|
||||
import com.mongodb.MongoCommandException;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = MongoConfig.class)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class MongoTransactionalIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MongoTemplate mongoTemplate;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void whenPerformMongoTransaction_thenSuccess() {
|
||||
userRepository.save(new User("John", 30));
|
||||
userRepository.save(new User("Ringo", 35));
|
||||
Query query = new Query().addCriteria(Criteria.where("name")
|
||||
.is("John"));
|
||||
List<User> users = mongoTemplate.find(query, User.class);
|
||||
|
||||
assertThat(users.size(), is(1));
|
||||
}
|
||||
|
||||
@Test(expected = MongoTransactionException.class)
|
||||
@Transactional
|
||||
public void whenListCollectionDuringMongoTransaction_thenException() {
|
||||
if (mongoTemplate.collectionExists(User.class)) {
|
||||
mongoTemplate.save(new User("John", 30));
|
||||
mongoTemplate.save(new User("Ringo", 35));
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = MongoCommandException.class)
|
||||
@Transactional
|
||||
public void whenCountDuringMongoTransaction_thenException() {
|
||||
userRepository.save(new User("John", 30));
|
||||
userRepository.save(new User("Ringo", 35));
|
||||
userRepository.count();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void whenQueryDuringMongoTransaction_thenSuccess() {
|
||||
userRepository.save(new User("Jane", 20));
|
||||
userRepository.save(new User("Nick", 33));
|
||||
List<User> users = mongoTemplate.find(new Query(), User.class);
|
||||
|
||||
assertTrue(users.size() > 1);
|
||||
}
|
||||
|
||||
// ==== Using test instead of before and after due to @transactional doesn't allow list collection
|
||||
|
||||
@Test
|
||||
public void setup() {
|
||||
if (!mongoTemplate.collectionExists(User.class)) {
|
||||
mongoTemplate.createCollection(User.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ztearDown() {
|
||||
mongoTemplate.dropCollection(User.class);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue