44
44
import org .junit .jupiter .params .ParameterizedTest ;
45
45
import org .junit .jupiter .params .provider .Arguments ;
46
46
import org .junit .jupiter .params .provider .MethodSource ;
47
- import org .junit .jupiter .params .provider .ValueSource ;
48
47
import org .objectweb .asm .Type ;
49
48
50
49
class ReferenceMatcherTest {
@@ -158,25 +157,23 @@ void matchingRef(Class<?> referenceClass, Flag referenceFlag, Class<? extends Mi
158
157
159
158
private static Stream <Arguments > methodMatchProvider () {
160
159
return Stream .of (
161
- Arguments .of ("method" , "(Ljava/lang/ String;)Ljava/lang/ String;" , emptySet (), Nested .B .class , null ),
162
- Arguments .of ("hashCode" , "()I" , emptySet (), Nested .B .class , null ),
163
- Arguments .of ("someMethod" , "()V" , emptySet (), Nested .SomeInterface .class , null ),
164
- Arguments .of ("privateStuff" , "()V" , singleton (PRIVATE_OR_HIGHER ), Nested .B .class , null ),
165
- Arguments .of ("privateStuff" , "()V" , singleton (PROTECTED_OR_HIGHER ), Nested .B2 .class , Mismatch .MissingFlag .class ),
166
- Arguments .of ("staticMethod" , "()V" , singleton (NON_STATIC ), Nested .B .class , Mismatch .MissingFlag .class ),
167
- Arguments .of ("missingMethod" , "()V" , emptySet (), Nested .B .class , Mismatch .MissingMethod .class ));
160
+ Arguments .of ("method" , Type . getMethodType ( Type . getType ( String . class ), Type . getType ( String . class )) , emptySet (), Nested .B .class , null ),
161
+ Arguments .of ("hashCode" , Type . getMethodType ( Type . INT_TYPE ) , emptySet (), Nested .B .class , null ),
162
+ Arguments .of ("someMethod" , Type . getMethodType ( Type . VOID_TYPE ) , emptySet (), Nested .SomeInterface .class , null ),
163
+ Arguments .of ("privateStuff" , Type . getMethodType ( Type . VOID_TYPE ) , singleton (PRIVATE_OR_HIGHER ), Nested .B .class , null ),
164
+ Arguments .of ("privateStuff" , Type . getMethodType ( Type . VOID_TYPE ) , singleton (PROTECTED_OR_HIGHER ), Nested .B2 .class , Mismatch .MissingFlag .class ),
165
+ Arguments .of ("staticMethod" , Type . getMethodType ( Type . VOID_TYPE ) , singleton (NON_STATIC ), Nested .B .class , Mismatch .MissingFlag .class ),
166
+ Arguments .of ("missingMethod" , Type . getMethodType ( Type . VOID_TYPE ) , emptySet (), Nested .B .class , Mismatch .MissingMethod .class ));
168
167
}
169
168
170
169
@ ParameterizedTest
171
170
@ MethodSource ("methodMatchProvider" )
172
171
void methodMatch (
173
172
String methodName ,
174
- String methodDesc ,
173
+ Type methodType ,
175
174
Set <Flag > methodFlags ,
176
175
Class <?> classToCheck ,
177
176
Class <? extends Mismatch > expectedMismatch ) {
178
- Type methodType = Type .getMethodType (methodDesc );
179
-
180
177
ClassRef reference =
181
178
ClassRef .builder (classToCheck .getName ())
182
179
.addMethod (
@@ -200,21 +197,21 @@ void methodMatch(
200
197
201
198
private static Stream <Arguments > fieldMatchProvider () {
202
199
return Stream .of (
203
- Arguments .of ("missingField" , "Ljava/lang/ String;" , emptySet (), Nested .A .class , Mismatch .MissingField .class ),
204
- Arguments .of ("privateField" , "Ljava/lang/ String;" , emptySet (), Nested .A .class , Mismatch .MissingField .class ),
205
- Arguments .of ("privateField" , "Ljava/lang/ Object;" , singleton (PRIVATE_OR_HIGHER ), Nested .A .class , null ),
206
- Arguments .of ("privateField" , "Ljava/lang/ Object;" , singleton (PROTECTED_OR_HIGHER ), Nested .A2 .class , Mismatch .MissingFlag .class ),
207
- Arguments .of ("protectedField" , "Ljava/lang/ Object;" , singleton (STATIC ), Nested .A .class , Mismatch .MissingFlag .class ),
208
- Arguments .of ("staticB" , "Lmuzzle/TestClasses$ Nested$B;" , new HashSet <>(Arrays .asList (STATIC , PROTECTED_OR_HIGHER )), Nested .A .class , null ),
209
- Arguments .of ("number" , "I" , singleton (PACKAGE_OR_HIGHER ), Nested .Primitives .class , null ),
210
- Arguments .of ("flag" , "Z" , singleton (PACKAGE_OR_HIGHER ), Nested .Primitives .class , null ));
200
+ Arguments .of ("missingField" , Type . getType ( String . class ) , emptySet (), Nested .A .class , Mismatch .MissingField .class ),
201
+ Arguments .of ("privateField" , Type . getType ( String . class ) , emptySet (), Nested .A .class , Mismatch .MissingField .class ),
202
+ Arguments .of ("privateField" , Type . getType ( Object . class ) , singleton (PRIVATE_OR_HIGHER ), Nested .A .class , null ),
203
+ Arguments .of ("privateField" , Type . getType ( Object . class ) , singleton (PROTECTED_OR_HIGHER ), Nested .A2 .class , Mismatch .MissingFlag .class ),
204
+ Arguments .of ("protectedField" , Type . getType ( Object . class ) , singleton (STATIC ), Nested .A .class , Mismatch .MissingFlag .class ),
205
+ Arguments .of ("staticB" , Type . getType ( Nested . B . class ) , new HashSet <>(Arrays .asList (STATIC , PROTECTED_OR_HIGHER )), Nested .A .class , null ),
206
+ Arguments .of ("number" , Type . INT_TYPE , singleton (PACKAGE_OR_HIGHER ), Nested .Primitives .class , null ),
207
+ Arguments .of ("flag" , Type . BOOLEAN_TYPE , singleton (PACKAGE_OR_HIGHER ), Nested .Primitives .class , null ));
211
208
}
212
209
213
210
@ ParameterizedTest
214
211
@ MethodSource ("fieldMatchProvider" )
215
212
void fieldMatch (
216
213
String fieldName ,
217
- String fieldType ,
214
+ Type fieldType ,
218
215
Set <Flag > fieldFlags ,
219
216
Class <?> classToCheck ,
220
217
Class <? extends Mismatch > expectedMismatch ) {
@@ -224,7 +221,7 @@ void fieldMatch(
224
221
new Source [0 ],
225
222
fieldFlags .toArray (new Flag [0 ]),
226
223
fieldName ,
227
- Type . getType ( fieldType ) ,
224
+ fieldType ,
228
225
false )
229
226
.build ();
230
227
@@ -239,12 +236,14 @@ void fieldMatch(
239
236
}
240
237
}
241
238
239
+ private static Stream <Arguments > shouldNotCheckAbstractHelperClassesProvider () {
240
+ return Stream .of (
241
+ Arguments .of ("io.opentelemetry.instrumentation.Helper" ),
242
+ Arguments .of ("com.external.otel.instrumentation.Helper" ));
243
+ }
244
+
242
245
@ ParameterizedTest
243
- @ ValueSource (
244
- strings = {
245
- "io.opentelemetry.instrumentation.Helper" ,
246
- "com.external.otel.instrumentation.Helper"
247
- })
246
+ @ MethodSource ("shouldNotCheckAbstractHelperClassesProvider" )
248
247
void shouldNotCheckAbstractHelperClasses (String className ) {
249
248
ClassRef reference =
250
249
ClassRef .builder (className )
@@ -262,17 +261,19 @@ void shouldNotCheckAbstractHelperClasses(String className) {
262
261
assertThat (mismatches ).isEmpty ();
263
262
}
264
263
264
+ private static Stream <Arguments > shouldNotCheckHelperClassesWithNoSupertypesProvider () {
265
+ return Stream .of (
266
+ Arguments .of ("io.opentelemetry.instrumentation.Helper" , "someMethod" , Type .VOID_TYPE ),
267
+ Arguments .of ("com.external.otel.instrumentation.Helper" , "someMethod" , Type .VOID_TYPE ));
268
+ }
269
+
265
270
@ ParameterizedTest
266
- @ ValueSource (
267
- strings = {
268
- "io.opentelemetry.instrumentation.Helper" ,
269
- "com.external.otel.instrumentation.Helper"
270
- })
271
- void shouldNotCheckHelperClassesWithNoSupertypes (String className ) {
271
+ @ MethodSource ("shouldNotCheckHelperClassesWithNoSupertypesProvider" )
272
+ void shouldNotCheckHelperClassesWithNoSupertypes (String className , String methodName , Type returnType ) {
272
273
ClassRef reference =
273
274
ClassRef .builder (className )
274
275
.setSuperClassName (Object .class .getName ())
275
- .addMethod (new Source [0 ], new Flag [0 ], "someMethod" , Type . VOID_TYPE )
276
+ .addMethod (new Source [0 ], new Flag [0 ], methodName , returnType )
276
277
.build ();
277
278
278
279
List <Mismatch > mismatches =
@@ -284,12 +285,14 @@ void shouldNotCheckHelperClassesWithNoSupertypes(String className) {
284
285
assertThat (mismatches ).isEmpty ();
285
286
}
286
287
288
+ private static Stream <Arguments > shouldFailHelperClassesThatDoNotImplementAllAbstractMethodsProvider () {
289
+ return Stream .of (
290
+ Arguments .of ("io.opentelemetry.instrumentation.Helper" ),
291
+ Arguments .of ("com.external.otel.instrumentation.Helper" ));
292
+ }
293
+
287
294
@ ParameterizedTest
288
- @ ValueSource (
289
- strings = {
290
- "io.opentelemetry.instrumentation.Helper" ,
291
- "com.external.otel.instrumentation.Helper"
292
- })
295
+ @ MethodSource ("shouldFailHelperClassesThatDoNotImplementAllAbstractMethodsProvider" )
293
296
void shouldFailHelperClassesThatDoNotImplementAllAbstractMethods (String className ) {
294
297
ClassRef reference =
295
298
ClassRef .builder (className )
@@ -306,12 +309,14 @@ void shouldFailHelperClassesThatDoNotImplementAllAbstractMethods(String classNam
306
309
assertThat (getMismatchClassSet (mismatches )).containsExactly (Mismatch .MissingMethod .class );
307
310
}
308
311
312
+ private static Stream <Arguments > shouldFailHelperClassesThatDoNotImplementAllAbstractMethodsEvenIfEmptyAbstractClassReferenceExistsProvider () {
313
+ return Stream .of (
314
+ Arguments .of ("io.opentelemetry.instrumentation.Helper" ),
315
+ Arguments .of ("com.external.otel.instrumentation.Helper" ));
316
+ }
317
+
309
318
@ ParameterizedTest
310
- @ ValueSource (
311
- strings = {
312
- "io.opentelemetry.instrumentation.Helper" ,
313
- "com.external.otel.instrumentation.Helper"
314
- })
319
+ @ MethodSource ("shouldFailHelperClassesThatDoNotImplementAllAbstractMethodsEvenIfEmptyAbstractClassReferenceExistsProvider" )
315
320
void
316
321
shouldFailHelperClassesThatDoNotImplementAllAbstractMethodsEvenIfEmptyAbstractClassReferenceExists (
317
322
String className ) {
@@ -336,12 +341,14 @@ void shouldFailHelperClassesThatDoNotImplementAllAbstractMethods(String classNam
336
341
assertThat (getMismatchClassSet (mismatches )).containsExactly (Mismatch .MissingMethod .class );
337
342
}
338
343
344
+ private static Stream <Arguments > shouldCheckHelperClassWhetherInterfaceMethodsAreImplementedInTheSuperClassProvider () {
345
+ return Stream .of (
346
+ Arguments .of ("io.opentelemetry.instrumentation.Helper" ),
347
+ Arguments .of ("com.external.otel.instrumentation.Helper" ));
348
+ }
349
+
339
350
@ ParameterizedTest
340
- @ ValueSource (
341
- strings = {
342
- "io.opentelemetry.instrumentation.Helper" ,
343
- "com.external.otel.instrumentation.Helper"
344
- })
351
+ @ MethodSource ("shouldCheckHelperClassWhetherInterfaceMethodsAreImplementedInTheSuperClassProvider" )
345
352
void shouldCheckHelperClassWhetherInterfaceMethodsAreImplementedInTheSuperClass (
346
353
String className ) {
347
354
ClassRef baseHelper =
@@ -371,12 +378,14 @@ void shouldCheckHelperClassWhetherInterfaceMethodsAreImplementedInTheSuperClass(
371
378
assertThat (mismatches ).isEmpty ();
372
379
}
373
380
381
+ private static Stream <Arguments > shouldCheckHelperClassWhetherUsedFieldsAreDeclaredInTheSuperClassProvider () {
382
+ return Stream .of (
383
+ Arguments .of ("io.opentelemetry.instrumentation.Helper" ),
384
+ Arguments .of ("com.external.otel.instrumentation.Helper" ));
385
+ }
386
+
374
387
@ ParameterizedTest
375
- @ ValueSource (
376
- strings = {
377
- "io.opentelemetry.instrumentation.Helper" ,
378
- "com.external.otel.instrumentation.Helper"
379
- })
388
+ @ MethodSource ("shouldCheckHelperClassWhetherUsedFieldsAreDeclaredInTheSuperClassProvider" )
380
389
void shouldCheckHelperClassWhetherUsedFieldsAreDeclaredInTheSuperClass (String className ) {
381
390
ClassRef helper =
382
391
ClassRef .builder (className )
@@ -388,7 +397,7 @@ void shouldCheckHelperClassWhetherUsedFieldsAreDeclaredInTheSuperClass(String cl
388
397
List <Mismatch > mismatches =
389
398
createMatcher (
390
399
Collections .singletonMap (helper .getClassName (), helper ),
391
- Collections .singletonList (helper .getClassName ()))
400
+ Collections .singletonList (helper .getClass (). getClassLoader ()))
392
401
.getMismatchedReferenceSources (this .getClass ().getClassLoader ());
393
402
394
403
assertThat (mismatches ).isEmpty ();
@@ -418,18 +427,20 @@ void shouldFailHelperClassWhenItUsesFieldsUndeclaredInTheSuperClass(
418
427
List <Mismatch > mismatches =
419
428
createMatcher (
420
429
Collections .singletonMap (helper .getClassName (), helper ),
421
- Collections .singletonList (helper .getClassName ()))
430
+ Collections .singletonList (helper .getClass (). getClassLoader ()))
422
431
.getMismatchedReferenceSources (this .getClass ().getClassLoader ());
423
432
424
433
assertThat (getMismatchClassSet (mismatches )).containsExactly (Mismatch .MissingField .class );
425
434
}
426
435
436
+ private static Stream <Arguments > shouldFailHelperClassWhenTheLibraryParentClassHasDifferentConstructorProvider () {
437
+ return Stream .of (
438
+ Arguments .of ("io.opentelemetry.instrumentation.Helper" ),
439
+ Arguments .of ("com.external.otel.instrumentation.Helper" ));
440
+ }
441
+
427
442
@ ParameterizedTest
428
- @ ValueSource (
429
- strings = {
430
- "io.opentelemetry.instrumentation.Helper" ,
431
- "com.external.otel.instrumentation.Helper"
432
- })
443
+ @ MethodSource ("shouldFailHelperClassWhenTheLibraryParentClassHasDifferentConstructorProvider" )
433
444
void shouldFailHelperClassWhenTheLibraryParentClassHasDifferentConstructor (String className ) {
434
445
ClassRef helper =
435
446
ClassRef .builder (className )
0 commit comments