1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.api.service;
|
12
|
|
13
|
import java.util.List;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import junit.framework.Assert;
|
17
|
|
18
|
import org.junit.Before;
|
19
|
import org.junit.BeforeClass;
|
20
|
import org.junit.Test;
|
21
|
|
22
|
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
|
23
|
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
|
24
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
25
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
26
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
27
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
28
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
29
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
|
30
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
|
31
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
32
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
33
|
|
34
|
/**
|
35
|
* This test checks of all the business logic methods do what they are expected to do.
|
36
|
*
|
37
|
* @author n.hoffmann
|
38
|
* @created Dec 16, 2010
|
39
|
* @version 1.0
|
40
|
*/
|
41
|
public class TaxonServiceImplBusinessTest {
|
42
|
|
43
|
private Synonym s1;
|
44
|
private Taxon t2;
|
45
|
private Taxon t1;
|
46
|
private TaxonServiceImpl service;
|
47
|
private String referenceDetail;
|
48
|
private Reference reference;
|
49
|
private SynonymRelationshipType homoTypicSynonymRelationshipType;
|
50
|
private SynonymRelationshipType heteroTypicSynonymRelationshipType;
|
51
|
private NonViralName s1n;
|
52
|
private NonViralName t2n;
|
53
|
private NonViralName t1n;
|
54
|
private NonViralName s2n;
|
55
|
|
56
|
@BeforeClass
|
57
|
public static void setUpClass() throws Exception{
|
58
|
new DefaultTermInitializer().initialize();
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* @throws java.lang.Exception
|
63
|
*/
|
64
|
@Before
|
65
|
public void setUp() throws Exception {
|
66
|
service = new TaxonServiceImpl();
|
67
|
|
68
|
t1n = NonViralName.NewInstance(null);
|
69
|
t1 = Taxon.NewInstance(t1n, reference);
|
70
|
|
71
|
t2n = NonViralName.NewInstance(null);
|
72
|
t2 = Taxon.NewInstance(t2n, reference);
|
73
|
|
74
|
s1n = NonViralName.NewInstance(null);
|
75
|
s1 = Synonym.NewInstance(s1n, reference);
|
76
|
|
77
|
s2n = NonViralName.NewInstance(null);
|
78
|
|
79
|
// referencing
|
80
|
homoTypicSynonymRelationshipType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
|
81
|
heteroTypicSynonymRelationshipType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
|
82
|
reference = ReferenceFactory.newGeneric();
|
83
|
referenceDetail = "test";
|
84
|
}
|
85
|
|
86
|
|
87
|
/**
|
88
|
* Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#swapSynonymAndAcceptedTaxon(eu.etaxonomy.cdm.model.taxon.Synonym, eu.etaxonomy.cdm.model.taxon.Taxon)}.
|
89
|
*/
|
90
|
@Test
|
91
|
public final void testSwapSynonymAndAcceptedTaxon() {
|
92
|
t1.addSynonym(s1, homoTypicSynonymRelationshipType);
|
93
|
|
94
|
service.swapSynonymAndAcceptedTaxon(s1, t1);
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#changeSynonymToAcceptedTaxon(eu.etaxonomy.cdm.model.taxon.Synonym, eu.etaxonomy.cdm.model.taxon.Taxon)}.
|
99
|
*/
|
100
|
@Test
|
101
|
public final void testChangeSynonymToAcceptedTaxon() {
|
102
|
|
103
|
t1.addSynonym(s1, homoTypicSynonymRelationshipType);
|
104
|
HomotypicalGroup oldGroup = s1.getName().getHomotypicalGroup();
|
105
|
Assert.assertEquals("Homotypical group of new accepted taxon should contain exactly one name", 1, oldGroup.getTypifiedNames().size());
|
106
|
Taxon taxon = service.changeSynonymToAcceptedTaxon(s1, t1,false, true, null, null);
|
107
|
|
108
|
|
109
|
Assert.assertTrue("Former accepted taxon should not have synonyms anymore", t1.getSynonyms().isEmpty());
|
110
|
Assert.assertNotNull(taxon);
|
111
|
Assert.assertEquals(s1n, taxon.getName());
|
112
|
HomotypicalGroup newGroup = taxon.getName().getHomotypicalGroup();
|
113
|
Assert.assertEquals("Homotypical group of new accepted taxon should contain exactly one name", 1, newGroup.getTypifiedNames().size());
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#changeSynonymToAcceptedTaxon(eu.etaxonomy.cdm.model.taxon.Synonym, eu.etaxonomy.cdm.model.taxon.Taxon)}.
|
118
|
*/
|
119
|
@Test
|
120
|
public final void testChangeSynonymWithMultipleSynonymsInHomotypicalGroupToAcceptedTaxon() {
|
121
|
t1.addSynonym(s1, heteroTypicSynonymRelationshipType);
|
122
|
TaxonNameBase otherHeteroSynonymName = NonViralName.NewInstance(null);
|
123
|
t1.addHeterotypicSynonymName(otherHeteroSynonymName);
|
124
|
TaxonNameBase homotypicSynonymName = NonViralName.NewInstance(null);
|
125
|
Synonym homotypicSynonym = Synonym.NewInstance(homotypicSynonymName, t1.getSec());
|
126
|
t1.addHomotypicSynonym(homotypicSynonym, null, null);
|
127
|
|
128
|
HomotypicalGroup group = s1.getHomotypicGroup();
|
129
|
Reference citation1 = ReferenceFactory.newBook();
|
130
|
String microReference1 = "p. 55";
|
131
|
SynonymRelationship s2rel = t1.addHeterotypicSynonymName(s2n, group, citation1, microReference1);
|
132
|
Synonym s2 = s2rel.getSynonym();
|
133
|
HomotypicalGroup homoGroup2 = s1.getHomotypicGroup();
|
134
|
Assert.assertEquals("Homotypical group must be the same group as for the old synonym", group, homoGroup2);
|
135
|
|
136
|
Taxon newTaxon = service.changeSynonymToAcceptedTaxon(s1, t1, false, true, null, null);
|
137
|
|
138
|
Assert.assertEquals("Former accepted taxon should now have 2 synonyms left", 2, t1.getSynonyms().size());
|
139
|
Assert.assertEquals("Former accepted taxon should now have 1 heterotypic synonym group left", 1, t1.getHeterotypicSynonymyGroups().size());
|
140
|
|
141
|
Assert.assertNotNull(newTaxon);
|
142
|
Assert.assertEquals(s1n, newTaxon.getName());
|
143
|
Assert.assertEquals("New accepted taxon should have 1 synonym", 1, newTaxon.getSynonyms().size());
|
144
|
Assert.assertEquals("The new synonym must be the homotypic synonym of the old synonym", s2, newTaxon.getSynonyms().iterator().next());
|
145
|
|
146
|
HomotypicalGroup homoGroup = newTaxon.getHomotypicGroup();
|
147
|
Assert.assertEquals("Homotypical group must be the same group as for the old synonym", group, homoGroup);
|
148
|
List<Synonym> synonymsInNewTaxonsGroup = homoGroup.getSynonymsInGroup(newTaxon.getSec());
|
149
|
Assert.assertEquals("New accepted taxons homotypic group should have 2 synonym: s2 and the old synonym (which has not been deleted)", 2, synonymsInNewTaxonsGroup.size());
|
150
|
Assert.assertTrue("The old synonym's homotypic 'partner' must be a synonym of the new accepted taxon, too.", synonymsInNewTaxonsGroup.contains(s2));
|
151
|
Assert.assertTrue("The old synonym must be in the new accepted taxons homotypic group as it has not been deleted ", synonymsInNewTaxonsGroup.contains(s2));
|
152
|
boolean iWasHere = false;
|
153
|
for (Synonym syn : synonymsInNewTaxonsGroup){
|
154
|
if (syn.equals(s2) ){
|
155
|
SynonymRelationship rel = s2.getSynonymRelations().iterator().next();
|
156
|
Assert.assertEquals("s2 relationship needs to have the same citation as the former relation to the given accepted taxon.", citation1, rel.getCitation());
|
157
|
iWasHere = true;
|
158
|
}
|
159
|
}
|
160
|
Assert.assertTrue("Relationship to s2 must have been concidered in 'for'-loop", iWasHere);
|
161
|
|
162
|
try {
|
163
|
Taxon newTaxon2 = service.changeSynonymToAcceptedTaxon(homotypicSynonym, t1, false, true, null, null);
|
164
|
Assert.fail("The method should throw an exception when invoked on taxa in the same homotypical group");
|
165
|
} catch (IllegalArgumentException e) {
|
166
|
//OK
|
167
|
}
|
168
|
|
169
|
// Assert.assertNull("Synonym should not be used in a name anymore", s1.getName());
|
170
|
|
171
|
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#changeSynonymToRelatedTaxon(eu.etaxonomy.cdm.model.taxon.Synonym, eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType, eu.etaxonomy.cdm.model.reference.Reference, java.lang.String)}.
|
176
|
*/
|
177
|
@Test
|
178
|
public final void testChangeSynonymToRelatedTaxon() {
|
179
|
t1.addSynonym(s1, homoTypicSynonymRelationshipType);
|
180
|
service.changeSynonymToRelatedTaxon(s1, t2, TaxonRelationshipType.CONGRUENT_OR_EXCLUDES(), reference, referenceDetail);
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#moveSynonymToAnotherTaxon(eu.etaxonomy.cdm.model.taxon.SynonymRelationship, eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType, eu.etaxonomy.cdm.model.reference.Reference, java.lang.String)}.
|
185
|
*/
|
186
|
@Test
|
187
|
public final void testMoveSynonymToAnotherTaxon() {
|
188
|
t1.addSynonym(s1, homoTypicSynonymRelationshipType);
|
189
|
|
190
|
SynonymRelationship synonymRelation = t1.getSynonymRelations().iterator().next();
|
191
|
|
192
|
service.moveSynonymToAnotherTaxon(synonymRelation, t2, homoTypicSynonymRelationshipType, reference, referenceDetail);
|
193
|
|
194
|
Assert.assertTrue("t1 should have no synonym relationships", t1.getSynonymRelations().isEmpty());
|
195
|
|
196
|
Set<SynonymRelationship> synonymRelations = t2.getSynonymRelations();
|
197
|
Assert.assertTrue("t2 should have exactly one synonym relationship", synonymRelations.size() == 1);
|
198
|
|
199
|
synonymRelation = synonymRelations.iterator().next();
|
200
|
|
201
|
Assert.assertEquals(t2, synonymRelation.getAcceptedTaxon());
|
202
|
Assert.assertEquals(homoTypicSynonymRelationshipType, synonymRelation.getType());
|
203
|
Assert.assertEquals(reference, synonymRelation.getCitation());
|
204
|
Assert.assertEquals(referenceDetail, synonymRelation.getCitationMicroReference());
|
205
|
}
|
206
|
|
207
|
@Test
|
208
|
public void changeHomotypicalGroupOfSynonym(){
|
209
|
|
210
|
//s1 - Heterotypic
|
211
|
t1.addSynonym(s1, heteroTypicSynonymRelationshipType);
|
212
|
|
213
|
//s2 - heterotypic
|
214
|
TaxonNameBase otherHeteroSynonymName = NonViralName.NewInstance(null);
|
215
|
Synonym s2 = Synonym.NewInstance(otherHeteroSynonymName, t1.getSec());
|
216
|
t1.addSynonym(s2, heteroTypicSynonymRelationshipType, null, null);
|
217
|
TaxonNameBase otherHeteroSynonymNameB = NonViralName.NewInstance(null);
|
218
|
otherHeteroSynonymName.addBasionym(otherHeteroSynonymNameB);
|
219
|
Synonym s2b = Synonym.NewInstance(otherHeteroSynonymNameB, t1.getSec());
|
220
|
t1.addSynonym(s2b, heteroTypicSynonymRelationshipType, null, null);
|
221
|
|
222
|
//homotypic
|
223
|
TaxonNameBase homotypicSynonymName = NonViralName.NewInstance(null);
|
224
|
Synonym homotypicSynonym = Synonym.NewInstance(homotypicSynonymName, t1.getSec());
|
225
|
t1.addHomotypicSynonym(homotypicSynonym, null, null);
|
226
|
t1.getName().addBasionym(homotypicSynonymName);
|
227
|
|
228
|
//Preconditions test
|
229
|
Assert.assertFalse("s2 must not be in s1 homotypic group", s2.getHomotypicGroup().equals(s1.getHomotypicGroup()));
|
230
|
Assert.assertFalse("s2 must not be in t1 homotypic group", s2.getHomotypicGroup().equals(t1.getHomotypicGroup()));
|
231
|
Assert.assertEquals("s2 must have exactly 1 synonym relationship", 1, s2.getSynonymRelations().size());
|
232
|
Assert.assertEquals("s2 must have heterotypic relationship", heteroTypicSynonymRelationshipType, s2.getSynonymRelations().iterator().next().getType());
|
233
|
Assert.assertEquals("s2 must have exactly 1 basionym relationships", 1, s2.getName().getBasionyms().size());
|
234
|
|
235
|
//do it
|
236
|
service.changeHomotypicalGroupOfSynonym(s2, s1.getHomotypicGroup(), t1, false, true);
|
237
|
|
238
|
//postconditions
|
239
|
Assert.assertEquals("s2 must be in s1 homotypic group", s2.getHomotypicGroup(), s1.getHomotypicGroup());
|
240
|
Assert.assertEquals("s2 must have exactly 1 synonym relationship", 1, s2.getSynonymRelations().size());
|
241
|
Assert.assertEquals("s2 must have heterotypic relationship", heteroTypicSynonymRelationshipType, s2.getSynonymRelations().iterator().next().getType());
|
242
|
Assert.assertEquals("s2 must have exactly 0 basionym relationships", 0, s2.getName().getBasionyms().size());
|
243
|
|
244
|
|
245
|
//Preconditions test
|
246
|
Assert.assertEquals("'homotypicSynonym' must have exactly 1 basionym relationships", 1, homotypicSynonym.getName().getNameRelations().size());
|
247
|
Assert.assertEquals("'t1' must have exactly 1 basionym relationships", 1, t1.getName().getBasionyms().size());
|
248
|
Assert.assertFalse("s2 must not be in t1 homotypic group", s2.getHomotypicGroup().equals(t1.getHomotypicGroup()));
|
249
|
|
250
|
|
251
|
//do it
|
252
|
service.changeHomotypicalGroupOfSynonym(s2, homotypicSynonym.getHomotypicGroup(), null, false, true);
|
253
|
|
254
|
//postconditions
|
255
|
Assert.assertEquals("s2 must be in 'homotypicSynonym' homotypic group", s2.getHomotypicGroup(), homotypicSynonym.getHomotypicGroup());
|
256
|
Assert.assertEquals("s2 must be in 't1' homotypic group", s2.getHomotypicGroup(), t1.getHomotypicGroup());
|
257
|
Assert.assertEquals("s2 must have exactly 1 synonym relationship", 1, s2.getSynonymRelations().size());
|
258
|
Assert.assertEquals("s2 must have homotypic relationship", this.homoTypicSynonymRelationshipType, s2.getSynonymRelations().iterator().next().getType());
|
259
|
Assert.assertEquals("s2 must have exactly 1 basionym relationships", 1, s2.getName().getBasionyms().size());
|
260
|
Assert.assertEquals("'homotypicSynonym' must have exactly 2 basionym relationships", 2, homotypicSynonym.getName().getNameRelations().size());
|
261
|
Assert.assertEquals("'t1' must have exactly 1 basionym relationships", 1, t1.getName().getBasionyms().size());
|
262
|
Assert.assertEquals("'t1' must have exactly 2 homotypic synonyms", 2, t1.getHomotypicSynonymsByHomotypicRelationship().size());
|
263
|
Assert.assertEquals("'t1' must have exactly 2 names in homotypic group", 2, t1.getHomotypicSynonymsByHomotypicGroup().size());
|
264
|
Assert.assertEquals("'t1' homotypic group must include 3 names (t1, s2, homotypicSynonym)", 3, t1.getHomotypicGroup().getTypifiedNames().size());
|
265
|
|
266
|
|
267
|
//do it
|
268
|
service.changeHomotypicalGroupOfSynonym(s2, t2.getHomotypicGroup(), t2, true, false);
|
269
|
|
270
|
//postconditions
|
271
|
Assert.assertEquals("s2 must be in 't2' homotypic group", t2.getHomotypicGroup(), s2.getHomotypicGroup());
|
272
|
Assert.assertFalse("s2 must not be in 't1' homotypic group", s2.getHomotypicGroup().equals(t1.getHomotypicGroup()));
|
273
|
Assert.assertEquals("s2 must have exactly 1 synonym relationship", 1, s2.getSynonymRelations().size());
|
274
|
Assert.assertEquals("s2 must have homotypic relationship", this.homoTypicSynonymRelationshipType, s2.getSynonymRelations().iterator().next().getType());
|
275
|
Assert.assertEquals("s2 must have exactly 0 basionym relationships", 0, s2.getName().getBasionyms().size());
|
276
|
Assert.assertEquals("'homotypicSynonym' must have exactly 1 basionym relationships", 1, homotypicSynonym.getName().getNameRelations().size());
|
277
|
Assert.assertEquals("'t1' must have exactly 1 basionym relationships", 1, t1.getName().getBasionyms().size());
|
278
|
Assert.assertEquals("'t1' must have exactly 1 homotypic synonyms", 1, t1.getHomotypicSynonymsByHomotypicRelationship().size());
|
279
|
Assert.assertEquals("'t1' must have exactly 1 names in homotypic group", 1, t1.getHomotypicSynonymsByHomotypicGroup().size());
|
280
|
Assert.assertEquals("'t1' homotypic group must include 2 names (t1, homotypicSynonym)", 2, t1.getHomotypicGroup().getTypifiedNames().size());
|
281
|
|
282
|
//do it
|
283
|
service.changeHomotypicalGroupOfSynonym(s2, s1.getHomotypicGroup(), t1, false, false);
|
284
|
|
285
|
//postconditions
|
286
|
Assert.assertEquals("s2 must be in s1 homotypic group", s2.getHomotypicGroup(), s1.getHomotypicGroup());
|
287
|
Assert.assertFalse("s2 must not be in 't2' homotypic group", t2.getHomotypicGroup().equals(s2.getHomotypicGroup()));
|
288
|
Assert.assertEquals("s2 must have exactly 2 synonym relationships", 2, s2.getSynonymRelations().size());
|
289
|
for (SynonymRelationship rel: s2.getSynonymRelations()){
|
290
|
Assert.assertEquals("Both relationships of s2 must be heterotypic", heteroTypicSynonymRelationshipType, rel.getType());
|
291
|
}
|
292
|
Assert.assertEquals("s2 must have exactly 0 basionym relationships", 0, s2.getName().getBasionyms().size());
|
293
|
|
294
|
}
|
295
|
|
296
|
}
|