KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > logging > pathable > GeneralTestCase


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.logging.pathable;
17
18 import java.net.URL JavaDoc;
19 import java.net.URLClassLoader JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23
24 import org.apache.commons.logging.PathableClassLoader;
25 import org.apache.commons.logging.PathableTestSuite;
26
27 /**
28  * Tests for the PathableTestSuite class.
29  */

30
31 public class GeneralTestCase extends TestCase {
32     
33     /**
34      * Set up a custom classloader hierarchy for this test case.
35      */

36     public static Test suite() throws Exception JavaDoc {
37         Class JavaDoc thisClass = GeneralTestCase.class;
38         ClassLoader JavaDoc thisClassLoader = thisClass.getClassLoader();
39         
40         PathableClassLoader loader = new PathableClassLoader(null);
41         loader.useExplicitLoader("junit.", thisClassLoader);
42         loader.addLogicalLib("testclasses");
43
44         // reload this class via the child classloader
45
Class JavaDoc testClass = loader.loadClass(thisClass.getName());
46         
47         // and return our custom TestSuite class
48
return new PathableTestSuite(testClass, loader);
49     }
50     
51     /**
52      * Verify that a certain system property is not set, then set it.
53      */

54     private static void checkAndSetProperties() {
55         String JavaDoc prop = System.getProperty("no.such.property");
56         assertNull("no.such.property is unexpectedly defined", prop);
57         System.setProperty("no.such.property", "dummy value");
58         prop = System.getProperty("no.such.property");
59         assertNotNull("no.such.property is unexpectedly undefined", prop);
60     }
61     
62     /**
63      * Verify that when a test method modifies the system properties they are
64      * reset before the next test is run.
65      * <p>
66      * This method works in conjunction with testResetProps2. There is no
67      * way of knowing which test method junit will run first, but it doesn't
68      * matter; whichever one of them runs first will modify the system properties.
69      * If the PathableTestSuite isn't resetting the system properties then whichever
70      * of them runs second will fail. Of course if other methods are run in-between
71      * then those methods might also fail...
72      */

73     public void testResetProps1() {
74         checkAndSetProperties();
75     }
76
77     /**
78      * See testResetProps1.
79      */

80     public void testResetProps2() {
81         checkAndSetProperties();
82     }
83     
84     /**
85      * Verify that the context classloader is a custom one, then reset it to
86      * a non-custom one.
87      */

88     private static void checkAndSetContext() {
89         ClassLoader JavaDoc contextLoader = Thread.currentThread().getContextClassLoader();
90         assertEquals("ContextLoader is of unexpected type",
91                 contextLoader.getClass().getName(),
92                 PathableClassLoader.class.getName());
93         
94         URL JavaDoc[] noUrls = new URL JavaDoc[0];
95         Thread.currentThread().setContextClassLoader(new URLClassLoader JavaDoc(noUrls));
96     }
97     
98     /**
99      * Verify that when a test method modifies the context classloader it is
100      * reset before the next test is run.
101      * <p>
102      * This method works in conjunction with testResetContext2. There is no
103      * way of knowing which test method junit will run first, but it doesn't
104      * matter; whichever one of them runs first will modify the contextClassloader.
105      * If the PathableTestSuite isn't resetting the contextClassLoader then whichever
106      * of them runs second will fail. Of course if other methods are run in-between
107      * then those methods might also fail...
108      */

109     public void testResetContext1() {
110         checkAndSetContext();
111     }
112
113     /**
114      * See testResetContext1.
115      */

116     public void testResetContext2() {
117         checkAndSetContext();
118     }
119 }
120
Popular Tags