001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.beanutils.converters;
019
020 import junit.framework.TestCase;
021 import junit.framework.TestSuite;
022
023 /**
024 * Tests for the ClassReloader utility class.
025 */
026
027 public class ClassReloaderTestCase extends TestCase {
028
029 // ------------------------------------------------------------------------
030
031 public ClassReloaderTestCase(String name) {
032 super(name);
033 }
034
035
036 public static TestSuite suite() {
037 return new TestSuite(ClassReloaderTestCase.class);
038 }
039
040 // ------------------------------------------------------------------------
041
042 public static class DummyClass {
043 }
044
045 /**
046 * Test basic operation of the ClassReloader.
047 */
048 public void testBasicOperation() throws Exception {
049 ClassLoader sharedLoader = this.getClass().getClassLoader();
050 ClassReloader componentLoader = new ClassReloader(sharedLoader);
051
052 Class sharedClass = DummyClass.class;
053 Class componentClass = componentLoader.reload(sharedClass);
054
055 // the two Class objects contain the same bytecode, but are not equal
056 assertTrue(sharedClass != componentClass);
057
058 // the two class objects have different classloaders
059 assertSame(sharedLoader, sharedClass.getClassLoader());
060 assertSame(componentLoader, componentClass.getClassLoader());
061 assertTrue(sharedLoader != componentLoader);
062
063 // verify that objects of these two types are not assignment-compatible
064 Object obj1 = sharedClass.newInstance();
065 Object obj2 = componentClass.newInstance();
066
067 assertTrue("Obj1 class incorrect", sharedClass.isInstance(obj1));
068 assertFalse("Obj1 class incorrect", componentClass.isInstance(obj1));
069 assertFalse("Obj2 class incorrect", sharedClass.isInstance(obj2));
070 assertTrue("Obj2 class incorrect", componentClass.isInstance(obj2));
071 }
072
073 }
074