Revert to branch base for coverage purposes.
--- a/test/jdk/java/util/Optional/Basic.java Thu Mar 29 16:12:41 2018 -0700
+++ b/test/jdk/java/util/Optional/Basic.java Mon Apr 02 16:11:53 2018 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -22,183 +22,350 @@
*/
/* @test
- * @bug 8195649
* @summary Basic functional test of Optional
* @author Mike Duigou
- * @build ObscureException
* @run testng Basic
*/
-import java.util.List;
+import java.lang.AssertionError;
+import java.lang.NullPointerException;
+import java.lang.Throwable;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;
-import static java.util.stream.Collectors.toList;
-
import static org.testng.Assert.*;
import org.testng.annotations.Test;
+
public class Basic {
- /**
- * Checks a block of assertions over an empty Optional.
- */
- void checkEmpty(Optional<String> empty) {
- assertTrue(empty.equals(Optional.empty()));
- assertTrue(Optional.empty().equals(empty));
- assertFalse(empty.equals(Optional.of("unexpected")));
- assertFalse(Optional.of("unexpected").equals(empty));
- assertFalse(empty.equals("unexpected"));
-
- assertFalse(empty.isPresent());
- assertEquals(empty.hashCode(), 0);
- assertEquals(empty.orElse("x"), "x");
- assertEquals(empty.orElseGet(() -> "y"), "y");
+ @Test(groups = "unit")
+ public void testEmpty() {
+ Optional<Boolean> empty = Optional.empty();
+ Optional<String> presentEmptyString = Optional.of("");
+ Optional<Boolean> present = Optional.of(Boolean.TRUE);
- assertThrows(NoSuchElementException.class, () -> empty.get());
- assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
- assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new));
-
- var b = new AtomicBoolean();
- empty.ifPresent(s -> b.set(true));
- assertFalse(b.get());
+ // empty
+ assertTrue(empty.equals(empty));
+ assertTrue(empty.equals(Optional.empty()));
+ assertTrue(!empty.equals(present));
+ assertTrue(0 == empty.hashCode());
+ assertTrue(!empty.toString().isEmpty());
+ assertTrue(!empty.toString().equals(presentEmptyString.toString()));
+ assertTrue(!empty.isPresent());
- var b1 = new AtomicBoolean(false);
- var b2 = new AtomicBoolean(false);
- empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
- assertFalse(b1.get());
- assertTrue(b2.get());
-
- assertEquals(empty.toString(), "Optional.empty");
- }
+ empty.ifPresent(v -> fail());
- /**
- * Checks a block of assertions over an Optional that is expected to
- * have a particular value present.
- */
- void checkPresent(Optional<String> opt, String expected) {
- assertFalse(opt.equals(Optional.empty()));
- assertFalse(Optional.empty().equals(opt));
- assertTrue(opt.equals(Optional.of(expected)));
- assertTrue(Optional.of(expected).equals(opt));
- assertFalse(opt.equals(Optional.of("unexpected")));
- assertFalse(Optional.of("unexpected").equals(opt));
- assertFalse(opt.equals("unexpected"));
+ AtomicBoolean emptyCheck = new AtomicBoolean();
+ empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
+ assertTrue(emptyCheck.get());
- assertTrue(opt.isPresent());
- assertEquals(opt.hashCode(), expected.hashCode());
- assertEquals(opt.orElse("unexpected"), expected);
- assertEquals(opt.orElseGet(() -> "unexpected"), expected);
-
- assertEquals(opt.get(), expected);
- assertEquals(opt.orElseThrow(), expected);
- assertEquals(opt.orElseThrow(ObscureException::new), expected);
+ try {
+ empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
+ fail();
+ } catch (ObscureException expected) {
+ } catch (AssertionError e) {
+ throw e;
+ } catch (Throwable t) {
+ fail();
+ }
- var b = new AtomicBoolean(false);
- opt.ifPresent(s -> b.set(true));
- assertTrue(b.get());
-
- var b1 = new AtomicBoolean(false);
- var b2 = new AtomicBoolean(false);
- opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
- assertTrue(b1.get());
- assertFalse(b2.get());
-
- assertEquals(opt.toString(), "Optional[" + expected + "]");
+ assertSame(null, empty.orElse(null));
+ RuntimeException orElse = new RuntimeException() { };
+ assertSame(Boolean.FALSE, empty.orElse(Boolean.FALSE));
+ assertSame(null, empty.orElseGet(() -> null));
+ assertSame(Boolean.FALSE, empty.orElseGet(() -> Boolean.FALSE));
}
@Test(groups = "unit")
- public void testEmpty() {
- checkEmpty(Optional.empty());
+ public void testIfPresentAndOrElseAndNull() {
+ Optional<Boolean> empty = Optional.empty();
+ Optional<Boolean> present = Optional.of(Boolean.TRUE);
+
+ // No NPE
+ present.ifPresentOrElse(v -> {}, null);
+ empty.ifPresent(null);
+ empty.ifPresentOrElse(null, () -> {});
+
+ // NPE
+ try {
+ present.ifPresent(null);
+ fail();
+ } catch (NullPointerException ex) {}
+ try {
+ present.ifPresentOrElse(null, () -> {});
+ fail();
+ } catch (NullPointerException ex) {}
+ try {
+ empty.ifPresentOrElse(v -> {}, null);
+ fail();
+ } catch (NullPointerException ex) {}
+ }
+
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyGet() {
+ Optional<Boolean> empty = Optional.empty();
+
+ Boolean got = empty.get();
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseGetNull() {
+ Optional<Boolean> empty = Optional.empty();
+
+ Boolean got = empty.orElseGet(null);
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseThrowNull() throws Throwable {
+ Optional<Boolean> empty = Optional.empty();
+
+ Boolean got = empty.orElseThrow(null);
+ }
+
+ @Test(expectedExceptions=ObscureException.class)
+ public void testEmptyOrElseThrow() throws Exception {
+ Optional<Boolean> empty = Optional.empty();
+
+ Boolean got = empty.orElseThrow(ObscureException::new);
+ }
+
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyOrElseThrowNoArg() throws Exception {
+ Optional<Boolean> empty = Optional.empty();
+
+ Boolean got = empty.orElseThrow();
+ }
+
+ @Test(groups = "unit")
+ public void testPresent() {
+ Optional<Boolean> empty = Optional.empty();
+ Optional<String> presentEmptyString = Optional.of("");
+ Optional<Boolean> present = Optional.of(Boolean.TRUE);
+
+ // present
+ assertTrue(present.equals(present));
+ assertTrue(present.equals(Optional.of(Boolean.TRUE)));
+ assertTrue(!present.equals(empty));
+ assertTrue(Boolean.TRUE.hashCode() == present.hashCode());
+ assertTrue(!present.toString().isEmpty());
+ assertTrue(!present.toString().equals(presentEmptyString.toString()));
+ assertTrue(-1 != present.toString().indexOf(Boolean.TRUE.toString()));
+ assertSame(Boolean.TRUE, present.get());
+ assertSame(Boolean.TRUE, present.orElseThrow());
+
+ AtomicBoolean presentCheck = new AtomicBoolean();
+ present.ifPresent(v -> presentCheck.set(true));
+ assertTrue(presentCheck.get());
+ presentCheck.set(false);
+ present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
+ assertTrue(presentCheck.get());
+
+ try {
+ present.ifPresent(v -> { throw new ObscureException(); });
+ fail();
+ } catch (ObscureException expected) {
+ } catch (AssertionError e) {
+ throw e;
+ } catch (Throwable t) {
+ fail();
+ }
+ try {
+ present.ifPresentOrElse(v -> { throw new ObscureException(); }, () -> fail());
+ fail();
+ } catch (ObscureException expected) {
+ } catch (AssertionError e) {
+ throw e;
+ } catch (Throwable t) {
+ fail();
+ }
+
+ assertSame(Boolean.TRUE, present.orElse(null));
+ assertSame(Boolean.TRUE, present.orElse(Boolean.FALSE));
+ assertSame(Boolean.TRUE, present.orElseGet(null));
+ assertSame(Boolean.TRUE, present.orElseGet(() -> null));
+ assertSame(Boolean.TRUE, present.orElseGet(() -> Boolean.FALSE));
+ assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(null));
+ assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(ObscureException::new));
}
@Test(groups = "unit")
- public void testOfNull() {
- assertThrows(NullPointerException.class, () -> Optional.of(null));
- }
+ public void testOfNullable() {
+ Optional<String> instance = Optional.ofNullable(null);
+ assertFalse(instance.isPresent());
- @Test(groups = "unit")
- public void testOfPresent() {
- checkPresent(Optional.of("xyzzy"), "xyzzy");
- }
-
- @Test(groups = "unit")
- public void testOfNullableNull() {
- checkEmpty(Optional.ofNullable(null));
+ instance = Optional.ofNullable("Duke");
+ assertTrue(instance.isPresent());
+ assertEquals(instance.get(), "Duke");
+ assertEquals(instance.orElseThrow(), "Duke");
}
@Test(groups = "unit")
- public void testOfNullablePresent() {
- checkPresent(Optional.of("xyzzy"), "xyzzy");
- }
+ public void testFilter() {
+ // Null mapper function
+ Optional<String> empty = Optional.empty();
+ Optional<String> duke = Optional.of("Duke");
+
+ try {
+ Optional<String> result = empty.filter(null);
+ fail("Should throw NPE on null mapping function");
+ } catch (NullPointerException npe) {
+ // expected
+ }
- @Test(groups = "unit")
- public void testFilterEmpty() {
- checkEmpty(Optional.<String>empty().filter(s -> { fail(); return true; }));
+ Optional<String> result = empty.filter(String::isEmpty);
+ assertFalse(result.isPresent());
+
+ result = duke.filter(String::isEmpty);
+ assertFalse(result.isPresent());
+ result = duke.filter(s -> s.startsWith("D"));
+ assertTrue(result.isPresent());
+ assertEquals(result.get(), "Duke");
+ assertEquals(result.orElseThrow(), "Duke");
+
+ Optional<String> emptyString = Optional.of("");
+ result = emptyString.filter(String::isEmpty);
+ assertTrue(result.isPresent());
+ assertEquals(result.get(), "");
+ assertEquals(result.orElseThrow(), "");
}
@Test(groups = "unit")
- public void testFilterFalse() {
- checkEmpty(Optional.of("xyzzy").filter(s -> s.equals("plugh")));
- }
+ public void testMap() {
+ Optional<String> empty = Optional.empty();
+ Optional<String> duke = Optional.of("Duke");
+
+ // Null mapper function
+ try {
+ Optional<Boolean> b = empty.map(null);
+ fail("Should throw NPE on null mapping function");
+ } catch (NullPointerException npe) {
+ // expected
+ }
- @Test(groups = "unit")
- public void testFilterTrue() {
- checkPresent(Optional.of("xyzzy").filter(s -> s.equals("xyzzy")), "xyzzy");
- }
+ // Map an empty value
+ Optional<Boolean> b = empty.map(String::isEmpty);
+ assertFalse(b.isPresent());
- @Test(groups = "unit")
- public void testMapEmpty() {
- checkEmpty(Optional.empty().map(s -> { fail(); return ""; }));
+ // Map into null
+ b = empty.map(n -> null);
+ assertFalse(b.isPresent());
+ b = duke.map(s -> null);
+ assertFalse(b.isPresent());
+
+ // Map to value
+ Optional<Integer> l = duke.map(String::length);
+ assertEquals(l.get().intValue(), 4);
}
@Test(groups = "unit")
- public void testMapPresent() {
- checkPresent(Optional.of("xyzzy").map(s -> s.replace("xyzzy", "plugh")), "plugh");
- }
+ public void testFlatMap() {
+ Optional<String> empty = Optional.empty();
+ Optional<String> duke = Optional.of("Duke");
- @Test(groups = "unit")
- public void testFlatMapEmpty() {
- checkEmpty(Optional.empty().flatMap(s -> { fail(); return Optional.of(""); }));
- }
+ // Null mapper function
+ try {
+ Optional<Boolean> b = empty.flatMap(null);
+ fail("Should throw NPE on null mapping function");
+ } catch (NullPointerException npe) {
+ // expected
+ }
+
+ // Map into null
+ try {
+ Optional<Boolean> b = duke.flatMap(s -> null);
+ fail("Should throw NPE when mapper return null");
+ } catch (NullPointerException npe) {
+ // expected
+ }
- @Test(groups = "unit")
- public void testFlatMapPresentReturnEmpty() {
- checkEmpty(Optional.of("xyzzy")
- .flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.empty(); }));
- }
+ // Empty won't invoke mapper function
+ try {
+ Optional<Boolean> b = empty.flatMap(s -> null);
+ assertFalse(b.isPresent());
+ } catch (NullPointerException npe) {
+ fail("Mapper function should not be invoked");
+ }
- @Test(groups = "unit")
- public void testFlatMapPresentReturnPresent() {
- checkPresent(Optional.of("xyzzy")
- .flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.of("plugh"); }),
- "plugh");
+ // Map an empty value
+ Optional<Integer> l = empty.flatMap(s -> Optional.of(s.length()));
+ assertFalse(l.isPresent());
+
+ // Map to value
+ Optional<Integer> fixture = Optional.of(Integer.MAX_VALUE);
+ l = duke.flatMap(s -> Optional.of(s.length()));
+ assertTrue(l.isPresent());
+ assertEquals(l.get().intValue(), 4);
+ assertEquals(l.orElseThrow().intValue(), 4);
+
+ // Verify same instance
+ l = duke.flatMap(s -> fixture);
+ assertSame(l, fixture);
}
@Test(groups = "unit")
- public void testOrEmptyEmpty() {
- checkEmpty(Optional.<String>empty().or(() -> Optional.empty()));
- }
+ public void testOr() {
+ Optional<String> empty = Optional.empty();
+ Optional<String> duke = Optional.of("Duke");
+
+ // Null supplier
+ try {
+ Optional<String> b = empty.or(null);
+ fail("Should throw NPE on null supplier");
+ } catch (NullPointerException npe) {
+ // expected
+ }
- @Test(groups = "unit")
- public void testOrEmptyPresent() {
- checkPresent(Optional.<String>empty().or(() -> Optional.of("plugh")), "plugh");
+ // Supply null
+ try {
+ Optional<String> b = empty.or(() -> null);
+ fail("Should throw NPE when supplier returns null");
+ } catch (NullPointerException npe) {
+ // expected
+ }
+
+ // Non-empty won't invoke supplier
+ try {
+ Optional<String> b = duke.or(() -> null);
+ assertTrue(b.isPresent());
+ } catch (NullPointerException npe) {
+ fail("Supplier should not be invoked");
+ }
+
+ // Supply for empty
+ Optional<String> suppliedDuke = empty.or(() -> duke);
+ assertTrue(suppliedDuke.isPresent());
+ assertSame(suppliedDuke, duke);
+
+ // Supply for non-empty
+ Optional<String> actualDuke = duke.or(() -> Optional.of("Other Duke"));
+ assertTrue(actualDuke.isPresent());
+ assertSame(actualDuke, duke);
}
@Test(groups = "unit")
- public void testOrPresentDontCare() {
- checkPresent(Optional.of("xyzzy").or(() -> { fail(); return Optional.of("plugh"); }), "xyzzy");
+ public void testStream() {
+ {
+ Stream<String> s = Optional.<String>empty().stream();
+ assertFalse(s.isParallel());
+
+ Object[] es = s.toArray();
+ assertEquals(es.length, 0);
+ }
+
+ {
+ Stream<String> s = Optional.of("Duke").stream();
+ assertFalse(s.isParallel());
+
+ String[] es = s.toArray(String[]::new);
+ assertEquals(es.length, 1);
+ assertEquals(es[0], "Duke");
+ }
}
- @Test(groups = "unit")
- public void testStreamEmpty() {
- assertEquals(Optional.empty().stream().collect(toList()), List.of());
- }
+ private static class ObscureException extends RuntimeException {
- @Test(groups = "unit")
- public void testStreamPresent() {
- assertEquals(Optional.of("xyzzy").stream().collect(toList()), List.of("xyzzy"));
}
}
--- a/test/jdk/java/util/Optional/BasicDouble.java Thu Mar 29 16:12:41 2018 -0700
+++ b/test/jdk/java/util/Optional/BasicDouble.java Mon Apr 02 16:11:53 2018 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -22,108 +22,187 @@
*/
/* @test
- * @bug 8195649
* @summary Basic functional test of OptionalDouble
* @author Mike Duigou
- * @build ObscureException
* @run testng BasicDouble
*/
import java.util.NoSuchElementException;
import java.util.OptionalDouble;
import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.DoubleStream;
import static org.testng.Assert.*;
import org.testng.annotations.Test;
+
public class BasicDouble {
- static final double DOUBLEVAL = Math.PI;
- static final double UNEXPECTED = 6.62607004E-34;
- /**
- * Checks a block of assertions over an empty OptionalDouble.
- */
- void checkEmpty(OptionalDouble empty) {
- assertTrue(empty.equals(OptionalDouble.empty()));
- assertTrue(OptionalDouble.empty().equals(empty));
- assertFalse(empty.equals(OptionalDouble.of(UNEXPECTED)));
- assertFalse(OptionalDouble.of(UNEXPECTED).equals(empty));
- assertFalse(empty.equals("unexpected"));
+ @Test(groups = "unit")
+ public void testEmpty() {
+ OptionalDouble empty = OptionalDouble.empty();
+ OptionalDouble present = OptionalDouble.of(1.0);
- assertFalse(empty.isPresent());
- assertEquals(empty.hashCode(), 0);
- assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
- assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
-
- assertThrows(NoSuchElementException.class, () -> empty.getAsDouble());
- assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
- assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new));
-
- var b = new AtomicBoolean();
- empty.ifPresent(s -> b.set(true));
- assertFalse(b.get());
-
- var b1 = new AtomicBoolean(false);
- var b2 = new AtomicBoolean(false);
- empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
- assertFalse(b1.get());
- assertTrue(b2.get());
+ // empty
+ assertTrue(empty.equals(empty));
+ assertTrue(empty.equals(OptionalDouble.empty()));
+ assertTrue(!empty.equals(present));
+ assertTrue(0 == empty.hashCode());
+ assertTrue(!empty.toString().isEmpty());
+ assertTrue(!empty.isPresent());
- assertEquals(empty.toString(), "OptionalDouble.empty");
- }
+ empty.ifPresent(v -> { fail(); });
- /**
- * Checks a block of assertions over an OptionalDouble that is expected to
- * have a particular value present.
- */
- void checkPresent(OptionalDouble opt, double expected) {
- assertFalse(opt.equals(OptionalDouble.empty()));
- assertFalse(OptionalDouble.empty().equals(opt));
- assertTrue(opt.equals(OptionalDouble.of(expected)));
- assertTrue(OptionalDouble.of(expected).equals(opt));
- assertFalse(opt.equals(OptionalDouble.of(UNEXPECTED)));
- assertFalse(OptionalDouble.of(UNEXPECTED).equals(opt));
- assertFalse(opt.equals("unexpected"));
+ AtomicBoolean emptyCheck = new AtomicBoolean();
+ empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
+ assertTrue(emptyCheck.get());
- assertTrue(opt.isPresent());
- assertEquals(opt.hashCode(), Double.hashCode(expected));
- assertEquals(opt.orElse(UNEXPECTED), expected);
- assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);
-
- assertEquals(opt.getAsDouble(), expected);
- assertEquals(opt.orElseThrow(), expected);
- assertEquals(opt.orElseThrow(ObscureException::new), expected);
+ try {
+ empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
+ fail();
+ } catch (ObscureException expected) {
+ } catch (AssertionError e) {
+ throw e;
+ } catch (Throwable t) {
+ fail();
+ }
- var b = new AtomicBoolean(false);
- opt.ifPresent(s -> b.set(true));
- assertTrue(b.get());
-
- var b1 = new AtomicBoolean(false);
- var b2 = new AtomicBoolean(false);
- opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
- assertTrue(b1.get());
- assertFalse(b2.get());
-
- assertEquals(opt.toString(), "OptionalDouble[" + expected + "]");
+ assertEquals(2.0, empty.orElse(2.0));
+ assertEquals(2.0, empty.orElseGet(()-> 2.0));
}
@Test(groups = "unit")
- public void testEmpty() {
- checkEmpty(OptionalDouble.empty());
+ public void testIfPresentAndOrElseAndNull() {
+ OptionalDouble empty = OptionalDouble.empty();
+ OptionalDouble present = OptionalDouble.of(1.0);
+
+ // No NPE
+ present.ifPresentOrElse(v -> {}, null);
+ empty.ifPresent(null);
+ empty.ifPresentOrElse(null, () -> {});
+
+ // NPE
+ try {
+ present.ifPresent(null);
+ fail();
+ } catch (NullPointerException ex) {}
+ try {
+ present.ifPresentOrElse(null, () -> {});
+ fail();
+ } catch (NullPointerException ex) {}
+ try {
+ empty.ifPresentOrElse(v -> {}, null);
+ fail();
+ } catch (NullPointerException ex) {}
+ }
+
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyGet() {
+ OptionalDouble empty = OptionalDouble.empty();
+
+ double got = empty.getAsDouble();
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseGetNull() {
+ OptionalDouble empty = OptionalDouble.empty();
+
+ double got = empty.orElseGet(null);
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseThrowNull() throws Throwable {
+ OptionalDouble empty = OptionalDouble.empty();
+
+ double got = empty.orElseThrow(null);
+ }
+
+ @Test(expectedExceptions=ObscureException.class)
+ public void testEmptyOrElseThrow() throws Exception {
+ OptionalDouble empty = OptionalDouble.empty();
+
+ double got = empty.orElseThrow(ObscureException::new);
+ }
+
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyOrElseThrowNoArg() throws Exception {
+ OptionalDouble empty = OptionalDouble.empty();
+
+ double got = empty.orElseThrow();
}
@Test(groups = "unit")
public void testPresent() {
- checkPresent(OptionalDouble.of(DOUBLEVAL), DOUBLEVAL);
+ OptionalDouble empty = OptionalDouble.empty();
+ OptionalDouble present = OptionalDouble.of(1.0);
+
+ // present
+ assertTrue(present.equals(present));
+ assertFalse(present.equals(OptionalDouble.of(0.0)));
+ assertTrue(present.equals(OptionalDouble.of(1.0)));
+ assertTrue(!present.equals(empty));
+ assertTrue(Double.hashCode(1.0) == present.hashCode());
+ assertFalse(present.toString().isEmpty());
+ assertTrue(-1 != present.toString().indexOf(Double.toString(present.getAsDouble()).toString()));
+ assertTrue(-1 != present.toString().indexOf(Double.toString(present.orElseThrow()).toString()));
+ assertEquals(1.0, present.getAsDouble());
+ assertEquals(1.0, present.orElseThrow());
+
+ AtomicBoolean presentCheck = new AtomicBoolean();
+ present.ifPresent(v -> presentCheck.set(true));
+ assertTrue(presentCheck.get());
+ presentCheck.set(false);
+ present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
+ assertTrue(presentCheck.get());
+
+ try {
+ present.ifPresent(v -> { throw new ObscureException(); });
+ fail();
+ } catch (ObscureException expected) {
+ } catch (AssertionError e) {
+ throw e;
+ } catch (Throwable t) {
+ fail();
+ }
+ try {
+ present.ifPresentOrElse(v -> { throw new ObscureException(); }, () -> fail());
+ fail();
+ } catch (ObscureException expected) {
+ } catch (AssertionError e) {
+ throw e;
+ } catch (Throwable t) {
+ fail();
+ }
+
+ assertEquals(1.0, present.orElse(2.0));
+ assertEquals(1.0, present.orElseGet(null));
+ assertEquals(1.0, present.orElseGet(()-> 2.0));
+ assertEquals(1.0, present.orElseGet(()-> 3.0));
+ assertEquals(1.0, present.<RuntimeException>orElseThrow(null));
+ assertEquals(1.0, present.<RuntimeException>orElseThrow(ObscureException::new));
}
@Test(groups = "unit")
- public void testStreamEmpty() {
- assertEquals(OptionalDouble.empty().stream().toArray(), new double[] { });
+ public void testStream() {
+ {
+ DoubleStream s = OptionalDouble.empty().stream();
+ assertFalse(s.isParallel());
+
+ double[] es = s.toArray();
+ assertEquals(es.length, 0);
+ }
+
+ {
+ DoubleStream s = OptionalDouble.of(42.0).stream();
+ assertFalse(s.isParallel());
+
+ double[] es = s.toArray();
+ assertEquals(es.length, 1);
+ assertEquals(es[0], 42.0);
+ }
}
- @Test(groups = "unit")
- public void testStreamPresent() {
- assertEquals(OptionalDouble.of(DOUBLEVAL).stream().toArray(), new double[] { DOUBLEVAL });
+ private static class ObscureException extends RuntimeException {
+
}
}
--- a/test/jdk/java/util/Optional/BasicInt.java Thu Mar 29 16:12:41 2018 -0700
+++ b/test/jdk/java/util/Optional/BasicInt.java Mon Apr 02 16:11:53 2018 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -22,10 +22,8 @@
*/
/* @test
- * @bug 8195649
* @summary Basic functional test of OptionalInt
* @author Mike Duigou
- * @build ObscureException
* @run testng BasicInt
*/
@@ -37,95 +35,174 @@
import static org.testng.Assert.*;
import org.testng.annotations.Test;
+
public class BasicInt {
- static final int INTVAL = 33_550_336;
- static final int UNEXPECTED = 0xCAFEBABE;
-
- /**
- * Checks a block of assertions over an empty OptionalInt.
- */
- void checkEmpty(OptionalInt empty) {
- assertTrue(empty.equals(OptionalInt.empty()));
- assertTrue(OptionalInt.empty().equals(empty));
- assertFalse(empty.equals(OptionalInt.of(UNEXPECTED)));
- assertFalse(OptionalInt.of(UNEXPECTED).equals(empty));
- assertFalse(empty.equals("unexpected"));
+ @Test(groups = "unit")
+ public void testEmpty() {
+ OptionalInt empty = OptionalInt.empty();
+ OptionalInt present = OptionalInt.of(1);
- assertFalse(empty.isPresent());
- assertEquals(empty.hashCode(), 0);
- assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
- assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
-
- assertThrows(NoSuchElementException.class, () -> empty.getAsInt());
- assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
- assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new));
+ // empty
+ assertTrue(empty.equals(empty));
+ assertTrue(empty.equals(OptionalInt.empty()));
+ assertTrue(!empty.equals(present));
+ assertTrue(0 == empty.hashCode());
+ assertTrue(!empty.toString().isEmpty());
+ assertTrue(!empty.isPresent());
- var b = new AtomicBoolean();
- empty.ifPresent(s -> b.set(true));
- assertFalse(b.get());
-
- var b1 = new AtomicBoolean(false);
- var b2 = new AtomicBoolean(false);
- empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
- assertFalse(b1.get());
- assertTrue(b2.get());
+ empty.ifPresent(v -> { fail(); });
- assertEquals(empty.toString(), "OptionalInt.empty");
- }
-
- /**
- * Checks a block of assertions over an OptionalInt that is expected to
- * have a particular value present.
- */
- void checkPresent(OptionalInt opt, int expected) {
- assertFalse(opt.equals(OptionalInt.empty()));
- assertFalse(OptionalInt.empty().equals(opt));
- assertTrue(opt.equals(OptionalInt.of(expected)));
- assertTrue(OptionalInt.of(expected).equals(opt));
- assertFalse(opt.equals(OptionalInt.of(UNEXPECTED)));
- assertFalse(OptionalInt.of(UNEXPECTED).equals(opt));
- assertFalse(opt.equals("unexpected"));
+ AtomicBoolean emptyCheck = new AtomicBoolean();
+ empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
+ assertTrue(emptyCheck.get());
- assertTrue(opt.isPresent());
- assertEquals(opt.hashCode(), Integer.hashCode(expected));
- assertEquals(opt.orElse(UNEXPECTED), expected);
- assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);
-
- assertEquals(opt.getAsInt(), expected);
- assertEquals(opt.orElseThrow(), expected);
- assertEquals(opt.orElseThrow(ObscureException::new), expected);
+ try {
+ empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
+ fail();
+ } catch (ObscureException expected) {
+ } catch (AssertionError e) {
+ throw e;
+ } catch (Throwable t) {
+ fail();
+ }
- var b = new AtomicBoolean(false);
- opt.ifPresent(s -> b.set(true));
- assertTrue(b.get());
-
- var b1 = new AtomicBoolean(false);
- var b2 = new AtomicBoolean(false);
- opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
- assertTrue(b1.get());
- assertFalse(b2.get());
-
- assertEquals(opt.toString(), "OptionalInt[" + expected + "]");
+ assertEquals(2, empty.orElse(2));
+ assertEquals(2, empty.orElseGet(()-> 2));
}
@Test(groups = "unit")
- public void testEmpty() {
- checkEmpty(OptionalInt.empty());
+ public void testIfPresentAndOrElseAndNull() {
+ OptionalInt empty = OptionalInt.empty();
+ OptionalInt present = OptionalInt.of(1);
+
+ // No NPE
+ present.ifPresentOrElse(v -> {}, null);
+ empty.ifPresent(null);
+ empty.ifPresentOrElse(null, () -> {});
+
+ // NPE
+ try {
+ present.ifPresent(null);
+ fail();
+ } catch (NullPointerException ex) {}
+ try {
+ present.ifPresentOrElse(null, () -> {});
+ fail();
+ } catch (NullPointerException ex) {}
+ try {
+ empty.ifPresentOrElse(v -> {}, null);
+ fail();
+ } catch (NullPointerException ex) {}
+ }
+
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyGet() {
+ OptionalInt empty = OptionalInt.empty();
+
+ int got = empty.getAsInt();
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseGetNull() {
+ OptionalInt empty = OptionalInt.empty();
+
+ int got = empty.orElseGet(null);
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseThrowNull() throws Throwable {
+ OptionalInt empty = OptionalInt.empty();
+
+ int got = empty.orElseThrow(null);
+ }
+
+ @Test(expectedExceptions=ObscureException.class)
+ public void testEmptyOrElseThrow() throws Exception {
+ OptionalInt empty = OptionalInt.empty();
+
+ int got = empty.orElseThrow(ObscureException::new);
+ }
+
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyOrElseThrowNoArg() throws Exception {
+ OptionalInt empty = OptionalInt.empty();
+
+ int got = empty.orElseThrow();
}
@Test(groups = "unit")
public void testPresent() {
- checkPresent(OptionalInt.of(INTVAL), INTVAL);
+ OptionalInt empty = OptionalInt.empty();
+ OptionalInt present = OptionalInt.of(1);
+
+ // present
+ assertTrue(present.equals(present));
+ assertFalse(present.equals(OptionalInt.of(0)));
+ assertTrue(present.equals(OptionalInt.of(1)));
+ assertFalse(present.equals(empty));
+ assertTrue(Integer.hashCode(1) == present.hashCode());
+ assertFalse(present.toString().isEmpty());
+ assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString()));
+ assertTrue(-1 != present.toString().indexOf(Integer.toString(present.orElseThrow()).toString()));
+ assertEquals(1, present.getAsInt());
+ assertEquals(1, present.orElseThrow());
+
+ AtomicBoolean presentCheck = new AtomicBoolean();
+ present.ifPresent(v -> presentCheck.set(true));
+ assertTrue(presentCheck.get());
+ presentCheck.set(false);
+ present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
+ assertTrue(presentCheck.get());
+
+ try {
+ present.ifPresent(v -> { throw new ObscureException(); });
+ fail();
+ } catch (ObscureException expected) {
+ } catch (AssertionError e) {
+ throw e;
+ } catch (Throwable t) {
+ fail();
+ }
+ try {
+ present.ifPresentOrElse(v -> { throw new ObscureException(); }, () -> fail());
+ fail();
+ } catch (ObscureException expected) {
+ } catch (AssertionError e) {
+ throw e;
+ } catch (Throwable t) {
+ fail();
+ }
+
+ assertEquals(1, present.orElse(2));
+ assertEquals(1, present.orElseGet(null));
+ assertEquals(1, present.orElseGet(()-> 2));
+ assertEquals(1, present.orElseGet(()-> 3));
+ assertEquals(1, present.<RuntimeException>orElseThrow(null));
+ assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
}
@Test(groups = "unit")
- public void testStreamEmpty() {
- assertEquals(OptionalInt.empty().stream().toArray(), new int[] { });
+ public void testStream() {
+ {
+ IntStream s = OptionalInt.empty().stream();
+ assertFalse(s.isParallel());
+
+ int[] es = s.toArray();
+ assertEquals(es.length, 0);
+ }
+
+ {
+ IntStream s = OptionalInt.of(42).stream();
+ assertFalse(s.isParallel());
+
+ int[] es = OptionalInt.of(42).stream().toArray();
+ assertEquals(es.length, 1);
+ assertEquals(es[0], 42);
+ }
}
- @Test(groups = "unit")
- public void testStreamPresent() {
- assertEquals(OptionalInt.of(INTVAL).stream().toArray(), new int[] { INTVAL });
+ private static class ObscureException extends RuntimeException {
+
}
}
--- a/test/jdk/java/util/Optional/BasicLong.java Thu Mar 29 16:12:41 2018 -0700
+++ b/test/jdk/java/util/Optional/BasicLong.java Mon Apr 02 16:11:53 2018 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -22,10 +22,8 @@
*/
/* @test
- * @bug 8195649
* @summary Basic functional test of OptionalLong
* @author Mike Duigou
- * @build ObscureException
* @run testng BasicLong
*/
@@ -37,94 +35,174 @@
import static org.testng.Assert.*;
import org.testng.annotations.Test;
+
public class BasicLong {
- static final long LONGVAL = 2_305_843_008_139_952_128L;
- static final long UNEXPECTED = 0xFEEDBEEFCAFEBABEL;
- /**
- * Checks a block of assertions over an empty OptionalLong.
- */
- void checkEmpty(OptionalLong empty) {
- assertTrue(empty.equals(OptionalLong.empty()));
- assertTrue(OptionalLong.empty().equals(empty));
- assertFalse(empty.equals(OptionalLong.of(UNEXPECTED)));
- assertFalse(OptionalLong.of(UNEXPECTED).equals(empty));
- assertFalse(empty.equals("unexpected"));
+ @Test(groups = "unit")
+ public void testEmpty() {
+ OptionalLong empty = OptionalLong.empty();
+ OptionalLong present = OptionalLong.of(1);
- assertFalse(empty.isPresent());
- assertEquals(empty.hashCode(), 0);
- assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
- assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
-
- assertThrows(NoSuchElementException.class, () -> empty.getAsLong());
- assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
- assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new));
-
- var b = new AtomicBoolean();
- empty.ifPresent(s -> b.set(true));
- assertFalse(b.get());
-
- var b1 = new AtomicBoolean(false);
- var b2 = new AtomicBoolean(false);
- empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
- assertFalse(b1.get());
- assertTrue(b2.get());
+ // empty
+ assertTrue(empty.equals(empty));
+ assertTrue(empty.equals(OptionalLong.empty()));
+ assertTrue(!empty.equals(present));
+ assertTrue(0 == empty.hashCode());
+ assertTrue(!empty.toString().isEmpty());
+ assertTrue(!empty.isPresent());
- assertEquals(empty.toString(), "OptionalLong.empty");
- }
+ empty.ifPresent(v -> { fail(); });
- /**
- * Checks a block of assertions over an OptionalLong that is expected to
- * have a particular value present.
- */
- void checkPresent(OptionalLong opt, long expected) {
- assertFalse(opt.equals(OptionalLong.empty()));
- assertFalse(OptionalLong.empty().equals(opt));
- assertTrue(opt.equals(OptionalLong.of(expected)));
- assertTrue(OptionalLong.of(expected).equals(opt));
- assertFalse(opt.equals(OptionalLong.of(UNEXPECTED)));
- assertFalse(OptionalLong.of(UNEXPECTED).equals(opt));
- assertFalse(opt.equals("unexpected"));
+ AtomicBoolean emptyCheck = new AtomicBoolean();
+ empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
+ assertTrue(emptyCheck.get());
- assertTrue(opt.isPresent());
- assertEquals(opt.hashCode(), Long.hashCode(expected));
- assertEquals(opt.orElse(UNEXPECTED), expected);
- assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);
-
- assertEquals(opt.getAsLong(), expected);
- assertEquals(opt.orElseThrow(), expected);
- assertEquals(opt.orElseThrow(ObscureException::new), expected);
+ try {
+ empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
+ fail();
+ } catch (ObscureException expected) {
+ } catch (AssertionError e) {
+ throw e;
+ } catch (Throwable t) {
+ fail();
+ }
- var b = new AtomicBoolean(false);
- opt.ifPresent(s -> b.set(true));
- assertTrue(b.get());
-
- var b1 = new AtomicBoolean(false);
- var b2 = new AtomicBoolean(false);
- opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
- assertTrue(b1.get());
- assertFalse(b2.get());
-
- assertEquals(opt.toString(), "OptionalLong[" + expected + "]");
+ assertEquals(2, empty.orElse(2));
+ assertEquals(2, empty.orElseGet(()-> 2));
}
@Test(groups = "unit")
- public void testEmpty() {
- checkEmpty(OptionalLong.empty());
+ public void testIfPresentAndOrElseAndNull() {
+ OptionalLong empty = OptionalLong.empty();
+ OptionalLong present = OptionalLong.of(1);
+
+ // No NPE
+ present.ifPresentOrElse(v -> {}, null);
+ empty.ifPresent(null);
+ empty.ifPresentOrElse(null, () -> {});
+
+ // NPE
+ try {
+ present.ifPresent(null);
+ fail();
+ } catch (NullPointerException ex) {}
+ try {
+ present.ifPresentOrElse(null, () -> {});
+ fail();
+ } catch (NullPointerException ex) {}
+ try {
+ empty.ifPresentOrElse(v -> {}, null);
+ fail();
+ } catch (NullPointerException ex) {}
+ }
+
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyGet() {
+ OptionalLong empty = OptionalLong.empty();
+
+ long got = empty.getAsLong();
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseGetNull() {
+ OptionalLong empty = OptionalLong.empty();
+
+ long got = empty.orElseGet(null);
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseThrowNull() throws Throwable {
+ OptionalLong empty = OptionalLong.empty();
+
+ long got = empty.orElseThrow(null);
+ }
+
+ @Test(expectedExceptions=ObscureException.class)
+ public void testEmptyOrElseThrow() throws Exception {
+ OptionalLong empty = OptionalLong.empty();
+
+ long got = empty.orElseThrow(ObscureException::new);
+ }
+
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyOrElseThrowNoArg() throws Exception {
+ OptionalLong empty = OptionalLong.empty();
+
+ long got = empty.orElseThrow();
}
@Test(groups = "unit")
public void testPresent() {
- checkPresent(OptionalLong.of(LONGVAL), LONGVAL);
+ OptionalLong empty = OptionalLong.empty();
+ OptionalLong present = OptionalLong.of(1L);
+
+ // present
+ assertTrue(present.equals(present));
+ assertFalse(present.equals(OptionalLong.of(0L)));
+ assertTrue(present.equals(OptionalLong.of(1L)));
+ assertFalse(present.equals(empty));
+ assertTrue(Long.hashCode(1) == present.hashCode());
+ assertFalse(present.toString().isEmpty());
+ assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
+ assertTrue(-1 != present.toString().indexOf(Long.toString(present.orElseThrow()).toString()));
+ assertEquals(1L, present.getAsLong());
+ assertEquals(1L, present.orElseThrow());
+
+ AtomicBoolean presentCheck = new AtomicBoolean();
+ present.ifPresent(v -> presentCheck.set(true));
+ assertTrue(presentCheck.get());
+ presentCheck.set(false);
+ present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
+ assertTrue(presentCheck.get());
+
+ try {
+ present.ifPresent(v -> { throw new ObscureException(); });
+ fail();
+ } catch (ObscureException expected) {
+ } catch (AssertionError e) {
+ throw e;
+ } catch (Throwable t) {
+ fail();
+ }
+ try {
+ present.ifPresentOrElse(v -> {
+ throw new ObscureException();
+ }, () -> fail());
+ fail();
+ } catch (ObscureException expected) {
+ } catch (AssertionError e) {
+ throw e;
+ } catch (Throwable t) {
+ fail();
+ }
+
+ assertEquals(1, present.orElse(2));
+ assertEquals(1, present.orElseGet(null));
+ assertEquals(1, present.orElseGet(()-> 2));
+ assertEquals(1, present.orElseGet(()-> 3));
+ assertEquals(1, present.<RuntimeException>orElseThrow(null));
+ assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
}
@Test(groups = "unit")
- public void testStreamEmpty() {
- assertEquals(OptionalLong.empty().stream().toArray(), new long[] { });
+ public void testStream() {
+ {
+ LongStream s = OptionalLong.empty().stream();
+
+ long[] es = s.toArray();
+ assertEquals(es.length, 0);
+ }
+
+ {
+ LongStream s = OptionalLong.of(42L).stream();
+
+ long[] es = s.toArray();
+ assertEquals(es.length, 1);
+ assertEquals(es[0], 42L);
+ }
}
- @Test(groups = "unit")
- public void testStreamPresent() {
- assertEquals(OptionalLong.of(LONGVAL).stream().toArray(), new long[] { LONGVAL });
+ private static class ObscureException extends RuntimeException {
+
}
}
--- a/test/jdk/java/util/Optional/ObscureException.java Thu Mar 29 16:12:41 2018 -0700
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-/**
- * A unique exception used for checking exception types.
- */
-public class ObscureException extends RuntimeException { }