From 5a86d9db629e1c411fcdc47111201a08fefb3499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Charaoui?= Date: Thu, 21 Jul 2022 19:38:22 -0400 Subject: [PATCH] Fix tests that read from jar-embedded resources It seems that none of the functions used here actually require a File object as arguments, and will happily work on InputStream objects. This also changes readFromResource() to use InputStream.readAllBytes() instead of File.readAllBytes(), which is available from Java 9. --- .../key/parsing/KeyRingReaderTest.java | 37 +++---------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/parsing/KeyRingReaderTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/parsing/KeyRingReaderTest.java index b3fe66d4..23479a78 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/parsing/KeyRingReaderTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/parsing/KeyRingReaderTest.java @@ -14,7 +14,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.StandardCharsets; @@ -56,31 +55,9 @@ class KeyRingReaderTest { return inputStream; } - private URI getResourceURI(String resourceName) { - try { - URL url = getClass().getClassLoader().getResource(resourceName); - if (url == null) { - throw new TestAbortedException("Cannot read resource " + resourceName + ": URL is null."); - } - return url.toURI(); - } catch (URISyntaxException | IllegalArgumentException e) { - throw new TestAbortedException("Cannot read resource " + resourceName, e); - } - } - - private File getFileFromResource(String resourceName) { - URI uri = getResourceURI(resourceName); - try { - return new File(uri); - } catch (IllegalArgumentException e) { - // When executing the tests from pgpainless-test.jar, we cannot read resources as - // URI is not hierarchical. - throw new TestAbortedException("Cannot read resource " + resourceName, e); - } - } - private byte[] readFromResource(String resourceName) throws IOException { - return Files.readAllBytes(getFileFromResource(resourceName).toPath()); + InputStream inputStream = requireResource(resourceName); + return inputStream.readAllBytes(); } @Test @@ -260,23 +237,19 @@ class KeyRingReaderTest { assertEquals(10, getPGPKeyRingsFromResource("10_prv_and_pub_keys_binary.key").size()); } - private InputStream getFileInputStreamFromResource(String fileName) throws IOException { - return new FileInputStream(getFileFromResource(fileName)); - } - private PGPKeyRingCollection getPGPKeyRingsFromResource(String fileName) throws IOException, PGPException { - return PGPainless.readKeyRing().keyRingCollection(getFileInputStreamFromResource(fileName), true); + return PGPainless.readKeyRing().keyRingCollection(requireResource(fileName), true); } private PGPPublicKeyRingCollection getPgpPublicKeyRingsFromResource(String fileName) throws IOException, PGPException { - return PGPainless.readKeyRing().publicKeyRingCollection(getFileInputStreamFromResource(fileName)); + return PGPainless.readKeyRing().publicKeyRingCollection(requireResource(fileName)); } private PGPSecretKeyRingCollection getPgpSecretKeyRingsFromResource(String fileName) throws IOException, PGPException { - return PGPainless.readKeyRing().secretKeyRingCollection(getFileInputStreamFromResource(fileName)); + return PGPainless.readKeyRing().secretKeyRingCollection(requireResource(fileName)); } @Test