Author: aadamchik
Date: Sun Jul 30 18:42:33 2006
New Revision: 426997

URL: http://svn.apache.org/viewvc?rev=426997&view=rev
Log:
CAY-613 - OSCache provider enhancements, unit tests

Added:
    
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/access/DataContextQueryCachingOSCacheTst.java
    
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/cache/
    
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/cache/MockQueryCache.java
    
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/cache/MockQueryCacheFactory.java
Modified:
    
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/access/DataContext.java
    
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/cache/OSQueryCache.java
    
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/cache/OSQueryCacheFactory.java
    
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/access/DataDomainTst.java

Modified: 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/access/DataContext.java
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/access/DataContext.java?rev=426997&r1=426996&r2=426997&view=diff
==============================================================================
--- 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/access/DataContext.java
 (original)
+++ 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/access/DataContext.java
 Sun Jul 30 18:42:33 2006
@@ -293,6 +293,15 @@
         
         return queryCache;
     }
+    
+    /**
+     * Sets a QueryCache to be used for storing cached query results.
+     * 
+     * @since 3.0
+     */
+    public void setQueryCache(QueryCache queryCache) {
+        this.queryCache = queryCache;
+    }
 
     /**
      * Returns a map of user-defined properties associated with this 
DataContext.

Modified: 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/cache/OSQueryCache.java
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/cache/OSQueryCache.java?rev=426997&r1=426996&r2=426997&view=diff
==============================================================================
--- 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/cache/OSQueryCache.java
 (original)
+++ 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/cache/OSQueryCache.java
 Sun Jul 30 18:42:33 2006
@@ -27,7 +27,10 @@
 import com.opensymphony.oscache.general.GeneralCacheAdministrator;
 
 /**
- * A [EMAIL PROTECTED] QueryCache} implementation based on OpenSymphony 
OSCache.
+ * A [EMAIL PROTECTED] QueryCache} implementation based on OpenSymphony 
OSCache. Query cache
+ * parameters are initialized from "/oscache.properties" file per <a
+ * 
href="http://www.opensymphony.com/oscache/wiki/Configuration.html";>OSCache</a>
+ * documentation.
  * 
  * @since 3.0
  * @author Andrus Adamchik
@@ -39,7 +42,11 @@
     protected String cronExpression;
 
     public OSQueryCache(int refreshPeriod, String cronExpression) {
-        this.cache = new GeneralCacheAdministrator();
+        this(new GeneralCacheAdministrator(), refreshPeriod, cronExpression);
+    }
+
+    OSQueryCache(GeneralCacheAdministrator cache, int refreshPeriod, String 
cronExpression) {
+        this.cache = cache;
         this.refreshPeriod = refreshPeriod;
         this.cronExpression = cronExpression;
     }

Modified: 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/cache/OSQueryCacheFactory.java
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/cache/OSQueryCacheFactory.java?rev=426997&r1=426996&r2=426997&view=diff
==============================================================================
--- 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/cache/OSQueryCacheFactory.java
 (original)
+++ 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/main/java/org/apache/cayenne/cache/OSQueryCacheFactory.java
 Sun Jul 30 18:42:33 2006
@@ -18,15 +18,69 @@
  ****************************************************************/
 package org.apache.cayenne.cache;
 
+import java.util.Collections;
 import java.util.Map;
 
