View Javadoc
1   /*
2    *    Copyright 2009-2022 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  
20  import java.util.HashSet;
21  import java.util.Set;
22  
23  import org.apache.ibatis.cache.decorators.LoggingCache;
24  import org.apache.ibatis.cache.decorators.ScheduledCache;
25  import org.apache.ibatis.cache.decorators.SerializedCache;
26  import org.apache.ibatis.cache.decorators.SynchronizedCache;
27  import org.apache.ibatis.cache.impl.PerpetualCache;
28  import org.junit.jupiter.api.Test;
29  
30  class BaseCacheTest {
31  
32    @Test
33    void shouldDemonstrateEqualsAndHashCodeForVariousCacheTypes() {
34      PerpetualCache cache = new PerpetualCache("test_cache");
35      assertEquals(cache, cache);
36      assertEquals(cache, new SynchronizedCache(cache));
37      assertEquals(cache, new SerializedCache(cache));
38      assertEquals(cache, new LoggingCache(cache));
39      assertEquals(cache, new ScheduledCache(cache));
40  
41      assertEquals(cache.hashCode(), new SynchronizedCache(cache).hashCode());
42      assertEquals(cache.hashCode(), new SerializedCache(cache).hashCode());
43      assertEquals(cache.hashCode(), new LoggingCache(cache).hashCode());
44      assertEquals(cache.hashCode(), new ScheduledCache(cache).hashCode());
45  
46      Set<Cache> caches = new HashSet<>();
47      caches.add(cache);
48      caches.add(new SynchronizedCache(cache));
49      caches.add(new SerializedCache(cache));
50      caches.add(new LoggingCache(cache));
51      caches.add(new ScheduledCache(cache));
52      assertEquals(1, caches.size());
53    }
54  
55  }