1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.util;
23
24 import static java.util.Arrays.asList;
25 import static net.grinder.testutility.FileUtilities.createFile;
26 import static net.grinder.util.ClassLoaderUtilities.allResourceLines;
27 import static net.grinder.util.ClassLoaderUtilities.loadRegisteredImplementations;
28 import static net.grinder.testutility.AssertUtilities.assertContains;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
32
33 import java.io.File;
34 import java.net.URL;
35 import java.net.URLClassLoader;
36 import java.util.Collections;
37 import java.util.HashSet;
38 import java.util.List;
39
40 import net.grinder.engine.common.EngineException;
41 import net.grinder.testutility.AbstractJUnit4FileTestCase;
42
43 import org.junit.Test;
44
45
46
47
48
49
50
51 public class TestClassLoaderUtilities extends AbstractJUnit4FileTestCase {
52
53 @Test public void testAllResourceLinesNoResources() throws Exception {
54 final List<String> resourceLines =
55 allResourceLines(getClass().getClassLoader(), "test/foo");
56
57 assertEquals(0, resourceLines.size());
58 }
59
60 @Test public void testAllResourceLinesResources() throws Exception {
61 final File d1 = new File(getDirectory(), "one");
62 createFile(new File(d1, "test/foo"),
63 "a",
64 "# Some comment",
65 "b",
66 "");
67
68 final File d2 = new File(getDirectory(), "two");
69 createFile(new File(d2, "test/foo"),
70 "a",
71 "c # Another comment");
72
73 final ClassLoader cl1 =
74 new URLClassLoader(new URL[] { d1.toURI().toURL() });
75 final ClassLoader cl2 =
76 new URLClassLoader(new URL[] { d1.toURI().toURL(),
77 d2.toURI().toURL()});
78
79 assertEquals(asList("a", "b"), allResourceLines(cl1, "test/foo"));
80
81 assertEquals(asList("a", "b", "a", "c"), allResourceLines(cl2, "test/foo"));
82 }
83
84 @Test public void filterDuplicateURLs() throws Exception {
85
86 final File d1 = new File(getDirectory(), "one");
87 createFile(new File(d1, "test/foo"),
88 "a");
89
90 final URLClassLoader cl1 =
91 new URLClassLoader(new URL[] { d1.toURI().toURL() });
92
93 final URLClassLoader cl2 = new URLClassLoader(cl1.getURLs(), cl1);
94
95 assertEquals(asList("a"), allResourceLines(cl2, "test/foo"));
96 }
97
98 @Test public void testUnknownImplementation() throws Exception {
99
100 final File path = new File(getDirectory(), "cp");
101
102 final String RESOURCE_NAME = "my.resource";
103
104 createFile(new File(path, RESOURCE_NAME), "bobbins");
105
106 final List<URL> additionalClasspath =
107 asList(new File(getDirectory(), "cp").toURI().toURL());
108
109 final ClassLoader blockingLoader =
110 new BlockingClassLoader(
111 additionalClasspath,
112 Collections.<String>emptySet(),
113 new HashSet<String>(
114 asList(RESOURCE_NAME)),
115 Collections.<String>emptySet(),
116 true);
117
118 try {
119 loadRegisteredImplementations(RESOURCE_NAME,
120 Object.class,
121 blockingLoader);
122 fail("Expected EngineException");
123 }
124 catch (EngineException e) {
125 assertTrue(e.getCause() instanceof ClassNotFoundException);
126 }
127 }
128
129 @Test public void testBadImplementation() throws Exception {
130
131 final File path = new File(getDirectory(), "cp");
132
133 final String RESOURCE_NAME = "my.resource";
134
135 createFile(new File(path, RESOURCE_NAME), "java.lang.Object");
136
137 final List<URL> additionalClasspath =
138 asList(new File(getDirectory(), "cp").toURI().toURL());
139
140 final ClassLoader blockingLoader =
141 new BlockingClassLoader(
142 additionalClasspath,
143 Collections.<String>emptySet(),
144 new HashSet<String>(
145 asList(RESOURCE_NAME)),
146 Collections.<String>emptySet(),
147 true);
148
149 try {
150 loadRegisteredImplementations(RESOURCE_NAME,
151 Integer.class,
152 blockingLoader);
153 fail("Expected EngineException");
154 }
155 catch (EngineException e) {
156 assertContains(e.getMessage(), "does not implement");
157 }
158 }
159
160 @SuppressWarnings("unchecked")
161 @Test public void testGoodImplementations() throws Exception {
162
163 final File path = new File(getDirectory(), "cp");
164
165 final String RESOURCE_NAME = "my.resource";
166
167 createFile(new File(path, RESOURCE_NAME),
168 "java.lang.Integer",
169 "# hello",
170 "java.lang.Long",
171 "java.lang.Long",
172 "java.lang.Integer");
173
174 final List<URL> additionalClasspath =
175 asList(new File(getDirectory(), "cp").toURI().toURL());
176
177 final ClassLoader blockingLoader =
178 new BlockingClassLoader(
179 additionalClasspath,
180 Collections.<String>emptySet(),
181 new HashSet<String>(
182 asList(RESOURCE_NAME)),
183 Collections.<String>emptySet(),
184 true);
185
186 final List<Class<? extends Number>> classes =
187 loadRegisteredImplementations(RESOURCE_NAME,
188 Number.class,
189 blockingLoader);
190
191
192 assertEquals(asList(Integer.class, Long.class), classes);
193 }
194 }