+import com.opensymphony.oscache.base.CacheEntry;
+import com.opensymphony.oscache.general.GeneralCacheAdministrator;
+
 /**
+ * A factory for the OSCache factory. A few extra properties in addition to 
what OSCache
+ * supports can be read from "/oscache.properties" file. Those are
+ * [EMAIL PROTECTED] OSQueryCacheFactory#OSCACHE_CRON_EXPRESSION_PROPERTY} and
+ * [EMAIL PROTECTED] OSQueryCacheFactory#OSCACHE_REFRESH_PERIOD_PROPERTY}.
+ * 
  * @since 3.0
  * @author Andrus Adamchik
  */
 public class OSQueryCacheFactory implements QueryCacheFactory {
 
+    public static final String OSCACHE_REFRESH_PERIOD_PROPERTY = 
"cayenne.OSQueryCacheFactory.refresh";
+    public static final String OSCACHE_CRON_EXPRESSION_PROPERTY = 
"cayenne.OSQueryCacheFactory.cron";
+
+    protected OSQueryCache createCache(
+            GeneralCacheAdministrator cache,
+            int refreshPeriod,
+            String cronExpression) {
+        return new OSQueryCache(cache, refreshPeriod, cronExpression);
+    }
+
     public QueryCache getQueryCache(Map properties) {
-        return null;
+
+        int refreshPeriod = CacheEntry.INDEFINITE_EXPIRY;
+        String cronExpression = null;
+
+        // this code loads oscache.properties. Use it as a failover for 
Cayenne-specific
+        // properties.
+        GeneralCacheAdministrator cache = new GeneralCacheAdministrator();
+
+        if (properties == null) {
+            properties = Collections.EMPTY_MAP;
+        }
+
+        Object refreshPeriodProperty = 
properties.get(OSCACHE_REFRESH_PERIOD_PROPERTY);
+        if (refreshPeriodProperty == null) {
+            refreshPeriodProperty = 
cache.getProperty(OSCACHE_REFRESH_PERIOD_PROPERTY);
+        }
+
+        if (refreshPeriodProperty != null) {
+            try {
+                refreshPeriod = 
Integer.parseInt(refreshPeriodProperty.toString());
+            }
+            catch (NumberFormatException e) {
+                // ignore
+            }
+        }
+
+        Object cronExpressionProperty = 
properties.get(OSCACHE_CRON_EXPRESSION_PROPERTY);
+        if (cronExpressionProperty == null) {
+            cronExpressionProperty = 
cache.getProperty(OSCACHE_CRON_EXPRESSION_PROPERTY);
+        }
+
+        if (cronExpressionProperty != null) {
+            cronExpression = cronExpressionProperty.toString();
+        }
+
+        return createCache(cache, refreshPeriod, cronExpression);
     }
 }

Added: 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/access/DataContextQueryCachingOSCacheTst.java
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/access/DataContextQueryCachingOSCacheTst.java?rev=426997&view=auto
==============================================================================
--- 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/access/DataContextQueryCachingOSCacheTst.java
 (added)
+++ 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/access/DataContextQueryCachingOSCacheTst.java
 Sun Jul 30 18:42:33 2006
@@ -0,0 +1,123 @@
+/*****************************************************************
+ *   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.
+ ****************************************************************/
+package org.apache.cayenne.access;
+
+import org.apache.cayenne.cache.OSQueryCacheFactory;
+
+public class DataContextQueryCachingOSCacheTst extends 
DataContextQueryCachingTst {
+
+    public void testLocalCacheDataObjectsNoRefresh() throws Exception {
+        runTest(new TestRun() {
+
+            public void execute() throws Exception {
+                DataContextQueryCachingOSCacheTst.super
+                        .testLocalCacheDataObjectsNoRefresh();
+            }
+        });
+    }
+
+    public void testLocalCacheDataObjectsRefresh() throws Exception {
+        runTest(new TestRun() {
+
+            public void execute() throws Exception {
+                DataContextQueryCachingOSCacheTst.super
+                        .testLocalCacheDataObjectsRefresh();
+            }
+        });
+    }
+
+    public void testLocalCacheDataRowsNoRefresh() throws Exception {
+        runTest(new TestRun() {
+
+            public void execute() throws Exception {
+                DataContextQueryCachingOSCacheTst.super
+                        .testLocalCacheDataRowsNoRefresh();
+            }
+        });
+    }
+
+    public void testLocalCacheDataRowsRefresh() throws Exception {
+        runTest(new TestRun() {
+
+            public void execute() throws Exception {
+                DataContextQueryCachingOSCacheTst.super
+                        .testLocalCacheDataRowsRefresh();
+            }
+        });
+    }
+
+    public void testLocalCacheRefreshObjectsRefresh() throws Exception {
+        runTest(new TestRun() {
+
+            public void execute() throws Exception {
+                DataContextQueryCachingOSCacheTst.super
+                        .testLocalCacheRefreshObjectsRefresh();
+            }
+        });
+    }
+
+    public void testSharedCacheDataObjectsNoRefresh() throws Exception {
+        runTest(new TestRun() {
+
+            public void execute() throws Exception {
+                DataContextQueryCachingOSCacheTst.super
+                        .testSharedCacheDataObjectsNoRefresh();
+            }
+        });
+    }
+
+    public void testSharedCacheDataRowsNoRefresh() throws Exception {
+        runTest(new TestRun() {
+
+            public void execute() throws Exception {
+                DataContextQueryCachingOSCacheTst.super
+                        .testSharedCacheDataRowsNoRefresh();
+            }
+        });
+    }
+
+    public void testSharedCacheDataRowsRefresh() throws Exception {
+        runTest(new TestRun() {
+
+            public void execute() throws Exception {
+                DataContextQueryCachingOSCacheTst.super
+                        .testSharedCacheDataRowsRefresh();
+            }
+        });
+    }
+
+    private void runTest(TestRun test) throws Exception {
+        context.queryCache = null;
+        getDomain().setQueryCacheFactory(new OSQueryCacheFactory());
+        getDomain().queryCache = null;
+        try {
+            test.execute();
+        }
+        finally {
+            getDomain().setQueryCacheFactory(null);
+            getDomain().queryCache = null;
+        }
+    }
+
+    interface TestRun {
+
+        void execute() throws Exception;
+    }
+
+}

