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