org.junit.function.ThrowingRunnable Java Examples

The following examples show how to use org.junit.function.ThrowingRunnable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PrerequisitesTaskTest.java    From appcenter-plugin with MIT License 7 votes vote down vote up
@Test
public void should_ThrowExecutionException_When_DebugSymbolsDoesNotExists() throws Exception {
    // Given
    final String pathToApp = String.join(File.separator, "path", "to", "app.apk");
    final String pathToReleaseNotes = String.join(File.separator, "path", "to", "release-notes.md");
    final FilePath[] files = {new FilePath(new File(pathToApp))};
    final FilePath[] debugSymbols = {};
    final FilePath[] releaseNotes = {new FilePath(new File(pathToReleaseNotes))};
    given(mockFilePath.list(anyString())).willReturn(files, debugSymbols, releaseNotes);

    // When
    final ThrowingRunnable throwingRunnable = () -> task.execute(fullUploadRequest).get();

    // Then
    final ExecutionException exception = assertThrows(ExecutionException.class, throwingRunnable);
    assertThat(exception).hasCauseThat().isInstanceOf(AppCenterException.class);
    assertThat(exception).hasCauseThat().hasMessageThat().isEqualTo(String.format("No symbols found matching pattern: %s", fullUploadRequest.pathToDebugSymbols));
}
 
Example #2
Source File: PrerequisitesTaskTest.java    From appcenter-plugin with MIT License 6 votes vote down vote up
@Test
public void should_ThrowExecutionException_When_MultipleFilesExists() throws Exception {
    // Given
    final String pathToApp = "path/to/app.apk";
    final String pathToAnotherApp = "path/to/sample.apk";
    final FilePath[] files = {new FilePath(new File(pathToApp)), new FilePath(new File(pathToAnotherApp))};
    given(mockFilePath.list(anyString())).willReturn(files);

    // When
    final ThrowingRunnable throwingRunnable = () -> task.execute(fullUploadRequest).get();

    // Then
    final ExecutionException exception = assertThrows(ExecutionException.class, throwingRunnable);
    assertThat(exception).hasCauseThat().isInstanceOf(AppCenterException.class);
    assertThat(exception).hasCauseThat().hasMessageThat().isEqualTo(String.format("Multiple files found matching pattern: %s", fullUploadRequest.pathToApp));
}
 
Example #3
Source File: PrerequisitesTaskTest.java    From appcenter-plugin with MIT License 6 votes vote down vote up
@Test
public void should_ThrowExecutionException_When_MultipleDebugSymbolsExists() throws Exception {
    // Given
    final String pathToApp = String.join(File.separator, "path", "to", "app.apk");
    final String pathToDebugSymbols = String.join(File.separator, "path", "to", "debug.zip");
    final String pathToAnotherDebugSymbols = String.join(File.separator, "path", "to", "more-debug.zip");
    final String pathToReleaseNotes = String.join(File.separator, "path", "to", "release-notes.md");
    final FilePath[] files = {new FilePath(new File(pathToApp))};
    final FilePath[] debugSymbols = {new FilePath(new File(pathToDebugSymbols)), new FilePath(new File(pathToAnotherDebugSymbols))};
    final FilePath[] releaseNotes = {new FilePath(new File(pathToReleaseNotes))};
    given(mockFilePath.list(anyString())).willReturn(files, debugSymbols, releaseNotes);

    // When
    final ThrowingRunnable throwingRunnable = () -> task.execute(fullUploadRequest).get();

    // Then
    final ExecutionException exception = assertThrows(ExecutionException.class, throwingRunnable);
    assertThat(exception).hasCauseThat().isInstanceOf(AppCenterException.class);
    assertThat(exception).hasCauseThat().hasMessageThat().isEqualTo(String.format("Multiple symbols found matching pattern: %s", fullUploadRequest.pathToDebugSymbols));
}
 
