Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6558e2f45fce37f5d13d9dd504c51c9951ba7b13 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*******************************************************************************
 * Copyright (c) 2008, 2009 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal;

import java.lang.reflect.Method;
import java.util.Arrays;
import junit.framework.TestCase;
import org.eclipse.jpt.common.utility.JavaType;
import org.eclipse.jpt.common.utility.MethodSignature;
import org.eclipse.jpt.common.utility.internal.SimpleJavaType;
import org.eclipse.jpt.common.utility.internal.SimpleMethodSignature;

@SuppressWarnings("nls")
public class MethodSignatureTests extends TestCase {

	public MethodSignatureTests(String name) {
		super(name);
	}

	public void testInvalidNameNull() throws Exception {
		boolean exCaught = false;
		try {
			MethodSignature methodSignature = new SimpleMethodSignature((String) null);
			fail("invalid MethodSignature: " + methodSignature);
		} catch (IllegalArgumentException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testInvalidNameEmpty() throws Exception {
		boolean exCaught = false;
		try {
			MethodSignature methodSignature = new SimpleMethodSignature("");
			fail("invalid MethodSignature: " + methodSignature);
		} catch (IllegalArgumentException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testInvalidParameterTypesNull() throws Exception {
		boolean exCaught = false;
		try {
			MethodSignature methodSignature = new SimpleMethodSignature("foo", (JavaType[]) null);
			fail("invalid MethodSignature: " + methodSignature);
		} catch (IllegalArgumentException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testInvalidParameterTypesNullItem() throws Exception {
		boolean exCaught = false;
		try {
			MethodSignature methodSignature = new SimpleMethodSignature("foo", new JavaType[1]);
			fail("invalid MethodSignature: " + methodSignature);
		} catch (IllegalArgumentException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testInvalidParameterTypesVoidItem() throws Exception {
		JavaType jt = new SimpleJavaType(void.class.getName());
		boolean exCaught = false;
		try {
			MethodSignature methodSignature = new SimpleMethodSignature("foo", new JavaType[] {jt});
			fail("invalid MethodSignature: " + methodSignature);
		} catch (IllegalArgumentException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testInvalidParameterTypeNamesNull() throws Exception {
		boolean exCaught = false;
		try {
			MethodSignature methodSignature = new SimpleMethodSignature("foo", (String[]) null);
			fail("invalid MethodSignature: " + methodSignature);
		} catch (IllegalArgumentException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testInvalidParameterTypeNamesNullItem() throws Exception {
		boolean exCaught = false;
		try {
			MethodSignature methodSignature = new SimpleMethodSignature("foo", new String[1]);
			fail("invalid MethodSignature: " + methodSignature);
		} catch (IllegalArgumentException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testInvalidParameterJavaClassesNull() throws Exception {
		boolean exCaught = false;
		try {
			MethodSignature methodSignature = new SimpleMethodSignature("foo", (Class<?>[]) null);
			fail("invalid MethodSignature: " + methodSignature);
		} catch (IllegalArgumentException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testInvalidParameterJavaClassesNullItem() throws Exception {
		boolean exCaught = false;
		try {
			MethodSignature methodSignature = new SimpleMethodSignature("foo", new Class[1]);
			fail("invalid MethodSignature: " + methodSignature);
		} catch (IllegalArgumentException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testGetSignature0() throws Exception {
		MethodSignature ms = new SimpleMethodSignature(this.getMethod("method0"));
		assertEquals("method0()", ms.getSignature());
	}

	public void testGetSignature1() throws Exception {
		MethodSignature ms = new SimpleMethodSignature(this.getMethod("method1"));
		assertEquals("method1(int)", ms.getSignature());
	}

	public void testGetSignature2() throws Exception {
		MethodSignature ms = new SimpleMethodSignature(this.getMethod("method2"));
		assertEquals("method2(int, java.lang.String)", ms.getSignature());
	}

	public void testGetSignature3() throws Exception {
		MethodSignature ms = new SimpleMethodSignature(this.getMethod("method3"));
		assertEquals("method3(int, java.lang.String, java.lang.Object[][])", ms.getSignature());
	}

	public void testGetName() throws Exception {
		MethodSignature ms = new SimpleMethodSignature(this.getMethod("method2"));
		assertEquals("method2", ms.getName());
	}

	public void testGetParameterTypes() throws Exception {
		MethodSignature ms = new SimpleMethodSignature(this.getMethod("method3"));
		JavaType[] expected = new JavaType[3];
		expected[0] = new SimpleJavaType("int");
		expected[1] = new SimpleJavaType("java.lang.String");
		expected[2] = new SimpleJavaType("java.lang.Object", 2);
		assertTrue(Arrays.equals(expected, ms.getParameterTypes()));
	}

	public void testEquals() throws Exception {
		Object ms1 = new SimpleMethodSignature(this.getMethod("method3"));
		Object ms2 = new SimpleMethodSignature(this.getMethod("method3"));
		assertNotSame(ms1, ms2);
		assertEquals(ms1, ms1);
		assertEquals(ms1, ms2);
		assertEquals(ms1.hashCode(), ms2.hashCode());

		Object ms3 = new SimpleMethodSignature(this.getMethod("method2"));
		assertNotSame(ms1, ms3);
		assertFalse(ms1.equals(ms3));
	}

	public void testClone() throws Exception {
		SimpleMethodSignature ms1 = new SimpleMethodSignature(this.getMethod("method3"));
		SimpleMethodSignature ms2 = (SimpleMethodSignature) ms1.clone();
		assertNotSame(ms1, ms2);
		assertEquals(ms1, ms2);
	}

	public void testSerialization() throws Exception {
		SimpleMethodSignature ms1 = new SimpleMethodSignature(this.getMethod("method3"));
		SimpleMethodSignature ms2 = TestTools.serialize(ms1);
		assertNotSame(ms1, ms2);
		assertEquals(ms1, ms2);
	}

	private Method getMethod(String methodName) {
		for (Method method : this.getClass().getMethods()) {
			if (method.getName().equals(methodName)) {
				return method;
			}
		}
		throw new IllegalArgumentException("method not found: " + methodName);
	}

	public void method0() { /* used by tests */ }
	@SuppressWarnings("unused") public void method1(int foo) { /* used by tests */ }
	@SuppressWarnings("unused") public void method2(int foo, String bar) { /* used by tests */ }
	@SuppressWarnings("unused") public void method3(int foo, String bar, Object[][] baz) { /* used by tests */ }

	@SuppressWarnings("unused") public void methodA(int foo, String bar) { /* used by tests */ }
	@SuppressWarnings("unused") public void methodA(int foo, String bar, String baz) { /* used by tests */ }

	@SuppressWarnings("unused") public void methodB(int foo, Object bar) { /* used by tests */ }
	@SuppressWarnings("unused") public void methodB(int foo, String bar) { /* used by tests */ }

}

Back to the top