Skip to content
Browse files

Add test cases to SPR-8714

  • Loading branch information...
1 parent bd086b0 commit cad5732de79da8436a0223b7c00aa83d7f43845d @rstoyanchev rstoyanchev committed
View
2 SPR-8714/pom.xml
@@ -8,7 +8,7 @@
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
+ <artifactId>spring-webmvc</artifactId>
<version>3.1.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
View
5 SPR-8714/src/main/java/org/springframework/issues/TestBean.java
@@ -0,0 +1,5 @@
+package org.springframework.issues;
+
+public class TestBean {
+
+}
View
18 SPR-8714/src/main/java/org/springframework/issues/TestMap.java
@@ -16,13 +16,11 @@
package org.springframework.issues;
-import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
-import java.util.Set;
/**
- * See
+ * Original source
* https://github.com/rgladwell/spring-map-converter-issue/blob/master/src/main/java/uk/co/bbc/spring/issue/TestMap.java
*/
@SuppressWarnings("serial")
@@ -34,18 +32,8 @@ public void init() {
init = true;
}
- @Override
- @SuppressWarnings("unchecked")
- public Set<java.util.Map.Entry<K, V>> entrySet() {
- return Collections.EMPTY_SET;
- }
-
- @Override
- public int size() {
- if (!init) {
- throw new IllegalStateException("map not initialised");
- }
- return super.size();
+ public boolean isInitialized() {
+ return this.init;
}
}
View
41 SPR-8714/src/main/java/org/springframework/issues/TestMapConsumer.java
@@ -1,7 +1,7 @@
/*
* Copyright 2002-2011 the original author or authors.
*
- * Licensed under the Apache License, Version 2.0 (the "License");
+ * Licensed under the Apache License, Stringersion 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
@@ -9,7 +9,7 @@
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY StringIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@@ -22,14 +22,45 @@
*/
public class TestMapConsumer {
- private TestMap<String, String> testMap;
+ private TestMap<String,String> testMap;
- public TestMap<String, String> getTestMap() {
+ private TestMap<Number, Number> testNumberMap;
+
+ private TestMap<Integer, Integer> testIntegerMap;
+
+ private TestMap<TestBean, TestBean> testBeanMap;
+
+ public TestMap<String,String> getTestMap() {
return testMap;
}
- public void setTestMap(TestMap<String, String> testMap) {
+ public void setTestMap(TestMap<String,String> testMap) {
this.testMap = testMap;
}
+ public TestMap<TestBean, TestBean> getTestBeanMap() {
+ return testBeanMap;
+ }
+
+ public void setTestBeanMap(TestMap<TestBean, TestBean> testBeanMap) {
+ this.testBeanMap = testBeanMap;
+ }
+
+ public TestMap<Number, Number> getTestNumberMap() {
+ return testNumberMap;
+ }
+
+ public void setTestNumberMap(TestMap<Number, Number> testNumberMap) {
+ this.testNumberMap = testNumberMap;
+ }
+
+ public TestMap<Integer, Integer> getTestIntegerMap() {
+ return testIntegerMap;
+ }
+
+ public void setTestIntegerMap(TestMap<Integer, Integer> testIntegerMap) {
+ this.testIntegerMap = testIntegerMap;
+ }
+
+
}
View
31 SPR-8714/src/main/java/org/springframework/issues/TestMapFactory.java
@@ -1,31 +0,0 @@
-/*
- * Copyright 2002-2011 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.issues;
-
-/**
- * See
- * https://github.com/rgladwell/spring-map-converter-issue/blob/master/src/main/java/uk/co/bbc/spring/issue/TestMapFactory.java
- */
-public class TestMapFactory<K, V> {
-
- TestMap<K, V> createTestMap() {
- TestMap<K, V> map = new TestMap<K, V>();
- map.init();
- return map;
- }
-
-}
View
107 SPR-8714/src/test/java/org/springframework/issues/ReproTests.java
@@ -1,9 +1,12 @@
package org.springframework.issues;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
import org.junit.Test;
-import org.springframework.context.support.GenericXmlApplicationContext;
+import org.springframework.beans.MutablePropertyValues;
+import org.springframework.format.support.DefaultFormattingConversionService;
+import org.springframework.web.bind.WebDataBinder;
/**
* Unit test that reproduces an issue reported against SPR JIRA. @Test methods within
@@ -11,18 +14,102 @@
* demonstrates the reported issue.
*/
public class ReproTests {
+
+ @Test
+ public void stringToString() {
+
+ TestMap<String, String> source = new TestMap<String, String>();
+ source.init();
+ source.put("key", "value");
+ source.put("key", null);
+
+ TestMapConsumer target = new TestMapConsumer();
+
+ WebDataBinder dataBinder = new WebDataBinder(target, "");
+ dataBinder.setConversionService(new DefaultFormattingConversionService());
+
+ MutablePropertyValues pvs = new MutablePropertyValues();
+ pvs.addPropertyValue("testMap", source);
+ dataBinder.bind(pvs);
+
+ // FallbackObjectToStringConverter for key-value pairs
+
+ assertSame(source.keySet().iterator().next(), target.getTestMap().keySet().iterator().next());
+ assertSame(source.values().iterator().next(), target.getTestMap().values().iterator().next());
+
+ assertTrue(target.getTestMap().isInitialized());
+ }
@Test
- public void repro() {
- GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
- ctx.load("classpath:org/springframework/issues/ReproTests-context.xml");
- ctx.refresh();
+ public void numberToNumber() {
+
+ TestMap<Integer, Integer> source = new TestMap<Integer, Integer>();
+ source.init();
+ source.put(new Integer(5), new Integer(6));
- TestMap testMap = ctx.getBean(TestMap.class);
- TestMapConsumer testMapConsumer = ctx.getBean(TestMapConsumer.class);
+ TestMapConsumer target = new TestMapConsumer();
- assertEquals(0, testMap.size());
- assertEquals(0, testMapConsumer.getTestMap().size());
+ WebDataBinder dataBinder = new WebDataBinder(target, "");
+ dataBinder.setConversionService(new DefaultFormattingConversionService());
+
+ MutablePropertyValues pvs = new MutablePropertyValues();
+ pvs.addPropertyValue("testNumberMap", source);
+ dataBinder.bind(pvs);
+
+ // NumberToNumber converter for key-value pairs
+
+ assertSame(source.keySet().iterator().next(), target.getTestNumberMap().keySet().iterator().next());
+ assertSame(source.values().iterator().next(), target.getTestNumberMap().values().iterator().next());
+
+ assertTrue(target.getTestNumberMap().isInitialized());
}
+ @Test
+ public void integerToInteger() {
+
+ TestMap<Integer, Integer> source = new TestMap<Integer, Integer>();
+ source.init();
+ source.put(new Integer(5), new Integer(6));
+
+ TestMapConsumer target = new TestMapConsumer();
+
+ WebDataBinder dataBinder = new WebDataBinder(target, "");
+ dataBinder.setConversionService(new DefaultFormattingConversionService());
+
+ MutablePropertyValues pvs = new MutablePropertyValues();
+ pvs.addPropertyValue("testIntegerMap", source);
+ dataBinder.bind(pvs);
+
+ // NumberToNumber converter for key-value pairs
+
+ assertSame(source.keySet().iterator().next(), target.getTestIntegerMap().keySet().iterator().next());
+ assertSame(source.values().iterator().next(), target.getTestIntegerMap().values().iterator().next());
+
+ assertTrue(target.getTestIntegerMap().isInitialized());
+ }
+
+ @Test
+ public void assignableKeyValueParis() {
+
+ TestMap<TestBean, TestBean> source = new TestMap<TestBean, TestBean>();
+ source.init();
+ source.put(new TestBean(), new TestBean());
+
+ TestMapConsumer target = new TestMapConsumer();
+
+ WebDataBinder dataBinder = new WebDataBinder(target, "");
+ dataBinder.setConversionService(new DefaultFormattingConversionService());
+
+ MutablePropertyValues pvs = new MutablePropertyValues();
+ pvs.addPropertyValue("testBeanMap", source);
+ dataBinder.bind(pvs);
+
+ // NO_OP converter for key-value pairs (no converter
+
+ assertSame(source.keySet().iterator().next(), target.getTestBeanMap().keySet().iterator().next());
+ assertSame(source.values().iterator().next(), target.getTestBeanMap().values().iterator().next());
+
+ assertTrue(target.getTestBeanMap().isInitialized());
+ }
+
}
View
16 SPR-8714/src/test/resources/org/springframework/issues/ReproTests-context.xml
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="ISO-8859-1"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
-
- <bean id="testMapFactory" class="org.springframework.issues.TestMapFactory" />
-
- <bean id="testMap" factory-bean="testMapFactory" factory-method="createTestMap"/>
-
- <bean id="testMapConsumer" class="org.springframework.issues.TestMapConsumer">
- <property name="testMap" ref="testMap"/>
- </bean>
-
-</beans>

0 comments on commit cad5732

Please sign in to comment.
Something went wrong with that request. Please try again.