@Test public void orThrow() throws Exception { try { OptionalShort.absent().orThrow(new Exception("An exception occurred.")); Assert.fail("Invoking orThrow() on an absent optional should throw an exception."); } catch (Exception anException) { // No-op. This is the expected behaviour. } }
@Test public void orThrowWithSupplier() throws Exception { final Supplier<Exception> anExceptionSupplier = new Supplier<Exception>() { @Override public Exception get() { return new Exception("An exception occurred."); } }; try { OptionalShort.absent().orThrow(anExceptionSupplier); Assert.fail("Invoking orThrow() on an absent optional should throw an exception."); } catch (Exception anException) { // No-op. This is the expected behaviour. } }
@Test public void orWithSupplier() throws Exception { Assert.assertEquals((short) 42, OptionalShort.absent().or(new ShortSupplier() { @Override public short get() { return 42; } })); }
@Test public void absent() throws Exception { Assert.assertFalse(OptionalShort.absent().isPresent()); }
@Test public void or() throws Exception { Assert.assertEquals(((short) 42), OptionalShort.absent().or(((short) 42))); }
@Test public void ifPresent() throws Exception { OptionalShort.of(((short) 42)).ifPresent(new ShortConsumer() { @Override public void consume(short value) { Assert.assertEquals(((short) 42), value); } }); OptionalShort.absent().ifPresent(new ShortConsumer() { @Override public void consume(short value) { Assert.fail("ifPresent() on an absent optional should never call its consumer"); } }); }
@Test public void ifPresentOrElse() throws Exception { OptionalShort.of(((short) 42)).ifPresentOrElse(new ShortConsumer() { @Override public void consume(short value) { Assert.assertEquals(((short) 42), value); } }, new Function() { @Override public void call() { Assert.fail("ifPresent() on an non-absent optional should call its consumer"); } }); OptionalShort.absent().ifPresentOrElse(new ShortConsumer() { @Override public void consume(short value) { Assert.fail("ifPresent() on an absent optional should never call its consumer"); } }, new Function() { @Override public void call() { Assert.assertTrue(true); // No-op. This is the expected behaviour. } }); } }
@Test public void isPresent() throws Exception { Assert.assertTrue(OptionalShort.of(((short) 42)).isPresent()); Assert.assertFalse(OptionalShort.absent().isPresent()); }
@Test public void get() throws Exception { Assert.assertEquals(((short) 42), OptionalShort.of(((short) 42)).get()); try { OptionalShort.absent().get(); Assert.fail("Invoking get() on an absent optional should raise an exception."); } catch (IllegalStateException expected) { // No-op. This is the expected behaviour. } }