001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.kew.plugin;
017
018 import org.junit.Test;
019 import org.kuali.rice.core.api.util.ContextClassLoaderBinder;
020
021 import java.net.URL;
022 import java.net.URLClassLoader;
023
024 import static org.junit.Assert.assertEquals;
025 import static org.junit.Assert.fail;
026
027 /**
028 * Tests the ContextClassLoaderBinder
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031 public class ContextClassLoaderBinderTest {
032
033 /**
034 * @deprecated {@link ContextClassLoaderBinder#doInContextClassLoader(ClassLoader, java.util.concurrent.Callable)} is the safe way to
035 * run code under a specific Context ClassLoader that ensures the classloader is always set and unset properly.
036 */
037 @Test public void testBinding() {
038 try {
039 ContextClassLoaderBinder.unbind();
040 fail("unbind succeeded without any prior bind");
041 } catch (IllegalStateException ise) {
042 // expect illegal state
043 }
044
045 ClassLoader cl0 = new URLClassLoader(new URL[] {});
046 ClassLoader cl1 = new URLClassLoader(new URL[] {});
047 ClassLoader cl2 = new URLClassLoader(new URL[] {});
048
049 ClassLoader original = Thread.currentThread().getContextClassLoader();
050 ContextClassLoaderBinder.bind(cl0);
051 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
052 ContextClassLoaderBinder.unbind();
053 assertEquals(original, Thread.currentThread().getContextClassLoader());
054
055 ContextClassLoaderBinder.bind(cl0);
056 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
057 ContextClassLoaderBinder.bind(cl1);
058 assertEquals(cl1, Thread.currentThread().getContextClassLoader());
059 ContextClassLoaderBinder.unbind();
060 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
061 ContextClassLoaderBinder.unbind();
062 assertEquals(original, Thread.currentThread().getContextClassLoader());
063
064 ContextClassLoaderBinder.bind(cl0);
065 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
066 ContextClassLoaderBinder.bind(cl1);
067 assertEquals(cl1, Thread.currentThread().getContextClassLoader());
068 ContextClassLoaderBinder.bind(cl2);
069 assertEquals(cl2, Thread.currentThread().getContextClassLoader());
070 ContextClassLoaderBinder.unbind();
071 assertEquals(cl1, Thread.currentThread().getContextClassLoader());
072 ContextClassLoaderBinder.unbind();
073 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
074 ContextClassLoaderBinder.unbind();
075 assertEquals(original, Thread.currentThread().getContextClassLoader());
076 }
077 }