< prev index next >

test/java/util/Optional/BasicDouble.java

Print this page
rev 11381 : 8071670: java.util.Optional: please add a way to specify if-else behavior
Reviewed-by: dfuchs

*** 27,36 **** --- 27,37 ---- * @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;
*** 47,61 **** --- 48,103 ---- assertTrue(empty.equals(OptionalDouble.empty())); assertTrue(!empty.equals(present)); assertTrue(0 == empty.hashCode()); assertTrue(!empty.toString().isEmpty()); assertTrue(!empty.isPresent()); + empty.ifPresent(v -> { fail(); }); + + AtomicBoolean emptyCheck = new AtomicBoolean(); + empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true)); + assertTrue(emptyCheck.get()); + + try { + empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); }); + fail(); + } catch (ObscureException expected) { + } catch (AssertionError e) { + throw e; + } catch (Throwable t) { + fail(); + } + assertEquals(2.0, empty.orElse(2.0)); assertEquals(2.0, empty.orElseGet(()-> 2.0)); } + @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) {} + } + @Test(expectedExceptions=NoSuchElementException.class) public void testEmptyGet() { OptionalDouble empty = OptionalDouble.empty(); double got = empty.getAsDouble();
*** 94,109 **** 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())); assertEquals(1.0, present.getAsDouble()); try { present.ifPresent(v -> { throw new ObscureException(); }); fail(); ! } catch(ObscureException expected) { ! } 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)); --- 136,172 ---- 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())); assertEquals(1.0, present.getAsDouble()); + + 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));
< prev index next >