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