Example #4
Source File: PrerequisitesTaskTest.java    From appcenter-plugin with MIT License 6 votes vote down vote up
@Test
public void should_ThrowExecutionException_When_ReleaseNotesDoesNotExists() throws Exception {
    // Given
    final UploadRequest uploadRequest = fullUploadRequest.newBuilder().setPathToDebugSymbols("").build();
    final String pathToApp = String.join(File.separator, "path", "to", "app.apk");
    final FilePath[] files = {new FilePath(new File(pathToApp))};
    final FilePath[] releaseNotes = {};
    given(mockFilePath.list(anyString())).willReturn(files, releaseNotes);

    // When
    final ThrowingRunnable throwingRunnable = () -> task.execute(uploadRequest).get();

    // Then
    final ExecutionException exception = assertThrows(ExecutionException.class, throwingRunnable);
    assertThat(exception).hasCauseThat().isInstanceOf(AppCenterException.class);
    assertThat(exception).hasCauseThat().hasMessageThat().isEqualTo(String.format("No release notes found matching pattern: %s", fullUploadRequest.pathToReleaseNotes));
}
 
Example #5
Source File: PrerequisitesTaskTest.java    From appcenter-plugin with MIT License 6 votes vote down vote up
@Test
public void should_ThrowExecutionException_When_MultipleReleaseNotesExists() throws Exception {
    // Given
    final UploadRequest uploadRequest = fullUploadRequest.newBuilder().setPathToDebugSymbols("").build();
    final String pathToApp = String.join(File.separator, "path", "to", "app.apk");
    final String pathToReleaseNotes = String.join(File.separator, "path", "to", "release-notes.md");
    final String pathToAnotherReleaseNotes = String.join(File.separator, "path", "to", "more-release-notes.md");
    final FilePath[] files = {new FilePath(new File(pathToApp))};
    final FilePath[] releaseNotes = {new FilePath(new File(pathToReleaseNotes)), new FilePath(new File(pathToAnotherReleaseNotes))};
    given(mockFilePath.list(anyString())).willReturn(files, releaseNotes);

    // When
    final ThrowingRunnable throwingRunnable = () -> task.execute(uploadRequest).get();

    // Then
    final ExecutionException exception = assertThrows(ExecutionException.class, throwingRunnable);
    assertThat(exception).hasCauseThat().isInstanceOf(AppCenterException.class);
    assertThat(exception).hasCauseThat().hasMessageThat().isEqualTo(String.format("Multiple release notes found matching pattern: %s", fullUploadRequest.pathToReleaseNotes));
}
 
Example #6
Source File: DistributeResourceTaskTest.java    From appcenter-plugin with MIT License 5 votes vote down vote up
@Test
public void should_ReturnException_When_RequestIsUnSuccessful() {
    // Given
    mockWebServer.enqueue(new MockResponse().setResponseCode(500));

    // When
    final ThrowingRunnable throwingRunnable = () -> task.execute(baseRequest).get();

    // Then
    final ExecutionException exception = assertThrows(ExecutionException.class, throwingRunnable);
    assertThat(exception).hasCauseThat().isInstanceOf(AppCenterException.class);
    assertThat(exception).hasMessageThat().contains("Distributing resource unsuccessful: HTTP 500 Server Error: ");
}
 
Example #7
Source File: HttpResponseExceptionTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testUnsupportedCharset() throws Exception {
  HttpTransport transport =
      new MockHttpTransport() {
        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
          return new MockLowLevelHttpRequest() {
            @Override
            public LowLevelHttpResponse execute() throws IOException {
              MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
              result.setStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
              result.setReasonPhrase("Not Found");
              result.setContentType("text/plain; charset=invalid-charset");
              result.setContent("Unable to find resource");
              return result;
            }
          };
        }
      };
  final HttpRequest request =
      transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
  HttpResponseException responseException =
      assertThrows(
          HttpResponseException.class,
          new ThrowingRunnable() {
            @Override
            public void run() throws Throwable {
              request.execute();
            }
          });
  assertThat(responseException)
      .hasMessageThat()
      .isEqualTo("404 Not Found\nGET " + SIMPLE_GENERIC_URL);
}
 
