1   /***************************************************************************************
2    * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package test;
9   
10  import junit.framework.TestCase;
11  import org.codehaus.aspectwerkz.reflect.MethodComparator;
12  import org.codehaus.aspectwerkz.reflect.MethodComparator;
13  import org.codehaus.aspectwerkz.reflect.ClassInfo;
14  import org.codehaus.aspectwerkz.reflect.MethodInfo;
15  import org.codehaus.aspectwerkz.reflect.impl.java.JavaClassInfo;
16  
17  import java.lang.reflect.Array;
18  import java.lang.reflect.Method;
19  
20  /***
21   * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
22   */
23  public class MethodComparatorTest extends TestCase {
24      public void testCompare() {
25          Method method1 = null;
26          Method method11 = null;
27          Method method2 = null;
28          Method method3 = null;
29          Method method4 = null;
30          Method method5 = null;
31          Method method6 = null;
32          try {
33              method1 = this.getClass().getMethod("__generated$_AW_$method1", new Class[]{});
34              method11 = this.getClass().getMethod("__generated$_AW_$method1$x", new Class[]{});
35              method2 = this.getClass().getMethod(
36                      "__generated$_AW_$method1", new Class[]{
37                          int.class
38                      }
39              );
40              method3 = this.getClass().getMethod("__generated$_AW_$method2", new Class[]{});
41              method4 = this.getClass().getMethod(
42                      "__generated$_AW_$method2", new Class[]{
43                          int.class
44                      }
45              );
46              method5 = this.getClass().getMethod(
47                      "__generated$_AW_$method2", new Class[]{
48                          String.class
49                      }
50              );
51              method6 = this.getClass().getMethod(
52                      "__generated$_AW_$method2", new Class[]{
53                          Array.newInstance(String.class, 1).getClass()
54                      }
55              );
56          } catch (Exception e) {
57              throw new RuntimeException("exception unexpected: " + e);
58          }
59          assertTrue(
60                  0 == MethodComparator.getInstance(MethodComparator.PREFIXED_METHOD).compare(
61                          method1,
62                          method1
63                  )
64          );
65          assertTrue(
66                  0 == MethodComparator.getInstance(MethodComparator.PREFIXED_METHOD).compare(
67                          method2,
68                          method2
69                  )
70          );
71          assertTrue(
72                  0 > MethodComparator.getInstance(MethodComparator.PREFIXED_METHOD).compare(
73                          method1,
74                          method2
75                  )
76          );
77          assertTrue(
78                  0 < MethodComparator.getInstance(MethodComparator.PREFIXED_METHOD).compare(
79                          method2,
80                          method1
81                  )
82          );
83          assertTrue(
84                  0 > MethodComparator.getInstance(MethodComparator.PREFIXED_METHOD).compare(
85                          method1,
86                          method11
87                  )
88          );
89          assertTrue(
90                  0 > MethodComparator.getInstance(MethodComparator.PREFIXED_METHOD).compare(
91                          method3,
92                          method4
93                  )
94          );
95          assertTrue(
96                  0 < MethodComparator.getInstance(MethodComparator.PREFIXED_METHOD).compare(
97                          method4,
98                          method3
99                  )
100         );
101         assertTrue(
102                 0 > MethodComparator.getInstance(MethodComparator.PREFIXED_METHOD).compare(
103                         method1,
104                         method4
105                 )
106         );
107         assertTrue(
108                 0 < MethodComparator.getInstance(MethodComparator.PREFIXED_METHOD).compare(
109                         method4,
110                         method1
111                 )
112         );
113         assertTrue(
114                 0 < MethodComparator.getInstance(MethodComparator.PREFIXED_METHOD).compare(
115                         method3,
116                         method2
117                 )
118         );
119         assertTrue(
120                 0 > MethodComparator.getInstance(MethodComparator.PREFIXED_METHOD).compare(
121                         method2,
122                         method3
123                 )
124         );
125         assertTrue(
126                 0 > MethodComparator.getInstance(MethodComparator.PREFIXED_METHOD).compare(
127                         method4,
128                         method5
129                 )
130         );
131 
132         // AW-104 test
133         assertTrue(
134                 0 > MethodComparator.getInstance(MethodComparator.PREFIXED_METHOD).compare(
135                         method5,
136                         method6
137                 )
138         );
139     }
140 
141     public static void main(String[] args) {
142         junit.textui.TestRunner.run(suite());
143     }
144 
145     public static junit.framework.Test suite() {
146         return new junit.framework.TestSuite(MethodComparatorTest.class);
147     }
148 
149     public void __generated$_AW_$method1() {
150     }
151 
152     public void __generated$_AW_$method1$x() {
153     }
154 
155     public void __generated$_AW_$method1(int i) {
156     }
157 
158     public void __generated$_AW_$method2() {
159     }
160 
161     public void __generated$_AW_$method2(int i) {
162     }
163 
164     public void __generated$_AW_$method2(String i) {
165     }
166 
167     public void __generated$_AW_$method2(String[] i) {
168     }
169 
170     public static interface TestInterface {
171          void test(String s);//test1
172          void test(String[] s);//test2
173     }
174 
175     public void testMethodComparison() {
176         ClassInfo theTest = JavaClassInfo.getClassInfo(TestInterface.class);
177         MethodInfo test1 = null;
178         MethodInfo test2 = null;
179         for (int i = 0; i < theTest.getMethods().length; i++) {
180             MethodInfo methodInfo = theTest.getMethods()[i];
181             if (methodInfo.getName().equals("test")) {
182                 if (methodInfo.getParameterTypes()[0].getSignature().startsWith("[")) {
183                     test2 = methodInfo;
184                 } else {
185                     test1 = methodInfo;
186                 }
187             }
188         }
189 
190         assertTrue(
191                 0 > MethodComparator.getInstance(MethodComparator.METHOD_INFO).compare(
192                         test1,
193                         test2
194                 ));
195         assertTrue(
196                 0 == MethodComparator.getInstance(MethodComparator.METHOD_INFO).compare(
197                         test1,
198                         test1
199                 ));
200 
201    }
202 
203 }