1   package org.apache.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 java.io.IOException;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.jcs.auxiliary.AuxiliaryCache;
27  import org.apache.jcs.auxiliary.AuxiliaryCacheMockImpl;
28  import org.apache.jcs.engine.CacheElement;
29  import org.apache.jcs.engine.CompositeCacheAttributes;
30  import org.apache.jcs.engine.ElementAttributes;
31  import org.apache.jcs.engine.behavior.ICache;
32  import org.apache.jcs.engine.behavior.ICacheElement;
33  import org.apache.jcs.engine.behavior.ICompositeCacheAttributes;
34  import org.apache.jcs.engine.behavior.IElementAttributes;
35  import org.apache.jcs.engine.memory.MemoryCacheMockImpl;
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.jcs.engine.memory.MemoryCacheMockImpl";
57          ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
58          cattr.setMemoryCacheName( mockMemoryCacheClassName );
59  
60          IElementAttributes attr = new ElementAttributes();
61  
62          CompositeCache cache = new CompositeCache( cacheName, cattr, attr );
63  
64          AuxiliaryCacheMockImpl diskMock = new AuxiliaryCacheMockImpl();
65          diskMock.cacheType = ICache.DISK_CACHE;
66          AuxiliaryCache[] aux = new AuxiliaryCache[] { diskMock };
67          cache.setAuxCaches( aux );
68  
69          // DO WORK
70          int numToInsert = 10;
71          for ( int i = 0; i < numToInsert; i++ )
72          {
73              ICacheElement element = new CacheElement( cacheName, String.valueOf( i ), new Integer( i ) );
74              cache.update( element, false );
75          }
76  
77          cache.dispose();
78  
79          // VERIFY
80          MemoryCacheMockImpl memoryCache = (MemoryCacheMockImpl) cache.getMemoryCache();
81          assertEquals( "Wrong number freed.", numToInsert, memoryCache.lastNumberOfFreedElements );
82      }
83  
84      /***
85       * Verify that the freeMemoryElements method on the memory cache is NOT called on shutdown if
86       * there is NOT a disk cache.
87       * <p>
88       * @throws IOException
89       */
90      public void testShutdownMemoryFlush_noDisk()
91          throws IOException
92      {
93          // SETUP
94          String cacheName = "testCacheName";
95          String mockMemoryCacheClassName = "org.apache.jcs.engine.memory.MemoryCacheMockImpl";
96          ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
97          cattr.setMemoryCacheName( mockMemoryCacheClassName );
98  
99          IElementAttributes attr = new ElementAttributes();
100 
101         CompositeCache cache = new CompositeCache( cacheName, cattr, attr );
102 
103         AuxiliaryCacheMockImpl diskMock = new AuxiliaryCacheMockImpl();
104         diskMock.cacheType = ICache.REMOTE_CACHE;
105         AuxiliaryCache[] aux = new AuxiliaryCache[] { diskMock };
106         cache.setAuxCaches( aux );
107 
108         // DO WORK
109         int numToInsert = 10;
110         for ( int i = 0; i < numToInsert; i++ )
111         {
112             ICacheElement element = new CacheElement( cacheName, String.valueOf( i ), new Integer( i ) );
113             cache.update( element, false );
114         }
115 
116         cache.dispose();
117 
118         // VERIFY
119         MemoryCacheMockImpl memoryCache = (MemoryCacheMockImpl) cache.getMemoryCache();
120         assertEquals( "Wrong number freed.", 0, memoryCache.lastNumberOfFreedElements );
121     }
122 }