Use assertThrows instead of try/fail constructs

This commit is contained in:
Richard Körber
2022-05-07 11:25:59 +02:00
parent cf0bfc1390
commit 00ee9e4dd5
20 changed files with 256 additions and 544 deletions

View File

@@ -16,6 +16,7 @@ package org.shredzone.acme4j.it.pebble;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import java.net.URI;
@@ -137,14 +138,12 @@ public class AccountIT extends PebbleITBase {
*/
@Test
public void testNotExisting() throws AcmeException {
try {
AcmeServerException ex = assertThrows(AcmeServerException.class, () -> {
KeyPair keyPair = createKeyPair();
Session session = new Session(pebbleURI());
new AccountBuilder().onlyExisting().useKeyPair(keyPair).create(session);
fail("onlyExisting flag was ignored");
} catch (AcmeServerException ex) {
assertThat(ex.getType(), is(URI.create("urn:ietf:params:acme:error:accountDoesNotExist")));
}
});
assertThat(ex.getType(), is(URI.create("urn:ietf:params:acme:error:accountDoesNotExist")));
}
/**
@@ -193,14 +192,11 @@ public class AccountIT extends PebbleITBase {
KeyPair newKeyPair = createKeyPair();
acct.changeKey(newKeyPair);
try {
assertThrows("Old account key is still accessible", AcmeServerException.class, () -> {
Session sessionOldKey = new Session(pebbleURI());
Account oldAccount = sessionOldKey.login(location, keyPair).getAccount();
oldAccount.update();
fail("Old account key is still accessible");
} catch (AcmeServerException ex) {
// Expected
}
});
Session sessionNewKey = new Session(pebbleURI());
Account newAccount = sessionNewKey.login(location, newKeyPair).getAccount();
@@ -227,14 +223,13 @@ public class AccountIT extends PebbleITBase {
assertThat(acct.getStatus(), is(Status.DEACTIVATED));
// Make sure account cannot be accessed any more...
try {
AcmeUnauthorizedException ex = assertThrows("Account can still be accessed",
AcmeUnauthorizedException.class, () -> {
Session session2 = new Session(pebbleURI());
Account acct2 = session2.login(location, keyPair).getAccount();
acct2.update();
fail("Account can still be accessed");
} catch (AcmeUnauthorizedException ex) {
assertThat(ex.getMessage(), is("Account has been deactivated"));
}
});
assertThat(ex.getMessage(), is("Account has been deactivated"));
}
}

View File

@@ -17,6 +17,7 @@ import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import java.net.URI;
@@ -42,6 +43,7 @@ import org.shredzone.acme4j.challenge.Http01Challenge;
import org.shredzone.acme4j.challenge.TlsAlpn01Challenge;
import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.exception.AcmeServerException;
import org.shredzone.acme4j.exception.AcmeUnauthorizedException;
import org.shredzone.acme4j.it.BammBammClient;
import org.shredzone.acme4j.util.CSRBuilder;
@@ -222,22 +224,17 @@ public class OrderIT extends PebbleITBase {
revoker.revoke(session, certificate, keyPair, domainKeyPair);
// Make sure certificate is revoked
try {
AcmeException ex = assertThrows("Could download revoked cert", AcmeException.class, () -> {
Login login2 = session.login(account.getLocation(), keyPair);
Certificate cert2 = login2.bindCertificate(certificate.getLocation());
cert2.download();
fail("Could download revoked cert");
} catch (AcmeException ex) {
assertThat(ex.getMessage(), is("HTTP 404: Not Found"));
}
});
assertThat(ex.getMessage(), is("HTTP 404: Not Found"));
// Try to revoke again
try {
certificate.revoke();
fail("Could revoke again");
} catch (AcmeServerException ex) {
assertThat(ex.getProblem().getType(), is(URI.create("urn:ietf:params:acme:error:alreadyRevoked")));
}
AcmeServerException ex2 = assertThrows("Could revoke again", AcmeServerException.class,
() -> certificate.revoke());
assertThat(ex2.getProblem().getType(), is(URI.create("urn:ietf:params:acme:error:alreadyRevoked")));
}
/**