Skip to content

Commit 9b61bfb

Browse files
committed
Tentative fix for #124 (and #125?)
1 parent 7552bb7 commit 9b61bfb

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

src/main/java/org/apache/ibatis/cache/CacheKey.java

+28-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2012 the original author or authors.
2+
* Copyright 2009-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.
@@ -16,6 +16,7 @@
1616
package org.apache.ibatis.cache;
1717

1818
import java.io.Serializable;
19+
import java.lang.reflect.Array;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122

@@ -51,6 +52,18 @@ public int getUpdateCount() {
5152
}
5253

5354
public void update(Object object) {
55+
if (object != null && object.getClass().isArray()) {
56+
int length = Array.getLength(object);
57+
for (int i = 0; i < length; i++) {
58+
Object element = Array.get(object, i);
59+
doUpdate(element);
60+
}
61+
} else {
62+
doUpdate(object);
63+
}
64+
}
65+
66+
private void doUpdate(Object object) {
5467
int baseHashCode = object == null ? 1 : object.hashCode();
5568

5669
count++;
@@ -69,22 +82,29 @@ public void updateAll(Object[] objects) {
6982
}
7083

7184
public boolean equals(Object object) {
72-
if (this == object) return true;
73-
if (!(object instanceof CacheKey)) return false;
85+
if (this == object)
86+
return true;
87+
if (!(object instanceof CacheKey))
88+
return false;
7489

7590
final CacheKey cacheKey = (CacheKey) object;
7691

77-
if (hashcode != cacheKey.hashcode) return false;
78-
if (checksum != cacheKey.checksum) return false;
79-
if (count != cacheKey.count) return false;
92+
if (hashcode != cacheKey.hashcode)
93+
return false;
94+
if (checksum != cacheKey.checksum)
95+
return false;
96+
if (count != cacheKey.count)
97+
return false;
8098

8199
for (int i = 0; i < updateList.size(); i++) {
82100
Object thisObject = updateList.get(i);
83101
Object thatObject = cacheKey.updateList.get(i);
84102
if (thisObject == null) {
85-
if (thatObject != null) return false;
103+
if (thatObject != null)
104+
return false;
86105
} else {
87-
if (!thisObject.equals(thatObject)) return false;
106+
if (!thisObject.equals(thatObject))
107+
return false;
88108
}
89109
}
90110
return true;

src/test/java/org/apache/ibatis/cache/CacheKeyTest.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public class CacheKeyTest {
2525
@Test
2626
public void shouldTestCacheKeysEqual() {
2727
Date date = new Date();
28-
CacheKey key1 = new CacheKey(new Object[]{1, "hello", null, new Date(date.getTime())});
29-
CacheKey key2 = new CacheKey(new Object[]{1, "hello", null, new Date(date.getTime())});
28+
CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null, new Date(date.getTime()) });
29+
CacheKey key2 = new CacheKey(new Object[] { 1, "hello", null, new Date(date.getTime()) });
3030
assertTrue(key1.equals(key2));
3131
assertTrue(key2.equals(key1));
3232
assertTrue(key1.hashCode() == key2.hashCode());
@@ -35,9 +35,9 @@ public void shouldTestCacheKeysEqual() {
3535

3636
@Test
3737
public void shouldTestCacheKeysNotEqualDueToDateDifference() throws Exception {
38-
CacheKey key1 = new CacheKey(new Object[]{1, "hello", null, new Date()});
38+
CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null, new Date() });
3939
Thread.sleep(1000);
40-
CacheKey key2 = new CacheKey(new Object[]{1, "hello", null, new Date()});
40+
CacheKey key2 = new CacheKey(new Object[] { 1, "hello", null, new Date() });
4141
assertFalse(key1.equals(key2));
4242
assertFalse(key2.equals(key1));
4343
assertFalse(key1.hashCode() == key2.hashCode());
@@ -46,9 +46,9 @@ public void shouldTestCacheKeysNotEqualDueToDateDifference() throws Exception {
4646

4747
@Test
4848
public void shouldTestCacheKeysNotEqualDueToOrder() throws Exception {
49-
CacheKey key1 = new CacheKey(new Object[]{1, "hello", null});
49+
CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null });
5050
Thread.sleep(1000);
51-
CacheKey key2 = new CacheKey(new Object[]{1, null, "hello"});
51+
CacheKey key2 = new CacheKey(new Object[] { 1, null, "hello" });
5252
assertFalse(key1.equals(key2));
5353
assertFalse(key2.equals(key1));
5454
assertFalse(key1.hashCode() == key2.hashCode());
@@ -71,4 +71,13 @@ public void shouldDemonstrateEmptyAndNullKeysAreEqual() {
7171
assertEquals(key2, key1);
7272
}
7373

74+
@Test
75+
public void shouldTestCacheKeysWithBinaryArrays() throws Exception {
76+
byte[] array1 = new byte[] { 1 };
77+
byte[] array2 = new byte[] { 1 };
78+
CacheKey key1 = new CacheKey(new Object[] { array1 });
79+
CacheKey key2 = new CacheKey(new Object[] { array2 });
80+
assertTrue(key1.equals(key2));
81+
}
82+
7483
}

0 commit comments

Comments
 (0)