View Javadoc
1   package org.apache.commons.jcs3.engine.control;
2   
3   import java.io.IOException;
4   import java.util.Arrays;
5   import java.util.Map;
6   
7   import org.apache.commons.jcs3.auxiliary.MockAuxiliaryCache;
8   import org.apache.commons.jcs3.engine.CacheElement;
9   import org.apache.commons.jcs3.engine.CompositeCacheAttributes;
10  import org.apache.commons.jcs3.engine.ElementAttributes;
11  import org.apache.commons.jcs3.engine.behavior.ICacheElement;
12  import org.apache.commons.jcs3.engine.behavior.ICacheType.CacheType;
13  import org.apache.commons.jcs3.engine.behavior.ICompositeCacheAttributes;
14  import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
15  import org.apache.commons.jcs3.engine.memory.MockMemoryCache;
16  
17  /*
18   * Licensed to the Apache Software Foundation (ASF) under one
19   * or more contributor license agreements.  See the NOTICE file
20   * distributed with this work for additional information
21   * regarding copyright ownership.  The ASF licenses this file
22   * to you under the Apache License, Version 2.0 (the
23   * "License"); you may not use this file except in compliance
24   * with the License.  You may obtain a copy of the License at
25   *
26   *   http://www.apache.org/licenses/LICENSE-2.0
27   *
28   * Unless required by applicable law or agreed to in writing,
29   * software distributed under the License is distributed on an
30   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
31   * KIND, either express or implied.  See the License for the
32   * specific language governing permissions and limitations
33   * under the License.
34   */
35  
36  import junit.framework.TestCase;
37  
38  /**
39   * Tests that directly engage the composite cache.
40   */
41  public class CompositeCacheUnitTest
42      extends TestCase
43  {
44      /**
45       * Verify that the freeMemoryElements method on the memory cache is called on shutdown if there
46       * is a disk cache.
47       * <p>
48       * @throws IOException
49       */
50      public void testShutdownMemoryFlush()
51          throws IOException
52      {
53          // SETUP
54          final String cacheName = "testCacheName";
55          final String mockMemoryCacheClassName = "org.apache.commons.jcs3.engine.memory.MockMemoryCache";
56          final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
57          cattr.setMemoryCacheName( mockMemoryCacheClassName );
58  
59          final IElementAttributes attr = new ElementAttributes();
60  
61          final CompositeCache<String, Integer> cache = new CompositeCache<>( cattr, attr );
62  
63          final MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<>();
64          diskMock.cacheType = CacheType.DISK_CACHE;
65          cache.setAuxCaches(Arrays.asList(diskMock));
66  
67          // DO WORK
68          final int numToInsert = 10;
69          for ( int i = 0; i < numToInsert; i++ )
70          {
71              final ICacheElement<String, Integer> element = new CacheElement<>( cacheName, String.valueOf( i ), Integer.valueOf( i ) );
72              cache.update( element, false );
73          }
74  
75          cache.dispose();
76  
77          // VERIFY
78          final MockMemoryCache<String, Integer> memoryCache = (MockMemoryCache<String, Integer>) cache.getMemoryCache();
79          assertEquals( "Wrong number freed.", numToInsert, memoryCache.lastNumberOfFreedElements );
80      }
81  
82      /**
83       * Verify that the freeMemoryElements method on the memory cache is NOT called on shutdown if
84       * there is NOT a disk cache.
85       * <p>
86       * @throws IOException
87       */
88      public void testShutdownMemoryFlush_noDisk()
89          throws IOException
90      {
91          // SETUP
92          final String cacheName = "testCacheName";
93          final String mockMemoryCacheClassName = "org.apache.commons.jcs3.engine.memory.MockMemoryCache";
94          final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
95          cattr.setMemoryCacheName( mockMemoryCacheClassName );
96  
97          final IElementAttributes attr = new ElementAttributes();
98  
99          final CompositeCache<String, Integer> cache = new CompositeCache<>( cattr, attr );
100 
101         final MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<>();
102         diskMock.cacheType = CacheType.REMOTE_CACHE;
103         cache.setAuxCaches(Arrays.asList(diskMock));
104 
105         // DO WORK
106         final int numToInsert = 10;
107         for ( int i = 0; i < numToInsert; i++ )
108         {
109             final ICacheElement<String, Integer> element = new CacheElement<>( cacheName, String.valueOf( i ), Integer.valueOf( i ) );
110             cache.update( element, false );
111         }
112 
113         cache.dispose();
114 
115         // VERIFY
116         final MockMemoryCache<String, Integer> memoryCache = (MockMemoryCache<String, Integer>) cache.getMemoryCache();
117         assertEquals( "Wrong number freed.", 0, memoryCache.lastNumberOfFreedElements );
118     }
119 
120     /**
121      * Verify we can get some matching elements..
122      * <p>
123      * @throws IOException
124      */
125     public void testGetMatching_Normal()
126         throws IOException
127     {
128         // SETUP
129         final int maxMemorySize = 1000;
130         final String keyprefix1 = "MyPrefix1";
131         final String keyprefix2 = "MyPrefix2";
132         final String cacheName = "testGetMatching_Normal";
133         final String memoryCacheClassName = "org.apache.commons.jcs3.engine.memory.lru.LRUMemoryCache";
134         final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
135         cattr.setMemoryCacheName( memoryCacheClassName );
136         cattr.setMaxObjects( maxMemorySize );
137 
138         final IElementAttributes attr = new ElementAttributes();
139 
140         final CompositeCache<String, Integer> cache = new CompositeCache<>( cattr, attr );
141 
142         final MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<>();
143         diskMock.cacheType = CacheType.DISK_CACHE;
144         cache.setAuxCaches(Arrays.asList(diskMock));
145 
146         // DO WORK
147         final int numToInsertPrefix1 = 10;
148         // insert with prefix1
149         for ( int i = 0; i < numToInsertPrefix1; i++ )
150         {
151             final ICacheElement<String, Integer> element = new CacheElement<>( cacheName, keyprefix1 + String.valueOf( i ), Integer.valueOf( i ) );
152             cache.update( element, false );
153         }
154 
155         final int numToInsertPrefix2 = 50;
156         // insert with prefix1
157         for ( int i = 0; i < numToInsertPrefix2; i++ )
158         {
159             final ICacheElement<String, Integer> element = new CacheElement<>( cacheName, keyprefix2 + String.valueOf( i ), Integer.valueOf( i ) );
160             cache.update( element, false );
161         }
162 
163         final Map<?, ?> result1 = cache.getMatching( keyprefix1 + "\\S+" );
164         final Map<?, ?> result2 = cache.getMatching( keyprefix2 + "\\S+" );
165 
166         // VERIFY
167         assertEquals( "Wrong number returned 1:", numToInsertPrefix1, result1.size() );
168         assertEquals( "Wrong number returned 2:", numToInsertPrefix2, result2.size() );
169     }
170 
171     /**
172      * Verify we try a disk aux on a getMatching call.
173      * <p>
174      * @throws IOException
175      */
176     public void testGetMatching_NotOnDisk()
177         throws IOException
178     {
179         // SETUP
180         final int maxMemorySize = 0;
181         final String cacheName = "testGetMatching_NotOnDisk";
182         final String memoryCacheClassName = "org.apache.commons.jcs3.engine.memory.lru.LRUMemoryCache";
183         final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
184         cattr.setCacheName(cacheName);
185         cattr.setMemoryCacheName( memoryCacheClassName );
186         cattr.setMaxObjects( maxMemorySize );
187 
188         final IElementAttributes attr = new ElementAttributes();
189 
190         final CompositeCache<String, Integer> cache = new CompositeCache<>( cattr, attr );
191 
192         final MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<>();
193         diskMock.cacheType = CacheType.DISK_CACHE;
194         cache.setAuxCaches(Arrays.asList(diskMock));
195 
196         // DO WORK
197         cache.getMatching( "junk" );
198 
199         // VERIFY
200         assertEquals( "Wrong number of calls", 1, diskMock.getMatchingCallCount );
201     }
202 
203     /**
204      * Verify we try a remote  aux on a getMatching call.
205      * <p>
206      * @throws IOException
207      */
208     public void testGetMatching_NotOnRemote()
209         throws IOException
210     {
211         // SETUP
212         final int maxMemorySize = 0;
213         final String cacheName = "testGetMatching_NotOnDisk";
214         final String memoryCacheClassName = "org.apache.commons.jcs3.engine.memory.lru.LRUMemoryCache";
215         final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
216         cattr.setCacheName(cacheName);
217         cattr.setMemoryCacheName( memoryCacheClassName );
218         cattr.setMaxObjects( maxMemorySize );
219 
220         final IElementAttributes attr = new ElementAttributes();
221 
222         final CompositeCache<String, Integer> cache = new CompositeCache<>( cattr, attr );
223 
224         final MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<>();
225         diskMock.cacheType = CacheType.REMOTE_CACHE;
226         cache.setAuxCaches(Arrays.asList(diskMock));
227 
228         // DO WORK
229         cache.getMatching( "junk" );
230 
231         // VERIFY
232         assertEquals( "Wrong number of calls", 1, diskMock.getMatchingCallCount );
233     }
234 }