diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/impl/DefaultConnectionTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/impl/DefaultConnectionTest.java index bb8a1e8b..8f527644 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/impl/DefaultConnectionTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/impl/DefaultConnectionTest.java @@ -22,12 +22,10 @@ import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; import java.security.KeyPair; -import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.util.HashMap; import java.util.HashSet; @@ -386,11 +384,7 @@ public class DefaultConnectionTest { */ @Test public void testReadCertificate() throws Exception { - CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); - X509Certificate original; - try (InputStream cert = getClass().getResourceAsStream("/cert.pem")) { - original = (X509Certificate) certificateFactory.generateCertificate(cert); - } + X509Certificate original = TestUtils.createCertificate(); when(mockUrlConnection.getHeaderField("Content-Type")).thenReturn("application/pkix-cert"); when(mockUrlConnection.getInputStream()).thenReturn(new ByteArrayInputStream(original.getEncoded())); diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/util/TestUtils.java b/acme4j-client/src/test/java/org/shredzone/acme4j/util/TestUtils.java index ab09460c..2bdb2d11 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/util/TestUtils.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/util/TestUtils.java @@ -26,6 +26,9 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; +import java.security.cert.CertificateException; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; @@ -133,6 +136,21 @@ public final class TestUtils { } } + /** + * Creates a standard certificate for testing. This certificate is read from a test + * resource and is guaranteed not to change between test runs. + * + * @return {@link X509Certificate} for testing + */ + public static X509Certificate createCertificate() throws IOException { + try (InputStream cert = TestUtils.class.getResourceAsStream("/cert.pem")) { + CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); + return (X509Certificate) certificateFactory.generateCertificate(cert); + } catch (CertificateException ex) { + throw new IOException(ex); + } + } + /** * Generates a new keypair for unit tests, and return its N, E, KTY and THUMBPRINT * parameters to be set in the {@link TestUtils} class.