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 java
.util
.Collection
;
13 import java
.util
.HashSet
;
14 import java
.util
.Iterator
;
15 import java
.util
.Map
.Entry
;
17 import java
.util
.UUID
;
19 import org
.junit
.Assert
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.junit
.Test
;
23 import org
.unitils
.dbunit
.annotation
.DataSet
;
24 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
26 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
28 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
29 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
30 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
31 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
32 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
33 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
34 import eu
.etaxonomy
.cdm
.test
.integration
.CdmIntegrationTest
;
37 * @author a.babadshanjan
42 public class DescriptionServiceImplTest
extends CdmIntegrationTest
{
43 @SuppressWarnings("unused")
44 private static Logger logger
= Logger
.getLogger(DescriptionServiceImplTest
.class);
47 private IDescriptionService service
;
50 private ITermService termService
;
55 public void testGetDefaultFeatureVocabulary() {
57 service
.getDefaultFeatureVocabulary();
61 @DataSet("CommonServiceImplTest.xml")
62 public void testChangeDescriptionElement(){
63 DescriptionBase
<?
> descBase
= service
.find(UUID
.fromString("eb17b80a-9be6-4642-a6a8-b19a318925e6"));
64 Set
<DescriptionElementBase
> elements
= descBase
.getElements();
65 Iterator
<?
> iterator
= elements
.iterator();
66 while (iterator
.hasNext()){
67 DescriptionElementBase base
= (DescriptionElementBase
) iterator
.next();
68 if (base
instanceof TextData
){
69 TextData textdata
= (TextData
) base
;
70 Set
<Entry
<Language
,LanguageString
>> entries
= textdata
.getMultilanguageText().entrySet();
71 Iterator entryIterator
= entries
.iterator();
72 while (entryIterator
.hasNext()){
73 Entry
<Language
, LanguageString
> entry
= (Entry
<Language
, LanguageString
>) entryIterator
.next();
74 LanguageString langString
= entry
.getValue();
75 // System.out.println(langString);
76 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
<?
> entryIterator
= entries
.iterator();
89 while (entryIterator
.hasNext()){
90 Entry
<Language
, LanguageString
> entry
= (Entry
<Language
, LanguageString
>) entryIterator
.next();
91 LanguageString langString
= entry
.getValue();
92 // System.out.println(langString);
98 public void testMoveDescriptionElementsToTaxon(){
99 UUID commonNameFeatureUuid
= Feature
.COMMON_NAME().getUuid();
101 Feature commonNameFeature
= (Feature
)termService
.find(commonNameFeatureUuid
);
103 TaxonDescription sourceDescription
= TaxonDescription
.NewInstance();
105 TextData element
= TextData
.NewInstance();
106 element
.setFeature(commonNameFeature
);
107 sourceDescription
.addElement(element
);
109 TextData element2
= TextData
.NewInstance();
110 element2
.setFeature(commonNameFeature
);
111 sourceDescription
.addElement(element2
);
113 Collection
<DescriptionElementBase
> sourceCollection
= new HashSet
<DescriptionElementBase
>();
114 sourceCollection
.addAll(sourceDescription
.getElements());
115 TextData element3
= TextData
.NewInstance();
116 element3
.setFeature(commonNameFeature
);
118 sourceDescription
.addElement(element3
);
120 Assert
.assertEquals(3, sourceDescription
.getElements().size());
122 TaxonDescription targetDescription
= TaxonDescription
.NewInstance();
123 this.service
.save(sourceDescription
);
124 this.service
.save(targetDescription
);
126 service
.moveDescriptionElementsToDescription(sourceCollection
, targetDescription
, false);
128 Assert
.assertEquals("Source descirption should have 1 element left", 1, sourceDescription
.getElements().size());
129 Assert
.assertEquals("Target descriptoin should have 2 new elements", 2, targetDescription
.getElements().size());
130 //the following tests are not valid anymore as elements are cloned now even if isCopy is false
131 // Assert.assertTrue("The moved element should be in the new description", targetDescription.getElements().contains(element));
132 // Assert.assertTrue("The moved element2 should be in the new description", targetDescription.getElements().contains(element2));
133 // Assert.assertFalse("Element3 should not be in the new description", targetDescription.getElements().contains(element3));
134 Assert
.assertTrue("Element3 should remain in the old description", sourceDescription
.getElements().contains(element3
));
135 this.service
.save(sourceDescription
);
136 this.service
.save(targetDescription
);
139 service
.moveDescriptionElementsToDescription(targetDescription
.getElements(), sourceDescription
, false);
140 } catch (Exception e
) {
141 //asserting that no ConcurrentModificationException is thrown when the elements collection is passed as a parameter
146 Assert
.assertEquals("Source description should have 3 elements again", 3, sourceDescription
.getElements().size());
147 Assert
.assertEquals("Destination description should have no elements again", 0, targetDescription
.getElements().size());
148 this.service
.save(sourceDescription
);
149 this.service
.save(targetDescription
);
152 sourceCollection
.clear();
153 sourceCollection
.add(sourceDescription
.getElements().iterator().next());
154 service
.moveDescriptionElementsToDescription(sourceCollection
, targetDescription
, true);
156 Assert
.assertEquals("Source description should still have 3 elements", 3, sourceDescription
.getElements().size());
157 int size
= targetDescription
.getElements().size();
158 Assert
.assertEquals("Destination descirption should have 1 element again", 1, size
);
159 for (DescriptionElementBase targetElement
: targetDescription
.getElements()){
160 Assert
.assertFalse("Target elements may not be in sourced description as they are only clones (but not same).", sourceDescription
.getElements().contains(targetElement
));
162 this.service
.save(targetDescription
);
163 this.service
.save(sourceDescription
);