1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs.impl.test;
18  
19  import org.apache.commons.vfs.Capability;
20  import org.apache.commons.vfs.FileSystemException;
21  import org.apache.commons.vfs.impl.VFSClassLoader;
22  import org.apache.commons.vfs.test.AbstractProviderTestCase;
23  
24  import java.net.URL;
25  import java.net.URLConnection;
26  
27  /***
28   * VfsClassLoader test cases.
29   *
30   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
31   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
32   */
33  public class VfsClassLoaderTests
34      extends AbstractProviderTestCase
35  {
36      /***
37       * Returns the capabilities required by the tests of this test case.
38       */
39      protected Capability[] getRequiredCaps()
40      {
41          return new Capability[]
42          {
43              Capability.READ_CONTENT,
44              Capability.URI
45          };
46      }
47  
48      /***
49       * Creates the classloader to use when testing.
50       */
51      private VFSClassLoader createClassLoader() throws FileSystemException
52      {
53          final VFSClassLoader loader =
54              new VFSClassLoader(getBaseFolder(), getManager());
55          return loader;
56      }
57  
58      /***
59       * Tests loading a class.
60       */
61      public void testLoadClass() throws Exception
62      {
63          final VFSClassLoader loader = createClassLoader();
64  
65          final Class testClass = loader.loadClass("code.ClassToLoad");
66          final Package pack = testClass.getPackage();
67          assertEquals("code", pack.getName());
68          verifyPackage(pack, false);
69  
70          final Object testObject = testClass.newInstance();
71          assertEquals("**PRIVATE**", testObject.toString());
72      }
73  
74      /***
75       * Tests loading a resource.
76       */
77      public void testLoadResource() throws Exception
78      {
79          final VFSClassLoader loader = createClassLoader();
80  
81          final URL resource = loader.getResource("read-tests/file1.txt");
82  
83          assertNotNull(resource);
84          final URLConnection urlCon = resource.openConnection();
85          assertSameURLContent(FILE1_CONTENT, urlCon);
86      }
87  
88      /***
89       * Tests package sealing.
90       */
91      public void testSealing() throws Exception
92      {
93          final VFSClassLoader loader = createClassLoader();
94          final Class testClass = loader.loadClass("code.sealed.AnotherClass");
95          final Package pack = testClass.getPackage();
96          assertEquals("code.sealed", pack.getName());
97          verifyPackage(pack, true);
98      }
99  
100     /***
101      * Verify the package loaded with class loader.
102      */
103     private void verifyPackage(final Package pack,
104                                final boolean sealed)
105         throws FileSystemException
106     {
107         if (getBaseFolder().getFileSystem().hasCapability(Capability.MANIFEST_ATTRIBUTES))
108         {
109             assertEquals("ImplTitle", pack.getImplementationTitle());
110             assertEquals("ImplVendor", pack.getImplementationVendor());
111             assertEquals("1.1", pack.getImplementationVersion());
112             assertEquals("SpecTitle", pack.getSpecificationTitle());
113             assertEquals("SpecVendor", pack.getSpecificationVendor());
114             assertEquals("1.0", pack.getSpecificationVersion());
115             assertEquals(sealed, pack.isSealed());
116         }
117         else
118         {
119             assertNull(pack.getImplementationTitle());
120             assertNull(pack.getImplementationVendor());
121             assertNull(pack.getImplementationVersion());
122             assertNull(pack.getSpecificationTitle());
123             assertNull(pack.getSpecificationVendor());
124             assertNull(pack.getSpecificationVersion());
125             assertFalse(pack.isSealed());
126         }
127     }
128 
129 }