1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.java.lang.reflect;
22  
23  import java.lang.reflect.Method;
24  import junit.framework.TestCase;
25  /***
26   * Unit test for reflection utilities
27   * @author TiongHiang Lee (thlee@onemindsoft.org)
28   * @version $Id: ReflectUtilsTest.java,v 1.7 2006/08/02 00:07:24 thlee Exp $ $Name:  $
29   */
30  public class ReflectUtilsTest extends TestCase
31  {
32  
33      private Class[] _classNames = {java.lang.Object.class, java.lang.Integer.class, java.lang.Float.class, java.lang.Double.class,
34              java.lang.Long.class, java.lang.Boolean.class, java.lang.String.class, java.lang.Double.class, java.lang.Byte.class,
35              java.lang.Character.class, java.lang.Exception.class, java.lang.Class.class};
36  
37      /*** a test class * */
38      public static class TestClass
39      {
40  
41          /***
42           * Static method
43           */
44          public static void staticMethod()
45          {
46          }
47  
48          public String normalMethod(Object obj)
49          {
50              return "A";
51              //do nothing
52          }
53  
54          public String normalMethod(Integer obj)
55          {
56              return "B";
57              //do nothing
58          }
59  
60          public String normalMethod(String obj)
61          {
62              return "C";
63              //do nothing
64          }
65  
66          public String similarMethod(short a)
67          {
68              return "short";
69          }
70  
71          public String similarMethod(int a)
72          {
73              return "int";
74          }
75  
76          public String similarMethod(long a)
77          {
78              return "long";
79          }
80      }
81  
82      public static class TestSubClass extends TestClass
83      {
84      }
85  
86      /*** test the util * */
87      public void testStaticMethodInvocation() throws Exception
88      {
89          ReflectUtils.invoke(TestClass.class, "staticMethod", null);
90          ReflectUtils.invoke(TestSubClass.class, "staticMethod", null);
91      }
92  
93      public void testClassMethodInvocation() throws Exception
94      {
95          ReflectUtils.invoke(new TestSubClass().getClass(), "getName", null);
96          ReflectUtils.invoke(new TestSubClass().getClass(), "getName", null);
97      }
98  
99      public void testObjectMethodInvcation() throws Exception
100     {
101         ReflectUtils.invoke(new TestClass(), "normalMethod", new Object[]{null});
102     }
103 
104     public void testMethodSearch() throws Exception
105     {
106         Object result = ReflectUtils.invoke(new TestSubClass(), "similarMethod", new Object[]{new Long(1)});
107         assertTrue(result.equals("long"));
108         result = ReflectUtils.invoke(new TestSubClass(), "similarMethod", new Object[]{new Integer(1)});
109         assertTrue(result.equals("int"));
110         result = ReflectUtils.invoke(new TestSubClass(), "similarMethod", new Object[]{new Short((short) 1)});
111         assertTrue(result.equals("short"));
112         Object args[] = new Object[] { new Integer(1), new Integer(1) } ;
113         ReflectUtils.invoke(Math.class, "pow", args);
114     }
115 
116     public void testClassCache() throws Exception
117     {
118         long start = System.currentTimeMillis();
119         ReflectUtils.setClassCaching(false);
120         for (int i = 0; i < 10000; i++)
121         {
122             lookForClasses();
123         }
124         long end = System.currentTimeMillis();
125         System.out.println("Time used for class lookup without caching = " + (end - start));
126         start = System.currentTimeMillis();
127         ReflectUtils.setClassCaching(true);
128         start = System.currentTimeMillis();
129         for (int i = 0; i < 10000; i++)
130         {
131             lookForClasses();
132         }
133         end = System.currentTimeMillis();
134         System.out.println("Time used for class lookup with caching = " + (end - start));
135     }
136 
137     public void testMethodCache() throws Exception
138     {
139         long start = System.currentTimeMillis();
140         ReflectUtils.setMethodCaching(false);
141         for (int i = 0; i < 10000; i++)
142         {
143             lookForMethods();
144         }
145         long end = System.currentTimeMillis();
146         System.out.println("Time used for method lookup without caching = " + (end - start));
147         start = System.currentTimeMillis();
148         ReflectUtils.setMethodCaching(true);
149         start = System.currentTimeMillis();
150         for (int i = 0; i < 10000; i++)
151         {
152             lookForMethods();
153         }
154         end = System.currentTimeMillis();
155         System.out.println("Time used for method lookup with caching = " + (end - start));
156     }
157 
158     private void lookForClasses() throws Exception
159     {
160         for (int i = 0; i < _classNames.length; i++)
161         {
162             ReflectUtils.getClass(_classNames[i].getName());
163         }
164     }
165 
166     private void lookForMethods() throws Exception
167     {
168         for (int i = 0; i < _classNames.length; i++)
169         {
170             ReflectUtils.getMethod(_classNames[i], "equals", new Class[]{Object.class});
171         }
172     }
173 
174     public void testPrimitiveArgumentCheck() throws Exception
175     {
176         assertTrue(ReflectUtils.isCompatible(new Class[]{Long.TYPE}, new Object[]{new Long(1)}));
177         assertTrue(ReflectUtils.isCompatible(new Class[]{Long.TYPE}, new Object[]{new Integer(1)}));
178         assertFalse(ReflectUtils.isCompatible(new Class[]{Long.TYPE}, new Object[]{new Float(1.5)}));
179         assertFalse(ReflectUtils.isCompatible(new Class[]{Long.TYPE}, new Object[]{new Double(1.5)}));
180         assertTrue(ReflectUtils.isCompatible(new Class[]{Long.TYPE}, new Class[]{Long.class}));
181         assertTrue(ReflectUtils.isCompatible(new Class[]{Long.TYPE}, new Class[]{Integer.class}));
182         assertFalse(ReflectUtils.isCompatible(new Class[]{Long.TYPE}, new Class[]{Float.class}));
183         assertFalse(ReflectUtils.isCompatible(new Class[]{Long.TYPE}, new Class[]{Double.class}));
184     }
185 
186     public static void main(String[] args) throws Exception
187     {
188         ReflectUtilsTest test = new ReflectUtilsTest();
189         test.testClassCache();
190     }
191 }