< prev index next >

test/jdk/java/util/Optional/Basic.java

Print this page

        

@@ -31,10 +31,11 @@
 
 import java.util.List;
 import java.util.NoSuchElementException;
 import java.util.Optional;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Predicate;
 
 import static java.util.stream.Collectors.toList;
 
 import static org.testng.Assert.*;
 import org.testng.annotations.Test;

@@ -50,10 +51,11 @@
         assertFalse(empty.equals(Optional.of("unexpected")));
         assertFalse(Optional.of("unexpected").equals(empty));
         assertFalse(empty.equals("unexpected"));
 
         assertFalse(empty.isPresent());
+        assertTrue(empty.isEmpty());
         assertEquals(empty.hashCode(), 0);
         assertEquals(empty.orElse("x"), "x");
         assertEquals(empty.orElseGet(() -> "y"), "y");
 
         assertThrows(NoSuchElementException.class, () -> empty.get());

@@ -198,6 +200,17 @@
 
     @Test(groups = "unit")
     public void testStreamPresent() {
         assertEquals(Optional.of("xyzzy").stream().collect(toList()), List.of("xyzzy"));
     }
+
+    @Test(groups = "unit")
+    public void testIsEmpty() {
+        var integerList = List.of(1,2,3,4,5);
+        Predicate<Integer> isPositiveNumber =  x -> x > 0;
+        Predicate<Integer> isNegativeNumber = x -> x < 0;
+        Optional<Integer> positiveNumber = integerList.stream().filter(isPositiveNumber).findAny();
+        Optional<Integer> negativeNumber = integerList.stream().filter(isNegativeNumber).findAny();
+        assertFalse(positiveNumber.isEmpty());
+        assertTrue(negativeNumber.isEmpty());
+    }
 }
< prev index next >