IService, ServiceBase : added method findWithoutFlush
authorCherian Mathew <c.mathew@bgbm.org>
Thu, 26 Jun 2014 15:03:58 +0000 (15:03 +0000)
committerCherian Mathew <c.mathew@bgbm.org>
Thu, 26 Jun 2014 15:03:58 +0000 (15:03 +0000)
CdmCacher, CdmDaoCacher : split CdmCacher class to sub class which uses dao to access the data
MockCdmCacher : added mock cdm cacher for tests
TaxonNodeServiceImplTest, FeatureNodeServiceImplTest : removed duplicate @SpringApplicationContext annotation
IdentifiableServiceBaseTest, TaxonServiceSearchTest : reverted @Ignore on failing tests
TaxonServiceSearchTest : added mock cdm cacher
SecurityTest : removed duplicate PlatformTransactionManager autowiring
CdmCacherTest : extended test

12 files changed:
.gitattributes
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/cache/CdmCacher.java
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/cache/CdmDaoCacher.java [new file with mode: 0644]
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IService.java
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/ServiceBase.java
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/cache/CdmCacherTest.java
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/cache/MockCdmCacher.java [new file with mode: 0644]
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/FeatureNodeServiceImplTest.java
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/IdentifiableServiceBaseTest.java
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/SecurityTest.java
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/TaxonNodeServiceImplTest.java
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/TaxonServiceSearchTest.java

index 7bfc58b8a1a4445994080904ef4103aedf07d5a1..8d075c2a054734a8c8ce9c0233c1dc5ddfc96d03 100644 (file)
@@ -2139,6 +2139,7 @@ cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/application/ICdmApplicationCo
 cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/application/MonitoredGenericApplicationContext.java -text
 cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/application/MonitoredListableBeanFactory.java -text
 cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/cache/CdmCacher.java -text
+cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/cache/CdmDaoCacher.java -text
 cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/conversation/ConversationHolder.java -text
 cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/conversation/ConversationHolderMock.java -text
 cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/conversation/IConversationEnabled.java -text
@@ -2320,6 +2321,7 @@ cdmlib-services/src/main/resources/wsdl/LSIDDataServiceSOAPBindings.wsdl -text
 cdmlib-services/src/main/resources/wsdl/LSIDPortTypes.wsdl -text
 cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/application/FirstDataInserterTest.java -text
 cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/cache/CdmCacherTest.java -text
+cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/cache/MockCdmCacher.java -text
 cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/facade/DerivedUnitFacadeCacheStrategyTest.java -text
 cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/facade/DerivedUnitFacadeFieldUnitCacheStrategyTest.java -text
 cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/facade/DerivedUnitFacadeTest.java -text
index 4c12d5d01f8e18d6ad54fb2acbd179baeabb8336..3ce0c66c39f03b1f5bff0bd635d949ac8ad51e78 100644 (file)
@@ -17,23 +17,23 @@ import eu.etaxonomy.cdm.api.service.ITermService;
 import eu.etaxonomy.cdm.model.ICdmCacher;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
+import eu.etaxonomy.cdm.persistence.dao.common.ICdmGenericDao;
+import eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl;
 
 /**
+ * CDM Entity Cacher class based on EhCache.
+ * 
  * @author cmathew
  *
  * @param <T>
  */
