View Javadoc
1   /**
2    *    Copyright 2010-2016 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.mybatis.caches.hazelcast;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertNull;
21  
22  import org.apache.ibatis.cache.Cache;
23  import org.junit.Test;
24  import org.mybatis.caches.oscache.OSCache;
25  
26  public final class OSCacheTestCase {
27  
28      private static final String DEFAULT_ID = "Hazelcast";
29  
30      private static Cache newCache() {
31          return new OSCache(DEFAULT_ID);
32      }
33  
34      @Test
35      public void shouldDemonstrateHowAllObjectsAreKept() {
36          Cache cache = newCache();
37          for (int i = 0; i < 100000; i++) {
38              cache.putObject(i, i);
39              assertEquals(i, cache.getObject(i));
40          }
41          assertEquals(100000, cache.getSize());
42      }
43  
44      @Test
45      public void shouldDemonstrateCopiesAreEqual() {
46          Cache cache = newCache();
47          for (int i = 0; i < 1000; i++) {
48              cache.putObject(i, i);
49              assertEquals(i, cache.getObject(i));
50          }
51      }
52  
53      @Test
54      public void shouldRemoveItemOnDemand() {
55          Cache cache = newCache();
56          cache.putObject(0, 0);
57          assertNotNull(cache.getObject(0));
58          cache.removeObject(0);
59          assertNull(cache.getObject(0));
60      }
61  
62      @Test
63      public void shouldFlushAllItemsOnDemand() {
64          Cache cache = newCache();
65          for (int i = 0; i < 5; i++) {
66              cache.putObject(i, i);
67          }
68          assertNotNull(cache.getObject(0));
69          assertNotNull(cache.getObject(4));
70          cache.clear();
71          assertNull(cache.getObject(0));
72          assertNull(cache.getObject(4));
73      }
74  
75  }