1   package org.apache.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  
26  /***
27   * Test which exercises the indexed disk cache. This one uses three different
28   * regions for thre threads.
29   *
30   * @version $Id: TestTCPLateralCache.java 536904 2007-05-10 16:03:42Z tv $
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 int items = 200;
40  
41      /***
42       * Constructor for the TestDiskCache object.
43       *
44       * @param testName
45       */
46      public TestTCPLateralCache( String testName )
47      {
48          super( testName );
49      }
50  
51      /***
52       * Main method passes this test to the text test runner.
53       *
54       * @param args
55       */
56      public static void main( String args[] )
57      {
58          String[] testCaseName = { TestTCPLateralCache.class.getName() };
59          junit.textui.TestRunner.main( testCaseName );
60      }
61  
62      /***
63       * A unit test suite for JUnit
64       *
65       * @return The test suite
66       */
67      public static Test suite()
68      {
69          ActiveTestSuite suite = new ActiveTestSuite();
70  
71          suite.addTest( new TestTCPLateralCache( "testTcpRegion1_no_receiver" )
72          {
73              public void runTest()
74                  throws Exception
75              {
76                  this.runTestForRegion( "testTcpRegion1" );
77              }
78          } );
79  
80          //        suite.addTest( new TestTCPLateralCache( "testIndexedDiskCache2" )
81          //        {
82          //            public void runTest() throws Exception
83          //            {
84          //                this.runTestForRegion( "indexedRegion2" );
85          //            }
86          //        } );
87          //
88          //        suite.addTest( new TestTCPLateralCache( "testIndexedDiskCache3" )
89          //        {
90          //            public void runTest() throws Exception
91          //            {
92          //                this.runTestForRegion( "indexedRegion3" );
93          //            }
94          //        } );
95  
96          return suite;
97      }
98  
99      /***
100      * Test setup
101      */
102     public void setUp()
103     {
104         JCS.setConfigFilename( "/TestTCPLateralCache.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      * @exception Exception
115      *                If an error occurs
116      */
117     public void runTestForRegion( String region )
118         throws Exception
119     {
120         JCS 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             String value = (String) jcs.get( i + ":key" );
134 
135             assertEquals( region + " data " + i, value );
136         }
137 
138         // Remove all the items
139 
140         for ( int i = 0; i <= items; i++ )
141         {
142             jcs.remove( i + ":key" );
143         }
144 
145         // Verify removal
146 
147         for ( int i = 0; i <= items; i++ )
148         {
149             assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
150         }
151     }
152 }