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
45 public class DescriptionServiceImplTest
extends CdmTransactionalIntegrationTest
{
47 @SuppressWarnings("unused")
48 private static Logger logger
= Logger
.getLogger(DescriptionServiceImplTest
.class);
51 private IDescriptionService service
;
54 private ITermService termService
;
57 public void testGetDefaultFeatureVocabulary() {
58 service
.getDefaultFeatureVocabulary();
62 @DataSet("CommonServiceImplTest.xml")
63 public void testChangeDescriptionElement(){
64 DescriptionBase
<?
> descBase
= service
.find(UUID
.fromString("eb17b80a-9be6-4642-a6a8-b19a318925e6"));
65 Set
<DescriptionElementBase
> elements
= descBase
.getElements();
66 Iterator
<?
> iterator
= elements
.iterator();
67 while (iterator
.hasNext()){
68 DescriptionElementBase base
= (DescriptionElementBase
) iterator
.next();
69 if (base
instanceof TextData
){
70 TextData textdata
= (TextData
) base
;
71 Set
<Entry
<Language
,LanguageString
>> entries
= textdata
.getMultilanguageText().entrySet();
72 Iterator
<?
> entryIterator
= entries
.iterator();
73 while (entryIterator
.hasNext()){
74 Entry
<Language
, LanguageString
> entry
= (Entry
<Language
, LanguageString
>) entryIterator
.next();
75 LanguageString langString
= entry
.getValue();
76 // System.out.println(langString);
77 langString
.setText("blablubber");
81 service
.saveOrUpdate(descBase
);
82 Pager
<DescriptionElementBase
> allElements
= service
.getDescriptionElements(null, null, null, null, null, null);
83 Assert
.assertEquals(1, allElements
.getCount().intValue());
84 DescriptionElementBase test
= allElements
.getRecords().get(0);
85 if (test
instanceof TextData
){
87 Set
<Entry
<Language
,LanguageString
>> entries
= ((TextData
) test
).getMultilanguageText().entrySet();
88 Iterator
<Entry
<Language
,LanguageString
>> entryIterator
= entries
.iterator();
89 while (entryIterator
.hasNext()){
90 Entry
<Language
, LanguageString
> entry
= entryIterator
.next();
91 LanguageString langString
= entry
.getValue();
92 // System.out.println(langString);
98 @DataSet("../../database/ClearDBDataSet.xml")
99 public void testMoveDescriptionElement(){
101 Taxon taxon
= Taxon
.NewInstance(null, null);
102 TaxonDescription desc1
= TaxonDescription
.NewInstance(taxon
);
103 TextData textData1
= TextData
.NewInstance(Feature
.HABITAT(), "My habitat", Language
.GERMAN(), null);
104 desc1
.addElement(textData1
);
105 service
.saveOrUpdate(desc1
);
107 TaxonDescription desc2
= TaxonDescription
.NewInstance(taxon
);
108 TextData textData2
= TextData
.NewInstance(Feature
.HABITAT(), "My habitat2", Language
.GERMAN(), null);
109 desc2
.addElement(textData2
);
110 service
.saveOrUpdate(desc2
);
111 commitAndStartNewTransaction(null);
113 DescriptionBase
<?
> descLoaded1
= service
.find(desc1
.getUuid());
114 DescriptionBase
<?
> descLoaded2
= service
.find(desc2
.getUuid());
116 DescriptionElementBase textDataLoaded
= descLoaded1
.getElements().iterator().next();
117 Set
<DescriptionElementBase
> tmpSet
= new HashSet
<DescriptionElementBase
>(descLoaded1
.getElements());
120 service
.moveDescriptionElementsToDescription(tmpSet
, descLoaded2
, false, true);
122 commitAndStartNewTransaction(null);
123 } catch (Exception e
) {
124 Assert
.fail("Moving description element should not throw an exception. Exception is " + e
.getMessage());
130 public void testMoveDescriptionElementsToTaxon(){
132 UUID commonNameFeatureUuid
= Feature
.COMMON_NAME().getUuid();
133 Feature commonNameFeatureData
= (Feature
)termService
.find(commonNameFeatureUuid
);
135 TaxonDescription sourceDescriptionData
= TaxonDescription
.NewInstance();
136 TextData elementData
= TextData
.NewInstance();
137 elementData
.setFeature(commonNameFeatureData
);
138 sourceDescriptionData
.addElement(elementData
);
140 TextData element2
= TextData
.NewInstance();
141 element2
.setFeature(commonNameFeatureData
);
142 sourceDescriptionData
.addElement(element2
);
144 TextData element3Data
= TextData
.NewInstance();
145 element3Data
.setFeature(commonNameFeatureData
);
146 sourceDescriptionData
.addElement(element3Data
);
147 Assert
.assertEquals(3, sourceDescriptionData
.getElements().size());
148 TaxonDescription targetDescriptionData
= TaxonDescription
.NewInstance();
149 this.service
.save(sourceDescriptionData
);
150 this.service
.save(targetDescriptionData
);
152 commitAndStartNewTransaction(null);
154 TaxonDescription sourceDescription
= (TaxonDescription
)this.service
.find(sourceDescriptionData
.getId());
155 Assert
.assertEquals(3, sourceDescription
.getElements().size());
157 TaxonDescription targetDescription
= (TaxonDescription
)this.service
.find(targetDescriptionData
.getId());
160 Collection
<DescriptionElementBase
> sourceCollection
= new HashSet
<DescriptionElementBase
>();
161 sourceCollection
.addAll(sourceDescription
.getElements());
162 sourceCollection
.remove(element3Data
); //should work as it works on equal
163 Assert
.assertEquals(2, sourceCollection
.size());
165 service
.moveDescriptionElementsToDescription(sourceCollection
, targetDescription
, false, true);
167 Assert
.assertEquals("Source description should have 1 element left", 1, sourceDescription
.getElements().size());
168 Assert
.assertEquals("Target description should have 2 new elements", 2, targetDescription
.getElements().size());
169 //the following tests are not valid anymore as elements are cloned now even if isCopy is false
170 // Assert.assertTrue("The moved element should be in the new description", targetDescription.getElements().contains(element));
171 // Assert.assertTrue("The moved element2 should be in the new description", targetDescription.getElements().contains(element2));
172 // Assert.assertFalse("Element3 should not be in the new description", targetDescription.getElements().contains(element3));
174 Assert
.assertTrue("Element3 should remain in the old description", sourceDescription
.getElements().contains(element3Data
));
175 sourceDescription
= (TaxonDescription
) this.service
.find(sourceDescription
.getUuid());
176 targetDescription
= (TaxonDescription
) this.service
.find(targetDescription
.getUuid());
177 assertNotNull(sourceDescription
);
178 assertNotNull(targetDescription
);
180 service
.moveDescriptionElementsToDescription(targetDescription
.getElements(), sourceDescription
, false, true);
181 } catch (Exception e
) {
182 //asserting that no ConcurrentModificationException is thrown when the elements collection is passed as a parameter
187 Assert
.assertEquals("Source description should have 3 elements again", 3, sourceDescription
.getElements().size());
188 Assert
.assertEquals("Destination description should have no elements again", 0, targetDescription
.getElements().size());
189 sourceDescription
= (TaxonDescription
) this.service
.find(sourceDescription
.getUuid());
190 targetDescription
= (TaxonDescription
) this.service
.find(targetDescription
.getUuid());
191 assertNotNull(sourceDescription
);
192 assertNull(targetDescription
);
201 public void testMoveDescriptionElementsToTaxonAndResaveDeletedDescription(){
204 UUID commonNameFeatureUuid
= Feature
.COMMON_NAME().getUuid();
205 Feature commonNameFeatureData
= (Feature
)termService
.find(commonNameFeatureUuid
);
207 TaxonDescription sourceDescriptionData
= TaxonDescription
.NewInstance();
208 TextData elementData
= TextData
.NewInstance();
209 elementData
.setFeature(commonNameFeatureData
);
210 sourceDescriptionData
.addElement(elementData
);
212 TextData element2
= TextData
.NewInstance();
213 element2
.setFeature(commonNameFeatureData
);
214 sourceDescriptionData
.addElement(element2
);
216 TextData element3Data
= TextData
.NewInstance();
217 element3Data
.setFeature(commonNameFeatureData
);
218 sourceDescriptionData
.addElement(element3Data
);
219 Assert
.assertEquals(3, sourceDescriptionData
.getElements().size());
220 TaxonDescription targetDescriptionData
= TaxonDescription
.NewInstance();
221 this.service
.save(sourceDescriptionData
);
222 this.service
.save(targetDescriptionData
);
224 commitAndStartNewTransaction(null);
226 TaxonDescription sourceDescription
= (TaxonDescription
)this.service
.find(sourceDescriptionData
.getId());
227 Assert
.assertEquals(3, sourceDescription
.getElements().size());
229 TaxonDescription targetDescription
= (TaxonDescription
)this.service
.find(targetDescriptionData
.getId());
230 service
.moveDescriptionElementsToDescription(sourceDescription
.getElements(), targetDescription
, false, true);
231 //the description is not removed anymore to avoid deletion of not persisted description elements
233 // TaxonDescription removedDescription = (TaxonDescription) this.service.find(sourceDescription.getUuid());
234 // assertNull(removedDescription);
235 this.service
.save(targetDescription
);
237 targetDescription
= (TaxonDescription
) this.service
.find(targetDescription
.getUuid());
238 assertNotNull(targetDescription
);
242 public void createTestDataSet() throws FileNotFoundException
{};