Skip to content

Commit

Permalink
Fix ClasspathResolver for non-context class loader case
Browse files Browse the repository at this point in the history
Broke the case where the context class loader can't actually find the resource and the fallback has to be used. Also added a test case to catch this specific regression.
  • Loading branch information
ianlevesque authored and spullara committed May 2, 2024
1 parent 634b460 commit adb82eb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Reader getReader(String resourceName) {
String normalizeResourceName = URI.create(fullResourceName).normalize().getPath();

URL resource = ccl.getResource(normalizeResourceName);
if (resource != null)
if (resource != null) {
if (resource.getProtocol().equals("jar")) {
if (normalizeResourceName.endsWith("/")) {
// This is a directory
Expand All @@ -50,8 +50,10 @@ public Reader getReader(String resourceName) {
return null;
}
}
else
} else {
resource = ClasspathResolver.class.getClassLoader().getResource(normalizeResourceName);
}


if (resource != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.junit.Test;

import java.io.Reader;
import java.net.URL;
import java.net.URLClassLoader;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -125,4 +127,19 @@ public void getReaderWithRootAndResourceAboveRoot() throws Exception {
assertNotNull(reader);
}
}

@Test
public void getReaderWithoutContextClassLoader() throws Exception {
ClassLoader savedContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[]{}, null));

ClasspathResolver underTest = new ClasspathResolver();
try (Reader reader = underTest.getReader("template.mustache")) {
assertNotNull(reader);
}
} finally {
Thread.currentThread().setContextClassLoader(savedContextClassLoader);
}
}
}

0 comments on commit adb82eb

Please sign in to comment.