01: package abbot.util;
02:
03: import junit.extensions.abbot.TestHelper;
04: import junit.framework.TestCase;
05: import abbot.Platform;
06:
07: public class PathClassLoaderTest extends TestCase {
08:
09: public void testConvertPathToFilesW32() {
10: if (Platform.isWindows()) {
11: String path = "c:\\;c:\\mydir;.";
12: String[] names = PathClassLoader.convertPathToFilenames(
13: path, ":;");
14: String[] expected = { "c:\\", "c:\\mydir", "." };
15: assertEquals("Wrong number of files", expected.length,
16: names.length);
17: for (int i = 0; i < names.length; i++) {
18: assertEquals("Wrong path", expected[i], names[i]);
19: }
20: }
21: }
22:
23: public void testConvertPathToFiles() {
24:
25: String path = "/:/tmp:/tmp/mydir:.";
26: String[] names = PathClassLoader.convertPathToFilenames(path,
27: ":;");
28: String[] expected = { "/", "/tmp", "/tmp/mydir", "." };
29: assertEquals("Wrong number of files", expected.length,
30: names.length);
31: for (int i = 0; i < names.length; i++) {
32: assertEquals("Wrong path", expected[i], names[i]);
33: }
34: }
35:
36: /** We assume the existence of "lib/example.jar" and a resource
37: * example/logo32.gif within it.
38: */
39: public void testLoadResource() throws Throwable {
40: String pathName = "lib/example.jar";
41: String rsrc = "/example/logo32.gif";
42: String sysPath = System.getProperty("java.class.path");
43: assertTrue("Can't test, path " + pathName
44: + " is already in classpath " + sysPath, sysPath
45: .indexOf(pathName) == -1);
46:
47: ClassLoader cl = new PathClassLoader(pathName, getClass()
48: .getClassLoader());
49: try {
50: cl.getResourceAsStream(rsrc);
51: } catch (Exception exc) {
52: fail("Resource not found: " + exc.getMessage());
53: }
54: }
55:
56: /** We assume the existence of "lib/example.jar" and
57: example/FontChooser.class within it.
58: */
59: public void testLoadClassFromPath() throws Throwable {
60: String pathName = "lib/example.jar";
61: String className = "example.FontChooser";
62: String sysPath = System.getProperty("java.class.path");
63: assertTrue("Can't test, path " + pathName
64: + " is already in classpath " + sysPath, sysPath
65: .indexOf(pathName) == -1);
66: ClassLoader cl = new PathClassLoader(pathName, getClass()
67: .getClassLoader());
68: try {
69: Class.forName(className, true, cl);
70: } catch (ClassNotFoundException cnf) {
71: fail("Path class loader failed to load " + className
72: + " from path " + pathName);
73: }
74: }
75:
76: public PathClassLoaderTest(String name) {
77: super (name);
78: }
79:
80: public static void main(String[] args) {
81: TestHelper.runTests(args, PathClassLoaderTest.class);
82: }
83: }
|