changeset 7582:20f7eb03af4a

regression tests for Optional
author mduigou
date Fri, 08 Mar 2013 11:34:25 -0800
parents 7e859eed69bd
children 79a9684a5542
files test/java/util/Optional/BasicDouble.java test/java/util/Optional/BasicInt.java test/java/util/Optional/BasicLong.java
diffstat 3 files changed, 345 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/util/Optional/BasicDouble.java	Fri Mar 08 11:34:25 2013 -0800
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 1997, 2012, 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.0 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.0 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.0 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.
+ */
+
+/* @test
+ * @summary Basic functional test of OptionalDouble
+ * @author Mike Duigou
+ * @run testng Basic
+ */
+
+import java.util.NoSuchElementException;
+import java.util.OptionalDouble;
+
+import static org.testng.Assert.*;
+import org.testng.annotations.Test;
+
+
+public class BasicDouble {
+
+    @Test(groups = "unit")
+    public void testEmpty() {
+        OptionalDouble empty = OptionalDouble.empty();
+        OptionalDouble present = OptionalDouble.of(1.0);
+
+        // 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());
+        empty.ifPresent(v -> { fail(); });
+        assertSame(2.0, empty.orElse(2.0));
+        assertSame(2.0, empty.orElseGet(()-> 2.0));
+    }
+
+        @Test(expectedExceptions=NoSuchElementException.class)
+        public void testEmptyGet() {
+            OptionalDouble<Boolean> empty = OptionalDouble.empty();
+
+            Boolean got = empty.get();
+        }
+
+        @Test(expectedExceptions=NullPointerException.class)
+        public void testEmptyOrElseGetNull() {
+            OptionalDouble empty = OptionalDouble.empty();
+
+            Boolean got = empty.orElseGet(null);
+        }
+
+        @Test(expectedExceptions=NullPointerException.class)
+        public void testEmptyOrElseThrowNull() throws Throwable {
+            OptionalDouble empty = OptionalDouble.empty();
+
+            Boolean got = empty.orElseThrow(null);
+        }
+
+        @Test(expectedExceptions=ObscureException.class)
+        public void testEmptyOrElseThrow() throws Exception {
+            OptionalDouble empty = OptionalDouble.empty();
+
+            Boolean got = empty.orElseThrow(ObscureException::new);
+        }
+
+        @Test(groups = "unit")
+        public void testPresent() {
+        OptionalDouble empty = OptionalDouble.empty();
+        OptionalDouble present = OptionalDouble.of(1.0);
+
+        // present
+        assertTrue(present.equals(present));
+        assertTrue(present.equals(OptionalDouble.of(0)));
+        assertTrue(!present.equals(empty));
+        assertTrue(Double.hashCode(1.0) == present.hashCode());
+        assertTrue(!present.toString().isEmpty());
+        assertTrue(!present.toString().equals(presentEmptyString.toString()));
+        assertTrue(-1 != present.toString().indexOf(Double.toString(1.0).toString()));
+        assertSame(1.0, present.get());
+        try {
+            present.ifPresent(v -> { throw new ObscureException(); });
+            fail();
+        } catch(ObscureException expected) {
+
+        }
+        assertSame(1.0, present.orElse(2.0));
+        assertSame(1.0, present.orElseGet(null));
+        assertSame(1.0, present.orElseGet(()-> 2.0));
+        assertSame(1.0, present.orElseGet(()-> 3.0));
+        assertSame(1.0, present.<RuntimeException>orElseThrow(null));
+        assertSame(1.0, present.<RuntimeException>orElseThrow(ObscureException::new));
+    }
+
+    private static class ObscureException extends RuntimeException {
+
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/util/Optional/BasicInt.java	Fri Mar 08 11:34:25 2013 -0800
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 1997, 2012, 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.
+ */
+
+/* @test
+ * @summary Basic functional test of OptionalInt
+ * @author Mike Duigou
+ * @run testng Basic
+ */
+
+import java.util.NoSuchElementException;
+import java.util.OptionalInt;
+
+import static org.testng.Assert.*;
+import org.testng.annotations.Test;
+
+
+public class BasicInt {
+
+    @Test(groups = "unit")
+    public void testEmpty() {
+        OptionalInt empty = OptionalInt.empty();
+        OptionalInt present = OptionalInt.of(1);
+
+        // 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());
+        empty.ifPresent(v -> { fail(); });
+        assertSame(2, empty.orElse(2));
+        assertSame(2, empty.orElseGet(()-> 2));
+    }
+
+        @Test(expectedExceptions=NoSuchElementException.class)
+        public void testEmptyGet() {
+            OptionalInt<Boolean> empty = OptionalInt.empty();
+
+            Boolean got = empty.get();
+        }
+
+        @Test(expectedExceptions=NullPointerException.class)
+        public void testEmptyOrElseGetNull() {
+            OptionalInt empty = OptionalInt.empty();
+
+            Boolean got = empty.orElseGet(null);
+        }
+
+        @Test(expectedExceptions=NullPointerException.class)
+        public void testEmptyOrElseThrowNull() throws Throwable {
+            OptionalInt empty = OptionalInt.empty();
+
+            Boolean got = empty.orElseThrow(null);
+        }
+
+        @Test(expectedExceptions=ObscureException.class)
+        public void testEmptyOrElseThrow() throws Exception {
+            OptionalInt empty = OptionalInt.empty();
+
+            Boolean got = empty.orElseThrow(ObscureException::new);
+        }
+
+        @Test(groups = "unit")
+        public void testPresent() {
+        OptionalInt empty = OptionalInt.empty();
+        OptionalInt present = OptionalInt.of(1);
+
+        // present
+        assertTrue(present.equals(present));
+        assertTrue(present.equals(OptionalInt.of(0)));
+        assertTrue(!present.equals(empty));
+        assertTrue(Integer.hashCode(1) == present.hashCode());
+        assertTrue(!present.toString().isEmpty());
+        assertTrue(!present.toString().equals(presentEmptyString.toString()));
+        assertTrue(-1 != present.toString().indexOf(Integer.toString(1).toString()));
+        assertSame(1, present.get());
+        try {
+            present.ifPresent(v -> { throw new ObscureException(); });
+            fail();
+        } catch(ObscureException expected) {
+
+        }
+        assertSame(1, present.orElse(2));
+        assertSame(1, present.orElseGet(null));
+        assertSame(1, present.orElseGet(()-> 2));
+        assertSame(1, present.orElseGet(()-> 3));
+        assertSame(1, present.<RuntimeException>orElseThrow(null));
+        assertSame(1, present.<RuntimeException>orElseThrow(ObscureException::new));
+    }
+
+    private static class ObscureException extends RuntimeException {
+
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/util/Optional/BasicLong.java	Fri Mar 08 11:34:25 2013 -0800
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 1997, 2012, 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.
+ */
+
+/* @test
+ * @summary Basic functional test of OptionalLong
+ * @author Mike Duigou
+ * @run testng Basic
+ */
+
+import java.util.NoSuchElementException;
+import java.util.OptionalLong;
+
+import static org.testng.Assert.*;
+import org.testng.annotations.Test;
+
+
+public class BasicLong {
+
+    @Test(groups = "unit")
+    public void testEmpty() {
+        OptionalLong empty = OptionalLong.empty();
+        OptionalLong present = OptionalLong.of(1);
+
+        // 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());
+        empty.ifPresent(v -> { fail(); });
+        assertSame(2, empty.orElse(2));
+        assertSame(2, empty.orElseGet(()-> 2));
+    }
+
+        @Test(expectedExceptions=NoSuchElementException.class)
+        public void testEmptyGet() {
+            OptionalLong<Boolean> empty = OptionalLong.empty();
+
+            Boolean got = empty.get();
+        }
+
+        @Test(expectedExceptions=NullPointerException.class)
+        public void testEmptyOrElseGetNull() {
+            OptionalLong empty = OptionalLong.empty();
+
+            Boolean got = empty.orElseGet(null);
+        }
+
+        @Test(expectedExceptions=NullPointerException.class)
+        public void testEmptyOrElseThrowNull() throws Throwable {
+            OptionalLong empty = OptionalLong.empty();
+
+            Boolean got = empty.orElseThrow(null);
+        }
+
+        @Test(expectedExceptions=ObscureException.class)
+        public void testEmptyOrElseThrow() throws Exception {
+            OptionalLong empty = OptionalLong.empty();
+
+            Boolean got = empty.orElseThrow(ObscureException::new);
+        }
+
+        @Test(groups = "unit")
+        public void testPresent() {
+        OptionalLong empty = OptionalLong.empty();
+        OptionalLong present = OptionalLong.of(1);
+
+        // present
+        assertTrue(present.equals(present));
+        assertTrue(present.equals(OptionalLong.of(0)));
+        assertTrue(!present.equals(empty));
+        assertTrue(Long.hashCode(1) == present.hashCode());
+        assertTrue(!present.toString().isEmpty());
+        assertTrue(!present.toString().equals(presentEmptyString.toString()));
+        assertTrue(-1 != present.toString().indexOf(Long.toString(1).toString()));
+        assertSame(1, present.get());
+        try {
+            present.ifPresent(v -> { throw new ObscureException(); });
+            fail();
+        } catch(ObscureException expected) {
+
+        }
+        assertSame(1, present.orElse(2));
+        assertSame(1, present.orElseGet(null));
+        assertSame(1, present.orElseGet(()-> 2));
+        assertSame(1, present.orElseGet(()-> 3));
+        assertSame(1, present.<RuntimeException>orElseThrow(null));
+        assertSame(1, present.<RuntimeException>orElseThrow(ObscureException::new));
+    }
+
+    private static class ObscureException extends RuntimeException {
+
+    }
+}