View Javadoc
1   package org.apache.commons.jcs.auxiliary.disk.indexed;
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.extensions.ActiveTestSuite;
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import org.apache.commons.jcs.JCS;
26  import org.apache.commons.jcs.access.CacheAccess;
27  import org.apache.commons.jcs.engine.behavior.ICacheElement;
28  
29  import java.util.HashSet;
30  import java.util.Map;
31  import java.util.Set;
32  
33  /**
34   * Test which exercises the indexed disk cache. This one uses three different
35   * regions for thre threads. It uses a config file that specifies 0 items in
36   * memory.
37   */
38  public class IndexedDiskCacheNoMemoryUnitTest
39      extends TestCase
40  {
41      /**
42       * Number of items to cache; the configured maxObjects for the memory cache
43       * regions is 0.
44       */
45      private static int items = 2000;
46  
47      /**
48       * @param testName
49       */
50      public IndexedDiskCacheNoMemoryUnitTest( String testName )
51      {
52          super( testName );
53      }
54  
55      /**
56       * Main method passes this test to the text test runner.
57       * <p>
58       * @param args
59       */
60      public static void main( String args[] )
61      {
62          String[] testCaseName = { IndexedDiskCacheNoMemoryUnitTest.class.getName() };
63          junit.textui.TestRunner.main( testCaseName );
64      }
65  
66      /**
67       * A unit test suite for JUnit
68       * <p>
69       * @return The test suite
70       */
71      public static Test suite()
72      {
73          ActiveTestSuite suite = new ActiveTestSuite();
74  
75          suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache1" )
76          {
77              @Override
78              public void runTest()
79                  throws Exception
80              {
81                  this.runTestForRegion( "indexedRegion1" );
82              }
83          } );
84  
85          suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache2" )
86          {
87              @Override
88              public void runTest()
89                  throws Exception
90              {
91                  this.runTestForRegion( "indexedRegion2" );
92              }
93          } );
94  
95          suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache3" )
96          {
97              @Override
98              public void runTest()
99                  throws Exception
100             {
101                 this.runTestForRegion( "indexedRegion3" );
102             }
103         } );
104 
105         return suite;
106     }
107 
108     /**
109      * Test setup
110      */
111     @Override
112     public void setUp()
113     {
114         JCS.setConfigFilename( "/TestDiskCacheNoMemory.ccf" );
115     }
116 
117     /**
118      * Adds items to cache, gets them, and removes them. The item count is more
119      * than the size of the memory cache, so items should spool to disk.
120      *
121      * @param region
122      *            Name of the region to access
123      *
124      * @throws Exception
125      *                If an error occurs
126      */
127     public void runTestForRegion( String region )
128         throws Exception
129     {
130         CacheAccess<String, String> jcs = JCS.getInstance( region );
131 
132         // Add items to cache
133 
134         for ( int i = 0; i <= items; i++ )
135         {
136             jcs.put( i + ":key", region + " data " + i );
137         }
138 
139         // Test that all items are in cache
140 
141         for ( int i = 0; i <= items; i++ )
142         {
143             String value = jcs.get( i + ":key" );
144 
145             assertEquals( region + " data " + i, value );
146         }
147 
148         // Test that getElements returns all the expected values
149         Set<String> keys = new HashSet<String>();
150         for ( int i = 0; i <= items; i++ )
151         {
152             keys.add( i + ":key" );
153         }
154 
155         Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
156         for ( int i = 0; i <= items; i++ )
157         {
158             ICacheElement<String, String> element = elements.get( i + ":key" );
159             assertNotNull( "element " + i + ":key is missing", element );
160             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
161         }
162 
163         // Remove all the items
164         for ( int i = 0; i <= items; i++ )
165         {
166             jcs.remove( i + ":key" );
167         }
168 
169         // Verify removal
170         for ( int i = 0; i <= items; i++ )
171         {
172             assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs.get( i + ":key" ) );
173         }
174 
175         // dump the stats to the report
176 //        System.out.println( jcs.getStats() );
177     }
178 }