224ef43490b39e811c15e22426fefaa908d345e8
[cdmlib.git] / cdmlib-model / src / test / java / eu / etaxonomy / cdm / model / MockCdmCacher.java
1 package eu.etaxonomy.cdm.model;
2
3 import java.util.Set;
4 import java.util.UUID;
5
6 import net.sf.ehcache.Cache;
7 import net.sf.ehcache.CacheManager;
8 import net.sf.ehcache.Element;
9 import net.sf.ehcache.config.CacheConfiguration;
10 import net.sf.ehcache.config.PersistenceConfiguration;
11 import net.sf.ehcache.config.PersistenceConfiguration.Strategy;
12 import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
13
14 import eu.etaxonomy.cdm.model.common.CdmBase;
15 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
16 import eu.etaxonomy.cdm.model.common.Language;
17
18 /**
19 * Since cdmlib-model cannot access CdmCacher we need to create a mock class
20 * for the tests
21 *
22 * @author cmathew
23 *
24 */
25 public class MockCdmCacher implements ICdmCacher {
26
27 private static final String DEFAULT_CACHE_NAME = "defaultCache";
28
29 private static final String DEFAULT_CACHE_MGR_NAME = "defaultCacheManager";
30 /**
31 * Constructor which initialises a singleton {@link net.sf.ehcache.CacheManager}
32 *
33 */
34 public MockCdmCacher() {
35 init();
36 }
37
38 /**
39 * Initialises an empty singleton {@link net.sf.ehcache.CacheManager} and
40 * sets itself as the cacher object in specific CDM Entity objects.
41 *
42 */
43 private void init() {
44 // Remove all caches
45 getDefaultCacheManager().removalAll();
46 // Create default cache
47 getDefaultCacheManager().addCache(new Cache(getDefaultCacheConfiguration()));
48 Set<Language> langList = Language.DEFAULT().getVocabulary().getTerms();
49 for (Language lang : langList){
50 put(lang.getUuid(),lang);
51 }
52 // We start first only with DefinedTermBase
53 DefinedTermBase.setCacher(this);
54
55 }
56
57 /**
58 * Returns the singleton default cache manager.
59 *
60 * @return
61 */
62 private static CacheManager getDefaultCacheManager() {
63 // this ensures a singleton cache manager
64 return CacheManager.create();
65 }
66
67 /**
68 * Returns the default cache configuration.
69 *
70 * @return
71 */
72 private CacheConfiguration getDefaultCacheConfiguration() {
73 return new CacheConfiguration(DEFAULT_CACHE_NAME, 50)
74 .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
75 .eternal(false)
76 // default ttl and tti set to 2 hours
77 .timeToLiveSeconds(60*60*2)
78 .timeToIdleSeconds(60*60*2)
79 .diskExpiryThreadIntervalSeconds(0)
80 .persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP));
81 }
82
83 /**
84 * Returns the default cache
85 *
86 * @return
87 */
88 private static Cache getDefaultCache() {
89 return getDefaultCacheManager().getCache(DEFAULT_CACHE_NAME);
90 }
91
92 /**
93 * Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
94 * in the cache
95 *
96 * @param uuid
97 * @param cdmEntity
98 */
99 public void put(UUID uuid, CdmBase cdmEntity) {
100 getDefaultCache().put(new Element(uuid, cdmEntity));
101 }
102
103 /**
104 * Gets the cache element corresponding to the given {@link java.util.UUID}
105 *
106 * @param uuid
107 * @return
108 */
109 private Element getCacheElement(UUID uuid) {
110 return getDefaultCache().get(uuid);
111 }
112
113 /**
114 * Get CDM Entity for given {@link java.util.UUID} from the cache
115 *
116 * @param uuid
117 * @return
118 */
119 private CdmBase getCdmEntity(UUID uuid) {
120 return (CdmBase)getDefaultCache().get(uuid).getObjectValue();
121 }
122
123 /* (non-Javadoc)
124 * @see eu.etaxonomy.cdm.remote.cache.ICdmCacher#load(java.util.UUID)
125 */
126
127 public CdmBase load(UUID uuid) {
128 Element e = getCacheElement(uuid);
129 CdmBase cdmEntity;
130 if (e == null) {
131 return null;
132 } else {
133 return (CdmBase)e.getObjectValue();
134 }
135 }
136
137
138 /* (non-Javadoc)
139 * @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(java.util.UUID)
140 */
141 public CdmBase getFromCache(UUID uuid) {
142 Element e = getCacheElement(uuid);
143 CdmBase cdmEntity;
144 if (e == null) {
145 return null;
146 } else {
147 return(CdmBase)e.getObjectValue();
148 }
149 }
150
151
152 /* (non-Javadoc)
153 * @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
154 */
155 public boolean exists(UUID uuid) {
156 return getCacheElement(uuid) != null;
157 }
158
159
160 /* (non-Javadoc)
161 * @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
162 */
163 public boolean existsAndIsNotNull(UUID uuid) {
164 Element e = getCacheElement(uuid);
165 CdmBase cdmEntity;
166 if (e != null) {
167 return (CdmBase)e.getObjectValue() != null;
168 }
169 return false;
170 }
171
172 }