1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.logging.pathable;
17
18 import java.net.URL;
19 import java.net.URLClassLoader;
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 {
37 Class thisClass = GeneralTestCase.class;
38 ClassLoader thisClassLoader = thisClass.getClassLoader();
39
40 PathableClassLoader loader = new PathableClassLoader(null);
41 loader.useExplicitLoader("junit.", thisClassLoader);
42 loader.addLogicalLib("testclasses");
43
44
45 Class testClass = loader.loadClass(thisClass.getName());
46
47
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 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 contextLoader = Thread.currentThread().getContextClassLoader();
90 assertEquals("ContextLoader is of unexpected type",
91 contextLoader.getClass().getName(),
92 PathableClassLoader.class.getName());
93
94 URL[] noUrls = new URL[0];
95 Thread.currentThread().setContextClassLoader(new URLClassLoader(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 }