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