@Test public void orThrow() throws Exception { try { OptionalByte.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 { OptionalByte.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((byte) 42, OptionalByte.absent().or(new ByteSupplier() { @Override public byte get() { return 42; } })); }
@Test public void or() throws Exception { Assert.assertEquals(((byte) 42), OptionalByte.absent().or(((byte) 42))); }
@Test public void absent() throws Exception { Assert.assertFalse(OptionalByte.absent().isPresent()); }
@Test public void ifPresent() throws Exception { OptionalByte.of(((byte) 42)).ifPresent(new ByteConsumer() { @Override public void consume(byte value) { Assert.assertEquals(((byte) 42), value); } }); OptionalByte.absent().ifPresent(new ByteConsumer() { @Override public void consume(byte value) { Assert.fail("ifPresent() on an absent optional should never call its consumer"); } }); }
@Test public void isPresent() throws Exception { Assert.assertTrue(OptionalByte.of(((byte) 42)).isPresent()); Assert.assertFalse(OptionalByte.absent().isPresent()); }
@Test public void ifPresentOrElse() throws Exception { OptionalByte.of(((byte) 42)).ifPresentOrElse(new ByteConsumer() { @Override public void consume(byte value) { Assert.assertEquals(((byte) 42), value); } }, new Function() { @Override public void call() { Assert.fail("ifPresent() on an non-absent optional should call its consumer"); } }); OptionalByte.absent().ifPresentOrElse(new ByteConsumer() { @Override public void consume(byte 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 get() throws Exception { Assert.assertEquals(((byte) 42), OptionalByte.of(((byte) 42)).get()); try { OptionalByte.absent().get(); Assert.fail("Invoking get() on an absent optional should raise an exception."); } catch (IllegalStateException expected) { // No-op. This is the expected behaviour. } }