001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.object.bytecode.aspectwerkz;
005:
006: import com.tc.aspectwerkz.reflect.ClassInfo;
007: import com.tc.aspectwerkz.reflect.MethodInfo;
008: import com.tc.aspectwerkz.reflect.impl.java.JavaMethodInfo;
009: import com.tc.object.BaseDSOTestCase;
010: import com.tc.object.config.DSOClientConfigHelper;
011: import com.tctest.AsmMethodInfoTestHelper;
012:
013: import java.lang.reflect.Method;
014:
015: /**
016: * Unit test for AsmMethodInfo
017: */
018: public class AsmMethodInfoTest extends BaseDSOTestCase {
019:
020: AsmMethodInfo methodInfo;
021: MethodInfo javaMethodInfo;
022: DSOClientConfigHelper config;
023:
024: private void setUp(Method method) throws Exception {
025: this .methodInfo = AsmMethodInfo.createNewAsmMethodInfo(method);
026: javaMethodInfo = JavaMethodInfo.getMethodInfo(method);
027: config = configHelper();
028: }
029:
030: public void testAsmMethodInfoTestHelperTest4() throws Exception {
031: Method method = AsmMethodInfoTestHelper.class.getMethod(
032: "test4", new Class[] { Integer.TYPE, Object.class });
033: setUp(method);
034: assertTrue(compareMethodInfos(this .javaMethodInfo,
035: this .methodInfo));
036:
037: String expression = "long "
038: + AsmMethodInfoTestHelper.class.getName() + "."
039: + method.getName() + "(int, java.lang.Object)";
040: assertTrue(config.matches(expression, this .javaMethodInfo));
041: assertTrue(config.matches(expression, this .methodInfo));
042: }
043:
044: public void publicVoidTestMethod() throws Exception { /**/
045: }
046:
047: public void testPublicVoidTestMethod() throws Exception {
048: Method method = getClass().getMethod("publicVoidTestMethod",
049: new Class[0]);
050: setUp(method);
051: assertTrue(compareMethodInfos(this .javaMethodInfo,
052: this .methodInfo));
053: }
054:
055: public void testPublicVoidTestMethodMatch() throws Exception {
056: String methodName = "publicVoidTestMethod";
057: Method method = getClass().getMethod(methodName, new Class[0]);
058: setUp(method);
059: String expression = "* " + getClass().getName() + "."
060: + methodName + "()";
061: assertTrue(config.matches(expression, this .javaMethodInfo));
062: assertTrue(config.matches(expression, this .methodInfo));
063: }
064:
065: public void publicVoidTestMethodStringInt(String string, int i)
066: throws Exception { /**/
067: }
068:
069: public void testPublicVoidTestMethodStringInt() throws Exception {
070: Method method = getClass().getMethod(
071: "publicVoidTestMethodStringInt",
072: new Class[] { String.class, Integer.TYPE });
073: setUp(method);
074: assertTrue(compareMethodInfos(this .javaMethodInfo,
075: this .methodInfo));
076: }
077:
078: public long[][][] publicLong3TestMethodDouble4(
079: double[][][][] double4) throws Exception {
080: return null;
081: }
082:
083: public void testPublicLong3TestMethodDouble4() throws Exception {
084: Method method = getClass().getMethod(
085: "publicLong3TestMethodDouble4",
086: new Class[] { double[][][][].class });
087: setUp(method);
088: assertTrue(compareMethodInfos(this .javaMethodInfo,
089: this .methodInfo));
090:
091: }
092:
093: private static boolean compareMethodInfos(MethodInfo source,
094: MethodInfo target) {
095: // test the method name;
096: // System.out.println("Testing method name: " + source.getName() + ", " +
097: // target.getName());
098: if (source.getModifiers() != target.getModifiers()) {
099: return false;
100: }
101: if (!source.getName().equals(target.getName())) {
102: return false;
103: }
104: // test the class name
105: if (!compareClassInfos(source.getDeclaringType(), target
106: .getDeclaringType())) {
107: return false;
108: }
109: // test the return type
110: if (!compareClassInfos(source.getReturnType(), target
111: .getReturnType())) {
112: return false;
113: }
114: // test the parameter types
115: if (!(source.getParameterTypes().length == target
116: .getParameterTypes().length)) {
117: return false;
118: }
119: for (int i = 0; i < source.getParameterTypes().length; i++) {
120: if (!compareClassInfos(source.getParameterTypes()[i],
121: target.getParameterTypes()[i])) {
122: return false;
123: }
124: }
125: if (!(source.getExceptionTypes().length == target
126: .getExceptionTypes().length)) {
127: return false;
128: }
129: // XXX: If the exceptions aren't reported in the same order, this test will
130: // fail, which
131: // is probably wrong.
132: for (int i = 0; i < source.getExceptionTypes().length; i++) {
133: if (!compareClassInfos(source.getExceptionTypes()[i],
134: target.getExceptionTypes()[i])) {
135: return false;
136: }
137: }
138: return true;
139: }
140:
141: private static boolean compareClassInfos(ClassInfo source,
142: ClassInfo target) {
143: // System.out.println("source name: "+source.getName() + ", target name: " +
144: // target.getName());
145: return source.getName().equals(target.getName());
146: }
147:
148: }
|