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