View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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.*;
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.Test;
28  
29  class CacheKeyTest {
30  
31    @Test
32    void shouldTestCacheKeysEqual() {
33      Date date = new Date();
34      CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null, new Date(date.getTime()) });
35      CacheKey key2 = new CacheKey(new Object[] { 1, "hello", null, new Date(date.getTime()) });
36      assertEquals(key1, key2);
37      assertEquals(key2, key1);
38      assertEquals(key1.hashCode(), key2.hashCode());
39      assertEquals(key1.toString(), key2.toString());
40    }
41  
42    @Test
43    void shouldTestCacheKeysNotEqualDueToDateDifference() throws Exception {
44      CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null, new Date() });
45      Thread.sleep(1000);
46      CacheKey key2 = new CacheKey(new Object[] { 1, "hello", null, new Date() });
47      assertNotEquals(key1, key2);
48      assertNotEquals(key2, key1);
49      assertNotEquals(key1.hashCode(), key2.hashCode());
50      assertNotEquals(key1.toString(), key2.toString());
51    }
52  
53    @Test
54    void shouldTestCacheKeysNotEqualDueToOrder() throws Exception {
55      CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null });
56      Thread.sleep(1000);
57      CacheKey key2 = new CacheKey(new Object[] { 1, null, "hello" });
58      assertNotEquals(key1, key2);
59      assertNotEquals(key2, key1);
60      assertNotEquals(key1.hashCode(), key2.hashCode());
61      assertNotEquals(key1.toString(), key2.toString());
62    }
63  
64    @Test
65    void shouldDemonstrateEmptyAndNullKeysAreEqual() {
66      CacheKey key1 = new CacheKey();
67      CacheKey key2 = new CacheKey();
68      assertEquals(key1, key2);
69      assertEquals(key2, key1);
70      key1.update(null);
71      key2.update(null);
72      assertEquals(key1, key2);
73      assertEquals(key2, key1);
74      key1.update(null);
75      key2.update(null);
76      assertEquals(key1, key2);
77      assertEquals(key2, key1);
78    }
79  
80    @Test
81    void shouldTestCacheKeysWithBinaryArrays() {
82      byte[] array1 = new byte[] { 1 };
83      byte[] array2 = new byte[] { 1 };
84      CacheKey key1 = new CacheKey(new Object[] { array1 });
85      CacheKey key2 = new CacheKey(new Object[] { array2 });
86      assertEquals(key1, key2);
87    }
88  
89    @Test
90    void throwExceptionWhenTryingToUpdateNullCacheKey() {
91      CacheKey cacheKey = CacheKey.NULL_CACHE_KEY;
92      assertThrows(CacheException.class, () -> cacheKey.update("null"));
93    }
94  
95    @Test
96    void throwExceptionWhenTryingToUpdateAllNullCacheKey() {
97      CacheKey cacheKey = CacheKey.NULL_CACHE_KEY;
98      assertThrows(CacheException.class, () -> cacheKey.updateAll(new Object[]{"null", "null"}));
99    }
100 
101   @Test
102   void shouldDemonstrateClonedNullCacheKeysAreEqual() throws Exception {
103     CacheKey cacheKey = CacheKey.NULL_CACHE_KEY;
104     CacheKey clonedCacheKey = cacheKey.clone();
105     assertEquals(cacheKey, clonedCacheKey);
106     assertEquals(cacheKey.hashCode(), clonedCacheKey.hashCode());
107   }
108 
109   @Test
110   void serializationExceptionTest() {
111     CacheKey cacheKey = new CacheKey();
112     cacheKey.update(new Object());
113     assertThrows(NotSerializableException.class, () -> {
114       serialize(cacheKey);
115     });
116   }
117 
118   @Test
119   void serializationTest() throws Exception {
120     CacheKey cacheKey = new CacheKey();
121     cacheKey.update("serializable");
122     assertEquals(cacheKey, serialize(cacheKey));
123   }
124 
125   private static <T> T serialize(T object) throws Exception {
126       ByteArrayOutputStream baos = new ByteArrayOutputStream();
127       new ObjectOutputStream(baos).writeObject(object);
128 
129       ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
130       return (T) new ObjectInputStream(bais).readObject();
131   }
132 
133 }