Browse Source

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
pull/480/head
Craig Andrews 11 years ago committed by Stephane Nicoll
parent
commit
6d8f3a0a20
  1. 5
      spring-context/src/main/java/org/springframework/cache/interceptor/SimpleKey.java
  2. 56
      spring-context/src/test/java/org/springframework/cache/interceptor/SimpleKeyGeneratorTests.java

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

@ -50,12 +50,13 @@ public final class SimpleKey implements Serializable { @@ -50,12 +50,13 @@ public final class SimpleKey implements Serializable {
@Override
public boolean equals(Object obj) {
return (this == obj || (obj instanceof SimpleKey && Arrays.equals(this.params, ((SimpleKey) obj).params)));
return (this == obj || (obj instanceof SimpleKey
&& Arrays.deepEquals(this.params, ((SimpleKey) obj).params)));
}
@Override
public int hashCode() {
return Arrays.hashCode(this.params);
return Arrays.deepHashCode(this.params);
}
@Override

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

@ -25,16 +25,17 @@ import static org.junit.Assert.*; @@ -25,16 +25,17 @@ import static org.junit.Assert.*;
* Tests for {@link SimpleKeyGenerator} and {@link SimpleKey}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
public class SimpleKeyGeneratorTests {
private SimpleKeyGenerator generator = new SimpleKeyGenerator();
@Test
public void noValues() throws Exception {
Object k1 = generator.generate(null, null, new Object[] {});
Object k2 = generator.generate(null, null, new Object[] {});
Object k3 = generator.generate(null, null, new Object[] { "different" });
public void noValues() {
Object k1 = generateKey(new Object[] {});
Object k2 = generateKey(new Object[] {});
Object k3 = generateKey(new Object[] { "different" });
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
assertThat(k1, equalTo(k2));
@ -42,10 +43,10 @@ public class SimpleKeyGeneratorTests { @@ -42,10 +43,10 @@ public class SimpleKeyGeneratorTests {
}
@Test
public void singleValue() throws Exception {
Object k1 = generator.generate(null, null, new Object[] { "a" });
Object k2 = generator.generate(null, null, new Object[] { "a" });
Object k3 = generator.generate(null, null, new Object[] { "different" });
public void singleValue(){
Object k1 = generateKey(new Object[] { "a" });
Object k2 = generateKey(new Object[] { "a" });
Object k3 = generateKey(new Object[] { "different" });
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
assertThat(k1, equalTo(k2));
@ -54,10 +55,10 @@ public class SimpleKeyGeneratorTests { @@ -54,10 +55,10 @@ public class SimpleKeyGeneratorTests {
}
@Test
public void multipleValues() throws Exception {
Object k1 = generator.generate(null, null, new Object[] { "a", 1, "b" });
Object k2 = generator.generate(null, null, new Object[] { "a", 1, "b" });
Object k3 = generator.generate(null, null, new Object[] { "b", 1, "a" });
public void multipleValues() {
Object k1 = generateKey(new Object[] { "a", 1, "b" });
Object k2 = generateKey(new Object[] { "a", 1, "b" });
Object k3 = generateKey(new Object[] { "b", 1, "a" });
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
assertThat(k1, equalTo(k2));
@ -65,10 +66,10 @@ public class SimpleKeyGeneratorTests { @@ -65,10 +66,10 @@ public class SimpleKeyGeneratorTests {
}
@Test
public void singleNullValue() throws Exception {
Object k1 = generator.generate(null, null, new Object[] { null });
Object k2 = generator.generate(null, null, new Object[] { null });
Object k3 = generator.generate(null, null, new Object[] { "different" });
public void singleNullValue() {
Object k1 = generateKey(new Object[] { null });
Object k2 = generateKey(new Object[] { null });
Object k3 = generateKey(new Object[] { "different" });
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
assertThat(k1, equalTo(k2));
@ -77,13 +78,28 @@ public class SimpleKeyGeneratorTests { @@ -77,13 +78,28 @@ public class SimpleKeyGeneratorTests {
}
@Test
public void multiplNullValues() throws Exception {
Object k1 = generator.generate(null, null, new Object[] { "a", null, "b", null });
Object k2 = generator.generate(null, null, new Object[] { "a", null, "b", null });
Object k3 = generator.generate(null, null, new Object[] { "a", null, "b" });
public void multipleNullValues() {
Object k1 = generateKey(new Object[] { "a", null, "b", null });
Object k2 = generateKey(new Object[] { "a", null, "b", null });
Object k3 = generateKey(new Object[] { "a", null, "b" });
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
assertThat(k1, equalTo(k2));
assertThat(k1, not(equalTo(k3)));
}
@Test
public void arrays() {
Object k1 = generateKey(new Object[] { new String[]{"a", "b"}, "c" });
Object k2 = generateKey(new Object[] { new String[]{"a", "b"}, "c" });
Object k3 = generateKey(new Object[] { new String[]{"b", "a"}, "c" });
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
assertThat(k1, equalTo(k2));
assertThat(k1, not(equalTo(k3)));
}
private Object generateKey(Object[] arguments) {
return generator.generate(null, null, arguments);
}
}

Loading…
Cancel
Save