-@Component
-public class CdmCacher<T extends CdmBase> implements ICdmCacher<T> {
+
+public abstract class CdmCacher<T extends CdmBase> implements ICdmCacher<T> {
                        
-       @Autowired
-       private ITermService termService;
-       
-       private static final String DEFAULT_CACHE_NAME = "defaultCache";
-       
-       private static final String DEFAULT_CACHE_MGR_NAME = "defaultCacheManager";
 
        
+       private static final String DEFAULT_CACHE_NAME = "defaultCache";        
+       
        /**
         * Constructor which initialises a singleton {@link net.sf.ehcache.CacheManager}
         * 
@@ -133,7 +133,7 @@ public class CdmCacher<T extends CdmBase> implements ICdmCacher<T> {
                if (e == null) {
 
                    // nothing in the cache for "key" (or expired) ... re-load the entity
-                       cdmEntity = (T)termService.load(uuid);
+                       cdmEntity = findByUuid(uuid);
                        put(uuid, cdmEntity);
                } else {
                    // there is a valid element in the cache, however getObjectValue() may be null
@@ -177,6 +177,14 @@ public class CdmCacher<T extends CdmBase> implements ICdmCacher<T> {
                return false;
        }
        
+       /**
+        * Finds CDM Entity by uuid
+        * 
+        * @param uuid
+        * @return
+        */
+       protected abstract T findByUuid(UUID uuid);
+       
 
 
 }
diff --git a/cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/cache/CdmDaoCacher.java b/cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/cache/CdmDaoCacher.java
new file mode 100644 (file)
index 0000000..bc36153
--- /dev/null
@@ -0,0 +1,29 @@
+package eu.etaxonomy.cdm.api.cache;
+
+import java.util.UUID;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl;
+
+/**
+ * CDM Entity Cacher class which uses CDM DAOs to find cdm entities
+ * 
+ * @author cmathew
+ *
+ * @param <T>
+ */
+@Component
+public class CdmDaoCacher<T extends CdmBase> extends CdmCacher<T> {
+
+       @Autowired
+       CdmGenericDaoImpl genericDao;
+       
+       @Override
+       protected T findByUuid(UUID uuid) {
+               return (T)genericDao.findByUuidWithoutFlush(uuid);
+       }
+
+}
index 65507e697b3c094bb7900a364c7d9d2b2dc5b502..039e67899278303eb9b2953b036dbbf78c7a977e 100644 (file)
@@ -113,6 +113,9 @@ public interface IService<T extends ICdmBase>{
      * @return an entity of type <T>, or null if the entity does not exist\r
      */\r
     public T find(UUID uuid);\r
+    \r
+\r
+       public T findWithoutFlush(UUID uuid);\r
 \r
     /**\r
      * Return a persisted entity that matches the database identifier\r
@@ -299,4 +302,5 @@ public interface IService<T extends ICdmBase>{
      */\r
     public List<T> list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);\r
 \r
+\r
 }
\ No newline at end of file
index 509535861ff2bf7f80ba7607387f7aa074e67739..6758293c41b63153cd100f76888f4e7254d52ce6 100644 (file)
@@ -95,6 +95,12 @@ public abstract class ServiceBase<T extends CdmBase, DAO extends ICdmEntityDao<T
     public T find(UUID uuid) {\r
         return dao.findByUuid(uuid);\r
     }\r
+    \r
+    @Override\r
+    @Transactional(readOnly = true)\r
+    public T findWithoutFlush(UUID uuid) {\r
+        return dao.findByUuidWithoutFlush(uuid);\r
+    }\r
 \r
     @Override\r
     @Transactional(readOnly = true)\r
index d91909a5a7f730dc2f1640c48963b2506b8ab440..62f76e8777e151e8b0d3d2ae027e666f292284d6 100644 (file)
@@ -6,9 +6,17 @@ import org.junit.Test;
 import org.unitils.spring.annotation.SpringBeanByType;
 
 import eu.etaxonomy.cdm.model.common.Language;
+import eu.etaxonomy.cdm.model.name.BotanicalName;
+import eu.etaxonomy.cdm.model.name.NonViralName;
+import eu.etaxonomy.cdm.model.name.Rank;
+import eu.etaxonomy.cdm.model.reference.Reference;
+import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
 
 import eu.etaxonomy.cdm.api.cache.CdmCacher;
