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