You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2012/11/22 12:10:41 UTC

svn commit: r1412504 - in /cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/cache/EhCacheQueryCache.java test/java/org/apache/cayenne/cache/EhCacheQueryCacheTest.java

Author: aadamchik
Date: Thu Nov 22 11:10:41 2012
New Revision: 1412504

URL: http://svn.apache.org/viewvc?rev=1412504&view=rev
Log:
CAY-1774 EhCacheQueryCache.get(QueryMetadata, QueryCacheEntryFactory) returns null if EhCache instance for group is not present

* 'testGet_WithFactory_WithCacheGroups' reproduces the problem
* fixing the issue

(cherry picked from commit 4a697979100d65d6c1cf9ad752b2ee4fe392bc95)

Added:
    cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/cache/EhCacheQueryCacheTest.java
Modified:
    cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java

Modified: cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java?rev=1412504&r1=1412503&r2=1412504&view=diff
==============================================================================
--- cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java (original)
+++ cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java Thu Nov 22 11:10:41 2012
@@ -103,66 +103,62 @@ public class EhCacheQueryCache implement
         if (key == null) {
             return null;
         }
-        
+
         Ehcache cache = null;
         Element result = null;
         String[] groupNames = metadata.getCacheGroups();
         if (groupNames != null && groupNames.length > 0) {
-            cache = cacheManager.getCache(groupNames[0]);
-            if (cache == null) {
-                return null;
-            }
-            else {
-                result = cache.get(key);
-            }
+
             if (groupNames.length > 1) {
-                logger.warn("multiple cache groups per key: " + key);
+                logger.warn("multiple cache groups per key '" + key + "', ignoring all but the first one: "
+                        + groupNames[0]);
             }
-        }
-        else {
+
+            // create empty cache for cache group here, as we have a factory to
+            // create an object, and should never ever return null from this
+            // method
+            cache = cacheManager.addCacheIfAbsent(groupNames[0]);
+            result = cache.get(key);
+
+        } else {
             cache = getDefaultCache();
             result = cache.get(key);
         }
 
         if (result != null) {
-            return (List)result.getObjectValue();
+            return (List) result.getObjectValue();
         }
 
         // if no result in cache locking the key to write
         // and putting it to the cache
         cache.acquireWriteLockOnKey(key);
         try {
-            
+
             // trying to read from cache again in case of
             // someone else put it to the cache before us
             List list = get(metadata);
-            
+
             if (list == null) {
-                
-                // if not succeeded  in reading again putting
+
+                // if not succeeded in reading again putting
                 // object to the cache ourselves
                 Object noResult = factory.createObject();
                 if (!(noResult instanceof List)) {
                     if (noResult == null) {
-                        throw new CayenneRuntimeException("Null object created: "
-                                + metadata.getCacheKey());
-                    }
-                    else {
-                        throw new CayenneRuntimeException(
-                                "Invalid query result, expected List, got "
+                        throw new CayenneRuntimeException("Null object created: " + metadata.getCacheKey());
+                    } else {
+                        throw new CayenneRuntimeException("Invalid query result, expected List, got "
                                 + noResult.getClass().getName());
                     }
                 }
 
-                list = (List)noResult;
+                list = (List) noResult;
                 put(metadata, list);
                 return list;
-            }
-            else {
+            } else {
                 return list;
             }
-        }
-        finally {
+        } finally {
             cache.releaseWriteLockOnKey(key);
         }
     }

Added: cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/cache/EhCacheQueryCacheTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/cache/EhCacheQueryCacheTest.java?rev=1412504&view=auto
==============================================================================
--- cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/cache/EhCacheQueryCacheTest.java (added)
+++ cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/cache/EhCacheQueryCacheTest.java Thu Nov 22 11:10:41 2012
@@ -0,0 +1,100 @@
+/*****************************************************************
+ *   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 static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+import net.sf.ehcache.CacheManager;
+
+import org.apache.cayenne.query.QueryMetadata;
+
+public class EhCacheQueryCacheTest extends TestCase {
+
+    private CacheManager cacheManager;
+
+    @Override
+    protected void setUp() throws Exception {
+        cacheManager = new CacheManager();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        cacheManager.shutdown();
+    }
+
+    public void testGet() {
+
+        EhCacheQueryCache cache = new EhCacheQueryCache(cacheManager);
+
+        QueryMetadata md = mock(QueryMetadata.class);
+        when(md.getCacheKey()).thenReturn("k1");
+
+        assertNull(cache.get(md));
+
+        List<?> results = new ArrayList<Object>();
+        cache.put(md, results);
+        assertSame(results, cache.get(md));
+    }
+
+    public void testGet_WithFactory() {
+
+        EhCacheQueryCache cache = new EhCacheQueryCache(cacheManager);
+
+        Object[] lists = new Object[] { new ArrayList<Object>(), new ArrayList<Object>(), new ArrayList<Object>() };
+        QueryCacheEntryFactory factory = mock(QueryCacheEntryFactory.class);
+        when(factory.createObject()).thenReturn(lists[0], lists[1], lists[2]);
+
+        QueryMetadata md = mock(QueryMetadata.class);
+        when(md.getCacheKey()).thenReturn("k1");
+
+        assertEquals(lists[0], cache.get(md, factory));
+        assertEquals(lists[0], cache.get(md, factory));
+        assertEquals(lists[0], cache.get(md, factory));
+
+        List<?> results = new ArrayList<Object>();
+        cache.put(md, results);
+        assertSame(results, cache.get(md));
+    }
+
+    public void testGet_WithFactory_WithCacheGroups() {
+
+        EhCacheQueryCache cache = new EhCacheQueryCache(cacheManager);
+
+        Object[] lists = new Object[] { new ArrayList<Object>(), new ArrayList<Object>(), new ArrayList<Object>() };
+        QueryCacheEntryFactory factory = mock(QueryCacheEntryFactory.class);
+        when(factory.createObject()).thenReturn(lists[0], lists[1], lists[2]);
+
+        QueryMetadata md = mock(QueryMetadata.class);
+        when(md.getCacheKey()).thenReturn("k1");
+        when(md.getCacheGroups()).thenReturn(new String[] { "cg1" });
+
+        assertEquals(lists[0], cache.get(md, factory));
+        assertEquals(lists[0], cache.get(md, factory));
+        assertEquals(lists[0], cache.get(md, factory));
+
+        List<?> results = new ArrayList<Object>();
+        cache.put(md, results);
+        assertSame(results, cache.get(md));
+    }
+}