1   /*
2    * $Id: ClassUtilsTestCase.java 10160 2007-12-27 20:43:31Z acooke $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.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      // we do not want to match these methods when looking for a service method to
34      // invoke
35      protected final Set ignoreMethods = new HashSet(Arrays.asList(new String[]{"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              // expected
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              // expected
70          }
71  
72      }
73  
74      public void testInstanciateClass() throws Exception
75      {
76          Object object = ClassUtils.instanciateClass("org.mule.tck.testmodels.fruit.Orange", new Object[]{});
77          assertNotNull(object);
78          assertTrue(object instanceof Orange);
79  
80          object = ClassUtils.instanciateClass("org.mule.tck.testmodels.fruit.FruitBowl", new Object[]{
81              new Apple(), new Banana()});
82          assertNotNull(object);
83          assertTrue(object instanceof FruitBowl);
84  
85          FruitBowl bowl = (FruitBowl)object;
86  
87          assertTrue(bowl.hasApple());
88          assertTrue(bowl.hasBanana());
89  
90          try
91          {
92              ClassUtils.instanciateClass("java.lang.Bing", new Object[]{});
93              fail("Class does not exist, ClassNotFoundException should have been thrown");
94          }
95          catch (ClassNotFoundException e)
96          {
97              // expected
98          }
99  
100     }
101 
102     public void testGetParameterTypes() throws Exception
103     {
104         FruitBowl bowl = new FruitBowl();
105 
106         Class[] classes = ClassUtils.getParameterTypes(bowl, "apple");
107         assertNotNull(classes);
108         assertEquals(1, classes.length);
109         assertEquals(Apple.class, classes[0]);
110 
111         classes = ClassUtils.getParameterTypes(bowl, "invalid");
112         assertNotNull(classes);
113         assertEquals(0, classes.length);
114     }
115 
116     public void testLoadingResources() throws Exception
117     {
118         URL resource = ClassUtils.getResource("log4j.properties", getClass());
119         assertNotNull(resource);
120 
121         resource = ClassUtils.getResource("does-not-exist.properties", getClass());
122         assertNull(resource);
123     }
124 
125     public void testLoadingResourceEnumeration() throws Exception
126     {
127         Enumeration enumeration = ClassUtils.getResources("log4j.properties", getClass());
128         assertNotNull(enumeration);
129         assertTrue(enumeration.hasMoreElements());
130 
131         enumeration = ClassUtils.getResources("does-not-exist.properties", getClass());
132         assertNotNull(enumeration);
133         assertTrue(!enumeration.hasMoreElements());
134     }
135 
136     public void testGetSatisfiableMethods() throws Exception
137     {
138         List methods = ClassUtils.getSatisfiableMethods(FruitBowl.class, new Class[]{Apple.class}, true,
139             true, ignoreMethods);
140         assertNotNull(methods);
141         assertEquals(1, methods.size());
142 
143         methods = ClassUtils.getSatisfiableMethods(FruitBowl.class, new Class[]{Apple.class}, false, true,
144             ignoreMethods);
145         assertNotNull(methods);
146         assertEquals(0, methods.size());
147 
148         // Test object param being unacceptible
149         methods = ClassUtils.getSatisfiableMethods(DummyObject.class, new Class[]{WaterMelon.class}, true,
150             false, ignoreMethods);
151         assertNotNull(methods);
152         assertEquals(0, methods.size());
153 
154         // Test object param being acceptible
155         methods = ClassUtils.getSatisfiableMethods(DummyObject.class, new Class[]{WaterMelon.class}, true,
156             true, ignoreMethods);
157         assertNotNull(methods);
158         assertEquals(2, methods.size());
159 
160         // Test object param being acceptible but not void
161         methods = ClassUtils.getSatisfiableMethods(DummyObject.class, new Class[]{WaterMelon.class}, false,
162             true, ignoreMethods);
163         assertNotNull(methods);
164         assertEquals(1, methods.size());
165         assertEquals("doSomethingElse", ((Method)methods.get(0)).getName());
166 
167         // Test object param being acceptible by interface Type
168         methods = ClassUtils.getSatisfiableMethods(FruitBowl.class, new Class[]{WaterMelon[].class}, true,
169             true, ignoreMethods);
170         assertNotNull(methods);
171         assertEquals(1, methods.size());
172         assertEquals("setFruit", ((Method)methods.get(0)).getName());
173     }
174 
175     public void testSimpleName()
176     {
177         simpleNameHelper("String", "foo".getClass());
178         simpleNameHelper("int[]", (new int[0]).getClass());
179         simpleNameHelper("Object[][]", (new Object[0][0]).getClass());
180         simpleNameHelper("null", null);
181     }
182 
183     public void testEqual()
184     {
185         Object a1 = new HashBlob(1);
186         Object a2 = new HashBlob(1);
187         Object b = new HashBlob(2);
188         assertTrue(ClassUtils.equal(a1, a2));
189         assertTrue(ClassUtils.equal(b, b));
190         assertTrue(ClassUtils.equal(null, null));
191         assertFalse(ClassUtils.equal(a1, b));
192         assertFalse(ClassUtils.equal(a2, b));
193         assertFalse(ClassUtils.equal(null, b));
194         assertFalse(ClassUtils.equal(b, a1));
195         assertFalse(ClassUtils.equal(b, a2));
196         assertFalse(ClassUtils.equal(b, null));
197     }
198 
199     public void testHash()
200     {
201         Object a = new HashBlob(1);
202         Object b = new HashBlob(2);
203         assertTrue(ClassUtils.hash(new Object[]{a, b, a, b}) == ClassUtils.hash(new Object[]{a, b, a, b}));
204         assertFalse(ClassUtils.hash(new Object[]{a, b, a}) == ClassUtils.hash(new Object[]{a, b, a, b}));
205         assertFalse(ClassUtils.hash(new Object[]{a, b, a, a}) == ClassUtils.hash(new Object[]{a, b, a, b}));
206         assertFalse(ClassUtils.hash(new Object[]{b, a, b, a}) == ClassUtils.hash(new Object[]{a, b, a, b}));
207     }
208 
209     private void simpleNameHelper(String target, Class clazz)
210     {
211         assertEquals(target, ClassUtils.getSimpleName(clazz));
212     }
213 
214     private static class DummyObject
215     {
216         public void doSomething(Object object)
217         {
218             // do nothing
219         }
220 
221         public Object doSomethingElse(Object object)
222         {
223             return object;
224         }
225     }
226 
227     private static class HashBlob
228     {
229 
230         private int hash;
231 
232         public HashBlob(int hash)
233         {
234             this.hash = hash;
235         }
236 
237         public int hashCode()
238         {
239             return hash;
240         }
241 
242         public boolean equals(Object other)
243         {
244             if (null == other || !getClass().equals(other.getClass())) return false;
245             return hash == ((HashBlob) other).hash;
246         }
247 
248     }
249 
250 }