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.services;
016:
017: import static org.testng.Assert.assertEquals;
018: import static org.testng.Assert.assertFalse;
019:
020: import java.lang.reflect.Modifier;
021: import java.util.Arrays;
022: import java.util.Collections;
023: import java.util.List;
024:
025: import org.apache.tapestry.ioc.internal.util.CollectionFactory;
026: import org.testng.annotations.Test;
027:
028: /**
029: *
030: */
031: public class MethodSignatureTest {
032:
033: @Test
034: public void signature_toString() {
035: MethodSignature sig = new MethodSignature(Modifier.PUBLIC,
036: "int", "doSomething", new String[] {
037: "java.lang.String", "int" }, new String[] {
038: "java.lang.RuntimeException",
039: "org.foo.FredException" });
040:
041: assertEquals(
042: sig.toString(),
043: "public int doSomething(java.lang.String, int) throws java.lang.RuntimeException, org.foo.FredException");
044:
045: sig = new MethodSignature(Modifier.ABSTRACT
046: + Modifier.PROTECTED, "boolean", "misoHapi",
047: new String[0], new String[0]);
048:
049: assertEquals(sig.toString(),
050: "protected abstract boolean misoHapi()");
051: }
052:
053: @Test
054: public void medium_description() {
055: MethodSignature sig = new MethodSignature(Modifier.PUBLIC,
056: "int", "doSomething", new String[] {
057: "java.lang.String", "int" }, new String[] {
058: "java.lang.RuntimeException",
059: "org.foo.FredException" });
060:
061: assertEquals(sig.getMediumDescription(),
062: "doSomething(java.lang.String, int)");
063: }
064:
065: @Test
066: public void package_private_toString() {
067: MethodSignature sig = new MethodSignature(0, "int",
068: "packagePrivate", null, null);
069:
070: assertEquals(sig.toString(), "int packagePrivate()");
071: }
072:
073: @Test
074: public void null_value_for_parameters_and_exceptions() {
075: MethodSignature sig = new MethodSignature(Modifier.PUBLIC,
076: "int", "doSomething", null, null);
077:
078: assertEquals(sig.toString(), "public int doSomething()");
079:
080: assertEquals(sig.getParameterTypes(), new String[0]);
081: assertEquals(sig.getExceptionTypes(), new String[0]);
082: }
083:
084: @Test
085: public void getters() {
086: MethodSignature sig = new MethodSignature(Modifier.PUBLIC,
087: "int", "doSomething", new String[] {
088: "java.lang.String", "int" }, new String[] {
089: "java.lang.RuntimeException",
090: "org.foo.FredException" });
091:
092: assertEquals(sig.getModifiers(), Modifier.PUBLIC);
093: assertEquals(sig.getReturnType(), "int");
094: assertEquals(sig.getMethodName(), "doSomething");
095: assertEquals(sig.getParameterTypes(), new String[] {
096: "java.lang.String", "int" });
097: assertEquals(sig.getExceptionTypes(), new String[] {
098: "java.lang.RuntimeException", "org.foo.FredException" });
099: }
100:
101: @Test
102: public void sorting() {
103: MethodSignature s1 = new MethodSignature(Modifier.PUBLIC,
104: "void", "foo", null, null);
105: MethodSignature s2 = new MethodSignature(Modifier.PUBLIC,
106: "void", "bar", null, null);
107: MethodSignature s3 = new MethodSignature(Modifier.PUBLIC,
108: "void", "baz", null, null);
109: MethodSignature s4 = new MethodSignature(Modifier.PUBLIC,
110: "void", "baz", new String[] { "int" }, null);
111:
112: List<MethodSignature> list = CollectionFactory.newList(Arrays
113: .asList(s1, s2, s3, s4));
114:
115: Collections.sort(list);
116:
117: assertEquals(list, Arrays.asList(s2, s3, s4, s1));
118: }
119:
120: @Test
121: public void hash_code_and_equals() {
122: MethodSignature sig1 = new MethodSignature(Modifier.PUBLIC,
123: "int", "doSomething", new String[] { "int" },
124: new String[] { "org.foo.BarException" });
125: int hashCode1 = sig1.hashCode();
126:
127: // Check that same value returned each time.
128:
129: assertEquals(sig1.hashCode(), hashCode1);
130:
131: MethodSignature sig2 = new MethodSignature(Modifier.PUBLIC,
132: "int", "doSomething", new String[] { "int" },
133: new String[] { "org.foo.BarException" });
134:
135: assertEquals(sig2.hashCode(), hashCode1);
136: assertEquals(sig2, sig1);
137:
138: // Now work through the different properties, changing each one.
139:
140: sig2 = new MethodSignature(Modifier.PRIVATE, "int",
141: "doSomething", new String[] { "int" },
142: new String[] { "org.foo.BarException" });
143:
144: assertFalse(sig2.hashCode() == hashCode1);
145: assertFalse(sig2.equals(sig1));
146:
147: sig2 = new MethodSignature(Modifier.PUBLIC, "long",
148: "doSomething", new String[] { "int" },
149: new String[] { "org.foo.BarException" });
150:
151: assertFalse(sig2.hashCode() == hashCode1);
152: assertFalse(sig2.equals(sig1));
153:
154: sig2 = new MethodSignature(Modifier.PUBLIC, "int",
155: "doSomethingElse", new String[] { "int" },
156: new String[] { "org.foo.BarException" });
157:
158: assertFalse(sig2.hashCode() == hashCode1);
159: assertFalse(sig2.equals(sig1));
160:
161: sig2 = new MethodSignature(Modifier.PUBLIC, "int",
162: "doSomething", new String[] { "long" },
163: new String[] { "org.foo.BarException" });
164:
165: assertFalse(sig2.hashCode() == hashCode1);
166: assertFalse(sig2.equals(sig1));
167:
168: sig2 = new MethodSignature(Modifier.PUBLIC, "int",
169: "doSomething", new String[] { "int" }, new String[0]);
170:
171: assertFalse(sig2.hashCode() == hashCode1);
172: assertFalse(sig2.equals(sig1));
173:
174: // Other equality checks
175:
176: assertFalse(sig1.equals(null));
177: assertFalse(sig1.equals(""));
178: }
179:
180: /** Tests the simple, no arguments constructor. */
181: @Test
182: public void short_constructor() {
183: MethodSignature sig = new MethodSignature("pageLoad");
184:
185: assertEquals(sig.getModifiers(), Modifier.PUBLIC);
186: assertEquals(sig.getReturnType(), "void");
187: assertEquals(sig.getMethodName(), "pageLoad");
188: assertEquals(sig.getParameterTypes(), new String[0]);
189: assertEquals(sig.getExceptionTypes(), new String[0]);
190: }
191: }
|