Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -12,5 +12,4 @@ This module contains articles about Java 11 core features
|
||||
- [Java HTTPS Client Certificate Authentication](https://www.baeldung.com/java-https-client-certificate-authentication)
|
||||
- [Call Methods at Runtime Using Java Reflection](https://www.baeldung.com/java-method-reflection)
|
||||
- [Java HttpClient Basic Authentication](https://www.baeldung.com/java-httpclient-basic-auth)
|
||||
- [Java HttpClient With SSL](https://www.baeldung.com/java-httpclient-ssl)
|
||||
- [Adding Parameters to Java HttpClient Requests](https://www.baeldung.com/java-httpclient-request-parameters)
|
||||
- [Java HttpClient With SSL](https://www.baeldung.com/java-httpclient-ssl)
|
||||
@@ -0,0 +1,6 @@
|
||||
## Core Java 11
|
||||
|
||||
This module contains articles about Java 11 core features
|
||||
|
||||
### Relevant articles
|
||||
- [Adding Parameters to Java HttpClient Requests](https://www.baeldung.com/java-httpclient-request-parameters)
|
||||
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-11-3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-11-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>11</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>11</maven.compiler.target.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.map.multikey;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class BaseClassUserCache {
|
||||
private final Map<Object, User> cache = new HashMap<>();
|
||||
|
||||
public User getById(String id) {
|
||||
return cache.get(id);
|
||||
}
|
||||
|
||||
public User getById(Long id) {
|
||||
return cache.get(id);
|
||||
}
|
||||
|
||||
public void storeById(String id, User user) {
|
||||
cache.put(id, user);
|
||||
}
|
||||
|
||||
public void storeById(Long id, User user) {
|
||||
cache.put(id, user);
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.map.multikey;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MultipleMapsUserCache {
|
||||
private final Map<String, User> stringCache = new HashMap<>();
|
||||
private final Map<Long, User> longCache = new HashMap<>();
|
||||
|
||||
public User getById(String id) {
|
||||
return stringCache.get(id);
|
||||
}
|
||||
|
||||
public User getById(Long id) {
|
||||
return longCache.get(id);
|
||||
}
|
||||
|
||||
public void storeById(String id, User user) {
|
||||
stringCache.put(id, user);
|
||||
}
|
||||
|
||||
public void storeById(Long id, User user) {
|
||||
longCache.put(id, user);
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.map.multikey;
|
||||
|
||||
public class User {
|
||||
private final String name;
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.map.multikey;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class WrapperClassUserCache {
|
||||
private Map<CacheKey, User> cache = new HashMap<>();
|
||||
|
||||
public User getById(CacheKey key) {
|
||||
return cache.get(key);
|
||||
}
|
||||
|
||||
public void storeById(CacheKey key, User user) {
|
||||
cache.put(key, user);
|
||||
}
|
||||
|
||||
public static class CacheKey {
|
||||
private final Object value;
|
||||
|
||||
public CacheKey(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public CacheKey(Long value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
CacheKey cacheKey = (CacheKey) o;
|
||||
return value.equals(cacheKey.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.map.multikey;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class WrapperInterfaceUserCache {
|
||||
private Map<CacheKey, User> cache = new HashMap<>();
|
||||
|
||||
public User getById(CacheKey key) {
|
||||
return cache.get(key);
|
||||
}
|
||||
|
||||
public void storeById(CacheKey key, User user) {
|
||||
cache.put(key, user);
|
||||
}
|
||||
|
||||
public interface CacheKey {
|
||||
}
|
||||
|
||||
public static class StringCacheKey implements CacheKey{
|
||||
private final String value;
|
||||
|
||||
public StringCacheKey(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
StringCacheKey that = (StringCacheKey) o;
|
||||
return value.equals(that.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(value);
|
||||
}
|
||||
}
|
||||
|
||||
public static class LongCacheKey implements CacheKey {
|
||||
private final Long value;
|
||||
|
||||
public LongCacheKey(Long value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
LongCacheKey that = (LongCacheKey) o;
|
||||
return value.equals(that.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.map.multikey;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class BaseClassUserCacheUnitTest {
|
||||
private BaseClassUserCache cache = new BaseClassUserCache();
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
cache.storeById("a", new User("User A"));
|
||||
cache.storeById("b", new User("User B"));
|
||||
cache.storeById(3L, new User("User 3"));
|
||||
cache.storeById(4L, new User("User 4"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByString() {
|
||||
User user = cache.getById("b");
|
||||
assertNotNull(user);
|
||||
assertEquals("User B", user.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByLong() {
|
||||
User user = cache.getById(4L);
|
||||
assertNotNull(user);
|
||||
assertEquals("User 4", user.getName());
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.map.multikey;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
class MultipleMapsUserCacheUnitTest {
|
||||
private MultipleMapsUserCache cache = new MultipleMapsUserCache();
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
cache.storeById("a", new User("User A"));
|
||||
cache.storeById("b", new User("User B"));
|
||||
cache.storeById(3L, new User("User 3"));
|
||||
cache.storeById(4L, new User("User 4"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByString() {
|
||||
User user = cache.getById("b");
|
||||
assertNotNull(user);
|
||||
assertEquals("User B", user.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByLong() {
|
||||
User user = cache.getById(4L);
|
||||
assertNotNull(user);
|
||||
assertEquals("User 4", user.getName());
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.map.multikey;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
class WrapperClassUserCacheUnitTest {
|
||||
private WrapperClassUserCache cache = new WrapperClassUserCache();
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
cache.storeById(new WrapperClassUserCache.CacheKey("a"), new User("User A"));
|
||||
cache.storeById(new WrapperClassUserCache.CacheKey("b"), new User("User B"));
|
||||
cache.storeById(new WrapperClassUserCache.CacheKey(3L), new User("User 3"));
|
||||
cache.storeById(new WrapperClassUserCache.CacheKey(4L), new User("User 4"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByString() {
|
||||
User user = cache.getById(new WrapperClassUserCache.CacheKey("b"));
|
||||
assertNotNull(user);
|
||||
assertEquals("User B", user.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByLong() {
|
||||
User user = cache.getById(new WrapperClassUserCache.CacheKey(4L));
|
||||
assertNotNull(user);
|
||||
assertEquals("User 4", user.getName());
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.map.multikey;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
class WrapperInterfaceUserCacheUnitTest {
|
||||
private WrapperInterfaceUserCache cache = new WrapperInterfaceUserCache();
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
cache.storeById(new WrapperInterfaceUserCache.StringCacheKey("a"), new User("User A"));
|
||||
cache.storeById(new WrapperInterfaceUserCache.StringCacheKey("b"), new User("User B"));
|
||||
cache.storeById(new WrapperInterfaceUserCache.LongCacheKey(3L), new User("User 3"));
|
||||
cache.storeById(new WrapperInterfaceUserCache.LongCacheKey(4L), new User("User 4"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByString() {
|
||||
User user = cache.getById(new WrapperInterfaceUserCache.StringCacheKey("b"));
|
||||
assertNotNull(user);
|
||||
assertEquals("User B", user.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByLong() {
|
||||
User user = cache.getById(new WrapperInterfaceUserCache.LongCacheKey(4L));
|
||||
assertNotNull(user);
|
||||
assertEquals("User 4", user.getName());
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,16 @@
|
||||
<version>3.23.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.streams.filteronlyoneelement;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
|
||||
public class BenchmarkRunner {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
public static class MyState {
|
||||
final Stream<Integer> getIntegers() {
|
||||
return IntStream.range(1, 1000000)
|
||||
.boxed();
|
||||
}
|
||||
|
||||
final Predicate<Integer> PREDICATE = i -> i == 751879;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void evaluateFindUniqueElementMatchingPredicate_WithReduction(Blackhole blackhole, MyState state) {
|
||||
blackhole.consume(FilterUtils.findUniqueElementMatchingPredicate_WithReduction(state.getIntegers(), state.PREDICATE));
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void evaluateFindUniqueElementMatchingPredicate_WithCollectingAndThen(Blackhole blackhole, MyState state) {
|
||||
blackhole.consume(FilterUtils.findUniqueElementMatchingPredicate_WithCollectingAndThen(state.getIntegers(), state.PREDICATE));
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void evaluateGetUniqueElementMatchingPredicate_WithReduction(Blackhole blackhole, MyState state) {
|
||||
try {
|
||||
FilterUtils.getUniqueElementMatchingPredicate_WithReduction(state.getIntegers(), state.PREDICATE);
|
||||
} catch (IllegalStateException exception) {
|
||||
blackhole.consume(exception);
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void evaluateGetUniqueElementMatchingPredicate_WithCollectingAndThen(Blackhole blackhole, MyState state) {
|
||||
try {
|
||||
FilterUtils.getUniqueElementMatchingPredicate_WithCollectingAndThen(state.getIntegers(), state.PREDICATE);
|
||||
} catch (IllegalStateException exception) {
|
||||
blackhole.consume(exception);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.streams.filteronlyoneelement;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class FilterUtils {
|
||||
|
||||
public static <T> Optional<T> findUniqueElementMatchingPredicate_WithReduction(Stream<T> elements, Predicate<T> predicate) {
|
||||
return elements.filter(predicate)
|
||||
.collect(Collectors.reducing((a, b) -> null));
|
||||
}
|
||||
|
||||
public static <T> T getUniqueElementMatchingPredicate_WithReduction(Stream<T> elements, Predicate<T> predicate) {
|
||||
return elements.filter(predicate)
|
||||
.reduce((a, b) -> {
|
||||
throw new IllegalStateException("Too many elements match the predicate");
|
||||
})
|
||||
.orElseThrow(() -> new IllegalStateException("No element matches the predicate"));
|
||||
}
|
||||
|
||||
public static <T> Optional<T> findUniqueElementMatchingPredicate_WithCollectingAndThen(Stream<T> elements, Predicate<T> predicate) {
|
||||
return elements.filter(predicate)
|
||||
.collect(Collectors.collectingAndThen(Collectors.toList(), list -> Optional.ofNullable(findUniqueElement(list))));
|
||||
}
|
||||
|
||||
private static <T> T findUniqueElement(List<T> elements) {
|
||||
if (elements.size() == 1) {
|
||||
return elements.get(0);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T> T getUniqueElementMatchingPredicate_WithCollectingAndThen(Stream<T> elements, Predicate<T> predicate) {
|
||||
return elements.filter(predicate)
|
||||
.collect(Collectors.collectingAndThen(Collectors.toList(), FilterUtils::getUniqueElement));
|
||||
}
|
||||
|
||||
private static <T> T getUniqueElement(List<T> elements) {
|
||||
if (elements.size() > 1) {
|
||||
throw new IllegalStateException("Too many elements match the predicate");
|
||||
} else if (elements.size() == 0) {
|
||||
throw new IllegalStateException("No element matches the predicate");
|
||||
}
|
||||
return elements.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package com.baeldung.streams.filteronlyoneelement;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class FilterUtilsUnitTest {
|
||||
|
||||
private static final Predicate<Integer> IS_STRICTLY_GREATER_THAN5 = i -> i > 5;
|
||||
private static final Predicate<Integer> IS_STRICTLY_GREATER_THAN4 = i -> i > 4;
|
||||
private static final Predicate<Integer> IS_STRICTLY_GREATER_THAN3 = i -> i > 3;
|
||||
|
||||
private Stream getIntegers() {
|
||||
return Stream.of(1, 2, 3, 4, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNoElementMatchingPredicate_WhenFindUniqueElementMatchingPredicateWithReduction_ThenNoneFound() {
|
||||
assertTrue(FilterUtils.findUniqueElementMatchingPredicate_WithReduction(getIntegers(), IS_STRICTLY_GREATER_THAN5)
|
||||
.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoElementsMatchingPredicate_WhenFindUniqueElementMatchingPredicateWithReduction_ThenEmpty() {
|
||||
assertTrue(FilterUtils.findUniqueElementMatchingPredicate_WithReduction(getIntegers(), IS_STRICTLY_GREATER_THAN3)
|
||||
.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenOnlyOneElementMatchingPredicate_WhenFindUniqueElementMatchingPredicateWithReduction_ThenFindsIt() {
|
||||
assertEquals(5, FilterUtils.findUniqueElementMatchingPredicate_WithReduction(getIntegers(), IS_STRICTLY_GREATER_THAN4)
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNoElementMatchingPredicate_WhenGetUniqueElementMatchingPredicateWithReduction_ThenThrows() {
|
||||
assertThrows(IllegalStateException.class, () -> FilterUtils.getUniqueElementMatchingPredicate_WithReduction(getIntegers(), IS_STRICTLY_GREATER_THAN5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoElementsMatchingPredicate_WhenGetUniqueElementMatchingPredicateWithReduction_ThenThrows() {
|
||||
assertThrows(IllegalStateException.class, () -> FilterUtils.getUniqueElementMatchingPredicate_WithReduction(getIntegers(), IS_STRICTLY_GREATER_THAN3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenOnlyOneElementMatchingPredicate_WhenFindUniqueElementMatchingPredicateWithReduction_ThenGetIt() {
|
||||
assertEquals(5, FilterUtils.getUniqueElementMatchingPredicate_WithReduction(getIntegers(), IS_STRICTLY_GREATER_THAN4));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNoElementMatchingPredicate_WhenFindUniqueElementMatchingPredicateWithCollectingAndThen_ThenEmpty() {
|
||||
assertTrue(FilterUtils.findUniqueElementMatchingPredicate_WithCollectingAndThen(getIntegers(), IS_STRICTLY_GREATER_THAN5)
|
||||
.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoElementsMatchingPredicate_WhenFindUniqueElementMatchingPredicateWithCollectingAndThen_ThenEmpty() {
|
||||
assertTrue(FilterUtils.findUniqueElementMatchingPredicate_WithCollectingAndThen(getIntegers(), IS_STRICTLY_GREATER_THAN3)
|
||||
.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenOnlyOneElementMatchingPredicate_WhenFindUniqueElementMatchingPredicateWithCollectingAndThen_ThenFindsIt() {
|
||||
assertEquals(5, FilterUtils.findUniqueElementMatchingPredicate_WithCollectingAndThen(getIntegers(), IS_STRICTLY_GREATER_THAN4)
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNoElementMatchingPredicate_WhenGetUniqueElementMatchingPredicateWithCollectingAndThen_ThenThrows() {
|
||||
assertThrows(IllegalStateException.class, () -> FilterUtils.getUniqueElementMatchingPredicate_WithCollectingAndThen(getIntegers(), IS_STRICTLY_GREATER_THAN5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoElementsMatchingPredicate_WhenGetUniqueElementMatchingPredicateWithCollectingAndThen_ThenThrows() {
|
||||
assertThrows(IllegalStateException.class, () -> FilterUtils.getUniqueElementMatchingPredicate_WithCollectingAndThen(getIntegers(), IS_STRICTLY_GREATER_THAN3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenOnlyOneElementMatchingPredicate_WhenFindUniqueElementMatchingPredicateWithCollectingAndThen_ThenGetIt() {
|
||||
assertEquals(5, FilterUtils.getUniqueElementMatchingPredicate_WithCollectingAndThen(getIntegers(), IS_STRICTLY_GREATER_THAN4));
|
||||
}
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.removewhitespace;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class RemoveWhitespaceUnitTest {
|
||||
private final String myString = " I am a wonderful String ! ";
|
||||
|
||||
@Test
|
||||
void givenStringWithWhitespace_whenRemoveAllWhitespace_shouldGetExpectedResult() {
|
||||
String result = myString.replaceAll("\\s", "");
|
||||
assertThat(result).isEqualTo("IamawonderfulString!");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringWithWhitespace_whenRemoveAllWhitespaceUsingStringUtils_shouldGetExpectedResult() {
|
||||
String result = StringUtils.deleteWhitespace(myString);
|
||||
assertThat(result).isEqualTo("IamawonderfulString!");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringWithWhitespace_whenReplaceConsecutiveSpacesWithSingleSpace_shouldGetExpectedResult() {
|
||||
String result = myString.replaceAll("\\s+", " ");
|
||||
assertThat(result).isEqualTo(" I am a wonderful String ! ");
|
||||
assertThat(result.trim()).isEqualTo("I am a wonderful String !");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringWithWhitespace_whenNormalizeWithApacheCommons_shouldGetExpectedResult() {
|
||||
String result = StringUtils.normalizeSpace(myString);
|
||||
assertThat(result).isEqualTo("I am a wonderful String !");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user