View Javadoc

1   package com.atlassian.plugin.servlet.util;
2   
3   import junit.framework.TestCase;
4   
5   import java.net.URISyntaxException;
6   
7   public class TestClassLoaderStack extends TestCase
8   {
9       public void testThreadClassLoaderIsReplacedAndRestored() throws URISyntaxException
10      {
11          ClassLoader mainLoader = Thread.currentThread().getContextClassLoader();
12          try
13          {
14              ClassLoader pluginLoader1 = new MockClassLoader();
15              ClassLoader pluginLoader2 = new MockClassLoader();
16  
17              ClassLoaderStack.push(pluginLoader1);
18              assertSame(pluginLoader1, Thread.currentThread().getContextClassLoader());
19              ClassLoaderStack.push(pluginLoader2);
20              assertSame(pluginLoader2, Thread.currentThread().getContextClassLoader());
21              ClassLoaderStack.pop();
22              assertSame(pluginLoader1, Thread.currentThread().getContextClassLoader());
23              ClassLoaderStack.pop();
24              assertSame(mainLoader, Thread.currentThread().getContextClassLoader());
25          }
26          finally
27          {
28              Thread.currentThread().setContextClassLoader(mainLoader);
29          }
30      }
31  
32      public void testPopReturnsPreviousContextClassLoader() throws Exception
33      {
34          ClassLoader mainLoader = Thread.currentThread().getContextClassLoader();
35          try
36          {
37              ClassLoader pluginLoader1 = new MockClassLoader();
38              ClassLoader pluginLoader2 = new MockClassLoader();
39  
40              ClassLoaderStack.push(pluginLoader1);
41              assertSame(pluginLoader1, Thread.currentThread().getContextClassLoader());
42              ClassLoaderStack.push(pluginLoader2);
43              assertSame(pluginLoader2, Thread.currentThread().getContextClassLoader());
44              ClassLoader previous = ClassLoaderStack.pop();
45              assertSame(pluginLoader2, previous);
46              assertSame(pluginLoader1, Thread.currentThread().getContextClassLoader());
47              previous = ClassLoaderStack.pop();
48              assertSame(pluginLoader1, previous);
49              assertSame(mainLoader, Thread.currentThread().getContextClassLoader());
50          }
51          finally
52          {
53              // Clean up in case of error
54              Thread.currentThread().setContextClassLoader(mainLoader);
55          }
56      }
57  
58      public void testPushAndPopHandleNull() throws Exception
59      {
60          ClassLoader mainLoader = Thread.currentThread().getContextClassLoader();
61          try
62          {
63              // popping with empty stack should return null
64              assertNull(ClassLoaderStack.pop());
65              // pushing null should be a no-op
66              ClassLoaderStack.push(null);
67              assertSame(mainLoader, Thread.currentThread().getContextClassLoader());
68              assertNull(ClassLoaderStack.pop());
69          }
70          finally
71          {
72              // Clean up in case of error
73              Thread.currentThread().setContextClassLoader(mainLoader);
74          }
75      }
76  
77      public static class MockClassLoader extends ClassLoader
78      {
79      }
80  }