Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer use of assertj's java8 exception assertions #1753

Merged
merged 1 commit into from Sep 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -35,7 +35,8 @@

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.catchThrowable;

public class DropwizardSSLConnectionSocketFactoryTest {
private TlsConfiguration tlsConfiguration;
Expand Down Expand Up @@ -108,12 +109,9 @@ public void shouldReturn200IfServerCertInTruststore() throws Exception {
public void shouldErrorIfServerCertNotFoundInTruststore() throws Exception {
tlsConfiguration.setTrustStorePath(new File(ResourceHelpers.resourceFilePath("stores/server/other_cert_truststore.ts")));
final Client client = new JerseyClientBuilder(TLS_APP_RULE.getEnvironment()).using(jerseyClientConfiguration).build("tls_broken_client");
try {
client.target(String.format("https://localhost:%d", TLS_APP_RULE.getLocalPort())).request().get();
fail("expected ProcessingException");
} catch (ProcessingException e) {
assertThat(e.getCause()).isInstanceOf(SSLHandshakeException.class);
}
assertThatThrownBy(() -> client.target(String.format("https://localhost:%d", TLS_APP_RULE.getLocalPort())).request().get())
.isInstanceOf(ProcessingException.class)
.hasCauseInstanceOf(SSLHandshakeException.class);
}

@Test
Expand All @@ -127,12 +125,9 @@ public void shouldNotErrorIfServerCertSelfSignedAndSelfSignedCertsAllowed() thro
@Test
public void shouldErrorIfServerCertSelfSignedAndSelfSignedCertsNotAllowed() throws Exception {
final Client client = new JerseyClientBuilder(TLS_APP_RULE.getEnvironment()).using(jerseyClientConfiguration).build("self_sign_failure");
try {
client.target(String.format("https://localhost:%d", TLS_APP_RULE.getPort(1))).request().get(ClientResponse.class);
fail("expected ProcessingException");
} catch (ProcessingException e) {
assertThat(e.getCause()).isInstanceOf(SSLHandshakeException.class);
}
assertThatThrownBy(() -> client.target(String.format("https://localhost:%d", TLS_APP_RULE.getPort(1))).request().get(ClientResponse.class))
.isInstanceOf(ProcessingException.class)
.hasCauseInstanceOf(SSLHandshakeException.class);
}

@Test
Expand All @@ -151,36 +146,25 @@ public void shouldErrorIfClientAuthFails() throws Exception {
tlsConfiguration.setKeyStorePassword("password");
tlsConfiguration.setKeyStoreType("PKCS12");
final Client client = new JerseyClientBuilder(TLS_APP_RULE.getEnvironment()).using(jerseyClientConfiguration).build("client_auth_broken");
try {
client.target(String.format("https://localhost:%d", TLS_APP_RULE.getPort(2))).request().get();
fail("expected ProcessingException");
} catch (ProcessingException e) {
assertThat(e.getCause()).isInstanceOfAny(SocketException.class, SSLHandshakeException.class);
}
final Throwable exn = catchThrowable(() -> client.target(String.format("https://localhost:%d", TLS_APP_RULE.getPort(2))).request().get());
assertThat(exn).isInstanceOf(ProcessingException.class);
assertThat(exn.getCause()).isInstanceOfAny(SocketException.class, SSLHandshakeException.class);
}

@Test
public void shouldErrorIfHostnameVerificationOnAndServerHostnameDoesntMatch() throws Exception {
final Client client = new JerseyClientBuilder(TLS_APP_RULE.getEnvironment()).using(jerseyClientConfiguration).build("bad_host_broken");
try {
client.target(String.format("https://localhost:%d", TLS_APP_RULE.getPort(3))).request().get();
fail("Expected ProcessingException");
} catch (ProcessingException e) {
assertThat(e.getCause()).isExactlyInstanceOf(SSLPeerUnverifiedException.class);
assertThat(e.getCause().getMessage()).isEqualTo("Host name 'localhost' does not match the certificate subject provided by the peer (O=server, CN=badhost)");
}
final Throwable exn = catchThrowable(() -> client.target(String.format("https://localhost:%d", TLS_APP_RULE.getPort(3))).request().get());
assertThat(exn).hasCauseExactlyInstanceOf(SSLPeerUnverifiedException.class);
assertThat(exn.getCause()).hasMessage("Host name 'localhost' does not match the certificate subject provided by the peer (O=server, CN=badhost)");
}

@Test
public void shouldErrorIfHostnameVerificationOnAndServerHostnameMatchesAndFailVerifierSpecified() throws Exception {
final Client client = new JerseyClientBuilder(TLS_APP_RULE.getEnvironment()).using(jerseyClientConfiguration).using(new FailVerifier()).build("bad_host_broken_fail_verifier");
try {
client.target(String.format("https://localhost:%d", TLS_APP_RULE.getLocalPort())).request().get();
fail("Expected ProcessingException");
} catch (ProcessingException e) {
assertThat(e.getCause()).isExactlyInstanceOf(SSLPeerUnverifiedException.class);
assertThat(e.getCause().getMessage()).isEqualTo("Host name 'localhost' does not match the certificate subject provided by the peer (O=server, CN=localhost)");
}
final Throwable exn = catchThrowable(() -> client.target(String.format("https://localhost:%d", TLS_APP_RULE.getLocalPort())).request().get());
assertThat(exn).hasCauseExactlyInstanceOf(SSLPeerUnverifiedException.class);
assertThat(exn.getCause()).hasMessage("Host name 'localhost' does not match the certificate subject provided by the peer (O=server, CN=localhost)");
}

@Test
Expand Down Expand Up @@ -210,12 +194,9 @@ public void shouldBeOkIfHostnameVerificationOffAndServerHostnameMatchesAndFailVe
public void shouldRejectNonSupportedProtocols() throws Exception {
tlsConfiguration.setSupportedProtocols(asList("TLSv1.2"));
final Client client = new JerseyClientBuilder(TLS_APP_RULE.getEnvironment()).using(jerseyClientConfiguration).build("reject_non_supported");
try {
client.target(String.format("https://localhost:%d", TLS_APP_RULE.getPort(4))).request().get();
fail("expected ProcessingException");
} catch (ProcessingException e) {
assertThat(e).hasRootCauseInstanceOf(SSLException.class);
}
assertThatThrownBy(() -> client.target(String.format("https://localhost:%d", TLS_APP_RULE.getPort(4))).request().get())
.isInstanceOf(ProcessingException.class)
.hasRootCauseInstanceOf(SSLException.class);
}

private static class FailVerifier implements HostnameVerifier {
Expand Down
Expand Up @@ -9,7 +9,6 @@
import io.dropwizard.validation.BaseValidator;
import org.assertj.core.data.MapEntry;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -27,7 +26,7 @@
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;

public class ConfigurationFactoryTest {
Expand Down Expand Up @@ -417,63 +416,50 @@ public void handleDefaultConfigurationWithoutOverriding() throws Exception {
@Test
public void throwsAnExceptionIfDefaultConfigurationCantBeInstantiated() throws Exception {
System.setProperty("dw.name", "Coda Hale Overridden");
try {
new YamlConfigurationFactory<>(NonInsatiableExample.class, validator, Jackson.newObjectMapper(), "dw").build();
Assert.fail("Configuration is parsed, but shouldn't be");
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Unable create an instance of the configuration class: " +
"'io.dropwizard.configuration.ConfigurationFactoryTest.NonInsatiableExample'");
}

final YamlConfigurationFactory<NonInsatiableExample> factory =
new YamlConfigurationFactory<>(NonInsatiableExample.class, validator, Jackson.newObjectMapper(), "dw");
assertThatThrownBy(factory::build)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Unable create an instance of the configuration class: " +
"'io.dropwizard.configuration.ConfigurationFactoryTest.NonInsatiableExample'");
}

@Test
public void printsDidYouMeanOnUnrecognizedField() throws Exception {
final File resourceFileName = resourceFileName("factory-test-typo.yml");
try {
factory.build(resourceFileName);
fail("Typo in a configuration should be caught");
} catch (ConfigurationParsingException e) {
assertThat(e.getMessage()).isEqualTo(resourceFileName + " has an error:" + NEWLINE +
" * Unrecognized field at: propertis" + NEWLINE +
" Did you mean?:" + NEWLINE +
" - properties" + NEWLINE +
" - servers" + NEWLINE +
" - type" + NEWLINE +
" - name" + NEWLINE +
" - age" + NEWLINE +
" [2 more]" + NEWLINE);
}
assertThatThrownBy(() -> factory.build(resourceFileName))
.isInstanceOf(ConfigurationParsingException.class)
.hasMessage(resourceFileName + " has an error:" + NEWLINE +
" * Unrecognized field at: propertis" + NEWLINE +
" Did you mean?:" + NEWLINE +
" - properties" + NEWLINE +
" - servers" + NEWLINE +
" - type" + NEWLINE +
" - name" + NEWLINE +
" - age" + NEWLINE +
" [2 more]" + NEWLINE);
}

@Test
public void incorrectTypeIsFound() throws Exception {
final File resourceFileName = resourceFileName("factory-test-wrong-type.yml");
try {
factory.build(resourceFileName);
fail("Incorrect type in a configuration should be found");
} catch (ConfigurationParsingException e) {
assertThat(e.getMessage()).isEqualTo(resourceFileName + " has an error:" + NEWLINE +
" * Incorrect type of value at: age; is of type: String, expected: int" + NEWLINE);
}
assertThatThrownBy(() -> factory.build(resourceFileName))
.isInstanceOf(ConfigurationParsingException.class)
.hasMessage(resourceFileName + " has an error:" + NEWLINE +
" * Incorrect type of value at: age; is of type: String, expected: int" + NEWLINE);
}

@Test
public void printsDetailedInformationOnMalformedYaml() throws Exception {
final File resourceFileName = resourceFileName("factory-test-malformed-advanced.yml");
try {
factory.build(resourceFileName);
fail("Should print a detailed error on a malformed YAML file");
} catch (Exception e) {
assertThat(e.getMessage()).isEqualTo(
"YAML decoding problem: while parsing a flow sequence\n" +
" in 'reader', line 2, column 7:\n" +
" type: [ coder,wizard\n" +
" ^\n" +
"expected ',' or ']', but got StreamEnd\n" +
" in 'reader', line 2, column 21:\n" +
" wizard\n" +
" ^\n");
}
assertThatThrownBy(() -> factory.build(resourceFileName))
.hasMessage("YAML decoding problem: while parsing a flow sequence\n" +
" in 'reader', line 2, column 7:\n" +
" type: [ coder,wizard\n" +
" ^\n" +
"expected ',' or ']', but got StreamEnd\n" +
" in 'reader', line 2, column 21:\n" +
" wizard\n" +
" ^\n");
}
}
Expand Up @@ -13,14 +13,14 @@
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.context.internal.ManagedSessionContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;

import java.lang.reflect.Method;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hibernate.resource.transaction.spi.TransactionStatus.ACTIVE;
import static org.hibernate.resource.transaction.spi.TransactionStatus.NOT_ACTIVE;
import static org.mockito.Mockito.doAnswer;
Expand Down Expand Up @@ -240,13 +240,10 @@ public void beginsAndCommitsATransactionForAnalytics() throws Exception {

@Test
public void throwsExceptionOnNotRegisteredDatabase() throws Exception {
try {
prepareAppEvent("methodWithUnitOfWorkOnNotRegisteredDatabase");
execute();
Assert.fail();
} catch (IllegalArgumentException e) {
Assert.assertEquals(e.getMessage(), "Unregistered Hibernate bundle: 'warehouse'");
}
prepareAppEvent("methodWithUnitOfWorkOnNotRegisteredDatabase");
assertThatThrownBy(this::execute)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Unregistered Hibernate bundle: 'warehouse'");
}

private void prepareAppEvent(String resourceMethodName) throws NoSuchMethodException {
Expand Down
Expand Up @@ -11,7 +11,7 @@
import java.util.jar.JarEntry;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class ResourceURLTest {
private File directory;
Expand Down Expand Up @@ -78,14 +78,10 @@ public void isDirectoryReturnsTrueForDirectoriesInJarsWithoutTrailingSlashes() t

@Test
public void isDirectoryThrowsResourceNotFoundExceptionForMissingDirectories() throws Exception {
URL url = Resources.getResource("META-INF/");
url = new URL(url.toExternalForm() + "missing");
try {
ResourceURL.isDirectory(url);
fail("should have thrown an exception");
} catch (ResourceNotFoundException ignored) {
// expected
}
final URL url = Resources.getResource("META-INF/");
final URL nurl = new URL(url.toExternalForm() + "missing");
assertThatThrownBy(() -> ResourceURL.isDirectory(nurl))
.isInstanceOf(ResourceNotFoundException.class);
}

@Test
Expand Down