1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2018 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.
42
42
*/
43
43
public class ReflectionTestUtilsTests {
44
44
45
- private static final Float PI = new Float ((float ) 22 / 7 );
45
+ private static final Float PI = Float . valueOf ((float ) 22 / 7 );
46
46
47
47
private final Person person = new PersonEntity ();
48
48
@@ -63,7 +63,7 @@ public void resetStaticFields() {
63
63
public void setFieldWithNullTargetObject () throws Exception {
64
64
exception .expect (IllegalArgumentException .class );
65
65
exception .expectMessage (startsWith ("Either targetObject or targetClass" ));
66
- setField ((Object ) null , "id" , new Long (99 ));
66
+ setField ((Object ) null , "id" , Long . valueOf (99 ));
67
67
}
68
68
69
69
@ Test
@@ -77,7 +77,7 @@ public void getFieldWithNullTargetObject() throws Exception {
77
77
public void setFieldWithNullTargetClass () throws Exception {
78
78
exception .expect (IllegalArgumentException .class );
79
79
exception .expectMessage (startsWith ("Either targetObject or targetClass" ));
80
- setField ((Class <?>) null , "id" , new Long (99 ));
80
+ setField ((Class <?>) null , "id" , Long . valueOf (99 ));
81
81
}
82
82
83
83
@ Test
@@ -91,21 +91,21 @@ public void getFieldWithNullTargetClass() throws Exception {
91
91
public void setFieldWithNullNameAndNullType () throws Exception {
92
92
exception .expect (IllegalArgumentException .class );
93
93
exception .expectMessage (startsWith ("Either name or type" ));
94
- setField (person , null , new Long (99 ), null );
94
+ setField (person , null , Long . valueOf (99 ), null );
95
95
}
96
96
97
97
@ Test
98
98
public void setFieldWithBogusName () throws Exception {
99
99
exception .expect (IllegalArgumentException .class );
100
100
exception .expectMessage (startsWith ("Could not find field 'bogus'" ));
101
- setField (person , "bogus" , new Long (99 ), long .class );
101
+ setField (person , "bogus" , Long . valueOf (99 ), long .class );
102
102
}
103
103
104
104
@ Test
105
105
public void setFieldWithWrongType () throws Exception {
106
106
exception .expect (IllegalArgumentException .class );
107
107
exception .expectMessage (startsWith ("Could not find field" ));
108
- setField (person , "id" , new Long (99 ), String .class );
108
+ setField (person , "id" , Long . valueOf (99 ), String .class );
109
109
}
110
110
111
111
@ Test
@@ -133,17 +133,17 @@ public void setFieldAndGetFieldViaCglibProxy() throws Exception {
133
133
134
134
private static void assertSetFieldAndGetFieldBehavior (Person person ) {
135
135
// Set reflectively
136
- setField (person , "id" , new Long (99 ), long .class );
136
+ setField (person , "id" , Long . valueOf (99 ), long .class );
137
137
setField (person , "name" , "Tom" );
138
- setField (person , "age" , new Integer (42 ));
138
+ setField (person , "age" , Integer . valueOf (42 ));
139
139
setField (person , "eyeColor" , "blue" , String .class );
140
140
setField (person , "likesPets" , Boolean .TRUE );
141
141
setField (person , "favoriteNumber" , PI , Number .class );
142
142
143
143
// Get reflectively
144
- assertEquals (new Long (99 ), getField (person , "id" ));
144
+ assertEquals (Long . valueOf (99 ), getField (person , "id" ));
145
145
assertEquals ("Tom" , getField (person , "name" ));
146
- assertEquals (new Integer (42 ), getField (person , "age" ));
146
+ assertEquals (Integer . valueOf (42 ), getField (person , "age" ));
147
147
assertEquals ("blue" , getField (person , "eyeColor" ));
148
148
assertEquals (Boolean .TRUE , getField (person , "likesPets" ));
149
149
assertEquals (PI , getField (person , "favoriteNumber" ));
@@ -247,33 +247,33 @@ public void getStaticFieldViaInstance() throws Exception {
247
247
248
248
@ Test
249
249
public void invokeSetterMethodAndInvokeGetterMethodWithExplicitMethodNames () throws Exception {
250
- invokeSetterMethod (person , "setId" , new Long (1 ), long .class );
250
+ invokeSetterMethod (person , "setId" , Long . valueOf (1 ), long .class );
251
251
invokeSetterMethod (person , "setName" , "Jerry" , String .class );
252
- invokeSetterMethod (person , "setAge" , new Integer (33 ), int .class );
252
+ invokeSetterMethod (person , "setAge" , Integer . valueOf (33 ), int .class );
253
253
invokeSetterMethod (person , "setEyeColor" , "green" , String .class );
254
254
invokeSetterMethod (person , "setLikesPets" , Boolean .FALSE , boolean .class );
255
- invokeSetterMethod (person , "setFavoriteNumber" , new Integer (42 ), Number .class );
255
+ invokeSetterMethod (person , "setFavoriteNumber" , Integer . valueOf (42 ), Number .class );
256
256
257
257
assertEquals ("ID (protected method in a superclass)" , 1 , person .getId ());
258
258
assertEquals ("name (private method)" , "Jerry" , person .getName ());
259
259
assertEquals ("age (protected method)" , 33 , person .getAge ());
260
260
assertEquals ("eye color (package private method)" , "green" , person .getEyeColor ());
261
261
assertEquals ("'likes pets' flag (protected method for a boolean)" , false , person .likesPets ());
262
- assertEquals ("'favorite number' (protected method for a Number)" , new Integer (42 ), person .getFavoriteNumber ());
262
+ assertEquals ("'favorite number' (protected method for a Number)" , Integer . valueOf (42 ), person .getFavoriteNumber ());
263
263
264
- assertEquals (new Long (1 ), invokeGetterMethod (person , "getId" ));
264
+ assertEquals (Long . valueOf (1 ), invokeGetterMethod (person , "getId" ));
265
265
assertEquals ("Jerry" , invokeGetterMethod (person , "getName" ));
266
- assertEquals (new Integer (33 ), invokeGetterMethod (person , "getAge" ));
266
+ assertEquals (Integer . valueOf (33 ), invokeGetterMethod (person , "getAge" ));
267
267
assertEquals ("green" , invokeGetterMethod (person , "getEyeColor" ));
268
268
assertEquals (Boolean .FALSE , invokeGetterMethod (person , "likesPets" ));
269
- assertEquals (new Integer (42 ), invokeGetterMethod (person , "getFavoriteNumber" ));
269
+ assertEquals (Integer . valueOf (42 ), invokeGetterMethod (person , "getFavoriteNumber" ));
270
270
}
271
271
272
272
@ Test
273
273
public void invokeSetterMethodAndInvokeGetterMethodWithJavaBeanPropertyNames () throws Exception {
274
- invokeSetterMethod (person , "id" , new Long (99 ), long .class );
274
+ invokeSetterMethod (person , "id" , Long . valueOf (99 ), long .class );
275
275
invokeSetterMethod (person , "name" , "Tom" );
276
- invokeSetterMethod (person , "age" , new Integer (42 ));
276
+ invokeSetterMethod (person , "age" , Integer . valueOf (42 ));
277
277
invokeSetterMethod (person , "eyeColor" , "blue" , String .class );
278
278
invokeSetterMethod (person , "likesPets" , Boolean .TRUE );
279
279
invokeSetterMethod (person , "favoriteNumber" , PI , Number .class );
@@ -285,9 +285,9 @@ public void invokeSetterMethodAndInvokeGetterMethodWithJavaBeanPropertyNames() t
285
285
assertEquals ("'likes pets' flag (protected method for a boolean)" , true , person .likesPets ());
286
286
assertEquals ("'favorite number' (protected method for a Number)" , PI , person .getFavoriteNumber ());
287
287
288
- assertEquals (new Long (99 ), invokeGetterMethod (person , "id" ));
288
+ assertEquals (Long . valueOf (99 ), invokeGetterMethod (person , "id" ));
289
289
assertEquals ("Tom" , invokeGetterMethod (person , "name" ));
290
- assertEquals (new Integer (42 ), invokeGetterMethod (person , "age" ));
290
+ assertEquals (Integer . valueOf (42 ), invokeGetterMethod (person , "age" ));
291
291
assertEquals ("blue" , invokeGetterMethod (person , "eyeColor" ));
292
292
assertEquals (Boolean .TRUE , invokeGetterMethod (person , "likesPets" ));
293
293
assertEquals (PI , invokeGetterMethod (person , "favoriteNumber" ));
@@ -347,8 +347,8 @@ public void invokeMethodSimulatingLifecycleEvents() {
347
347
assertNull ("text" , component .getText ());
348
348
349
349
// Simulate autowiring a configuration method
350
- invokeMethod (component , "configure" , new Integer (42 ), "enigma" );
351
- assertEquals ("number should have been configured" , new Integer (42 ), component .getNumber ());
350
+ invokeMethod (component , "configure" , Integer . valueOf (42 ), "enigma" );
351
+ assertEquals ("number should have been configured" , Integer . valueOf (42 ), component .getNumber ());
352
352
assertEquals ("text should have been configured" , "enigma" , component .getText ());
353
353
354
354
// Simulate @PostConstruct life-cycle event
@@ -379,14 +379,14 @@ public void invokeMethodWithIncompatibleArgumentTypes() {
379
379
public void invokeMethodWithTooFewArguments () {
380
380
exception .expect (IllegalStateException .class );
381
381
exception .expectMessage (startsWith ("Method not found" ));
382
- invokeMethod (component , "configure" , new Integer (42 ));
382
+ invokeMethod (component , "configure" , Integer . valueOf (42 ));
383
383
}
384
384
385
385
@ Test
386
386
public void invokeMethodWithTooManyArguments () {
387
387
exception .expect (IllegalStateException .class );
388
388
exception .expectMessage (startsWith ("Method not found" ));
389
- invokeMethod (component , "configure" , new Integer (42 ), "enigma" , "baz" , "quux" );
389
+ invokeMethod (component , "configure" , Integer . valueOf (42 ), "enigma" , "baz" , "quux" );
390
390
}
391
391
392
392
@ Test // SPR-14363
@@ -404,8 +404,8 @@ public void setFieldOnLegacyEntityWithSideEffectsInToString() {
404
404
405
405
@ Test // SPR-14363
406
406
public void invokeMethodOnLegacyEntityWithSideEffectsInToString () {
407
- invokeMethod (entity , "configure" , new Integer (42 ), "enigma" );
408
- assertEquals ("number should have been configured" , new Integer (42 ), entity .getNumber ());
407
+ invokeMethod (entity , "configure" , Integer . valueOf (42 ), "enigma" );
408
+ assertEquals ("number should have been configured" , Integer . valueOf (42 ), entity .getNumber ());
409
409
assertEquals ("text should have been configured" , "enigma" , entity .getText ());
410
410
}
411
411
0 commit comments