Return-Path: X-Original-To: apmail-cayenne-commits-archive@www.apache.org Delivered-To: apmail-cayenne-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 44B8BDE7E for ; Thu, 22 Nov 2012 11:09:52 +0000 (UTC) Received: (qmail 95631 invoked by uid 500); 22 Nov 2012 11:09:52 -0000 Delivered-To: apmail-cayenne-commits-archive@cayenne.apache.org Received: (qmail 95613 invoked by uid 500); 22 Nov 2012 11:09:52 -0000 Mailing-List: contact commits-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cayenne.apache.org Delivered-To: mailing list commits@cayenne.apache.org Received: (qmail 95602 invoked by uid 99); 22 Nov 2012 11:09:51 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 22 Nov 2012 11:09:51 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 22 Nov 2012 11:09:48 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 61CEE23888CD for ; Thu, 22 Nov 2012 11:09:27 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@cayenne.apache.org From: aadamchik@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121122110927.61CEE23888CD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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(); + cache.put(md, results); + assertSame(results, cache.get(md)); + } + + public void testGet_WithFactory() { + + EhCacheQueryCache cache = new EhCacheQueryCache(cacheManager); + + Object[] lists = new Object[] { new ArrayList(), new ArrayList(), new ArrayList() }; + 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(); + 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(), new ArrayList(), new ArrayList() }; + 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(); + cache.put(md, results); + assertSame(results, cache.get(md)); + } +}