Example #1
0
  @Test
  public void getAThrowingSupplierUnchecked() {
    assertThat(getUnchecked(() -> 42), is(42));

    Exception e = new Exception();
    try {
      getUnchecked(
          () -> {
            throw e;
          });
    } catch (RuntimeException ex) {
      assertThat(ex.getCause(), sameInstance(e));
      return;
    }
    fail("Should throw exception");
  }
Example #2
0
  @Test
  public void applyAThrowingFunctionUnchecked() {
    assertThat(applyUnchecked(Math::round, 4.6f), is(5));
    assertThat(applyUnchecked(n -> n, null), nullValue());
    assertThat(applyUnchecked(n -> n, Optional.empty()), is(Optional.empty()));

    Exception e = new Exception();
    try {
      applyUnchecked(
          t -> {
            throw e;
          },
          "anything");
    } catch (RuntimeException ex) {
      assertThat(ex.getCause(), sameInstance(e));
      return;
    }
    fail("Should throw exception");
  }
Example #3
0
 @Test
 @SuppressWarnings("unchecked")
 public void returnsTheCausalChainOfExceptions() {
   List<Throwable> exception =
       causalChainOf(new Exception(new IllegalStateException(new IOException())))
           .collect(toList());
   assertThat(
       exception,
       contains(
           instanceOf(Exception.class),
           instanceOf(IllegalStateException.class),
           instanceOf(IOException.class)));
 }
Example #4
0
  @Test
  public void factoryMethodsForThrowingFunctionalInterfaces() throws Throwable {
    assertThat(mayThrow((t) -> t).apply("a"), is("a"));
    assertThat(mayThrow((t, u) -> t).apply("a", "b"), is("a"));
    assertThat(mayThrow(() -> "a").get(), is("a"));

    Exception ex = new Exception();
    @SuppressWarnings("unchecked")
    Consumer<Exception> exceptionHandler = mock(Consumer.class);
    mayThrow(
            t -> {
              if (t == null) throw ex;
            })
        .ifException(exceptionHandler)
        .accept(null);
    verify(exceptionHandler).accept(ex);

    expectedException.expect(sameInstance(ex));
    mayThrow(
            (t, u) -> {
              if (t == null) throw ex;
            })
        .accept(null, null);
  }
Example #5
0
  @Test
  public void runAThrowingRunnableUnchecked() {
    OneTimeToggle toggled = new OneTimeToggle();
    DiggExceptions.runUnchecked(
        () ->
            toggled.nowOrIfAlreadyThenThrow(() -> new AssertionError("should not be run twice!")));
    assertTrue(toggled.yet());

    Exception e = new Exception();
    try {
      runUnchecked(
          () -> {
            throw e;
          });
    } catch (RuntimeException ex) {
      assertThat(ex.getCause(), sameInstance(e));
      return;
    }
    fail("Should throw exception");
  }
Example #6
0
 @Test
 public void causalChainOfNullIsEmptyStream() {
   assertThat(causalChainOf(null).collect(toList()), empty());
 }