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.
9 package eu
.etaxonomy
.taxeditor
.lazyloading
;
11 import java
.util
.HashSet
;
12 import java
.util
.Iterator
;
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
.junit
.Assert
;
20 import org
.junit
.BeforeClass
;
21 import org
.junit
.Ignore
;
22 import org
.junit
.Test
;
23 import org
.unitils
.dbunit
.annotation
.DataSet
;
25 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationRemoteController
;
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
.Classification
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationship
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
38 import eu
.etaxonomy
.taxeditor
.httpinvoker
.BaseRemotingTest
;
44 * Class to test the {@link CdmApplicationRemoteController}
48 public class AbstractLazyInitializerTest
extends BaseRemotingTest
{
49 private static final Logger logger
= Logger
.getLogger(AbstractLazyInitializerTest
.class);
51 private static IClassificationService classificationService
;
52 private static ITaxonService taxonService
;
54 private static List
<TaxonNode
> taxonNodes
;
56 private final UUID taxonUuid1
= UUID
.fromString("8217ef77-2ab1-4318-bd67-ccd0cdef07c4");
57 private final UUID taxonUuid2
= UUID
.fromString("ef96fafa-7750-4141-b31b-1ad1daab3e76");
61 public static void initializeRemoteLazyLoading() {
63 Logger
.getRootLogger().setLevel(Level
.DEBUG
);
65 taxonService
= getRemoteApplicationController().getTaxonService();
66 classificationService
= getRemoteApplicationController().getClassificationService();
67 List
<Classification
> classifications
= classificationService
.listClassifications(1,0,null,null);
68 Assert
.assertFalse(classifications
.isEmpty());
70 Classification classification
= classifications
.get(0);
71 Assert
.assertNotNull(classification
);
72 taxonNodes
= classificationService
.getAllNodes();
73 Assert
.assertFalse(taxonNodes
.isEmpty());
80 public void testCDMEntityGet() {
81 //ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
82 Iterator
<TaxonNode
> taxonNodeItr
= taxonNodes
.iterator();
85 while(taxonNodeItr
.hasNext() && count
<= maxcount
) {
86 TaxonNode taxonNode
= taxonNodeItr
.next();
87 Assert
.assertNotNull(taxonNode
);
89 Taxon taxon
= taxonNode
.getTaxon();
90 Assert
.assertNotNull(taxon
);
92 String taxonTitle
= taxon
.getTitleCache();
93 logger
.info("Taxon : " + taxonTitle
);
95 TaxonNameBase name
= taxon
.getName();
96 Assert
.assertNotNull(name
);
98 String nameTitle
= name
.getTitleCache();
99 logger
.info("Taxon Name : " + nameTitle
);
106 public void taxonReadTest() {
107 Taxon taxon
= (Taxon
)taxonService
.find(taxonUuid1
);
113 public void testCDMEntitySave() {
114 Taxon taxon
= (Taxon
)taxonService
.find(taxonUuid1
);
115 String oldTitleCache
= taxon
.getTitleCache();
117 System
.out
.println("Taxon title : " + oldTitleCache
);
119 taxon
.setTitleCache(oldTitleCache
+ ":updated", true);
120 taxonService
.merge(taxon
);
122 Taxon taxonNew
= (Taxon
)taxonService
.find(taxonUuid1
);
123 System
.out
.println("New Taxon Title : " + taxonNew
.getTitleCache());
125 Assert
.assertNotEquals("Title caches should not be equal",oldTitleCache
,taxonNew
.getTitleCache());
127 taxonNew
.setTitleCache(oldTitleCache
, true);
128 taxonService
.merge(taxonNew
);
130 Taxon taxonOld
= (Taxon
)taxonService
.find(taxonUuid1
);
131 System
.out
.println("Old Taxon Title : " + taxonOld
.getTitleCache());
133 Assert
.assertEquals("Title caches should be equal",oldTitleCache
,taxonOld
.getTitleCache());
139 public void testCDMEntitySaveLazyNew() {
140 Team combAuthor
= Team
.NewInstance();
141 combAuthor
.addTeamMember(Person
.NewTitledInstance("test member"));
142 BotanicalName name
= BotanicalName
.NewInstance(null, "Test1", null, null, null, null, null, null, null);
143 name
.setCombinationAuthorTeam(combAuthor
);
144 Taxon tax1
= Taxon
.NewInstance(name
, null);
145 UUID taxonUuid1
= taxonService
.save(tax1
);
147 Taxon taxon
= (Taxon
)taxonService
.find(taxonUuid1
);
149 NonViralName nvn
= CdmBase
.deproxy(taxon
.getName(),NonViralName
.class);
150 String oldTitleCache
= nvn
.getTitleCache();
151 logger
.info("Taxon Name Title : " + oldTitleCache
);
152 nvn
.setTitleCache(oldTitleCache
+ ":updated",false);
153 taxonService
.update(taxon
);
155 Taxon taxonNew
= (Taxon
)taxonService
.find(taxonUuid1
);
156 NonViralName nvnNew
= CdmBase
.deproxy(taxon
.getName(),NonViralName
.class);
157 logger
.info("New Taxon Name Title : " + nvnNew
.getTitleCache());
159 Assert
.assertNotEquals("Title caches should not be equal",oldTitleCache
,nvnNew
.getTitleCache());
161 nvnNew
.setTitleCache(oldTitleCache
, true);
162 taxonService
.update(taxon
);
164 Taxon taxonOld
= (Taxon
)taxonService
.find(taxonUuid1
);
165 NonViralName nvnOld
= CdmBase
.deproxy(taxon
.getName(),NonViralName
.class);
166 logger
.info("Old Taxon Name Title : " + nvnNew
.getTitleCache());
168 Assert
.assertEquals("Title caches should be equal",oldTitleCache
,nvnOld
.getTitleCache());
173 public void testCDMEntitySaveCollection() {
174 Taxon taxon
= (Taxon
)taxonService
.find(taxonUuid1
);
176 Set
<SynonymRelationship
> synRelations
= taxon
.getSynonymRelations();
177 Set
<String
> relToTitles
= new HashSet
<String
>();
178 Iterator
<SynonymRelationship
> srItr
= synRelations
.iterator();
179 while(srItr
.hasNext()) {
180 SynonymRelationship sr
= srItr
.next();
181 System
.out
.println("Synonym Title Cache : " + sr
.getSynonym().getTitleCache());
182 relToTitles
.add(sr
.getSynonym().getTitleCache());
183 sr
.getSynonym().setTitleCache(sr
.getSynonym().getTitleCache() + ":updated");
186 taxonService
.merge(taxon
);
188 Taxon taxonNew
= (Taxon
)taxonService
.find(taxonUuid1
);
189 Set
<SynonymRelationship
> synRelationsNew
= taxonNew
.getSynonymRelations();
191 Iterator
<SynonymRelationship
> srItrNew
= synRelationsNew
.iterator();
192 Iterator
<String
> relToTitlesItr
= relToTitles
.iterator();
193 while(srItrNew
.hasNext() && relToTitlesItr
.hasNext()) {
194 SynonymRelationship srNew
= srItrNew
.next();
195 String relToTitle
= relToTitlesItr
.next();
196 System
.out
.println("New Synonym Title Cache: " + srNew
.getSynonym().getTitleCache());
197 Assert
.assertNotEquals("Synonym Title caches should not be equal", srNew
.getSynonym().getTitleCache(), relToTitle
);
198 srNew
.getSynonym().setTitleCache(relToTitle
);
201 Taxon taxonOld
= (Taxon
)taxonService
.find(taxonUuid1
);
203 Set
<SynonymRelationship
> synRelationsOld
= taxonNew
.getSynonymRelations();
204 Iterator
<SynonymRelationship
> srItrOld
= synRelationsOld
.iterator();
205 relToTitlesItr
= relToTitles
.iterator();
206 while(srItrOld
.hasNext() && relToTitlesItr
.hasNext()) {
207 SynonymRelationship srOld
= srItrOld
.next();
208 String relToTitle
= relToTitlesItr
.next();
209 System
.out
.println("New Synonym Title Cache: " + srOld
.getSynonym().getTitleCache());
210 Assert
.assertEquals("Synonym Title caches should be equal", srOld
.getSynonym().getTitleCache(), relToTitle
);