View Javadoc

1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *      http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package org.apache.commons.classscan.builtin;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertFalse;
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertNull;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import java.net.URISyntaxException;
24  import java.util.Collection;
25  import java.util.Iterator;
26  
27  import org.apache.commons.classscan.MetaClassLoader;
28  import org.apache.commons.classscan.MetaClassPathElement;
29  import org.apache.commons.classscan.MetaRegistry;
30  import org.apache.commons.classscan.model.MetaClass;
31  import org.apache.commons.classscan.test.classes.FullInterface;
32  import org.apache.commons.classscan.test.classes.FullyDecorated;
33  import org.apache.commons.classscan.test.classes.ValidateFullyDecorated;
34  import org.junit.Before;
35  import org.junit.Test;
36  
37  public class UrlMetaClassLoaderTest {
38  
39      private static final String OBJECT_CANONICAL_NAME = Object.class.getCanonicalName();
40      private static final String FULLY_DECORATED_NAME = FullyDecorated.class.getCanonicalName();
41  
42      MetaClassLoader classLoader;
43      MetaClass fdClass;
44      MetaClass objClass;
45  
46      @Before
47      public void loadLocation() throws URISyntaxException {
48          classLoader = MetaRegistry.DEFAULT_REGISTRY.getMetaClassLoader(getClass().getClassLoader());
49          fdClass = classLoader.findMetaClass(FULLY_DECORATED_NAME);
50          objClass = classLoader.findMetaClass(OBJECT_CANONICAL_NAME);
51      }
52  
53      @Test
54      public void testGetMetaClass() {
55          new ValidateFullyDecorated(classLoader.getMetaClass(FULLY_DECORATED_NAME));        
56          assertNull(classLoader.getMetaClass(OBJECT_CANONICAL_NAME));
57      }
58  
59      @Test
60      public void testFindMetaClass() {
61          assertEquals(FULLY_DECORATED_NAME, fdClass.getName());
62          assertEquals(OBJECT_CANONICAL_NAME, objClass.getName());
63      }
64  
65      <T> boolean contains(Iterator<? extends T> classes, T toFind) {
66      	while(classes.hasNext()) {
67      		T mc = classes.next();
68      		if(mc.equals(toFind)) {
69      			return true;
70      		}
71      	}
72      	return false;
73      }
74      
75      @Test
76      public void testGetMetaClasses() {
77          Iterator<? extends MetaClass> classes = classLoader.getMetaClasses();
78          assertTrue(contains(classes, fdClass));
79          assertFalse(contains(classes, objClass));
80      }
81  
82      @Test
83      public void testIterateGetMetaClasses() {
84          Iterator<? extends MetaClass> classes = classLoader.getMetaClasses();
85          while(classes.hasNext()) {
86          	assertNotNull(classes.next());
87          }
88      }
89  
90      @Test
91      public void testGetClassLocations() throws URISyntaxException {
92          Collection<? extends MetaClassPathElement> locations = classLoader.getClassLocations();
93          MetaClassPathElement location = classLoader.getClassLocation(getClass().getProtectionDomain().getCodeSource().getLocation().toString());
94          assertTrue(locations.contains(location));
95      }
96  
97      @Test
98      public void testGetParent() {
99          for (int i = 0; i < 4; ++i) {
100             MetaClassLoader pcl = classLoader.getParent();
101             if (pcl == null) {
102                 // cl is now bootstrap class loader
103                 assertNotNull(classLoader.getMetaClass(Integer.class.getCanonicalName()));
104                 return;
105             }
106             classLoader = pcl;
107         }
108         fail("Too many nested ClassLoaders");
109     }
110 
111     @Test
112     public void findAllImplementations() throws URISyntaxException {
113         Collection<? extends MetaClass> implementations= classLoader.findAllImplementors(FullInterface.class.getCanonicalName());
114         assertTrue(implementations.contains(fdClass));
115         assertFalse(implementations.contains(objClass));
116     }
117 }