Skip to content

Commit e8562bb

Browse files
committed
LinkedCaseInsensitiveMap properly overrides HashMap.clone()
Issue: SPR-14509 (cherry picked from commit dadd2c3)
1 parent a4b5425 commit e8562bb

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -36,7 +36,7 @@
3636
@SuppressWarnings("serial")
3737
public class LinkedCaseInsensitiveMap<V> extends LinkedHashMap<String, V> {
3838

39-
private final Map<String, String> caseInsensitiveKeys;
39+
private Map<String, String> caseInsensitiveKeys;
4040

4141
private final Locale locale;
4242

@@ -114,21 +114,23 @@ public boolean containsKey(Object key) {
114114
@Override
115115
public V get(Object key) {
116116
if (key instanceof String) {
117-
return super.get(this.caseInsensitiveKeys.get(convertKey((String) key)));
118-
}
119-
else {
120-
return null;
117+
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
118+
if (caseInsensitiveKey != null) {
119+
return super.get(caseInsensitiveKey);
120+
}
121121
}
122+
return null;
122123
}
123124

124125
@Override
125126
public V remove(Object key) {
126-
if (key instanceof String ) {
127-
return super.remove(this.caseInsensitiveKeys.remove(convertKey((String) key)));
128-
}
129-
else {
130-
return null;
127+
if (key instanceof String) {
128+
String caseInsensitiveKey = this.caseInsensitiveKeys.remove(convertKey((String) key));
129+
if (caseInsensitiveKey != null) {
130+
return super.remove(caseInsensitiveKey);
131+
}
131132
}
133+
return null;
132134
}
133135

134136
@Override
@@ -137,6 +139,14 @@ public void clear() {
137139
super.clear();
138140
}
139141

142+
@Override
143+
@SuppressWarnings("unchecked")
144+
public Object clone() {
145+
LinkedCaseInsensitiveMap<V> copy = (LinkedCaseInsensitiveMap<V>) super.clone();
146+
copy.caseInsensitiveKeys = new HashMap<String, String>(this.caseInsensitiveKeys);
147+
return copy;
148+
}
149+
140150

141151
/**
142152
* Convert the given key to a case-insensitive key.

spring-core/src/test/java/org/springframework/util/LinkedCaseInsensitiveMapTests.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.util;
1818

19-
import org.junit.Before;
2019
import org.junit.Test;
2120

2221
import static org.junit.Assert.*;
@@ -26,12 +25,8 @@
2625
*/
2726
public class LinkedCaseInsensitiveMapTests {
2827

29-
private LinkedCaseInsensitiveMap<String> map;
28+
private final LinkedCaseInsensitiveMap<String> map = new LinkedCaseInsensitiveMap<String>();
3029

31-
@Before
32-
public void setUp() {
33-
map = new LinkedCaseInsensitiveMap<String>();
34-
}
3530

3631
@Test
3732
public void putAndGet() {
@@ -55,4 +50,26 @@ public void putWithOverlappingKeys() {
5550
assertEquals("value3", map.get("Key"));
5651
}
5752

53+
@Test
54+
@SuppressWarnings("unchecked")
55+
public void mapClone() {
56+
map.put("key", "value1");
57+
LinkedCaseInsensitiveMap<String> copy = (LinkedCaseInsensitiveMap<String>) map.clone();
58+
assertEquals("value1", map.get("key"));
59+
assertEquals("value1", map.get("KEY"));
60+
assertEquals("value1", map.get("Key"));
61+
assertEquals("value1", copy.get("key"));
62+
assertEquals("value1", copy.get("KEY"));
63+
assertEquals("value1", copy.get("Key"));
64+
copy.put("Key", "value2");
65+
assertEquals(1, map.size());
66+
assertEquals(1, copy.size());
67+
assertEquals("value1", map.get("key"));
68+
assertEquals("value1", map.get("KEY"));
69+
assertEquals("value1", map.get("Key"));
70+
assertEquals("value2", copy.get("key"));
71+
assertEquals("value2", copy.get("KEY"));
72+
assertEquals("value2", copy.get("Key"));
73+
}
74+
5875
}

0 commit comments

Comments
 (0)