2 * Copyright (C) 2007 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
.model
.name
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertNotNull
;
15 import java
.util
.HashSet
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.junit
.After
;
20 import org
.junit
.Assert
;
21 import org
.junit
.Before
;
22 import org
.junit
.BeforeClass
;
23 import org
.junit
.Test
;
25 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
26 import eu
.etaxonomy
.cdm
.model
.common
.DefaultTermInitializer
;
27 import eu
.etaxonomy
.cdm
.model
.reference
.IGeneric
;
28 import eu
.etaxonomy
.cdm
.model
.reference
.INomenclaturalReference
;
29 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
30 import eu
.etaxonomy
.cdm
.test
.unit
.EntityTestBase
;
36 public class NonViralNameTest
extends EntityTestBase
{
37 private static Logger logger
= Logger
.getLogger(NonViralNameTest
.class);
40 NonViralName
<?
> nonViralName1
;
41 NonViralName
<?
> nonViralName2
;
44 public static void setUpBeforeClass() {
45 DefaultTermInitializer vocabularyStore
= new DefaultTermInitializer();
46 vocabularyStore
.initialize();
50 * @throws java.lang.Exception
53 public void setUp() throws Exception
{
54 nonViralName1
= new BotanicalName();
55 nonViralName2
= new BotanicalName();
59 * @throws java.lang.Exception
62 public void tearDown() throws Exception
{
65 // ******************* TESTS ***********************************/
68 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#generateTitle()}.
71 public final void testGenerateTitle() {
72 String fullName
= "Abies alba subsp. beta (L.) Mill.";
73 nonViralName1
.setGenusOrUninomial("Genus");
74 nonViralName1
.setSpecificEpithet("spec");
75 nonViralName1
.setRank(Rank
.SPECIES());
76 assertEquals("Genus spec", nonViralName1
.generateTitle());
77 assertEquals("", nonViralName2
.generateTitle());
81 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#isCodeCompliant()}.
84 public final void testIsCodeCompliant() {
85 logger
.warn("Not yet implemented"); // TODO
89 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#NonViralName()}.
92 public final void testNonViralName() {
93 assertNotNull(nonViralName1
);
97 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#NonViralName(eu.etaxonomy.cdm.model.name.Rank)}.
100 public final void testNonViralNameRank() {
101 NonViralName
<?
> nonViralName
= NonViralName
.NewInstance(Rank
.GENUS());
102 assertNotNull(nonViralName
);
106 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#NonViralName(eu.etaxonomy.cdm.model.name.Rank, java.lang.String, java.lang.String, java.lang.String, eu.etaxonomy.cdm.model.agent.Agent, eu.etaxonomy.cdm.model.reference.INomenclaturalReference, java.lang.String)}.
109 public final void testNonViralNameRankStringStringStringAgentINomenclaturalReferenceString() {
110 Team agent
= Team
.NewInstance();
111 INomenclaturalReference article
= ReferenceFactory
.newArticle();
112 HomotypicalGroup homotypicalGroup
= HomotypicalGroup
.NewInstance();
113 NonViralName
<?
> nonViralName
= new NonViralName(Rank
.GENUS(), "Genus", "infraGen", "species", "infraSpec", agent
, article
, "mikro", homotypicalGroup
);
114 assertEquals("Genus", nonViralName
.getGenusOrUninomial() );
115 assertEquals("infraGen", nonViralName
.getInfraGenericEpithet());
116 assertEquals("species", nonViralName
.getSpecificEpithet() );
117 assertEquals("infraSpec", nonViralName
.getInfraSpecificEpithet());
118 assertEquals(agent
, nonViralName
.getCombinationAuthorship() );
119 assertEquals(article
, nonViralName
.getNomenclaturalReference() );
120 assertEquals("mikro", nonViralName
.getNomenclaturalMicroReference() );
124 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#getCombinationAuthorship()}.
125 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#setCombinationAuthorship(eu.etaxonomy.cdm.model.agent.Agent)}.
128 public final void testGetSetCombinationAuthorship() {
129 Team team1
= Team
.NewInstance();
130 nonViralName1
.setCombinationAuthorship(team1
);
131 assertEquals(team1
, nonViralName1
.getCombinationAuthorship());
132 nonViralName1
.setCombinationAuthorship(null);
133 assertEquals(null, nonViralName1
.getCombinationAuthorship());
134 nonViralName2
.setCombinationAuthorship(null);
135 assertEquals(null, nonViralName2
.getCombinationAuthorship());
140 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#getExCombinationAuthorship()}.
143 public final void testGetSetExCombinationAuthorship() {
144 Team team1
= Team
.NewInstance();
145 nonViralName1
.setExCombinationAuthorship(team1
);
146 assertEquals(team1
, nonViralName1
.getExCombinationAuthorship());
147 nonViralName1
.setExCombinationAuthorship(null);
148 assertEquals(null, nonViralName1
.getExCombinationAuthorship());
149 nonViralName2
.setExCombinationAuthorship(null);
150 assertEquals(null, nonViralName2
.getExCombinationAuthorship());
155 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#getCombinationAuthorship()}.
156 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#setCombinationAuthorship(eu.etaxonomy.cdm.model.agent.Agent)}.
159 public final void testGetSetBasionymAuthorship() {
160 Team team1
= Team
.NewInstance();
161 nonViralName1
.setBasionymAuthorship(team1
);
162 assertEquals(team1
, nonViralName1
.getBasionymAuthorship());
163 nonViralName1
.setBasionymAuthorship(null);
164 assertEquals(null, nonViralName1
.getBasionymAuthorship());
165 nonViralName2
.setBasionymAuthorship(null);
166 assertEquals(null, nonViralName2
.getBasionymAuthorship());
171 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#getExCombinationAuthorship()}.
174 public final void testGetSetExBasionymAuthorship() {
175 Team team1
= Team
.NewInstance();
176 nonViralName1
.setExBasionymAuthorship(team1
);
177 assertEquals(team1
, nonViralName1
.getExBasionymAuthorship());
178 nonViralName1
.setExBasionymAuthorship(null);
179 assertEquals(null, nonViralName1
.getExBasionymAuthorship());
180 nonViralName2
.setExBasionymAuthorship(null);
181 assertEquals(null, nonViralName2
.getExBasionymAuthorship());
186 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#getGenusOrUninomial()}.
189 public final void testGetSetGenusOrUninomial() {
190 nonViralName1
.setGenusOrUninomial("Genus");
191 assertEquals("Genus", nonViralName1
.getGenusOrUninomial());
192 nonViralName1
.setGenusOrUninomial(null);
193 assertEquals(null, nonViralName1
.getGenusOrUninomial());
194 nonViralName2
.setGenusOrUninomial(null);
195 assertEquals(null, nonViralName2
.getGenusOrUninomial());
199 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#getInfragenericEpithet()}.
202 public final void testGetSetInfraGenericEpithet() {
203 nonViralName1
.setInfraGenericEpithet("InfraGenus");
204 assertEquals("InfraGenus", nonViralName1
.getInfraGenericEpithet());
205 nonViralName1
.setInfraGenericEpithet(null);
206 assertEquals(null, nonViralName1
.getInfraGenericEpithet());
207 nonViralName2
.setInfraGenericEpithet(null);
208 assertEquals(null, nonViralName2
.getInfraGenericEpithet());
212 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#getSpecificEpithet()}.
215 public final void testGetSetSpecificEpithet() {
216 nonViralName1
.setSpecificEpithet("specEpi");
217 assertEquals("specEpi", nonViralName1
.getSpecificEpithet());
218 nonViralName1
.setSpecificEpithet(null);
219 assertEquals(null, nonViralName1
.getSpecificEpithet());
220 nonViralName2
.setSpecificEpithet(null);
221 assertEquals(null, nonViralName2
.getSpecificEpithet());
225 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#getInfraSpecificEpithet()}.
228 public final void testGetSetInfraSpecificEpithet() {
229 nonViralName1
.setInfraSpecificEpithet("InfraSpecEpi");
230 assertEquals("InfraSpecEpi", nonViralName1
.getInfraSpecificEpithet());
231 nonViralName1
.setInfraSpecificEpithet(null);
232 assertEquals(null, nonViralName1
.getInfraSpecificEpithet());
233 nonViralName2
.setInfraSpecificEpithet(null);
234 assertEquals(null, nonViralName2
.getInfraSpecificEpithet());
238 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#setAuthorshipCache(java.lang.String)}.
239 * Test method for {@link eu.etaxonomy.cdm.model.name.NonViralName#getAuthorshipCache()}.
244 public final void testGetSetAuthorshipCache() {
245 String strTeam1
= "Team1";
246 String strTeam2
= "Team2";
247 String strTeam3
= "Team3";
248 IGeneric ref1
= ReferenceFactory
.newGeneric();
249 ref1
.setTitleCache("RefTitle",true);
251 Team team1
= Team
.NewInstance();
252 Team team2
= Team
.NewInstance();
253 team1
.setNomenclaturalTitle(strTeam1
);
254 team2
.setNomenclaturalTitle(strTeam2
);
255 nonViralName1
.setGenusOrUninomial("Abies");
256 nonViralName1
.setSpecificEpithet("alba");
257 nonViralName1
.setNomenclaturalReference(ref1
);
258 Assert
.assertEquals("Abies alba", nonViralName1
.getNameCache());
260 nonViralName1
.setCombinationAuthorship(team1
);
261 assertEquals(team1
, nonViralName1
.getCombinationAuthorship());
262 assertEquals(strTeam1
, nonViralName1
.getAuthorshipCache());
263 Assert
.assertEquals("Abies alba "+strTeam1
, nonViralName1
.getTitleCache());
264 Assert
.assertEquals("Abies alba "+strTeam1
+ ", RefTitle", nonViralName1
.getFullTitleCache());
266 nonViralName1
.setAuthorshipCache(strTeam2
);
267 assertEquals(strTeam2
, nonViralName1
.getAuthorshipCache());
268 nonViralName1
.setGenusOrUninomial("Calendula");
269 Assert
.assertEquals("Calendula alba "+strTeam2
, nonViralName1
.getTitleCache());
271 nonViralName1
.setAuthorshipCache(strTeam3
);
272 Assert
.assertEquals("Calendula alba "+strTeam3
, nonViralName1
.getTitleCache());
274 Assert
.assertEquals("Calendula alba "+strTeam3
+ ", RefTitle", nonViralName1
.getFullTitleCache());
276 nonViralName1
.setCombinationAuthorship(null);
277 assertEquals(null, nonViralName1
.getCombinationAuthorship());
282 public final void testGetChildAndParentRelationships() {
283 NonViralName
<?
> nonViralName1
= new NonViralName();
284 assertEquals(0, nonViralName1
.getHybridParentRelations().size());
285 assertEquals(0, nonViralName1
.getHybridChildRelations().size());
286 BotanicalName femaleParent
= BotanicalName
.NewInstance(null);
287 HybridRelationship hybridRelationship
= new HybridRelationship(nonViralName1
, femaleParent
, HybridRelationshipType
.FEMALE_PARENT(), null );
288 assertEquals(1, nonViralName1
.getHybridChildRelations().size());
289 assertEquals(hybridRelationship
, nonViralName1
.getHybridChildRelations().iterator().next());
290 assertEquals(1, femaleParent
.getHybridParentRelations().size());
294 public final void testAddHybridRelationships() {
295 NonViralName
<?
> nonViralName1
= new NonViralName();
296 assertEquals(0, nonViralName1
.getHybridParentRelations().size());
297 assertEquals(0, nonViralName1
.getHybridChildRelations().size());
298 BotanicalName femaleParent
= BotanicalName
.NewInstance(null);
299 BotanicalName maleParent
= BotanicalName
.NewInstance(null);
301 nonViralName1
.addHybridParent(femaleParent
, HybridRelationshipType
.MALE_PARENT(), null);
302 nonViralName1
.addHybridParent(maleParent
, HybridRelationshipType
.MALE_PARENT(), null);
304 assertEquals(2, nonViralName1
.getHybridChildRelations().size());
305 assertEquals(0, nonViralName1
.getHybridParentRelations().size());
306 assertEquals(1, maleParent
.getHybridParentRelations().size());
307 assertEquals(1, femaleParent
.getHybridParentRelations().size());
308 assertEquals(0, maleParent
.getHybridChildRelations().size());
309 assertEquals(0, femaleParent
.getHybridChildRelations().size());
313 @Test(expected
=IllegalArgumentException
.class)
314 public final void testAddHybridRelationship() {
315 NonViralName
<?
> nonViralName1
= new NonViralName();
316 assertEquals(0, nonViralName1
.getHybridParentRelations().size());
317 assertEquals(0, nonViralName1
.getHybridChildRelations().size());
318 NonViralName
<?
> botanicalName2
= new NonViralName();
319 botanicalName2
.addHybridRelationship(null);
323 public final void testRemoveHybridRelationship() {
324 NonViralName
<?
> botanicalName1
= new NonViralName();
325 assertEquals(0, botanicalName1
.getHybridParentRelations().size());
326 assertEquals(0, botanicalName1
.getHybridChildRelations().size());
327 BotanicalName femaleParent
= BotanicalName
.NewInstance(null);
328 NonViralName
<?
> maleParent
= NonViralName
.NewInstance(null);
329 ZoologicalName child
= ZoologicalName
.NewInstance(null);
331 botanicalName1
.addHybridParent(femaleParent
, HybridRelationshipType
.FEMALE_PARENT(), null);
332 botanicalName1
.addHybridParent(maleParent
, HybridRelationshipType
.MALE_PARENT(), null);
333 botanicalName1
.addHybridChild(child
, HybridRelationshipType
.FIRST_PARENT(), null);
334 assertEquals(2, botanicalName1
.getHybridChildRelations().size());
335 assertEquals(1, botanicalName1
.getHybridParentRelations().size());
336 assertEquals(1, child
.getHybridChildRelations().size());
338 botanicalName1
.removeHybridParent(femaleParent
);
339 assertEquals(1, botanicalName1
.getHybridChildRelations().size());
340 assertEquals(1, botanicalName1
.getHybridParentRelations().size());
342 botanicalName1
.removeHybridParent(maleParent
);
343 assertEquals(0, botanicalName1
.getHybridChildRelations().size());
344 assertEquals(1, botanicalName1
.getHybridParentRelations().size());
346 botanicalName1
.removeHybridChild(child
);
347 assertEquals(0, botanicalName1
.getHybridParentRelations().size());
350 botanicalName1
.removeHybridRelationship(null);
351 assertEquals(0, botanicalName1
.getHybridChildRelations().size());
357 public void testClone(){
359 Team combinationAuthor
= Team
.NewTitledInstance("CombinationAuthor", "comb. auth.");
360 nonViralName1
.setRank(Rank
.SUBSPECIES());
361 nonViralName1
.setCombinationAuthorship(combinationAuthor
);
362 nonViralName1
.setGenusOrUninomial("Aus");
363 nonViralName1
.setInfraGenericEpithet("Infaus");
364 nonViralName1
.setSpecificEpithet("bus");
365 nonViralName1
.setInfraSpecificEpithet("infrabus");
366 nonViralName1
.setBinomHybrid(true);
368 NonViralName
<?
> parent
= NonViralName
.NewInstance(Rank
.SPECIES());
369 NonViralName
<?
> parent2
= NonViralName
.NewInstance(Rank
.SPECIES());
370 NonViralName
<?
> child
= NonViralName
.NewInstance(Rank
.SPECIES());
371 NonViralName
<?
> child2
= NonViralName
.NewInstance(Rank
.SPECIES());
372 nonViralName1
.addHybridParent(parent
, HybridRelationshipType
.FIRST_PARENT(), "parent rule");
373 nonViralName1
.addHybridParent(parent2
, HybridRelationshipType
.SECOND_PARENT(), "parent rule2");
374 nonViralName1
.addHybridChild(child
, HybridRelationshipType
.FEMALE_PARENT(), "child rule");
377 INonViralName
<?
> clone
= (INonViralName
)nonViralName1
.clone();
378 Assert
.assertEquals("Genus should be equal", "Aus", clone
.getGenusOrUninomial());
379 Assert
.assertEquals("Infragenus should be equal", "Infaus", clone
.getInfraGenericEpithet());
380 Assert
.assertEquals("Specific epithet should be equal", "bus", clone
.getSpecificEpithet());
381 Assert
.assertEquals("Infraspecific epithet should be equal", "infrabus", clone
.getInfraSpecificEpithet());
382 Assert
.assertEquals("BinomHybrid should be equal", true, clone
.isBinomHybrid());
383 Assert
.assertSame("Combination author should be the same", combinationAuthor
, clone
.getCombinationAuthorship());
384 Assert
.assertEquals("NameCache should be equal", nonViralName1
.getNameCache(), clone
.getNameCache());
385 Assert
.assertEquals("AuthorshipCache should be equal", nonViralName1
.getAuthorshipCache(), clone
.getAuthorshipCache());
387 clone
.setSpecificEpithet("sub");
388 Assert
.assertEquals("NameCache should be changed", "Aus (\u00D7Infaus) sub subsp. infrabus", clone
.getNameCache());
390 //hybrid parents of clone
391 Assert
.assertEquals("There should be exactly 2 hybrid relationships in which the clone takes the child role", 2, clone
.getHybridChildRelations().size());
392 Set
<TaxonNameBase
> parentSet
= new HashSet
<>();
393 Set
<TaxonNameBase
> childSet
= new HashSet
<>();
394 for (Object object
: clone
.getHybridChildRelations()){
395 HybridRelationship childRelation
= (HybridRelationship
)object
;
396 TaxonNameBase
<?
,?
> relatedFrom
= childRelation
.getRelatedFrom();
397 parentSet
.add(relatedFrom
);
398 TaxonNameBase
<?
,?
> relatedTo
= childRelation
.getRelatedTo();
399 childSet
.add(relatedTo
);
401 Assert
.assertTrue("Parent set should contain parent1", parentSet
.contains(parent
));
402 Assert
.assertTrue("Parent set should contain parent2", parentSet
.contains(parent2
));
403 Assert
.assertTrue("Child set should contain clone", childSet
.contains(clone
));
405 //hybrid child of clone
406 Assert
.assertEquals("There should be exactly 1 hybrid relationship in which the clone takes the parent role", 1, clone
.getHybridParentRelations().size());
407 HybridRelationship parentRelation
= clone
.getHybridParentRelations().iterator().next();
408 Assert
.assertSame("Clone should be parent in parentRelationship", clone
, parentRelation
.getRelatedFrom());
409 Assert
.assertSame("Child should be child in parentRelationship", child
, parentRelation
.getRelatedTo());
410 Assert
.assertSame("Relationship type should be cloned correctly", HybridRelationshipType
.FEMALE_PARENT(), parentRelation
.getType());
411 Assert
.assertEquals("Rule should be cloned correctly", "child rule", parentRelation
.getRuleConsidered());