1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13 import org.mule.tck.AbstractMuleTestCase;
14 import org.mule.tck.testmodels.fruit.AbstractFruit;
15 import org.mule.tck.testmodels.fruit.Apple;
16 import org.mule.tck.testmodels.fruit.Banana;
17 import org.mule.tck.testmodels.fruit.Fruit;
18 import org.mule.tck.testmodels.fruit.FruitBowl;
19 import org.mule.tck.testmodels.fruit.Orange;
20 import org.mule.tck.testmodels.fruit.WaterMelon;
21
22 import java.lang.reflect.Method;
23 import java.net.URL;
24 import java.util.Arrays;
25 import java.util.Enumeration;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29
30 public class ClassUtilsTestCase extends AbstractMuleTestCase
31 {
32
33
34
35 protected final Set<String> ignoreMethods = new HashSet<String>(Arrays.asList("equals",
36 "getInvocationHandler"));
37
38 public void testIsConcrete() throws Exception
39 {
40 assertTrue(ClassUtils.isConcrete(Orange.class));
41 assertTrue(!ClassUtils.isConcrete(Fruit.class));
42 assertTrue(!ClassUtils.isConcrete(AbstractFruit.class));
43
44 try
45 {
46 ClassUtils.isConcrete(null);
47 fail("Class cannot be null, exception should be thrown");
48 }
49 catch (RuntimeException e)
50 {
51
52 }
53 }
54
55 public void testLoadClass() throws Exception
56 {
57 Class clazz = ClassUtils.loadClass("java.lang.String", getClass());
58 assertNotNull(clazz);
59
60 assertEquals(clazz.getName(), "java.lang.String");
61
62 try
63 {
64 ClassUtils.loadClass("java.lang.Bing", getClass());
65 fail("ClassNotFoundException should be thrown");
66 }
67 catch (ClassNotFoundException e)
68 {
69
70 }
71
72 }
73
74 public void testLoadPrimitiveClass() throws Exception
75 {
76 assertSame(ClassUtils.loadClass("boolean", getClass()), Boolean.TYPE);
77 assertSame(ClassUtils.loadClass("byte", getClass()), Byte.TYPE);
78 assertSame(ClassUtils.loadClass("char", getClass()), Character.TYPE);
79 assertSame(ClassUtils.loadClass("double", getClass()), Double.TYPE);
80 assertSame(ClassUtils.loadClass("float", getClass()), Float.TYPE);
81 assertSame(ClassUtils.loadClass("int", getClass()), Integer.TYPE);
82 assertSame(ClassUtils.loadClass("long", getClass()), Long.TYPE);
83 assertSame(ClassUtils.loadClass("short", getClass()), Short.TYPE);
84 }
85
86 public void testLoadClassOfType() throws Exception
87 {
88
89 Class<? extends Exception> clazz = ClassUtils.loadClass("java.lang.IllegalArgumentException", getClass(), Exception.class);
90 assertNotNull(clazz);
91
92 assertEquals(clazz.getName(), "java.lang.IllegalArgumentException");
93
94 try
95 {
96 ClassUtils.loadClass("java.lang.UnsupportedOperationException", getClass(), String.class);
97 fail("IllegalArgumentException should be thrown since class is not of expected type");
98 }
99 catch (IllegalArgumentException e)
100 {
101
102 }
103
104 }
105
106 public void testInstanciateClass() throws Exception
107 {
108 Object object = ClassUtils.instanciateClass("org.mule.tck.testmodels.fruit.Orange");
109 assertNotNull(object);
110 assertTrue(object instanceof Orange);
111
112 object = ClassUtils.instanciateClass("org.mule.tck.testmodels.fruit.FruitBowl", new Apple(), new Banana());
113 assertNotNull(object);
114 assertTrue(object instanceof FruitBowl);
115
116 FruitBowl bowl = (FruitBowl) object;
117
118 assertTrue(bowl.hasApple());
119 assertTrue(bowl.hasBanana());
120
121 try
122 {
123 ClassUtils.instanciateClass("java.lang.Bing");
124 fail("Class does not exist, ClassNotFoundException should have been thrown");
125 }
126 catch (ClassNotFoundException e)
127 {
128
129 }
130
131 }
132
133 public void testGetParameterTypes() throws Exception
134 {
135 FruitBowl bowl = new FruitBowl();
136
137 Class[] classes = ClassUtils.getParameterTypes(bowl, "apple");
138 assertNotNull(classes);
139 assertEquals(1, classes.length);
140 assertEquals(Apple.class, classes[0]);
141
142 classes = ClassUtils.getParameterTypes(bowl, "invalid");
143 assertNotNull(classes);
144 assertEquals(0, classes.length);
145 }
146
147 public void testLoadingResources() throws Exception
148 {
149 URL resource = ClassUtils.getResource("log4j.properties", getClass());
150 assertNotNull(resource);
151
152 resource = ClassUtils.getResource("does-not-exist.properties", getClass());
153 assertNull(resource);
154 }
155
156 public void testLoadingResourceEnumeration() throws Exception
157 {
158 Enumeration enumeration = ClassUtils.getResources("log4j.properties", getClass());
159 assertNotNull(enumeration);
160 assertTrue(enumeration.hasMoreElements());
161
162 enumeration = ClassUtils.getResources("does-not-exist.properties", getClass());
163 assertNotNull(enumeration);
164 assertTrue(!enumeration.hasMoreElements());
165 }
166
167 public void testGetSatisfiableMethods() throws Exception
168 {
169 List methods = ClassUtils.getSatisfiableMethods(FruitBowl.class, new Class[]{Apple.class}, true,
170 true, ignoreMethods);
171 assertNotNull(methods);
172 assertEquals(2, methods.size());
173
174 methods = ClassUtils.getSatisfiableMethods(FruitBowl.class, new Class[]{Apple.class}, false, true,
175 ignoreMethods);
176 assertNotNull(methods);
177 assertEquals(0, methods.size());
178
179
180 methods = ClassUtils.getSatisfiableMethods(DummyObject.class, new Class[]{WaterMelon.class}, true,
181 false, ignoreMethods);
182 assertNotNull(methods);
183 assertEquals(0, methods.size());
184
185
186 methods = ClassUtils.getSatisfiableMethods(DummyObject.class, new Class[]{WaterMelon.class}, true,
187 true, ignoreMethods);
188 assertNotNull(methods);
189 assertEquals(2, methods.size());
190
191
192 methods = ClassUtils.getSatisfiableMethods(DummyObject.class, new Class[]{WaterMelon.class}, false,
193 true, ignoreMethods);
194 assertNotNull(methods);
195 assertEquals(1, methods.size());
196 assertEquals("doSomethingElse", ((Method) methods.get(0)).getName());
197
198
199 methods = ClassUtils.getSatisfiableMethods(FruitBowl.class, new Class[]{WaterMelon[].class}, true,
200 true, ignoreMethods);
201 assertNotNull(methods);
202 assertEquals(1, methods.size());
203 assertEquals("setFruit", ((Method) methods.get(0)).getName());
204 }
205
206 public void testSimpleName()
207 {
208 simpleNameHelper("String", "foo".getClass());
209 simpleNameHelper("int[]", (new int[0]).getClass());
210 simpleNameHelper("Object[][]", (new Object[0][0]).getClass());
211 simpleNameHelper("null", null);
212 }
213
214 public void testEqual()
215 {
216 Object a1 = new HashBlob(1);
217 Object a2 = new HashBlob(1);
218 Object b = new HashBlob(2);
219 assertTrue(ClassUtils.equal(a1, a2));
220 assertTrue(ClassUtils.equal(b, b));
221 assertTrue(ClassUtils.equal(null, null));
222 assertFalse(ClassUtils.equal(a1, b));
223 assertFalse(ClassUtils.equal(a2, b));
224 assertFalse(ClassUtils.equal(null, b));
225 assertFalse(ClassUtils.equal(b, a1));
226 assertFalse(ClassUtils.equal(b, a2));
227 assertFalse(ClassUtils.equal(b, null));
228 }
229
230 public void testHash()
231 {
232 Object a = new HashBlob(1);
233 Object b = new HashBlob(2);
234 assertTrue(ClassUtils.hash(new Object[]{a, b, a, b}) == ClassUtils.hash(new Object[]{a, b, a, b}));
235 assertFalse(ClassUtils.hash(new Object[]{a, b, a}) == ClassUtils.hash(new Object[]{a, b, a, b}));
236 assertFalse(ClassUtils.hash(new Object[]{a, b, a, a}) == ClassUtils.hash(new Object[]{a, b, a, b}));
237 assertFalse(ClassUtils.hash(new Object[]{b, a, b, a}) == ClassUtils.hash(new Object[]{a, b, a, b}));
238 }
239
240 public void testClassTypesWithNullInArray()
241 {
242 Object[] array = new Object[]{"hello", null, "world"};
243 Class<?>[] classTypes = ClassUtils.getClassTypes(array);
244 assertEquals(3, classTypes.length);
245 assertEquals(String.class, classTypes[0]);
246 assertEquals(null, classTypes[1]);
247 assertEquals(String.class, classTypes[2]);
248 }
249
250 public void testCompareWithNull()
251 {
252 Class[] c1 = new Class[]{String.class, Integer.class};
253 Class[] c2 = new Class[]{String.class, null};
254 assertFalse(ClassUtils.compare(c1, c2, true));
255 assertFalse(ClassUtils.compare(c2, c1, true));
256 }
257
258 private void simpleNameHelper(String target, Class clazz)
259 {
260 assertEquals(target, ClassUtils.getSimpleName(clazz));
261 }
262
263 private static class DummyObject
264 {
265 public void doSomething(Object object)
266 {
267
268 }
269
270 public Object doSomethingElse(Object object)
271 {
272 return object;
273 }
274 }
275
276 private static class HashBlob
277 {
278
279 private int hash;
280
281 public HashBlob(int hash)
282 {
283 this.hash = hash;
284 }
285
286 public int hashCode()
287 {
288 return hash;
289 }
290
291 public boolean equals(Object other)
292 {
293 if (null == other || !getClass().equals(other.getClass()))
294 {
295 return false;
296 }
297 return hash == ((HashBlob) other).hash;
298 }
299
300 }
301
302 }