1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 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 package org.apache.commons.logging.pathable; 18 19 import java.net.URL; 20 import java.net.URLClassLoader; 21 22 import org.apache.commons.logging.PathableClassLoader; 23 import org.apache.commons.logging.PathableTestSuite; 24 25 import junit.framework.Test; 26 import junit.framework.TestCase; 27 28 /** 29 * Tests for the PathableTestSuite class. 30 */ 31 32 public class GeneralTestCase extends TestCase { 33 34 /** 35 * Verify that the context class loader is a custom one, then reset it to 36 * a non-custom one. 37 */ 38 private static void checkAndSetContext() { 39 final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); 40 assertEquals("ContextLoader is of unexpected type", 41 contextLoader.getClass().getName(), 42 PathableClassLoader.class.getName()); 43 44 final URL[] noUrls = {}; 45 Thread.currentThread().setContextClassLoader(new URLClassLoader(noUrls)); 46 } 47 48 /** 49 * Verify that a certain system property is not set, then set it. 50 */ 51 private static void checkAndSetProperties() { 52 String prop = System.getProperty("no.such.property"); 53 assertNull("no.such.property is unexpectedly defined", prop); 54 System.setProperty("no.such.property", "dummy value"); 55 prop = System.getProperty("no.such.property"); 56 assertNotNull("no.such.property is unexpectedly undefined", prop); 57 } 58 59 /** 60 * Sets up a custom class loader hierarchy for this test case. 61 */ 62 public static Test suite() throws Exception { 63 final Class thisClass = GeneralTestCase.class; 64 final ClassLoader thisClassLoader = thisClass.getClassLoader(); 65 66 final PathableClassLoader loader = new PathableClassLoader(null); 67 loader.useExplicitLoader("junit.", thisClassLoader); 68 loader.addLogicalLib("testclasses"); 69 70 // reload this class via the child class loader 71 final Class testClass = loader.loadClass(thisClass.getName()); 72 73 // and return our custom TestSuite class 74 return new PathableTestSuite(testClass, loader); 75 } 76 77 /** 78 * Verify that when a test method modifies the context class loader it is 79 * reset before the next test is run. 80 * <p> 81 * This method works in conjunction with testResetContext2. There is no 82 * way of knowing which test method junit will run first, but it doesn't 83 * matter; whichever one of them runs first will modify the contextClassloader. 84 * If the PathableTestSuite isn't resetting the contextClassLoader then whichever 85 * of them runs second will fail. Of course if other methods are run in-between 86 * then those methods might also fail... 87 */ 88 public void testResetContext1() { 89 checkAndSetContext(); 90 } 91 92 /** 93 * See testResetContext1. 94 */ 95 public void testResetContext2() { 96 checkAndSetContext(); 97 } 98 99 /** 100 * Verify that when a test method modifies the system properties they are 101 * reset before the next test is run. 102 * <p> 103 * This method works in conjunction with testResetProps2. There is no 104 * way of knowing which test method junit will run first, but it doesn't 105 * matter; whichever one of them runs first will modify the system properties. 106 * If the PathableTestSuite isn't resetting the system properties then whichever 107 * of them runs second will fail. Of course if other methods are run in-between 108 * then those methods might also fail... 109 */ 110 public void testResetProps1() { 111 checkAndSetProperties(); 112 } 113 114 /** 115 * See testResetProps1. 116 */ 117 public void testResetProps2() { 118 checkAndSetProperties(); 119 } 120 }