1 |
7b567c03
|
a.babadshanjan
|
/**
|
2 |
|
|
* Copyright (C) 2009 EDIT
|
3 |
66aeb5aa
|
Andreas Kohlbecker
|
* European Distributed Institute of Taxonomy
|
4 |
7b567c03
|
a.babadshanjan
|
* http://www.e-taxonomy.eu
|
5 |
66aeb5aa
|
Andreas Kohlbecker
|
*
|
6 |
7b567c03
|
a.babadshanjan
|
* 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.
|
8 |
|
|
*/
|
9 |
|
|
|
10 |
|
|
package eu.etaxonomy.cdm.api.service;
|
11 |
|
|
|
12 |
a883c19f
|
Andreas Müller
|
import java.util.Collection;
|
13 |
|
|
import java.util.HashSet;
|
14 |
2ff67c03
|
Katja Luther
|
import java.util.Iterator;
|
15 |
fcdd5642
|
n.hoffmann
|
import java.util.Map.Entry;
|
16 |
2ff67c03
|
Katja Luther
|
import java.util.Set;
|
17 |
|
|
import java.util.UUID;
|
18 |
|
|
|
19 |
a13c5f66
|
Andreas Müller
|
import org.junit.Assert;
|
20 |
2ff67c03
|
Katja Luther
|
|
21 |
7b567c03
|
a.babadshanjan
|
import org.apache.log4j.Logger;
|
22 |
|
|
import org.junit.Test;
|
23 |
2ff67c03
|
Katja Luther
|
import org.unitils.dbunit.annotation.DataSet;
|
24 |
7b567c03
|
a.babadshanjan
|
import org.unitils.spring.annotation.SpringBeanByType;
|
25 |
|
|
|
26 |
2ff67c03
|
Katja Luther
|
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 |
8051fca9
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.description.Feature;
|
32 |
fcdd5642
|
n.hoffmann
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
33 |
2ff67c03
|
Katja Luther
|
import eu.etaxonomy.cdm.model.description.TextData;
|
34 |
7b567c03
|
a.babadshanjan
|
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
|
35 |
|
|
|
36 |
|
|
/**
|
37 |
|
|
* @author a.babadshanjan
|
38 |
|
|
* @created 09.02.2009
|
39 |
|
|
* @version 1.0
|
40 |
|
|
*/
|
41 |
bce1dda8
|
Katja Luther
|
|
42 |
7b567c03
|
a.babadshanjan
|
public class DescriptionServiceImplTest extends CdmIntegrationTest {
|
43 |
66aeb5aa
|
Andreas Kohlbecker
|
@SuppressWarnings("unused")
|
44 |
|
|
private static Logger logger = Logger.getLogger(DescriptionServiceImplTest.class);
|
45 |
|
|
|
46 |
|
|
@SpringBeanByType
|
47 |
|
|
private IDescriptionService service;
|
48 |
|
|
|
49 |
|
|
@SpringBeanByType
|
50 |
|
|
private ITermService termService;
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
@Test
|
55 |
|
|
public void testGetDefaultFeatureVocabulary() {
|
56 |
|
|
service.getDefaultFeatureVocabulary();
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
@Test
|
60 |
|
|
@DataSet("CommonServiceImplTest.xml")
|
61 |
|
|
public void testChangeDescriptionElement(){
|
62 |
a13c5f66
|
Andreas Müller
|
DescriptionBase<?> descBase = service.find(UUID.fromString("eb17b80a-9be6-4642-a6a8-b19a318925e6"));
|
63 |
66aeb5aa
|
Andreas Kohlbecker
|
Set<DescriptionElementBase> elements = descBase.getElements();
|
64 |
a13c5f66
|
Andreas Müller
|
Iterator<?> iterator = elements.iterator();
|
65 |
66aeb5aa
|
Andreas Kohlbecker
|
while (iterator.hasNext()){
|
66 |
|
|
DescriptionElementBase base = (DescriptionElementBase) iterator.next();
|
67 |
|
|
if (base instanceof TextData){
|
68 |
|
|
TextData textdata = (TextData) base;
|
69 |
|
|
Set <Entry<Language,LanguageString>> entries = textdata.getMultilanguageText().entrySet();
|
70 |
|
|
Iterator entryIterator = entries.iterator();
|
71 |
|
|
while (entryIterator.hasNext()){
|
72 |
|
|
Entry <Language, LanguageString> entry = (Entry<Language, LanguageString>) entryIterator.next();
|
73 |
|
|
LanguageString langString = entry.getValue();
|
74 |
8051fca9
|
Andreas Müller
|
// System.out.println(langString);
|
75 |
66aeb5aa
|
Andreas Kohlbecker
|
langString.setText("blablubber");
|
76 |
|
|
}
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
}
|
80 |
|
|
service.saveOrUpdate(descBase);
|
81 |
|
|
Pager<DescriptionElementBase> allElements = service.getDescriptionElements(null, null, null, null, null, null);
|
82 |
|
|
Assert.assertEquals(1, allElements.getCount().intValue());
|
83 |
|
|
DescriptionElementBase test = allElements.getRecords().get(0);
|
84 |
|
|
if (test instanceof TextData){
|
85 |
|
|
|
86 |
|
|
Set <Entry<Language,LanguageString>> entries = ((TextData) test).getMultilanguageText().entrySet();
|
87 |
a13c5f66
|
Andreas Müller
|
Iterator<?> entryIterator = entries.iterator();
|
88 |
66aeb5aa
|
Andreas Kohlbecker
|
while (entryIterator.hasNext()){
|
89 |
|
|
Entry <Language, LanguageString> entry = (Entry<Language, LanguageString>) entryIterator.next();
|
90 |
|
|
LanguageString langString = entry.getValue();
|
91 |
8051fca9
|
Andreas Müller
|
// System.out.println(langString);
|
92 |
66aeb5aa
|
Andreas Kohlbecker
|
}
|
93 |
|
|
}
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
@Test
|
97 |
|
|
public void testMoveDescriptionElementsToTaxon(){
|
98 |
|
|
UUID commonNameFeatureUuid = Feature.COMMON_NAME().getUuid();
|
99 |
|
|
|
100 |
|
|
Feature commonNameFeature = (Feature)termService.find(commonNameFeatureUuid);
|
101 |
|
|
|
102 |
|
|
TaxonDescription sourceDescription = TaxonDescription.NewInstance();
|
103 |
|
|
|
104 |
|
|
TextData element = TextData.NewInstance();
|
105 |
|
|
element.setFeature(commonNameFeature);
|
106 |
|
|
sourceDescription.addElement(element);
|
107 |
|
|
|
108 |
|
|
TextData element2 = TextData.NewInstance();
|
109 |
|
|
element2.setFeature(commonNameFeature);
|
110 |
|
|
sourceDescription.addElement(element2);
|
111 |
|
|
|
112 |
|
|
Collection<DescriptionElementBase> sourceCollection = new HashSet<DescriptionElementBase>();
|
113 |
|
|
sourceCollection.addAll(sourceDescription.getElements());
|
114 |
|
|
TextData element3 = TextData.NewInstance();
|
115 |
|
|
element3.setFeature(commonNameFeature);
|
116 |
|
|
|
117 |
|
|
sourceDescription.addElement(element3);
|
118 |
|
|
|
119 |
|
|
Assert.assertEquals(3, sourceDescription.getElements().size());
|
120 |
|
|
|
121 |
|
|
TaxonDescription targetDescription = TaxonDescription.NewInstance();
|
122 |
|
|
this.service.save(sourceDescription);
|
123 |
|
|
this.service.save(targetDescription);
|
124 |
|
|
|
125 |
|
|
service.moveDescriptionElementsToDescription(sourceCollection, targetDescription, false);
|
126 |
|
|
|
127 |
|
|
Assert.assertEquals("Source descirption should have 1 element left", 1, sourceDescription.getElements().size());
|
128 |
|
|
Assert.assertEquals("Target descriptoin should have 2 new elements", 2, targetDescription.getElements().size());
|
129 |
2cf910bc
|
Andreas Müller
|
//the following tests are not valid anymore as elements are cloned now even if isCopy is false
|
130 |
|
|
// Assert.assertTrue("The moved element should be in the new description", targetDescription.getElements().contains(element));
|
131 |
|
|
// Assert.assertTrue("The moved element2 should be in the new description", targetDescription.getElements().contains(element2));
|
132 |
|
|
// Assert.assertFalse("Element3 should not be in the new description", targetDescription.getElements().contains(element3));
|
133 |
66aeb5aa
|
Andreas Kohlbecker
|
Assert.assertTrue("Element3 should remain in the old description", sourceDescription.getElements().contains(element3));
|
134 |
|
|
this.service.save(sourceDescription);
|
135 |
|
|
this.service.save(targetDescription);
|
136 |
|
|
|
137 |
|
|
try {
|
138 |
|
|
service.moveDescriptionElementsToDescription(targetDescription.getElements(), sourceDescription, false);
|
139 |
|
|
} catch (Exception e) {
|
140 |
|
|
//asserting that no ConcurrentModificationException is thrown when the elements collection is passed as a parameter
|
141 |
|
|
e.printStackTrace();
|
142 |
|
|
Assert.fail();
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
Assert.assertEquals("Source description should have 3 elements again", 3, sourceDescription.getElements().size());
|
146 |
|
|
Assert.assertEquals("Destination description should have no elements again", 0, targetDescription.getElements().size());
|
147 |
|
|
this.service.save(sourceDescription);
|
148 |
|
|
this.service.save(targetDescription);
|
149 |
|
|
|
150 |
|
|
//test copy
|
151 |
|
|
sourceCollection.clear();
|
152 |
|
|
sourceCollection.add(sourceDescription.getElements().iterator().next());
|
153 |
|
|
service.moveDescriptionElementsToDescription(sourceCollection, targetDescription, true);
|
154 |
|
|
|
155 |
|
|
Assert.assertEquals("Source description should still have 3 elements", 3, sourceDescription.getElements().size());
|
156 |
|
|
int size = targetDescription.getElements().size();
|
157 |
|
|
Assert.assertEquals("Destination descirption should have 1 element again", 1, size);
|
158 |
|
|
for (DescriptionElementBase targetElement : targetDescription.getElements()){
|
159 |
|
|
Assert.assertFalse("Target elements may not be in sourced description as they are only clones (but not same).", sourceDescription.getElements().contains(targetElement));
|
160 |
|
|
}
|
161 |
|
|
this.service.save(targetDescription);
|
162 |
|
|
this.service.save(sourceDescription);
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
}
|
166 |
7b567c03
|
a.babadshanjan
|
}
|