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 @Test public void testBinding() {
034 try {
035 ContextClassLoaderBinder.unbind();
036 fail("unbind succeeded without any prior bind");
037 } catch (IllegalStateException ise) {
038 // expect illegal state
039 }
040
041 ClassLoader cl0 = new URLClassLoader(new URL[] {});
042 ClassLoader cl1 = new URLClassLoader(new URL[] {});
043 ClassLoader cl2 = new URLClassLoader(new URL[] {});
044
045 ClassLoader original = Thread.currentThread().getContextClassLoader();
046 ContextClassLoaderBinder.bind(cl0);
047 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
048 ContextClassLoaderBinder.unbind();
049 assertEquals(original, Thread.currentThread().getContextClassLoader());
050
051 ContextClassLoaderBinder.bind(cl0);
052 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
053 ContextClassLoaderBinder.bind(cl1);
054 assertEquals(cl1, Thread.currentThread().getContextClassLoader());
055 ContextClassLoaderBinder.unbind();
056 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
057 ContextClassLoaderBinder.unbind();
058 assertEquals(original, Thread.currentThread().getContextClassLoader());
059
060 ContextClassLoaderBinder.bind(cl0);
061 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
062 ContextClassLoaderBinder.bind(cl1);
063 assertEquals(cl1, Thread.currentThread().getContextClassLoader());
064 ContextClassLoaderBinder.bind(cl2);
065 assertEquals(cl2, Thread.currentThread().getContextClassLoader());
066 ContextClassLoaderBinder.unbind();
067 assertEquals(cl1, Thread.currentThread().getContextClassLoader());
068 ContextClassLoaderBinder.unbind();
069 assertEquals(cl0, Thread.currentThread().getContextClassLoader());
070 ContextClassLoaderBinder.unbind();
071 assertEquals(original, Thread.currentThread().getContextClassLoader());
072 }
073 }