View Javadoc
1   package org.apache.commons.jcs3.auxiliary.disk.indexed;
2   
3   import java.util.HashSet;
4   import java.util.Map;
5   import java.util.Set;
6   
7   import org.apache.commons.jcs3.JCS;
8   import org.apache.commons.jcs3.access.CacheAccess;
9   import org.apache.commons.jcs3.engine.behavior.ICacheElement;
10  
11  /*
12   * Licensed to the Apache Software Foundation (ASF) under one
13   * or more contributor license agreements.  See the NOTICE file
14   * distributed with this work for additional information
15   * regarding copyright ownership.  The ASF licenses this file
16   * to you under the Apache License, Version 2.0 (the
17   * "License"); you may not use this file except in compliance
18   * with the License.  You may obtain a copy of the License at
19   *
20   *   http://www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing,
23   * software distributed under the License is distributed on an
24   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
25   * KIND, either express or implied.  See the License for the
26   * specific language governing permissions and limitations
27   * under the License.
28   */
29  
30  import junit.extensions.ActiveTestSuite;
31  import junit.framework.Test;
32  import junit.framework.TestCase;
33  
34  /**
35   * Test which exercises the indexed disk cache. This one uses three different
36   * regions for thre threads. It uses a config file that specifies 0 items in
37   * memory.
38   */
39  public class IndexedDiskCacheNoMemoryUnitTest
40      extends TestCase
41  {
42      /**
43       * Number of items to cache; the configured maxObjects for the memory cache
44       * regions is 0.
45       */
46      private static final int items = 2000;
47  
48      /**
49       * @param testName
50       */
51      public IndexedDiskCacheNoMemoryUnitTest( final String testName )
52      {
53          super( testName );
54      }
55  
56      /**
57       * A unit test suite for JUnit
58       * <p>
59       * @return The test suite
60       */
61      public static Test suite()
62      {
63          final ActiveTestSuite suite = new ActiveTestSuite();
64  
65          suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache1" )
66          {
67              @Override
68              public void runTest()
69                  throws Exception
70              {
71                  this.runTestForRegion( "indexedRegion1" );
72              }
73          } );
74  
75          suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache2" )
76          {
77              @Override
78              public void runTest()
79                  throws Exception
80              {
81                  this.runTestForRegion( "indexedRegion2" );
82              }
83          } );
84  
85          suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache3" )
86          {
87              @Override
88              public void runTest()
89                  throws Exception
90              {
91                  this.runTestForRegion( "indexedRegion3" );
92              }
93          } );
94  
95          return suite;
96      }
97  
98      /**
99       * Test setup
100      */
101     @Override
102     public void setUp()
103     {
104         JCS.setConfigFilename( "/TestDiskCacheNoMemory.ccf" );
105     }
106 
107     /**
108      * Adds items to cache, gets them, and removes them. The item count is more
109      * than the size of the memory cache, so items should spool to disk.
110      *
111      * @param region
112      *            Name of the region to access
113      *
114      * @throws Exception
115      *                If an error occurs
116      */
117     public void runTestForRegion( final String region )
118         throws Exception
119     {
120         final CacheAccess<String, String> jcs = JCS.getInstance( region );
121 
122         // Add items to cache
123 
124         for ( int i = 0; i <= items; i++ )
125         {
126             jcs.put( i + ":key", region + " data " + i );
127         }
128 
129         // Test that all items are in cache
130 
131         for ( int i = 0; i <= items; i++ )
132         {
133             final String value = jcs.get( i + ":key" );
134 
135             assertEquals( region + " data " + i, value );
136         }
137 
138         // Test that getElements returns all the expected values
139         final Set<String> keys = new HashSet<>();
140         for ( int i = 0; i <= items; i++ )
141         {
142             keys.add( i + ":key" );
143         }
144 
145         final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
146         for ( int i = 0; i <= items; i++ )
147         {
148             final ICacheElement<String, String> element = elements.get( i + ":key" );
149             assertNotNull( "element " + i + ":key is missing", element );
150             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
151         }
152 
153         // Remove all the items
154         for ( int i = 0; i <= items; i++ )
155         {
156             jcs.remove( i + ":key" );
157         }
158 
159         // Verify removal
160         for ( int i = 0; i <= items; i++ )
161         {
162             assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs.get( i + ":key" ) );
163         }
164 
165         // dump the stats to the report
166 //        System.out.println( jcs.getStats() );
167     }
168 }