Permalink
Browse files

Merge pull request #41 from sslavic/SPR-7843

* SPR-7843:
  Predict specific object type in EhCacheFactoryBean
  • Loading branch information...
2 parents b2291ff + 0690b58 commit f5042d1928f96967b6fe7dec198b55eb4746baa1 @cbeams cbeams committed Feb 21, 2012
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2011 the original author or authors.
+ * Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
import net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache;
import net.sf.ehcache.event.CacheEventListener;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -398,8 +399,27 @@ public Ehcache getObject() {
return this.cache;
}
+ /**
+ * Predict the particular {@code Ehcache} implementation that will be returned from
+ * {@link #getObject()} based on logic in {@link #createCache()} and
+ * {@link #decorateCache(Ehcache)} as orchestrated by {@link #afterPropertiesSet()}.
+ */
public Class<? extends Ehcache> getObjectType() {
- return (this.cache != null ? this.cache.getClass() : Ehcache.class);
+ if (this.cache != null) {
+ return this.cache.getClass();
+ }
+ if (this.cacheEntryFactory != null) {
+ if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) {
+ return UpdatingSelfPopulatingCache.class;
+ }
+ else {
+ return SelfPopulatingCache.class;
+ }
+ }
+ if (this.blocking) {
+ return BlockingCache.class;
+ }
+ return Cache.class;
}
public boolean isSingleton() {
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2009 the original author or authors.
+ * Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.cache.ehcache;
import junit.framework.TestCase;
+
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
@@ -82,7 +83,8 @@ private void doTestEhCacheFactoryBean(boolean useCacheManagerFb) throws Exceptio
EhCacheManagerFactoryBean cacheManagerFb = null;
try {
EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
- assertEquals(Ehcache.class, cacheFb.getObjectType());
+ Class<? extends Ehcache> objectType = cacheFb.getObjectType();
+ assertTrue(Ehcache.class.isAssignableFrom(objectType));
assertTrue("Singleton property", cacheFb.isSingleton());
if (useCacheManagerFb) {
cacheManagerFb = new EhCacheManagerFactoryBean();
@@ -94,6 +96,8 @@ private void doTestEhCacheFactoryBean(boolean useCacheManagerFb) throws Exceptio
cacheFb.setCacheName("myCache1");
cacheFb.afterPropertiesSet();
cache = (Cache) cacheFb.getObject();
+ Class<? extends Ehcache> objectType2 = cacheFb.getObjectType();
+ assertSame(objectType, objectType2);
CacheConfiguration config = cache.getCacheConfiguration();
assertEquals("myCache1", cache.getName());
if (useCacheManagerFb){
@@ -166,6 +170,7 @@ public void testEhCacheFactoryBeanWithBlockingCache() throws Exception {
cacheFb.setCacheManager(cm);
cacheFb.setCacheName("myCache1");
cacheFb.setBlocking(true);
+ assertEquals(cacheFb.getObjectType(), BlockingCache.class);
cacheFb.afterPropertiesSet();
Ehcache myCache1 = cm.getEhcache("myCache1");
assertTrue(myCache1 instanceof BlockingCache);
@@ -188,6 +193,7 @@ public Object createEntry(Object key) throws Exception {
return key;
}
});
+ assertEquals(cacheFb.getObjectType(), SelfPopulatingCache.class);
cacheFb.afterPropertiesSet();
Ehcache myCache1 = cm.getEhcache("myCache1");
assertTrue(myCache1 instanceof SelfPopulatingCache);
@@ -213,6 +219,7 @@ public Object createEntry(Object key) throws Exception {
public void updateEntryValue(Object key, Object value) throws Exception {
}
});
+ assertEquals(cacheFb.getObjectType(), UpdatingSelfPopulatingCache.class);
cacheFb.afterPropertiesSet();
Ehcache myCache1 = cm.getEhcache("myCache1");
assertTrue(myCache1 instanceof UpdatingSelfPopulatingCache);

0 comments on commit f5042d1

Please sign in to comment.