Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit cad5732

Browse files
committed
Add test cases to SPR-8714
1 parent bd086b0 commit cad5732

File tree

7 files changed

+142
-78
lines changed

7 files changed

+142
-78
lines changed

SPR-8714/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<dependencies>
99
<dependency>
1010
<groupId>org.springframework</groupId>
11-
<artifactId>spring-context</artifactId>
11+
<artifactId>spring-webmvc</artifactId>
1212
<version>3.1.0.BUILD-SNAPSHOT</version>
1313
</dependency>
1414
<dependency>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.springframework.issues;
2+
3+
public class TestBean {
4+
5+
}

SPR-8714/src/main/java/org/springframework/issues/TestMap.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616

1717
package org.springframework.issues;
1818

19-
import java.util.Collections;
2019
import java.util.HashMap;
2120
import java.util.Map;
22-
import java.util.Set;
2321

2422
/**
25-
* See
23+
* Original source
2624
* https://github.com/rgladwell/spring-map-converter-issue/blob/master/src/main/java/uk/co/bbc/spring/issue/TestMap.java
2725
*/
2826
@SuppressWarnings("serial")
@@ -34,18 +32,8 @@ public void init() {
3432
init = true;
3533
}
3634

37-
@Override
38-
@SuppressWarnings("unchecked")
39-
public Set<java.util.Map.Entry<K, V>> entrySet() {
40-
return Collections.EMPTY_SET;
41-
}
42-
43-
@Override
44-
public int size() {
45-
if (!init) {
46-
throw new IllegalStateException("map not initialised");
47-
}
48-
return super.size();
35+
public boolean isInitialized() {
36+
return this.init;
4937
}
5038

5139
}
Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/*
22
* Copyright 2002-2011 the original author or authors.
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* Licensed under the Apache License, Stringersion 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
88
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY StringIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
@@ -22,14 +22,45 @@
2222
*/
2323
public class TestMapConsumer {
2424

25-
private TestMap<String, String> testMap;
25+
private TestMap<String,String> testMap;
2626

27-
public TestMap<String, String> getTestMap() {
27+
private TestMap<Number, Number> testNumberMap;
28+
29+
private TestMap<Integer, Integer> testIntegerMap;
30+
31+
private TestMap<TestBean, TestBean> testBeanMap;
32+
33+
public TestMap<String,String> getTestMap() {
2834
return testMap;
2935
}
3036

31-
public void setTestMap(TestMap<String, String> testMap) {
37+
public void setTestMap(TestMap<String,String> testMap) {
3238
this.testMap = testMap;
3339
}
3440

41+
public TestMap<TestBean, TestBean> getTestBeanMap() {
42+
return testBeanMap;
43+
}
44+
45+
public void setTestBeanMap(TestMap<TestBean, TestBean> testBeanMap) {
46+
this.testBeanMap = testBeanMap;
47+
}
48+
49+
public TestMap<Number, Number> getTestNumberMap() {
50+
return testNumberMap;
51+
}
52+
53+
public void setTestNumberMap(TestMap<Number, Number> testNumberMap) {
54+
this.testNumberMap = testNumberMap;
55+
}
56+
57+
public TestMap<Integer, Integer> getTestIntegerMap() {
58+
return testIntegerMap;
59+
}
60+
61+
public void setTestIntegerMap(TestMap<Integer, Integer> testIntegerMap) {
62+
this.testIntegerMap = testIntegerMap;
63+
}
64+
65+
3566
}

SPR-8714/src/main/java/org/springframework/issues/TestMapFactory.java

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,115 @@
11
package org.springframework.issues;
22

3-
import static org.junit.Assert.*;
3+
import static org.junit.Assert.assertSame;
4+
import static org.junit.Assert.assertTrue;
45

56
import org.junit.Test;
6-
import org.springframework.context.support.GenericXmlApplicationContext;
7+
import org.springframework.beans.MutablePropertyValues;
8+
import org.springframework.format.support.DefaultFormattingConversionService;
9+
import org.springframework.web.bind.WebDataBinder;
710

811
/**
912
* Unit test that reproduces an issue reported against SPR JIRA. @Test methods within
1013
* need not pass with the green bar! Rather they should fail in such a way that
1114
* demonstrates the reported issue.
1215
*/
1316
public class ReproTests {
17+
18+
@Test
19+
public void stringToString() {
20+
21+
TestMap<String, String> source = new TestMap<String, String>();
22+
source.init();
23+
source.put("key", "value");
24+
source.put("key", null);
25+
26+
TestMapConsumer target = new TestMapConsumer();
27+
28+
WebDataBinder dataBinder = new WebDataBinder(target, "");
29+
dataBinder.setConversionService(new DefaultFormattingConversionService());
30+
31+
MutablePropertyValues pvs = new MutablePropertyValues();
32+
pvs.addPropertyValue("testMap", source);
33+
dataBinder.bind(pvs);
34+
35+
// FallbackObjectToStringConverter for key-value pairs
36+
37+
assertSame(source.keySet().iterator().next(), target.getTestMap().keySet().iterator().next());
38+
assertSame(source.values().iterator().next(), target.getTestMap().values().iterator().next());
39+
40+
assertTrue(target.getTestMap().isInitialized());
41+
}
1442

1543
@Test
16-
public void repro() {
17-
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
18-
ctx.load("classpath:org/springframework/issues/ReproTests-context.xml");
19-
ctx.refresh();
44+
public void numberToNumber() {
45+
46+
TestMap<Integer, Integer> source = new TestMap<Integer, Integer>();
47+
source.init();
48+
source.put(new Integer(5), new Integer(6));
2049

21-
TestMap testMap = ctx.getBean(TestMap.class);
22-
TestMapConsumer testMapConsumer = ctx.getBean(TestMapConsumer.class);
50+
TestMapConsumer target = new TestMapConsumer();
2351

24-
assertEquals(0, testMap.size());
25-
assertEquals(0, testMapConsumer.getTestMap().size());
52+
WebDataBinder dataBinder = new WebDataBinder(target, "");
53+
dataBinder.setConversionService(new DefaultFormattingConversionService());
54+
55+
MutablePropertyValues pvs = new MutablePropertyValues();
56+
pvs.addPropertyValue("testNumberMap", source);
57+
dataBinder.bind(pvs);
58+
59+
// NumberToNumber converter for key-value pairs
60+
61+
assertSame(source.keySet().iterator().next(), target.getTestNumberMap().keySet().iterator().next());
62+
assertSame(source.values().iterator().next(), target.getTestNumberMap().values().iterator().next());
63+
64+
assertTrue(target.getTestNumberMap().isInitialized());
2665
}
2766

67+
@Test
68+
public void integerToInteger() {
69+
70+
TestMap<Integer, Integer> source = new TestMap<Integer, Integer>();
71+
source.init();
72+
source.put(new Integer(5), new Integer(6));
73+
74+
TestMapConsumer target = new TestMapConsumer();
75+
76+
WebDataBinder dataBinder = new WebDataBinder(target, "");
77+
dataBinder.setConversionService(new DefaultFormattingConversionService());
78+
79+
MutablePropertyValues pvs = new MutablePropertyValues();
80+
pvs.addPropertyValue("testIntegerMap", source);
81+
dataBinder.bind(pvs);
82+
83+
// NumberToNumber converter for key-value pairs
84+
85+
assertSame(source.keySet().iterator().next(), target.getTestIntegerMap().keySet().iterator().next());
86+
assertSame(source.values().iterator().next(), target.getTestIntegerMap().values().iterator().next());
87+
88+
assertTrue(target.getTestIntegerMap().isInitialized());
89+
}
90+
91+
@Test
92+
public void assignableKeyValueParis() {
93+
94+
TestMap<TestBean, TestBean> source = new TestMap<TestBean, TestBean>();
95+
source.init();
96+
source.put(new TestBean(), new TestBean());
97+
98+
TestMapConsumer target = new TestMapConsumer();
99+
100+
WebDataBinder dataBinder = new WebDataBinder(target, "");
101+
dataBinder.setConversionService(new DefaultFormattingConversionService());
102+
103+
MutablePropertyValues pvs = new MutablePropertyValues();
104+
pvs.addPropertyValue("testBeanMap", source);
105+
dataBinder.bind(pvs);
106+
107+
// NO_OP converter for key-value pairs (no converter
108+
109+
assertSame(source.keySet().iterator().next(), target.getTestBeanMap().keySet().iterator().next());
110+
assertSame(source.values().iterator().next(), target.getTestBeanMap().values().iterator().next());
111+
112+
assertTrue(target.getTestBeanMap().isInitialized());
113+
}
114+
28115
}

SPR-8714/src/test/resources/org/springframework/issues/ReproTests-context.xml

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)