From d986e9d2171d1a28ac42de4cb73edfff01757806 Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Wed, 30 Oct 2024 14:42:41 -0500 Subject: [PATCH] Add test for path resource loader URL connection --- .../resource/PathResourceLoaderTests.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 resource/src/test/java/io/smallrye/common/resource/PathResourceLoaderTests.java diff --git a/resource/src/test/java/io/smallrye/common/resource/PathResourceLoaderTests.java b/resource/src/test/java/io/smallrye/common/resource/PathResourceLoaderTests.java new file mode 100644 index 0000000..9ca7c1c --- /dev/null +++ b/resource/src/test/java/io/smallrye/common/resource/PathResourceLoaderTests.java @@ -0,0 +1,39 @@ +package io.smallrye.common.resource; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.file.Path; + +import org.junit.jupiter.api.Test; + +public final class PathResourceLoaderTests { + + @Test + public void testLoading() throws IOException { + URL myClass = PathResourceLoaderTests.class.getResource("PathResourceLoaderTests.class"); + assumeTrue(myClass != null); + assumeTrue("file".equals(myClass.getProtocol())); + String absPath = myClass.getPath(); + Path testClasses = Path.of(absPath).getParent().getParent().getParent().getParent().getParent(); + byte[] myClassBytes; + try (InputStream is = myClass.openStream()) { + assumeTrue(is != null); + myClassBytes = is.readAllBytes(); + } + try (PathResourceLoader rl = new PathResourceLoader(testClasses)) { + Resource myClassRsrc = rl.findResource("io/smallrye/common/resource/PathResourceLoaderTests.class"); + assertNotNull(myClassRsrc); + try (InputStream is = myClassRsrc.openStream()) { + assertArrayEquals(myClassBytes, is.readAllBytes()); + } + URL url = myClassRsrc.url(); + assertInstanceOf(ResourceURLConnection.class, url.openConnection()); + } + } +}