View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.cache;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNotEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.NotSerializableException;
25  import java.io.ObjectInputStream;
26  import java.io.ObjectOutputStream;
27  import java.util.Date;
28  
29  import org.junit.jupiter.api.Test;
30  
31  class CacheKeyTest {
32  
33    @Test
34    void shouldTestCacheKeysEqual() {
35      Date date = new Date();
36      CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null, new Date(date.getTime()) });
37      CacheKey key2 = new CacheKey(new Object[] { 1, "hello", null, new Date(date.getTime()) });
38      assertEquals(key1, key2);
39      assertEquals(key2, key1);
40      assertEquals(key1.hashCode(), key2.hashCode());
41      assertEquals(key1.toString(), key2.toString());
42    }
43  
44    @Test
45    void shouldTestCacheKeysNotEqualDueToDateDifference() throws Exception {
46      CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null, new Date() });
47      Thread.sleep(1000);
48      CacheKey key2 = new CacheKey(new Object[] { 1, "hello", null, new Date() });
49      assertNotEquals(key1, key2);
50      assertNotEquals(key2, key1);
51      assertNotEquals(key1.hashCode(), key2.hashCode());
52      assertNotEquals(key1.toString(), key2.toString());
53    }
54  
55    @Test
56    void shouldTestCacheKeysNotEqualDueToOrder() throws Exception {
57      CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null });
58      Thread.sleep(1000);
59      CacheKey key2 = new CacheKey(new Object[] { 1, null, "hello" });
60      assertNotEquals(key1, key2);
61      assertNotEquals(key2, key1);
62      assertNotEquals(key1.hashCode(), key2.hashCode());
63      assertNotEquals(key1.toString(), key2.toString());
64    }
65  
66    @Test
67    void shouldDemonstrateEmptyAndNullKeysAreEqual() {
68      CacheKey key1 = new CacheKey();
69      CacheKey key2 = new CacheKey();
70      assertEquals(key1, key2);
71      assertEquals(key2, key1);
72      key1.update(null);
73      key2.update(null);
74      assertEquals(key1, key2);
75      assertEquals(key2, key1);
76      key1.update(null);
77      key2.update(null);
78      assertEquals(key1, key2);
79      assertEquals(key2, key1);
80    }
81  
82    @Test
83    void shouldTestCacheKeysWithBinaryArrays() {
84      byte[] array1 = { 1 };
85      byte[] array2 = { 1 };
86      CacheKey key1 = new CacheKey(new Object[] { array1 });
87      CacheKey key2 = new CacheKey(new Object[] { array2 });
88      assertEquals(key1, key2);
89    }
90  
91    @Test
92    void throwExceptionWhenTryingToUpdateNullCacheKey() {
93      CacheKey cacheKey = CacheKey.NULL_CACHE_KEY;
94      assertThrows(CacheException.class, () -> cacheKey.update("null"));
95    }
96  
97    @Test
98    void throwExceptionWhenTryingToUpdateAllNullCacheKey() {
99      CacheKey cacheKey = CacheKey.NULL_CACHE_KEY;
100     assertThrows(CacheException.class, () -> cacheKey.updateAll(new Object[] { "null", "null" }));
101   }
102 
103   @Test
104   void shouldDemonstrateClonedNullCacheKeysAreEqual() throws Exception {
105     CacheKey cacheKey = CacheKey.NULL_CACHE_KEY;
106     CacheKey clonedCacheKey = cacheKey.clone();
107     assertEquals(cacheKey, clonedCacheKey);
108     assertEquals(cacheKey.hashCode(), clonedCacheKey.hashCode());
109   }
110 
111   @Test
112   void serializationExceptionTest() {
113     CacheKey cacheKey = new CacheKey();
114     cacheKey.update(new Object());
115     assertThrows(NotSerializableException.class, () -> {
116       serialize(cacheKey);
117     });
118   }
119 
120   @Test
121   void serializationTest() throws Exception {
122     CacheKey cacheKey = new CacheKey();
123     cacheKey.update("serializable");
124     assertEquals(cacheKey, serialize(cacheKey));
125   }
126 
127   private static <T> T serialize(T object) throws Exception {
128     ByteArrayOutputStream baos = new ByteArrayOutputStream();
129     new ObjectOutputStream(baos).writeObject(object);
130 
131     ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
132     return (T) new ObjectInputStream(bais).readObject();
133   }
134 
135 }