+import eu.etaxonomy.cdm.api.service.IReferenceService;
+import eu.etaxonomy.cdm.api.service.ITaxonService;
 
 public class CdmCacherTest extends CdmIntegrationTest {
        @SuppressWarnings("unused")
@@ -17,6 +25,12 @@ public class CdmCacherTest extends CdmIntegrationTest {
        @SpringBeanByType
        private CdmCacher cdmCacher;
        
+    @SpringBeanByType
+    private IReferenceService referenceService;
+    
+    @SpringBeanByType
+    private ITaxonService taxonService;
+       
        @Test
        public void testLanguageCache() {
                Language defaultLanguage = Language.getDefaultLanguage();       
@@ -27,6 +41,17 @@ public class CdmCacherTest extends CdmIntegrationTest {
                Language language = Language.getLanguageFromUuid(Language.uuidFrench);
                Language languageInCache = (Language)cdmCacher.getFromCache(language.getUuid());
                Assert.assertEquals("Loaded Language Term should match Language Term in Cache",language,languageInCache);
+               
+               // Following test is just to make sure no exception is raised when saving a taxon corresponding
+               // to a taxon name with no name cache to begin with 
+               Reference sec = ReferenceFactory.newDatabase();
+        referenceService.save(sec);
+               Taxon taxon = Taxon.NewInstance(NonViralName.NewInstance(Rank.SERIES()), sec);
+        taxon.setTitleCache("Tax" + "CdmCacher", true);
+        taxonService.save(taxon);
+        NonViralName nvn = (NonViralName)taxon.getName();
+        String nameCache = nvn.getNameCache();
+        logger.warn("name cache : " + nameCache);
        }
 
 }
diff --git a/cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/cache/MockCdmCacher.java b/cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/cache/MockCdmCacher.java
new file mode 100644 (file)
index 0000000..9f31e52
--- /dev/null
@@ -0,0 +1,169 @@
+package eu.etaxonomy.cdm.api.cache;
+
+import java.util.UUID;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.Element;
+import net.sf.ehcache.config.CacheConfiguration;
+import net.sf.ehcache.config.PersistenceConfiguration;
+import net.sf.ehcache.config.PersistenceConfiguration.Strategy;
+import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
+
+import eu.etaxonomy.cdm.model.ICdmCacher;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.common.DefinedTermBase;
+import eu.etaxonomy.cdm.model.common.Language;
+
+/**
+ * Since cdmlib-model cannot access CdmCacher we need to create a mock class
+ * for the tests
+ * 
+ * @author cmathew
+ *
+ */
+public class MockCdmCacher implements ICdmCacher {
+
+       private static final String DEFAULT_CACHE_NAME = "defaultCache";
+       
+       private static final String DEFAULT_CACHE_MGR_NAME = "defaultCacheManager";
+       /**
+        * Constructor which initialises a singleton {@link net.sf.ehcache.CacheManager}
+        * 
+        */
+       public MockCdmCacher() {
+               init();
+       }
+       
+       /**
+        * Initialises an empty singleton {@link net.sf.ehcache.CacheManager} and
+        * sets itself as the cacher object in specific CDM Entity objects. 
+        * 
+        */
+       private void init() {           
+               // Remove all caches
+               getDefaultCacheManager().removalAll();
+               // Create default cache
+               getDefaultCacheManager().addCache(new Cache(getDefaultCacheConfiguration()));
+               put(Language.DEFAULT().getUuid(),Language.DEFAULT());
+               // We start first only with DefinedTermBase
+               DefinedTermBase.setCacher(this);
+               
+       }
+       
+       /**
+        * Returns the singleton default cache manager.
+        * 
+        * @return
+        */
+       private static CacheManager getDefaultCacheManager() {
+               // this ensures a singleton cache manager
+               return CacheManager.create();
+       }
+       
+       /**
+        * Returns the default cache configuration.
+        * 
+        * @return
+        */
+       private CacheConfiguration getDefaultCacheConfiguration() {
+               return new CacheConfiguration(DEFAULT_CACHE_NAME, 50)
+           .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
+           .eternal(false)
+           // default ttl and tti set to 2 hours
+           .timeToLiveSeconds(60*60*2)
+           .timeToIdleSeconds(60*60*2)
+           .diskExpiryThreadIntervalSeconds(0)
+           .persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP));
+       }
+       
+       /**
+        * Returns the default cache
+        * 
+        * @return
+        */
+       private static Cache getDefaultCache() {
+               return getDefaultCacheManager().getCache(DEFAULT_CACHE_NAME);
+       }
+
+       /**
+        * Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
+        * in the cache
+        * 
+        * @param uuid
+        * @param cdmEntity
+        */
+       public void put(UUID uuid, CdmBase cdmEntity) {
+               getDefaultCache().put(new Element(uuid, cdmEntity));
+       }
+       
+       /**
+        * Gets the cache element corresponding to the given {@link java.util.UUID}
+        * 
+        * @param uuid
+        * @return
+        */
+       private Element getCacheElement(UUID uuid) {
+               return getDefaultCache().get(uuid);
+       }
+               
+       /**
+        * Get CDM Entity for given {@link java.util.UUID} from the cache
+        * 
+        * @param uuid
+        * @return
+        */
+       private CdmBase getCdmEntity(UUID uuid) {
+               return  (CdmBase)getDefaultCache().get(uuid).getObjectValue();                          
+       }
+       
+       /* (non-Javadoc)
+        * @see eu.etaxonomy.cdm.remote.cache.ICdmCacher#load(java.util.UUID)
+        */
+       
+       public CdmBase load(UUID uuid) {
+               Element e = getCacheElement(uuid);                      
+               CdmBase cdmEntity;
+               if (e == null) {
+                       return null;
+               } else {
+                   return (CdmBase)e.getObjectValue();
+               }               
+       }
+       
+
+       /* (non-Javadoc)
+        * @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(java.util.UUID)
+        */
+       public CdmBase getFromCache(UUID uuid) {
+               Element e = getCacheElement(uuid);                      
+               CdmBase cdmEntity;
+               if (e == null) {
+                       return null;
+               } else {
+                   return(CdmBase)e.getObjectValue();
+               }
+       }
+       
+
+       /* (non-Javadoc)
+        * @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
+        */
+       public boolean exists(UUID uuid) {
+               return getCacheElement(uuid) != null;
+       }
+       
+
+       /* (non-Javadoc)
+        * @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
+        */
+       public boolean existsAndIsNotNull(UUID uuid) {
+               Element e = getCacheElement(uuid);                      
+               CdmBase cdmEntity;
+               if (e != null) {
+                       return (CdmBase)e.getObjectValue() != null;                
+               } 
+               return false;
+       }
+       
+}
index 26d8e75a4fe41d1fd5814cd57b7c21e93a673129..d9210116b6d4c7ef6e79d4e3b74eb4c7e676659d 100644 (file)
@@ -29,7 +29,7 @@ import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
  * @author a.mueller
  * @created 2013-09-28
  */
