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 @Test public void testBinding() {
34 try {
35 ContextClassLoaderBinder.unbind();
36 fail("unbind succeeded without any prior bind");
37 } catch (IllegalStateException ise) {
38
39 }
40
41 ClassLoader cl0 = new URLClassLoader(new URL[] {});
42 ClassLoader cl1 = new URLClassLoader(new URL[] {});
43 ClassLoader cl2 = new URLClassLoader(new URL[] {});
44
45 ClassLoader original = Thread.currentThread().getContextClassLoader();
46 ContextClassLoaderBinder.bind(cl0);
47 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
48 ContextClassLoaderBinder.unbind();
49 assertEquals(original, Thread.currentThread().getContextClassLoader());
50
51 ContextClassLoaderBinder.bind(cl0);
52 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
53 ContextClassLoaderBinder.bind(cl1);
54 assertEquals(cl1, Thread.currentThread().getContextClassLoader());
55 ContextClassLoaderBinder.unbind();
56 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
57 ContextClassLoaderBinder.unbind();
58 assertEquals(original, Thread.currentThread().getContextClassLoader());
59
60 ContextClassLoaderBinder.bind(cl0);
61 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
62 ContextClassLoaderBinder.bind(cl1);
63 assertEquals(cl1, Thread.currentThread().getContextClassLoader());
64 ContextClassLoaderBinder.bind(cl2);
65 assertEquals(cl2, Thread.currentThread().getContextClassLoader());
66 ContextClassLoaderBinder.unbind();
67 assertEquals(cl1, Thread.currentThread().getContextClassLoader());
68 ContextClassLoaderBinder.unbind();
69 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
70 ContextClassLoaderBinder.unbind();
71 assertEquals(original, Thread.currentThread().getContextClassLoader());
72 }
73 }