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