openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r810197 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/ openjpa-persistence-jdbc/src/test/resources/META-INF/
Date Tue, 01 Sep 2009 19:48:09 GMT
Author: mikedd
Date: Tue Sep  1 19:48:09 2009
New Revision: 810197

URL: http://svn.apache.org/viewvc?rev=810197&view=rev
Log:
OPENJPA-1279: 
Adding testcases and fixing cache exclusion logic in DataCacheStoreManager

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeEmpty.java
  (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeUnspecified.java
  (with props)
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/AbstractCacheModeTestCase.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/caching-persistence.xml

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java?rev=810197&r1=810196&r2=810197&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java
Tue Sep  1 19:48:09 2009
@@ -336,9 +336,9 @@
     public boolean initialize(OpenJPAStateManager sm, PCState state, FetchConfiguration fetch,
Object edata) {
         boolean rval; 
         DataCache cache = sm.getMetaData().getDataCache();
+        boolean updateCache = _ctx.getCacheStoreMode() != DataCacheStoreMode.BYPASS &&
_ctx.getPopulateDataCache();
         if (cache == null || sm.isEmbedded() || _ctx.getCacheRetrieveMode() == DataCacheRetrieveMode.BYPASS
             || _ctx.getCacheStoreMode() == DataCacheStoreMode.REFRESH) {
-            // save the return value and return later in case we need to update the cache)
             rval = super.initialize(sm, state, fetch, edata);
         }
 
@@ -349,18 +349,17 @@
                 //### addressed for bug 511
                 sm.initialize(data.getType(), state);
                 data.load(sm, fetch, edata);
-                return true;
+                // no need to update the cache. 
+                updateCache = false;
+                rval = true;
+            } else {
+                // initialize from store manager
+                rval = super.initialize(sm, state, fetch, edata);
             }
-
-            // initialize from store manager
-            if (!super.initialize(sm, state, fetch, edata)) {
-                return false;
-            }
-            rval = true; // same as rval = super.initialize(...)
         }
 
-        // update the cache if configured appropriately. 
-        if (_ctx.getCacheStoreMode() == DataCacheStoreMode.REFRESH && _ctx.getPopulateDataCache())
{
+        if (cache != null && (rval && updateCache)) {
+            // update cache if the result came from the database and configured to store
or refresh the cache.
             cacheStateManager(cache, sm);
         }
         return rval;

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/AbstractCacheModeTestCase.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/AbstractCacheModeTestCase.java?rev=810197&r1=810196&r2=810197&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/AbstractCacheModeTestCase.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/AbstractCacheModeTestCase.java
Tue Sep  1 19:48:09 2009
@@ -25,6 +25,7 @@
 import javax.persistence.CacheStoreMode;
 import javax.persistence.EntityManager;
 
+import org.apache.openjpa.jdbc.meta.ClassMapping;
 import org.apache.openjpa.lib.jdbc.AbstractJDBCListener;
 import org.apache.openjpa.lib.jdbc.JDBCEvent;
 import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
@@ -393,4 +394,53 @@
             entityManagerStoreModeTest(CacheStoreMode.REFRESH, CacheStoreMode.REFRESH, true,
true, 1);
         }
     }
