Skip to content

Commit 8f12d98

Browse files
jhoellerunknown
authored andcommitted
LinkedCaseInsensitiveMap checks for uniqueness of case-insensitive keys now
Issue: SPR-9754
1 parent 82739dd commit 8f12d98

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -88,7 +88,10 @@ public LinkedCaseInsensitiveMap(int initialCapacity, Locale locale) {
8888

8989
@Override
9090
public V put(String key, V value) {
91-
this.caseInsensitiveKeys.put(convertKey(key), key);
91+
String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key);
92+
if (oldKey != null && !oldKey.equals(key)) {
93+
super.remove(oldKey);
94+
}
9295
return super.put(key, value);
9396
}
9497

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util;
18+
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
22+
import static org.junit.Assert.*;
23+
24+
/**
25+
* @author Juergen Hoeller
26+
*/
27+
public class LinkedCaseInsensitiveMapTests {
28+
29+
private LinkedCaseInsensitiveMap<String> map;
30+
31+
@Before
32+
public void setUp() {
33+
map = new LinkedCaseInsensitiveMap<String>();
34+
}
35+
36+
@Test
37+
public void putAndGet() {
38+
map.put("key", "value1");
39+
map.put("key", "value2");
40+
map.put("key", "value3");
41+
assertEquals(1, map.size());
42+
assertEquals("value3", map.get("key"));
43+
assertEquals("value3", map.get("KEY"));
44+
assertEquals("value3", map.get("Key"));
45+
}
46+
47+
@Test
48+
public void putWithOverlappingKeys() {
49+
map.put("key", "value1");
50+
map.put("KEY", "value2");
51+
map.put("Key", "value3");
52+
assertEquals(1, map.size());
53+
assertEquals("value3", map.get("key"));
54+
assertEquals("value3", map.get("KEY"));
55+
assertEquals("value3", map.get("Key"));
56+
}
57+
58+
}

0 commit comments

Comments
 (0)