View Javadoc
1   package org.apache.commons.jcs.access;
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 java.util.Set;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.commons.jcs.JCS;
27  import org.apache.commons.jcs.access.exception.CacheException;
28  
29  /**
30   * Tests the methods of the group cache access class.
31   * <p>
32   * @author Aaron Smuts
33   */
34  public class GroupCacheAccessUnitTest
35      extends TestCase
36  {
37      /**
38       * Verify that we can put and get an object
39       * @throws Exception
40       */
41      public void testPutAndGet()
42          throws Exception
43      {
44          GroupCacheAccess<String, String> access = JCS.getGroupCacheInstance( "test" );
45          assertNotNull( "We should have an access class", access );
46  
47          String key = "mykey";
48          String group = "mygroup";
49          String value = "myvalue";
50  
51          access.putInGroup(key, group, value);
52  
53          String returnedValue1 = access.getFromGroup(key, group);
54          assertEquals( "Wrong value returned.", value, returnedValue1 );
55      }
56  
57      /**
58       * Try to put a null key and verify that we get an exception.
59       * @throws Exception
60       */
61      public void testPutNullKey()
62          throws Exception
63      {
64          GroupCacheAccess<String, String> access = JCS.getGroupCacheInstance( "test" );
65          assertNotNull( "We should have an access class", access );
66  
67          String key = null;
68          String group = "mygroup";
69          String value = "myvalue";
70  
71          try
72          {
73              access.putInGroup(key, group, value);
74              fail( "Should not have been able to put a null key." );
75          }
76          catch ( CacheException e )
77          {
78              assertTrue( "Should have the word null in the error message.", e.getMessage().indexOf( "null" ) != -1 );
79          }
80      }
81  
82      /**
83       * Try to put a null value and verify that we get an exception.
84       * @throws Exception
85       */
86      public void testPutNullValue()
87          throws Exception
88      {
89          GroupCacheAccess<String, String> access = JCS.getGroupCacheInstance( "test" );
90          assertNotNull( "We should have an access class", access );
91  
92          String key = "myKey";
93          String group = "mygroup";
94          String value = null;
95  
96          try
97          {
98              access.putInGroup(key, group, value);
99              fail( "Should not have been able to put a null object." );
100         }
101         catch ( CacheException e )
102         {
103             assertTrue( "Should have the word null in the error message.", e.getMessage().indexOf( "null" ) != -1 );
104         }
105     }
106 
107     /**
108      * Verify that we can remove items from the cache
109      * @throws Exception
110      */
111     public void testRemove()
112         throws Exception
113     {
114         GroupCacheAccess<String, String> access = JCS.getGroupCacheInstance( "test" );
115         assertNotNull( "We should have an access class", access );
116 
117         String key = "mykey";
118         String group = "mygroup";
119         String value = "myvalue";
120 
121         for (int i = 0; i < 10; i++)
122         {
123             access.putInGroup(key + i, group, value + i);
124         }
125 
126         // Make sure cache contains some data
127         for (int i = 0; i < 10; i++)
128         {
129             String returnedValue1 = access.getFromGroup(key + i, group);
130             assertEquals( "Wrong value returned.", value + i, returnedValue1 );
131         }
132 
133         access.removeFromGroup(key + 0, group);
134 
135         assertNull("Should not be in cache", access.getFromGroup(key + 0, group));
136 
137         for (int i = 1; i < 10; i++)
138         {
139             String returnedValue1 = access.getFromGroup(key + i, group);
140             assertEquals( "Wrong value returned.", value + i, returnedValue1 );
141         }
142     }
143 
144     /**
145      * Verify that we can invalidate the group
146      * @throws Exception
147      */
148     public void testInvalidate()
149         throws Exception
150     {
151         GroupCacheAccess<String, String> access = JCS.getGroupCacheInstance( "test" );
152         assertNotNull( "We should have an access class", access );
153 
154         String key = "mykey";
155         String group = "mygroup";
156         String value = "myvalue";
157 
158         for (int i = 0; i < 10; i++)
159         {
160             access.putInGroup(key + i, group + 0, value + i);
161         }
162 
163         for (int i = 0; i < 10; i++)
164         {
165             access.putInGroup(key + i, group + 1, value + i);
166         }
167 
168         // Make sure cache contains some data
169         for (int i = 0; i < 10; i++)
170         {
171             String returnedValue1 = access.getFromGroup(key + i, group + 0);
172             assertEquals( "Wrong value returned.", value + i, returnedValue1 );
173             String returnedValue2 = access.getFromGroup(key + i, group + 1);
174             assertEquals( "Wrong value returned.", value + i, returnedValue2 );
175         }
176 
177         access.invalidateGroup(group + 0);
178 
179         for (int i = 0; i < 10; i++)
180         {
181             assertNull("Should not be in cache", access.getFromGroup(key + i, group + 0));
182         }
183 
184         for (int i = 0; i < 10; i++)
185         {
186             String returnedValue1 = access.getFromGroup(key + i, group + 1);
187             assertEquals( "Wrong value returned.", value + i, returnedValue1 );
188         }
189     }
190 
191     /**
192      * Verify we can use the group cache.
193      * <p>
194      * @throws Exception
195      */
196     public void testGroupCache()
197         throws Exception
198     {
199         GroupCacheAccess<String, Integer> access = JCS.getGroupCacheInstance( "testGroup" );
200         String groupName1 = "testgroup1";
201         String groupName2 = "testgroup2";
202 
203         Set<String> keys1 = access.getGroupKeys( groupName1 );
204         assertNotNull(keys1);
205         assertEquals(0, keys1.size());
206 
207         Set<String> keys2 = access.getGroupKeys( groupName2 );
208         assertNotNull(keys2);
209         assertEquals(0, keys2.size());
210 
211         // DO WORK
212         int numToInsertGroup1 = 10;
213         // insert with prefix1
214         for ( int i = 0; i < numToInsertGroup1; i++ )
215         {
216             access.putInGroup(String.valueOf( i ), groupName1, Integer.valueOf( i ) );
217         }
218 
219         int numToInsertGroup2 = 50;
220         // insert with prefix1
221         for ( int i = 0; i < numToInsertGroup2; i++ )
222         {
223             access.putInGroup(String.valueOf( i ), groupName2, Integer.valueOf( i + 1 ) );
224         }
225 
226         keys1 = access.getGroupKeys( groupName1 ); // Test for JCS-102
227         assertNotNull(keys1);
228         assertEquals("Wrong number returned 1:", 10, keys1.size());
229 
230         keys2 = access.getGroupKeys( groupName2 );
231         assertNotNull(keys2);
232         assertEquals("Wrong number returned 2:", 50, keys2.size());
233 
234         assertEquals(Integer.valueOf(5), access.getFromGroup("5", groupName1));
235         assertEquals(Integer.valueOf(6), access.getFromGroup("5", groupName2));
236 
237         assertTrue(access.getGroupNames().contains(groupName1));
238         assertTrue(access.getGroupNames().contains(groupName2));
239     }
240 }