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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/* *******************************************************************
* Copyright (c) 2002-2008 Contributors
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
*
* Contributors:
* PARC initial implementation
* Andy Clement
* ******************************************************************/
package org.aspectj.weaver;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import junit.framework.TestCase;
import org.aspectj.testing.util.TestUtil;
/**
* An abstract set of tests that any World implementation should be able to pass. To run it against your World, subclass it and
* implement getWorld().
*
* @author Andy Clement
*/
public abstract class CommonWorldTests extends TestCase {
/**
* @return an instance of the World to be tested
*/
protected abstract World getWorld();
private World world;
@Override
public void setUp() {
world = getWorld();
}
private final UnresolvedType[] primitiveTypes = UnresolvedType.forSignatures(new String[] { "B", "S", "C", "I", "J", "F", "D",
"V" });
public void testPrimitiveTypes() {
ResolvedType[] primitives = world.resolve(primitiveTypes);
for (ResolvedType ty : primitives) {
modifiersTest(ty, Modifier.PUBLIC | Modifier.FINAL);
fieldsTest(ty, ResolvedMember.NONE);
methodsTest(ty, ResolvedMember.NONE);
interfacesTest(ty, ResolvedType.NONE);
superclassTest(ty, null);
pointcutsTest(ty, ResolvedMember.NONE);
isInterfaceTest(ty, false);
isClassTest(ty, false);
isAspectTest(ty, false);
for (ResolvedType ty1 : primitives) {
if (ty.equals(ty1)) {
isCoerceableFromTest(ty, ty1, true);
} else if (ty.equals(UnresolvedType.BOOLEAN) || ty1.equals(UnresolvedType.BOOLEAN)
|| ty.equals(UnresolvedType.VOID) || ty1.equals(UnresolvedType.VOID)) {
isCoerceableFromTest(ty, ty1, false);
} else {
isCoerceableFromTest(ty, ty1, true);
}
}
// Result of this depends on whether autoboxing is supported
// isCoerceableFromTest(ty, UnresolvedType.OBJECT, getSupportsAutoboxing());
primAssignTest("B", new String[]{});
primAssignTest("S", new String[]{"B"});
primAssignTest("C", new String[]{"B"});
primAssignTest("I", new String[]{"B", "S", "C"});
primAssignTest("J", new String[]{"B", "S", "C", "I"});
primAssignTest("F", new String[]{"B", "S", "C", "I", "J"});
primAssignTest("D", new String[]{"B", "S", "C", "I", "J", "F"});
primAssignTest("Z", new String[]{});
primAssignTest("V", new String[]{});
}
}
private void primAssignTest(String sig, String[] lowers) {
ResolvedType[] primitives = world.resolve(primitiveTypes);
UnresolvedType tx = UnresolvedType.forSignature(sig);
ResolvedType ty = world.resolve(tx, true);
assertTrue("Couldnt find type " + tx, !ty.isMissing());
ResolvedType[] lowerTyArray = world.resolve(UnresolvedType.forSignatures(lowers));
List<ResolvedType> lowerTys = new ArrayList<>(Arrays.asList(lowerTyArray));
lowerTys.add(ty);
Set<ResolvedType> allLowerTys = new HashSet<>(lowerTys);
Set<ResolvedType> allUpperTys = new HashSet<>(Arrays.asList(primitives));
allUpperTys.removeAll(allLowerTys);
for (ResolvedType other : allLowerTys) {
isAssignableFromTest(ty, other, true);
}
for (ResolvedType other : allUpperTys) {
isAssignableFromTest(ty, other, false);
}
}
public void testPrimitiveArrays() {
ResolvedType[] primitives = world.resolve(primitiveTypes);
for (ResolvedType ty : primitives) {
UnresolvedType tx = UnresolvedType.forSignature("[" + ty.getSignature());
// 'void[]' is an illegal type -> skip
if (tx.getSignature().equals("[V"))
continue;
ResolvedType aty = world.resolve(tx, true);
assertTrue("Couldnt find type " + tx, !aty.isMissing());
modifiersTest(aty, Modifier.PUBLIC | Modifier.FINAL);
fieldsTest(aty, ResolvedMember.NONE);
methodsTest(aty, ResolvedMember.NONE);
interfaceTest(
aty,
new ResolvedType[]{world.getCoreType(UnresolvedType.CLONEABLE),
world.getCoreType(UnresolvedType.SERIALIZABLE)});
superclassTest(aty, UnresolvedType.OBJECT);
pointcutsTest(aty, ResolvedMember.NONE);
isInterfaceTest(aty, false);
isClassTest(aty, false);
isAspectTest(aty, false);
for (ResolvedType ty1 : primitives) {
isCoerceableFromTest(aty, ty1, false);
tx = UnresolvedType.forSignature("[" + ty1.getSignature());
// 'void[]' is an illegal type -> skip
if (tx.getSignature().equals("[V"))
continue;
ResolvedType aty1 = getWorld().resolve(tx, true);
assertTrue("Couldnt find type " + tx, !aty1.isMissing());
if (ty.equals(ty1)) {
isCoerceableFromTest(aty, aty1, true);
isAssignableFromTest(aty, aty1, true);
} else {
isCoerceableFromTest(aty, aty1, false);
isAssignableFromTest(aty, aty1, false);
}
}
}
// double dimension arrays
for (ResolvedType ty : primitives) {
UnresolvedType tx = UnresolvedType.forSignature("[[" + ty.getSignature());
// 'void[][]' is an illegal type -> skip
if (tx.getSignature().equals("[[V"))
continue;
ResolvedType aty = world.resolve(tx, true);
assertTrue("Couldnt find type " + tx, !aty.isMissing());
modifiersTest(aty, Modifier.PUBLIC | Modifier.FINAL);
fieldsTest(aty, ResolvedMember.NONE);
methodsTest(aty, ResolvedMember.NONE);
interfaceTest(
aty,
new ResolvedType[]{world.getCoreType(UnresolvedType.CLONEABLE),
world.getCoreType(UnresolvedType.SERIALIZABLE)});
superclassTest(aty, UnresolvedType.OBJECT);
pointcutsTest(aty, ResolvedMember.NONE);
isInterfaceTest(aty, false);
isClassTest(aty, false);
isAspectTest(aty, false);
for (ResolvedType ty1 : primitives) {
isCoerceableFromTest(aty, ty1, false);
tx = UnresolvedType.forSignature("[[" + ty1.getSignature());
// 'void[][]' is an illegal type -> skip
if (tx.getSignature().equals("[[V"))
continue;
ResolvedType aty1 = getWorld().resolve(tx, true);
assertTrue("Couldnt find type " + tx, !aty1.isMissing());
if (ty.equals(ty1)) {
isCoerceableFromTest(aty, aty1, true);
isAssignableFromTest(aty, aty1, true);
} else {
isCoerceableFromTest(aty, aty1, false);
isAssignableFromTest(aty, aty1, false);
}
}
}
}
// ---- tests for parts of ResolvedType objects
protected void modifiersTest(ResolvedType ty, int mods) {
assertEquals(ty + " modifiers:", Modifier.toString(mods), Modifier.toString(ty.getModifiers()));
}
protected void fieldsTest(ResolvedType ty, Member[] x) {
TestUtil.assertSetEquals(ty + " fields:", x, ty.getDeclaredJavaFields());
}
protected void methodsTest(ResolvedType ty, Member[] x) {
TestUtil.assertSetEquals(ty + " methods:", x, ty.getDeclaredJavaMethods());
}
protected void mungersTest(ResolvedType ty, ShadowMunger[] x) {
List<ShadowMunger> l = ty.getDeclaredShadowMungers();
ShadowMunger[] array = (ShadowMunger[]) l.toArray(new ShadowMunger[0]);
TestUtil.assertSetEquals(ty + " mungers:", x, array);
}
protected void interfaceTest(ResolvedType type, ResolvedType[] expectedInterfaces) {
ResolvedType[] interfaces = type.getDeclaredInterfaces();
for (ResolvedType expectedInterface : expectedInterfaces) {
boolean wasMissing = true;
for (ResolvedType anInterface : interfaces) {
if (anInterface.getSignature().equals(expectedInterface.getSignature())) {
wasMissing = false;
}
}
if (wasMissing) {
fail("Expected declared interface " + expectedInterface + " but it wasn't found in "
+ Arrays.asList(interfaces));
}
}
}
protected void interfacesTest(ResolvedType ty, ResolvedType[] x) {
TestUtil.assertArrayEquals(ty + " interfaces:", x, ty.getDeclaredInterfaces());
}
protected void superclassTest(ResolvedType ty, UnresolvedType x) {
assertEquals(ty + " superclass:", x, ty.getSuperclass());
}
protected void pointcutsTest(ResolvedType ty, Member[] x) {
TestUtil.assertSetEquals(ty + " pointcuts:", x, ty.getDeclaredPointcuts());
}
protected void isInterfaceTest(ResolvedType ty, boolean x) {
assertEquals(ty + " is interface:", x, ty.isInterface());
}
protected void isAspectTest(ResolvedType ty, boolean x) {
assertEquals(ty + " is aspect:", x, ty.isAspect());
}
protected void isClassTest(ResolvedType ty, boolean x) {
assertEquals(ty + " is class:", x, ty.isClass());
}
protected void isCoerceableFromTest(UnresolvedType ty0, UnresolvedType ty1, boolean x) {
assertEquals(ty0 + " is coerceable from " + ty1, ty0.resolve(world).isCoerceableFrom(ty1.resolve(world)), x);
assertEquals(ty1 + " is coerceable from " + ty0, ty1.resolve(world).isCoerceableFrom(ty0.resolve(world)), x);
}
protected void isAssignableFromTest(UnresolvedType ty0, UnresolvedType ty1, boolean x) {
ResolvedType rty0 = ty0.resolve(world);
ResolvedType rty1 = ty1.resolve(world);
boolean result = rty0.isAssignableFrom(rty1);
assertEquals(ty0 + " is assignable from " + ty1, result, x);
}
// ---- tests for parts of ResolvedMethod objects
protected void modifiersTest(ResolvedMember m, int mods) {
assertEquals(m + " modifiers:", Modifier.toString(mods), Modifier.toString(m.getModifiers()));
}
protected void exceptionsTest(ResolvedMember m, UnresolvedType[] exns) {
TestUtil.assertSetEquals(m + " exceptions:", exns, m.getExceptions());
}
}
|