KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > services > TestMethodSignature


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package hivemind.test.services;
16
17 import java.io.IOException JavaDoc;
18 import java.io.ObjectInput JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 import org.apache.hivemind.service.MethodSignature;
24 import org.apache.hivemind.test.HiveMindTestCase;
25
26 /**
27  * Tests for the {@link org.apache.hivemind.service.impl.MethodSignature} class.
28  *
29  * @author Howard Lewis Ship
30  */

31 public class TestMethodSignature extends HiveMindTestCase
32 {
33     private MethodSignature find(Class JavaDoc sourceClass, String JavaDoc methodName)
34     {
35         Method JavaDoc[] methods = sourceClass.getMethods();
36
37         for (int i = 0; i < methods.length; i++)
38         {
39             Method JavaDoc m = methods[i];
40
41             if (m.getName().equals(methodName))
42                 return new MethodSignature(m);
43         }
44
45         unreachable();
46         return null;
47     }
48
49     public void testEqualsAndHashCode()
50     {
51         MethodSignature m1 = find(Object JavaDoc.class, "hashCode");
52         MethodSignature m2 = find(Boolean JavaDoc.class, "hashCode");
53
54         assertEquals(m1.hashCode(), m2.hashCode());
55         assertTrue(m1.equals(m2));
56
57         m1 = find(String JavaDoc.class, "charAt");
58         m2 = find(StringBuffer JavaDoc.class, "charAt");
59
60         assertEquals(m1.hashCode(), m2.hashCode());
61         assertTrue(m1.equals(m2));
62
63         m1 = find(ObjectInput JavaDoc.class, "close");
64         m2 = find(ObjectInputStream JavaDoc.class, "close");
65
66         assertEquals(m1.hashCode(), m2.hashCode());
67         assertTrue(m1.equals(m2));
68     }
69
70     public void testEqualsAndHashCodeWithNulls()
71     {
72         MethodSignature m1 = new MethodSignature(void.class, "foo", null, null);
73         MethodSignature m2 = new MethodSignature(void.class, "foo", new Class JavaDoc[0], new Class JavaDoc[0]);
74
75         assertEquals(m1, m2);
76         assertEquals(m2, m1);
77
78         assertEquals(m1.hashCode(), m2.hashCode());
79     }
80
81     /** @since 1.1 */
82
83     public void testEqualsNameMismatch()
84     {
85         MethodSignature m1 = new MethodSignature(void.class, "foo", null, null);
86         MethodSignature m2 = new MethodSignature(void.class, "bar", null, null);
87
88         assertEquals(false, m1.equals(m2));
89     }
90
91     /** @since 1.1 */
92     public void testParametersMismatch()
93     {
94         MethodSignature m1 = new MethodSignature(void.class, "foo", new Class JavaDoc[]
95         { String JavaDoc.class }, null);
96         MethodSignature m2 = new MethodSignature(void.class, "foo", new Class JavaDoc[]
97         { Boolean JavaDoc.class }, null);
98
99         assertEquals(false, m1.equals(m2));
100     }
101
102     /** @since 1.1 */
103
104     public void testEqualsNull()
105     {
106         MethodSignature m1 = new MethodSignature(void.class, "foo", null, null);
107
108         assertEquals(false, m1.equals(null));
109     }
110
111     /** @since 1.1 */
112
113     public void testEqualsNonMethodSignature()
114     {
115         MethodSignature m1 = new MethodSignature(void.class, "foo", null, null);
116
117         assertEquals(false, m1.equals("Method Signature"));
118     }
119
120     public void testToString()
121     {
122         MethodSignature m = find(String JavaDoc.class, "getChars");
123
124         assertEquals("void getChars(int, int, char[], int)", m.toString());
125
126         m = find(Class JavaDoc.class, "newInstance");
127
128         assertEquals(
129                 "java.lang.Object newInstance() throws java.lang.InstantiationException, java.lang.IllegalAccessException",
130                 m.toString());
131     }
132
133     /** @since 1.1 */
134     public void testGetUniqueId()
135     {
136         MethodSignature m = find(String JavaDoc.class, "getChars");
137
138         assertEquals("getChars(int,int,char[],int)", m.getUniqueId());
139
140         m = find(Class JavaDoc.class, "newInstance");
141
142         assertEquals("newInstance()", m.getUniqueId());
143     }
144
145     /** @since 1.1 */
146
147     public void testOverridingSigTypeMismatch()
148     {
149         MethodSignature m1 = new MethodSignature(void.class, "foo", null, null);
150         MethodSignature m2 = new MethodSignature(int.class, "foo", null, null);
151
152         assertEquals(false, m1.isOverridingSignatureOf(m2));
153     }
154
155     /** @since 1.1 */
156
157     public void testOverridingSigNameMismatch()
158     {
159         MethodSignature m1 = new MethodSignature(void.class, "foo", null, null);
160         MethodSignature m2 = new MethodSignature(void.class, "bar", null, null);
161
162         assertEquals(false, m1.isOverridingSignatureOf(m2));
163     }
164
165     /** @since 1.1 */
166
167     public void testOverridingSigParametersMismatch()
168     {
169         MethodSignature m1 = new MethodSignature(void.class, "foo", null, null);
170         MethodSignature m2 = new MethodSignature(void.class, "foo", new Class JavaDoc[]
171         { String JavaDoc.class }, null);
172
173         assertEquals(false, m1.isOverridingSignatureOf(m2));
174     }
175
176     /** @since 1.1 */
177
178     public void testOverridingSig()
179     {
180         MethodSignature m1 = new MethodSignature(void.class, "close", null, new Class JavaDoc[]
181         { Exception JavaDoc.class });
182         MethodSignature m2 = new MethodSignature(void.class, "close", null, new Class JavaDoc[]
183         { RuntimeException JavaDoc.class });
184
185         assertEquals(true, m1.isOverridingSignatureOf(m2));
186         assertEquals(false, m2.isOverridingSignatureOf(m1));
187     }
188
189     /**
190      * Tests a shorcut used when one signature has zero exceptions.
191      *
192      * @since 1.1
193      */

194     public void testOverridingSigShortcut()
195     {
196         MethodSignature m1 = new MethodSignature(void.class, "close", null, null);
197         MethodSignature m2 = new MethodSignature(void.class, "close", null, new Class JavaDoc[]
198         { RuntimeException JavaDoc.class });
199
200         assertEquals(false, m1.isOverridingSignatureOf(m2));
201         assertEquals(true, m2.isOverridingSignatureOf(m1));
202     }
203
204     /**
205      * Fill in code coverage for multiple matchd signatures.
206      *
207      * @since 1.1
208      */

209     public void testMultipleExceptionsToMatch()
210     {
211         MethodSignature m1 = new MethodSignature(void.class, "close", null, new Class JavaDoc[]
212         { SQLException JavaDoc.class, NumberFormatException JavaDoc.class });
213         MethodSignature m2 = new MethodSignature(void.class, "close", null, new Class JavaDoc[]
214         { SQLException JavaDoc.class, IOException JavaDoc.class });
215
216         assertEquals(false, m1.isOverridingSignatureOf(m2));
217         assertEquals(false, m2.isOverridingSignatureOf(m1));
218     }
219 }
Popular Tags