1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.cache;
17
18 import static org.junit.jupiter.api.Assertions.*;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.NotSerializableException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.util.Date;
26
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.Test;
29
30 class CacheKeyTest {
31
32 @Test
33 void shouldTestCacheKeysEqual() {
34 Date date = new Date();
35 CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null, new Date(date.getTime()) });
36 CacheKey key2 = new CacheKey(new Object[] { 1, "hello", null, new Date(date.getTime()) });
37 assertEquals(key1, key2);
38 assertEquals(key2, key1);
39 assertEquals(key1.hashCode(), key2.hashCode());
40 assertEquals(key1.toString(), key2.toString());
41 }
42
43 @Test
44 void shouldTestCacheKeysNotEqualDueToDateDifference() throws Exception {
45 CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null, new Date() });
46 Thread.sleep(1000);
47 CacheKey key2 = new CacheKey(new Object[] { 1, "hello", null, new Date() });
48 assertNotEquals(key1, key2);
49 assertNotEquals(key2, key1);
50 assertNotEquals(key1.hashCode(), key2.hashCode());
51 assertNotEquals(key1.toString(), key2.toString());
52 }
53
54 @Test
55 void shouldTestCacheKeysNotEqualDueToOrder() throws Exception {
56 CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null });
57 Thread.sleep(1000);
58 CacheKey key2 = new CacheKey(new Object[] { 1, null, "hello" });
59 assertNotEquals(key1, key2);
60 assertNotEquals(key2, key1);
61 assertNotEquals(key1.hashCode(), key2.hashCode());
62 assertNotEquals(key1.toString(), key2.toString());
63 }
64
65 @Test
66 void shouldDemonstrateEmptyAndNullKeysAreEqual() {
67 CacheKey key1 = new CacheKey();
68 CacheKey key2 = new CacheKey();
69 assertEquals(key1, key2);
70 assertEquals(key2, key1);
71 key1.update(null);
72 key2.update(null);
73 assertEquals(key1, key2);
74 assertEquals(key2, key1);
75 key1.update(null);
76 key2.update(null);
77 assertEquals(key1, key2);
78 assertEquals(key2, key1);
79 }
80
81 @Test
82 void shouldTestCacheKeysWithBinaryArrays() {
83 byte[] array1 = new byte[] { 1 };
84 byte[] array2 = new byte[] { 1 };
85 CacheKey key1 = new CacheKey(new Object[] { array1 });
86 CacheKey key2 = new CacheKey(new Object[] { array2 });
87 assertEquals(key1, key2);
88 }
89
90 @Test
91 void serializationExceptionTest() {
92 CacheKey cacheKey = new CacheKey();
93 cacheKey.update(new Object());
94 Assertions.assertThrows(NotSerializableException.class, () -> {
95 serialize(cacheKey);
96 });
97 }
98
99 @Test
100 void serializationTest() throws Exception {
101 CacheKey cacheKey = new CacheKey();
102 cacheKey.update("serializable");
103 Assertions.assertEquals(cacheKey, serialize(cacheKey));
104 }
105
106 private static <T> T serialize(T object) throws Exception {
107 ByteArrayOutputStream baos = new ByteArrayOutputStream();
108 new ObjectOutputStream(baos).writeObject(object);
109
110 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
111 return (T) new ObjectInputStream(bais).readObject();
112 }
113
114 }