BAEL-2820 Transforming Optional of an Empty String into Empty Optional (#6634)

This commit is contained in:
kwoyke 2019-04-04 18:43:36 +02:00 committed by Josh Cummings
parent 2b3b70167b
commit a7eb7bf8e0
2 changed files with 41 additions and 0 deletions

View File

@ -14,6 +14,14 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
@ -31,6 +39,7 @@
<properties>
<maven.compiler.source.version>11</maven.compiler.source.version>
<maven.compiler.target.version>11</maven.compiler.target.version>
<guava.version>27.1-jre</guava.version>
</properties>
</project>

View File

@ -0,0 +1,32 @@
package com.baeldung;
import com.google.common.base.Strings;
import org.junit.Assert;
import org.junit.Test;
import java.util.Optional;
import java.util.function.Predicate;
public class EmptyStringToEmptyOptionalUnitTest {
@Test
public void givenEmptyString_whenFilteringOnOptional_thenEmptyOptionalIsReturned() {
String str = "";
Optional<String> opt = Optional.ofNullable(str).filter(s -> !s.isEmpty());
Assert.assertFalse(opt.isPresent());
}
@Test
public void givenEmptyString_whenFilteringOnOptionalInJava11_thenEmptyOptionalIsReturned() {
String str = "";
Optional<String> opt = Optional.ofNullable(str).filter(Predicate.not(String::isEmpty));
Assert.assertFalse(opt.isPresent());
}
@Test
public void givenEmptyString_whenPassingResultOfEmptyToNullToOfNullable_thenEmptyOptionalIsReturned() {
String str = "";
Optional<String> opt = Optional.ofNullable(Strings.emptyToNull(str));
Assert.assertFalse(opt.isPresent());
}
}