View Javadoc

1   // Copyright (C) 2005 - 2011 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Unit tests for {@link BlockingClassLoader}.
41   *
42   * @author Philip Aston
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 }