+    
+    public void testResultsFromQueryAreInCache() { 
+        // clear cache
+        getEntityManagerFactory().getStoreCache().evictAll();
+        getEntityManagerFactory().getQueryResultCache().evictAll();
+
+        EntityManager em = getEntityManagerFactory().createEntityManager();
+        String query; 
+        for(Class<?> cls : persistentTypes) {
+            query = "Select e from " + getAlias(cls) + " e";
+            List<?> res = em.createQuery(query).getResultList();
+            assertNotNull(String.format("Expected to find some results when running query
%s",query), res);
+            assertTrue(String.format("Expected more than 0 results running query %s",query),res.size()
!= 0 ) ;
+        }
+        for(Class<?> cls : getExpectedInCache()) { 
+            assertCached(getEntityManagerFactory().getCache(), cls, 1, true);
+        }
+        
+        for(Class<?> cls : getExpectedNotInCache()) { 
+            assertCached(getEntityManagerFactory().getCache(), cls, 1, false);
+        }
+        em.close();
+    }
+    
+    public void testResultsFromFindAreInCache() { 
+        // clear cache
+        getEntityManagerFactory().getStoreCache().evictAll();
+        getEntityManagerFactory().getQueryResultCache().evictAll();
+
+        EntityManager em = getEntityManagerFactory().createEntityManager();
+        for(Class<?> cls : persistentTypes) {
+            assertNotNull(String.format("Expected to find %s::%d from database or from cache",
cls, 1),em.find(cls, 1));
+        }
+        for(Class<?> cls : getExpectedInCache()) { 
+            assertCached(getEntityManagerFactory().getCache(), cls, 1, true);
+        }
+        
+        for(Class<?> cls : getExpectedNotInCache()) { 
+            assertCached(getEntityManagerFactory().getCache(), cls, 1, false);
+        }
+        em.close();
+    }
+    
+    private String getAlias(Class<?> cls) {
+        ClassMapping mapping =
+            (ClassMapping) getEntityManagerFactory().getConfiguration().getMetaDataRepositoryInstance().getMetaData(
+                cls, null, true);
+        return mapping.getTypeAlias();
+    }
 }

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeEmpty.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeEmpty.java?rev=810197&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeEmpty.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeEmpty.java
Tue Sep  1 19:48:09 2009
@@ -0,0 +1,81 @@
+/*
+ * 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.openjpa.persistence.cache.jpa;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.Cache;
+
+import org.apache.openjpa.lib.jdbc.JDBCListener;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+
+public class TestCacheModeEmpty extends AbstractCacheModeTestCase {
+    private static OpenJPAEntityManagerFactorySPI emf = null;
+    private static Cache cache = null;
+    private static List<String> sql = new ArrayList<String>();
+    private static JDBCListener listener;
+    
+    private static Class<?>[] expectedInCache = persistentTypes;
+    private static Class<?>[] expectedNotInCache = {};
+
+    @Override
+    public OpenJPAEntityManagerFactorySPI getEntityManagerFactory() {
+        if (emf == null) {
+            emf = createEntityManagerFactory("cache-mode-empty");
+            assertNotNull(emf);
+            cache = emf.getCache();
+            assertNotNull(cache);
+        }
+        return emf;
+    }
+
+    public JDBCListener getListener() {
+        if (listener == null) {
+            listener = new Listener();
+        }
+        return listener;
+    }
+    
+    public List<String> getSql() { 
+        return sql;
+    }
+    
+    public void testCacheables() {
+        assertCacheables(cache, true);
+    }
+
+    public void testUncacheables() {
+        assertUncacheables(cache, true);
+    }
+
+    public void testUnspecified() {
+        assertUnspecified(cache, true);
+    }
+
+    @Override
+    protected Class<?>[] getExpectedInCache() {
+        return expectedInCache;
+    }
+
+    @Override
+    protected Class<?>[] getExpectedNotInCache() {
+        return expectedNotInCache;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeEmpty.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeUnspecified.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeUnspecified.java?rev=810197&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeUnspecified.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeUnspecified.java
Tue Sep  1 19:48:09 2009
@@ -0,0 +1,81 @@
+/*
+ * 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.openjpa.persistence.cache.jpa;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.Cache;
+
+import org.apache.openjpa.lib.jdbc.JDBCListener;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+
+public class TestCacheModeUnspecified extends AbstractCacheModeTestCase {
+    private static OpenJPAEntityManagerFactorySPI emf = null;
+    private static Cache cache = null;
+    private static List<String> sql = new ArrayList<String>();
+    private static JDBCListener listener;
+    
+    private static Class<?>[] expectedInCache = persistentTypes;
+    private static Class<?>[] expectedNotInCache = {};
+
+    @Override
+    public OpenJPAEntityManagerFactorySPI getEntityManagerFactory() {
+        if (emf == null) {
+            emf = createEntityManagerFactory("cache-mode-unspecified");
+            assertNotNull(emf);
+            cache = emf.getCache();
+            assertNotNull(cache);
+        }
+        return emf;
+    }
+
+    public JDBCListener getListener() {
+        if (listener == null) {
+            listener = new Listener();
+        }
+        return listener;
+    }
+    
+    public List<String> getSql() { 
+        return sql;
+    }
+    
+    public void testCacheables() {
+        assertCacheables(cache, true);
+    }
+
+    public void testUncacheables() {
+        assertUncacheables(cache, true);
+    }
+
+    public void testUnspecified() {
+        assertUnspecified(cache, true);
+    }
+
+    @Override
+    protected Class<?>[] getExpectedInCache() {
+        return expectedInCache;
+    }
+
+    @Override
+    protected Class<?>[] getExpectedNotInCache() {
+        return expectedNotInCache;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeUnspecified.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/caching-persistence.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/caching-persistence.xml?rev=810197&r1=810196&r2=810197&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/caching-persistence.xml
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/caching-persistence.xml
Tue Sep  1 19:48:09 2009
@@ -67,4 +67,12 @@
             <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"
/>
         </properties>
     </persistence-unit>
+        <persistence-unit name="cache-mode-empty">
+        <mapping-file>META-INF/caching-orm.xml</mapping-file>
+        <properties>
+            <!-- Connection info is passed in via system properties -->
+            <!-- Cache configuration ie openjpa.DataCache is passed in at EMF initialization
-->
+            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"
/>
+        </properties>
+    </persistence-unit>
 </persistence>



Mime
View raw message