cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1412501 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/cache/EhCacheQueryCache.java test/java/org/apache/cayenne/cache/EhCacheQueryCacheTest.java
Date Thu, 22 Nov 2012 11:09:27 GMT
Author: aadamchik
Date: Thu Nov 22 11:09:26 2012
New Revision: 1412501

URL: http://svn.apache.org/viewvc?rev=1412501&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

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

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java?rev=1412501&r1=1412500&r2=1412501&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java
Thu Nov 22 11:09:26 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/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/cache/EhCacheQueryCacheTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/cache/EhCacheQueryCacheTest.java?rev=1412501&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/cache/EhCacheQueryCacheTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/cache/EhCacheQueryCacheTest.java
Thu Nov 22 11:09:26 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));
+    }
+}



Mime
View raw message