Skip to content

Commit de8645b

Browse files
committed
Test for <util:map> with LinkedCaseInsensitiveMap and specified key/value types
Issue: SPR-10994 (cherry picked from commit 9243869)
1 parent c8b8dc5 commit de8645b

File tree

2 files changed

+76
-30
lines changed

2 files changed

+76
-30
lines changed

spring-beans/src/test/java/org/springframework/beans/factory/xml/UtilNamespaceHandlerTests.java

Lines changed: 71 additions & 30 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.
@@ -17,14 +17,15 @@
1717
package org.springframework.beans.factory.xml;
1818

1919
import java.lang.reflect.Proxy;
20+
import java.util.Arrays;
2021
import java.util.List;
2122
import java.util.Map;
2223
import java.util.Properties;
2324
import java.util.Set;
2425
import java.util.TreeMap;
25-
import java.util.Arrays;
2626

27-
import junit.framework.TestCase;
27+
import org.junit.Before;
28+
import org.junit.Test;
2829

2930
import org.springframework.beans.factory.config.FieldRetrievingFactoryBean;
3031
import org.springframework.beans.factory.config.PropertiesFactoryBean;
@@ -35,37 +36,45 @@
3536
import org.springframework.tests.beans.CollectingReaderEventListener;
3637
import org.springframework.tests.sample.beans.CustomEnum;
3738
import org.springframework.tests.sample.beans.TestBean;
39+
import org.springframework.util.LinkedCaseInsensitiveMap;
40+
41+
import static org.junit.Assert.*;
3842

