Skip to content

Commit fcbd3b1

Browse files
committed
Added test for inconsistency between read and write method
Issue: SPR-11361
1 parent d004b63 commit fcbd3b1

File tree

3 files changed

+49
-31
lines changed

3 files changed

+49
-31
lines changed

spring-beans/src/main/java/org/springframework/beans/BeanWrapper.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,11 +79,13 @@ public interface BeanWrapper extends ConfigurablePropertyAccessor {
7979
PropertyDescriptor getPropertyDescriptor(String propertyName) throws InvalidPropertyException;
8080

8181
/**
82-
* Set whether this BeanWrapper should attempt to "auto-grow" a nested path that contains a null value.
83-
* <p>If "true", a null path location will be populated with a default object value and traversed
84-
* instead of resulting in a {@link NullValueInNestedPathException}. Turning this flag on also
85-
* enables auto-growth of collection elements when accessing an out-of-bounds index.
86-
* <p>Default is "false" on a plain BeanWrapper.
82+
* Set whether this BeanWrapper should attempt to "auto-grow" a
83+
* nested path that contains a {@code null} value.
84+
* <p>If {@code true}, a {@code null} path location will be populated
85+
* with a default object value and traversed instead of resulting in a
86+
* {@link NullValueInNestedPathException}. Turning this flag on also enables
87+
* auto-growth of collection elements when accessing an out-of-bounds index.
88+
* <p>Default is {@code false} on a plain BeanWrapper.
8789
*/
8890
void setAutoGrowNestedPaths(boolean autoGrowNestedPaths);
8991

spring-beans/src/main/java/org/springframework/beans/BeanWrapperImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,8 +871,9 @@ private Object growArrayIfNecessary(Object array, int index, String name) {
871871
}
872872
}
873873

874-
private void growCollectionIfNecessary(Collection<Object> collection, int index,
875-
String name, PropertyDescriptor pd, int nestingLevel) {
874+
private void growCollectionIfNecessary(Collection<Object> collection, int index, String name,
875+
PropertyDescriptor pd, int nestingLevel) {
876+
876877
if (!this.autoGrowNestedPaths) {
877878
return;
878879
}

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

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,6 @@
1616

1717
package org.springframework.beans;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertSame;
23-
import static org.junit.Assert.assertTrue;
24-
import static org.junit.Assert.fail;
25-
2619
import java.beans.PropertyEditorSupport;
2720
import java.math.BigDecimal;
2821
import java.math.BigInteger;
@@ -44,27 +37,27 @@
4437

4538
import org.apache.commons.logging.LogFactory;
4639
import org.junit.Test;
40+
4741
import org.springframework.beans.factory.annotation.Autowire;
4842
import org.springframework.beans.propertyeditors.CustomNumberEditor;
4943
import org.springframework.beans.propertyeditors.StringArrayPropertyEditor;
5044
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
5145
import org.springframework.beans.support.DerivedFromProtectedBaseBean;
46+
import org.springframework.core.convert.ConversionFailedException;
47+
import org.springframework.core.convert.TypeDescriptor;
48+
import org.springframework.core.convert.support.DefaultConversionService;
49+
import org.springframework.core.convert.support.GenericConversionService;
5250
import org.springframework.tests.Assume;
5351
import org.springframework.tests.TestGroup;
5452
import org.springframework.tests.sample.beans.BooleanTestBean;
5553
import org.springframework.tests.sample.beans.ITestBean;
5654
import org.springframework.tests.sample.beans.IndexedTestBean;
5755
import org.springframework.tests.sample.beans.NumberTestBean;
5856
import org.springframework.tests.sample.beans.TestBean;
59-
import org.springframework.core.convert.ConversionFailedException;
60-
import org.springframework.core.convert.TypeDescriptor;
61-
import org.springframework.core.convert.support.DefaultConversionService;
62-
import org.springframework.core.convert.support.GenericConversionService;
6357
import org.springframework.util.StopWatch;
6458
import org.springframework.util.StringUtils;
6559

6660
import static org.hamcrest.Matchers.*;
67-
6861
import static org.junit.Assert.*;
6962

7063
/**
@@ -1556,28 +1549,37 @@ public void testWildcardedGenericEnum() {
15561549
@Test
15571550
public void cornerSpr10115() {
15581551
Spr10115Bean foo = new Spr10115Bean();
1559-
BeanWrapperImpl bwi = new BeanWrapperImpl();
1560-
bwi.setWrappedInstance(foo);
1552+
BeanWrapperImpl bwi = new BeanWrapperImpl(foo);
15611553
bwi.setPropertyValue("prop1", "val1");
15621554
assertEquals("val1", Spr10115Bean.prop1);
15631555
}
15641556

15651557
@Test
1566-
public void testArrayToObject() throws Exception {
1558+
public void testArrayToObject() {
15671559
ArrayToObject foo = new ArrayToObject();
1568-
BeanWrapperImpl bwi = new BeanWrapperImpl();
1569-
bwi.setWrappedInstance(foo);
1560+
BeanWrapperImpl bwi = new BeanWrapperImpl(foo);
15701561

15711562
Object[] array = new Object[] {"1","2"};
1572-
bwi.setPropertyValue("object", array );
1563+
bwi.setPropertyValue("object", array);
15731564
assertThat(foo.getObject(), equalTo((Object) array));
15741565

15751566
array = new Object[] {"1"};
1576-
bwi.setPropertyValue("object", array );
1567+
bwi.setPropertyValue("object", array);
15771568
assertThat(foo.getObject(), equalTo((Object) array));
1578-
}
1569+
}
1570+
1571+
@Test
1572+
public void testPropertyTypeMismatch() {
1573+
PropertyTypeMismatch foo = new PropertyTypeMismatch();
1574+
BeanWrapperImpl bwi = new BeanWrapperImpl(foo);
1575+
bwi.setPropertyValue("object", "a String");
1576+
assertEquals("a String", foo.value);
1577+
assertEquals(8, bwi.getPropertyValue("object"));
1578+
}
1579+
15791580

15801581
static class Spr10115Bean {
1582+
15811583
private static String prop1;
15821584

15831585
public static void setProp1(String prop1) {
@@ -1962,18 +1964,31 @@ public enum TestEnum {
19621964
}
19631965

19641966

1965-
static class ArrayToObject {
1967+
public static class ArrayToObject {
19661968

19671969
private Object object;
19681970

1969-
19701971
public void setObject(Object object) {
19711972
this.object = object;
19721973
}
19731974

19741975
public Object getObject() {
19751976
return object;
19761977
}
1978+
}
1979+
19771980

1981+
public static class PropertyTypeMismatch {
1982+
1983+
public String value;
1984+
1985+
public void setObject(String object) {
1986+
this.value = object;
1987+
}
1988+
1989+
public Integer getObject() {
1990+
return (this.value != null ? this.value.length() : null);
1991+
}
19781992
}
1993+
19791994
}

0 commit comments

Comments
 (0)