Author: tv Date: Fri Sep 11 14:21:12 2015 New Revision: 1702477 URL: http://svn.apache.org/r1702477 Log: Update jcs dependency to commons-jcs 2.0-beta-1 Adjusted managers, added more tests
Added: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/manager/MethodResultCacheTest.java (with props) Modified: db/torque/torque4/trunk/torque-runtime/pom.xml db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/AbstractBaseManager.java db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/MethodResultCache.java db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/NoOpMethodResultCache.java db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/manager/AbstractBaseManagerTest.java db/torque/torque4/trunk/torque-runtime/src/test/resources/cache.ccf Modified: db/torque/torque4/trunk/torque-runtime/pom.xml URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/pom.xml?rev=1702477&r1=1702476&r2=1702477&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-runtime/pom.xml (original) +++ db/torque/torque4/trunk/torque-runtime/pom.xml Fri Sep 11 14:21:12 2015 @@ -71,13 +71,13 @@ <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> - <version>1.1.1</version> + <version>1.2</version> </dependency> <dependency> - <groupId>org.apache.jcs</groupId> - <artifactId>jcs</artifactId> - <version>1.3</version> + <groupId>org.apache.commons</groupId> + <artifactId>commons-jcs-core</artifactId> + <version>2.0-beta-1</version> </dependency> <dependency> Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/AbstractBaseManager.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/AbstractBaseManager.java?rev=1702477&r1=1702476&r2=1702477&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/AbstractBaseManager.java (original) +++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/AbstractBaseManager.java Fri Sep 11 14:21:12 2015 @@ -31,11 +31,12 @@ import java.util.List; import java.util.Map; import org.apache.commons.collections.FastArrayList; +import org.apache.commons.jcs.JCS; +import org.apache.commons.jcs.access.CacheAccess; +import org.apache.commons.jcs.access.GroupCacheAccess; +import org.apache.commons.jcs.access.exception.CacheException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.jcs.JCS; -import org.apache.jcs.access.GroupCacheAccess; -import org.apache.jcs.access.exception.CacheException; import org.apache.torque.Torque; import org.apache.torque.TorqueException; import org.apache.torque.om.ObjectKey; @@ -59,10 +60,13 @@ public abstract class AbstractBaseManage protected static final Log log = LogFactory.getLog(AbstractBaseManager.class); /** used to cache the om objects. cache is set by the region property */ - protected transient GroupCacheAccess cache; + protected transient CacheAccess<Serializable, T> cache; + + /** used to cache the method result objects. cache is set by the region property */ + protected transient GroupCacheAccess<MethodCacheKey, Object> groupCache; /** method results cache */ - protected MethodResultCache mrCache; + protected transient MethodResultCache mrCache; /** The OM class that the service will instantiate. */ private Class<T> omClass; @@ -220,16 +224,12 @@ public abstract class AbstractBaseManage * @param key the primary key of the object * @return the object from cache */ - @SuppressWarnings("unchecked") protected T cacheGet(final Serializable key) { T om = null; if (cache != null) { - synchronized (this) - { - om = (T) cache.get(key); - } + om = cache.get(key); } return om; } @@ -269,6 +269,12 @@ public abstract class AbstractBaseManage cache.dispose(); cache = null; } + + if (groupCache != null) + { + groupCache.dispose(); + groupCache = null; + } } /** @@ -279,27 +285,14 @@ public abstract class AbstractBaseManage * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ - @SuppressWarnings("unchecked") protected T removeInstanceImpl(final Serializable key) throws TorqueException { T oldOm = null; if (cache != null) { - try - { - synchronized (this) - { - oldOm = (T) cache.get(key); - cache.remove(key); - } - } - catch (CacheException ce) - { - throw new TorqueException - ("Could not remove from cache due to internal JCS error", - ce); - } + oldOm = cache.get(key); + cache.remove(key); } return oldOm; } @@ -330,7 +323,6 @@ public abstract class AbstractBaseManage * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ - @SuppressWarnings("unchecked") protected T putInstanceImpl(final Serializable key, final T om) throws TorqueException { @@ -344,19 +336,8 @@ public abstract class AbstractBaseManage T oldOm = null; if (cache != null) { - try - { - synchronized (this) - { - oldOm = (T) cache.get(key); - cache.put(key, om); - } - } - catch (CacheException ce) - { - throw new TorqueException - ("Could not cache due to internal JCS error", ce); - } + oldOm = cache.get(key); + cache.put(key, om); } return oldOm; } @@ -496,12 +477,22 @@ public abstract class AbstractBaseManage { if (Torque.getConfiguration().getBoolean(Torque.CACHE_KEY, false)) { + if (cache != null) + { + cache.dispose(); + } + if (groupCache != null) + { + groupCache.dispose(); + } cache = JCS.getInstance(getRegion()); - mrCache = new MethodResultCache(cache); + // FIXME: This is actually the same cache instance which will cause cross-effects + groupCache = JCS.getGroupCacheInstance(getRegion()); + mrCache = new MethodResultCache(groupCache); } else { - mrCache = new NoOpMethodResultCache(cache); + mrCache = new NoOpMethodResultCache(null); } } catch (CacheException e) Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/MethodResultCache.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/MethodResultCache.java?rev=1702477&r1=1702476&r2=1702477&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/MethodResultCache.java (original) +++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/MethodResultCache.java Fri Sep 11 14:21:12 2015 @@ -20,13 +20,11 @@ package org.apache.torque.manager; */ import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; +import org.apache.commons.jcs.access.GroupCacheAccess; +import org.apache.commons.jcs.access.exception.CacheException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.jcs.access.GroupCacheAccess; -import org.apache.jcs.access.exception.CacheException; import org.apache.torque.TorqueException; /** @@ -39,10 +37,7 @@ import org.apache.torque.TorqueException public class MethodResultCache { /** The underlying jcs cache. */ - private GroupCacheAccess jcsCache; - - /** All chache groups which are currently filled. */ - private Map<String, Object> groups; + private GroupCacheAccess<MethodCacheKey, Object> jcsCache; /** Logging */ private static Log log = LogFactory.getLog(MethodResultCache.class); @@ -52,10 +47,9 @@ public class MethodResultCache * * @param cache the cache instance to use */ - public MethodResultCache(GroupCacheAccess cache) + public MethodResultCache(GroupCacheAccess<MethodCacheKey, Object> cache) { this.jcsCache = cache; - groups = new HashMap<String, Object>(); } /** @@ -78,7 +72,6 @@ public class MethodResultCache try { jcsCache.clear(); - groups.clear(); } catch (CacheException ce) { @@ -93,10 +86,7 @@ public class MethodResultCache Object result = null; if (jcsCache != null) { - synchronized (this) - { - result = jcsCache.getFromGroup(key, key.getGroupKey()); - } + result = jcsCache.getFromGroup(key, key.getGroupKey()); } if (result != null && log.isDebugEnabled()) @@ -111,23 +101,15 @@ public class MethodResultCache protected Object putImpl(MethodCacheKey key, Object value) throws TorqueException { - //register the group, if this is the first occurrence String group = key.getGroupKey(); - if (!groups.containsKey(group)) - { - groups.put(group, null); - } Object old = null; if (jcsCache != null) { try { - synchronized (this) - { - old = jcsCache.getFromGroup(key, group); - jcsCache.putInGroup(key, group, value); - } + old = jcsCache.getFromGroup(key, group); + jcsCache.putInGroup(key, group, value); } catch (CacheException ce) { @@ -143,11 +125,8 @@ public class MethodResultCache Object old = null; if (jcsCache != null) { - synchronized (this) - { - old = jcsCache.getFromGroup(key, key.getGroupKey()); - jcsCache.remove(key, key.getGroupKey()); - } + old = jcsCache.getFromGroup(key, key.getGroupKey()); + jcsCache.removeFromGroup(key, key.getGroupKey()); } return old; } @@ -168,15 +147,8 @@ public class MethodResultCache T result = null; if (jcsCache != null) { - try - { - MethodCacheKey key = new MethodCacheKey(instanceOrClass, method, arg); - result = (T) getImpl(key); - } - catch (Exception e) - { - log.error("Problem getting object from cache", e); - } + MethodCacheKey key = new MethodCacheKey(instanceOrClass, method, arg); + result = (T) getImpl(key); } return result; @@ -198,7 +170,7 @@ public class MethodResultCache MethodCacheKey key = new MethodCacheKey(instanceOrClass, method, arg); putImpl(key, value); } - catch (Exception e) + catch (TorqueException e) { log.error("Problem putting object into cache", e); } @@ -215,17 +187,9 @@ public class MethodResultCache { if (jcsCache != null) { - try - { - MethodCacheKey key = new MethodCacheKey(instanceOrClass, method); - String groupName = key.getGroupKey(); - jcsCache.invalidateGroup(groupName); - groups.remove(groupName); - } - catch (Exception e) - { - log.error("Problem removing all", e); - } + MethodCacheKey key = new MethodCacheKey(instanceOrClass, method); + String groupName = key.getGroupKey(); + jcsCache.invalidateGroup(groupName); } } @@ -245,15 +209,8 @@ public class MethodResultCache T result = null; if (jcsCache != null) { - try - { - MethodCacheKey key = new MethodCacheKey(instanceOrClass, method, arg); - result = (T) removeImpl(key); - } - catch (Exception e) - { - log.error("Problem removing object from cache", e); - } + MethodCacheKey key = new MethodCacheKey(instanceOrClass, method, arg); + result = (T) removeImpl(key); } return result; Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/NoOpMethodResultCache.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/NoOpMethodResultCache.java?rev=1702477&r1=1702476&r2=1702477&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/NoOpMethodResultCache.java (original) +++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/manager/NoOpMethodResultCache.java Fri Sep 11 14:21:12 2015 @@ -21,7 +21,7 @@ package org.apache.torque.manager; import java.io.Serializable; -import org.apache.jcs.access.GroupCacheAccess; +import org.apache.commons.jcs.access.GroupCacheAccess; import org.apache.torque.TorqueException; /** @@ -33,7 +33,7 @@ import org.apache.torque.TorqueException public class NoOpMethodResultCache extends MethodResultCache { - public NoOpMethodResultCache(final GroupCacheAccess cache) + public NoOpMethodResultCache(final GroupCacheAccess<MethodCacheKey, Object> cache) { super(); } Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/manager/AbstractBaseManagerTest.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/manager/AbstractBaseManagerTest.java?rev=1702477&r1=1702476&r2=1702477&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/manager/AbstractBaseManagerTest.java (original) +++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/manager/AbstractBaseManagerTest.java Fri Sep 11 14:21:12 2015 @@ -19,6 +19,10 @@ package org.apache.torque.manager; * under the License. */ +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; + import junit.framework.TestCase; import org.apache.torque.BaseTestCase; @@ -28,11 +32,15 @@ import org.apache.torque.om.ObjectKey; import org.apache.torque.om.SimpleKey; public class AbstractBaseManagerTest extends TestCase + implements CacheListener<TestPersistent> { private static final ObjectKey TEST_PRIMARY_KEY = SimpleKey.keyFor("testID"); private static final String CACHE_REGION = "testCache1"; private TestManager manager; + private boolean addedObjectCalled; + private boolean refreshedObjectCalled; + static { try @@ -46,6 +54,22 @@ public class AbstractBaseManagerTest ext } } + /* CacheListener methods */ + public void addedObject(TestPersistent om) + { + this.addedObjectCalled = true; + } + + public void refreshedObject(TestPersistent om) + { + this.refreshedObjectCalled = true; + } + + public List<String> getInterestedFields() + { + return Arrays.asList("test1", "test2"); + } + /** * @see junit.framework.TestCase#setUp() */ @@ -109,6 +133,7 @@ public class AbstractBaseManagerTest ext TestPersistent test1 = manager.getOMInstance(TEST_PRIMARY_KEY, true); TestPersistent test2 = manager.cacheGet(TEST_PRIMARY_KEY); assertNotNull("Should be in cache", test2); + assertSame("Should be same instances", test1, test2); manager.clearImpl(); TestPersistent test3 = manager.cacheGet(TEST_PRIMARY_KEY); assertNull("Should not be in cache", test3); @@ -139,15 +164,24 @@ public class AbstractBaseManagerTest ext assertNotNull("Should have MethodResultCache", mrc); } -/* - public void testAddCacheListenerImpl() - { - fail("Not yet implemented"); - } - - public void testNotifyListeners() + @SuppressWarnings("unchecked") + public void testListeners() { - fail("Not yet implemented"); + manager.validFields = new HashMap<String, Object>(); + manager.validFields.put("test1", null); + manager.validFields.put("test2", null); + manager.addCacheListenerImpl(this); + this.addedObjectCalled = false; + this.refreshedObjectCalled = false; + TestPersistent test1 = new TestPersistent(); + TestPersistent test2 = new TestPersistent(); + manager.notifyListeners(manager.listenersMap.get("test1"), (TestPersistent)null, test1); + assertTrue("Should call addedObject", addedObjectCalled); + assertFalse("Should not call refreshedObject", refreshedObjectCalled); + this.addedObjectCalled = false; + this.refreshedObjectCalled = false; + manager.notifyListeners(manager.listenersMap.get("test2"), test2, test1); + assertFalse("Should not call addedObject", addedObjectCalled); + assertTrue("Should call refreshedObject", refreshedObjectCalled); } -*/ } Added: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/manager/MethodResultCacheTest.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/manager/MethodResultCacheTest.java?rev=1702477&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/manager/MethodResultCacheTest.java (added) +++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/manager/MethodResultCacheTest.java Fri Sep 11 14:21:12 2015 @@ -0,0 +1,122 @@ +package org.apache.torque.manager; + +/* + * 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 junit.framework.TestCase; + +import org.apache.torque.BaseTestCase; +import org.apache.torque.Torque; +import org.apache.torque.TorqueException; + +public class MethodResultCacheTest extends TestCase +{ + private static final String CACHE_REGION = "testCache1"; + private static final String TEST_METHOD1 = "testMethod1"; + private static final String TEST_METHOD2 = "testMethod2"; + private static final String TEST_ARG_ONE = "one"; + private static final String TEST_ARG_TWO = "two"; + private static final String TEST_ARG_THREE = "three"; + + private TestManager manager; + private MethodResultCache mrc; + + static + { + try + { + org.apache.torque.Torque.init(BaseTestCase.CONFIG_FILE); + org.apache.torque.Torque.getConfiguration().setProperty(Torque.CACHE_KEY, Boolean.TRUE); + } + catch (TorqueException e) + { + e.printStackTrace(); + } + } + + /** + * @see junit.framework.TestCase#setUp() + */ + @Override + protected void setUp() throws Exception + { + super.setUp(); + + this.manager = new TestManager(); + this.manager.setOMClass(TestPersistent.class); + this.manager.setRegion(CACHE_REGION); + this.mrc = this.manager.getMethodResultCache(); + } + + /** + * @see junit.framework.TestCase#tearDown() + */ + @Override + protected void tearDown() throws Exception + { + mrc.clear(); + super.tearDown(); + } + + public void testGetAndPut() + { + TestPersistent test1 = new TestPersistent(); + mrc.put(test1, TestPersistent.class, TEST_METHOD1, TEST_ARG_ONE, TEST_ARG_TWO); + TestPersistent test2 = mrc.get(TestPersistent.class, TEST_METHOD1, TEST_ARG_ONE, TEST_ARG_TWO); + assertNotNull("Should be in cache", test2); + assertSame("Should be same instance", test1, test2); + } + + public void testRemoveAll() + { + TestPersistent test1 = new TestPersistent(); + mrc.put(test1, TestPersistent.class, TEST_METHOD1, TEST_ARG_ONE, TEST_ARG_TWO); + assertNotNull("Should be in cache", mrc.get(TestPersistent.class, TEST_METHOD1, TEST_ARG_ONE, TEST_ARG_TWO)); + TestPersistent test2 = new TestPersistent(); + mrc.put(test2, TestPersistent.class, TEST_METHOD1, TEST_ARG_ONE, TEST_ARG_THREE); + assertNotNull("Should be in cache", mrc.get(TestPersistent.class, TEST_METHOD1, TEST_ARG_ONE, TEST_ARG_THREE)); + + TestPersistent test3 = new TestPersistent(); + mrc.put(test3, TestPersistent.class, TEST_METHOD2, TEST_ARG_ONE, TEST_ARG_TWO); + assertNotNull("Should be in cache", mrc.get(TestPersistent.class, TEST_METHOD2, TEST_ARG_ONE, TEST_ARG_TWO)); + TestPersistent test4 = new TestPersistent(); + mrc.put(test4, TestPersistent.class, TEST_METHOD2, TEST_ARG_ONE, TEST_ARG_THREE); + assertNotNull("Should be in cache", mrc.get(TestPersistent.class, TEST_METHOD2, TEST_ARG_ONE, TEST_ARG_THREE)); + + mrc.removeAll(TestPersistent.class, TEST_METHOD1); + + assertNull("Should not be in cache", mrc.get(TestPersistent.class, TEST_METHOD1, TEST_ARG_ONE, TEST_ARG_TWO)); + assertNull("Should not be in cache", mrc.get(TestPersistent.class, TEST_METHOD1, TEST_ARG_ONE, TEST_ARG_THREE)); + + assertNotNull("Should still be in cache", mrc.get(TestPersistent.class, TEST_METHOD2, TEST_ARG_ONE, TEST_ARG_TWO)); + assertNotNull("Should still be in cache", mrc.get(TestPersistent.class, TEST_METHOD2, TEST_ARG_ONE, TEST_ARG_THREE)); + } + + public void testRemove() + { + TestPersistent test1 = new TestPersistent(); + mrc.put(test1, TestPersistent.class, TEST_METHOD1, TEST_ARG_ONE, TEST_ARG_TWO); + assertNotNull("Should be in cache", mrc.get(TestPersistent.class, TEST_METHOD1, TEST_ARG_ONE, TEST_ARG_TWO)); + + TestPersistent test3 = mrc.remove(TestPersistent.class, TEST_METHOD1, TEST_ARG_ONE, TEST_ARG_TWO); + + assertNull("Should not be in cache", mrc.get(TestPersistent.class, TEST_METHOD1, TEST_ARG_ONE, TEST_ARG_TWO)); + assertSame("Should be same instance", test1, test3); + } +} Propchange: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/manager/MethodResultCacheTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: db/torque/torque4/trunk/torque-runtime/src/test/resources/cache.ccf URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/resources/cache.ccf?rev=1702477&r1=1702476&r2=1702477&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-runtime/src/test/resources/cache.ccf (original) +++ db/torque/torque4/trunk/torque-runtime/src/test/resources/cache.ccf Fri Sep 11 14:21:12 2015 @@ -18,11 +18,11 @@ # JCS Config for unit testing, just a simple memory only cache jcs.default= -jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes +jcs.default.cacheattributes=org.apache.commons.jcs.engine.CompositeCacheAttributes jcs.default.cacheattributes.MaxObjects=1000 -jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache +jcs.default.cacheattributes.MemoryCacheName=org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache jcs.region.testCache1= -jcs.region.testCache1.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes +jcs.region.testCache1.cacheattributes=org.apache.commons.jcs.engine.CompositeCacheAttributes jcs.region.testCache1.cacheattributes.MaxObjects=1000 -jcs.region.testCache1.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache +jcs.region.testCache1.cacheattributes.MemoryCacheName=org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache --------------------------------------------------------------------- To unsubscribe, e-mail: torque-dev-unsubscr...@db.apache.org For additional commands, e-mail: torque-dev-h...@db.apache.org