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 testLoadClassOfType() throws Exception
75 {
76
77 Class<? extends Exception> clazz = ClassUtils.loadClass("java.lang.IllegalArgumentException", getClass(), Exception.class);
78 assertNotNull(clazz);
79
80 assertEquals(clazz.getName(), "java.lang.IllegalArgumentException");
81
82 try
83 {
84 ClassUtils.loadClass("java.lang.UnsupportedOperationException", getClass(), String.class);
85 fail("IllegalArgumentException should be thrown since class is not of expected type");
86 }
87 catch (IllegalArgumentException e)
88 {
89
90 }
91
92 }
93
94 public void testInstanciateClass() throws Exception
95 {
96 Object object = ClassUtils.instanciateClass("org.mule.tck.testmodels.fruit.Orange");
97 assertNotNull(object);
98 assertTrue(object instanceof Orange);
99
100 object = ClassUtils.instanciateClass("org.mule.tck.testmodels.fruit.FruitBowl", new Apple(), new Banana());
101 assertNotNull(object);
102 assertTrue(object instanceof FruitBowl);
103
104 FruitBowl bowl = (FruitBowl) object;
105
106 assertTrue(bowl.hasApple());
107 assertTrue(bowl.hasBanana());
108
109 try
110 {
111 ClassUtils.instanciateClass("java.lang.Bing");
112 fail("Class does not exist, ClassNotFoundException should have been thrown");
113 }
114 catch (ClassNotFoundException e)
115 {
116
117 }
118
119 }
120
121 public void testGetParameterTypes() throws Exception
122 {
123 FruitBowl bowl = new FruitBowl();
124
125 Class[] classes = ClassUtils.getParameterTypes(bowl, "apple");
126 assertNotNull(classes);
127 assertEquals(1, classes.length);
128 assertEquals(Apple.class, classes[0]);
129
130 classes = ClassUtils.getParameterTypes(bowl, "invalid");
131 assertNotNull(classes);
132 assertEquals(0, classes.length);
133 }
134
135 public void testLoadingResources() throws Exception
136 {
137 URL resource = ClassUtils.getResource("log4j.properties", getClass());
138 assertNotNull(resource);
139
140 resource = ClassUtils.getResource("does-not-exist.properties", getClass());
141 assertNull(resource);
142 }
143
144 public void testLoadingResourceEnumeration() throws Exception
145 {
146 Enumeration enumeration = ClassUtils.getResources("log4j.properties", getClass());
147 assertNotNull(enumeration);
148 assertTrue(enumeration.hasMoreElements());
149
150 enumeration = ClassUtils.getResources("does-not-exist.properties", getClass());
151 assertNotNull(enumeration);
152 assertTrue(!enumeration.hasMoreElements());
153 }
154
155 public void testGetSatisfiableMethods() throws Exception
156 {
157 List methods = ClassUtils.getSatisfiableMethods(FruitBowl.class, new Class[]{Apple.class}, true,
158 true, ignoreMethods);
159 assertNotNull(methods);
160 assertEquals(2, methods.size());
161
162 methods = ClassUtils.getSatisfiableMethods(FruitBowl.class, new Class[]{Apple.class}, false, true,
163 ignoreMethods);
164 assertNotNull(methods);
165 assertEquals(0, methods.size());
166
167
168 methods = ClassUtils.getSatisfiableMethods(DummyObject.class, new Class[]{WaterMelon.class}, true,
169 false, ignoreMethods);
170 assertNotNull(methods);
171 assertEquals(0, methods.size());
172
173
174 methods = ClassUtils.getSatisfiableMethods(DummyObject.class, new Class[]{WaterMelon.class}, true,
175 true, ignoreMethods);
176 assertNotNull(methods);
177 assertEquals(2, methods.size());
178
179
180 methods = ClassUtils.getSatisfiableMethods(DummyObject.class, new Class[]{WaterMelon.class}, false,
181 true, ignoreMethods);
182 assertNotNull(methods);
183 assertEquals(1, methods.size());
184 assertEquals("doSomethingElse", ((Method) methods.get(0)).getName());
185
186
187 methods = ClassUtils.getSatisfiableMethods(FruitBowl.class, new Class[]{WaterMelon[].class}, true,
188 true, ignoreMethods);
189 assertNotNull(methods);
190 assertEquals(1, methods.size());
191 assertEquals("setFruit", ((Method) methods.get(0)).getName());
192 }
193
194 public void testSimpleName()
195 {
196 simpleNameHelper("String", "foo".getClass());
197 simpleNameHelper("int[]", (new int[0]).getClass());
198 simpleNameHelper("Object[][]", (new Object[0][0]).getClass());
199 simpleNameHelper("null", null);
200 }
201
202 public void testEqual()
203 {
204 Object a1 = new HashBlob(1);
205 Object a2 = new HashBlob(1);
206 Object b = new HashBlob(2);
207 assertTrue(ClassUtils.equal(a1, a2));
208 assertTrue(ClassUtils.equal(b, b));
209 assertTrue(ClassUtils.equal(null, null));
210 assertFalse(ClassUtils.equal(a1, b));
211 assertFalse(ClassUtils.equal(a2, b));
212 assertFalse(ClassUtils.equal(null, b));
213 assertFalse(ClassUtils.equal(b, a1));
214 assertFalse(ClassUtils.equal(b, a2));
215 assertFalse(ClassUtils.equal(b, null));
216 }
217
218 public void testHash()
219 {
220 Object a = new HashBlob(1);
221 Object b = new HashBlob(2);
222 assertTrue(ClassUtils.hash(new Object[]{a, b, a, b}) == ClassUtils.hash(new Object[]{a, b, a, b}));
223 assertFalse(ClassUtils.hash(new Object[]{a, b, a}) == ClassUtils.hash(new Object[]{a, b, a, b}));
224 assertFalse(ClassUtils.hash(new Object[]{a, b, a, a}) == ClassUtils.hash(new Object[]{a, b, a, b}));
225 assertFalse(ClassUtils.hash(new Object[]{b, a, b, a}) == ClassUtils.hash(new Object[]{a, b, a, b}));
226 }
227
228 public void testClassTypesWithNullInArray()
229 {
230 Object[] array = new Object[]{"hello", null, "world"};
231 Class<?>[] classTypes = ClassUtils.getClassTypes(array);
232 assertEquals(3, classTypes.length);
233 assertEquals(String.class, classTypes[0]);
234 assertEquals(null, classTypes[1]);
235 assertEquals(String.class, classTypes[2]);
236 }
237
238 public void testCompareWithNull()
239 {
240 Class[] c1 = new Class[]{String.class, Integer.class};
241 Class[] c2 = new Class[]{String.class, null};
242 assertFalse(ClassUtils.compare(c1, c2, true));
243 assertFalse(ClassUtils.compare(c2, c1, true));
244 }
245
246 private void simpleNameHelper(String target, Class clazz)
247 {
248 assertEquals(target, ClassUtils.getSimpleName(clazz));
249 }
250
251 private static class DummyObject
252 {
253 public void doSomething(Object object)
254 {
255
256 }
257
258 public Object doSomethingElse(Object object)
259 {
260 return object;
261 }
262 }
263
264 private static class HashBlob
265 {
266
267 private int hash;
268
269 public HashBlob(int hash)
270 {
271 this.hash = hash;
272 }
273
274 public int hashCode()
275 {
276 return hash;
277 }
278
279 public boolean equals(Object other)
280 {
281 if (null == other || !getClass().equals(other.getClass()))
282 {
283 return false;
284 }
285 return hash == ((HashBlob) other).hash;
286 }
287
288 }
289
290 }