001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test;
008:
009: import junit.framework.TestCase;
010: import org.codehaus.aspectwerkz.reflect.MethodComparator;
011: import org.codehaus.aspectwerkz.reflect.MethodComparator;
012: import org.codehaus.aspectwerkz.reflect.ClassInfo;
013: import org.codehaus.aspectwerkz.reflect.MethodInfo;
014: import org.codehaus.aspectwerkz.reflect.impl.java.JavaClassInfo;
015:
016: import java.lang.reflect.Array;
017: import java.lang.reflect.Method;
018:
019: /**
020: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
021: */
022: public class MethodComparatorTest extends TestCase {
023: public void testCompare() {
024: Method method1 = null;
025: Method method11 = null;
026: Method method2 = null;
027: Method method3 = null;
028: Method method4 = null;
029: Method method5 = null;
030: Method method6 = null;
031: try {
032: method1 = this .getClass().getMethod(
033: "__generated$_AW_$method1", new Class[] {});
034: method11 = this .getClass().getMethod(
035: "__generated$_AW_$method1$x", new Class[] {});
036: method2 = this .getClass().getMethod(
037: "__generated$_AW_$method1",
038: new Class[] { int.class });
039: method3 = this .getClass().getMethod(
040: "__generated$_AW_$method2", new Class[] {});
041: method4 = this .getClass().getMethod(
042: "__generated$_AW_$method2",
043: new Class[] { int.class });
044: method5 = this .getClass().getMethod(
045: "__generated$_AW_$method2",
046: new Class[] { String.class });
047: method6 = this .getClass().getMethod(
048: "__generated$_AW_$method2",
049: new Class[] { Array.newInstance(String.class, 1)
050: .getClass() });
051: } catch (Exception e) {
052: throw new RuntimeException("exception unexpected: " + e);
053: }
054: assertTrue(0 == MethodComparator.getInstance(
055: MethodComparator.PREFIXED_METHOD).compare(method1,
056: method1));
057: assertTrue(0 == MethodComparator.getInstance(
058: MethodComparator.PREFIXED_METHOD).compare(method2,
059: method2));
060: assertTrue(0 > MethodComparator.getInstance(
061: MethodComparator.PREFIXED_METHOD).compare(method1,
062: method2));
063: assertTrue(0 < MethodComparator.getInstance(
064: MethodComparator.PREFIXED_METHOD).compare(method2,
065: method1));
066: assertTrue(0 > MethodComparator.getInstance(
067: MethodComparator.PREFIXED_METHOD).compare(method1,
068: method11));
069: assertTrue(0 > MethodComparator.getInstance(
070: MethodComparator.PREFIXED_METHOD).compare(method3,
071: method4));
072: assertTrue(0 < MethodComparator.getInstance(
073: MethodComparator.PREFIXED_METHOD).compare(method4,
074: method3));
075: assertTrue(0 > MethodComparator.getInstance(
076: MethodComparator.PREFIXED_METHOD).compare(method1,
077: method4));
078: assertTrue(0 < MethodComparator.getInstance(
079: MethodComparator.PREFIXED_METHOD).compare(method4,
080: method1));
081: assertTrue(0 < MethodComparator.getInstance(
082: MethodComparator.PREFIXED_METHOD).compare(method3,
083: method2));
084: assertTrue(0 > MethodComparator.getInstance(
085: MethodComparator.PREFIXED_METHOD).compare(method2,
086: method3));
087: assertTrue(0 > MethodComparator.getInstance(
088: MethodComparator.PREFIXED_METHOD).compare(method4,
089: method5));
090:
091: // AW-104 test
092: assertTrue(0 > MethodComparator.getInstance(
093: MethodComparator.PREFIXED_METHOD).compare(method5,
094: method6));
095: }
096:
097: public static void main(String[] args) {
098: junit.textui.TestRunner.run(suite());
099: }
100:
101: public static junit.framework.Test suite() {
102: return new junit.framework.TestSuite(MethodComparatorTest.class);
103: }
104:
105: public void __generated$_AW_$method1() {
106: }
107:
108: public void __generated$_AW_$method1$x() {
109: }
110:
111: public void __generated$_AW_$method1(int i) {
112: }
113:
114: public void __generated$_AW_$method2() {
115: }
116:
117: public void __generated$_AW_$method2(int i) {
118: }
119:
120: public void __generated$_AW_$method2(String i) {
121: }
122:
123: public void __generated$_AW_$method2(String[] i) {
124: }
125:
126: public static interface TestInterface {
127: void test(String s);//test1
128:
129: void test(String[] s);//test2
130: }
131:
132: public void testMethodComparison() {
133: ClassInfo theTest = JavaClassInfo
134: .getClassInfo(TestInterface.class);
135: MethodInfo test1 = null;
136: MethodInfo test2 = null;
137: for (int i = 0; i < theTest.getMethods().length; i++) {
138: MethodInfo methodInfo = theTest.getMethods()[i];
139: if (methodInfo.getName().equals("test")) {
140: if (methodInfo.getParameterTypes()[0].getSignature()
141: .startsWith("[")) {
142: test2 = methodInfo;
143: } else {
144: test1 = methodInfo;
145: }
146: }
147: }
148:
149: assertTrue(0 > MethodComparator.getInstance(
150: MethodComparator.METHOD_INFO).compare(test1, test2));
151: assertTrue(0 == MethodComparator.getInstance(
152: MethodComparator.METHOD_INFO).compare(test1, test1));
153:
154: }
155:
156: }
|