001: /*
002: * Copyright 2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.apache.commons.logging.pathable;
017:
018: import java.net.URL;
019: import java.net.URLClassLoader;
020:
021: import junit.framework.Test;
022: import junit.framework.TestCase;
023:
024: import org.apache.commons.logging.PathableClassLoader;
025: import org.apache.commons.logging.PathableTestSuite;
026:
027: /**
028: * Tests for the PathableTestSuite class.
029: */
030:
031: public class GeneralTestCase extends TestCase {
032:
033: /**
034: * Set up a custom classloader hierarchy for this test case.
035: */
036: public static Test suite() throws Exception {
037: Class this Class = GeneralTestCase.class;
038: ClassLoader this ClassLoader = this Class.getClassLoader();
039:
040: PathableClassLoader loader = new PathableClassLoader(null);
041: loader.useExplicitLoader("junit.", this ClassLoader);
042: loader.addLogicalLib("testclasses");
043:
044: // reload this class via the child classloader
045: Class testClass = loader.loadClass(this Class.getName());
046:
047: // and return our custom TestSuite class
048: return new PathableTestSuite(testClass, loader);
049: }
050:
051: /**
052: * Verify that a certain system property is not set, then set it.
053: */
054: private static void checkAndSetProperties() {
055: String prop = System.getProperty("no.such.property");
056: assertNull("no.such.property is unexpectedly defined", prop);
057: System.setProperty("no.such.property", "dummy value");
058: prop = System.getProperty("no.such.property");
059: assertNotNull("no.such.property is unexpectedly undefined",
060: prop);
061: }
062:
063: /**
064: * Verify that when a test method modifies the system properties they are
065: * reset before the next test is run.
066: * <p>
067: * This method works in conjunction with testResetProps2. There is no
068: * way of knowing which test method junit will run first, but it doesn't
069: * matter; whichever one of them runs first will modify the system properties.
070: * If the PathableTestSuite isn't resetting the system properties then whichever
071: * of them runs second will fail. Of course if other methods are run in-between
072: * then those methods might also fail...
073: */
074: public void testResetProps1() {
075: checkAndSetProperties();
076: }
077:
078: /**
079: * See testResetProps1.
080: */
081: public void testResetProps2() {
082: checkAndSetProperties();
083: }
084:
085: /**
086: * Verify that the context classloader is a custom one, then reset it to
087: * a non-custom one.
088: */
089: private static void checkAndSetContext() {
090: ClassLoader contextLoader = Thread.currentThread()
091: .getContextClassLoader();
092: assertEquals("ContextLoader is of unexpected type",
093: contextLoader.getClass().getName(),
094: PathableClassLoader.class.getName());
095:
096: URL[] noUrls = new URL[0];
097: Thread.currentThread().setContextClassLoader(
098: new URLClassLoader(noUrls));
099: }
100:
101: /**
102: * Verify that when a test method modifies the context classloader it is
103: * reset before the next test is run.
104: * <p>
105: * This method works in conjunction with testResetContext2. There is no
106: * way of knowing which test method junit will run first, but it doesn't
107: * matter; whichever one of them runs first will modify the contextClassloader.
108: * If the PathableTestSuite isn't resetting the contextClassLoader then whichever
109: * of them runs second will fail. Of course if other methods are run in-between
110: * then those methods might also fail...
111: */
112: public void testResetContext1() {
113: checkAndSetContext();
114: }
115:
116: /**
117: * See testResetContext1.
118: */
119: public void testResetContext2() {
120: checkAndSetContext();
121: }
122: }
|