1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
27
27
/**
28
28
* Unit tests for the {@link AspectJAdviceParameterNameDiscoverer} class.
29
29
*
30
- * <p>See also {@link TigerAspectJAdviceParameterNameDiscovererTests} in
31
- * the 'tiger' tree for tests relating to annotations.
30
+ * <p>See also {@link TigerAspectJAdviceParameterNameDiscovererTests} for tests relating to annotations.
32
31
*
33
32
* @author Adrian Colyer
34
33
* @author Chris Beams
@@ -83,27 +82,28 @@ public void testNoArgs() {
83
82
84
83
@ Test
85
84
public void testJoinPointOnly () {
86
- assertParameterNames (getMethod ("tjp" ), "execution(* *(..))" , new String []{"thisJoinPoint" });
85
+ assertParameterNames (getMethod ("tjp" ), "execution(* *(..))" , new String [] {"thisJoinPoint" });
87
86
}
88
87
89
88
@ Test
90
89
public void testJoinPointStaticPartOnly () {
91
- assertParameterNames (getMethod ("tjpsp" ), "execution(* *(..))" , new String []{"thisJoinPointStaticPart" });
90
+ assertParameterNames (getMethod ("tjpsp" ), "execution(* *(..))" , new String [] {"thisJoinPointStaticPart" });
92
91
}
93
92
94
93
@ Test
95
94
public void testTwoJoinPoints () {
96
- assertException (getMethod ("twoJoinPoints" ), "foo()" , IllegalStateException .class , "Failed to bind all argument names: 1 argument(s) could not be bound" );
95
+ assertException (getMethod ("twoJoinPoints" ), "foo()" , IllegalStateException .class ,
96
+ "Failed to bind all argument names: 1 argument(s) could not be bound" );
97
97
}
98
98
99
99
@ Test
100
100
public void testOneThrowable () {
101
- assertParameterNames (getMethod ("oneThrowable" ), "foo()" , null , "ex" , new String []{"ex" });
101
+ assertParameterNames (getMethod ("oneThrowable" ), "foo()" , null , "ex" , new String [] {"ex" });
102
102
}
103
103
104
104
@ Test
105
105
public void testOneJPAndOneThrowable () {
106
- assertParameterNames (getMethod ("jpAndOneThrowable" ), "foo()" , null , "ex" , new String []{"thisJoinPoint" , "ex" });
106
+ assertParameterNames (getMethod ("jpAndOneThrowable" ), "foo()" , null , "ex" , new String [] {"thisJoinPoint" , "ex" });
107
107
}
108
108
109
109
@ Test
@@ -120,7 +120,7 @@ public void testThrowableNoCandidates() {
120
120
121
121
@ Test
122
122
public void testReturning () {
123
- assertParameterNames (getMethod ("oneObject" ), "foo()" , "obj" , null , new String []{"obj" });
123
+ assertParameterNames (getMethod ("oneObject" ), "foo()" , "obj" , null , new String [] {"obj" });
124
124
}
125
125
126
126
@ Test
@@ -137,16 +137,16 @@ public void testReturningNoCandidates() {
137
137
138
138
@ Test
139
139
public void testThisBindingOneCandidate () {
140
- assertParameterNames (getMethod ("oneObject" ), "this(x)" , new String []{"x" });
140
+ assertParameterNames (getMethod ("oneObject" ), "this(x)" , new String [] {"x" });
141
141
}
142
142
143
143
@ Test
144
144
public void testThisBindingWithAlternateTokenizations () {
145
- assertParameterNames (getMethod ("oneObject" ), "this( x )" , new String []{"x" });
146
- assertParameterNames (getMethod ("oneObject" ), "this( x)" , new String []{"x" });
147
- assertParameterNames (getMethod ("oneObject" ), "this (x )" , new String []{"x" });
148
- assertParameterNames (getMethod ("oneObject" ), "this(x )" , new String []{"x" });
149
- assertParameterNames (getMethod ("oneObject" ), "foo() && this(x)" , new String []{"x" });
145
+ assertParameterNames (getMethod ("oneObject" ), "this( x )" , new String [] {"x" });
146
+ assertParameterNames (getMethod ("oneObject" ), "this( x)" , new String [] {"x" });
147
+ assertParameterNames (getMethod ("oneObject" ), "this (x )" , new String [] {"x" });
148
+ assertParameterNames (getMethod ("oneObject" ), "this(x )" , new String [] {"x" });
149
+ assertParameterNames (getMethod ("oneObject" ), "foo() && this(x)" , new String [] {"x" });
150
150
}
151
151
152
152
@ Test
@@ -165,16 +165,16 @@ public void testThisBindingWithBadPointcutExpressions() {
165
165
166
166
@ Test
167
167
public void testTargetBindingOneCandidate () {
168
- assertParameterNames (getMethod ("oneObject" ), "target(x)" , new String []{"x" });
168
+ assertParameterNames (getMethod ("oneObject" ), "target(x)" , new String [] {"x" });
169
169
}
170
170
171
171
@ Test
172
172
public void testTargetBindingWithAlternateTokenizations () {
173
- assertParameterNames (getMethod ("oneObject" ), "target( x )" , new String []{"x" });
174
- assertParameterNames (getMethod ("oneObject" ), "target( x)" , new String []{"x" });
175
- assertParameterNames (getMethod ("oneObject" ), "target (x )" , new String []{"x" });
176
- assertParameterNames (getMethod ("oneObject" ), "target(x )" , new String []{"x" });
177
- assertParameterNames (getMethod ("oneObject" ), "foo() && target(x)" , new String []{"x" });
173
+ assertParameterNames (getMethod ("oneObject" ), "target( x )" , new String [] {"x" });
174
+ assertParameterNames (getMethod ("oneObject" ), "target( x)" , new String [] {"x" });
175
+ assertParameterNames (getMethod ("oneObject" ), "target (x )" , new String [] {"x" });
176
+ assertParameterNames (getMethod ("oneObject" ), "target(x )" , new String [] {"x" });
177
+ assertParameterNames (getMethod ("oneObject" ), "foo() && target(x)" , new String [] {"x" });
178
178
}
179
179
180
180
@ Test
@@ -193,7 +193,7 @@ public void testTargetBindingWithBadPointcutExpressions() {
193
193
194
194
@ Test
195
195
public void testArgsBindingOneObject () {
196
- assertParameterNames (getMethod ("oneObject" ), "args(x)" , new String []{"x" });
196
+ assertParameterNames (getMethod ("oneObject" ), "args(x)" , new String [] {"x" });
197
197
}
198
198
199
199
@ Test
@@ -210,7 +210,7 @@ public void testAmbiguousArgsBinding() {
210
210
211
211
@ Test
212
212
public void testArgsOnePrimitive () {
213
- assertParameterNames (getMethod ("onePrimitive" ), "args(count)" , new String []{"count" });
213
+ assertParameterNames (getMethod ("onePrimitive" ), "args(count)" , new String [] {"count" });
214
214
}
215
215
216
216
@ Test
@@ -221,62 +221,64 @@ public void testArgsOnePrimitiveOneObject() {
221
221
222
222
@ Test
223
223
public void testThisAndPrimitive () {
224
- assertParameterNames (getMethod ("oneObjectOnePrimitive" ), "args(count) && this(obj)" , new String []{"obj" , "count" });
224
+ assertParameterNames (getMethod ("oneObjectOnePrimitive" ), "args(count) && this(obj)" , new String [] {"obj" , "count" });
225
225
}
226
226
227
227
@ Test
228
228
public void testTargetAndPrimitive () {
229
- assertParameterNames (getMethod ("oneObjectOnePrimitive" ), "args(count) && target(obj)" , new String []{"obj" , "count" });
229
+ assertParameterNames (getMethod ("oneObjectOnePrimitive" ), "args(count) && target(obj)" , new String [] {"obj" , "count" });
230
230
}
231
231
232
232
@ Test
233
233
public void testThrowingAndPrimitive () {
234
- assertParameterNames (getMethod ("oneThrowableOnePrimitive" ), "args(count)" , null , "ex" , new String []{"ex" , "count" });
234
+ assertParameterNames (getMethod ("oneThrowableOnePrimitive" ), "args(count)" , null , "ex" , new String [] {"ex" , "count" });
235
235
}
236
236
237
237
@ Test
238
238
public void testAllTogetherNow () {
239
- assertParameterNames (getMethod ("theBigOne" ), "this(foo) && args(x)" , null , "ex" , new String []{"thisJoinPoint" , "ex" , "x" , "foo" });
239
+ assertParameterNames (getMethod ("theBigOne" ), "this(foo) && args(x)" , null , "ex" , new String [] {"thisJoinPoint" , "ex" , "x" , "foo" });
240
240
}
241
241
242
242
@ Test
243
243
public void testReferenceBinding () {
244
- assertParameterNames (getMethod ("onePrimitive" ),"somepc(foo)" ,new String [] {"foo" });
244
+ assertParameterNames (getMethod ("onePrimitive" ),"somepc(foo)" , new String [] {"foo" });
245
245
}
246
246
247
247
@ Test
248
248
public void testReferenceBindingWithAlternateTokenizations () {
249
- assertParameterNames (getMethod ("onePrimitive" ),"call(bar *) && somepc(foo)" ,new String [] {"foo" });
250
- assertParameterNames (getMethod ("onePrimitive" ),"somepc ( foo )" ,new String [] {"foo" });
251
- assertParameterNames (getMethod ("onePrimitive" ),"somepc( foo)" ,new String [] {"foo" });
249
+ assertParameterNames (getMethod ("onePrimitive" ),"call(bar *) && somepc(foo)" , new String [] {"foo" });
250
+ assertParameterNames (getMethod ("onePrimitive" ),"somepc ( foo )" , new String [] {"foo" });
251
+ assertParameterNames (getMethod ("onePrimitive" ),"somepc( foo)" , new String [] {"foo" });
252
252
}
253
253
254
254
255
255
protected Method getMethod (String name ) {
256
256
// assumes no overloading of test methods...
257
257
Method [] candidates = this .getClass ().getMethods ();
258
- for (int i = 0 ; i < candidates . length ; i ++ ) {
259
- if (candidates [ i ] .getName ().equals (name )) {
260
- return candidates [ i ] ;
258
+ for (Method candidate : candidates ) {
259
+ if (candidate .getName ().equals (name )) {
260
+ return candidate ;
261
261
}
262
262
}
263
263
fail ("Bad test specification, no method '" + name + "' found in test class" );
264
264
return null ;
265
265
}
266
266
267
- protected void assertParameterNames (Method m , String pointcut , String [] parameterNames ) {
268
- assertParameterNames (m , pointcut , null , null , parameterNames );
267
+ protected void assertParameterNames (Method method , String pointcut , String [] parameterNames ) {
268
+ assertParameterNames (method , pointcut , null , null , parameterNames );
269
269
}
270
270
271
- protected void assertParameterNames (Method m , String pointcut , String returning , String throwing , String [] parameterNames ) {
271
+ protected void assertParameterNames (Method method , String pointcut , String returning , String throwing ,
272
+ String [] parameterNames ) {
273
+
272
274
assertEquals ("bad test specification, must have same number of parameter names as method arguments" ,
273
- m .getParameterCount (), parameterNames .length );
275
+ method .getParameterCount (), parameterNames .length );
274
276
275
277
AspectJAdviceParameterNameDiscoverer discoverer = new AspectJAdviceParameterNameDiscoverer (pointcut );
276
278
discoverer .setRaiseExceptions (true );
277
279
discoverer .setReturningName (returning );
278
280
discoverer .setThrowingName (throwing );
279
- String [] discoveredNames = discoverer .getParameterNames (m );
281
+ String [] discoveredNames = discoverer .getParameterNames (method );
280
282
281
283
String formattedExpectedNames = format (parameterNames );
282
284
String formattedActualNames = format (discoveredNames );
@@ -294,18 +296,20 @@ protected void assertParameterNames(Method m, String pointcut, String returning,
294
296
}
295
297
}
296
298
297
- protected void assertException (Method m , String pointcut , Class <?> exceptionType , String message ) {
298
- assertException (m , pointcut , null , null , exceptionType , message );
299
+ protected void assertException (Method method , String pointcut , Class <?> exceptionType , String message ) {
300
+ assertException (method , pointcut , null , null , exceptionType , message );
299
301
}
300
302
301
- protected void assertException (Method m , String pointcut , String returning , String throwing , Class <?> exceptionType , String message ) {
303
+ protected void assertException (Method method , String pointcut , String returning , String throwing ,
304
+ Class <?> exceptionType , String message ) {
305
+
302
306
AspectJAdviceParameterNameDiscoverer discoverer = new AspectJAdviceParameterNameDiscoverer (pointcut );
303
307
discoverer .setRaiseExceptions (true );
304
308
discoverer .setReturningName (returning );
305
309
discoverer .setThrowingName (throwing );
306
310
307
311
try {
308
- discoverer .getParameterNames (m );
312
+ discoverer .getParameterNames (method );
309
313
fail ("Expecting " + exceptionType .getName () + " with message '" + message + "'" );
310
314
}
311
315
catch (RuntimeException expected ) {
0 commit comments