2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.lazyloading
;
12 import java
.util
.Arrays
;
13 import java
.util
.HashSet
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.apache
.log4j
.Level
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.junit
.Assert
;
22 import org
.junit
.BeforeClass
;
23 import org
.junit
.Ignore
;
24 import org
.junit
.Test
;
26 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
28 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
29 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
32 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
33 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationship
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
37 import eu
.etaxonomy
.taxeditor
.httpinvoker
.BaseRemotingTest
;
42 * Test class which tests remoting for persistent cdm entities.
44 * FIXME:Remoting saving tests are ignored until the merge development is underway
48 //FIXME:Remoting need to create a proper dataset for this test
49 //Could be combined with AbstractLazyInitializerTest
51 public class RemoteLazyLoadingTest
extends BaseRemotingTest
{
53 private static final Logger logger
= Logger
.getLogger(RemoteLazyLoadingTest
.class);
55 private static IClassificationService classificationService
;
56 private static ITaxonService taxonService
;
58 private static List
<TaxonNode
> taxonNodes
;
60 private final UUID taxonUuid1
= UUID
.fromString("8217ef77-2ab1-4318-bd67-ccd0cdef07c4");
61 private final UUID taxonUuid2
= UUID
.fromString("ef96fafa-7750-4141-b31b-1ad1daab3e76");
65 public static void initializeRemoteLazyLoading() {
67 Logger
.getRootLogger().setLevel(Level
.DEBUG
);
68 taxonService
= getRemoteApplicationController().getTaxonService();
70 classificationService
= getRemoteApplicationController().getClassificationService();
71 //List<Classification> classifications = classificationService.listClassifications(1,0,null,null);
72 // Assert.assertFalse(classifications.isEmpty());
74 // Classification classification = classifications.get(0);
75 // Assert.assertNotNull(classification);
76 taxonNodes
= classificationService
.getAllNodes();
77 Assert
.assertFalse(taxonNodes
.isEmpty());
84 public void testCDMEntityGet() {
85 Iterator
<TaxonNode
> taxonNodeItr
= taxonNodes
.iterator();
88 while(taxonNodeItr
.hasNext() && count
<= maxcount
) {
89 TaxonNode taxonNode
= taxonNodeItr
.next();
90 Assert
.assertNotNull(taxonNode
);
92 Taxon taxon
= taxonNode
.getTaxon();
93 Assert
.assertNotNull(taxon
);
95 String taxonTitle
= taxon
.getTitleCache();
96 logger
.info("Taxon : " + taxonTitle
);
98 TaxonNameBase name
= taxon
.getName();
99 Assert
.assertNotNull(name
);
101 String nameTitle
= name
.getTitleCache();
102 logger
.info("Taxon Name : " + nameTitle
);
116 public void testCDMEntitySaveEager() {
117 Taxon taxon
= (Taxon
)taxonService
.find(taxonUuid1
);
118 String oldTitleCache
= taxon
.getTitleCache();
120 System
.out
.println("Taxon title : " + oldTitleCache
);
122 taxon
.setTitleCache(oldTitleCache
+ ":updated");
123 taxonService
.merge(taxon
);
125 Taxon taxonNew
= (Taxon
)taxonService
.find(taxonUuid1
);
126 System
.out
.println("New Taxon Title : " + taxonNew
.getTitleCache());
128 Assert
.assertTrue("Title caches should not be equal",oldTitleCache
.equals(taxonNew
.getTitleCache()));
130 taxonNew
.setTitleCache(oldTitleCache
);
131 taxonService
.merge(taxonNew
);
133 Taxon taxonOld
= (Taxon
)taxonService
.find(taxonUuid1
);
134 System
.out
.println("Old Taxon Title : " + taxonOld
.getTitleCache());
136 Assert
.assertEquals("Title caches should be equal",oldTitleCache
,taxonOld
.getTitleCache());
142 public void testCDMEntityUpdate() {
144 Team combAuthor
= Team
.NewInstance();
145 combAuthor
.addTeamMember(Person
.NewTitledInstance("test member"));
146 BotanicalName name
= BotanicalName
.NewInstance(null, "Test1", null, null, null, null, null, null, null);
147 name
.setCombinationAuthorTeam(combAuthor
);
148 Taxon taxon
= Taxon
.NewInstance(name
, null);
149 UUID taxonUuid
= taxonService
.save(taxon
);
151 // Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
152 // NonViralName nvn = CdmBase.deproxy(taxon.getName(),NonViralName.class);
153 // String oldTitleCache = nvn.getTitleCache();
154 // System.out.println("Taxon Name Title : " + oldTitleCache);
155 // nvn.setTitleCache(oldTitleCache + ":updated", true);
157 // taxon.setTitleCache(oldTitleCache + ":updated",true);
159 // taxonService.update(taxon);
160 // } catch (LazyInitializationException lie) {
161 // lie.printStackTrace();
164 List
<String
> TAXON_INIT_STRATEGY
= Arrays
.asList(new String
[] {
167 Taxon taxonNew
= (Taxon
)taxonService
.findTaxonByUuid(taxonUuid
,TAXON_INIT_STRATEGY
);
168 NonViralName nvn
= CdmBase
.deproxy(taxonNew
.getName(),NonViralName
.class);
169 Team team
= CdmBase
.deproxy(nvn
.getCombinationAuthorTeam(),Team
.class);
170 String oldTitleCache
= nvn
.getTitleCache();
171 System
.out
.println("Taxon Name Title : " + oldTitleCache
);
172 nvn
.setTitleCache(oldTitleCache
+ ":updated", true);
173 taxonService
.update(taxonNew
);
179 public void testCDMEntitySaveLazy() {
180 Taxon taxon
= (Taxon
)taxonService
.find(taxonUuid1
);
182 NonViralName nvn
= CdmBase
.deproxy(taxon
.getName(),NonViralName
.class);
183 String oldTitleCache
= nvn
.getTitleCache();
184 System
.out
.println("Taxon Name Title : " + oldTitleCache
);
185 nvn
.setTitleCache(oldTitleCache
+ ":updated", true);
186 taxonService
.update(taxon
);
188 // Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
189 // NonViralName nvnNew = CdmBase.deproxy(taxon.getName(),NonViralName.class);
190 // System.out.println("New Taxon Name Title : " + nvnNew.getTitleCache());
192 // Assert.assertNotEquals("Title caches should not be equal",oldTitleCache,nvnNew.getTitleCache());
194 // nvnNew.setTitleCache(oldTitleCache, true);
195 // taxonService.update(taxon);
197 // Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
198 // NonViralName nvnOld = CdmBase.deproxy(taxon.getName(),NonViralName.class);
199 // System.out.println("Old Taxon Name Title : " + nvnNew.getTitleCache());
201 // Assert.assertEquals("Title caches should be equal",oldTitleCache,nvnOld.getTitleCache());
205 public void testCDMEntitySaveLazyNew() {
206 Team combAuthor
= Team
.NewInstance();
207 combAuthor
.addTeamMember(Person
.NewTitledInstance("test member"));
208 BotanicalName name
= BotanicalName
.NewInstance(null, "Test1", null, null, null, null, null, null, null);
209 name
.setCombinationAuthorTeam(combAuthor
);
210 Taxon tax1
= Taxon
.NewInstance(name
, null);
211 UUID taxonUuid1
= taxonService
.save(tax1
);
213 Taxon taxon
= (Taxon
)taxonService
.find(taxonUuid1
);
215 NonViralName nvn
= CdmBase
.deproxy(taxon
.getName(),NonViralName
.class);
216 String oldTitleCache
= nvn
.getTitleCache();
217 logger
.info("Taxon Name Title : " + oldTitleCache
);
218 nvn
.setTitleCache(oldTitleCache
+ ":updated",false);
219 taxonService
.update(taxon
);
221 Taxon taxonNew
= (Taxon
)taxonService
.find(taxonUuid1
);
222 NonViralName nvnNew
= CdmBase
.deproxy(taxon
.getName(),NonViralName
.class);
223 logger
.info("New Taxon Name Title : " + nvnNew
.getTitleCache());
225 Assert
.assertTrue("Title caches should not be equal",oldTitleCache
.equals(nvnNew
.getTitleCache()));
227 nvnNew
.setTitleCache(oldTitleCache
, true);
228 taxonService
.update(taxon
);
230 Taxon taxonOld
= (Taxon
)taxonService
.find(taxonUuid1
);
231 NonViralName nvnOld
= CdmBase
.deproxy(taxon
.getName(),NonViralName
.class);
232 logger
.info("Old Taxon Name Title : " + nvnNew
.getTitleCache());
234 Assert
.assertEquals("Title caches should be equal",oldTitleCache
,nvnOld
.getTitleCache());
238 public void testCDMEntitySaveCollection() {
239 Taxon taxon
= (Taxon
)taxonService
.find(taxonUuid1
);
241 Set
<SynonymRelationship
> synRelations
= taxon
.getSynonymRelations();
242 Set
<String
> relToTitles
= new HashSet
<String
>();
243 Iterator
<SynonymRelationship
> srItr
= synRelations
.iterator();
244 while(srItr
.hasNext()) {
245 SynonymRelationship sr
= srItr
.next();
246 System
.out
.println("Synonym Title Cache : " + sr
.getSynonym().getTitleCache());
247 relToTitles
.add(sr
.getSynonym().getTitleCache());
248 sr
.getSynonym().setTitleCache(sr
.getSynonym().getTitleCache() + ":updated");
251 taxonService
.merge(taxon
);
253 Taxon taxonNew
= (Taxon
)taxonService
.find(taxonUuid1
);
254 Set
<SynonymRelationship
> synRelationsNew
= taxonNew
.getSynonymRelations();
256 Iterator
<SynonymRelationship
> srItrNew
= synRelationsNew
.iterator();
257 Iterator
<String
> relToTitlesItr
= relToTitles
.iterator();
258 while(srItrNew
.hasNext() && relToTitlesItr
.hasNext()) {
259 SynonymRelationship srNew
= srItrNew
.next();
260 String relToTitle
= relToTitlesItr
.next();
261 System
.out
.println("New Synonym Title Cache: " + srNew
.getSynonym().getTitleCache());
262 Assert
.assertTrue("Synonym Title caches should not be equal", srNew
.getSynonym().getTitleCache().equals(relToTitle
));
263 srNew
.getSynonym().setTitleCache(relToTitle
);
266 Taxon taxonOld
= (Taxon
)taxonService
.find(taxonUuid1
);
268 Set
<SynonymRelationship
> synRelationsOld
= taxonNew
.getSynonymRelations();
269 Iterator
<SynonymRelationship
> srItrOld
= synRelationsOld
.iterator();
270 relToTitlesItr
= relToTitles
.iterator();
271 while(srItrOld
.hasNext() && relToTitlesItr
.hasNext()) {
272 SynonymRelationship srOld
= srItrOld
.next();
273 String relToTitle
= relToTitlesItr
.next();
274 System
.out
.println("New Synonym Title Cache: " + srOld
.getSynonym().getTitleCache());
275 Assert
.assertEquals("Synonym Title caches should be equal", srOld
.getSynonym().getTitleCache(), relToTitle
);