View Javadoc
1   /**
2    * Copyright 2005-2014 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      /**
34       * @deprecated {@link ContextClassLoaderBinder#doInContextClassLoader(ClassLoader, java.util.concurrent.Callable)} is the safe way to
35       * run code under a specific Context ClassLoader that ensures the classloader is always set and unset properly.
36       */
37  	@Test public void testBinding() {
38          try {
39              ContextClassLoaderBinder.unbind();
40              fail("unbind succeeded without any prior bind");
41          } catch (IllegalStateException ise) {
42              // expect illegal state
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  }