KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > methodhashing > MethodHashingTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.aop.methodhashing;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javassist.CtClass;
29 import javassist.CtMethod;
30 import junit.framework.TestCase;
31
32 import org.jboss.aop.util.ClassInfoMethodHashing;
33 import org.jboss.aop.util.JavassistMethodHashing;
34 import org.jboss.aop.util.MethodHashing;
35 import org.jboss.aop.util.ReflectToJavassist;
36 import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactory;
37 import org.jboss.reflect.plugins.javassist.JavassistTypeInfoFactory;
38 import org.jboss.reflect.spi.ClassInfo;
39 import org.jboss.reflect.spi.MethodInfo;
40
41 /**
42  * Test to make sure that the different mechanisms of creating method hashes return the same hashes
43  *
44  * Don't use security for this test
45  *
46  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
47  * @version $Revision: 46050 $
48  */

49 public class MethodHashingTestCase extends TestCase
50 {
51
52    public MethodHashingTestCase(String JavaDoc arg0)
53    {
54       // FIXME MethodHashingTestCase constructor
55
super(arg0);
56    }
57
58    public void testDeclaredMethodHashing() throws Exception JavaDoc
59    {
60       Class JavaDoc clazz = SubClass.class;
61       CtClass ctclass = getCtClass(clazz);
62       ClassInfo classInfoReflect = getIntrospectTypeInfo(clazz);
63       ClassInfo classInfoJavassist = getJavassistTypeInfo(clazz);
64       
65       Map JavaDoc reflectInfoMethods = ClassInfoMethodHashing.getDeclaredMethodMap(classInfoReflect);
66       Map JavaDoc javassistInfoMethods = ClassInfoMethodHashing.getDeclaredMethodMap(classInfoJavassist);
67       Map JavaDoc javassistMethods = JavassistMethodHashing.getDeclaredMethodMap(ctclass);
68       
69       compareMaps(reflectInfoMethods, javassistInfoMethods, 1);
70       compareMaps(reflectInfoMethods, javassistMethods, 1);
71       
72       for (Iterator JavaDoc it = reflectInfoMethods.keySet().iterator() ; it.hasNext() ; )
73       {
74          Long JavaDoc hash = (Long JavaDoc)it.next();
75          CtMethod ctmethod = (CtMethod)javassistMethods.get(hash);
76          MethodInfo methodInfo = (MethodInfo)reflectInfoMethods.get(hash);
77          compareMethods(methodInfo, ctmethod);
78          
79          MethodInfo methodInfo2 = (MethodInfo)javassistInfoMethods.get(hash);
80          compareMethods(methodInfo, methodInfo2);
81
82          Method JavaDoc method = MethodHashing.findMethodByHash(clazz, hash.longValue());
83          assertNotNull(method);
84          compareMethods(methodInfo, method);
85          
86          Method JavaDoc methodB = org.jboss.util.MethodHashing.findMethodByHash(clazz, hash.longValue());
87          assertNotNull(methodB);
88          compareMethods(methodInfo, methodB);
89       }
90    }
91    
92    public void testMethodHashing() throws Exception JavaDoc
93    {
94       Class JavaDoc clazz = SubClass.class;
95       CtClass ctclass = getCtClass(clazz);
96       ClassInfo classInfoReflect = getIntrospectTypeInfo(clazz);
97       ClassInfo classInfoJavassist = getJavassistTypeInfo(clazz);
98       
99       Map JavaDoc reflectInfoMethods = ClassInfoMethodHashing.getMethodMap(classInfoReflect);
100       Map JavaDoc javassistInfoMethods = ClassInfoMethodHashing.getMethodMap(classInfoJavassist);
101       Map JavaDoc javassistMethods = JavassistMethodHashing.getMethodMap(ctclass);
102       
103       compareMaps(reflectInfoMethods, javassistInfoMethods, 2);
104       compareMaps(reflectInfoMethods, javassistMethods, 2);
105       
106       for (Iterator JavaDoc it = reflectInfoMethods.keySet().iterator() ; it.hasNext() ; )
107       {
108          Long JavaDoc hash = (Long JavaDoc)it.next();
109          CtMethod ctmethod = (CtMethod)javassistMethods.get(hash);
110          MethodInfo methodInfo = (MethodInfo)reflectInfoMethods.get(hash);
111          compareMethods(methodInfo, ctmethod);
112          
113          MethodInfo methodInfo2 = (MethodInfo)javassistInfoMethods.get(hash);
114          compareMethods(methodInfo, methodInfo2);
115
116          Method JavaDoc method = MethodHashing.findMethodByHash(clazz, hash.longValue());
117          assertNotNull(method);
118          compareMethods(methodInfo, method);
119          
120          Method JavaDoc methodB = org.jboss.util.MethodHashing.findMethodByHash(clazz, hash.longValue());
121          assertNotNull(methodB);
122          compareMethods(methodInfo, methodB);
123       }
124    }
125    
126    private CtClass getCtClass(Class JavaDoc clazz) throws Exception JavaDoc
127    {
128       return ReflectToJavassist.classToJavassist(clazz);
129    }
130    
131    private ClassInfo getIntrospectTypeInfo(Class JavaDoc clazz)
132    {
133       IntrospectionTypeInfoFactory typeInfoFactory = new IntrospectionTypeInfoFactory();
134       ClassInfo classInfo = (ClassInfo)typeInfoFactory.getTypeInfo(clazz);
135       return classInfo;
136    }
137    
138    private ClassInfo getJavassistTypeInfo(Class JavaDoc clazz)
139    {
140       JavassistTypeInfoFactory typeInfoFactory = new JavassistTypeInfoFactory();
141       ClassInfo classInfo = (ClassInfo)typeInfoFactory.getTypeInfo(clazz);
142       return classInfo;
143    }
144    
145    private void compareMaps(Map JavaDoc mapA, Map JavaDoc mapB, int expecedSize)
146    {
147       assertEquals(expecedSize, mapA.size());
148       assertEquals(expecedSize, mapB.size());
149       
150       for (Iterator JavaDoc it = mapA.keySet().iterator() ; it.hasNext() ; )
151       {
152          Long JavaDoc l = (Long JavaDoc)it.next();
153          assertNotNull(mapB.get(l));
154       }
155    }
156    
157    private void compareMethods(MethodInfo methodInfo, CtMethod method) throws Exception JavaDoc
158    {
159       System.out.println("-----> method " + method);
160       assertEquals(methodInfo.getName(), method.getName());
161       assertEquals(methodInfo.getDeclaringClass().getName(), method.getDeclaringClass().getName());
162       assertEquals(methodInfo.getReturnType().getName(), method.getReturnType().getName());
163       assertEquals(methodInfo.getModifiers(), method.getModifiers());
164       assertEquals(methodInfo.getParameterTypes().length, method.getParameterTypes().length);
165    }
166
167    private void compareMethods(MethodInfo methodInfo, MethodInfo method) throws Exception JavaDoc
168    {
169       System.out.println("-----> method " + method);
170       assertEquals(methodInfo.getName(), method.getName());
171       assertEquals(methodInfo.getDeclaringClass().getName(), method.getDeclaringClass().getName());
172       assertEquals(methodInfo.getReturnType().getName(), method.getReturnType().getName());
173       assertEquals(methodInfo.getModifiers(), method.getModifiers());
174       assertEquals(methodInfo.getParameterTypes().length, method.getParameterTypes().length);
175    }
176
177    private void compareMethods(MethodInfo methodInfo, Method JavaDoc method) throws Exception JavaDoc
178    {
179       System.out.println("-----> method " + method);
180       assertEquals(methodInfo.getName(), method.getName());
181       assertEquals(methodInfo.getDeclaringClass().getName(), method.getDeclaringClass().getName());
182       assertEquals(methodInfo.getReturnType().getName(), method.getReturnType().getName());
183       assertEquals(methodInfo.getModifiers(), method.getModifiers());
184       assertEquals(methodInfo.getParameterTypes().length, method.getParameterTypes().length);
185    }
186
187 }
188
Popular Tags