1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2021 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.
33
33
import java .util .Set ;
34
34
35
35
import org .junit .jupiter .api .BeforeEach ;
36
+ import org .junit .jupiter .api .Nested ;
36
37
import org .junit .jupiter .api .Test ;
37
38
import org .junit .jupiter .params .ParameterizedTest ;
38
39
import org .junit .jupiter .params .provider .CsvSource ;
@@ -59,14 +60,6 @@ class ClassUtilsTests {
59
60
private final ClassLoader classLoader = getClass ().getClassLoader ();
60
61
61
62
62
- @ BeforeEach
63
- void clearStatics () {
64
- InnerClass .noArgCalled = false ;
65
- InnerClass .argCalled = false ;
66
- InnerClass .overloadedCalled = false ;
67
- }
68
-
69
-
70
63
@ Test
71
64
void isPresent () {
72
65
assertThat (ClassUtils .isPresent ("java.lang.String" , classLoader )).isTrue ();
@@ -86,8 +79,12 @@ void forName() throws ClassNotFoundException {
86
79
assertThat (ClassUtils .forName ("org.springframework.tests.sample.objects.TestObject[][]" , classLoader )).isEqualTo (TestObject [][].class );
87
80
assertThat (ClassUtils .forName (TestObject [][].class .getName (), classLoader )).isEqualTo (TestObject [][].class );
88
81
assertThat (ClassUtils .forName ("[[[S" , classLoader )).isEqualTo (short [][][].class );
89
- assertThat (ClassUtils .forName ("org.springframework.tests.sample.objects.TestObject$NestedObject" , classLoader )).isEqualTo (TestObject .NestedObject .class );
90
- assertThat (ClassUtils .forName ("org.springframework.tests.sample.objects.TestObject.NestedObject" , classLoader )).isEqualTo (TestObject .NestedObject .class );
82
+ }
83
+
84
+ @ Test
85
+ void forNameWithNestedType () throws ClassNotFoundException {
86
+ assertThat (ClassUtils .forName ("org.springframework.util.ClassUtilsTests$NestedClass" , classLoader )).isEqualTo (NestedClass .class );
87
+ assertThat (ClassUtils .forName ("org.springframework.util.ClassUtilsTests.NestedClass" , classLoader )).isEqualTo (NestedClass .class );
91
88
}
92
89
93
90
@ Test
@@ -145,11 +142,11 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
145
142
assertThat (ClassUtils .isCacheSafe (String .class , childLoader1 )).isTrue ();
146
143
assertThat (ClassUtils .isCacheSafe (String .class , childLoader2 )).isTrue ();
147
144
assertThat (ClassUtils .isCacheSafe (String .class , childLoader3 )).isTrue ();
148
- assertThat (ClassUtils .isCacheSafe (InnerClass .class , null )).isFalse ();
149
- assertThat (ClassUtils .isCacheSafe (InnerClass .class , classLoader )).isTrue ();
150
- assertThat (ClassUtils .isCacheSafe (InnerClass .class , childLoader1 )).isTrue ();
151
- assertThat (ClassUtils .isCacheSafe (InnerClass .class , childLoader2 )).isTrue ();
152
- assertThat (ClassUtils .isCacheSafe (InnerClass .class , childLoader3 )).isTrue ();
145
+ assertThat (ClassUtils .isCacheSafe (NestedClass .class , null )).isFalse ();
146
+ assertThat (ClassUtils .isCacheSafe (NestedClass .class , classLoader )).isTrue ();
147
+ assertThat (ClassUtils .isCacheSafe (NestedClass .class , childLoader1 )).isTrue ();
148
+ assertThat (ClassUtils .isCacheSafe (NestedClass .class , childLoader2 )).isTrue ();
149
+ assertThat (ClassUtils .isCacheSafe (NestedClass .class , childLoader3 )).isTrue ();
153
150
assertThat (ClassUtils .isCacheSafe (composite , null )).isFalse ();
154
151
assertThat (ClassUtils .isCacheSafe (composite , classLoader )).isFalse ();
155
152
assertThat (ClassUtils .isCacheSafe (composite , childLoader1 )).isTrue ();
@@ -211,9 +208,9 @@ void getShortNameForMultiDimensionalPrimitiveArrayClass() {
211
208
}
212
209
213
210
@ Test
214
- void getShortNameForInnerClass () {
215
- String className = ClassUtils .getShortName (InnerClass .class );
216
- assertThat (className ).as ("Class name did not match" ).isEqualTo ("ClassUtilsTests.InnerClass " );
211
+ void getShortNameForNestedClass () {
212
+ String className = ClassUtils .getShortName (NestedClass .class );
213
+ assertThat (className ).as ("Class name did not match" ).isEqualTo ("ClassUtilsTests.NestedClass " );
217
214
}
218
215
219
216
@ Test
@@ -301,27 +298,6 @@ void countOverloadedMethods() {
301
298
assertThat (ClassUtils .hasAtLeastOneMethodWithName (TestObject .class , "setAge" )).isTrue ();
302
299
}
303
300
304
- @ Test
305
- void noArgsStaticMethod () throws IllegalAccessException , InvocationTargetException {
306
- Method method = ClassUtils .getStaticMethod (InnerClass .class , "staticMethod" );
307
- method .invoke (null , (Object []) null );
308
- assertThat (InnerClass .noArgCalled ).as ("no argument method was not invoked." ).isTrue ();
309
- }
310
-
311
- @ Test
312
- void argsStaticMethod () throws IllegalAccessException , InvocationTargetException {
313
- Method method = ClassUtils .getStaticMethod (InnerClass .class , "argStaticMethod" , String .class );
314
- method .invoke (null , "test" );
315
- assertThat (InnerClass .argCalled ).as ("argument method was not invoked." ).isTrue ();
316
- }
317
-
318
- @ Test
319
- void overloadedStaticMethod () throws IllegalAccessException , InvocationTargetException {
320
- Method method = ClassUtils .getStaticMethod (InnerClass .class , "staticMethod" , String .class );
321
- method .invoke (null , "test" );
322
- assertThat (InnerClass .overloadedCalled ).as ("argument method was not invoked." ).isTrue ();
323
- }
324
-
325
301
@ Test
326
302
void isAssignable () {
327
303
assertThat (ClassUtils .isAssignable (Object .class , Object .class )).isTrue ();
@@ -433,6 +409,40 @@ void isPrimitiveOrWrapperWithWrapper(Class<?> type) {
433
409
}
434
410
435
411
412
+ @ Nested
413
+ class GetStaticMethodTests {
414
+
415
+ @ BeforeEach
416
+ void clearStatics () {
417
+ NestedClass .noArgCalled = false ;
418
+ NestedClass .argCalled = false ;
419
+ NestedClass .overloadedCalled = false ;
420
+ }
421
+
422
+ @ Test
423
+ void noArgsStaticMethod () throws IllegalAccessException , InvocationTargetException {
424
+ Method method = ClassUtils .getStaticMethod (NestedClass .class , "staticMethod" );
425
+ method .invoke (null , (Object []) null );
426
+ assertThat (NestedClass .noArgCalled ).as ("no argument method was not invoked." ).isTrue ();
427
+ }
428
+
429
+ @ Test
430
+ void argsStaticMethod () throws IllegalAccessException , InvocationTargetException {
431
+ Method method = ClassUtils .getStaticMethod (NestedClass .class , "argStaticMethod" , String .class );
432
+ method .invoke (null , "test" );
433
+ assertThat (NestedClass .argCalled ).as ("argument method was not invoked." ).isTrue ();
434
+ }
435
+
436
+ @ Test
437
+ void overloadedStaticMethod () throws IllegalAccessException , InvocationTargetException {
438
+ Method method = ClassUtils .getStaticMethod (NestedClass .class , "staticMethod" , String .class );
439
+ method .invoke (null , "test" );
440
+ assertThat (NestedClass .overloadedCalled ).as ("argument method was not invoked." ).isTrue ();
441
+ }
442
+
443
+ }
444
+
445
+
436
446
@ Target (ElementType .METHOD )
437
447
@ Retention (RetentionPolicy .RUNTIME )
438
448
@ ValueSource (classes = { Boolean .class , Character .class , Byte .class , Short .class ,
@@ -447,7 +457,7 @@ void isPrimitiveOrWrapperWithWrapper(Class<?> type) {
447
457
@interface PrimitiveTypes {
448
458
}
449
459
450
- public static class InnerClass {
460
+ public static class NestedClass {
451
461
452
462
static boolean noArgCalled ;
453
463
static boolean argCalled ;
0 commit comments