-@SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/applicationContext-test.xml")
+//@SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/applicationContext-test.xml")
 public class FeatureNodeServiceImplTest extends CdmTransactionalIntegrationTest{
 
        private static final String sep = ITreeNode.separator;
index d3fd683b6d142d49593cd51725ea3379e79f2b11..ec0c4e38045880de56ce547ec8356960f92349ea 100644 (file)
@@ -49,8 +49,7 @@ public class IdentifiableServiceBaseTest extends CdmTransactionalIntegrationTest
        \r
        @Test\r
        @DataSet\r
-       @ExpectedDataSet\r
-       @Ignore\r
+       @ExpectedDataSet        \r
        public final void testUpdateTitleCache() {\r
                Assert.assertEquals("There should be 5 TaxonNames in the data set", 5, nameService.count(TaxonNameBase.class));\r
                Class clazz = TaxonNameBase.class;\r
index 00f36ac3217609d2ab86fbffa5caa705f6b29312..8a81afe954742818966e4ddc44e498afb16d5872 100644 (file)
@@ -76,12 +76,6 @@ public class SecurityTest extends AbstractSecurityTestBase{
 \r
     private static final Logger logger = Logger.getLogger(SecurityTest.class);\r
 \r
-    /**\r
-     * The transaction manager to use\r
-     */\r
-    @SpringBeanByType\r
-    PlatformTransactionManager transactionManager;\r
-\r
     @SpringBeanByType\r
     private ITaxonService taxonService;\r
 \r
index d6c0938c025bd09003f81d01459945b24f423bf6..7d4e475305c5c8cb3dae4ac55627ba6f94c0720f 100644 (file)
@@ -53,7 +53,7 @@ import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
  * @author n.hoffmann
  * @created Dec 16, 2010
  */
-@SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/applicationContext-test.xml")
+//@SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/applicationContext-test.xml")
 public class TaxonNodeServiceImplTest extends CdmTransactionalIntegrationTest{
 
        
index b00ff94999a6ff48152cb663e6069c9ebb930291..9bf5042d21ece040b0a82a073b5b72505931d105 100644 (file)
@@ -46,6 +46,7 @@ import eu.etaxonomy.cdm.api.service.search.SearchResult;
 import eu.etaxonomy.cdm.common.UTF8;\r
 import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;\r
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;\r
+import eu.etaxonomy.cdm.api.cache.MockCdmCacher;\r
 import eu.etaxonomy.cdm.model.common.CdmBase;\r
 import eu.etaxonomy.cdm.model.common.DefinedTermBase;\r
 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;\r
@@ -148,6 +149,13 @@ public class TaxonServiceSearchTest extends CdmTransactionalIntegrationTest {
         france = Country.FRANCEFRENCHREPUBLIC();\r
         russia = Country.RUSSIANFEDERATION();\r
         canada = Country.CANADA();\r
+        \r
+               // Have to add a mock cdm cacher since the disabling of transactions\r
+               // results in the default CdmCacher object not working due to the fact that \r
+               // the current session for the CdmGenericDaoImpl.findByUuidWithoutFlush in\r
+               // the autowired CdmDaoCacher is null\r
+               // Comment out the line below to see the error.\r
+        MockCdmCacher cdmCacher = new MockCdmCacher();\r
 \r
     }\r
 \r
@@ -337,8 +345,7 @@ public class TaxonServiceSearchTest extends CdmTransactionalIntegrationTest {
      */\r
     @SuppressWarnings("rawtypes")\r
     @Test\r
-    @DataSet\r
-    @Ignore\r
+    @DataSet    \r
     public final void testFullText_Paging() throws CorruptIndexException, IOException, ParseException {\r
 \r
         Reference sec = ReferenceFactory.newDatabase();\r