|
1 | 1 | package org.springframework.issues; |
2 | 2 |
|
3 | | -import static org.junit.Assert.*; |
| 3 | +import static org.junit.Assert.assertSame; |
| 4 | +import static org.junit.Assert.assertTrue; |
4 | 5 |
|
5 | 6 | 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; |
7 | 10 |
|
8 | 11 | /** |
9 | 12 | * Unit test that reproduces an issue reported against SPR JIRA. @Test methods within |
10 | 13 | * need not pass with the green bar! Rather they should fail in such a way that |
11 | 14 | * demonstrates the reported issue. |
12 | 15 | */ |
13 | 16 | 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 | + } |
14 | 42 |
|
15 | 43 | @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)); |
20 | 49 |
|
21 | | - TestMap testMap = ctx.getBean(TestMap.class); |
22 | | - TestMapConsumer testMapConsumer = ctx.getBean(TestMapConsumer.class); |
| 50 | + TestMapConsumer target = new TestMapConsumer(); |
23 | 51 |
|
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()); |
26 | 65 | } |
27 | 66 |
|
| 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 | + |
28 | 115 | } |
0 commit comments