2 * Copyright (C) 2009 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
.cdm
.api
.service
;
12 import static org
.junit
.Assert
.assertNotNull
;
13 import static org
.junit
.Assert
.assertNull
;
15 import java
.io
.FileNotFoundException
;
16 import java
.util
.Collection
;
17 import java
.util
.HashSet
;
18 import java
.util
.Iterator
;
19 import java
.util
.Map
.Entry
;
21 import java
.util
.UUID
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.junit
.Assert
;
25 import org
.junit
.Ignore
;
26 import org
.junit
.Test
;
27 import org
.unitils
.dbunit
.annotation
.DataSet
;
28 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
30 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
31 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
32 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
33 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
34 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
35 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
36 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
37 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
39 import eu
.etaxonomy
.cdm
.test
.integration
.CdmTransactionalIntegrationTest
;
42 * @author a.babadshanjan
47 public class DescriptionServiceImplTest
extends CdmTransactionalIntegrationTest
{
48 @SuppressWarnings("unused")
49 private static Logger logger
= Logger
.getLogger(DescriptionServiceImplTest
.class);
52 private IDescriptionService service
;
55 private ITermService termService
;
60 public void testGetDefaultFeatureVocabulary() {
61 service
.getDefaultFeatureVocabulary();
65 @DataSet("CommonServiceImplTest.xml")
66 public void testChangeDescriptionElement(){
67 DescriptionBase
<?
> descBase
= service
.find(UUID
.fromString("eb17b80a-9be6-4642-a6a8-b19a318925e6"));
68 Set
<DescriptionElementBase
> elements
= descBase
.getElements();
69 Iterator
<?
> iterator
= elements
.iterator();
70 while (iterator
.hasNext()){
71 DescriptionElementBase base
= (DescriptionElementBase
) iterator
.next();
72 if (base
instanceof TextData
){
73 TextData textdata
= (TextData
) base
;
74 Set
<Entry
<Language
,LanguageString
>> entries
= textdata
.getMultilanguageText().entrySet();
75 Iterator
<?
> entryIterator
= entries
.iterator();
76 while (entryIterator
.hasNext()){
77 Entry
<Language
, LanguageString
> entry
= (Entry
<Language
, LanguageString
>) entryIterator
.next();
78 LanguageString langString
= entry
.getValue();
79 // System.out.println(langString);
80 langString
.setText("blablubber");
85 service
.saveOrUpdate(descBase
);
86 Pager
<DescriptionElementBase
> allElements
= service
.getDescriptionElements(null, null, null, null, null, null);
87 Assert
.assertEquals(1, allElements
.getCount().intValue());
88 DescriptionElementBase test
= allElements
.getRecords().get(0);
89 if (test
instanceof TextData
){
91 Set
<Entry
<Language
,LanguageString
>> entries
= ((TextData
) test
).getMultilanguageText().entrySet();
92 Iterator
<Entry
<Language
,LanguageString
>> entryIterator
= entries
.iterator();
93 while (entryIterator
.hasNext()){
94 Entry
<Language
, LanguageString
> entry
= entryIterator
.next();
95 LanguageString langString
= entry
.getValue();
96 // System.out.println(langString);
103 public void testMoveDescriptionElement(){
105 Taxon taxon
= Taxon
.NewInstance(null, null);
106 TaxonDescription desc1
= TaxonDescription
.NewInstance(taxon
);
107 TextData textData1
= TextData
.NewInstance(Feature
.HABITAT(), "My habitat", Language
.GERMAN(), null);
108 desc1
.addElement(textData1
);
109 service
.saveOrUpdate(desc1
);
111 TaxonDescription desc2
= TaxonDescription
.NewInstance(taxon
);
112 TextData textData2
= TextData
.NewInstance(Feature
.HABITAT(), "My habitat2", Language
.GERMAN(), null);
113 desc2
.addElement(textData2
);
114 service
.saveOrUpdate(desc2
);
115 commitAndStartNewTransaction(null);
118 DescriptionBase
<?
> descLoaded1
= service
.find(desc1
.getUuid());
119 DescriptionBase
<?
> descLoaded2
= service
.find(desc2
.getUuid());
121 DescriptionElementBase textDataLoaded
= descLoaded1
.getElements().iterator().next();
122 Set
<DescriptionElementBase
> tmpSet
= new HashSet
<DescriptionElementBase
>(descLoaded1
.getElements());
125 service
.moveDescriptionElementsToDescription(tmpSet
, descLoaded2
, false);
127 commitAndStartNewTransaction(null);
128 } catch (Exception e
) {
129 Assert
.fail("Moving description element should not throw an exception. Exception is " + e
.getMessage());
137 public void testMoveDescriptionElementsToTaxon(){
139 UUID commonNameFeatureUuid
= Feature
.COMMON_NAME().getUuid();
140 Feature commonNameFeatureData
= (Feature
)termService
.find(commonNameFeatureUuid
);
142 TaxonDescription sourceDescriptionData
= TaxonDescription
.NewInstance();
143 TextData elementData
= TextData
.NewInstance();
144 elementData
.setFeature(commonNameFeatureData
);
145 sourceDescriptionData
.addElement(elementData
);
147 TextData element2
= TextData
.NewInstance();
148 element2
.setFeature(commonNameFeatureData
);
149 sourceDescriptionData
.addElement(element2
);
151 TextData element3Data
= TextData
.NewInstance();
152 element3Data
.setFeature(commonNameFeatureData
);
153 sourceDescriptionData
.addElement(element3Data
);
154 Assert
.assertEquals(3, sourceDescriptionData
.getElements().size());
155 TaxonDescription targetDescriptionData
= TaxonDescription
.NewInstance();
156 this.service
.save(sourceDescriptionData
);
157 this.service
.save(targetDescriptionData
);
159 commitAndStartNewTransaction(null);
161 TaxonDescription sourceDescription
= (TaxonDescription
)this.service
.find(sourceDescriptionData
.getId());
162 Assert
.assertEquals(3, sourceDescription
.getElements().size());
164 TaxonDescription targetDescription
= (TaxonDescription
)this.service
.find(targetDescriptionData
.getId());
167 Collection
<DescriptionElementBase
> sourceCollection
= new HashSet
<DescriptionElementBase
>();
168 sourceCollection
.addAll(sourceDescription
.getElements());
169 sourceCollection
.remove(element3Data
); //should work as it works on equal
170 Assert
.assertEquals(2, sourceCollection
.size());
172 service
.moveDescriptionElementsToDescription(sourceCollection
, targetDescription
, false);
174 Assert
.assertEquals("Source description should have 1 element left", 1, sourceDescription
.getElements().size());
175 Assert
.assertEquals("Target description should have 2 new elements", 2, targetDescription
.getElements().size());
176 //the following tests are not valid anymore as elements are cloned now even if isCopy is false
177 // Assert.assertTrue("The moved element should be in the new description", targetDescription.getElements().contains(element));
178 // Assert.assertTrue("The moved element2 should be in the new description", targetDescription.getElements().contains(element2));
179 // Assert.assertFalse("Element3 should not be in the new description", targetDescription.getElements().contains(element3));
181 Assert
.assertTrue("Element3 should remain in the old description", sourceDescription
.getElements().contains(element3Data
));
182 sourceDescription
= (TaxonDescription
) this.service
.find(sourceDescription
.getUuid());
183 targetDescription
= (TaxonDescription
) this.service
.find(targetDescription
.getUuid());
184 assertNotNull(sourceDescription
);
185 assertNotNull(targetDescription
);
187 service
.moveDescriptionElementsToDescription(targetDescription
.getElements(), sourceDescription
, false);
188 } catch (Exception e
) {
189 //asserting that no ConcurrentModificationException is thrown when the elements collection is passed as a parameter
194 Assert
.assertEquals("Source description should have 3 elements again", 3, sourceDescription
.getElements().size());
195 Assert
.assertEquals("Destination description should have no elements again", 0, targetDescription
.getElements().size());
196 sourceDescription
= (TaxonDescription
) this.service
.find(sourceDescription
.getUuid());
197 targetDescription
= (TaxonDescription
) this.service
.find(targetDescription
.getUuid());
198 assertNotNull(sourceDescription
);
199 assertNull(targetDescription
);
207 public void testMoveDescriptionElementsToTaxonAndResaveDeletedDescription(){
210 UUID commonNameFeatureUuid
= Feature
.COMMON_NAME().getUuid();
211 Feature commonNameFeatureData
= (Feature
)termService
.find(commonNameFeatureUuid
);
213 TaxonDescription sourceDescriptionData
= TaxonDescription
.NewInstance();
214 TextData elementData
= TextData
.NewInstance();
215 elementData
.setFeature(commonNameFeatureData
);
216 sourceDescriptionData
.addElement(elementData
);
218 TextData element2
= TextData
.NewInstance();
219 element2
.setFeature(commonNameFeatureData
);
220 sourceDescriptionData
.addElement(element2
);
222 TextData element3Data
= TextData
.NewInstance();
223 element3Data
.setFeature(commonNameFeatureData
);
224 sourceDescriptionData
.addElement(element3Data
);
225 Assert
.assertEquals(3, sourceDescriptionData
.getElements().size());
226 TaxonDescription targetDescriptionData
= TaxonDescription
.NewInstance();
227 this.service
.save(sourceDescriptionData
);
228 this.service
.save(targetDescriptionData
);
230 commitAndStartNewTransaction(null);
232 TaxonDescription sourceDescription
= (TaxonDescription
)this.service
.find(sourceDescriptionData
.getId());
233 Assert
.assertEquals(3, sourceDescription
.getElements().size());
235 TaxonDescription targetDescription
= (TaxonDescription
)this.service
.find(targetDescriptionData
.getId());
236 service
.moveDescriptionElementsToDescription(sourceDescription
.getElements(), targetDescription
, false);
237 TaxonDescription removedDescription
= (TaxonDescription
) this.service
.find(sourceDescription
.getUuid());
238 assertNull(removedDescription
);
239 this.service
.save(targetDescription
);
241 removedDescription
= (TaxonDescription
) this.service
.find(targetDescription
.getUuid());
242 assertNotNull(removedDescription
);
246 public void createTestDataSet() throws FileNotFoundException
{};