Polishing

This commit is contained in:
Sam Brannen 2020-04-28 12:57:17 +02:00
parent e9d63a0a7c
commit cdde19c0bc
1 changed files with 25 additions and 25 deletions

View File

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