Modified: 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/access/DataDomainTst.java
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/access/DataDomainTst.java?rev=426997&r1=426996&r2=426997&view=diff
==============================================================================
--- 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/access/DataDomainTst.java
 (original)
+++ 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/access/DataDomainTst.java
 Sun Jul 30 18:42:33 2006
@@ -25,6 +25,11 @@
 
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.DataChannel;
+import org.apache.cayenne.cache.MapQueryCacheFactory;
+import org.apache.cayenne.cache.MockQueryCache;
+import org.apache.cayenne.cache.MockQueryCacheFactory;
+import org.apache.cayenne.cache.QueryCache;
+import org.apache.cayenne.cache.QueryCacheFactory;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.unit.CayenneTestCase;
@@ -272,6 +277,27 @@
         }
     }
 
+    public void testQueryCache() {
+        DataDomain domain = new DataDomain("X");
+        QueryCache cache = domain.getQueryCache();
+        assertNotNull(cache);
+    }
+
+    public void testQueryCacheFactory() {
+        DataDomain domain = new DataDomain("X");
+        QueryCacheFactory qcFactory = domain.getQueryCacheFactory();
+        assertNotNull(qcFactory);
+        assertTrue(qcFactory instanceof MapQueryCacheFactory);
+
+        domain = new DataDomain("Y");
+        MockQueryCacheFactory f2 = new MockQueryCacheFactory();
+        domain.setQueryCacheFactory(f2);
+        assertSame(f2, domain.getQueryCacheFactory());
+        if (!(domain.getQueryCache() instanceof MockQueryCache)) {
+            fail("Unknown query cache created: " + domain.getQueryCache());
+        }
+    }
+
     public void testShutdown() {
         DataDomain domain = new DataDomain("X");
 
@@ -311,7 +337,7 @@
                 cacheShutdown[0] = true;
             }
         };
-        
+
         domain.setSharedSnapshotCache(cache);
         domain.shutdown();
 

Added: 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/cache/MockQueryCache.java
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/cache/MockQueryCache.java?rev=426997&view=auto
==============================================================================
--- 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/cache/MockQueryCache.java
 (added)
+++ 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/cache/MockQueryCache.java
 Sun Jul 30 18:42:33 2006
@@ -0,0 +1,48 @@
+/*****************************************************************
+ *   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.
+ ****************************************************************/
+package org.apache.cayenne.cache;
+
+import java.util.List;
+
+import org.apache.cayenne.query.QueryMetadata;
+
+
+public class MockQueryCache implements QueryCache {
+
+    public void clear() {
+    }
+
+    public List get(QueryMetadata metadata) {
+        return null;
+    }
+
+    public void put(QueryMetadata metadata, List results) {
+    }
+
+    public void remove(String key) {
+    }
+
+    public void removeGroup(String groupKey) {
+    }
+
+    public int size() {
+        return 0;
+    }
+
+}

Added: 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/cache/MockQueryCacheFactory.java
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/cache/MockQueryCacheFactory.java?rev=426997&view=auto
==============================================================================
--- 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/cache/MockQueryCacheFactory.java
 (added)
+++ 
incubator/cayenne/main/trunk/core/cayenne-jdk1.4-core/src/test/java/org/apache/cayenne/cache/MockQueryCacheFactory.java
 Sun Jul 30 18:42:33 2006
@@ -0,0 +1,29 @@
+/*****************************************************************
+ *   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.
+ ****************************************************************/
+package org.apache.cayenne.cache;
+
+import java.util.Map;
+
+public class MockQueryCacheFactory implements QueryCacheFactory {
+
+    public QueryCache getQueryCache(Map properties) {
+        return new MockQueryCache();
+    }
+
+}


Reply via email to