< prev index next >

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

Print this page




  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8195649
  26  * @summary Basic functional test of Optional
  27  * @author Mike Duigou
  28  * @build ObscureException
  29  * @run testng Basic
  30  */
  31 
  32 import java.util.List;
  33 import java.util.NoSuchElementException;
  34 import java.util.Optional;
  35 import java.util.concurrent.atomic.AtomicBoolean;

  36 
  37 import static java.util.stream.Collectors.toList;
  38 
  39 import static org.testng.Assert.*;
  40 import org.testng.annotations.Test;
  41 
  42 public class Basic {
  43 
  44     /**
  45      * Checks a block of assertions over an empty Optional.
  46      */
  47     void checkEmpty(Optional<String> empty) {
  48         assertTrue(empty.equals(Optional.empty()));
  49         assertTrue(Optional.empty().equals(empty));
  50         assertFalse(empty.equals(Optional.of("unexpected")));
  51         assertFalse(Optional.of("unexpected").equals(empty));
  52         assertFalse(empty.equals("unexpected"));
  53 
  54         assertFalse(empty.isPresent());

  55         assertEquals(empty.hashCode(), 0);
  56         assertEquals(empty.orElse("x"), "x");
  57         assertEquals(empty.orElseGet(() -> "y"), "y");
  58 
  59         assertThrows(NoSuchElementException.class, () -> empty.get());
  60         assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
  61         assertThrows(ObscureException.class,       () -> empty.orElseThrow(ObscureException::new));
  62 
  63         var b = new AtomicBoolean();
  64         empty.ifPresent(s -> b.set(true));
  65         assertFalse(b.get());
  66 
  67         var b1 = new AtomicBoolean(false);
  68         var b2 = new AtomicBoolean(false);
  69         empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
  70         assertFalse(b1.get());
  71         assertTrue(b2.get());
  72 
  73         assertEquals(empty.toString(), "Optional.empty");
  74     }


 183 
 184     @Test(groups = "unit")
 185     public void testOrEmptyPresent() {
 186         checkPresent(Optional.<String>empty().or(() -> Optional.of("plugh")), "plugh");
 187     }
 188 
 189     @Test(groups = "unit")
 190     public void testOrPresentDontCare() {
 191         checkPresent(Optional.of("xyzzy").or(() -> { fail(); return Optional.of("plugh"); }), "xyzzy");
 192     }
 193 
 194     @Test(groups = "unit")
 195     public void testStreamEmpty() {
 196         assertEquals(Optional.empty().stream().collect(toList()), List.of());
 197     }
 198 
 199     @Test(groups = "unit")
 200     public void testStreamPresent() {
 201         assertEquals(Optional.of("xyzzy").stream().collect(toList()), List.of("xyzzy"));
 202     }











 203 }


  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8195649
  26  * @summary Basic functional test of Optional
  27  * @author Mike Duigou
  28  * @build ObscureException
  29  * @run testng Basic
  30  */
  31 
  32 import java.util.List;
  33 import java.util.NoSuchElementException;
  34 import java.util.Optional;
  35 import java.util.concurrent.atomic.AtomicBoolean;
  36 import java.util.function.Predicate;
  37 
  38 import static java.util.stream.Collectors.toList;
  39 
  40 import static org.testng.Assert.*;
  41 import org.testng.annotations.Test;
  42 
  43 public class Basic {
  44 
  45     /**
  46      * Checks a block of assertions over an empty Optional.
  47      */
  48     void checkEmpty(Optional<String> empty) {
  49         assertTrue(empty.equals(Optional.empty()));
  50         assertTrue(Optional.empty().equals(empty));
  51         assertFalse(empty.equals(Optional.of("unexpected")));
  52         assertFalse(Optional.of("unexpected").equals(empty));
  53         assertFalse(empty.equals("unexpected"));
  54 
  55         assertFalse(empty.isPresent());
  56         assertTrue(empty.isEmpty());
  57         assertEquals(empty.hashCode(), 0);
  58         assertEquals(empty.orElse("x"), "x");
  59         assertEquals(empty.orElseGet(() -> "y"), "y");
  60 
  61         assertThrows(NoSuchElementException.class, () -> empty.get());
  62         assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
  63         assertThrows(ObscureException.class,       () -> empty.orElseThrow(ObscureException::new));
  64 
  65         var b = new AtomicBoolean();
  66         empty.ifPresent(s -> b.set(true));
  67         assertFalse(b.get());
  68 
  69         var b1 = new AtomicBoolean(false);
  70         var b2 = new AtomicBoolean(false);
  71         empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
  72         assertFalse(b1.get());
  73         assertTrue(b2.get());
  74 
  75         assertEquals(empty.toString(), "Optional.empty");
  76     }


 185 
 186     @Test(groups = "unit")
 187     public void testOrEmptyPresent() {
 188         checkPresent(Optional.<String>empty().or(() -> Optional.of("plugh")), "plugh");
 189     }
 190 
 191     @Test(groups = "unit")
 192     public void testOrPresentDontCare() {
 193         checkPresent(Optional.of("xyzzy").or(() -> { fail(); return Optional.of("plugh"); }), "xyzzy");
 194     }
 195 
 196     @Test(groups = "unit")
 197     public void testStreamEmpty() {
 198         assertEquals(Optional.empty().stream().collect(toList()), List.of());
 199     }
 200 
 201     @Test(groups = "unit")
 202     public void testStreamPresent() {
 203         assertEquals(Optional.of("xyzzy").stream().collect(toList()), List.of("xyzzy"));
 204     }
 205 
 206     @Test(groups = "unit")
 207     public void testIsEmpty() {
 208         var integerList = List.of(1,2,3,4,5);
 209         Predicate<Integer> isPositiveNumber =  x -> x > 0;
 210         Predicate<Integer> isNegativeNumber = x -> x < 0;
 211         Optional<Integer> positiveNumber = integerList.stream().filter(isPositiveNumber).findAny();
 212         Optional<Integer> negativeNumber = integerList.stream().filter(isNegativeNumber).findAny();
 213         assertFalse(positiveNumber.isEmpty());
 214         assertTrue(negativeNumber.isEmpty());
 215     }
 216 }
< prev index next >