1 17 18 package org.apache.geronimo.kernel; 19 20 import java.util.Set ; 21 import java.util.LinkedHashSet ; 22 import java.io.Serializable ; 23 24 import junit.framework.TestCase; 25 import org.apache.geronimo.kernel.basic.BasicKernel; 26 27 32 public class ClassloadingTest extends TestCase { 33 public void testLoadClass_Null() { 34 try { 35 ClassLoading.loadClass("org.apache.geronimo.kernel.ClassLoading", null); 36 fail("Expected NullArgumentException"); 37 } catch (IllegalArgumentException ignore) { 38 } catch (ClassNotFoundException e) { 39 fail("Class should have been found: " + e); 40 } 41 } 42 43 public void testLoadClass_Simple() { 44 String className = "org.apache.geronimo.kernel.ClassLoading"; 45 Class type = loadClass(className); 46 assertEquals(className, type.getName()); 47 } 48 49 public void testLoadClass_Primitives() { 50 String className = "boolean"; 51 Class type = loadClass(className); 52 assertEquals(className, type.getName()); 53 } 54 55 public void testLoadClass_VMPrimitives() { 56 String className = "B"; 57 Class type = loadClass(className); 58 assertEquals(byte.class, type); 59 } 60 61 public void testLoadClass_VMClassSyntax() { 62 String className = "org.apache.geronimo.kernel.ClassLoading"; 63 Class type = loadClass("L" + className + ";"); 64 assertEquals(className, type.getName()); 65 } 66 67 public void testLoadClass_VMArraySyntax() { 68 String className = "[B"; 69 Class type = loadClass(className); 70 assertEquals(byte[].class, type); 71 72 className = "[java.lang.String"; 73 type = loadClass(className); 74 assertEquals(String [].class, type); 75 } 76 77 public void testLoadClass_UserFriendlySyntax() { 78 String className = "I[]"; 79 Class type = loadClass(className); 80 assertEquals(int[].class, type); 81 82 className = "I[][][]"; 83 type = loadClass(className); 84 assertEquals(int[][][].class, type); 85 } 86 87 public void testgetClassName() throws ClassNotFoundException { 88 Class t; 89 Class y; 90 String x; 91 92 t = String .class; 93 x = ClassLoading.getClassName(t); 94 y = loadClass(x); 95 assertEquals(t, y); 96 97 t = int.class; 98 x = ClassLoading.getClassName(t); 99 y = loadClass(x); 100 assertEquals(t, y); 101 102 t = String [].class; 103 x = ClassLoading.getClassName(t); 104 y = loadClass(x); 105 assertEquals(t, y); 106 107 t = int[].class; 108 x = ClassLoading.getClassName(t); 109 y = loadClass(x); 110 assertEquals(t, y); 111 112 t = String [][].class; 113 x = ClassLoading.getClassName(t); 114 y = loadClass(x); 115 assertEquals(t, y); 116 117 t = int[][].class; 118 x = ClassLoading.getClassName(t); 119 y = loadClass(x); 120 assertEquals(t, y); 121 122 } 123 124 public void testGetAllTypes() throws Exception { 125 Set allTypes = ClassLoading.getAllTypes(MockGBean.class); 126 assertTrue(allTypes.contains(MockGBean.class)); 127 assertTrue(allTypes.contains(Object .class)); 128 assertTrue(allTypes.contains(MockEndpoint.class)); 129 assertTrue(allTypes.contains(MockParentInterface1.class)); 130 assertTrue(allTypes.contains(MockParentInterface2.class)); 131 assertTrue(allTypes.contains(MockChildInterface1.class)); 132 assertTrue(allTypes.contains(MockChildInterface2.class)); 133 assertFalse(allTypes.contains(Comparable .class)); 134 } 135 136 public void testReduceInterfaces() throws Exception { 137 Set types = new LinkedHashSet (); 138 139 types.add(MockGBean.class); 141 types = ClassLoading.reduceInterfaces(types); 142 assertTrue(types.contains(MockGBean.class)); 143 assertFalse(types.contains(Object .class)); 144 assertFalse(types.contains(MockEndpoint.class)); 145 assertFalse(types.contains(MockParentInterface1.class)); 146 assertFalse(types.contains(MockParentInterface2.class)); 147 assertFalse(types.contains(MockChildInterface1.class)); 148 assertFalse(types.contains(MockChildInterface2.class)); 149 assertFalse(types.contains(Comparable .class)); 150 151 types = ClassLoading.getAllTypes(MockGBean.class); 153 types = ClassLoading.reduceInterfaces(types); 154 assertTrue(types.contains(MockGBean.class)); 155 assertFalse(types.contains(Object .class)); 156 assertFalse(types.contains(MockEndpoint.class)); 157 assertFalse(types.contains(MockParentInterface1.class)); 158 assertFalse(types.contains(MockParentInterface2.class)); 159 assertFalse(types.contains(MockChildInterface1.class)); 160 assertFalse(types.contains(MockChildInterface2.class)); 161 assertFalse(types.contains(Comparable .class)); 162 163 types = ClassLoading.getAllTypes(MockGBean.class); 165 types.addAll(ClassLoading.getAllTypes(MockGBean.class)); 166 types = ClassLoading.reduceInterfaces(types); 167 assertTrue(types.contains(MockGBean.class)); 168 assertFalse(types.contains(Object .class)); 169 assertFalse(types.contains(MockEndpoint.class)); 170 assertFalse(types.contains(MockParentInterface1.class)); 171 assertFalse(types.contains(MockParentInterface2.class)); 172 assertFalse(types.contains(MockChildInterface1.class)); 173 assertFalse(types.contains(MockChildInterface2.class)); 174 assertFalse(types.contains(Comparable .class)); 175 176 types = ClassLoading.getAllTypes(MockGBean.class); 178 types.addAll(ClassLoading.getAllTypes(Kernel.class)); 179 types.addAll(ClassLoading.getAllTypes(Serializable .class)); 180 types = ClassLoading.reduceInterfaces(types); 181 assertTrue(types.contains(Kernel.class)); 182 assertTrue(types.contains(Serializable .class)); 183 assertTrue(types.contains(MockGBean.class)); 184 assertFalse(types.contains(Object .class)); 185 assertFalse(types.contains(MockEndpoint.class)); 186 assertFalse(types.contains(MockParentInterface1.class)); 187 assertFalse(types.contains(MockParentInterface2.class)); 188 assertFalse(types.contains(MockChildInterface1.class)); 189 assertFalse(types.contains(MockChildInterface2.class)); 190 assertFalse(types.contains(Comparable .class)); 191 192 types = ClassLoading.getAllTypes(MockGBean.class); 194 try { 195 types.addAll(ClassLoading.getAllTypes(BasicKernel.class)); 196 } catch (IllegalArgumentException e) { 197 } 199 } 200 201 private Class loadClass(String name) { 202 Class type = null; 203 204 try { 205 type = ClassLoading.loadClass(name, getClass().getClassLoader()); 206 } catch (ClassNotFoundException e) { 207 fail("Class should have been found: " + e); 208 } 209 210 assertNotNull(type); 211 212 return type; 213 } 214 } 215 | Popular Tags |