Skip to content

Commit f54395b

Browse files
committed
Polish BeanUtilsTests
1 parent 8036435 commit f54395b

File tree

1 file changed

+47
-67
lines changed

1 file changed

+47
-67
lines changed

spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java

Lines changed: 47 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
import java.util.Date;
2727
import java.util.List;
2828
import java.util.Locale;
29-
import java.util.stream.Stream;
3029

3130
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.params.ParameterizedTest;
32+
import org.junit.jupiter.params.provider.ValueSource;
3233

3334
import org.springframework.beans.factory.BeanFactory;
3435
import org.springframework.beans.propertyeditors.CustomDateEditor;
@@ -53,22 +54,22 @@
5354
* @author Sam Brannen
5455
* @since 19.05.2003
5556
*/
56-
public class BeanUtilsTests {
57+
class BeanUtilsTests {
5758

5859
@Test
59-
public void testInstantiateClassGivenInterface() {
60+
void testInstantiateClassGivenInterface() {
6061
assertThatExceptionOfType(FatalBeanException.class).isThrownBy(() ->
6162
BeanUtils.instantiateClass(List.class));
6263
}
6364

6465
@Test
65-
public void testInstantiateClassGivenClassWithoutDefaultConstructor() {
66+
void testInstantiateClassGivenClassWithoutDefaultConstructor() {
6667
assertThatExceptionOfType(FatalBeanException.class).isThrownBy(() ->
6768
BeanUtils.instantiateClass(CustomDateEditor.class));
6869
}
6970

7071
@Test // gh-22531
71-
public void testInstantiateClassWithOptionalNullableType() throws NoSuchMethodException {
72+
void testInstantiateClassWithOptionalNullableType() throws NoSuchMethodException {
7273
Constructor<BeanWithNullableTypes> ctor = BeanWithNullableTypes.class.getDeclaredConstructor(
7374
Integer.class, Boolean.class, String.class);
7475
BeanWithNullableTypes bean = BeanUtils.instantiateClass(ctor, null, null, "foo");
@@ -78,7 +79,7 @@ public void testInstantiateClassWithOptionalNullableType() throws NoSuchMethodEx
7879
}
7980

8081
@Test // gh-22531
81-
public void testInstantiateClassWithOptionalPrimitiveType() throws NoSuchMethodException {
82+
void testInstantiateClassWithOptionalPrimitiveType() throws NoSuchMethodException {
8283
Constructor<BeanWithPrimitiveTypes> ctor = BeanWithPrimitiveTypes.class.getDeclaredConstructor(int.class, boolean.class, String.class);
8384
BeanWithPrimitiveTypes bean = BeanUtils.instantiateClass(ctor, null, null, "foo");
8485
assertThat(bean.getCounter()).isEqualTo(0);
@@ -87,22 +88,22 @@ public void testInstantiateClassWithOptionalPrimitiveType() throws NoSuchMethodE
8788
}
8889

8990
@Test // gh-22531
90-
public void testInstantiateClassWithMoreArgsThanParameters() throws NoSuchMethodException {
91+
void testInstantiateClassWithMoreArgsThanParameters() throws NoSuchMethodException {
9192
Constructor<BeanWithPrimitiveTypes> ctor = BeanWithPrimitiveTypes.class.getDeclaredConstructor(int.class, boolean.class, String.class);
9293
assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() ->
9394
BeanUtils.instantiateClass(ctor, null, null, "foo", null));
9495
}
9596

9697
@Test
97-
public void testGetPropertyDescriptors() throws Exception {
98+
void testGetPropertyDescriptors() throws Exception {
9899
PropertyDescriptor[] actual = Introspector.getBeanInfo(TestBean.class).getPropertyDescriptors();
99100
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(TestBean.class);
100101
assertThat(descriptors).as("Descriptors should not be null").isNotNull();
101102
assertThat(descriptors.length).as("Invalid number of descriptors returned").isEqualTo(actual.length);
102103
}
103104

104105
@Test
105-
public void testBeanPropertyIsArray() {
106+
void testBeanPropertyIsArray() {
106107
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(ContainerBean.class);
107108
for (PropertyDescriptor descriptor : descriptors) {
108109
if ("containedBeans".equals(descriptor.getName())) {
@@ -113,12 +114,12 @@ public void testBeanPropertyIsArray() {
113114
}
114115

115116
@Test
116-
public void testFindEditorByConvention() {
117+
void testFindEditorByConvention() {
117118
assertThat(BeanUtils.findEditorByConvention(Resource.class).getClass()).isEqualTo(ResourceEditor.class);
118119
}
119120

120121
@Test
121-
public void testCopyProperties() throws Exception {
122+
void testCopyProperties() throws Exception {
122123
TestBean tb = new TestBean();
123124
tb.setName("rod");
124125
tb.setAge(32);
@@ -134,7 +135,7 @@ public void testCopyProperties() throws Exception {
134135
}
135136

136137
@Test
137-
public void testCopyPropertiesWithDifferentTypes1() throws Exception {
138+
void testCopyPropertiesWithDifferentTypes1() throws Exception {
138139
DerivedTestBean tb = new DerivedTestBean();
139140
tb.setName("rod");
140141
tb.setAge(32);
@@ -150,7 +151,7 @@ public void testCopyPropertiesWithDifferentTypes1() throws Exception {
150151
}
151152

152153
@Test
153-
public void testCopyPropertiesWithDifferentTypes2() throws Exception {
154+
void testCopyPropertiesWithDifferentTypes2() throws Exception {
154155
TestBean tb = new TestBean();
155156
tb.setName("rod");
156157
tb.setAge(32);
@@ -166,7 +167,7 @@ public void testCopyPropertiesWithDifferentTypes2() throws Exception {
166167
}
167168

168169
@Test
169-
public void testCopyPropertiesWithEditable() throws Exception {
170+
void testCopyPropertiesWithEditable() throws Exception {
170171
TestBean tb = new TestBean();
171172
assertThat(tb.getName() == null).as("Name empty").isTrue();
172173
tb.setAge(32);
@@ -184,7 +185,7 @@ public void testCopyPropertiesWithEditable() throws Exception {
184185
}
185186

186187
@Test
187-
public void testCopyPropertiesWithIgnore() throws Exception {
188+
void testCopyPropertiesWithIgnore() throws Exception {
188189
TestBean tb = new TestBean();
189190
assertThat(tb.getName() == null).as("Name empty").isTrue();
190191
tb.setAge(32);
@@ -202,7 +203,7 @@ public void testCopyPropertiesWithIgnore() throws Exception {
202203
}
203204

204205
@Test
205-
public void testCopyPropertiesWithIgnoredNonExistingProperty() {
206+
void testCopyPropertiesWithIgnoredNonExistingProperty() {
206207
NameAndSpecialProperty source = new NameAndSpecialProperty();
207208
source.setName("name");
208209
TestBean target = new TestBean();
@@ -211,7 +212,7 @@ public void testCopyPropertiesWithIgnoredNonExistingProperty() {
211212
}
212213

213214
@Test
214-
public void testCopyPropertiesWithInvalidProperty() {
215+
void testCopyPropertiesWithInvalidProperty() {
215216
InvalidProperty source = new InvalidProperty();
216217
source.setName("name");
217218
source.setFlag1(true);
@@ -224,39 +225,39 @@ public void testCopyPropertiesWithInvalidProperty() {
224225
}
225226

226227
@Test
227-
public void testResolveSimpleSignature() throws Exception {
228+
void testResolveSimpleSignature() throws Exception {
228229
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomething");
229230
assertSignatureEquals(desiredMethod, "doSomething");
230231
assertSignatureEquals(desiredMethod, "doSomething()");
231232
}
232233

233234
@Test
234-
public void testResolveInvalidSignatureEndParen() {
235+
void testResolveInvalidSignatureEndParen() {
235236
assertThatIllegalArgumentException().isThrownBy(() ->
236237
BeanUtils.resolveSignature("doSomething(", MethodSignatureBean.class));
237238
}
238239

239240
@Test
240-
public void testResolveInvalidSignatureStartParen() {
241+
void testResolveInvalidSignatureStartParen() {
241242
assertThatIllegalArgumentException().isThrownBy(() ->
242243
BeanUtils.resolveSignature("doSomething)", MethodSignatureBean.class));
243244
}
244245

245246
@Test
246-
public void testResolveWithAndWithoutArgList() throws Exception {
247+
void testResolveWithAndWithoutArgList() throws Exception {
247248
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", String.class, int.class);
248249
assertSignatureEquals(desiredMethod, "doSomethingElse");
249250
assertThat(BeanUtils.resolveSignature("doSomethingElse()", MethodSignatureBean.class)).isNull();
250251
}
251252

252253
@Test
253-
public void testResolveTypedSignature() throws Exception {
254+
void testResolveTypedSignature() throws Exception {
254255
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", String.class, int.class);
255256
assertSignatureEquals(desiredMethod, "doSomethingElse(java.lang.String, int)");
256257
}
257258

258259
@Test
259-
public void testResolveOverloadedSignature() throws Exception {
260+
void testResolveOverloadedSignature() throws Exception {
260261
// test resolve with no args
261262
Method desiredMethod = MethodSignatureBean.class.getMethod("overloaded");
262263
assertSignatureEquals(desiredMethod, "overloaded()");
@@ -271,7 +272,7 @@ public void testResolveOverloadedSignature() throws Exception {
271272
}
272273

273274
@Test
274-
public void testResolveSignatureWithArray() throws Exception {
275+
void testResolveSignatureWithArray() throws Exception {
275276
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingWithAnArray", String[].class);
276277
assertSignatureEquals(desiredMethod, "doSomethingWithAnArray(java.lang.String[])");
277278

@@ -280,7 +281,7 @@ public void testResolveSignatureWithArray() throws Exception {
280281
}
281282

282283
@Test
283-
public void testSPR6063() {
284+
void testSPR6063() {
284285
PropertyDescriptor[] descrs = BeanUtils.getPropertyDescriptors(Bean.class);
285286

286287
PropertyDescriptor keyDescr = BeanUtils.getPropertyDescriptor(Bean.class, "value");
@@ -292,57 +293,36 @@ public void testSPR6063() {
292293
}
293294
}
294295

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) {
334303
assertThat(BeanUtils.isSimpleValueType(type)).as("Type [" + type.getName() + "] should be a simple value type").isTrue();
335304
}
336305

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) {
338309
assertThat(BeanUtils.isSimpleValueType(type)).as("Type [" + type.getName() + "] should not be a simple value type").isFalse();
339310
}
340311

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) {
342320
assertThat(BeanUtils.isSimpleProperty(type)).as("Type [" + type.getName() + "] should be a simple property").isTrue();
343321
}
344322

345-
private void assertIsNotSimpleProperty(Class<?> type) {
323+
@ParameterizedTest
324+
@ValueSource(classes = { Object.class, List.class, void.class, Void.class })
325+
void isNotSimpleProperty(Class<?> type) {
346326
assertThat(BeanUtils.isSimpleProperty(type)).as("Type [" + type.getName() + "] should not be a simple property").isFalse();
347327
}
348328

0 commit comments

Comments
 (0)