3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.api
.cache
;
12 import java
.util
.Arrays
;
13 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Level
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.hibernate
.collection
.spi
.PersistentCollection
;
20 import org
.junit
.Assert
;
21 import org
.junit
.Before
;
22 import org
.junit
.BeforeClass
;
23 import org
.junit
.Test
;
24 import org
.unitils
.dbunit
.annotation
.DataSet
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IPolytomousKeyService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
31 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
32 import eu
.etaxonomy
.cdm
.model
.description
.KeyStatement
;
33 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
34 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
35 import eu
.etaxonomy
.taxeditor
.httpinvoker
.BaseRemotingTest
;
36 import eu
.etaxonomy
.taxeditor
.httpinvoker
.CDMServer
;
37 import eu
.etaxonomy
.taxeditor
.remoting
.session
.CdmEntitySessionManager
;
38 import eu
.etaxonomy
.taxeditor
.remoting
.session
.ISessionEventListener
;
39 import eu
.etaxonomy
.taxeditor
.remoting
.session
.MockSessionOwner
;
47 public class CdmClientCachingTest
extends BaseRemotingTest
{
49 private static final Logger logger
= Logger
.getLogger(CdmClientCachingTest
.class);
51 UUID polytomousKeyUuid
= UUID
.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
52 UUID taxon1Uuid
= UUID
.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
55 IPolytomousKeyService polytomousKeyService
= getRemoteApplicationController().getPolytomousKeyService();
56 ICommonService commonService
= getRemoteApplicationController().getCommonService();
57 ITaxonService taxonService
= getRemoteApplicationController().getTaxonService();
59 CdmEntitySessionManager cdmEntitySessionManager
= getRemoteApplicationController().getCdmEntitySessionManager();
61 CdmTransientEntityCacher cacher
;
63 Language english
= Language
.getLanguageFromUuid(Language
.uuidEnglish
);
65 private static final List
<String
> PKEY_DEPTH1_INIT_STRATEGY
= Arrays
.asList(new String
[] {
68 private static final List
<String
> PKEY_DEPTH2_INIT_STRATEGY
= Arrays
.asList(new String
[] {
71 private static final List
<String
> PKEY_DEPTH3_INIT_STRATEGY
= Arrays
.asList(new String
[] {
74 private ISessionEventListener sessionOwner
;
77 public static void initializePolytomousKeyTest() {
78 logger
.setLevel(Level
.INFO
);
79 CDMServer
.getInstance().setKeepServerRunning(true);
81 CdmModelCacher cmdmc
= new CdmModelCacher();
82 cmdmc
.cacheGetterFields();
84 //initializeController("default", "127.0.0.1", 8080, "", NomenclaturalCode.ICNAFP);
88 public void initializeSession() {
89 sessionOwner
= new MockSessionOwner();
90 cdmEntitySessionManager
.bind(sessionOwner
);
91 cacher
= cdmEntitySessionManager
.getCdmTransientEntityCacher(sessionOwner
);
95 public void whenRecursiveCachingIsDisabledObjectsInSubGraphAreNotLoadedInSessionCache() {
96 CdmTransientEntityCacher
.setRecursiveEnabled(false);
97 ISessionEventListener sessionOwner
= new MockSessionOwner();
98 cdmEntitySessionManager
.bind(sessionOwner
);
99 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
100 cdmEntitySessionManager
.add(sessionOwner
, pkey
);
101 CdmTransientEntityCacher cacher
= cdmEntitySessionManager
.getCdmTransientEntityCacher(sessionOwner
);
102 Assert
.assertNull(cacher
.getFromCache(pkey
));
103 CdmTransientEntityCacher
.setRecursiveEnabled(true);
108 public void recursiveLoadSubGraphDepth1Test() {
110 // this call will load into the session cache the graph
112 // |- root : polytomous key node
113 // in a recursive call
114 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
115 pkey1
= cdmEntitySessionManager
.add(sessionOwner
, pkey1
);
116 CdmTransientEntityCacher cacher
= cdmEntitySessionManager
.getCdmTransientEntityCacher(sessionOwner
);
117 // checking to make sure the root object is in the session cache
118 Assert
.assertSame(pkey1
.getRoot(), cacher
.getFromCache(pkey1
.getRoot()));
124 public void recursiveLoadSubGraphDepth2Test() {
126 // this call will load into the session cache the graph
128 // |- root : polytomous key node
129 // |- question : KeyStatement
130 // |- statement : KeyStatement
131 // in a recursive call
132 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
133 pkey
= cdmEntitySessionManager
.add(sessionOwner
, pkey
);
134 CdmTransientEntityCacher cacher
= cdmEntitySessionManager
.getCdmTransientEntityCacher(sessionOwner
);
135 // checking to make sure the root object is in the session cache
136 Assert
.assertSame(pkey
.getRoot(), cacher
.getFromCache(pkey
.getRoot()));
137 Assert
.assertSame(pkey
.getRoot().getStatement(), cacher
.getFromCache(pkey
.getRoot().getStatement()));
138 Assert
.assertSame(pkey
.getRoot().getQuestion(), cacher
.getFromCache(pkey
.getRoot().getQuestion()));
143 * when : retrieving objects using recursive caching of object graphs with different depths
144 * then : the objects in the sub-graph having the same persistence id should be the same
147 public void lazyLoadRecursiveTest() {
149 // this call will load into the session cache the graph and update the objects in the sub-graph for a
151 // |- root : polytomous key node
152 // |- question : KeyStatement
153 // |- statement : KeyStatement
154 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
155 pkey1
= cdmEntitySessionManager
.add(sessionOwner
, pkey1
);
156 CdmTransientEntityCacher cacher
= cdmEntitySessionManager
.getCdmTransientEntityCacher(sessionOwner
);
162 * when : retrieving objects using recursive caching of object graphs with different depths
163 * then : the objects in the sub-graph having the same persistence id should be the same
166 public void differentSubGraphDepthTest1() {
168 // this call will load into the session cache the graph and update the objects in the sub-graph for a
170 // |- root : polytomous key node
171 // in a recursive call
172 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
173 pkey1
= cdmEntitySessionManager
.add(sessionOwner
, pkey1
);
174 CdmTransientEntityCacher cacher
= cdmEntitySessionManager
.getCdmTransientEntityCacher(sessionOwner
);
176 // this call will load into the session cache the graph and update the objects in the sub-graph for a
178 // |- root : polytomous key node
179 // |- question : KeyStatement
180 // |- statement : KeyStatement
181 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
182 pkey2
= cdmEntitySessionManager
.add(sessionOwner
, pkey2
);
184 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
185 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
186 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
191 * when : retrieving objects using recursive caching of object graphs with different depths
192 * then : the objects in the sub-graph having the same persistence id should be the same
195 public void differentSubGraphDepthTest2() {
197 // this call will load into the session cache the graph and update the objects in the sub-graph for a
199 // |- root : polytomous key node
200 // |- question : KeyStatement
201 // |- statement : KeyStatement
202 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
203 pkey1
= cdmEntitySessionManager
.add(sessionOwner
, pkey1
);
204 CdmTransientEntityCacher cacher
= cdmEntitySessionManager
.getCdmTransientEntityCacher(sessionOwner
);
206 // this call will load into the session cache the graph and update the objects in the sub-graph for a
208 // |- root : polytomous key node
209 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
210 pkey2
= cdmEntitySessionManager
.add(sessionOwner
, pkey2
);
212 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
213 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
214 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
218 * when : retrieving objects using (first) recursive load directly and (second) lazy loading in the same session
219 * then : the objects in the sub-graph having the same persistence id should be the same
222 public void recursiveLoadAndLazyLoadTest() {
224 // this call will load into the session cache the graph and update the objects in the sub-graph for a
226 // |- root : polytomous key node
227 // |- question : KeyStatement
228 // |- statement : KeyStatement
229 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
230 pkey1
= cdmEntitySessionManager
.add(sessionOwner
, pkey1
);
231 CdmTransientEntityCacher cacher
= cdmEntitySessionManager
.getCdmTransientEntityCacher(sessionOwner
);
233 // checking that the root is not null and
234 // that it exists in the cache and
235 // that both the original object and the
236 // cached object are the same
237 Assert
.assertNotNull(pkey1
.getRoot().getStatement());
238 Assert
.assertNotNull(cacher
.getFromCache(pkey1
.getRoot().getStatement()));
240 // this call will load into the session cache the graph and update the objects in the sub-graph for a
242 // |- root : polytomous key node
243 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
244 pkey2
= cdmEntitySessionManager
.add(sessionOwner
, pkey2
);
247 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
248 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
249 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
253 * when : retrieving objects using (first) lazy loading and (second) recursive load directly in the same session
254 * then : the objects in the sub-graph having the same persistence id should be the same
257 public void lazyLoadAndRecursiveLoadTest() {
260 // this call will load into the session cache the graph and update the objects in the sub-graph for a
262 // |- root : polytomous key node
264 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
265 pkey1
= cdmEntitySessionManager
.add(sessionOwner
, pkey1
);
267 // lazy initialising root.statement
268 KeyStatement st
= CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class);
271 // checking that the root is not null and
272 // that it exists in the cache and
273 // that both the original object and the
274 // cached object are the same
275 Assert
.assertNotNull(pkey1
.getRoot().getStatement());
276 Assert
.assertSame(CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class),
277 cacher
.getFromCache(pkey1
.getRoot().getStatement(), KeyStatement
.class));
279 // this call will load into the session cache the graph and update the objects in the sub-graph for a
281 // |- root : polytomous key node
282 // |- question : KeyStatement
283 // |- statement : KeyStatement
285 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
286 pkey2
= cdmEntitySessionManager
.add(sessionOwner
, pkey2
);
288 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
289 Assert
.assertSame(st
, pkey2
.getRoot().getStatement());
290 Assert
.assertSame(cacher
.getFromCache(st
), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
296 public void savePolytomousKeyNodeDataWithSameSubKey() {
297 //CachedCommonServiceImpl.setCacheEnabled(false);
298 ISessionEventListener sessionOwner
= new MockSessionOwner();
299 cdmEntitySessionManager
.bind(sessionOwner
);
300 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
301 PolytomousKeyNode pkeynode
= pkey
.getRoot();
303 PolytomousKey subkey1
= pkeynode
.getChildAt(0).getSubkey();
304 String subkey1title
= subkey1
.getTitleCache();
305 subkey1
.setTitleCache(subkey1title
+ "test", true);
308 PolytomousKey subkey2
= pkeynode
.getChildAt(1).getChildAt(0).getSubkey();
309 String subkey2title
= subkey2
.getTitleCache();
310 subkey2
.setTitleCache(subkey2title
+ "test", true);
312 Assert
.assertSame(subkey1
, subkey2
);
314 polytomousKeyService
.merge(pkey
);
318 public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
319 CachedCommonServiceImpl
.setCacheEnabled(false);
320 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
321 PolytomousKeyNode pkeynode
= pkey
.getRoot();
323 PersistentCollection children
= (PersistentCollection
) pkeynode
.getChildren();
324 PolytomousKeyNode childNode0
= (PolytomousKeyNode
)commonService
.get(children
, 0);
325 PolytomousKey subkey1
= CdmBase
.deproxy(childNode0
.getSubkey(),PolytomousKey
.class);
326 String subkey1title
= subkey1
.getTitleCache();
327 subkey1
.setTitleCache(subkey1title
+ "test", true);
329 PolytomousKeyNode childNode1
= (PolytomousKeyNode
)commonService
.get(children
, 1);
330 PolytomousKey subkey2
= CdmBase
.deproxy(childNode1
.getSubkey(),PolytomousKey
.class);
331 String subkey2title
= subkey2
.getTitleCache();
332 subkey2
.setTitleCache(subkey2title
+ "test", true);
334 Assert
.assertNotSame(subkey1
, subkey2
);
336 polytomousKeyService
.merge(pkey
);
341 public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
342 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
343 PolytomousKeyNode pkeynode
= pkey
.getRoot();
345 Map
<Language
, LanguageString
> label1
= pkeynode
.getQuestion().getLabel();
347 //Language lang1 = label1.get(english).getLanguage();
348 //String title1 = lang1.getTitleCache();
350 Map
<Language
, LanguageString
> label2
= pkeynode
.getChildAt(0).getStatement().getLabel();
352 //Language lang2 = label2.get(english).getLanguage();
353 //String title2 = lang2.getTitleCache();
355 polytomousKeyService
.merge(pkey
);