001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.services;
016:
017: import java.io.IOException;
018: import java.io.ObjectInput;
019: import java.io.ObjectInputStream;
020: import java.lang.reflect.Method;
021: import java.sql.SQLException;
022:
023: import org.apache.tapestry.ioc.test.IOCTestCase;
024: import org.testng.annotations.Test;
025:
026: public class MethodSignatureTest extends IOCTestCase {
027: private MethodSignature find(Class sourceClass, String methodName) {
028: Method[] methods = sourceClass.getMethods();
029:
030: for (int i = 0; i < methods.length; i++) {
031: Method m = methods[i];
032:
033: if (m.getName().equals(methodName))
034: return new MethodSignature(m);
035: }
036:
037: unreachable();
038: return null;
039: }
040:
041: @Test
042: public void excercize_equals_and_hashcode() {
043: MethodSignature m1 = find(Object.class, "hashCode");
044: MethodSignature m2 = find(Boolean.class, "hashCode");
045:
046: assertEquals(m1.hashCode(), m2.hashCode());
047: assertTrue(m1.equals(m2));
048:
049: m1 = find(String.class, "charAt");
050: m2 = find(StringBuilder.class, "charAt");
051:
052: assertEquals(m1.hashCode(), m2.hashCode());
053: assertTrue(m1.equals(m2));
054:
055: m1 = find(ObjectInput.class, "close");
056: m2 = find(ObjectInputStream.class, "close");
057:
058: assertEquals(m1.hashCode(), m2.hashCode());
059: assertTrue(m1.equals(m2));
060: }
061:
062: @Test
063: public void equals_and_hashcode_with_null_parameters_and_exception_lists() {
064: MethodSignature m1 = new MethodSignature(void.class, "foo",
065: null, null);
066: MethodSignature m2 = new MethodSignature(void.class, "foo",
067: new Class[0], new Class[0]);
068:
069: assertEquals(m1, m2);
070: assertEquals(m2, m1);
071:
072: assertEquals(m1.hashCode(), m2.hashCode());
073: }
074:
075: @Test
076: public void equals_with_name_mismatch() {
077: MethodSignature m1 = new MethodSignature(void.class, "foo",
078: null, null);
079: MethodSignature m2 = new MethodSignature(void.class, "bar",
080: null, null);
081:
082: assertEquals(false, m1.equals(m2));
083: }
084:
085: @Test
086: public void equals_with_parameters_mismatch() {
087: MethodSignature m1 = new MethodSignature(void.class, "foo",
088: new Class[] { String.class }, null);
089: MethodSignature m2 = new MethodSignature(void.class, "foo",
090: new Class[] { Boolean.class }, null);
091:
092: assertEquals(false, m1.equals(m2));
093: }
094:
095: @Test
096: public void equals_with_null() {
097: MethodSignature m1 = new MethodSignature(void.class, "foo",
098: null, null);
099:
100: assertEquals(m1.equals(null), false);
101: }
102:
103: @Test
104: public void equals_with_not_method_signature() {
105: MethodSignature m1 = new MethodSignature(void.class, "foo",
106: null, null);
107:
108: assertEquals(m1.equals("Method Signature"), false);
109: }
110:
111: @Test
112: public void to_string() {
113: MethodSignature m = find(String.class, "getChars");
114:
115: assertEquals(m.toString(),
116: "void getChars(int, int, char[], int)");
117:
118: m = find(Class.class, "newInstance");
119:
120: assertEquals(
121: m.toString(),
122: "java.lang.Object newInstance() throws java.lang.InstantiationException, java.lang.IllegalAccessException");
123: }
124:
125: @Test
126: public void unique_id() {
127: MethodSignature m = find(String.class, "getChars");
128:
129: assertEquals(m.getUniqueId(), "getChars(int,int,char[],int)");
130:
131: m = find(Class.class, "newInstance");
132:
133: assertEquals(m.getUniqueId(), "newInstance()");
134: }
135:
136: @Test
137: public void overriding_signature_type_mismatch() {
138: MethodSignature m1 = new MethodSignature(void.class, "foo",
139: null, null);
140: MethodSignature m2 = new MethodSignature(int.class, "foo",
141: null, null);
142:
143: assertEquals(m1.isOverridingSignatureOf(m2), false);
144: }
145:
146: @Test
147: public void overriding_signature_name_mismatch() {
148: MethodSignature m1 = new MethodSignature(void.class, "foo",
149: null, null);
150: MethodSignature m2 = new MethodSignature(void.class, "bar",
151: null, null);
152:
153: assertEquals(m1.isOverridingSignatureOf(m2), false);
154: }
155:
156: @Test
157: public void overriding_signature_parameters_mismatch() {
158: MethodSignature m1 = new MethodSignature(void.class, "foo",
159: null, null);
160: MethodSignature m2 = new MethodSignature(void.class, "foo",
161: new Class[] { String.class }, null);
162:
163: assertEquals(m1.isOverridingSignatureOf(m2), false);
164: }
165:
166: @Test
167: public void overriding_signature() {
168: MethodSignature m1 = new MethodSignature(void.class, "close",
169: null, new Class[] { Exception.class });
170: MethodSignature m2 = new MethodSignature(void.class, "close",
171: null, new Class[] { RuntimeException.class });
172:
173: assertEquals(m1.isOverridingSignatureOf(m2), true);
174: assertEquals(m2.isOverridingSignatureOf(m1), false);
175: }
176:
177: /**
178: * Tests a shorcut used when one signature has zero exceptions.
179: */
180: @Test
181: public void overriding_signature_with_no_exceptions() {
182: MethodSignature m1 = new MethodSignature(void.class, "close",
183: null, null);
184: MethodSignature m2 = new MethodSignature(void.class, "close",
185: null, new Class[] { RuntimeException.class });
186:
187: assertEquals(m1.isOverridingSignatureOf(m2), false);
188: assertEquals(m2.isOverridingSignatureOf(m1), true);
189: }
190:
191: /**
192: * Fill in code coverage for multiple matched signatures.
193: */
194: @Test
195: public void overriding_signature_with_multiple_matched_exceptions() {
196: MethodSignature m1 = new MethodSignature(void.class, "close",
197: null, new Class[] { SQLException.class,
198: NumberFormatException.class });
199: MethodSignature m2 = new MethodSignature(void.class, "close",
200: null, new Class[] { SQLException.class,
201: IOException.class });
202:
203: assertEquals(m1.isOverridingSignatureOf(m2), false);
204: assertEquals(m2.isOverridingSignatureOf(m1), false);
205: }
206: }
|