You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jc...@apache.org on 2005/11/19 19:47:14 UTC

svn commit: r345668 - in /jakarta/commons/sandbox/proxy/trunk/src: java/org/apache/commons/proxy/provider/ test/org/apache/commons/proxy/provider/ test/org/apache/commons/proxy/provider/cache/

Author: jcarman
Date: Sat Nov 19 10:47:10 2005
New Revision: 345668

URL: http://svn.apache.org/viewcvs?rev=345668&view=rev
Log:
CachedProvider now supports a CacheEvictionListener.

Added:
    jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/AbstractCacheTestCase.java   (with props)
    jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestSimpleCache.java   (with props)
    jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestThreadLocalCache.java   (with props)
Modified:
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CachedProvider.java
    jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCachedProvider.java

Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CachedProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CachedProvider.java?rev=345668&r1=345667&r2=345668&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CachedProvider.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CachedProvider.java Sat Nov 19 10:47:10 2005
@@ -18,10 +18,11 @@
 
 import org.apache.commons.proxy.ObjectProvider;
 import org.apache.commons.proxy.provider.cache.Cache;
+import org.apache.commons.proxy.provider.cache.CacheEvictionListener;
 
 /**
  * Uses a {@link Cache} to store its target object which is provided by an <code>inner</code> object provider.
- * 
+ *
  * @author James Carman
  * @version 1.0
  */
@@ -31,8 +32,8 @@
 //----------------------------------------------------------------------------------------------------------------------
 // Fields
 //----------------------------------------------------------------------------------------------------------------------
-
     private final Object cacheKey = new Object();
+    private CacheEvictionListener evictionListener;
     private Cache cache;
 
 //----------------------------------------------------------------------------------------------------------------------
@@ -54,7 +55,14 @@
         if( object == null )
         {
             object = super.getObject();
-            cache.storeObject( cacheKey, object );
+            if( evictionListener == null )
+            {
+                cache.storeObject( cacheKey, object );
+            }
+            else
+            {
+                cache.storeObject( cacheKey, object, evictionListener );
+            }
         }
         return object;
     }
@@ -62,6 +70,11 @@
 //----------------------------------------------------------------------------------------------------------------------
 // Getter/Setter Methods
 //----------------------------------------------------------------------------------------------------------------------
