View Javadoc

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