1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.plugin;
17
18 import org.junit.Test;
19 import org.kuali.rice.core.api.util.ContextClassLoaderBinder;
20
21 import java.net.URL;
22 import java.net.URLClassLoader;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26
27
28
29
30
31 public class ContextClassLoaderBinderTest {
32
33
34
35
36
37 @Test public void testBinding() {
38 try {
39 ContextClassLoaderBinder.unbind();
40 fail("unbind succeeded without any prior bind");
41 } catch (IllegalStateException ise) {
42
43 }
44
45 ClassLoader cl0 = new URLClassLoader(new URL[] {});
46 ClassLoader cl1 = new URLClassLoader(new URL[] {});
47 ClassLoader cl2 = new URLClassLoader(new URL[] {});
48
49 ClassLoader original = Thread.currentThread().getContextClassLoader();
50 ContextClassLoaderBinder.bind(cl0);
51 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
52 ContextClassLoaderBinder.unbind();
53 assertEquals(original, Thread.currentThread().getContextClassLoader());
54
55 ContextClassLoaderBinder.bind(cl0);
56 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
57 ContextClassLoaderBinder.bind(cl1);
58 assertEquals(cl1, Thread.currentThread().getContextClassLoader());
59 ContextClassLoaderBinder.unbind();
60 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
61 ContextClassLoaderBinder.unbind();
62 assertEquals(original, Thread.currentThread().getContextClassLoader());
63
64 ContextClassLoaderBinder.bind(cl0);
65 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
66 ContextClassLoaderBinder.bind(cl1);
67 assertEquals(cl1, Thread.currentThread().getContextClassLoader());
68 ContextClassLoaderBinder.bind(cl2);
69 assertEquals(cl2, Thread.currentThread().getContextClassLoader());
70 ContextClassLoaderBinder.unbind();
71 assertEquals(cl1, Thread.currentThread().getContextClassLoader());
72 ContextClassLoaderBinder.unbind();
73 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
74 ContextClassLoaderBinder.unbind();
75 assertEquals(original, Thread.currentThread().getContextClassLoader());
76 }
77 }