Skip to content

Commit adb82eb

Browse files
ianlevesquespullara
authored andcommitted
Fix ClasspathResolver for non-context class loader case
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.
1 parent 634b460 commit adb82eb

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

compiler/src/main/java/com/github/mustachejava/resolver/ClasspathResolver.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Reader getReader(String resourceName) {
3535
String normalizeResourceName = URI.create(fullResourceName).normalize().getPath();
3636

3737
URL resource = ccl.getResource(normalizeResourceName);
38-
if (resource != null)
38+
if (resource != null) {
3939
if (resource.getProtocol().equals("jar")) {
4040
if (normalizeResourceName.endsWith("/")) {
4141
// This is a directory
@@ -50,8 +50,10 @@ public Reader getReader(String resourceName) {
5050
return null;
5151
}
5252
}
53-
else
53+
} else {
5454
resource = ClasspathResolver.class.getClassLoader().getResource(normalizeResourceName);
55+
}
56+
5557

5658
if (resource != null) {
5759
try {

compiler/src/test/java/com/github/mustachejava/resolver/ClasspathResolverTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.junit.Test;
44

55
import java.io.Reader;
6+
import java.net.URL;
7+
import java.net.URLClassLoader;
68

79
import static org.junit.Assert.assertNotNull;
810
import static org.junit.Assert.assertNull;
@@ -125,4 +127,19 @@ public void getReaderWithRootAndResourceAboveRoot() throws Exception {
125127
assertNotNull(reader);
126128
}
127129
}
130+
131+
@Test
132+
public void getReaderWithoutContextClassLoader() throws Exception {
133+
ClassLoader savedContextClassLoader = Thread.currentThread().getContextClassLoader();
134+
try {
135+
Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[]{}, null));
136+
137+
ClasspathResolver underTest = new ClasspathResolver();
138+
try (Reader reader = underTest.getReader("template.mustache")) {
139+
assertNotNull(reader);
140+
}
141+
} finally {
142+
Thread.currentThread().setContextClassLoader(savedContextClassLoader);
143+
}
144+
}
128145
}

0 commit comments

Comments
 (0)