View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Tests the ContextClassLoaderBinder
29   * @author Kuali Rice Team (rice.collab@kuali.org)
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              // expect illegal state
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  }