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.Collections.singleton;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31
32 import java.net.URL;
33 import java.net.URLClassLoader;
34 import java.util.Collections;
35
36 import org.junit.Test;
37
38
39
40
41
42
43
44 public class TestBlockingClassLoader {
45
46 @Test public void testWithBootLoaderClass() throws Exception {
47 final BlockingClassLoader cl =
48 new BlockingClassLoader(singleton(String.class.getName()),
49 Collections.<String>emptySet(),
50 Collections.<String>emptySet(),
51 true);
52
53 assertSame(String.class, Class.forName("java.lang.String", false, cl));
54 }
55
56 @Test public void testWithBootLoaderClass2() throws Exception {
57
58 final BlockingClassLoader cl =
59 new BlockingClassLoader(singleton(String.class.getName()),
60 Collections.<String>emptySet(),
61 Collections.<String>emptySet(),
62 false);
63
64 try {
65 Class.forName("java.lang.String", false, cl);
66 fail("Expected ClassNotFoundException");
67 }
68 catch (ClassNotFoundException e) {
69 }
70 }
71
72 @Test public void testWithLoadableClass() throws Exception {
73 final BlockingClassLoader cl =
74 new BlockingClassLoader(Collections.<String>emptySet(),
75 singleton("net.grinder.*"),
76 Collections.<String>emptySet(),
77 false);
78
79 final String name = "net.grinder.util.AnIsolatedClass";
80
81 final Class<?> c = cl.loadClass(name, true);
82
83 assertSame(c, Class.forName(name, false, cl));
84 }
85
86 @Test public void testGetResource() throws Exception {
87 final String resourceName = "TestResources.properties";
88
89 assertNotNull(getClass().getClassLoader().getResource(resourceName));
90
91 final BlockingClassLoader cl =
92 new BlockingClassLoader(singleton(resourceName),
93 Collections.<String>emptySet(),
94 Collections.<String>emptySet(),
95 false);
96
97 assertNull(cl.getResource(resourceName));
98 assertNull(cl.getResource("Something else"));
99 }
100
101 @Test public void testGetResource2() throws Exception {
102 final String resourceName = "TestResources.properties";
103
104 assertNotNull(getClass().getClassLoader().getResource(resourceName));
105
106 final URLClassLoader grandParentLoader =
107 (URLClassLoader)getClass().getClassLoader();
108
109 final URLClassLoader parentLoader =
110 new URLClassLoader(grandParentLoader.getURLs(), grandParentLoader);
111
112 final BlockingClassLoader cl =
113 new BlockingClassLoader(parentLoader,
114 Collections.<URL>emptyList(),
115 singleton(resourceName),
116 Collections.<String>emptySet(),
117 Collections.<String>emptySet(),
118 true);
119
120 assertNotNull(cl.getResource(resourceName));
121 assertNull(cl.getResource("Something else"));
122 }
123
124 @Test public void testGetResource3() throws Exception {
125 final String resourceName = "TestResources.properties";
126
127 assertNotNull(getClass().getClassLoader().getResource(resourceName));
128
129 final BlockingClassLoader cl =
130 new BlockingClassLoader(singleton(resourceName),
131 singleton(resourceName),
132 singleton(resourceName),
133 false);
134
135 assertNotNull(cl.getResource(resourceName));
136 assertNull(cl.getResource("Something else"));
137 }
138
139 @Test public void testGetResources() throws Exception {
140 final String resourceName = "TestResources.properties";
141
142 assertTrue(
143 getClass().getClassLoader().getResources(resourceName).hasMoreElements());
144
145 final BlockingClassLoader cl =
146 new BlockingClassLoader(singleton(resourceName),
147 Collections.<String>emptySet(),
148 Collections.<String>emptySet(),
149 false);
150
151 assertFalse(cl.getResources(resourceName).hasMoreElements());
152
153 assertFalse(cl.getResources("Something else").hasMoreElements());
154 }
155
156 @Test public void testGetResources2() throws Exception {
157 final String resourceName = "TestResources.properties";
158
159 assertTrue(
160 getClass().getClassLoader().getResources(resourceName).hasMoreElements());
161
162 final BlockingClassLoader cl =
163 new BlockingClassLoader(Collections.<String>emptySet(),
164 Collections.<String>emptySet(),
165 singleton(resourceName),
166 false);
167
168 assertTrue(cl.getResources(resourceName).hasMoreElements());
169 }
170 }