Move ObscureException to its own file. Rewrite BasicDouble. JDK-8195649-branch
authorsmarks
Thu, 29 Mar 2018 14:40:53 -0700
branchJDK-8195649-branch
changeset 56375 e0530ee8af48
parent 56364 ed0bfd119607
child 56376 718b9df3e302
Move ObscureException to its own file. Rewrite BasicDouble.
test/jdk/java/util/Optional/Basic.java
test/jdk/java/util/Optional/BasicDouble.java
test/jdk/java/util/Optional/BasicInt.java
test/jdk/java/util/Optional/BasicLong.java
test/jdk/java/util/Optional/ObscureException.java
--- a/test/jdk/java/util/Optional/Basic.java	Wed Mar 28 23:43:54 2018 -0700
+++ b/test/jdk/java/util/Optional/Basic.java	Thu Mar 29 14:40:53 2018 -0700
@@ -24,6 +24,7 @@
 /* @test
  * @summary Basic functional test of Optional
  * @author Mike Duigou
+ * @build ObscureException
  * @run testng Basic
  */
 
@@ -42,21 +43,6 @@
 public class Basic {
 
     /**
-     * Asserts that the runnable throws an exception of the given type.
-     * Fails if no exception is thrown, or if an exception of the wrong
-     * type is thrown. This is used instead of @Test(expectedExceptions)
-     * because that can be applied only at the granularity of a test.
-     */
-    void assertThrows(Runnable r, Class<? extends Exception> clazz) {
-        try {
-            r.run();
-            fail();
-        } catch (Exception ex) {
-            assertTrue(clazz.isInstance(ex));
-        }
-    }
-
-    /**
      * Checks a block of assertions over an empty Optional.
      */
     void checkEmpty(Optional<String> empty) {
@@ -71,9 +57,9 @@
         assertEquals(empty.orElse("x"), "x");
         assertEquals(empty.orElseGet(() -> "y"), "y");
 
-        assertThrows(() -> empty.get(), NoSuchElementException.class);
-        assertThrows(() -> empty.orElseThrow(), NoSuchElementException.class);
-        assertThrows(() -> empty.orElseThrow(ObscureException::new), ObscureException.class);
+        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));
@@ -123,98 +109,96 @@
         assertEquals(opt.toString(), "Optional[" + expected + "]");
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testEmpty() {
         checkEmpty(Optional.empty());
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testOfNull() {
-        assertThrows(() -> Optional.of(null), NullPointerException.class);
+        assertThrows(NullPointerException.class, () -> Optional.of(null));
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testOfPresent() {
         checkPresent(Optional.of("xyzzy"), "xyzzy");
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testOfNullableNull() {
         checkEmpty(Optional.ofNullable(null));
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testOfNullablePresent() {
         checkPresent(Optional.of("xyzzy"), "xyzzy");
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testFilterEmpty() {
         checkEmpty(Optional.<String>empty().filter(s -> { fail(); return true; }));
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testFilterFalse() {
         checkEmpty(Optional.of("xyzzy").filter(s -> s.equals("plugh")));
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testFilterTrue() {
         checkPresent(Optional.of("xyzzy").filter(s -> s.equals("xyzzy")), "xyzzy");
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testMapEmpty() {
         checkEmpty(Optional.empty().map(s -> { fail(); return ""; }));
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testMapPresent() {
         checkPresent(Optional.of("xyzzy").map(s -> s.replace("xyzzy", "plugh")), "plugh");
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testFlatMapEmpty() {
         checkEmpty(Optional.empty().flatMap(s -> { fail(); return Optional.of(""); }));
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testFlatMapPresentReturnEmpty() {
         checkEmpty(Optional.of("xyzzy")
                            .flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.empty(); }));
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testFlatMapPresentReturnPresent() {
         checkPresent(Optional.of("xyzzy")
                              .flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.of("plugh"); }),
                      "plugh");
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testOrEmptyEmpty() {
         checkEmpty(Optional.<String>empty().or(() -> Optional.empty()));
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testOrEmptyPresent() {
         checkPresent(Optional.<String>empty().or(() -> Optional.of("plugh")), "plugh");
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testOrPresentDontCare() {
         checkPresent(Optional.of("xyzzy").or(() -> { fail(); return Optional.of("plugh"); }), "xyzzy");
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testStreamEmpty() {
         assertEquals(Optional.empty().stream().collect(toList()), List.of());
     }
 
-    @Test
+    @Test(groups = "unit")
     public void testStreamPresent() {
         assertEquals(Optional.of("xyzzy").stream().collect(toList()), List.of("xyzzy"));
     }
-
-    private static class ObscureException extends RuntimeException { }
 }
--- a/test/jdk/java/util/Optional/BasicDouble.java	Wed Mar 28 23:43:54 2018 -0700
+++ b/test/jdk/java/util/Optional/BasicDouble.java	Thu Mar 29 14:40:53 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * 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
@@ -24,185 +24,104 @@
 /* @test
  * @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 UNEXPECTED = 6.62607004E-34;
 
-    @Test(groups = "unit")
-    public void testEmpty() {
-        OptionalDouble empty = OptionalDouble.empty();
-        OptionalDouble present = OptionalDouble.of(1.0);
+    /**
+     * 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"));
 
-        // 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());
+        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.ifPresent(v -> { fail(); });
+        assertEquals(empty.toString(), "OptionalDouble.empty");
+    }
 
-        AtomicBoolean emptyCheck = new AtomicBoolean();
-        empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
-        assertTrue(emptyCheck.get());
+    /**
+     * 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"));
 
-        try {
-            empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
-            fail();
-        } catch (ObscureException expected) {
-        } catch (AssertionError e) {
-            throw e;
-        } catch (Throwable t) {
-            fail();
-        }
+        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);
 
-        assertEquals(2.0, empty.orElse(2.0));
-        assertEquals(2.0, empty.orElseGet(()-> 2.0));
+        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 + "]");
     }
 
     @Test(groups = "unit")
-    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) {}
+    public void testEmpty() {
+        checkEmpty(OptionalDouble.empty());
     }
 
-    @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 testOf() {
+        checkPresent(OptionalDouble.of(Math.PI), Math.PI);
     }
 
     @Test(groups = "unit")
-    public void testPresent() {
-        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));
+    public void testStreamEmpty() {
+        assertEquals(OptionalDouble.empty().stream().toArray().length, 0);
     }
 
     @Test(groups = "unit")
-    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);
-        }
-    }
-
-    private static class ObscureException extends RuntimeException {
-
+    public void testStreamPresent() {
+        assertEquals(OptionalDouble.of(Math.PI).stream().toArray(), new double[] { Math.PI });
     }
 }
--- a/test/jdk/java/util/Optional/BasicInt.java	Wed Mar 28 23:43:54 2018 -0700
+++ b/test/jdk/java/util/Optional/BasicInt.java	Thu Mar 29 14:40:53 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * 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
@@ -24,6 +24,7 @@
 /* @test
  * @summary Basic functional test of OptionalInt
  * @author Mike Duigou
+ * @build ObscureException
  * @run testng BasicInt
  */
 
@@ -201,8 +202,4 @@
             assertEquals(es[0], 42);
         }
     }
-
-    private static class ObscureException extends RuntimeException {
-
-    }
 }
--- a/test/jdk/java/util/Optional/BasicLong.java	Wed Mar 28 23:43:54 2018 -0700
+++ b/test/jdk/java/util/Optional/BasicLong.java	Thu Mar 29 14:40:53 2018 -0700
@@ -24,6 +24,7 @@
 /* @test
  * @summary Basic functional test of OptionalLong
  * @author Mike Duigou
+ * @build ObscureException
  * @run testng BasicLong
  */
 
@@ -201,8 +202,4 @@
             assertEquals(es[0], 42L);
         }
     }
-
-    private static class ObscureException extends RuntimeException {
-
-    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/util/Optional/ObscureException.java	Thu Mar 29 14:40:53 2018 -0700
@@ -0,0 +1,27 @@
+/*
+ * 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 { }