Example #8
Source File: HttpResponseExceptionTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testInvalidCharset() throws Exception {
  HttpTransport transport =
      new MockHttpTransport() {
        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
          return new MockLowLevelHttpRequest() {
            @Override
            public LowLevelHttpResponse execute() throws IOException {
              MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
              result.setStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
              result.setReasonPhrase("Not Found");
              result.setContentType("text/plain; charset=");
              result.setContent("Unable to find resource");
              return result;
            }
          };
        }
      };
  final HttpRequest request =
      transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
  HttpResponseException responseException =
      assertThrows(
          HttpResponseException.class,
          new ThrowingRunnable() {
            @Override
            public void run() throws Throwable {
              request.execute();
            }
          });

  assertThat(responseException)
      .hasMessageThat()
      .isEqualTo("404 Not Found\nGET " + SIMPLE_GENERIC_URL);
}
 
Example #9
Source File: HttpResponseExceptionTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testThrown() throws Exception {
  HttpTransport transport =
      new MockHttpTransport() {
        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
          return new MockLowLevelHttpRequest() {
            @Override
            public LowLevelHttpResponse execute() throws IOException {
              MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
              result.setStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
              result.setReasonPhrase("Not Found");
              result.setContent("Unable to find resource");
              return result;
            }
          };
        }
      };
  final HttpRequest request =
      transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
  HttpResponseException responseException =
      assertThrows(
          HttpResponseException.class,
          new ThrowingRunnable() {
            @Override
            public void run() throws Throwable {
              request.execute();
            }
          });

  assertThat(responseException)
      .hasMessageThat()
      .isEqualTo(
          "404 Not Found\nGET "
              + SIMPLE_GENERIC_URL
              + LINE_SEPARATOR
              + "Unable to find resource");
}
 
Example #10
Source File: HttpResponseExceptionTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testConstructor_messageButNoStatusCode() throws Exception {
  HttpTransport transport =
      new MockHttpTransport() {
        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
          return new MockLowLevelHttpRequest() {
            @Override
            public LowLevelHttpResponse execute() throws IOException {
              MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
              result.setStatusCode(0);
              result.setReasonPhrase("Foo");
              return result;
            }
          };
        }
      };
  final HttpRequest request =
      transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
  HttpResponseException responseException =
      assertThrows(
          HttpResponseException.class,
          new ThrowingRunnable() {
            @Override
            public void run() throws Throwable {
              request.execute();
            }
          });
  assertThat(responseException).hasMessageThat().isEqualTo("Foo\nGET " + SIMPLE_GENERIC_URL);
}
 
Example #11
Source File: HttpResponseExceptionTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testConstructor_noStatusCode() throws Exception {
  HttpTransport transport =
      new MockHttpTransport() {
        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
          return new MockLowLevelHttpRequest() {
            @Override
            public LowLevelHttpResponse execute() throws IOException {
              MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
              result.setStatusCode(0);
              return result;
            }
          };
        }
      };
  final HttpRequest request =
      transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
  HttpResponseException responseException =
      assertThrows(
          HttpResponseException.class,
          new ThrowingRunnable() {
            @Override
            public void run() throws Throwable {
              request.execute();
            }
          });
  assertThat(responseException).hasMessageThat().isEqualTo("GET " + SIMPLE_GENERIC_URL);
}
 
Example #12
Source File: BaseOperatorTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
private DynamicTest toDynamicTest(OperatorScenario<I, PI, O, PO> scenario, ThrowingRunnable runnable) {
	return DynamicTest.dynamicTest(scenario.description(), () -> {
		if (scenario.stack != null) {
			System.out.println("\tat " + scenario.stack.getStackTrace()[2]);
		}
		runnable.run();
	});
}
 
Example #13
Source File: SqlHelper.java    From nomulus with Apache License 2.0 5 votes vote down vote up
public static void assertThrowForeignKeyViolation(ThrowingRunnable runnable) {
  PersistenceException thrown = assertThrows(PersistenceException.class, runnable);
  assertThat(Throwables.getRootCause(thrown)).isInstanceOf(SQLException.class);
  assertThat(Throwables.getRootCause(thrown))
      .hasMessageThat()
      .contains("violates foreign key constraint");
}
 
