Repository navigation
Expand file tree
/
Copy pathClasspathJarResolverTest.java
More file actions
89 lines (79 loc) · 2.84 KB
/
Copy pathClasspathJarResolverTest.java
File metadata and controls
89 lines (79 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.github.mustachejava.resolver;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.Reader;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
public class ClasspathJarResolverTest {
@Rule
public TemporaryFolder temporary = new TemporaryFolder();
@Test
public void readsFileWhenClassLoaderAlsoResolvesTrailingSlash() throws Exception {
assertResource("templates/view.mustache", true, "hello");
}
@Test
public void readsFileWithOrdinaryJarClassLoader() throws Exception {
assertResource("templates/view.mustache", false, "hello");
}
@Test
public void rejectsDirectoryWithoutTrailingSlash() throws Exception {
assertResource("templates", false, null);
assertResource("templates", true, null);
}
@Test
public void rejectsDirectoryWithTrailingSlash() throws Exception {
assertResource("templates/", false, null);
assertResource("templates/", true, null);
}
@Test
public void missingJarResourceReturnsNull() throws Exception {
assertResource("missing.mustache", false, null);
assertResource("missing.mustache", true, null);
}
private void assertResource(String resource, boolean normalizeTrailingSlash, String expected)
throws Exception {
File jar = temporary.newFile();
try (JarOutputStream output = new JarOutputStream(new FileOutputStream(jar))) {
output.putNextEntry(new JarEntry("templates/"));
output.closeEntry();
output.putNextEntry(new JarEntry("templates/view.mustache"));
output.write("hello".getBytes(StandardCharsets.UTF_8));
output.closeEntry();
}
ClassLoader original = Thread.currentThread().getContextClassLoader();
try (URLClassLoader loader = new URLClassLoader(new URL[] {jar.toURI().toURL()}, null) {
@Override
public URL getResource(String name) {
if (normalizeTrailingSlash && name.endsWith("/")) {
name = name.substring(0, name.length() - 1);
}
return super.getResource(name);
}
}) {
Thread.currentThread().setContextClassLoader(loader);
try (Reader reader = new ClasspathResolver().getReader(resource)) {
if (expected == null) {
assertNull(reader);
} else {
assertNotNull(reader);
StringBuilder text = new StringBuilder();
for (int c; (c = reader.read()) != -1;) {
text.append((char) c);
}
assertEquals(expected, text.toString());
}
}
} finally {
Thread.currentThread().setContextClassLoader(original);
}
}
}