Skip to content

Commit

Permalink
Merge pull request #11716 from jaikiran/qk-11707
Browse files Browse the repository at this point in the history
Correctly handle directories in getResourceAsStream of QuarkusClassLoader
  • Loading branch information
jaikiran committed Sep 2, 2020
2 parents 953c5b2 + 96de5bf commit a4f910f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ public InputStream getResourceAsStream(String unsanitisedName) {
for (ClassPathElement i : elements) {
ClassPathResource res = i.getResource(name);
if (res != null) {
if (res.isDirectory()) {
try {
return res.getUrl().openStream();
} catch (IOException e) {
log.debug("Ignoring exception that occurred while opening a stream for resource " + unsanitisedName,
e);
// behave like how java.lang.ClassLoader#getResourceAsStream() behaves
// and don't propagate the exception
continue;
}
}
return new ByteArrayInputStream(res.getData());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.quarkus.bootstrap.classloading.JarClassPathElement;
import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.bootstrap.util.IoUtils;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -51,6 +52,36 @@ public void testUrlReturnedFromClassLoaderDirectory() throws Exception {
}
}

/**
* Test that {@link QuarkusClassLoader#getResourceAsStream(String)} returns a stream for directory
* resources
*
* @throws Exception
* @see <a href="https://github.com/quarkusio/quarkus/issues/11707"/>
*/
@Test
public void testResourceAsStreamForDirectory() throws Exception {
final JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
.add(new StringAsset("a"), "a.txt")
.add(new StringAsset("b"), "b/b.txt");
final Path tmpDir = Files.createTempDirectory("test");
try {
jar.as(ExplodedExporter.class).exportExploded(tmpDir.toFile(), "tmpcltest");
final ClassLoader cl = QuarkusClassLoader.builder("test", getClass().getClassLoader(), false)
.addElement(new DirectoryClassPathElement(tmpDir.resolve("tmpcltest")))
.build();

try (final InputStream is = cl.getResourceAsStream("b/")) {
Assertions.assertNotNull(is, "InputStream is null for a directory resource");
}
try (final InputStream is = cl.getResourceAsStream("b")) {
Assertions.assertNotNull(is, "InputStream is null for a directory resource");
}
} finally {
IoUtils.recursiveDelete(tmpDir);
}
}

/**
* URLClassLoader will return URL's that end with a / if the call to getResource ends with a /
*/
Expand Down

0 comments on commit a4f910f

Please sign in to comment.