+
+    public void setEvictionListener( CacheEvictionListener evictionListener )
+    {
+        this.evictionListener = evictionListener;
+    }
 
     public void setCache( Cache cache )
     {

Modified: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCachedProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCachedProvider.java?rev=345668&r1=345667&r2=345668&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCachedProvider.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCachedProvider.java Sat Nov 19 10:47:10 2005
@@ -18,12 +18,33 @@
 import junit.framework.TestCase;
 import org.apache.commons.proxy.provider.cache.SimpleCache;
 import org.apache.commons.proxy.provider.cache.ThreadLocalCache;
+import org.apache.commons.proxy.provider.cache.CacheEvictionListener;
+import org.apache.commons.proxy.provider.cache.CacheEvictionEvent;
 import org.apache.commons.proxy.util.Echo;
 import org.apache.commons.proxy.util.EchoImpl;
 import EDU.oswego.cs.dl.util.concurrent.CountDown;
 
 public class TestCachedProvider extends TestCase
 {
+//----------------------------------------------------------------------------------------------------------------------
+// Other Methods
+//----------------------------------------------------------------------------------------------------------------------
+
+    public void testWithEvictionListener()
+    {
+        final SimpleCache cache = new SimpleCache();
+        EchoImpl echo = new EchoImpl();
+        final CountingProvider counter = new CountingProvider( new ConstantProvider( echo ) );
+                final CachedProvider provider = new CachedProvider( counter );
+        provider.setCache( cache );
+        final CacheEvictionTester tester = new CacheEvictionTester();
+        provider.setEvictionListener( tester );
+        provider.getObject();
+        cache.clearCache();
+        assertNotNull( tester.event );
+        assertEquals( echo, tester.event.getEvictedObject() );
+    }
+
     public void testWithSimpleCache()
     {
         final CountingProvider counter = new CountingProvider( new ConstantProvider( new EchoImpl() ) );
@@ -57,5 +78,19 @@
         }
         latch.acquire();
         assertEquals( 10, counter.getCount() );
+    }
+
+//----------------------------------------------------------------------------------------------------------------------
+// Inner Classes
+//----------------------------------------------------------------------------------------------------------------------
+
+    private static class CacheEvictionTester implements CacheEvictionListener
+    {
+        private CacheEvictionEvent event;
+
+        public void objectEvicted( CacheEvictionEvent e )
+        {
+            event = e;
+        }
     }
 }

Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/AbstractCacheTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/AbstractCacheTestCase.java?rev=345668&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/AbstractCacheTestCase.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/AbstractCacheTestCase.java Sat Nov 19 10:47:10 2005
@@ -0,0 +1,71 @@
+/* $Id$
+ *
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+package org.apache.commons.proxy.provider.cache;
+
+import junit.framework.TestCase;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public abstract class AbstractCacheTestCase extends TestCase
+{
+//----------------------------------------------------------------------------------------------------------------------
+// Abstract Methods
+//----------------------------------------------------------------------------------------------------------------------
+
+    protected abstract Cache createCache();
+
+//----------------------------------------------------------------------------------------------------------------------
+// Other Methods
+//----------------------------------------------------------------------------------------------------------------------
+
+    public void testEviction()
+    {
+        final Cache cache = createCache();
+        final CacheEvictionTester tester = new CacheEvictionTester();
+        cache.storeObject( "hello", "world", tester );
+        cache.clearCache();
+        assertNull( cache.retrieveObject( "hello" ) );
+    }
+
+    public void testEvictionWithListener()
+    {
+        final Cache cache = createCache();
+        final CacheEvictionTester tester = new CacheEvictionTester();
+        cache.storeObject( "hello", "world", tester );
+        cache.clearCache();
+        assertNull( cache.retrieveObject( "hello" ) );
+        assertNotNull( tester.event );
+        assertEquals( "world", tester.event.getEvictedObject() );
+    }
+
+//----------------------------------------------------------------------------------------------------------------------
+// Inner Classes
+//----------------------------------------------------------------------------------------------------------------------
+
+    private static class CacheEvictionTester implements CacheEvictionListener
+    {
+        private CacheEvictionEvent event;
+
+        public void objectEvicted( CacheEvictionEvent e )
+        {
+            event = e;
+        }
+    }
+}
+

Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/AbstractCacheTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/AbstractCacheTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestSimpleCache.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestSimpleCache.java?rev=345668&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestSimpleCache.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestSimpleCache.java Sat Nov 19 10:47:10 2005
@@ -0,0 +1,24 @@
+/* $Id$
+ *
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+package org.apache.commons.proxy.provider.cache;
+public class TestSimpleCache extends AbstractCacheTestCase
+{
+    protected Cache createCache()
+    {
+        return new SimpleCache();
+    }
+}
\ No newline at end of file

Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestSimpleCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestSimpleCache.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestThreadLocalCache.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestThreadLocalCache.java?rev=345668&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestThreadLocalCache.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestThreadLocalCache.java Sat Nov 19 10:47:10 2005
@@ -0,0 +1,24 @@
+/* $Id$
+ *
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+package org.apache.commons.proxy.provider.cache;
+public class TestThreadLocalCache extends AbstractCacheTestCase
+{
+    protected Cache createCache()
+    {
+        return new ThreadLocalCache();
+    }
+}
\ No newline at end of file

Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestThreadLocalCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/cache/TestThreadLocalCache.java
------------------------------------------------------------------------------
    svn:keywords = Id



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org