Author: tv Date: Fri Sep 11 14:58:59 2015 New Revision: 1702493 URL: http://svn.apache.org/r1702493 Log: Fix removal of items from group cache, added several tests
Added: commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/access/GroupCacheAccessUnitTest.java (with props) Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/access/CacheAccessUnitTest.java commons/proper/jcs/trunk/src/changes/changes.xml Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java?rev=1702493&r1=1702492&r2=1702493&view=diff ============================================================================== --- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java (original) +++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java Fri Sep 11 14:58:59 2015 @@ -388,7 +388,7 @@ public abstract class AbstractDoubleLink } } } - else if ( key instanceof GroupAttrName ) + else if ( key instanceof GroupAttrName && ((GroupAttrName<?>)key).attrName == null) { // remove all keys of the same name hierarchy. for (Iterator<Map.Entry<K, MemoryElementDescriptor<K, V>>> itr = map.entrySet().iterator(); itr.hasNext(); ) Modified: commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/access/CacheAccessUnitTest.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/access/CacheAccessUnitTest.java?rev=1702493&r1=1702492&r2=1702493&view=diff ============================================================================== --- commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/access/CacheAccessUnitTest.java (original) +++ commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/access/CacheAccessUnitTest.java Fri Sep 11 14:58:59 2015 @@ -19,7 +19,12 @@ package org.apache.commons.jcs.access; * under the License. */ +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + import junit.framework.TestCase; + import org.apache.commons.jcs.JCS; import org.apache.commons.jcs.access.exception.CacheException; import org.apache.commons.jcs.access.exception.ObjectExistsException; @@ -29,12 +34,8 @@ import org.apache.commons.jcs.engine.beh import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes; import org.apache.commons.jcs.engine.behavior.IElementAttributes; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - /** - * Tests the methods of the cache access class from which the class JCS extends. + * Tests the methods of the cache access class. * <p> * @author Aaron Smuts */ @@ -62,7 +63,7 @@ public class CacheAccessUnitTest try { access.putSafe( key, "someothervalue" ); - fail( "We should have received an eception since this key is alredy in the cache." ); + fail( "We should have received an exception since this key is already in the cache." ); } catch ( CacheException e ) { @@ -71,7 +72,7 @@ public class CacheAccessUnitTest } String returnedValue2 = access.get( key ); - assertEquals( "Wrong value returned. Shoudl still be the original.", value, returnedValue2 ); + assertEquals( "Wrong value returned. Should still be the original.", value, returnedValue2 ); } /** @@ -94,7 +95,7 @@ public class CacheAccessUnitTest } catch ( CacheException e ) { - assertTrue( "Should have the work null in the error message.", e.getMessage().indexOf( "null" ) != -1 ); + assertTrue( "Should have the word null in the error message.", e.getMessage().indexOf( "null" ) != -1 ); } } @@ -118,7 +119,7 @@ public class CacheAccessUnitTest } catch ( CacheException e ) { - assertTrue( "Should have the work null in the error message.", e.getMessage().indexOf( "null" ) != -1 ); + assertTrue( "Should have the word null in the error message.", e.getMessage().indexOf( "null" ) != -1 ); } } @@ -223,7 +224,7 @@ public class CacheAccessUnitTest /** * Verify that we can get a region using the define region method with cache attributes and - * elemetn attributes. + * element attributes. * @throws Exception */ public void testRegionDefinitonWithBothAttributes() @@ -352,51 +353,4 @@ public class CacheAccessUnitTest assertTrue( "Should be a cache element.", value instanceof ICacheElement ); } } - - /** - * Verify we can use the group cache. - * <p> - * @throws Exception - */ - public void testGroupCache() - throws Exception - { - GroupCacheAccess<String, Integer> access = JCS.getGroupCacheInstance( "testGroup" ); - String groupName1 = "testgroup1"; - String groupName2 = "testgroup2"; - - Set<String> keys1 = access.getGroupKeys( groupName1 ); - assertNotNull(keys1); - assertEquals(0, keys1.size()); - - Set<String> keys2 = access.getGroupKeys( groupName2 ); - assertNotNull(keys2); - assertEquals(0, keys2.size()); - - // DO WORK - int numToInsertGroup1 = 10; - // insert with prefix1 - for ( int i = 0; i < numToInsertGroup1; i++ ) - { - access.putInGroup(String.valueOf( i ), groupName1, Integer.valueOf( i ) ); - } - - int numToInsertGroup2 = 50; - // insert with prefix1 - for ( int i = 0; i < numToInsertGroup2; i++ ) - { - access.putInGroup(String.valueOf( i ), groupName2, Integer.valueOf( i + 1 ) ); - } - - keys1 = access.getGroupKeys( groupName1 ); // Test for JCS-102 - assertNotNull(keys1); - assertEquals("Wrong number returned 1:", 10, keys1.size()); - - keys2 = access.getGroupKeys( groupName2 ); - assertNotNull(keys2); - assertEquals("Wrong number returned 2:", 50, keys2.size()); - - assertEquals(Integer.valueOf(5), access.getFromGroup("5", groupName1)); - assertEquals(Integer.valueOf(6), access.getFromGroup("5", groupName2)); - } } Added: commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/access/GroupCacheAccessUnitTest.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/access/GroupCacheAccessUnitTest.java?rev=1702493&view=auto ============================================================================== --- commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/access/GroupCacheAccessUnitTest.java (added) +++ commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/access/GroupCacheAccessUnitTest.java Fri Sep 11 14:58:59 2015 @@ -0,0 +1,240 @@ +package org.apache.commons.jcs.access; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Set; + +import junit.framework.TestCase; + +import org.apache.commons.jcs.JCS; +import org.apache.commons.jcs.access.exception.CacheException; + +/** + * Tests the methods of the group cache access class. + * <p> + * @author Aaron Smuts + */ +public class GroupCacheAccessUnitTest + extends TestCase +{ + /** + * Verify that we can put and get an object + * @throws Exception + */ + public void testPutAndGet() + throws Exception + { + GroupCacheAccess<String, String> access = JCS.getGroupCacheInstance( "test" ); + assertNotNull( "We should have an access class", access ); + + String key = "mykey"; + String group = "mygroup"; + String value = "myvalue"; + + access.putInGroup(key, group, value); + + String returnedValue1 = access.getFromGroup(key, group); + assertEquals( "Wrong value returned.", value, returnedValue1 ); + } + + /** + * Try to put a null key and verify that we get an exception. + * @throws Exception + */ + public void testPutNullKey() + throws Exception + { + GroupCacheAccess<String, String> access = JCS.getGroupCacheInstance( "test" ); + assertNotNull( "We should have an access class", access ); + + String key = null; + String group = "mygroup"; + String value = "myvalue"; + + try + { + access.putInGroup(key, group, value); + fail( "Should not have been able to put a null key." ); + } + catch ( CacheException e ) + { + assertTrue( "Should have the word null in the error message.", e.getMessage().indexOf( "null" ) != -1 ); + } + } + + /** + * Try to put a null value and verify that we get an exception. + * @throws Exception + */ + public void testPutNullValue() + throws Exception + { + GroupCacheAccess<String, String> access = JCS.getGroupCacheInstance( "test" ); + assertNotNull( "We should have an access class", access ); + + String key = "myKey"; + String group = "mygroup"; + String value = null; + + try + { + access.putInGroup(key, group, value); + fail( "Should not have been able to put a null object." ); + } + catch ( CacheException e ) + { + assertTrue( "Should have the word null in the error message.", e.getMessage().indexOf( "null" ) != -1 ); + } + } + + /** + * Verify that we can remove items from the cache + * @throws Exception + */ + public void testRemove() + throws Exception + { + GroupCacheAccess<String, String> access = JCS.getGroupCacheInstance( "test" ); + assertNotNull( "We should have an access class", access ); + + String key = "mykey"; + String group = "mygroup"; + String value = "myvalue"; + + for (int i = 0; i < 10; i++) + { + access.putInGroup(key + i, group, value + i); + } + + // Make sure cache contains some data + for (int i = 0; i < 10; i++) + { + String returnedValue1 = access.getFromGroup(key + i, group); + assertEquals( "Wrong value returned.", value + i, returnedValue1 ); + } + + access.removeFromGroup(key + 0, group); + + assertNull("Should not be in cache", access.getFromGroup(key + 0, group)); + + for (int i = 1; i < 10; i++) + { + String returnedValue1 = access.getFromGroup(key + i, group); + assertEquals( "Wrong value returned.", value + i, returnedValue1 ); + } + } + + /** + * Verify that we can invalidate the group + * @throws Exception + */ + public void testInvalidate() + throws Exception + { + GroupCacheAccess<String, String> access = JCS.getGroupCacheInstance( "test" ); + assertNotNull( "We should have an access class", access ); + + String key = "mykey"; + String group = "mygroup"; + String value = "myvalue"; + + for (int i = 0; i < 10; i++) + { + access.putInGroup(key + i, group + 0, value + i); + } + + for (int i = 0; i < 10; i++) + { + access.putInGroup(key + i, group + 1, value + i); + } + + // Make sure cache contains some data + for (int i = 0; i < 10; i++) + { + String returnedValue1 = access.getFromGroup(key + i, group + 0); + assertEquals( "Wrong value returned.", value + i, returnedValue1 ); + String returnedValue2 = access.getFromGroup(key + i, group + 1); + assertEquals( "Wrong value returned.", value + i, returnedValue2 ); + } + + access.invalidateGroup(group + 0); + + for (int i = 0; i < 10; i++) + { + assertNull("Should not be in cache", access.getFromGroup(key + i, group + 0)); + } + + for (int i = 0; i < 10; i++) + { + String returnedValue1 = access.getFromGroup(key + i, group + 1); + assertEquals( "Wrong value returned.", value + i, returnedValue1 ); + } + } + + /** + * Verify we can use the group cache. + * <p> + * @throws Exception + */ + public void testGroupCache() + throws Exception + { + GroupCacheAccess<String, Integer> access = JCS.getGroupCacheInstance( "testGroup" ); + String groupName1 = "testgroup1"; + String groupName2 = "testgroup2"; + + Set<String> keys1 = access.getGroupKeys( groupName1 ); + assertNotNull(keys1); + assertEquals(0, keys1.size()); + + Set<String> keys2 = access.getGroupKeys( groupName2 ); + assertNotNull(keys2); + assertEquals(0, keys2.size()); + + // DO WORK + int numToInsertGroup1 = 10; + // insert with prefix1 + for ( int i = 0; i < numToInsertGroup1; i++ ) + { + access.putInGroup(String.valueOf( i ), groupName1, Integer.valueOf( i ) ); + } + + int numToInsertGroup2 = 50; + // insert with prefix1 + for ( int i = 0; i < numToInsertGroup2; i++ ) + { + access.putInGroup(String.valueOf( i ), groupName2, Integer.valueOf( i + 1 ) ); + } + + keys1 = access.getGroupKeys( groupName1 ); // Test for JCS-102 + assertNotNull(keys1); + assertEquals("Wrong number returned 1:", 10, keys1.size()); + + keys2 = access.getGroupKeys( groupName2 ); + assertNotNull(keys2); + assertEquals("Wrong number returned 2:", 50, keys2.size()); + + assertEquals(Integer.valueOf(5), access.getFromGroup("5", groupName1)); + assertEquals(Integer.valueOf(6), access.getFromGroup("5", groupName2)); + + assertTrue(access.getGroupNames().contains(groupName1)); + assertTrue(access.getGroupNames().contains(groupName2)); + } +} Propchange: commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/access/GroupCacheAccessUnitTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: commons/proper/jcs/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/changes/changes.xml?rev=1702493&r1=1702492&r2=1702493&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/changes/changes.xml (original) +++ commons/proper/jcs/trunk/src/changes/changes.xml Fri Sep 11 14:58:59 2015 @@ -20,6 +20,9 @@ </properties> <body> <release version="2.0" date="unreleased" description="JDK 1.6 based major release"> + <action dev="tv" type="fix"> + Fix removal of items from group cache, added several tests + </action> <action dev="tv" type="add"> Add orderly shutdown of ThreadPoolManager </action>