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