26
26
import java .util .Date ;
27
27
import java .util .List ;
28
28
import java .util .Locale ;
29
- import java .util .stream .Stream ;
30
29
31
30
import org .junit .jupiter .api .Test ;
31
+ import org .junit .jupiter .params .ParameterizedTest ;
32
+ import org .junit .jupiter .params .provider .ValueSource ;
32
33
33
34
import org .springframework .beans .factory .BeanFactory ;
34
35
import org .springframework .beans .propertyeditors .CustomDateEditor ;
53
54
* @author Sam Brannen
54
55
* @since 19.05.2003
55
56
*/
56
- public class BeanUtilsTests {
57
+ class BeanUtilsTests {
57
58
58
59
@ Test
59
- public void testInstantiateClassGivenInterface () {
60
+ void testInstantiateClassGivenInterface () {
60
61
assertThatExceptionOfType (FatalBeanException .class ).isThrownBy (() ->
61
62
BeanUtils .instantiateClass (List .class ));
62
63
}
63
64
64
65
@ Test
65
- public void testInstantiateClassGivenClassWithoutDefaultConstructor () {
66
+ void testInstantiateClassGivenClassWithoutDefaultConstructor () {
66
67
assertThatExceptionOfType (FatalBeanException .class ).isThrownBy (() ->
67
68
BeanUtils .instantiateClass (CustomDateEditor .class ));
68
69
}
69
70
70
71
@ Test // gh-22531
71
- public void testInstantiateClassWithOptionalNullableType () throws NoSuchMethodException {
72
+ void testInstantiateClassWithOptionalNullableType () throws NoSuchMethodException {
72
73
Constructor <BeanWithNullableTypes > ctor = BeanWithNullableTypes .class .getDeclaredConstructor (
73
74
Integer .class , Boolean .class , String .class );
74
75
BeanWithNullableTypes bean = BeanUtils .instantiateClass (ctor , null , null , "foo" );
@@ -78,7 +79,7 @@ public void testInstantiateClassWithOptionalNullableType() throws NoSuchMethodEx
78
79
}
79
80
80
81
@ Test // gh-22531
81
- public void testInstantiateClassWithOptionalPrimitiveType () throws NoSuchMethodException {
82
+ void testInstantiateClassWithOptionalPrimitiveType () throws NoSuchMethodException {
82
83
Constructor <BeanWithPrimitiveTypes > ctor = BeanWithPrimitiveTypes .class .getDeclaredConstructor (int .class , boolean .class , String .class );
83
84
BeanWithPrimitiveTypes bean = BeanUtils .instantiateClass (ctor , null , null , "foo" );
84
85
assertThat (bean .getCounter ()).isEqualTo (0 );
@@ -87,22 +88,22 @@ public void testInstantiateClassWithOptionalPrimitiveType() throws NoSuchMethodE
87
88
}
88
89
89
90
@ Test // gh-22531
90
- public void testInstantiateClassWithMoreArgsThanParameters () throws NoSuchMethodException {
91
+ void testInstantiateClassWithMoreArgsThanParameters () throws NoSuchMethodException {
91
92
Constructor <BeanWithPrimitiveTypes > ctor = BeanWithPrimitiveTypes .class .getDeclaredConstructor (int .class , boolean .class , String .class );
92
93
assertThatExceptionOfType (BeanInstantiationException .class ).isThrownBy (() ->
93
94
BeanUtils .instantiateClass (ctor , null , null , "foo" , null ));
94
95
}
95
96
96
97
@ Test
97
- public void testGetPropertyDescriptors () throws Exception {
98
+ void testGetPropertyDescriptors () throws Exception {
98
99
PropertyDescriptor [] actual = Introspector .getBeanInfo (TestBean .class ).getPropertyDescriptors ();
99
100
PropertyDescriptor [] descriptors = BeanUtils .getPropertyDescriptors (TestBean .class );
100
101
assertThat (descriptors ).as ("Descriptors should not be null" ).isNotNull ();
101
102
assertThat (descriptors .length ).as ("Invalid number of descriptors returned" ).isEqualTo (actual .length );
102
103
}
103
104
104
105
@ Test
105
- public void testBeanPropertyIsArray () {
106
+ void testBeanPropertyIsArray () {
106
107
PropertyDescriptor [] descriptors = BeanUtils .getPropertyDescriptors (ContainerBean .class );
107
108
for (PropertyDescriptor descriptor : descriptors ) {
108
109
if ("containedBeans" .equals (descriptor .getName ())) {
@@ -113,12 +114,12 @@ public void testBeanPropertyIsArray() {
113
114
}
114
115
115
116
@ Test
116
- public void testFindEditorByConvention () {
117
+ void testFindEditorByConvention () {
117
118
assertThat (BeanUtils .findEditorByConvention (Resource .class ).getClass ()).isEqualTo (ResourceEditor .class );
118
119
}
119
120
120
121
@ Test
121
- public void testCopyProperties () throws Exception {
122
+ void testCopyProperties () throws Exception {
122
123
TestBean tb = new TestBean ();
123
124
tb .setName ("rod" );
124
125
tb .setAge (32 );
@@ -134,7 +135,7 @@ public void testCopyProperties() throws Exception {
134
135
}
135
136
136
137
@ Test
137
- public void testCopyPropertiesWithDifferentTypes1 () throws Exception {
138
+ void testCopyPropertiesWithDifferentTypes1 () throws Exception {
138
139
DerivedTestBean tb = new DerivedTestBean ();
139
140
tb .setName ("rod" );
140
141
tb .setAge (32 );
@@ -150,7 +151,7 @@ public void testCopyPropertiesWithDifferentTypes1() throws Exception {
150
151
}
151
152
152
153
@ Test
153
- public void testCopyPropertiesWithDifferentTypes2 () throws Exception {
154
+ void testCopyPropertiesWithDifferentTypes2 () throws Exception {
154
155
TestBean tb = new TestBean ();
155
156
tb .setName ("rod" );
156
157
tb .setAge (32 );
@@ -166,7 +167,7 @@ public void testCopyPropertiesWithDifferentTypes2() throws Exception {
166
167
}
167
168
168
169
@ Test
169
- public void testCopyPropertiesWithEditable () throws Exception {
170
+ void testCopyPropertiesWithEditable () throws Exception {
170
171
TestBean tb = new TestBean ();
171
172
assertThat (tb .getName () == null ).as ("Name empty" ).isTrue ();
172
173
tb .setAge (32 );
@@ -184,7 +185,7 @@ public void testCopyPropertiesWithEditable() throws Exception {
184
185
}
185
186
186
187
@ Test
187
- public void testCopyPropertiesWithIgnore () throws Exception {
188
+ void testCopyPropertiesWithIgnore () throws Exception {
188
189
TestBean tb = new TestBean ();
189
190
assertThat (tb .getName () == null ).as ("Name empty" ).isTrue ();
190
191
tb .setAge (32 );
@@ -202,7 +203,7 @@ public void testCopyPropertiesWithIgnore() throws Exception {
202
203
}
203
204
204
205
@ Test
205
- public void testCopyPropertiesWithIgnoredNonExistingProperty () {
206
+ void testCopyPropertiesWithIgnoredNonExistingProperty () {
206
207
NameAndSpecialProperty source = new NameAndSpecialProperty ();
207
208
source .setName ("name" );
208
209
TestBean target = new TestBean ();
@@ -211,7 +212,7 @@ public void testCopyPropertiesWithIgnoredNonExistingProperty() {
211
212
}
212
213
213
214
@ Test
214
- public void testCopyPropertiesWithInvalidProperty () {
215
+ void testCopyPropertiesWithInvalidProperty () {
215
216
InvalidProperty source = new InvalidProperty ();
216
217
source .setName ("name" );
217
218
source .setFlag1 (true );
@@ -224,39 +225,39 @@ public void testCopyPropertiesWithInvalidProperty() {
224
225
}
225
226
226
227
@ Test
227
- public void testResolveSimpleSignature () throws Exception {
228
+ void testResolveSimpleSignature () throws Exception {
228
229
Method desiredMethod = MethodSignatureBean .class .getMethod ("doSomething" );
229
230
assertSignatureEquals (desiredMethod , "doSomething" );
230
231
assertSignatureEquals (desiredMethod , "doSomething()" );
231
232
}
232
233
233
234
@ Test
234
- public void testResolveInvalidSignatureEndParen () {
235
+ void testResolveInvalidSignatureEndParen () {
235
236
assertThatIllegalArgumentException ().isThrownBy (() ->
236
237
BeanUtils .resolveSignature ("doSomething(" , MethodSignatureBean .class ));
237
238
}
238
239
239
240
@ Test
240
- public void testResolveInvalidSignatureStartParen () {
241
+ void testResolveInvalidSignatureStartParen () {
241
242
assertThatIllegalArgumentException ().isThrownBy (() ->
242
243
BeanUtils .resolveSignature ("doSomething)" , MethodSignatureBean .class ));
243
244
}
244
245
245
246
@ Test
246
- public void testResolveWithAndWithoutArgList () throws Exception {
247
+ void testResolveWithAndWithoutArgList () throws Exception {
247
248
Method desiredMethod = MethodSignatureBean .class .getMethod ("doSomethingElse" , String .class , int .class );
248
249
assertSignatureEquals (desiredMethod , "doSomethingElse" );
249
250
assertThat (BeanUtils .resolveSignature ("doSomethingElse()" , MethodSignatureBean .class )).isNull ();
250
251
}
251
252
252
253
@ Test
253
- public void testResolveTypedSignature () throws Exception {
254
+ void testResolveTypedSignature () throws Exception {
254
255
Method desiredMethod = MethodSignatureBean .class .getMethod ("doSomethingElse" , String .class , int .class );
255
256
assertSignatureEquals (desiredMethod , "doSomethingElse(java.lang.String, int)" );
256
257
}
257
258
258
259
@ Test
259
- public void testResolveOverloadedSignature () throws Exception {
260
+ void testResolveOverloadedSignature () throws Exception {
260
261
// test resolve with no args
261
262
Method desiredMethod = MethodSignatureBean .class .getMethod ("overloaded" );
262
263
assertSignatureEquals (desiredMethod , "overloaded()" );
@@ -271,7 +272,7 @@ public void testResolveOverloadedSignature() throws Exception {
271
272
}
272
273
273
274
@ Test
274
- public void testResolveSignatureWithArray () throws Exception {
275
+ void testResolveSignatureWithArray () throws Exception {
275
276
Method desiredMethod = MethodSignatureBean .class .getMethod ("doSomethingWithAnArray" , String [].class );
276
277
assertSignatureEquals (desiredMethod , "doSomethingWithAnArray(java.lang.String[])" );
277
278
@@ -280,7 +281,7 @@ public void testResolveSignatureWithArray() throws Exception {
280
281
}
281
282
282
283
@ Test
283
- public void testSPR6063 () {
284
+ void testSPR6063 () {
284
285
PropertyDescriptor [] descrs = BeanUtils .getPropertyDescriptors (Bean .class );
285
286
286
287
PropertyDescriptor keyDescr = BeanUtils .getPropertyDescriptor (Bean .class , "value" );
@@ -292,57 +293,36 @@ public void testSPR6063() {
292
293
}
293
294
}
294
295
295
- @ Test
296
- public void isSimpleValueType () {
297
- Stream .of (
298
-
299
- boolean .class , char .class , byte .class , short .class , int .class ,
300
- long .class , float .class , double .class ,
301
-
302
- Boolean .class , Character .class , Byte .class , Short .class , Integer .class ,
303
- Long .class , Float .class , Double .class ,
304
-
305
- DayOfWeek .class , String .class , Date .class , URI .class , URL .class , Locale .class , Class .class
306
-
307
- ).forEach (this ::assertIsSimpleValueType );
308
-
309
- Stream .of (int [].class , Object .class , List .class , void .class , Void .class )
310
- .forEach (this ::assertIsNotSimpleValueType );
311
- }
312
-
313
- @ Test
314
- public void isSimpleProperty () {
315
- Stream .of (
316
-
317
- boolean .class , char .class , byte .class , short .class , int .class ,
318
- long .class , float .class , double .class ,
319
-
320
- Boolean .class , Character .class , Byte .class , Short .class , Integer .class ,
321
- Long .class , Float .class , Double .class ,
322
-
323
- DayOfWeek .class , String .class , Date .class , URI .class , URL .class , Locale .class , Class .class ,
324
-
325
- boolean [].class , Boolean [].class , Date [].class
326
-
327
- ).forEach (this ::assertIsSimpleProperty );
328
-
329
- Stream .of (Object .class , List .class , void .class , Void .class )
330
- .forEach (this ::assertIsNotSimpleProperty );
331
- }
332
-
333
- private void assertIsSimpleValueType (Class <?> type ) {
296
+ @ ParameterizedTest
297
+ @ ValueSource (classes = {
298
+ boolean .class , char .class , byte .class , short .class , int .class , long .class , float .class , double .class ,
299
+ Boolean .class , Character .class , Byte .class , Short .class , Integer .class , Long .class , Float .class , Double .class ,
300
+ DayOfWeek .class , String .class , Date .class , URI .class , URL .class , Locale .class , Class .class
301
+ })
302
+ void isSimpleValueType (Class <?> type ) {
334
303
assertThat (BeanUtils .isSimpleValueType (type )).as ("Type [" + type .getName () + "] should be a simple value type" ).isTrue ();
335
304
}
336
305
337
- private void assertIsNotSimpleValueType (Class <?> type ) {
306
+ @ ParameterizedTest
307
+ @ ValueSource (classes = { int [].class , Object .class , List .class , void .class , Void .class })
308
+ void isNotSimpleValueType (Class <?> type ) {
338
309
assertThat (BeanUtils .isSimpleValueType (type )).as ("Type [" + type .getName () + "] should not be a simple value type" ).isFalse ();
339
310
}
340
311
341
- private void assertIsSimpleProperty (Class <?> type ) {
312
+ @ ParameterizedTest
313
+ @ ValueSource (classes = {
314
+ boolean .class , char .class , byte .class , short .class , int .class , long .class , float .class , double .class ,
315
+ Boolean .class , Character .class , Byte .class , Short .class , Integer .class , Long .class , Float .class , Double .class ,
316
+ DayOfWeek .class , String .class , Date .class , URI .class , URL .class , Locale .class , Class .class ,
317
+ boolean [].class , Boolean [].class , Date [].class
318
+ })
319
+ void isSimpleProperty (Class <?> type ) {
342
320
assertThat (BeanUtils .isSimpleProperty (type )).as ("Type [" + type .getName () + "] should be a simple property" ).isTrue ();
343
321
}
344
322
345
- private void assertIsNotSimpleProperty (Class <?> type ) {
323
+ @ ParameterizedTest
324
+ @ ValueSource (classes = { Object .class , List .class , void .class , Void .class })
325
+ void isNotSimpleProperty (Class <?> type ) {
346
326
assertThat (BeanUtils .isSimpleProperty (type )).as ("Type [" + type .getName () + "] should not be a simple property" ).isFalse ();
347
327
}
348
328
0 commit comments