Skip to content

Commit 6d8f3a0

Browse files
candrewssnicoll
authored andcommitted
Fix SimpleKey equality with array argument
Prior to this commit, an array argument was not handled properly in SimpleKey#equals and SimpleKey#hashCode. As a result, two method invocations with the same array argument lead to two different keys and therefore two different entries in the cache. This commit uses deepEquals and deepHashCode to properly handle methods that have arguments that are array types. Issue: SPR-11505
1 parent a60b34f commit 6d8f3a0

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

spring-context/src/main/java/org/springframework/cache/interceptor/SimpleKey.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ public SimpleKey(Object... elements) {
5050

5151
@Override
5252
public boolean equals(Object obj) {
53-
return (this == obj || (obj instanceof SimpleKey && Arrays.equals(this.params, ((SimpleKey) obj).params)));
53+
return (this == obj || (obj instanceof SimpleKey
54+
&& Arrays.deepEquals(this.params, ((SimpleKey) obj).params)));
5455
}
5556

5657
@Override
5758
public int hashCode() {
58-
return Arrays.hashCode(this.params);
59+
return Arrays.deepHashCode(this.params);
5960
}
6061

6162
@Override

spring-context/src/test/java/org/springframework/cache/interceptor/SimpleKeyGeneratorTests.java

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,28 @@
2525
* Tests for {@link SimpleKeyGenerator} and {@link SimpleKey}.
2626
*
2727
* @author Phillip Webb
28+
* @author Stephane Nicoll
2829
*/
2930
public class SimpleKeyGeneratorTests {
3031

3132
private SimpleKeyGenerator generator = new SimpleKeyGenerator();
3233

3334
@Test
34-
public void noValues() throws Exception {
35-
Object k1 = generator.generate(null, null, new Object[] {});
36-
Object k2 = generator.generate(null, null, new Object[] {});
37-
Object k3 = generator.generate(null, null, new Object[] { "different" });
35+
public void noValues() {
36+
Object k1 = generateKey(new Object[] {});
37+
Object k2 = generateKey(new Object[] {});
38+
Object k3 = generateKey(new Object[] { "different" });
3839
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
3940
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
4041
assertThat(k1, equalTo(k2));
4142
assertThat(k1, not(equalTo(k3)));
4243
}
4344

4445
@Test
45-
public void singleValue() throws Exception {
46-
Object k1 = generator.generate(null, null, new Object[] { "a" });
47-
Object k2 = generator.generate(null, null, new Object[] { "a" });
48-
Object k3 = generator.generate(null, null, new Object[] { "different" });
46+
public void singleValue(){
47+
Object k1 = generateKey(new Object[] { "a" });
48+
Object k2 = generateKey(new Object[] { "a" });
49+
Object k3 = generateKey(new Object[] { "different" });
4950
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
5051
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
5152
assertThat(k1, equalTo(k2));
@@ -54,21 +55,21 @@ public void singleValue() throws Exception {
5455
}
5556

5657
@Test
57-
public void multipleValues() throws Exception {
58-
Object k1 = generator.generate(null, null, new Object[] { "a", 1, "b" });
59-
Object k2 = generator.generate(null, null, new Object[] { "a", 1, "b" });
60-
Object k3 = generator.generate(null, null, new Object[] { "b", 1, "a" });
58+
public void multipleValues() {
59+
Object k1 = generateKey(new Object[] { "a", 1, "b" });
60+
Object k2 = generateKey(new Object[] { "a", 1, "b" });
61+
Object k3 = generateKey(new Object[] { "b", 1, "a" });
6162
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
6263
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
6364
assertThat(k1, equalTo(k2));
6465
assertThat(k1, not(equalTo(k3)));
6566
}
6667

6768
@Test
68-
public void singleNullValue() throws Exception {
69-
Object k1 = generator.generate(null, null, new Object[] { null });
70-
Object k2 = generator.generate(null, null, new Object[] { null });
71-
Object k3 = generator.generate(null, null, new Object[] { "different" });
69+
public void singleNullValue() {
70+
Object k1 = generateKey(new Object[] { null });
71+
Object k2 = generateKey(new Object[] { null });
72+
Object k3 = generateKey(new Object[] { "different" });
7273
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
7374
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
7475
assertThat(k1, equalTo(k2));
@@ -77,13 +78,28 @@ public void singleNullValue() throws Exception {
7778
}
7879

7980
@Test
80-
public void multiplNullValues() throws Exception {
81-
Object k1 = generator.generate(null, null, new Object[] { "a", null, "b", null });
82-
Object k2 = generator.generate(null, null, new Object[] { "a", null, "b", null });
83-
Object k3 = generator.generate(null, null, new Object[] { "a", null, "b" });
81+
public void multipleNullValues() {
82+
Object k1 = generateKey(new Object[] { "a", null, "b", null });
83+
Object k2 = generateKey(new Object[] { "a", null, "b", null });
84+
Object k3 = generateKey(new Object[] { "a", null, "b" });
8485
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
8586
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
8687
assertThat(k1, equalTo(k2));
8788
assertThat(k1, not(equalTo(k3)));
8889
}
90+
91+
@Test
92+
public void arrays() {
93+
Object k1 = generateKey(new Object[] { new String[]{"a", "b"}, "c" });
94+
Object k2 = generateKey(new Object[] { new String[]{"a", "b"}, "c" });
95+
Object k3 = generateKey(new Object[] { new String[]{"b", "a"}, "c" });
96+
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
97+
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
98+
assertThat(k1, equalTo(k2));
99+
assertThat(k1, not(equalTo(k3)));
100+
}
101+
102+
private Object generateKey(Object[] arguments) {
103+
return generator.generate(null, null, arguments);
104+
}
89105
}

0 commit comments

Comments
 (0)