3943
/**
4044
* @author Rob Harrop
4145
* @author Juergen Hoeller
4246
* @author Mark Fisher
4347
*/
44-
public class UtilNamespaceHandlerTests extends TestCase {
48+
public class UtilNamespaceHandlerTests {
4549

4650
private DefaultListableBeanFactory beanFactory;
4751

4852
private CollectingReaderEventListener listener = new CollectingReaderEventListener();
4953

50-
@Override
54+
55+
@Before
5156
public void setUp() {
5257
this.beanFactory = new DefaultListableBeanFactory();
5358
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
5459
reader.setEventListener(this.listener);
5560
reader.loadBeanDefinitions(new ClassPathResource("testUtilNamespace.xml", getClass()));
5661
}
5762

58-
public void testConstant() throws Exception {
63+
64+
@Test
65+
public void testConstant() {
5966
Integer min = (Integer) this.beanFactory.getBean("min");
6067
assertEquals(Integer.MIN_VALUE, min.intValue());
6168
}
6269

63-
public void testConstantWithDefaultName() throws Exception {
70+
@Test
71+
public void testConstantWithDefaultName() {
6472
Integer max = (Integer) this.beanFactory.getBean("java.lang.Integer.MAX_VALUE");
6573
assertEquals(Integer.MAX_VALUE, max.intValue());
6674
}
6775

68-
public void testEvents() throws Exception {
76+
@Test
77+
public void testEvents() {
6978
ComponentDefinition propertiesComponent = this.listener.getComponentDefinition("myProperties");
7079
assertNotNull("Event for 'myProperties' not sent", propertiesComponent);
7180
AbstractBeanDefinition propertiesBean = (AbstractBeanDefinition) propertiesComponent.getBeanDefinitions()[0];
@@ -77,74 +86,92 @@ public void testEvents() throws Exception {
7786
assertEquals("Incorrect BeanDefinition", FieldRetrievingFactoryBean.class, constantBean.getBeanClass());
7887
}
7988

80-
public void testNestedProperties() throws Exception {
89+
@Test
90+
public void testNestedProperties() {
8191
TestBean bean = (TestBean) this.beanFactory.getBean("testBean");
8292
Properties props = bean.getSomeProperties();
8393
assertEquals("Incorrect property value", "bar", props.get("foo"));
8494
}
8595

86-
public void testPropertyPath() throws Exception {
96+
@Test
97+
public void testPropertyPath() {
8798
String name = (String) this.beanFactory.getBean("name");
8899
assertEquals("Rob Harrop", name);
89100
}
90101

91-
public void testNestedPropertyPath() throws Exception {
102+
@Test
103+
public void testNestedPropertyPath() {
92104
TestBean bean = (TestBean) this.beanFactory.getBean("testBean");
93105
assertEquals("Rob Harrop", bean.getName());
94106
}
95107

96-
public void testSimpleMap() throws Exception {
108+
@Test
109+
public void testSimpleMap() {
97110
Map map = (Map) this.beanFactory.getBean("simpleMap");
98111
assertEquals("bar", map.get("foo"));
99112
Map map2 = (Map) this.beanFactory.getBean("simpleMap");
100113
assertTrue(map == map2);
101114
}
102115

103-
public void testScopedMap() throws Exception {
116+
@Test
117+
public void testScopedMap() {
104118
Map map = (Map) this.beanFactory.getBean("scopedMap");
105119
assertEquals("bar", map.get("foo"));
106120
Map map2 = (Map) this.beanFactory.getBean("scopedMap");
107121
assertEquals("bar", map2.get("foo"));
108122
assertTrue(map != map2);
109123
}
110124

111-
public void testSimpleList() throws Exception {
125+
@Test
126+
public void testSimpleList() {
112127
List list = (List) this.beanFactory.getBean("simpleList");
113128
assertEquals("Rob Harrop", list.get(0));
114129
List list2 = (List) this.beanFactory.getBean("simpleList");
115130
assertTrue(list == list2);
116131
}
117132

118-
public void testScopedList() throws Exception {
133+
@Test
134+
public void testScopedList() {
119135
List list = (List) this.beanFactory.getBean("scopedList");
120136
assertEquals("Rob Harrop", list.get(0));
121137
List list2 = (List) this.beanFactory.getBean("scopedList");
122138
assertEquals("Rob Harrop", list2.get(0));
123139
assertTrue(list != list2);
124140
}
125141

126-
public void testSimpleSet() throws Exception {
142+
@Test
143+
public void testSimpleSet() {
127144
Set set = (Set) this.beanFactory.getBean("simpleSet");
128145
assertTrue(set.contains("Rob Harrop"));
129146
Set set2 = (Set) this.beanFactory.getBean("simpleSet");
130147
assertTrue(set == set2);
131148
}
132149

133-
public void testScopedSet() throws Exception {
150+
@Test
151+
public void testScopedSet() {
134152
Set set = (Set) this.beanFactory.getBean("scopedSet");
135153
assertTrue(set.contains("Rob Harrop"));
136154
Set set2 = (Set) this.beanFactory.getBean("scopedSet");
137155
assertTrue(set2.contains("Rob Harrop"));
138156
assertTrue(set != set2);
139157
}
140158

141-
public void testMapWithRef() throws Exception {
159+
@Test
160+
public void testMapWithRef() {
142161
Map map = (Map) this.beanFactory.getBean("mapWithRef");
143162
assertTrue(map instanceof TreeMap);
144163
assertEquals(this.beanFactory.getBean("testBean"), map.get("bean"));
145164
}
146165

147-
public void testNestedCollections() throws Exception {
166+
@Test
167+
public void testMapWithTypes() {
168+
Map map = (Map) this.beanFactory.getBean("mapWithTypes");
169+
assertTrue(map instanceof LinkedCaseInsensitiveMap);
170+
assertEquals(this.beanFactory.getBean("testBean"), map.get("bean"));
171+
}
172+
173+
@Test
174+
public void testNestedCollections() {
148175
TestBean bean = (TestBean) this.beanFactory.getBean("nestedCollectionsBean");
149176

150177
List list = bean.getSomeList();
@@ -171,7 +198,8 @@ public void testNestedCollections() throws Exception {
171198
assertFalse(map == bean2.getSomeMap());
172199
}
173200

174-
public void testNestedShortcutCollections() throws Exception {
201+
@Test
202+
public void testNestedShortcutCollections() {
175203
TestBean bean = (TestBean) this.beanFactory.getBean("nestedShortcutCollections");
176204

177205
assertEquals(1, bean.getStringArray().length);
@@ -194,7 +222,8 @@ public void testNestedShortcutCollections() throws Exception {
194222
assertFalse(set == bean2.getSomeSet());
195223
}
196224

197-
public void testNestedInCollections() throws Exception {
225+
@Test
226+
public void testNestedInCollections() {
198227
TestBean bean = (TestBean) this.beanFactory.getBean("nestedCustomTagBean");
199228

200229
List list = bean.getSomeList();
@@ -219,7 +248,8 @@ public void testNestedInCollections() throws Exception {
219248
assertFalse(map == bean2.getSomeMap());
220249
}
221250

222-
public void testCircularCollections() throws Exception {
251+
@Test
252+
public void testCircularCollections() {
223253
TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionsBean");
224254

225255
List list = bean.getSomeList();
@@ -235,7 +265,8 @@ public void testCircularCollections() throws Exception {
235265
assertEquals(bean, map.get("foo"));
236266
}
237267

238-
public void testCircularCollectionBeansStartingWithList() throws Exception {
268+
@Test
269+
public void testCircularCollectionBeansStartingWithList() {
239270
this.beanFactory.getBean("circularList");
240271
TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean");
241272

@@ -255,7 +286,8 @@ public void testCircularCollectionBeansStartingWithList() throws Exception {
255286
assertEquals(bean, map.get("foo"));
256287
}
257288

258-
public void testCircularCollectionBeansStartingWithSet() throws Exception {
289+
@Test
290+
public void testCircularCollectionBeansStartingWithSet() {
259291
this.beanFactory.getBean("circularSet");
260292
TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean");
261293

@@ -275,7 +307,8 @@ public void testCircularCollectionBeansStartingWithSet() throws Exception {
275307
assertEquals(bean, map.get("foo"));
276308
}
277309

278-
public void testCircularCollectionBeansStartingWithMap() throws Exception {
310+
@Test
311+
public void testCircularCollectionBeansStartingWithMap() {
279312
this.beanFactory.getBean("circularMap");
280313
TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean");
281314

@@ -295,20 +328,23 @@ public void testCircularCollectionBeansStartingWithMap() throws Exception {
295328
assertEquals(bean, map.get("foo"));
296329
}
297330

298-
public void testNestedInConstructor() throws Exception {
331+
@Test
332+
public void testNestedInConstructor() {
299333
TestBean bean = (TestBean) this.beanFactory.getBean("constructedTestBean");
300334
assertEquals("Rob Harrop", bean.getName());
301335
}
302336

303-
public void testLoadProperties() throws Exception {
337+
@Test
338+
public void testLoadProperties() {
304339
Properties props = (Properties) this.beanFactory.getBean("myProperties");
305340
assertEquals("Incorrect property value", "bar", props.get("foo"));
306341
assertEquals("Incorrect property value", null, props.get("foo2"));
307342
Properties props2 = (Properties) this.beanFactory.getBean("myProperties");
308343
assertTrue(props == props2);
309344
}
310345

311-
public void testScopedProperties() throws Exception {
346+
@Test
347+
public void testScopedProperties() {
312348
Properties props = (Properties) this.beanFactory.getBean("myScopedProperties");
313349
assertEquals("Incorrect property value", "bar", props.get("foo"));
314350
assertEquals("Incorrect property value", null, props.get("foo2"));
@@ -318,30 +354,35 @@ public void testScopedProperties() throws Exception {
318354
assertTrue(props != props2);
319355
}
320356

321-
public void testLocalProperties() throws Exception {
357+
@Test
358+
public void testLocalProperties() {
322359
Properties props = (Properties) this.beanFactory.getBean("myLocalProperties");
323360
assertEquals("Incorrect property value", null, props.get("foo"));
324361
assertEquals("Incorrect property value", "bar2", props.get("foo2"));
325362
}
326363

327-
public void testMergedProperties() throws Exception {
364+
@Test
365+
public void testMergedProperties() {
328366
Properties props = (Properties) this.beanFactory.getBean("myMergedProperties");
329367
assertEquals("Incorrect property value", "bar", props.get("foo"));
330368
assertEquals("Incorrect property value", "bar2", props.get("foo2"));
331369
}
332370

371+
@Test
333372
public void testLocalOverrideDefault() {
334373
Properties props = (Properties) this.beanFactory.getBean("defaultLocalOverrideProperties");
335374
assertEquals("Incorrect property value", "bar", props.get("foo"));
336375
assertEquals("Incorrect property value", "local2", props.get("foo2"));
337376
}
338377

378+
@Test
339379
public void testLocalOverrideFalse() {
340380
Properties props = (Properties) this.beanFactory.getBean("falseLocalOverrideProperties");
341381
assertEquals("Incorrect property value", "bar", props.get("foo"));
342382
assertEquals("Incorrect property value", "local2", props.get("foo2"));
343383
}
344384

385+
@Test
345386
public void testLocalOverrideTrue() {
346387
Properties props = (Properties) this.beanFactory.getBean("trueLocalOverrideProperties");
347388
assertEquals("Incorrect property value", "local", props.get("foo"));

spring-beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
<entry key="bean" value-ref="testBean"/>
5050
</util:map>
5151

52+
<util:map id="mapWithTypes" map-class="org.springframework.util.LinkedCaseInsensitiveMap"
53+
key-type="java.lang.String" value-type="org.springframework.tests.sample.beans.TestBean">
54+
<entry key="bean" value-ref="testBean"/>
55+
</util:map>
56+
5257
<util:list id="simpleList">
5358
<value>Rob Harrop</value>
5459
</util:list>

0 commit comments

Comments
 (0)