KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > impl > test > VfsClassLoaderTests


1 /*
2  * Copyright 2002-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.vfs.impl.test;
17
18 import org.apache.commons.vfs.Capability;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.impl.VFSClassLoader;
21 import org.apache.commons.vfs.test.AbstractProviderTestCase;
22
23 import java.net.URL JavaDoc;
24 import java.net.URLConnection JavaDoc;
25
26 /**
27  * VfsClassLoader test cases.
28  *
29  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
30  */

31 public class VfsClassLoaderTests
32     extends AbstractProviderTestCase
33 {
34     /**
35      * Returns the capabilities required by the tests of this test case.
36      */

37     protected Capability[] getRequiredCaps()
38     {
39         return new Capability[]
40         {
41             Capability.READ_CONTENT,
42             Capability.URI
43         };
44     }
45
46     /**
47      * Creates the classloader to use when testing.
48      */

49     private VFSClassLoader createClassLoader() throws FileSystemException
50     {
51         final VFSClassLoader loader =
52             new VFSClassLoader(getBaseFolder(), getManager());
53         return loader;
54     }
55
56     /**
57      * Tests loading a class.
58      */

59     public void testLoadClass() throws Exception JavaDoc
60     {
61         final VFSClassLoader loader = createClassLoader();
62
63         final Class JavaDoc testClass = loader.loadClass("code.ClassToLoad");
64         final Package JavaDoc pack = testClass.getPackage();
65         assertEquals("code", pack.getName());
66         verifyPackage(pack, false);
67
68         final Object JavaDoc testObject = testClass.newInstance();
69         assertEquals("**PRIVATE**", testObject.toString());
70     }
71
72     /**
73      * Tests loading a resource.
74      */

75     public void testLoadResource() throws Exception JavaDoc
76     {
77         final VFSClassLoader loader = createClassLoader();
78
79         final URL JavaDoc resource = loader.getResource("read-tests/file1.txt");
80
81         assertNotNull(resource);
82         final URLConnection JavaDoc urlCon = resource.openConnection();
83         assertSameURLContent(FILE1_CONTENT, urlCon);
84     }
85
86     /**
87      * Tests package sealing.
88      */

89     public void testSealing() throws Exception JavaDoc
90     {
91         final VFSClassLoader loader = createClassLoader();
92         final Class JavaDoc testClass = loader.loadClass("code.sealed.AnotherClass");
93         final Package JavaDoc pack = testClass.getPackage();
94         assertEquals("code.sealed", pack.getName());
95         verifyPackage(pack, true);
96     }
97
98     /**
99      * Verify the package loaded with class loader.
100      */

101     private void verifyPackage(final Package JavaDoc pack,
102                                final boolean sealed)
103         throws FileSystemException
104     {
105         if (getBaseFolder().getFileSystem().hasCapability(Capability.MANIFEST_ATTRIBUTES))
106         {
107             assertEquals("ImplTitle", pack.getImplementationTitle());
108             assertEquals("ImplVendor", pack.getImplementationVendor());
109             assertEquals("1.1", pack.getImplementationVersion());
110             assertEquals("SpecTitle", pack.getSpecificationTitle());
111             assertEquals("SpecVendor", pack.getSpecificationVendor());
112             assertEquals("1.0", pack.getSpecificationVersion());
113             assertEquals(sealed, pack.isSealed());
114         }
115         else
116         {
117             assertNull(pack.getImplementationTitle());
118             assertNull(pack.getImplementationVendor());
119             assertNull(pack.getImplementationVersion());
120             assertNull(pack.getSpecificationTitle());
121             assertNull(pack.getSpecificationVendor());
122             assertNull(pack.getSpecificationVersion());
123             assertFalse(pack.isSealed());
124         }
125     }
126
127 }
128
Popular Tags