Example #14
Source File: Assert.java    From j2cl with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when
 * executed. If it does, the exception object is returned. If it does not throw an exception, an
 * {@link AssertionError} is thrown. If it throws the wrong type of exception, an {@code
 * AssertionError} is thrown describing the mismatch; the exception that was actually thrown can
 * be obtained by calling {@link AssertionError#getCause}.
 *
 * @param message the identifying message for the {@link AssertionError} (<code>null</code>
 * okay)
 * @param expectedThrowable the expected type of the exception
 * @param runnable a function that is expected to throw an exception when executed
 * @return the exception thrown by {@code runnable}
 * @since 4.13
 */
public static <T extends Throwable> T assertThrows(String message, Class<T> expectedThrowable,
        ThrowingRunnable runnable) {
    // Sanity check due to b/31386321.
    assertFalse("Class metadata is corrupt", isInstanceOfTypeJ2cl(new Object(), String.class));

    try {
        runnable.run();
    } catch (Throwable actualThrown) {
        if (isInstanceOfTypeJ2cl(actualThrown, expectedThrowable)) {
            @SuppressWarnings("unchecked") T retVal = (T) actualThrown;
            return retVal;
        } else {
            String mismatchMessage =
                buildPrefix(message)
                    + format(
                        "unexpected exception type thrown;",
                        expectedThrowable.getName(),
                        actualThrown.getClass().getName());
            throw new AssertionError(mismatchMessage, actualThrown);
        }
    }
String notThrownMessage =
    buildPrefix(message)
        + "expected "
        + expectedThrowable.getName()
        + " to be thrown, but nothing was thrown";
    throw new AssertionError(notThrownMessage);
}
 
Example #15
Source File: UploadAppToResourceTaskTest.java    From appcenter-plugin with MIT License 5 votes vote down vote up
@Test
public void should_ReturnException_When_RequestIsUnSuccessful() {
    // Given
    mockWebServer.enqueue(new MockResponse().setResponseCode(500));

    // When
    final ThrowingRunnable throwingRunnable = () -> task.execute(baseRequest).get();

    // Then
    final ExecutionException exception = assertThrows(ExecutionException.class, throwingRunnable);
    assertThat(exception).hasCauseThat().isInstanceOf(AppCenterException.class);
    assertThat(exception).hasCauseThat().hasMessageThat().contains("Upload app to resource unsuccessful: HTTP 500 Server Error: ");
}
 
Example #16
Source File: CreateUploadResourceTaskTest.java    From appcenter-plugin with MIT License 5 votes vote down vote up
@Test
public void should_ReturnException_When_RequestIsUnSuccessful() {
    // Given
    mockWebServer.enqueue(new MockResponse().setResponseCode(500));

    // When
    final ThrowingRunnable throwingRunnable = () -> task.execute(baseRequest).get();

    // Then
    final ExecutionException exception = assertThrows(ExecutionException.class, throwingRunnable);
    assertThat(exception).hasCauseThat().isInstanceOf(AppCenterException.class);
    assertThat(exception).hasCauseThat().hasMessageThat().contains("Create upload resource for app unsuccessful: HTTP 500 Server Error: ");
}
 
Example #17
Source File: PrerequisitesTaskTest.java    From appcenter-plugin with MIT License 5 votes vote down vote up
@Test
public void should_ThrowExecutionException_When_FileDoesNotExists() throws Exception {
    // Given
    final FilePath[] files = {};
    given(mockFilePath.list(anyString())).willReturn(files);

    // When
    final ThrowingRunnable throwingRunnable = () -> task.execute(fullUploadRequest).get();

    // Then
    final ExecutionException exception = assertThrows(ExecutionException.class, throwingRunnable);
    assertThat(exception).hasCauseThat().isInstanceOf(AppCenterException.class);
    assertThat(exception).hasCauseThat().hasMessageThat().isEqualTo(String.format("No file found matching pattern: %s", fullUploadRequest.pathToApp));
}
 
Example #18
Source File: CommitUploadResourceTaskTest.java    From appcenter-plugin with MIT License 5 votes vote down vote up
@Test
public void should_ReturnException_When_RequestIsUnSuccessful() {
    // Given
    mockWebServer.enqueue(new MockResponse().setResponseCode(400));

    // When
    final ThrowingRunnable throwingRunnable = () -> task.execute(baseRequest).get();

    // Then
    final ExecutionException exception = assertThrows(ExecutionException.class, throwingRunnable);
    assertThat(exception).hasCauseThat().isInstanceOf(AppCenterException.class);
    assertThat(exception).hasCauseThat().hasMessageThat().contains("Committing app resource unsuccessful: HTTP 400 Client Error: ");
}
 
Example #19
Source File: GenericCastTest.java    From j2cl with Apache License 2.0 4 votes vote down vote up
private static void assertThrowsClassCastException(ThrowingRunnable runnable, Class<?> toClass) {
  assertThat(assertThrows(ClassCastException.class, runnable))
      .hasMessageThat()
      .endsWith("cannot be cast to " + toClass.getName());
}
 
Example #20
Source File: CalciteCannotParseSimpleIdentifiersTest.java    From beam with Apache License 2.0 4 votes vote down vote up
private ThrowingRunnable attemptParse(String alias) {
  return () -> BeamSqlEnv.inMemory().isDdl(String.format("SELECT 321 AS %s", alias));
}
 
Example #21
Source File: GrpcClientRequiresTrailersTest.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
private static void assertThrowsGrpcStatusException(ThrowingRunnable runnable) {
    assertGrpcStatusException(assertThrows(GrpcStatusException.class, runnable));
}
 
Example #22
Source File: GrpcClientRequiresTrailersTest.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
private static void assertThrowsExecutionException(ThrowingRunnable runnable) {
    ExecutionException ex = assertThrows(ExecutionException.class, runnable);
    assertThat(ex.getCause(), is(instanceOf(GrpcStatusException.class)));
    assertGrpcStatusException((GrpcStatusException) ex.getCause());
}
 
Example #23
Source File: PyProviderUtilsTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
private static void assertThrowsEvalExceptionContaining(
    ThrowingRunnable runnable, String message) {
  assertThat(assertThrows(EvalException.class, runnable)).hasMessageThat().contains(message);
}
 
Example #24
Source File: PyStructUtilsTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
private static void assertThrowsEvalExceptionContaining(
    ThrowingRunnable runnable, String message) {
  assertThat(assertThrows(EvalException.class, runnable)).hasMessageThat().contains(message);
}
 
Example #25
Source File: PyStructUtilsTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
private static void assertHasMissingFieldMessage(ThrowingRunnable access, String fieldName) {
  assertThrowsEvalExceptionContaining(
      access, String.format("\'py' provider missing '%s' field", fieldName));
}
 
Example #26
Source File: PyStructUtilsTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
private static void assertHasWrongTypeMessage(
    ThrowingRunnable access, String fieldName, String expectedType) {
  assertThrowsEvalExceptionContaining(
      access,
      String.format("\'py' provider's '%s' field was int, want %s", fieldName, expectedType));
}
 
Example #27
Source File: Assert.java    From j2cl with Apache License 2.0 2 votes vote down vote up
/**
 * Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when
 * executed. If it does not throw an exception, an {@link AssertionError} is thrown. If it throws
 * the wrong type of exception, an {@code AssertionError} is thrown describing the mismatch; the
 * exception that was actually thrown can be obtained by calling {@link AssertionError#getCause}.
 *
 * @param expectedThrowable the expected type of the exception
 * @param runnable a function that is expected to throw an exception when executed
 * @since 4.13
 */
public static <T extends Throwable> T assertThrows(
    Class<T> expectedThrowable, ThrowingRunnable runnable) {
  return assertThrows(null, expectedThrowable, runnable);
  }