View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.kuali.rice.kew.plugin;
19  
20  import java.net.URL;
21  import java.net.URLClassLoader;
22  
23  import junit.framework.AssertionFailedError;
24  import junit.framework.TestCase;
25  
26  import org.junit.Test;
27  import org.kuali.rice.core.resourceloader.ContextClassLoaderBinder;
28  
29  /**
30   * Tests the ContextClassLoaderBinder
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class ContextClassLoaderBinderTest extends TestCase {
34      
35  	@Test public void testBinding() {
36          try {
37              ContextClassLoaderBinder.unbind();
38              throw new AssertionFailedError("unbind succeeded without any prior bind");
39          } catch (IllegalStateException ise) {
40              // expect illegal state
41          }
42  
43          ClassLoader cl0 = new URLClassLoader(new URL[] {});
44          ClassLoader cl1 = new URLClassLoader(new URL[] {});
45          ClassLoader cl2 = new URLClassLoader(new URL[] {});
46  
47          ClassLoader original = Thread.currentThread().getContextClassLoader();
48          ContextClassLoaderBinder.bind(cl0);
49          assertEquals(cl0, Thread.currentThread().getContextClassLoader());
50          ContextClassLoaderBinder.unbind();
51          assertEquals(original, Thread.currentThread().getContextClassLoader());
52  
53          ContextClassLoaderBinder.bind(cl0);
54          assertEquals(cl0, Thread.currentThread().getContextClassLoader());
55          ContextClassLoaderBinder.bind(cl1);
56          assertEquals(cl1, Thread.currentThread().getContextClassLoader());
57          ContextClassLoaderBinder.unbind();
58          assertEquals(cl0, Thread.currentThread().getContextClassLoader());
59          ContextClassLoaderBinder.unbind();
60          assertEquals(original, Thread.currentThread().getContextClassLoader());
61  
62          ContextClassLoaderBinder.bind(cl0);
63          assertEquals(cl0, Thread.currentThread().getContextClassLoader());
64          ContextClassLoaderBinder.bind(cl1);
65          assertEquals(cl1, Thread.currentThread().getContextClassLoader());
66          ContextClassLoaderBinder.bind(cl2);
67          assertEquals(cl2, Thread.currentThread().getContextClassLoader());
68          ContextClassLoaderBinder.unbind();
69          assertEquals(cl1, Thread.currentThread().getContextClassLoader());
70          ContextClassLoaderBinder.unbind();
71          assertEquals(cl0, Thread.currentThread().getContextClassLoader());
72          ContextClassLoaderBinder.unbind();
73          assertEquals(original, Thread.currentThread().getContextClassLoader());
74      }
75  }