|
|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2019 the original author or authors. |
|
|
|
|
* Copyright 2002-2020 the original author or authors. |
|
|
|
|
* |
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
|
@ -58,19 +58,19 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
@@ -58,19 +58,19 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
|
|
|
|
class BeanUtilsTests { |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testInstantiateClassGivenInterface() { |
|
|
|
|
void instantiateClassGivenInterface() { |
|
|
|
|
assertThatExceptionOfType(FatalBeanException.class).isThrownBy(() -> |
|
|
|
|
BeanUtils.instantiateClass(List.class)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testInstantiateClassGivenClassWithoutDefaultConstructor() { |
|
|
|
|
void instantiateClassGivenClassWithoutDefaultConstructor() { |
|
|
|
|
assertThatExceptionOfType(FatalBeanException.class).isThrownBy(() -> |
|
|
|
|
BeanUtils.instantiateClass(CustomDateEditor.class)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test // gh-22531
|
|
|
|
|
void testInstantiateClassWithOptionalNullableType() throws NoSuchMethodException { |
|
|
|
|
void instantiateClassWithOptionalNullableType() throws NoSuchMethodException { |
|
|
|
|
Constructor<BeanWithNullableTypes> ctor = BeanWithNullableTypes.class.getDeclaredConstructor( |
|
|
|
|
Integer.class, Boolean.class, String.class); |
|
|
|
|
BeanWithNullableTypes bean = BeanUtils.instantiateClass(ctor, null, null, "foo"); |
|
|
|
|
@ -80,7 +80,7 @@ class BeanUtilsTests {
@@ -80,7 +80,7 @@ class BeanUtilsTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test // gh-22531
|
|
|
|
|
void testInstantiateClassWithOptionalPrimitiveType() throws NoSuchMethodException { |
|
|
|
|
void instantiateClassWithOptionalPrimitiveType() throws NoSuchMethodException { |
|
|
|
|
Constructor<BeanWithPrimitiveTypes> ctor = BeanWithPrimitiveTypes.class.getDeclaredConstructor(int.class, boolean.class, String.class); |
|
|
|
|
BeanWithPrimitiveTypes bean = BeanUtils.instantiateClass(ctor, null, null, "foo"); |
|
|
|
|
assertThat(bean.getCounter()).isEqualTo(0); |
|
|
|
|
@ -88,21 +88,21 @@ class BeanUtilsTests {
@@ -88,21 +88,21 @@ class BeanUtilsTests {
|
|
|
|
|
assertThat(bean.getValue()).isEqualTo("foo"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test // gh-22531
|
|
|
|
|
void testInstantiateClassWithMoreArgsThanParameters() throws NoSuchMethodException { |
|
|
|
|
@Test // gh-22531
|
|
|
|
|
void instantiateClassWithMoreArgsThanParameters() throws NoSuchMethodException { |
|
|
|
|
Constructor<BeanWithPrimitiveTypes> ctor = BeanWithPrimitiveTypes.class.getDeclaredConstructor(int.class, boolean.class, String.class); |
|
|
|
|
assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() -> |
|
|
|
|
BeanUtils.instantiateClass(ctor, null, null, "foo", null)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testInstantiatePrivateClassWithPrivateConstructor() throws NoSuchMethodException { |
|
|
|
|
void instantiatePrivateClassWithPrivateConstructor() throws NoSuchMethodException { |
|
|
|
|
Constructor<PrivateBeanWithPrivateConstructor> ctor = PrivateBeanWithPrivateConstructor.class.getDeclaredConstructor(); |
|
|
|
|
BeanUtils.instantiateClass(ctor); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testGetPropertyDescriptors() throws Exception { |
|
|
|
|
void getPropertyDescriptors() throws Exception { |
|
|
|
|
PropertyDescriptor[] actual = Introspector.getBeanInfo(TestBean.class).getPropertyDescriptors(); |
|
|
|
|
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(TestBean.class); |
|
|
|
|
assertThat(descriptors).as("Descriptors should not be null").isNotNull(); |
|
|
|
|
@ -110,7 +110,7 @@ class BeanUtilsTests {
@@ -110,7 +110,7 @@ class BeanUtilsTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testBeanPropertyIsArray() { |
|
|
|
|
void beanPropertyIsArray() { |
|
|
|
|
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(ContainerBean.class); |
|
|
|
|
for (PropertyDescriptor descriptor : descriptors) { |
|
|
|
|
if ("containedBeans".equals(descriptor.getName())) { |
|
|
|
|
@ -121,12 +121,12 @@ class BeanUtilsTests {
@@ -121,12 +121,12 @@ class BeanUtilsTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testFindEditorByConvention() { |
|
|
|
|
void findEditorByConvention() { |
|
|
|
|
assertThat(BeanUtils.findEditorByConvention(Resource.class).getClass()).isEqualTo(ResourceEditor.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testCopyProperties() throws Exception { |
|
|
|
|
void copyProperties() throws Exception { |
|
|
|
|
TestBean tb = new TestBean(); |
|
|
|
|
tb.setName("rod"); |
|
|
|
|
tb.setAge(32); |
|
|
|
|
@ -142,7 +142,7 @@ class BeanUtilsTests {
@@ -142,7 +142,7 @@ class BeanUtilsTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testCopyPropertiesWithDifferentTypes1() throws Exception { |
|
|
|
|
void copyPropertiesWithDifferentTypes1() throws Exception { |
|
|
|
|
DerivedTestBean tb = new DerivedTestBean(); |
|
|
|
|
tb.setName("rod"); |
|
|
|
|
tb.setAge(32); |
|
|
|
|
@ -158,7 +158,7 @@ class BeanUtilsTests {
@@ -158,7 +158,7 @@ class BeanUtilsTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testCopyPropertiesWithDifferentTypes2() throws Exception { |
|
|
|
|
void copyPropertiesWithDifferentTypes2() throws Exception { |
|
|
|
|
TestBean tb = new TestBean(); |
|
|
|
|
tb.setName("rod"); |
|
|
|
|
tb.setAge(32); |
|
|
|
|
@ -192,7 +192,7 @@ class BeanUtilsTests {
@@ -192,7 +192,7 @@ class BeanUtilsTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testCopyPropertiesWithIgnore() throws Exception { |
|
|
|
|
void copyPropertiesWithIgnore() throws Exception { |
|
|
|
|
TestBean tb = new TestBean(); |
|
|
|
|
assertThat(tb.getName() == null).as("Name empty").isTrue(); |
|
|
|
|
tb.setAge(32); |
|
|
|
|
@ -210,7 +210,7 @@ class BeanUtilsTests {
@@ -210,7 +210,7 @@ class BeanUtilsTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testCopyPropertiesWithIgnoredNonExistingProperty() { |
|
|
|
|
void copyPropertiesWithIgnoredNonExistingProperty() { |
|
|
|
|
NameAndSpecialProperty source = new NameAndSpecialProperty(); |
|
|
|
|
source.setName("name"); |
|
|
|
|
TestBean target = new TestBean(); |
|
|
|
|
@ -219,7 +219,7 @@ class BeanUtilsTests {
@@ -219,7 +219,7 @@ class BeanUtilsTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testCopyPropertiesWithInvalidProperty() { |
|
|
|
|
void copyPropertiesWithInvalidProperty() { |
|
|
|
|
InvalidProperty source = new InvalidProperty(); |
|
|
|
|
source.setName("name"); |
|
|
|
|
source.setFlag1(true); |
|
|
|
|
@ -232,39 +232,39 @@ class BeanUtilsTests {
@@ -232,39 +232,39 @@ class BeanUtilsTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testResolveSimpleSignature() throws Exception { |
|
|
|
|
void resolveSimpleSignature() throws Exception { |
|
|
|
|
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomething"); |
|
|
|
|
assertSignatureEquals(desiredMethod, "doSomething"); |
|
|
|
|
assertSignatureEquals(desiredMethod, "doSomething()"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testResolveInvalidSignatureEndParen() { |
|
|
|
|
void resolveInvalidSignatureEndParen() { |
|
|
|
|
assertThatIllegalArgumentException().isThrownBy(() -> |
|
|
|
|
BeanUtils.resolveSignature("doSomething(", MethodSignatureBean.class)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testResolveInvalidSignatureStartParen() { |
|
|
|
|
void resolveInvalidSignatureStartParen() { |
|
|
|
|
assertThatIllegalArgumentException().isThrownBy(() -> |
|
|
|
|
BeanUtils.resolveSignature("doSomething)", MethodSignatureBean.class)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testResolveWithAndWithoutArgList() throws Exception { |
|
|
|
|
void resolveWithAndWithoutArgList() throws Exception { |
|
|
|
|
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", String.class, int.class); |
|
|
|
|
assertSignatureEquals(desiredMethod, "doSomethingElse"); |
|
|
|
|
assertThat(BeanUtils.resolveSignature("doSomethingElse()", MethodSignatureBean.class)).isNull(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testResolveTypedSignature() throws Exception { |
|
|
|
|
void resolveTypedSignature() throws Exception { |
|
|
|
|
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", String.class, int.class); |
|
|
|
|
assertSignatureEquals(desiredMethod, "doSomethingElse(java.lang.String, int)"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testResolveOverloadedSignature() throws Exception { |
|
|
|
|
void resolveOverloadedSignature() throws Exception { |
|
|
|
|
// test resolve with no args
|
|
|
|
|
Method desiredMethod = MethodSignatureBean.class.getMethod("overloaded"); |
|
|
|
|
assertSignatureEquals(desiredMethod, "overloaded()"); |
|
|
|
|
@ -279,7 +279,7 @@ class BeanUtilsTests {
@@ -279,7 +279,7 @@ class BeanUtilsTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testResolveSignatureWithArray() throws Exception { |
|
|
|
|
void resolveSignatureWithArray() throws Exception { |
|
|
|
|
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingWithAnArray", String[].class); |
|
|
|
|
assertSignatureEquals(desiredMethod, "doSomethingWithAnArray(java.lang.String[])"); |
|
|
|
|
|
|
|
|
|
@ -288,7 +288,7 @@ class BeanUtilsTests {
@@ -288,7 +288,7 @@ class BeanUtilsTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testSPR6063() { |
|
|
|
|
void spr6063() { |
|
|
|
|
PropertyDescriptor[] descrs = BeanUtils.getPropertyDescriptors(Bean.class); |
|
|
|
|
|
|
|
|
|
PropertyDescriptor keyDescr = BeanUtils.getPropertyDescriptor(Bean.class, "value"); |
|
|
|
|
|