Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
39a9b7cb4b
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.nullsafecollectionstreams;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import static org.apache.commons.collections4.CollectionUtils.emptyIfNull;
|
||||||
|
|
||||||
|
public class NullSafeCollectionStreamsUsingCommonsEmptyIfNull {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method shows how to make a null safe stream from a collection through the use of
|
||||||
|
* emptyIfNull() method from Apache Commons CollectionUtils library
|
||||||
|
*
|
||||||
|
* @param collection The collection that is to be converted into a stream
|
||||||
|
* @return The stream that has been created from the collection or an empty stream if the collection is null
|
||||||
|
*/
|
||||||
|
public Stream<String> collectionAsStream(Collection<String> collection) {
|
||||||
|
return emptyIfNull(collection).stream();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.nullsafecollectionstreams;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class NullSafeCollectionStreamsUsingJava8OptionalContainer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method shows how to make a null safe stream from a collection through the use of
|
||||||
|
* Java SE 8’s Optional Container
|
||||||
|
*
|
||||||
|
* @param collection The collection that is to be converted into a stream
|
||||||
|
* @return The stream that has been created from the collection or an empty stream if the collection is null
|
||||||
|
*/
|
||||||
|
public Stream<String> collectionAsStream(Collection<String> collection) {
|
||||||
|
return Optional.ofNullable(collection)
|
||||||
|
.map(Collection::stream)
|
||||||
|
.orElseGet(Stream::empty);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.nullsafecollectionstreams;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class NullSafeCollectionStreamsUsingNullDereferenceCheck {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method shows how to make a null safe stream from a collection through the use of a check
|
||||||
|
* to prevent null dereferences
|
||||||
|
*
|
||||||
|
* @param collection The collection that is to be converted into a stream
|
||||||
|
* @return The stream that has been created from the collection or an empty stream if the collection is null
|
||||||
|
*/
|
||||||
|
public Stream<String> collectionAsStream(Collection<String> collection) {
|
||||||
|
return collection == null ? Stream.empty() : collection.stream();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
package com.baeldung.list;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class AddElementsUnitTest {
|
||||||
|
|
||||||
|
List<Flower> flowers;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
this.flowers = new ArrayList<>(Arrays.asList(
|
||||||
|
new Flower("Poppy", 12),
|
||||||
|
new Flower("Anemone", 8),
|
||||||
|
new Flower("Catmint", 12)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithNewItems() {
|
||||||
|
List<Flower> anotherList = new ArrayList<>();
|
||||||
|
anotherList.addAll(flowers);
|
||||||
|
|
||||||
|
assertEquals(anotherList.size(), flowers.size());
|
||||||
|
Assert.assertTrue(anotherList.containsAll(flowers));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithOneModifiedElementByConstructor() {
|
||||||
|
List<Flower> anotherList = new ArrayList<>();
|
||||||
|
anotherList.addAll(flowers);
|
||||||
|
|
||||||
|
Flower flower = anotherList.get(0);
|
||||||
|
flower.setPetals(flowers.get(0).getPetals() * 3);
|
||||||
|
|
||||||
|
assertEquals(anotherList.size(), flowers.size());
|
||||||
|
Assert.assertTrue(anotherList.containsAll(flowers));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAListAndElements_whenUseCollectionsAddAll_thenAddElementsToTargetList() {
|
||||||
|
List<Flower> target = new ArrayList<>();
|
||||||
|
|
||||||
|
Collections.addAll(target, flowers.get(0), flowers.get(1), flowers.get(2), flowers.get(0));
|
||||||
|
|
||||||
|
assertEquals(target.size(), 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListSkipFirstElementByStreamProcess() {
|
||||||
|
List<Flower> flowerVase = new ArrayList<>();
|
||||||
|
|
||||||
|
flowers.stream()
|
||||||
|
.skip(1)
|
||||||
|
.forEachOrdered(flowerVase::add);
|
||||||
|
|
||||||
|
assertEquals(flowerVase.size() + 1, flowers.size());
|
||||||
|
assertFalse(flowerVase.containsAll(flowers));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListFilteringElementsByStreamProcess() {
|
||||||
|
List<Flower> flowerVase = new ArrayList<>();
|
||||||
|
|
||||||
|
flowers.stream()
|
||||||
|
.filter(f -> f.getPetals() > 10)
|
||||||
|
.forEachOrdered(flowerVase::add);
|
||||||
|
|
||||||
|
assertEquals(flowerVase.size() + 1, flowers.size());
|
||||||
|
assertFalse(flowerVase.containsAll(flowers));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenListIsNotNull_thenAddElementsToListByStreamProcessWihtOptional() {
|
||||||
|
List<Flower> target = new ArrayList<>();
|
||||||
|
|
||||||
|
Optional.ofNullable(flowers)
|
||||||
|
.ifPresent(target::addAll);
|
||||||
|
|
||||||
|
assertNotNull(target);
|
||||||
|
assertEquals(target.size(), 3);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.baeldung.nullsafecollectionstreams;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest {
|
||||||
|
|
||||||
|
private final NullSafeCollectionStreamsUsingCommonsEmptyIfNull instance =
|
||||||
|
new NullSafeCollectionStreamsUsingCommonsEmptyIfNull();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
|
||||||
|
Collection<String> collection = null;
|
||||||
|
Stream<String> expResult = Stream.empty();
|
||||||
|
Stream<String> result = instance.collectionAsStream(collection);
|
||||||
|
assertStreamEquals(expResult, result);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
|
||||||
|
|
||||||
|
Collection<String> collection = Arrays.asList("a", "b", "c");
|
||||||
|
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
|
||||||
|
Stream<String> result = instance.collectionAsStream(collection);
|
||||||
|
assertStreamEquals(expResult, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
|
||||||
|
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
|
||||||
|
while (iter1.hasNext() && iter2.hasNext())
|
||||||
|
assertEquals(iter1.next(), iter2.next());
|
||||||
|
assert !iter1.hasNext() && !iter2.hasNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package com.baeldung.nullsafecollectionstreams;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Kwaje Anthony <kwajeanthony@gmail.com>
|
||||||
|
*/
|
||||||
|
public class NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest {
|
||||||
|
|
||||||
|
private final NullSafeCollectionStreamsUsingJava8OptionalContainer instance =
|
||||||
|
new NullSafeCollectionStreamsUsingJava8OptionalContainer();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
|
||||||
|
Collection<String> collection = null;
|
||||||
|
Stream<String> expResult = Stream.empty();
|
||||||
|
Stream<String> result = instance.collectionAsStream(collection);
|
||||||
|
assertStreamEquals(expResult, result);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
|
||||||
|
|
||||||
|
Collection<String> collection = Arrays.asList("a", "b", "c");
|
||||||
|
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
|
||||||
|
Stream<String> result = instance.collectionAsStream(collection);
|
||||||
|
assertStreamEquals(expResult, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
|
||||||
|
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
|
||||||
|
while (iter1.hasNext() && iter2.hasNext())
|
||||||
|
assertEquals(iter1.next(), iter2.next());
|
||||||
|
assert !iter1.hasNext() && !iter2.hasNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
|
||||||
|
package com.baeldung.nullsafecollectionstreams;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Kwaje Anthony <kwajeanthony@gmail.com>
|
||||||
|
*/
|
||||||
|
public class NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest {
|
||||||
|
|
||||||
|
private final NullSafeCollectionStreamsUsingNullDereferenceCheck instance =
|
||||||
|
new NullSafeCollectionStreamsUsingNullDereferenceCheck();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
|
||||||
|
Collection<String> collection = null;
|
||||||
|
Stream<String> expResult = Stream.empty();
|
||||||
|
Stream<String> result = instance.collectionAsStream(collection);
|
||||||
|
assertStreamEquals(expResult, result);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
|
||||||
|
|
||||||
|
Collection<String> collection = Arrays.asList("a", "b", "c");
|
||||||
|
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
|
||||||
|
Stream<String> result = instance.collectionAsStream(collection);
|
||||||
|
assertStreamEquals(expResult, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
|
||||||
|
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
|
||||||
|
while (iter1.hasNext() && iter2.hasNext())
|
||||||
|
assertEquals(iter1.next(), iter2.next());
|
||||||
|
assert !iter1.hasNext() && !iter2.hasNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -19,11 +19,23 @@
|
|||||||
<version>${awaitility.version}</version>
|
<version>${awaitility.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.guava</groupId>
|
<groupId>com.google.guava</groupId>
|
||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
<version>${guava.version}</version>
|
<version>${guava.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-runner</artifactId>
|
||||||
|
<version>${junit.platform.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -50,6 +62,8 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- testing -->
|
<!-- testing -->
|
||||||
|
<assertj.version>3.10.0</assertj.version>
|
||||||
|
<junit.platform.version>1.2.0</junit.platform.version>
|
||||||
<awaitility.version>1.7.0</awaitility.version>
|
<awaitility.version>1.7.0</awaitility.version>
|
||||||
<maven.compiler.source>1.9</maven.compiler.source>
|
<maven.compiler.source>1.9</maven.compiler.source>
|
||||||
<maven.compiler.target>1.9</maven.compiler.target>
|
<maven.compiler.target>1.9</maven.compiler.target>
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.java9.language.stream;
|
||||||
|
|
||||||
|
import static java.util.stream.Collectors.filtering;
|
||||||
|
import static java.util.stream.Collectors.groupingBy;
|
||||||
|
import static java.util.stream.Collectors.toList;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public class StreamsGroupingCollectionFilter {
|
||||||
|
|
||||||
|
static public Map<Integer, List<Integer>> findEvenNumbersAfterGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {
|
||||||
|
Function<Integer, Integer> getQuantityOfDigits = item -> (int) Math.log10(item) + 1;
|
||||||
|
|
||||||
|
return baseCollection.stream()
|
||||||
|
.collect(groupingBy(getQuantityOfDigits, filtering(item -> item % 2 == 0, toList())));
|
||||||
|
}
|
||||||
|
|
||||||
|
static public Map<Integer, List<Integer>> findEvenNumbersBeforeGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {
|
||||||
|
|
||||||
|
return baseCollection.stream()
|
||||||
|
.filter(item -> item % 2 == 0)
|
||||||
|
.collect(groupingBy(item -> (int) Math.log10(item) + 1, toList()));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.java9.maps.initialize;
|
||||||
|
|
||||||
|
import java.util.AbstractMap;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class MapsInitializer {
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public void createMapWithMapOf() {
|
||||||
|
Map<String, String> emptyMap = Map.of();
|
||||||
|
Map<String, String> singletonMap = Map.of("key1", "value");
|
||||||
|
Map<String, String> map = Map.of("key1","value1", "key2", "value2");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createMapWithMapEntries() {
|
||||||
|
Map<String, String> map = Map.ofEntries(
|
||||||
|
new AbstractMap.SimpleEntry<String, String>("name", "John"),
|
||||||
|
new AbstractMap.SimpleEntry<String, String>("city", "budapest"),
|
||||||
|
new AbstractMap.SimpleEntry<String, String>("zip", "000000"),
|
||||||
|
new AbstractMap.SimpleEntry<String, String>("home", "1231231231")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public void createMutableMaps() {
|
||||||
|
Map<String, String> map = new HashMap<String, String> (Map.of("key1","value1", "key2", "value2"));
|
||||||
|
Map<String, String> map2 = new HashMap<String, String> ( Map.ofEntries(
|
||||||
|
new AbstractMap.SimpleEntry<String, String>("name", "John"),
|
||||||
|
new AbstractMap.SimpleEntry<String, String>("city", "budapest")));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
package com.baeldung.java9.language.stream;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.platform.runner.JUnitPlatform;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
@RunWith(JUnitPlatform.class)
|
||||||
|
public class CollectionFilterUnitTest {
|
||||||
|
|
||||||
|
private static final Collection<Integer> BASE_INTEGER_COLLECTION = Arrays.asList(9, 12, 55, 56, 101, 115, 8002, 223, 2668, 19, 8);
|
||||||
|
private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP = createExpectedFilterAfterGroupingMap();
|
||||||
|
private static Map<Integer, List<Integer>> createExpectedFilterAfterGroupingMap() {
|
||||||
|
Map<Integer, List<Integer>> map = new HashMap<>();
|
||||||
|
map.put(1, Arrays.asList(8));
|
||||||
|
map.put(2, Arrays.asList(12, 56));
|
||||||
|
map.put(3, Collections.emptyList());
|
||||||
|
map.put(4, Arrays.asList(8002, 2668));
|
||||||
|
return map;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP = createExpectedFilterBeforeGroupingMap();
|
||||||
|
private static Map<Integer, List<Integer>> createExpectedFilterBeforeGroupingMap() {
|
||||||
|
Map<Integer, List<Integer>> map = new HashMap<>();
|
||||||
|
map.put(1, Arrays.asList(8));
|
||||||
|
map.put(2, Arrays.asList(12, 56));
|
||||||
|
map.put(4, Arrays.asList(8002, 2668));
|
||||||
|
return map;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAStringCollection_whenFilteringFourLetterWords_thenObtainTheFilteredCollection() {
|
||||||
|
Map<Integer, List<Integer>> filteredAfterGroupingMap = StreamsGroupingCollectionFilter.findEvenNumbersAfterGroupingByQuantityOfDigits(BASE_INTEGER_COLLECTION);
|
||||||
|
Map<Integer, List<Integer>> filteredBeforeGroupingMap = StreamsGroupingCollectionFilter.findEvenNumbersBeforeGroupingByQuantityOfDigits(BASE_INTEGER_COLLECTION);
|
||||||
|
|
||||||
|
assertThat(filteredAfterGroupingMap).containsAllEntriesOf(EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP);
|
||||||
|
assertThat(filteredBeforeGroupingMap).doesNotContainKey(3)
|
||||||
|
.containsAllEntriesOf(EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -36,19 +36,37 @@
|
|||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>${commons-lang3.version}</version>
|
<version>${commons-lang3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.collections</groupId>
|
||||||
|
<artifactId>eclipse-collections-api</artifactId>
|
||||||
|
<version>${eclipse.collections.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.collections</groupId>
|
||||||
|
<artifactId>eclipse-collections</artifactId>
|
||||||
|
<version>${eclipse.collections.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<version>${assertj.version}</version>
|
<version>${assertj.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-runner</artifactId>
|
||||||
|
<version>${junit.platform.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
<junit.platform.version>1.2.0</junit.platform.version>
|
||||||
<commons-lang3.version>3.5</commons-lang3.version>
|
<commons-lang3.version>3.5</commons-lang3.version>
|
||||||
<commons-collections4.version>4.1</commons-collections4.version>
|
<commons-collections4.version>4.1</commons-collections4.version>
|
||||||
<collections-generic.version>4.01</collections-generic.version>
|
<collections-generic.version>4.01</collections-generic.version>
|
||||||
<avaitility.version>1.7.0</avaitility.version>
|
<avaitility.version>1.7.0</avaitility.version>
|
||||||
<assertj.version>3.6.1</assertj.version>
|
<assertj.version>3.6.1</assertj.version>
|
||||||
|
<eclipse.collections.version>9.2.0</eclipse.collections.version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.java.filtering;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
|
import org.apache.commons.collections4.Predicate;
|
||||||
|
|
||||||
|
public class CollectionUtilsCollectionFilter {
|
||||||
|
|
||||||
|
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
|
||||||
|
Predicate<Integer> apacheEventNumberPredicate = item -> item % 2 == 0;
|
||||||
|
|
||||||
|
CollectionUtils.filter(baseCollection, apacheEventNumberPredicate);
|
||||||
|
return baseCollection;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.java.filtering;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.eclipse.collections.api.block.predicate.Predicate;
|
||||||
|
import org.eclipse.collections.impl.factory.Lists;
|
||||||
|
import org.eclipse.collections.impl.utility.Iterate;
|
||||||
|
|
||||||
|
public class EclipseCollectionsCollectionFilter {
|
||||||
|
|
||||||
|
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
|
||||||
|
Predicate<Integer> eclipsePredicate = item -> item % 2 == 0;
|
||||||
|
Collection<Integer> filteredList = Lists.mutable.ofAll(baseCollection)
|
||||||
|
.select(eclipsePredicate);
|
||||||
|
|
||||||
|
return filteredList;
|
||||||
|
}
|
||||||
|
|
||||||
|
static public Collection<Integer> findEvenNumbersUsingIterate(Collection<Integer> baseCollection) {
|
||||||
|
Predicate<Integer> eclipsePredicate = new Predicate<Integer>() {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean accept(Integer arg0) {
|
||||||
|
return arg0 % 2 == 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Collection<Integer> filteredList = Iterate.select(baseCollection, eclipsePredicate);
|
||||||
|
|
||||||
|
return filteredList;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.java.filtering;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import com.google.common.base.Predicate;
|
||||||
|
import com.google.common.collect.Collections2;
|
||||||
|
|
||||||
|
public class GuavaCollectionFilter {
|
||||||
|
|
||||||
|
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
|
||||||
|
Predicate<Integer> guavaPredicate = item -> item % 2 == 0;
|
||||||
|
|
||||||
|
Collection<Integer> filteredCollection = Collections2.filter(baseCollection, guavaPredicate);
|
||||||
|
return filteredCollection;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.java.filtering;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class StreamsCollectionFilter {
|
||||||
|
|
||||||
|
public static <T> Collection<T> filterCollectionHelperMethod(Collection<T> baseCollection, Predicate<T> predicate) {
|
||||||
|
return baseCollection.stream()
|
||||||
|
.filter(predicate)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
static public Collection<Integer> findEvenNumbersUsingHelperMethod(Collection<Integer> baseCollection) {
|
||||||
|
return filterCollectionHelperMethod(baseCollection, item -> item % 2 == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
|
||||||
|
Predicate<Integer> streamsPredicate = item -> item % 2 == 0;
|
||||||
|
|
||||||
|
return baseCollection.stream()
|
||||||
|
.filter(streamsPredicate)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.java.list;
|
||||||
|
|
||||||
|
public class Flower {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private int petals;
|
||||||
|
|
||||||
|
public Flower(String name, int petals) {
|
||||||
|
this.name = name;
|
||||||
|
this.petals = petals;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPetals() {
|
||||||
|
return petals;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPetals(int petals) {
|
||||||
|
this.petals = petals;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
package com.baeldung.java.map.initialize;
|
||||||
|
|
||||||
|
import java.util.AbstractMap;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class MapInitializer {
|
||||||
|
|
||||||
|
public static Map<String, String> articleMapOne;
|
||||||
|
static {
|
||||||
|
articleMapOne = new HashMap<>();
|
||||||
|
articleMapOne.put("ar01", "Intro to Map");
|
||||||
|
articleMapOne.put("ar02", "Some article");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String> createSingletonMap() {
|
||||||
|
Map<String, String> passwordMap = Collections.singletonMap("username1", "password1");
|
||||||
|
return passwordMap;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> createEmptyMap() {
|
||||||
|
Map<String, String> emptyMap = Collections.emptyMap();
|
||||||
|
return emptyMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> createUsingDoubleBrace() {
|
||||||
|
Map<String, String> doubleBraceMap = new HashMap<String, String>() {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
{
|
||||||
|
put("key1", "value1");
|
||||||
|
put("key2", "value2");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return doubleBraceMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> createMapUsingStreamStringArray() {
|
||||||
|
Map<String, String> map = Stream.of(new String[][] { { "Hello", "World" }, { "John", "Doe" }, })
|
||||||
|
.collect(Collectors.toMap(data -> data[0], data -> data[1]));
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Integer> createMapUsingStreamObjectArray() {
|
||||||
|
Map<String, Integer> map = Stream.of(new Object[][] { { "data1", 1 }, { "data2", 2 }, })
|
||||||
|
.collect(Collectors.toMap(data -> (String) data[0], data -> (Integer) data[1]));
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Integer> createMapUsingStreamSimpleEntry() {
|
||||||
|
Map<String, Integer> map = Stream.of(new AbstractMap.SimpleEntry<>("idea", 1), new AbstractMap.SimpleEntry<>("mobile", 2))
|
||||||
|
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Integer> createMapUsingStreamSimpleImmutableEntry() {
|
||||||
|
Map<String, Integer> map = Stream.of(new AbstractMap.SimpleImmutableEntry<>("idea", 1), new AbstractMap.SimpleImmutableEntry<>("mobile", 2))
|
||||||
|
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> createImmutableMapWithStreams() {
|
||||||
|
Map<String, String> map = Stream.of(new String[][] { { "Hello", "World" }, { "John", "Doe" }, })
|
||||||
|
.collect(Collectors.collectingAndThen(Collectors.toMap(data -> data[0], data -> data[1]), Collections::<String, String> unmodifiableMap));
|
||||||
|
return map;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.baeldung.java.filtering;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.platform.runner.JUnitPlatform;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
@RunWith(JUnitPlatform.class)
|
||||||
|
public class CollectionFiltersUnitTest {
|
||||||
|
|
||||||
|
private static final Collection<Integer> BASE_INTEGER_COLLECTION = Arrays.asList(9, 14, 2, 7, 1, 5, 8);
|
||||||
|
private static final Collection<Integer> EXPECTED_EVEN_FILTERED_COLLECTION = Arrays.asList(14, 2, 8);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAStringCollection_whenFilteringFourLetterWords_thenObtainTheFilteredCollection() {
|
||||||
|
final Collection<String> baseStrings = Arrays.asList("java", "baeldung", "type", "example", "other");
|
||||||
|
|
||||||
|
Collection<String> filtered = StreamsCollectionFilter.filterCollectionHelperMethod(baseStrings, item -> item.length() == 4);
|
||||||
|
|
||||||
|
assertThat(filtered).containsExactlyInAnyOrder("java", "type");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnIntegerCollection_whenFilteringEvenValues_thenObtainTheFilteredCollectionForAllCases() {
|
||||||
|
Collection<Integer> filteredWithStreams1 = StreamsCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
|
||||||
|
Collection<Integer> filteredWithCollectionUtils = CollectionUtilsCollectionFilter.findEvenNumbers(new ArrayList<>(BASE_INTEGER_COLLECTION));
|
||||||
|
Collection<Integer> filteredWithEclipseCollections = EclipseCollectionsCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
|
||||||
|
Collection<Integer> filteredWithEclipseCollectionsUsingIterate = EclipseCollectionsCollectionFilter.findEvenNumbersUsingIterate(BASE_INTEGER_COLLECTION);
|
||||||
|
Collection<Integer> filteredWithGuava = GuavaCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
|
||||||
|
|
||||||
|
assertThat(filteredWithStreams1).hasSameElementsAs(filteredWithCollectionUtils)
|
||||||
|
.hasSameElementsAs(filteredWithEclipseCollections)
|
||||||
|
.hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate)
|
||||||
|
.hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate)
|
||||||
|
.hasSameElementsAs(filteredWithGuava)
|
||||||
|
.hasSameElementsAs(EXPECTED_EVEN_FILTERED_COLLECTION);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.java.map.initialize;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class MapInitializerUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenStaticMap_whenUpdated_thenCorrect() {
|
||||||
|
|
||||||
|
MapInitializer.articleMapOne.put("NewArticle1", "Convert array to List");
|
||||||
|
|
||||||
|
assertEquals(MapInitializer.articleMapOne.get("NewArticle1"), "Convert array to List");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=UnsupportedOperationException.class)
|
||||||
|
public void givenSingleTonMap_whenEntriesAdded_throwsException() {
|
||||||
|
|
||||||
|
Map<String, String> map = MapInitializer.createSingletonMap();
|
||||||
|
map.put("username2", "password2");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
package com.baeldung.list.listoflist;
|
||||||
|
|
||||||
|
import com.baeldung.java.list.Flower;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import java.util.*;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class AddElementsToListUnitTest {
|
||||||
|
|
||||||
|
List<Flower> flowers;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
this.flowers = new ArrayList<>(Arrays.asList(
|
||||||
|
new Flower("Poppy", 12),
|
||||||
|
new Flower("Anemone", 8),
|
||||||
|
new Flower("Catmint", 12)));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithNewItems() {
|
||||||
|
List<Flower> anotherList = new ArrayList<>();
|
||||||
|
anotherList.addAll(flowers);
|
||||||
|
assertEquals(anotherList.size(), flowers.size());
|
||||||
|
Assert.assertTrue(anotherList.containsAll(flowers));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithOneModifiedElementByConstructor() {
|
||||||
|
List<Flower> anotherList = new ArrayList<>();
|
||||||
|
anotherList.addAll(flowers);
|
||||||
|
Flower flower = anotherList.get(0);
|
||||||
|
flower.setPetals(flowers.get(0).getPetals() * 3);
|
||||||
|
assertEquals(anotherList.size(), flowers.size());
|
||||||
|
Assert.assertTrue(anotherList.containsAll(flowers));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void givenAListAndElements_whenUseCollectionsAddAll_thenAddElementsToTargetList() {
|
||||||
|
List<Flower> target = new ArrayList<>();
|
||||||
|
Collections.addAll(target, flowers.get(0), flowers.get(1), flowers.get(2), flowers.get(0));
|
||||||
|
assertEquals(target.size(), 4);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListSkipFirstElementByStreamProcess() {
|
||||||
|
List<Flower> flowerVase = new ArrayList<>();
|
||||||
|
flowers.stream()
|
||||||
|
.skip(1)
|
||||||
|
.forEachOrdered(flowerVase::add);
|
||||||
|
assertEquals(flowerVase.size() + 1, flowers.size());
|
||||||
|
assertFalse(flowerVase.containsAll(flowers));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListFilteringElementsByStreamProcess() {
|
||||||
|
List<Flower> flowerVase = new ArrayList<>();
|
||||||
|
flowers.stream()
|
||||||
|
.filter(f -> f.getPetals() > 10)
|
||||||
|
.forEachOrdered(flowerVase::add);
|
||||||
|
assertEquals(flowerVase.size() + 1, flowers.size());
|
||||||
|
assertFalse(flowerVase.containsAll(flowers));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenListIsNotNull_thenAddElementsToListByStreamProcessWihtOptional() {
|
||||||
|
List<Flower> target = new ArrayList<>();
|
||||||
|
Optional.ofNullable(flowers)
|
||||||
|
.ifPresent(target::addAll);
|
||||||
|
assertNotNull(target);
|
||||||
|
assertEquals(target.size(), 3);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
package com.baeldung.list.removefirst;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class RemoveFirstElementUnitTest {
|
||||||
|
|
||||||
|
private List<String> list = new ArrayList<>();
|
||||||
|
private LinkedList<String> linkedList = new LinkedList<>();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
|
||||||
|
list.add("cat");
|
||||||
|
list.add("dog");
|
||||||
|
list.add("pig");
|
||||||
|
list.add("cow");
|
||||||
|
list.add("goat");
|
||||||
|
|
||||||
|
linkedList.add("cat");
|
||||||
|
linkedList.add("dog");
|
||||||
|
linkedList.add("pig");
|
||||||
|
linkedList.add("cow");
|
||||||
|
linkedList.add("goat");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenList_whenRemoveFirst_thenRemoved() {
|
||||||
|
list.remove(0);
|
||||||
|
|
||||||
|
assertThat(list, hasSize(4));
|
||||||
|
assertThat(list, not(contains("cat")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenLinkedList_whenRemoveFirst_thenRemoved() {
|
||||||
|
|
||||||
|
linkedList.removeFirst();
|
||||||
|
|
||||||
|
assertThat(linkedList, hasSize(4));
|
||||||
|
assertThat(linkedList, not(contains("cat")));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
59
core-java-persistence/pom.xml
Normal file
59
core-java-persistence/pom.xml
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<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.core-java-persistence</groupId>
|
||||||
|
<artifactId>core-java-persistence</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>core-java-persistence</name>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-java</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../parent-java</relativePath>
|
||||||
|
</parent>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj-core.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<version>${h2database.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-dbcp2</artifactId>
|
||||||
|
<version>${commons-dbcp2.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.zaxxer</groupId>
|
||||||
|
<artifactId>HikariCP</artifactId>
|
||||||
|
<version>${HikariCP.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mchange</groupId>
|
||||||
|
<artifactId>c3p0</artifactId>
|
||||||
|
<version>${c3p0.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<finalName>core-java-persistence</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
<properties>
|
||||||
|
<assertj-core.version>3.10.0</assertj-core.version>
|
||||||
|
<h2database.version>1.4.197</h2database.version>
|
||||||
|
<commons-dbcp2.version>2.4.0</commons-dbcp2.version>
|
||||||
|
<HikariCP.version>3.2.0</HikariCP.version>
|
||||||
|
<c3p0.version>0.9.5.2</c3p0.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
@ -1,85 +1,92 @@
|
|||||||
package com.baeldung.connectionpool.connectionpools;
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class BasicConnectionPool implements ConnectionPool {
|
public class BasicConnectionPool implements ConnectionPool {
|
||||||
|
|
||||||
private final String url;
|
private final String url;
|
||||||
private final String user;
|
private final String user;
|
||||||
private final String password;
|
private final String password;
|
||||||
private final List<Connection> connectionPool;
|
private final List<Connection> connectionPool;
|
||||||
private final List<Connection> usedConnections = new ArrayList<>();
|
private final List<Connection> usedConnections = new ArrayList<>();
|
||||||
private static final int INITIAL_POOL_SIZE = 10;
|
private static final int INITIAL_POOL_SIZE = 10;
|
||||||
private final int MAX_POOL_SIZE = 20;
|
private final int MAX_POOL_SIZE = 20;
|
||||||
|
|
||||||
public static BasicConnectionPool create(String url, String user, String password) throws SQLException {
|
public static BasicConnectionPool create(String url, String user, String password) throws SQLException {
|
||||||
List<Connection> pool = new ArrayList<>(INITIAL_POOL_SIZE);
|
List<Connection> pool = new ArrayList<>(INITIAL_POOL_SIZE);
|
||||||
for (int i = 0; i < INITIAL_POOL_SIZE; i++) {
|
for (int i = 0; i < INITIAL_POOL_SIZE; i++) {
|
||||||
pool.add(createConnection(url, user, password));
|
pool.add(createConnection(url, user, password));
|
||||||
}
|
}
|
||||||
return new BasicConnectionPool(url, user, password, pool);
|
return new BasicConnectionPool(url, user, password, pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BasicConnectionPool(String url, String user, String password, List<Connection> connectionPool) {
|
private BasicConnectionPool(String url, String user, String password, List<Connection> connectionPool) {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.connectionPool = connectionPool;
|
this.connectionPool = connectionPool;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Connection getConnection() throws SQLException {
|
public Connection getConnection() throws SQLException {
|
||||||
if (connectionPool.isEmpty()) {
|
if (connectionPool.isEmpty()) {
|
||||||
if (usedConnections.size() < MAX_POOL_SIZE) {
|
if (usedConnections.size() < MAX_POOL_SIZE) {
|
||||||
connectionPool.add(createConnection(url, user, password));
|
connectionPool.add(createConnection(url, user, password));
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("Maximum pool size reached, no available connections!");
|
throw new RuntimeException("Maximum pool size reached, no available connections!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection connection = connectionPool.remove(connectionPool.size() - 1);
|
Connection connection = connectionPool.remove(connectionPool.size() - 1);
|
||||||
usedConnections.add(connection);
|
usedConnections.add(connection);
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean releaseConnection(Connection connection) {
|
public boolean releaseConnection(Connection connection) {
|
||||||
connectionPool.add(connection);
|
connectionPool.add(connection);
|
||||||
return usedConnections.remove(connection);
|
return usedConnections.remove(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Connection createConnection(String url, String user, String password) throws SQLException {
|
private static Connection createConnection(String url, String user, String password) throws SQLException {
|
||||||
return DriverManager.getConnection(url, user, password);
|
return DriverManager.getConnection(url, user, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSize() {
|
@Override
|
||||||
return connectionPool.size() + usedConnections.size();
|
public int getSize() {
|
||||||
}
|
return connectionPool.size() + usedConnections.size();
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public String getUrl() {
|
@Override
|
||||||
return url;
|
public List<Connection> getConnectionPool() {
|
||||||
}
|
return connectionPool;
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public String getUser() {
|
@Override
|
||||||
return user;
|
public String getUrl() {
|
||||||
}
|
return url;
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public String getPassword() {
|
@Override
|
||||||
return password;
|
public String getUser() {
|
||||||
}
|
return user;
|
||||||
|
}
|
||||||
public void shutdown() throws SQLException {
|
|
||||||
usedConnections.forEach(this::releaseConnection);
|
@Override
|
||||||
for (Connection c : connectionPool) {
|
public String getPassword() {
|
||||||
c.close();
|
return password;
|
||||||
}
|
}
|
||||||
connectionPool.clear();
|
|
||||||
}
|
@Override
|
||||||
}
|
public void shutdown() throws SQLException {
|
||||||
|
usedConnections.forEach(this::releaseConnection);
|
||||||
|
for (Connection c : connectionPool) {
|
||||||
|
c.close();
|
||||||
|
}
|
||||||
|
connectionPool.clear();
|
||||||
|
}
|
||||||
|
}
|
@ -1,28 +1,28 @@
|
|||||||
package com.baeldung.connectionpool.connectionpools;
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||||
import java.beans.PropertyVetoException;
|
import java.beans.PropertyVetoException;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class C3poDataSource {
|
public class C3poDataSource {
|
||||||
|
|
||||||
private static final ComboPooledDataSource cpds = new ComboPooledDataSource();
|
private static final ComboPooledDataSource cpds = new ComboPooledDataSource();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
cpds.setDriverClass("org.h2.Driver");
|
cpds.setDriverClass("org.h2.Driver");
|
||||||
cpds.setJdbcUrl("jdbc:h2:mem:test");
|
cpds.setJdbcUrl("jdbc:h2:mem:test");
|
||||||
cpds.setUser("user");
|
cpds.setUser("user");
|
||||||
cpds.setPassword("password");
|
cpds.setPassword("password");
|
||||||
} catch (PropertyVetoException e) {
|
} catch (PropertyVetoException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Connection getConnection() throws SQLException {
|
public static Connection getConnection() throws SQLException {
|
||||||
return cpds.getConnection();
|
return cpds.getConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
private C3poDataSource(){}
|
private C3poDataSource(){}
|
||||||
}
|
}
|
@ -1,18 +1,24 @@
|
|||||||
package com.baeldung.connectionpool.connectionpools;
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface ConnectionPool {
|
public interface ConnectionPool {
|
||||||
|
|
||||||
Connection getConnection() throws SQLException;
|
Connection getConnection() throws SQLException;
|
||||||
|
|
||||||
boolean releaseConnection(Connection connection);
|
boolean releaseConnection(Connection connection);
|
||||||
|
|
||||||
String getUrl();
|
List<Connection> getConnectionPool();
|
||||||
|
|
||||||
String getUser();
|
int getSize();
|
||||||
|
|
||||||
String getPassword();
|
String getUrl();
|
||||||
|
|
||||||
|
String getUser();
|
||||||
|
|
||||||
|
String getPassword();
|
||||||
|
|
||||||
|
void shutdown() throws SQLException;;
|
||||||
}
|
}
|
@ -1,25 +1,25 @@
|
|||||||
package com.baeldung.connectionpool.connectionpools;
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import org.apache.commons.dbcp2.BasicDataSource;
|
import org.apache.commons.dbcp2.BasicDataSource;
|
||||||
|
|
||||||
public class DBCPDataSource {
|
public class DBCPDataSource {
|
||||||
|
|
||||||
private static final BasicDataSource ds = new BasicDataSource();
|
private static final BasicDataSource ds = new BasicDataSource();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
ds.setUrl("jdbc:h2:mem:test");
|
ds.setUrl("jdbc:h2:mem:test");
|
||||||
ds.setUsername("user");
|
ds.setUsername("user");
|
||||||
ds.setPassword("password");
|
ds.setPassword("password");
|
||||||
ds.setMinIdle(5);
|
ds.setMinIdle(5);
|
||||||
ds.setMaxIdle(10);
|
ds.setMaxIdle(10);
|
||||||
ds.setMaxOpenPreparedStatements(100);
|
ds.setMaxOpenPreparedStatements(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Connection getConnection() throws SQLException {
|
public static Connection getConnection() throws SQLException {
|
||||||
return ds.getConnection();
|
return ds.getConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
private DBCPDataSource(){}
|
private DBCPDataSource(){}
|
||||||
}
|
}
|
@ -1,28 +1,28 @@
|
|||||||
package com.baeldung.connectionpool.connectionpools;
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class HikariCPDataSource {
|
public class HikariCPDataSource {
|
||||||
|
|
||||||
private static final HikariConfig config = new HikariConfig();
|
private static final HikariConfig config = new HikariConfig();
|
||||||
private static final HikariDataSource ds;
|
private static final HikariDataSource ds;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
config.setJdbcUrl("jdbc:h2:mem:test");
|
config.setJdbcUrl("jdbc:h2:mem:test");
|
||||||
config.setUsername("user");
|
config.setUsername("user");
|
||||||
config.setPassword("password");
|
config.setPassword("password");
|
||||||
config.addDataSourceProperty("cachePrepStmts", "true");
|
config.addDataSourceProperty("cachePrepStmts", "true");
|
||||||
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
||||||
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
||||||
ds = new HikariDataSource(config);
|
ds = new HikariDataSource(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Connection getConnection() throws SQLException {
|
public static Connection getConnection() throws SQLException {
|
||||||
return ds.getConnection();
|
return ds.getConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
private HikariCPDataSource(){}
|
private HikariCPDataSource(){}
|
||||||
}
|
}
|
@ -1,10 +1,8 @@
|
|||||||
package com.baeldung.connectionpool;
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
import com.baeldung.connectionpool.connectionpools.BasicConnectionPool;
|
|
||||||
import com.baeldung.connectionpool.connectionpools.ConnectionPool;
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.connectionpool;
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
import com.baeldung.connectionpool.connectionpools.C3poDataSource;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.connectionpool;
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
import com.baeldung.connectionpool.connectionpools.DBCPDataSource;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.connectionpool;
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
import com.baeldung.connectionpool.connectionpools.HikariCPDataSource;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
@ -158,21 +158,6 @@
|
|||||||
<artifactId>jmimemagic</artifactId>
|
<artifactId>jmimemagic</artifactId>
|
||||||
<version>${jmime-magic.version}</version>
|
<version>${jmime-magic.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.commons</groupId>
|
|
||||||
<artifactId>commons-dbcp2</artifactId>
|
|
||||||
<version>${commons-dbcp2.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.zaxxer</groupId>
|
|
||||||
<artifactId>HikariCP</artifactId>
|
|
||||||
<version>${HikariCP.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.mchange</groupId>
|
|
||||||
<artifactId>c3p0</artifactId>
|
|
||||||
<version>${c3p0.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<!-- instrumentation -->
|
<!-- instrumentation -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.javassist</groupId>
|
<groupId>org.javassist</groupId>
|
||||||
@ -544,11 +529,6 @@
|
|||||||
<!-- testing -->
|
<!-- testing -->
|
||||||
<assertj-core.version>3.10.0</assertj-core.version>
|
<assertj-core.version>3.10.0</assertj-core.version>
|
||||||
|
|
||||||
<!-- JDBC pooling frameworks -->
|
|
||||||
<commons-dbcp2.version>2.4.0</commons-dbcp2.version>
|
|
||||||
<HikariCP.version>3.2.0</HikariCP.version>
|
|
||||||
<c3p0.version>0.9.5.2</c3p0.version>
|
|
||||||
|
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||||
<springframework.spring-web.version>4.3.4.RELEASE</springframework.spring-web.version>
|
<springframework.spring-web.version>4.3.4.RELEASE</springframework.spring-web.version>
|
||||||
|
@ -6,10 +6,16 @@
|
|||||||
<!-- POM file generated with GWT webAppCreator -->
|
<!-- POM file generated with GWT webAppCreator -->
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>google-web-toolkit</artifactId>
|
<artifactId>google_web_toolkit</artifactId>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- ensure all GWT deps use the same version (unless overridden) -->
|
<!-- ensure all GWT deps use the same version (unless overridden) -->
|
||||||
|
@ -22,6 +22,8 @@
|
|||||||
<!-- Specify the app entry point class. -->
|
<!-- Specify the app entry point class. -->
|
||||||
<entry-point class='com.baeldung.client.Google_web_toolkit'/>
|
<entry-point class='com.baeldung.client.Google_web_toolkit'/>
|
||||||
|
|
||||||
|
<set-property name="user.agent" value="gecko1_8"/>
|
||||||
|
|
||||||
<!-- Specify the paths for translatable code -->
|
<!-- Specify the paths for translatable code -->
|
||||||
<source path='client'/>
|
<source path='client'/>
|
||||||
<source path='shared'/>
|
<source path='shared'/>
|
||||||
|
@ -771,6 +771,11 @@
|
|||||||
<version>${hamcrest-all.version}</version>
|
<version>${hamcrest-all.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.paho</groupId>
|
||||||
|
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||||
|
<version>1.2.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
@ -1019,4 +1024,4 @@
|
|||||||
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
|
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.baeldung.mqtt;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
|
import org.eclipse.paho.client.mqttv3.IMqttClient;
|
||||||
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class EngineTemperatureSensor implements Callable<Void> {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(EngineTemperatureSensor.class);
|
||||||
|
public static final String TOPIC = "engine/temperature";
|
||||||
|
|
||||||
|
private IMqttClient client;
|
||||||
|
private Random rnd = new Random();
|
||||||
|
|
||||||
|
public EngineTemperatureSensor(IMqttClient client) {
|
||||||
|
this.client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void call() throws Exception {
|
||||||
|
|
||||||
|
if ( !client.isConnected()) {
|
||||||
|
log.info("[I31] Client not connected.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
MqttMessage msg = readEngineTemp();
|
||||||
|
msg.setQos(0);
|
||||||
|
msg.setRetained(true);
|
||||||
|
client.publish(TOPIC,msg);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method simulates reading the engine temperature
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private MqttMessage readEngineTemp() {
|
||||||
|
double temp = 80 + rnd.nextDouble() * 20.0;
|
||||||
|
byte[] payload = String.format("T:%04.2f",temp).getBytes();
|
||||||
|
MqttMessage msg = new MqttMessage(payload);
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,109 @@
|
|||||||
|
package com.baeldung.mqtt;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||||
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class EngineTemperatureSensorLiveTest {
|
||||||
|
|
||||||
|
private static Logger log = LoggerFactory.getLogger(EngineTemperatureSensorLiveTest.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSendSingleMessage_thenSuccess() throws Exception {
|
||||||
|
|
||||||
|
String publisherId = UUID.randomUUID().toString();
|
||||||
|
MqttClient publisher = new MqttClient("tcp://iot.eclipse.org:1883",publisherId);
|
||||||
|
|
||||||
|
String subscriberId = UUID.randomUUID().toString();
|
||||||
|
MqttClient subscriber = new MqttClient("tcp://iot.eclipse.org:1883",subscriberId);
|
||||||
|
|
||||||
|
MqttConnectOptions options = new MqttConnectOptions();
|
||||||
|
options.setAutomaticReconnect(true);
|
||||||
|
options.setCleanSession(true);
|
||||||
|
options.setConnectionTimeout(10);
|
||||||
|
|
||||||
|
|
||||||
|
subscriber.connect(options);
|
||||||
|
publisher.connect(options);
|
||||||
|
|
||||||
|
CountDownLatch receivedSignal = new CountDownLatch(1);
|
||||||
|
|
||||||
|
subscriber.subscribe(EngineTemperatureSensor.TOPIC, (topic, msg) -> {
|
||||||
|
byte[] payload = msg.getPayload();
|
||||||
|
log.info("[I46] Message received: topic={}, payload={}", topic, new String(payload));
|
||||||
|
receivedSignal.countDown();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Callable<Void> target = new EngineTemperatureSensor(publisher);
|
||||||
|
target.call();
|
||||||
|
|
||||||
|
receivedSignal.await(1, TimeUnit.MINUTES);
|
||||||
|
|
||||||
|
log.info("[I56] Success !");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSendMultipleMessages_thenSuccess() throws Exception {
|
||||||
|
|
||||||
|
String publisherId = UUID.randomUUID().toString();
|
||||||
|
MqttClient publisher = new MqttClient("tcp://iot.eclipse.org:1883",publisherId);
|
||||||
|
|
||||||
|
String subscriberId = UUID.randomUUID().toString();
|
||||||
|
MqttClient subscriber = new MqttClient("tcp://iot.eclipse.org:1883",subscriberId);
|
||||||
|
|
||||||
|
|
||||||
|
MqttConnectOptions options = new MqttConnectOptions();
|
||||||
|
options.setAutomaticReconnect(true);
|
||||||
|
options.setCleanSession(true);
|
||||||
|
options.setConnectionTimeout(10);
|
||||||
|
|
||||||
|
|
||||||
|
publisher.connect(options);
|
||||||
|
subscriber.connect(options);
|
||||||
|
|
||||||
|
CountDownLatch receivedSignal = new CountDownLatch(10);
|
||||||
|
|
||||||
|
subscriber.subscribe(EngineTemperatureSensor.TOPIC, (topic, msg) -> {
|
||||||
|
byte[] payload = msg.getPayload();
|
||||||
|
log.info("[I82] Message received: topic={}, payload={}", topic, new String(payload));
|
||||||
|
receivedSignal.countDown();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Callable<Void> target = new EngineTemperatureSensor(publisher);
|
||||||
|
|
||||||
|
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
executor.scheduleAtFixedRate(() -> {
|
||||||
|
try {
|
||||||
|
target.call();
|
||||||
|
}
|
||||||
|
catch(Exception ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
}, 1, 1, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
|
||||||
|
receivedSignal.await(1, TimeUnit.MINUTES);
|
||||||
|
executor.shutdown();
|
||||||
|
|
||||||
|
assertTrue(receivedSignal.getCount() == 0 , "Countdown should be zero");
|
||||||
|
|
||||||
|
log.info("[I105] Success !");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -18,6 +18,13 @@
|
|||||||
<artifactId>log4j-core</artifactId>
|
<artifactId>log4j-core</artifactId>
|
||||||
<version>${log4j-core.version}</version>
|
<version>${log4j-core.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- This is the needed API component. -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-api</artifactId>
|
||||||
|
<version>${log4j-core.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- This is used by JSONLayout. -->
|
<!-- This is used by JSONLayout. -->
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -114,4 +121,4 @@
|
|||||||
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
|
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,54 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.baeldung.logging.log4j2.appender;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.Level;
|
||||||
|
import org.apache.logging.log4j.core.Appender;
|
||||||
|
import org.apache.logging.log4j.core.Core;
|
||||||
|
import org.apache.logging.log4j.core.Filter;
|
||||||
|
import org.apache.logging.log4j.core.LogEvent;
|
||||||
|
import org.apache.logging.log4j.core.appender.AbstractAppender;
|
||||||
|
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||||
|
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
|
||||||
|
import org.apache.logging.log4j.core.config.plugins.PluginElement;
|
||||||
|
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
|
||||||
|
|
||||||
|
@Plugin(name = "MapAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE)
|
||||||
|
public class MapAppender extends AbstractAppender {
|
||||||
|
|
||||||
|
private ConcurrentMap<String, LogEvent> eventMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
protected MapAppender(String name, Filter filter) {
|
||||||
|
super(name, filter, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PluginFactory
|
||||||
|
public static MapAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Filter") final Filter filter) {
|
||||||
|
return new MapAppender(name, filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void append(LogEvent event) {
|
||||||
|
if (event.getLevel()
|
||||||
|
.isLessSpecificThan(Level.WARN)) {
|
||||||
|
error("Unable to log less than WARN level.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
eventMap.put(Instant.now()
|
||||||
|
.toString(), event);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConcurrentMap<String, LogEvent> getEventMap() {
|
||||||
|
return eventMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEventMap(ConcurrentMap<String, LogEvent> eventMap) {
|
||||||
|
this.eventMap = eventMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.baeldung.logging.log4j2.appender;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.apache.logging.log4j.core.LoggerContext;
|
||||||
|
import org.apache.logging.log4j.core.config.Configuration;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.JUnit4;
|
||||||
|
|
||||||
|
@RunWith(JUnit4.class)
|
||||||
|
public class MapAppenderIntegrationTest {
|
||||||
|
|
||||||
|
private Logger logger;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
logger = LogManager.getLogger(MapAppenderIntegrationTest.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenLoggerEmitsLoggingEvent_thenAppenderReceivesEvent() throws Exception {
|
||||||
|
logger.info("Test from {}", this.getClass()
|
||||||
|
.getSimpleName());
|
||||||
|
LoggerContext context = LoggerContext.getContext(false);
|
||||||
|
Configuration config = context.getConfiguration();
|
||||||
|
MapAppender appender = config.getAppender("MapAppender");
|
||||||
|
assertEquals(appender.getEventMap()
|
||||||
|
.size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude"
|
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude"
|
||||||
status="WARN">
|
packages="com.baeldung" status="WARN">
|
||||||
<Appenders>
|
<Appenders>
|
||||||
<xi:include
|
<xi:include
|
||||||
href="log4j2-includes/console-appender_pattern-layout_colored.xml" />
|
href="log4j2-includes/console-appender_pattern-layout_colored.xml" />
|
||||||
@ -50,6 +50,7 @@
|
|||||||
size="17 kB" />
|
size="17 kB" />
|
||||||
</Policies>
|
</Policies>
|
||||||
</RollingFile>
|
</RollingFile>
|
||||||
|
<MapAppender name="MapAppender"/>
|
||||||
</Appenders>
|
</Appenders>
|
||||||
<Loggers>
|
<Loggers>
|
||||||
<Logger name="CONSOLE_PATTERN_APPENDER_MARKER" level="TRACE"
|
<Logger name="CONSOLE_PATTERN_APPENDER_MARKER" level="TRACE"
|
||||||
@ -82,6 +83,7 @@
|
|||||||
</Logger>
|
</Logger>
|
||||||
<Root level="DEBUG">
|
<Root level="DEBUG">
|
||||||
<AppenderRef ref="ConsoleAppender" />
|
<AppenderRef ref="ConsoleAppender" />
|
||||||
|
<AppenderRef ref="MapAppender" />
|
||||||
</Root>
|
</Root>
|
||||||
</Loggers>
|
</Loggers>
|
||||||
</Configuration>
|
</Configuration>
|
39
pom.xml
39
pom.xml
@ -312,6 +312,7 @@
|
|||||||
<module>core-java-collections</module>
|
<module>core-java-collections</module>
|
||||||
<module>core-java-io</module>
|
<module>core-java-io</module>
|
||||||
<module>core-java-8</module>
|
<module>core-java-8</module>
|
||||||
|
<module>core-java-persistence</module>
|
||||||
<module>core-kotlin</module>
|
<module>core-kotlin</module>
|
||||||
<module>core-groovy</module>
|
<module>core-groovy</module>
|
||||||
<module>core-java-concurrency</module>
|
<module>core-java-concurrency</module>
|
||||||
@ -858,7 +859,7 @@
|
|||||||
<module>core-java-io</module>
|
<module>core-java-io</module>
|
||||||
<module>core-java-8</module>
|
<module>core-java-8</module>
|
||||||
<module>core-groovy</module>
|
<module>core-groovy</module>
|
||||||
<module>core-java-concurrency</module>
|
|
||||||
<module>couchbase</module>
|
<module>couchbase</module>
|
||||||
<module>deltaspike</module>
|
<module>deltaspike</module>
|
||||||
<module>dozer</module>
|
<module>dozer</module>
|
||||||
@ -877,7 +878,7 @@
|
|||||||
<module>spring-static-resources</module>
|
<module>spring-static-resources</module>
|
||||||
<module>hazelcast</module>
|
<module>hazelcast</module>
|
||||||
<module>hbase</module>
|
<module>hbase</module>
|
||||||
<module>httpclient</module>
|
|
||||||
<module>hystrix</module>
|
<module>hystrix</module>
|
||||||
<module>image-processing</module>
|
<module>image-processing</module>
|
||||||
<module>immutables</module>
|
<module>immutables</module>
|
||||||
@ -906,11 +907,11 @@
|
|||||||
<module>linkrest</module>
|
<module>linkrest</module>
|
||||||
<module>logging-modules/log-mdc</module>
|
<module>logging-modules/log-mdc</module>
|
||||||
<module>logging-modules/log4j</module>
|
<module>logging-modules/log4j</module>
|
||||||
<module>logging-modules/log4j2</module>
|
|
||||||
<module>logging-modules/logback</module>
|
<module>logging-modules/logback</module>
|
||||||
<module>lombok</module>
|
<module>lombok</module>
|
||||||
<module>mapstruct</module>
|
<module>mapstruct</module>
|
||||||
<module>metrics</module>
|
|
||||||
<module>maven</module>
|
<module>maven</module>
|
||||||
<module>mesos-marathon</module>
|
<module>mesos-marathon</module>
|
||||||
<module>msf4j</module>
|
<module>msf4j</module>
|
||||||
@ -962,25 +963,25 @@
|
|||||||
<module>spring-cucumber</module>
|
<module>spring-cucumber</module>
|
||||||
<module>spring-ejb</module>
|
<module>spring-ejb</module>
|
||||||
<module>spring-aop</module>
|
<module>spring-aop</module>
|
||||||
<module>spring-data-couchbase-2</module>
|
|
||||||
<module>persistence-modules/spring-data-dynamodb</module>
|
<module>persistence-modules/spring-data-dynamodb</module>
|
||||||
<module>spring-data-keyvalue</module>
|
<module>spring-data-keyvalue</module>
|
||||||
<module>spring-data-mongodb</module>
|
<module>spring-data-mongodb</module>
|
||||||
<module>persistence-modules/spring-data-neo4j</module>
|
<module>persistence-modules/spring-data-neo4j</module>
|
||||||
<module>persistence-modules/spring-data-redis</module>
|
|
||||||
<module>spring-data-rest</module>
|
<module>spring-data-rest</module>
|
||||||
<module>persistence-modules/spring-data-solr</module>
|
<module>persistence-modules/spring-data-solr</module>
|
||||||
<module>spring-dispatcher-servlet</module>
|
<module>spring-dispatcher-servlet</module>
|
||||||
<module>spring-exceptions</module>
|
<module>spring-exceptions</module>
|
||||||
<module>spring-freemarker</module>
|
<module>spring-freemarker</module>
|
||||||
<module>persistence-modules/spring-hibernate-3</module>
|
<module>persistence-modules/spring-hibernate-3</module>
|
||||||
<module>spring-hibernate4</module>
|
|
||||||
<module>persistence-modules/spring-hibernate-5</module>
|
<module>persistence-modules/spring-hibernate-5</module>
|
||||||
<module>persistence-modules/spring-data-eclipselink</module>
|
<module>persistence-modules/spring-data-eclipselink</module>
|
||||||
<module>spring-integration</module>
|
<module>spring-integration</module>
|
||||||
<module>spring-jenkins-pipeline</module>
|
<module>spring-jenkins-pipeline</module>
|
||||||
<module>spring-jersey</module>
|
<module>spring-jersey</module>
|
||||||
<module>jmeter</module>
|
|
||||||
<module>spring-jms</module>
|
<module>spring-jms</module>
|
||||||
<module>spring-jooq</module>
|
<module>spring-jooq</module>
|
||||||
<module>persistence-modules/spring-jpa</module>
|
<module>persistence-modules/spring-jpa</module>
|
||||||
@ -1041,13 +1042,13 @@
|
|||||||
<module>testing-modules/testing</module>
|
<module>testing-modules/testing</module>
|
||||||
<module>testing-modules/testng</module>
|
<module>testing-modules/testng</module>
|
||||||
<module>video-tutorials</module>
|
<module>video-tutorials</module>
|
||||||
<module>xml</module>
|
|
||||||
<module>xmlunit-2</module>
|
<module>xmlunit-2</module>
|
||||||
<module>struts-2</module>
|
<module>struts-2</module>
|
||||||
<module>apache-velocity</module>
|
<module>apache-velocity</module>
|
||||||
<module>apache-solrj</module>
|
<module>apache-solrj</module>
|
||||||
<module>rabbitmq</module>
|
<module>rabbitmq</module>
|
||||||
<module>vertx</module>
|
|
||||||
<module>persistence-modules/spring-data-gemfire</module>
|
<module>persistence-modules/spring-data-gemfire</module>
|
||||||
<module>mybatis</module>
|
<module>mybatis</module>
|
||||||
<module>spring-drools</module>
|
<module>spring-drools</module>
|
||||||
@ -1077,11 +1078,22 @@
|
|||||||
<module>apache-meecrowave</module>
|
<module>apache-meecrowave</module>
|
||||||
<module>testing-modules/junit-abstract</module>
|
<module>testing-modules/junit-abstract</module>
|
||||||
|
|
||||||
|
<module>spring-hibernate4</module>
|
||||||
|
<module>xml</module>
|
||||||
|
<module>vertx</module>
|
||||||
|
<module>metrics</module>
|
||||||
|
<module>httpclient</module>
|
||||||
|
|
||||||
<!-- problematic -->
|
<!-- problematic -->
|
||||||
<!--
|
<!--
|
||||||
<module>ejb</module>
|
<module>ejb</module>
|
||||||
<module>persistence-modules/java-cassandra</module>
|
<module>persistence-modules/java-cassandra</module>
|
||||||
<module>persistence-modules/spring-data-cassandra</module>
|
<module>persistence-modules/spring-data-cassandra</module>
|
||||||
|
<module>logging-modules/log4j2</module>
|
||||||
|
<module>spring-data-couchbase-2</module>
|
||||||
|
<module>persistence-modules/spring-data-redis</module>
|
||||||
|
|
||||||
|
<module>jmeter</module>
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<!-- heavy -->
|
<!-- heavy -->
|
||||||
@ -1097,6 +1109,7 @@
|
|||||||
<module>core-java</module>
|
<module>core-java</module>
|
||||||
<module>google-web-toolkit</module>
|
<module>google-web-toolkit</module>
|
||||||
<module>spring-security-mvc-custom</module>
|
<module>spring-security-mvc-custom</module>
|
||||||
|
<module>core-java-concurrency</module>
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<!-- 30:32 min -->
|
<!-- 30:32 min -->
|
||||||
@ -1159,7 +1172,7 @@
|
|||||||
<module>spring-security-mvc-custom</module>
|
<module>spring-security-mvc-custom</module>
|
||||||
<module>hibernate5</module>
|
<module>hibernate5</module>
|
||||||
<module>spring-data-elasticsearch</module>
|
<module>spring-data-elasticsearch</module>
|
||||||
|
<module>core-java-concurrency</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
</profile>
|
</profile>
|
||||||
@ -1223,6 +1236,4 @@
|
|||||||
<!-- <maven-pmd-plugin.version>3.9.0</maven-pmd-plugin.version> -->
|
<!-- <maven-pmd-plugin.version>3.9.0</maven-pmd-plugin.version> -->
|
||||||
<maven-pmd-plugin.version>3.8</maven-pmd-plugin.version>
|
<maven-pmd-plugin.version>3.8</maven-pmd-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
</project>
|
||||||
</project>
|
|
||||||
|
|
@ -34,8 +34,7 @@ public class FormHandler {
|
|||||||
|
|
||||||
private AtomicLong extractData(List<DataBuffer> dataBuffers) {
|
private AtomicLong extractData(List<DataBuffer> dataBuffers) {
|
||||||
AtomicLong atomicLong = new AtomicLong(0);
|
AtomicLong atomicLong = new AtomicLong(0);
|
||||||
dataBuffers.forEach(d -> atomicLong.addAndGet(d.asByteBuffer()
|
dataBuffers.forEach(d -> atomicLong.addAndGet(d.readableByteCount()));
|
||||||
.array().length));
|
|
||||||
return atomicLong;
|
return atomicLong;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import java.util.List;
|
|||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
import org.apache.catalina.Context;
|
import org.apache.catalina.Context;
|
||||||
|
import org.apache.catalina.Wrapper;
|
||||||
import org.apache.catalina.startup.Tomcat;
|
import org.apache.catalina.startup.Tomcat;
|
||||||
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
||||||
import org.springframework.boot.web.server.WebServer;
|
import org.springframework.boot.web.server.WebServer;
|
||||||
@ -61,7 +62,8 @@ public class FunctionalWebApplication {
|
|||||||
tomcat.setPort(9090);
|
tomcat.setPort(9090);
|
||||||
Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
|
Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
|
||||||
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
|
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
|
||||||
Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
|
Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
|
||||||
|
servletWrapper.setAsyncSupported(true);
|
||||||
rootContext.addServletMappingDecoded("/", "httpHandlerServlet");
|
rootContext.addServletMappingDecoded("/", "httpHandlerServlet");
|
||||||
|
|
||||||
TomcatWebServer server = new TomcatWebServer(tomcat);
|
TomcatWebServer server = new TomcatWebServer(tomcat);
|
||||||
|
@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@RestController
|
@RestController("FurtherCorsConfigsController-cors-on-global-config-and-more")
|
||||||
@RequestMapping("/cors-on-global-config-and-more")
|
@RequestMapping("/cors-on-global-config-and-more")
|
||||||
public class FurtherCorsConfigsController {
|
public class FurtherCorsConfigsController {
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@RestController
|
@RestController("RegularRestController-cors-on-global-config")
|
||||||
@RequestMapping("/cors-on-global-config")
|
@RequestMapping("/cors-on-global-config")
|
||||||
public class RegularRestController {
|
public class RegularRestController {
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.baeldung.reactive.security;
|
package com.baeldung.reactive.security;
|
||||||
|
|
||||||
|
import org.springframework.boot.actuate.autoconfigure.security.reactive.EndpointRequest;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
|
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
|
||||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
||||||
@ -9,6 +10,8 @@ import org.springframework.security.core.userdetails.User;
|
|||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||||
|
|
||||||
|
import com.baeldung.reactive.actuator.FeaturesEndpoint;
|
||||||
|
|
||||||
@EnableWebFluxSecurity
|
@EnableWebFluxSecurity
|
||||||
@EnableReactiveMethodSecurity
|
@EnableReactiveMethodSecurity
|
||||||
public class SecurityConfig {
|
public class SecurityConfig {
|
||||||
@ -16,25 +19,33 @@ public class SecurityConfig {
|
|||||||
@Bean
|
@Bean
|
||||||
public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
|
public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
|
||||||
return http.authorizeExchange()
|
return http.authorizeExchange()
|
||||||
.pathMatchers("/admin").hasAuthority("ROLE_ADMIN")
|
.pathMatchers("/admin")
|
||||||
.anyExchange().permitAll()
|
.hasAuthority("ROLE_ADMIN")
|
||||||
.and().formLogin()
|
.matchers(EndpointRequest.to(FeaturesEndpoint.class))
|
||||||
.and().build();
|
.permitAll()
|
||||||
|
.anyExchange()
|
||||||
|
.permitAll()
|
||||||
|
.and()
|
||||||
|
.formLogin()
|
||||||
|
.and()
|
||||||
|
.csrf()
|
||||||
|
.disable()
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public MapReactiveUserDetailsService userDetailsService() {
|
public MapReactiveUserDetailsService userDetailsService() {
|
||||||
UserDetails user = User.withDefaultPasswordEncoder()
|
UserDetails user = User.withDefaultPasswordEncoder()
|
||||||
.username("user")
|
.username("user")
|
||||||
.password("password")
|
.password("password")
|
||||||
.roles("USER")
|
.roles("USER")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
UserDetails admin = User.withDefaultPasswordEncoder()
|
UserDetails admin = User.withDefaultPasswordEncoder()
|
||||||
.username("admin")
|
.username("admin")
|
||||||
.password("password")
|
.password("password")
|
||||||
.roles("ADMIN")
|
.roles("ADMIN")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
return new MapReactiveUserDetailsService(user, admin);
|
return new MapReactiveUserDetailsService(user, admin);
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import static org.springframework.web.reactive.function.server.RouterFunctions.t
|
|||||||
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
|
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
|
||||||
|
|
||||||
import org.apache.catalina.Context;
|
import org.apache.catalina.Context;
|
||||||
|
import org.apache.catalina.Wrapper;
|
||||||
import org.apache.catalina.startup.Tomcat;
|
import org.apache.catalina.startup.Tomcat;
|
||||||
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
||||||
import org.springframework.boot.web.server.WebServer;
|
import org.springframework.boot.web.server.WebServer;
|
||||||
@ -41,7 +42,8 @@ public class ExploreSpring5URLPatternUsingRouterFunctions {
|
|||||||
tomcat.setPort(9090);
|
tomcat.setPort(9090);
|
||||||
Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
|
Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
|
||||||
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
|
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
|
||||||
Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
|
Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
|
||||||
|
servletWrapper.setAsyncSupported(true);
|
||||||
rootContext.addServletMappingDecoded("/", "httpHandlerServlet");
|
rootContext.addServletMappingDecoded("/", "httpHandlerServlet");
|
||||||
|
|
||||||
TomcatWebServer server = new TomcatWebServer(tomcat);
|
TomcatWebServer server = new TomcatWebServer(tomcat);
|
||||||
|
@ -4,8 +4,12 @@ import lombok.AllArgsConstructor;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
|
||||||
public class EmployeeCreationEvent {
|
public class EmployeeCreationEvent {
|
||||||
private String employeeId;
|
private String employeeId;
|
||||||
private String creationTime;
|
private String creationTime;
|
||||||
|
public EmployeeCreationEvent(String employeeId, String creationTime) {
|
||||||
|
super();
|
||||||
|
this.employeeId = employeeId;
|
||||||
|
this.creationTime = creationTime;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@Component
|
@Component("EmployeeWebSocketHandler")
|
||||||
public class EmployeeWebSocketHandler implements WebSocketHandler {
|
public class EmployeeWebSocketHandler implements WebSocketHandler {
|
||||||
|
|
||||||
ObjectMapper om = new ObjectMapper();
|
ObjectMapper om = new ObjectMapper();
|
||||||
|
@ -1,19 +1,22 @@
|
|||||||
package com.baeldung.websocket;
|
package com.baeldung.websocket;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.reactive.HandlerMapping;
|
import org.springframework.web.reactive.HandlerMapping;
|
||||||
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
|
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
|
||||||
import org.springframework.web.reactive.socket.WebSocketHandler;
|
import org.springframework.web.reactive.socket.WebSocketHandler;
|
||||||
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;
|
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class ReactiveWebSocketConfiguration {
|
public class ReactiveWebSocketConfiguration {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@Qualifier("ReactiveWebSocketHandler")
|
||||||
private WebSocketHandler webSocketHandler;
|
private WebSocketHandler webSocketHandler;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -14,7 +14,7 @@ import java.time.Duration;
|
|||||||
import static java.time.LocalDateTime.now;
|
import static java.time.LocalDateTime.now;
|
||||||
import static java.util.UUID.randomUUID;
|
import static java.util.UUID.randomUUID;
|
||||||
|
|
||||||
@Component
|
@Component("ReactiveWebSocketHandler")
|
||||||
public class ReactiveWebSocketHandler implements WebSocketHandler {
|
public class ReactiveWebSocketHandler implements WebSocketHandler {
|
||||||
|
|
||||||
private static final ObjectMapper json = new ObjectMapper();
|
private static final ObjectMapper json = new ObjectMapper();
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package com.baeldung.functional;
|
package com.baeldung.functional;
|
||||||
|
|
||||||
|
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
||||||
|
import static org.springframework.web.reactive.function.BodyInserters.fromResource;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -12,9 +15,6 @@ import org.springframework.util.LinkedMultiValueMap;
|
|||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.reactive.function.BodyInserters;
|
import org.springframework.web.reactive.function.BodyInserters;
|
||||||
|
|
||||||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
|
||||||
import static org.springframework.web.reactive.function.BodyInserters.fromResource;
|
|
||||||
|
|
||||||
public class FunctionalWebApplicationIntegrationTest {
|
public class FunctionalWebApplicationIntegrationTest {
|
||||||
|
|
||||||
private static WebTestClient client;
|
private static WebTestClient client;
|
||||||
|
@ -6,8 +6,6 @@ import org.junit.Test;
|
|||||||
import org.springframework.boot.web.server.WebServer;
|
import org.springframework.boot.web.server.WebServer;
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
|
||||||
import com.baeldung.reactive.urlmatch.ExploreSpring5URLPatternUsingRouterFunctions;
|
|
||||||
|
|
||||||
public class ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest {
|
public class ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest {
|
||||||
|
|
||||||
private static WebTestClient client;
|
private static WebTestClient client;
|
||||||
|
@ -28,7 +28,7 @@ public class SecurityIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenNoCredentials_thenRedirectToLogin() {
|
public void whenNoCredentials_thenRedirectToLogin() {
|
||||||
this.rest.get().uri("/").exchange().expectStatus().is3xxRedirection();
|
this.rest.get().uri("/").exchange().expectStatus().is4xxClientError();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -53,7 +53,7 @@ public class WebTestClientIntegrationTest {
|
|||||||
.uri("/resource")
|
.uri("/resource")
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.is3xxRedirection()
|
.isOk()
|
||||||
.expectBody();
|
.expectBody();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BIN
spring-5-reactive/src/test/resources/baeldung-weekly.png
Normal file
BIN
spring-5-reactive/src/test/resources/baeldung-weekly.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 27 KiB |
@ -58,6 +58,12 @@
|
|||||||
<artifactId>spring-security-test</artifactId>
|
<artifactId>spring-security-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.security.oauth.boot</groupId>
|
||||||
|
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
||||||
|
<version>2.0.1.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.oauth2extractors;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@Controller
|
||||||
|
public class ExtractorsApplication {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ExtractorsApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/")
|
||||||
|
public String index() {
|
||||||
|
return "oauth2_extractors";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.baeldung.oauth2extractors.configuration;
|
||||||
|
|
||||||
|
import com.baeldung.oauth2extractors.extractor.CustomAuthoritiesExtractor;
|
||||||
|
import com.baeldung.oauth2extractors.extractor.CustomPrincipalExtractor;
|
||||||
|
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
|
||||||
|
import org.springframework.boot.autoconfigure.security.oauth2.resource.AuthoritiesExtractor;
|
||||||
|
import org.springframework.boot.autoconfigure.security.oauth2.resource.PrincipalExtractor;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@PropertySource("application-oauth2-extractors.properties")
|
||||||
|
@EnableOAuth2Sso
|
||||||
|
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
|
http.antMatcher("/**")
|
||||||
|
.authorizeRequests()
|
||||||
|
.antMatchers("/login**")
|
||||||
|
.permitAll()
|
||||||
|
.anyRequest()
|
||||||
|
.authenticated()
|
||||||
|
.and()
|
||||||
|
.formLogin().disable();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PrincipalExtractor principalExtractor() {
|
||||||
|
return new CustomPrincipalExtractor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public AuthoritiesExtractor authoritiesExtractor() {
|
||||||
|
return new CustomAuthoritiesExtractor();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.baeldung.oauth2extractors.extractor;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.security.oauth2.resource.AuthoritiesExtractor;
|
||||||
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
|
import org.springframework.security.core.authority.AuthorityUtils;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class CustomAuthoritiesExtractor implements AuthoritiesExtractor {
|
||||||
|
private List<GrantedAuthority> GITHUB_FREE_AUTHORITIES = AuthorityUtils
|
||||||
|
.commaSeparatedStringToAuthorityList("GITHUB_USER,GITHUB_USER_FREE");
|
||||||
|
private List<GrantedAuthority> GITHUB_SUBSCRIBED_AUTHORITIES = AuthorityUtils
|
||||||
|
.commaSeparatedStringToAuthorityList("GITHUB_USER,GITHUB_USER_SUBSCRIBED");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<GrantedAuthority> extractAuthorities(Map<String, Object> map) {
|
||||||
|
if (Objects.nonNull(map.get("plan"))) {
|
||||||
|
if (!((LinkedHashMap) map.get("plan"))
|
||||||
|
.get("name")
|
||||||
|
.equals("free")) {
|
||||||
|
return GITHUB_SUBSCRIBED_AUTHORITIES;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return GITHUB_FREE_AUTHORITIES;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.oauth2extractors.extractor;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.security.oauth2.resource.PrincipalExtractor;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class CustomPrincipalExtractor implements PrincipalExtractor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object extractPrincipal(Map<String, Object> map) {
|
||||||
|
return map.get("login");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
security.oauth2.client.client-id=89a7c4facbb3434d599d
|
||||||
|
security.oauth2.client.client-secret=9b3b08e4a340bd20e866787e4645b54f73d74b6a
|
||||||
|
security.oauth2.client.access-token-uri=https://github.com/login/oauth/access_token
|
||||||
|
security.oauth2.client.user-authorization-uri=https://github.com/login/oauth/authorize
|
||||||
|
security.oauth2.client.scope=read:user,user:email
|
||||||
|
security.oauth2.resource.user-info-uri=https://api.github.com/user
|
@ -0,0 +1,21 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
||||||
|
<title>Spring Security Principal and Authorities extractor</title>
|
||||||
|
<link rel="stylesheet"
|
||||||
|
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"/>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<div class="col-sm-12">
|
||||||
|
<h1>Secured Page</h1>
|
||||||
|
Authenticated username:
|
||||||
|
<div th:text="${#authentication.name}"></div>
|
||||||
|
Authorities:
|
||||||
|
<div th:text="${#authentication.authorities}"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,54 @@
|
|||||||
|
package com.baeldung.oauth2extractors;
|
||||||
|
|
||||||
|
import com.baeldung.oauth2extractors.configuration.SecurityConfig;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = ExtractorsApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
@ContextConfiguration(classes = {SecurityConfig.class})
|
||||||
|
public class ExtractorsUnitTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebApplicationContext context;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Filter springSecurityFilterChain;
|
||||||
|
|
||||||
|
private MockMvc mvc;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
mvc = MockMvcBuilders
|
||||||
|
.webAppContextSetup(context)
|
||||||
|
.addFilters(springSecurityFilterChain)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() throws Exception {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidRequestWithoutAuthentication_shouldFailWith302() throws Exception {
|
||||||
|
mvc
|
||||||
|
.perform(get("/"))
|
||||||
|
.andExpect(status().isFound())
|
||||||
|
.andReturn();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,6 +3,7 @@ package com.baeldung.displayallbeans.controller;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
||||||
@ -14,9 +15,9 @@ public class FooController {
|
|||||||
private FooService fooService;
|
private FooService fooService;
|
||||||
|
|
||||||
@GetMapping(value = "/displayallbeans")
|
@GetMapping(value = "/displayallbeans")
|
||||||
public String getHeaderAndBody(Map<String, Object> model) {
|
public ResponseEntity<String> getHeaderAndBody(Map<String, Object> model) {
|
||||||
model.put("header", fooService.getHeader());
|
model.put("header", fooService.getHeader());
|
||||||
model.put("message", fooService.getBody());
|
model.put("message", fooService.getBody());
|
||||||
return "displayallbeans";
|
return ResponseEntity.ok("displayallbeans");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package org.baeldung.boot.config;
|
||||||
|
|
||||||
|
import org.baeldung.boot.converter.StringToEmployeeConverter;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.format.FormatterRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addFormatters(FormatterRegistry registry) {
|
||||||
|
registry.addConverter(new StringToEmployeeConverter());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package org.baeldung.boot.converter;
|
||||||
|
|
||||||
|
import org.springframework.core.convert.converter.Converter;
|
||||||
|
|
||||||
|
import com.baeldung.toggle.Employee;
|
||||||
|
|
||||||
|
public class StringToEmployeeConverter implements Converter<String, Employee> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Employee convert(String from) {
|
||||||
|
String[] data = from.split(",");
|
||||||
|
return new Employee(
|
||||||
|
Long.parseLong(data[0]),
|
||||||
|
Double.parseDouble(data[1]));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package org.baeldung.boot.converter.controller;
|
||||||
|
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.baeldung.toggle.Employee;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class StringToEmployeeConverterController {
|
||||||
|
|
||||||
|
@GetMapping("/string-to-employee")
|
||||||
|
public ResponseEntity<Object> getStringToEmployee(@RequestParam("employee") Employee employee) {
|
||||||
|
return ResponseEntity.ok(employee);
|
||||||
|
}
|
||||||
|
}
|
@ -4,17 +4,15 @@ import static org.hamcrest.CoreMatchers.is;
|
|||||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
import org.baeldung.boot.ApplicationIntegrationTest;
|
import org.baeldung.boot.DemoApplicationIntegrationTest;
|
||||||
import org.baeldung.demo.model.Foo;
|
import org.baeldung.demo.model.Foo;
|
||||||
import org.baeldung.session.exception.repository.FooRepository;
|
import org.baeldung.demo.repository.FooRepository;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.test.context.TestPropertySource;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@TestPropertySource("classpath:exception-hibernate.properties")
|
public class HibernateSessionIntegrationTest extends DemoApplicationIntegrationTest {
|
||||||
public class HibernateSessionIntegrationTest extends ApplicationIntegrationTest {
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FooRepository fooRepository;
|
private FooRepository fooRepository;
|
||||||
|
|
||||||
@ -23,7 +21,7 @@ public class HibernateSessionIntegrationTest extends ApplicationIntegrationTest
|
|||||||
Foo foo = new Foo("Exception Solved");
|
Foo foo = new Foo("Exception Solved");
|
||||||
fooRepository.save(foo);
|
fooRepository.save(foo);
|
||||||
foo = null;
|
foo = null;
|
||||||
foo = fooRepository.get(1);
|
foo = fooRepository.findByName("Exception Solved");
|
||||||
|
|
||||||
assertThat(foo, notNullValue());
|
assertThat(foo, notNullValue());
|
||||||
assertThat(foo.getId(), is(1));
|
assertThat(foo.getId(), is(1));
|
||||||
|
@ -6,9 +6,11 @@ import org.baeldung.session.exception.repository.FooRepository;
|
|||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.TestPropertySource;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@TestPropertySource("classpath:exception-hibernate.properties")
|
||||||
public class NoHibernateSessionIntegrationTest extends ApplicationIntegrationTest {
|
public class NoHibernateSessionIntegrationTest extends ApplicationIntegrationTest {
|
||||||
@Autowired
|
@Autowired
|
||||||
private FooRepository fooRepository;
|
private FooRepository fooRepository;
|
||||||
|
@ -43,7 +43,7 @@ public class CustomConverterIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenConvertingToBigDecimalUsingGenericConverter_thenSuccess() {
|
public void whenConvertingToBigDecimalUsingGenericConverter_thenSuccess() {
|
||||||
assertThat(conversionService.convert(Integer.valueOf(11), BigDecimal.class)).isEqualTo(BigDecimal.valueOf(11.00).setScale(2, BigDecimal.ROUND_HALF_EVEN));
|
assertThat(conversionService.convert(Integer.valueOf(11), BigDecimal.class)).isEqualTo(BigDecimal.valueOf(11.00).setScale(0, BigDecimal.ROUND_HALF_EVEN));
|
||||||
assertThat(conversionService.convert(Double.valueOf(25.23), BigDecimal.class)).isEqualByComparingTo(BigDecimal.valueOf(Double.valueOf(25.23)));
|
assertThat(conversionService.convert(Double.valueOf(25.23), BigDecimal.class)).isEqualByComparingTo(BigDecimal.valueOf(Double.valueOf(25.23)));
|
||||||
assertThat(conversionService.convert("2.32", BigDecimal.class)).isEqualTo(BigDecimal.valueOf(2.32));
|
assertThat(conversionService.convert("2.32", BigDecimal.class)).isEqualTo(BigDecimal.valueOf(2.32));
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package org.baeldung.demo.boottest;
|
package org.baeldung.demo.boottest;
|
||||||
|
|
||||||
import org.baeldung.demo.boottest.Employee;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import org.baeldung.demo.boottest.EmployeeRepository;
|
|
||||||
import org.baeldung.demo.boottest.EmployeeService;
|
import java.util.Arrays;
|
||||||
import org.baeldung.demo.boottest.EmployeeServiceImpl;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -15,11 +17,6 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
public class EmployeeServiceImplIntegrationTest {
|
public class EmployeeServiceImplIntegrationTest {
|
||||||
|
|
||||||
@ -50,9 +47,9 @@ public class EmployeeServiceImplIntegrationTest {
|
|||||||
Mockito.when(employeeRepository.findByName(john.getName())).thenReturn(john);
|
Mockito.when(employeeRepository.findByName(john.getName())).thenReturn(john);
|
||||||
Mockito.when(employeeRepository.findByName(alex.getName())).thenReturn(alex);
|
Mockito.when(employeeRepository.findByName(alex.getName())).thenReturn(alex);
|
||||||
Mockito.when(employeeRepository.findByName("wrong_name")).thenReturn(null);
|
Mockito.when(employeeRepository.findByName("wrong_name")).thenReturn(null);
|
||||||
Mockito.when(employeeRepository.findById(john.getId()).orElse(null)).thenReturn(john);
|
Mockito.when(employeeRepository.findById(john.getId())).thenReturn(Optional.of(john));
|
||||||
Mockito.when(employeeRepository.findAll()).thenReturn(allEmployees);
|
Mockito.when(employeeRepository.findAll()).thenReturn(allEmployees);
|
||||||
Mockito.when(employeeRepository.findById(-99L).orElse(null)).thenReturn(null);
|
Mockito.when(employeeRepository.findById(-99L)).thenReturn(Optional.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -11,9 +11,7 @@ public class PushController {
|
|||||||
@GetMapping(path = "/demoWithPush")
|
@GetMapping(path = "/demoWithPush")
|
||||||
public String demoWithPush(PushBuilder pushBuilder) {
|
public String demoWithPush(PushBuilder pushBuilder) {
|
||||||
if (null != pushBuilder) {
|
if (null != pushBuilder) {
|
||||||
pushBuilder.path("resources/logo.png")
|
pushBuilder.path("resources/logo.png").push();
|
||||||
.addHeader("Content-Type", "image/png")
|
|
||||||
.push();
|
|
||||||
}
|
}
|
||||||
return "demo";
|
return "demo";
|
||||||
}
|
}
|
||||||
@ -22,4 +20,4 @@ public class PushController {
|
|||||||
public String demoWithoutPush() {
|
public String demoWithoutPush() {
|
||||||
return "demo";
|
return "demo";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
29
testing-modules/java-faker/pom.xml
Normal file
29
testing-modules/java-faker/pom.xml
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?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-faker</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.javafaker</groupId>
|
||||||
|
<artifactId>javafaker</artifactId>
|
||||||
|
<version>0.15</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,115 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import com.github.javafaker.Faker;
|
||||||
|
import com.github.javafaker.service.FakeValuesService;
|
||||||
|
import com.github.javafaker.service.FakerIDN;
|
||||||
|
import com.github.javafaker.service.LocaleDoesNotExistException;
|
||||||
|
import com.github.javafaker.service.RandomService;
|
||||||
|
import javafx.scene.Parent;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
public class JavaFakerTest {
|
||||||
|
|
||||||
|
private Faker faker;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
faker = new Faker();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJavaFaker_whenAddressObjectCalled_checkValidAddressInfoGiven() throws Exception {
|
||||||
|
|
||||||
|
Faker faker = new Faker();
|
||||||
|
|
||||||
|
String streetName = faker.address().streetName();
|
||||||
|
String number = faker.address().buildingNumber();
|
||||||
|
String city = faker.address().city();
|
||||||
|
String country = faker.address().country();
|
||||||
|
|
||||||
|
System.out.println(String.format("%s\n%s\n%s\n%s",
|
||||||
|
number,
|
||||||
|
streetName,
|
||||||
|
city,
|
||||||
|
country));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJavaFakersWithSameSeed_whenNameCalled_CheckSameName() throws Exception {
|
||||||
|
|
||||||
|
Faker faker1 = new Faker(new Random(24));
|
||||||
|
Faker faker2 = new Faker(new Random(24));
|
||||||
|
|
||||||
|
assertEquals(faker1.name().firstName(), faker2.name().firstName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJavaFakersWithDifferentLocals_checkZipCodesMatchRegex() throws Exception {
|
||||||
|
|
||||||
|
Faker ukFaker = new Faker(new Locale("en-GB"));
|
||||||
|
Faker usFaker = new Faker(new Locale("en-US"));
|
||||||
|
|
||||||
|
System.out.println(String.format("American zipcode: %s", usFaker.address().zipCode()));
|
||||||
|
System.out.println(String.format("British postcode: %s", ukFaker.address().zipCode()));
|
||||||
|
|
||||||
|
Pattern ukPattern = Pattern.compile("([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\\s?[0-9][A-Za-z]{2})");
|
||||||
|
Matcher ukMatcher = ukPattern.matcher(ukFaker.address().zipCode());
|
||||||
|
|
||||||
|
assertTrue(ukMatcher.find());
|
||||||
|
|
||||||
|
Matcher usMatcher = Pattern.compile("^\\d{5}(?:[-\\s]\\d{4})?$").matcher(usFaker.address().zipCode());
|
||||||
|
|
||||||
|
assertTrue(usMatcher.find());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJavaFakerService_testFakersCreated() throws Exception {
|
||||||
|
|
||||||
|
RandomService randomService = new RandomService();
|
||||||
|
|
||||||
|
System.out.println(randomService.nextBoolean());
|
||||||
|
System.out.println(randomService.nextDouble());
|
||||||
|
|
||||||
|
Faker faker = new Faker(new Random(randomService.nextLong()));
|
||||||
|
|
||||||
|
System.out.println(faker.address().city());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFakeValuesService() throws Exception {
|
||||||
|
|
||||||
|
FakeValuesService fakeValuesService = new FakeValuesService(new Locale("en-GB"), new RandomService());
|
||||||
|
|
||||||
|
String email = fakeValuesService.bothify("????##@gmail.com");
|
||||||
|
Matcher emailMatcher = Pattern.compile("\\w{4}\\d{2}@gmail.com").matcher(email);
|
||||||
|
assertTrue(emailMatcher.find());
|
||||||
|
|
||||||
|
String alphaNumericString = fakeValuesService.regexify("[a-z1-9]{10}");
|
||||||
|
Matcher alphaNumericMatcher = Pattern.compile("[a-z1-9]{10}").matcher(alphaNumericString);
|
||||||
|
assertTrue(alphaNumericMatcher.find());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test(expected = LocaleDoesNotExistException.class)
|
||||||
|
public void givenWrongLocale_whenFakerIsInitialised_testLocaleDoesNotExistExceptionIsThrown() throws Exception {
|
||||||
|
|
||||||
|
Faker wrongLocaleFaker = new Faker(new Locale("en-seaWorld"));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user