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