/** * Asserts that {@code runnable} throws an exception of type {@code throwableClass} 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 throwableClass the expected type of the exception * @param runnable A function that is expected to throw an exception when invoked * @since 6.9.5 */ @SuppressWarnings("ThrowableResultOfMethodCallIgnored") public static <T extends Throwable> void assertThrows(Class<T> throwableClass, ThrowingRunnable runnable) { expectThrows(throwableClass, runnable); }
/** * Asserts that {@code runnable} throws an exception of type {@code throwableClass} 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 throwableClass the expected type of the exception * @param runnable A function that is expected to throw an exception when invoked * @since 6.9.5 */ @SuppressWarnings("ThrowableResultOfMethodCallIgnored") public static <T extends Throwable> void assertThrows( Class<T> throwableClass, ThrowingRunnable runnable) { expectThrows(throwableClass, runnable); }
@Test(expectedExceptions = AssertionError.class) public void expectThrowsDetectsTypeMismatchesViaExplicitTypeHint() { NullPointerException npe = new NullPointerException(); expectThrows(IOException.class, throwingRunnable(npe)); }
@Test public void unknownCaptureIsAnError() { Pattern<?> pattern = any(); Capture<?> unknownCapture = newCapture(); Match<?> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42); Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(unknownCapture)); assertTrue(throwable.getMessage().contains("unknown Capture")); }
@Test(expectedExceptions = AssertionError.class) public void expectThrowsRequiresAnExceptionToBeThrown() { expectThrows(Throwable.class, nonThrowingRunnable()); }
@Test public void expectThrowsReturnsTheSameObjectThrown() { NullPointerException npe = new NullPointerException(); Throwable throwable = expectThrows(Throwable.class, throwingRunnable(npe)); assertSame(npe, throwable); }
@Test public void noMatchMeansNoCaptures() { Capture<Void> impossible = newCapture(); Pattern<Void> pattern = typeOf(Void.class).capturedAs(impossible); Match<Void> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42); assertTrue(match.isEmpty()); Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(impossible)); assertTrue(throwable.getMessage().contains("Captures are undefined for an empty Match")); }
@Test public void expectThrowsIncludesAnInformativeDefaultMessage() { try { expectThrows(Throwable.class, nonThrowingRunnable()); } catch (AssertionError ex) { assertEquals("Expected Throwable to be thrown, but nothing was thrown", ex.getMessage()); return; } fail(); }
@Test public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() { NullPointerException npe = new NullPointerException(); try { expectThrows(IOException.class, throwingRunnable(npe)); } catch (AssertionError error) { assertEquals("Expected IOException to be thrown, but NullPointerException was thrown", error.getMessage()); assertSame(npe, error.getCause()); return; } fail(); }
@Test public void testRenameTable() { SchemaTableName tableName = new SchemaTableName("test_schema", "test_table_to_be_renamed"); metadata.createSchema(SESSION, "test_schema", ImmutableMap.of()); ConnectorOutputTableHandle table = metadata.beginCreateTable( SESSION, new ConnectorTableMetadata(tableName, ImmutableList.of(), ImmutableMap.of()), Optional.empty()); metadata.finishCreateTable(SESSION, table, ImmutableList.of(), ImmutableList.of()); // rename table to schema which does not exist SchemaTableName invalidSchemaTableName = new SchemaTableName("test_schema_not_exist", "test_table_renamed"); ConnectorTableHandle tableHandle = metadata.getTableHandle(SESSION, tableName); Throwable throwable = expectThrows(SchemaNotFoundException.class, () -> metadata.renameTable(SESSION, tableHandle, invalidSchemaTableName)); assertTrue(throwable.getMessage().equals("Schema test_schema_not_exist not found")); // rename table to same schema SchemaTableName sameSchemaTableName = new SchemaTableName("test_schema", "test_renamed"); metadata.renameTable(SESSION, metadata.getTableHandle(SESSION, tableName), sameSchemaTableName); assertEquals(metadata.listTables(SESSION, "test_schema"), ImmutableList.of(sameSchemaTableName)); // rename table to different schema metadata.createSchema(SESSION, "test_different_schema", ImmutableMap.of()); SchemaTableName differentSchemaTableName = new SchemaTableName("test_different_schema", "test_renamed"); metadata.renameTable(SESSION, metadata.getTableHandle(SESSION, sameSchemaTableName), differentSchemaTableName); assertEquals(metadata.listTables(SESSION, "test_schema"), ImmutableList.of()); assertEquals(metadata.listTables(SESSION, "test_different_schema"), ImmutableList.of(differentSchemaTableName)); }
@Test public void expectThrowsWrapsAndPropagatesUnexpectedExceptions() { NullPointerException npe = new NullPointerException("inner-message"); try { expectThrows(IOException.class, throwingRunnable(npe)); } catch (AssertionError ex) { assertSame(npe, ex.getCause()); assertEquals("inner-message", ex.getCause().getMessage()); return; } fail(); }
@Test void testInvalidPassword() { Assert.expectThrows(SVNAuthenticationException.class, () -> checkUser(administrator, "wrongpassword")); }
@Test void invalidUser() { Assert.expectThrows(SVNAuthenticationException.class, () -> checkUser("wronguser", rootPassword)); }
@Test public void invalidPassword() { Assert.expectThrows(SVNAuthenticationException.class, () -> checkUser("ldapadmin", "wrongpassword")); }
@Test public void testTruncatedStream() throws Exception { byte[] compressed = Files.readAllBytes(BLOCK_COMPRESSED.toPath()); byte[] truncated = Arrays.copyOf(compressed, compressed.length * 2 / 3); try (BlockCompressedInputStream stream = new BlockCompressedInputStream(new ByteArrayInputStream(truncated))) { Assert.expectThrows(FileTruncatedException.class, () -> InputStreamUtils.readFully(stream)); } }
@Test public void unknownCaptureIsAnError() { Pattern<?> pattern = any(); Capture<?> unknownCapture = newCapture(); Match<?> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42); Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(unknownCapture)); assertTrue(throwable.getMessage().contains("unknown Capture")); }
@Test public void unknownCaptureIsAnError() { Pattern<?> pattern = any(); Capture<?> unknownCapture = newCapture(); Match<?> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42); Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(unknownCapture)); assertTrue(throwable.getMessage().contains("unknown Capture")); }
@Test public void noMatchMeansNoCaptures() { Capture<Void> impossible = newCapture(); Pattern<Void> pattern = typeOf(Void.class).capturedAs(impossible); Match<Void> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42); assertTrue(match.isEmpty()); Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(impossible)); assertTrue(throwable.getMessage().contains("Captures are undefined for an empty Match")); }
@Test public void noMatchMeansNoCaptures() { Capture<Void> impossible = newCapture(); Pattern<Void> pattern = typeOf(Void.class).capturedAs(impossible); Match<Void> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42); assertTrue(match.isEmpty()); Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(impossible)); assertTrue(throwable.getMessage().contains("Captures are undefined for an empty Match")); }
@Test public void testDuplicateField() { GraphQLAnnotations instance = new GraphQLAnnotations(); GraphQLObjectHandler graphQLObjectHandler = instance.getObjectHandler(); instance.registerTypeExtension(TestObjectExtensionInvalid.class); GraphQLAnnotationsException e = expectThrows(GraphQLAnnotationsException.class, () -> graphQLObjectHandler.getObject(TestObject.class,instance.getContainer())); assertTrue(e.getMessage().startsWith("Duplicate field")); } }