Project

General

Profile

Download (10.4 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9
package eu.etaxonomy.cdm.model.taxon;
10

    
11
import static org.junit.Assert.assertEquals;
12
import static org.junit.Assert.assertFalse;
13
import static org.junit.Assert.assertNotNull;
14
import static org.junit.Assert.assertSame;
15
import static org.junit.Assert.assertTrue;
16

    
17
import java.util.Set;
18

    
19
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
20
import org.junit.Assert;
21
import org.junit.Before;
22
import org.junit.Test;
23
import org.springframework.beans.BeanUtils;
24

    
25
import eu.etaxonomy.cdm.model.agent.Person;
26
import eu.etaxonomy.cdm.model.description.TaxonDescription;
27
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
28
import eu.etaxonomy.cdm.model.name.IBotanicalName;
29
import eu.etaxonomy.cdm.model.name.IZoologicalName;
30
import eu.etaxonomy.cdm.model.name.Rank;
31
import eu.etaxonomy.cdm.model.name.TaxonName;
32
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
33
//import eu.etaxonomy.cdm.model.reference.Book;
34
import eu.etaxonomy.cdm.model.reference.Reference;
35
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
36
import eu.etaxonomy.cdm.test.unit.EntityTestBase;
37

    
38
public class TaxonTest extends EntityTestBase {
39

    
40
    @SuppressWarnings("unused")
41
    private static final Logger logger = LogManager.getLogger(TaxonTest.class);
42

    
43
	private Reference sec;
44
	private Reference misSec;
45
	private IZoologicalName name1;
46
	private IBotanicalName name2;
47
	private Taxon rootT;
48
	private Taxon child1;
49
	private Taxon child2;
50
	private Synonym syn1;
51
	private Synonym syn2;
52
	private IBotanicalName name3;
53
	private IBotanicalName name4;
54
	private Taxon freeT;
55
	private Taxon misTaxon1;
56
	private Taxon misTaxon2;
57

    
58
	@Before
59
	public void setUp() throws Exception {
60
		Person linne =new Person("Carl", "Linné", "L.");
61
		sec= ReferenceFactory.newBook();
62
		sec.setAuthorship(linne);
63
		sec.setTitleCache("Schönes saftiges Allgäu", true);
64
		misSec = ReferenceFactory.newBook();
65
		misSec.setTitleCache("Stupid book", true);
66

    
67
		name1 = TaxonNameFactory.NewZoologicalInstance(Rank.SPECIES(),"Panthera",null,"onca",null,linne,null,"p.1467", null);
68
		name2 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES(),"Abies",null,"alba",null,linne,null,"p.317", null);
69
		name3 = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES(),"Polygala",null,"vulgaris","alpina",linne,null,"p.191", null);
70
		name4 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES(),"Cichoria",null,"carminata",null,linne,null,"p.14", null);
71
		rootT = Taxon.NewInstance(name1,sec);
72
		freeT = Taxon.NewInstance(name4,sec);
73
		// taxonomic children
74
		child1 = Taxon.NewInstance(name2,sec);
75
		child2 = Taxon.NewInstance(name3,sec);
76

    
77
//		Classification newTree = Classification.NewInstance("testTree");
78
//		newTree.addParentChild(rootT, child1, sec, "p.998");
79
//		newTree.addParentChild(rootT, child2, sec, "p.987");
80

    
81
		// synonymy
82
		syn1=Synonym.NewInstance(name1,sec);
83
		syn2=Synonym.NewInstance(name2,sec);
84
		child1.addSynonym(syn1, SynonymType.HETEROTYPIC_SYNONYM_OF());
85
		child2.addSynonym(syn2, SynonymType.HOMOTYPIC_SYNONYM_OF());
86
		//misaplication
87
		misTaxon1 = Taxon.NewInstance(name4, misSec);
88
		misTaxon2 = Taxon.NewInstance(name4, misSec);
89
		rootT.addMisappliedName(misTaxon1, null, "99");
90
		child1.addMisappliedName(misTaxon2, null, "44");
91
	}
92

    
93
	@Test
94
	public void testGenerateTitle() {
95
		assertTrue(rootT.generateTitle().startsWith(rootT.getName().getTitleCache()));
96
	}
97

    
98
	@Test
99
	public void testIsMisappliedName() {
100
		assertFalse(child2.isMisapplication());
101
		assertFalse(rootT.isMisapplication());
102
		assertTrue(misTaxon1.isMisapplication());
103
		assertTrue(misTaxon2.isMisapplication());
104
	}
105

    
106
	@Test
107
	public void testGetSynonyms() {
108
		assertTrue(child1.getSynonyms().contains(syn1));
109
		assertTrue(child2.getSynonyms().contains(syn2));
110
		assertTrue(rootT.getSynonyms().isEmpty());
111
	}
112

    
113
	@Test
114
	public void testGetSynonymNames() {
115
		assertTrue(child1.getSynonymNames().contains(name1));
116
		assertTrue(child2.getSynonymNames().contains(name2));
117
		assertTrue(rootT.getSynonymNames().isEmpty());
118
	}
119

    
120
	@Test
121
	public void testAddSynonym() {
122
		freeT.addSynonym(syn1, SynonymType.HETEROTYPIC_SYNONYM_OF());
123
		assertTrue(freeT.getSynonyms().contains(syn1));
124
		assertTrue(syn1.getAcceptedTaxon().equals(freeT));
125
		assertFalse(freeT.getSynonyms().contains(syn2));
126
	}
127

    
128
	@Test
129
	public void testAddAndRemoveDescriptionTaxonDescription() {
130
		TaxonDescription desc = TaxonDescription.NewInstance();
131
		rootT.addDescription(desc);  //test if reflection method in addDescription() works
132
		assertTrue("The description has not properly been added to the taxon", rootT.getDescriptions().contains(desc));
133
		assertEquals("The taxon has not properly been added to the description", rootT, desc.getTaxon());
134
		rootT.removeDescription(desc); //test if reflection method in removeDescription() works
135
		assertFalse("The description has not properly been removed from the taxon", rootT.getDescriptions().contains(desc));
136
		assertEquals("The taxon has not properly been removed from the description", null, desc.getTaxon());
137
	}
138

    
139
	@Test
140
	public void testGetTaxonRelations() {
141
		Taxon taxon = Taxon.NewInstance(null, null);
142
		taxon.addTaxonRelation(Taxon.NewInstance(null, null), TaxonRelationshipType.CONTRADICTION(), null, null);
143
		Set<TaxonRelationship> relationships = taxon.getTaxonRelations();
144
		assertTrue("There should be exactly one relationship", relationships.size() == 1);
145
	}
146

    
147
	@Test
148
	public void testRemoveTaxonRelation() {
149
		Taxon taxon = Taxon.NewInstance(null, null);
150
		taxon.addTaxonRelation(Taxon.NewInstance(null, null), TaxonRelationshipType.CONTRADICTION(), null, null);
151
		assertTrue("There should be exactly one taxon relationship", taxon.getTaxonRelations().size() == 1);
152
		TaxonRelationship relationship = (TaxonRelationship) taxon.getTaxonRelations().toArray()[0];
153
		assertNotNull("Relationship should not be null", relationship);
154
		taxon.removeTaxonRelation(relationship);
155
		assertTrue("There should be no taxon relationships", taxon.getTaxonRelations().size() == 0);
156
	}
157

    
158
	@Test
159
	public void testAddTaxonRelationTaxonRelationship() {
160
		Taxon taxon = Taxon.NewInstance(null, null);
161
		taxon.addTaxonRelation(Taxon.NewInstance(null, null), TaxonRelationshipType.CONTRADICTION(), null, null);
162
		assertTrue("There should be exactly one taxon relationship", taxon.getTaxonRelations().size() == 1);
163
		TaxonRelationship relationship = (TaxonRelationship) taxon.getTaxonRelations().toArray()[0];
164
		assertNotNull("Relationship should not be null", relationship);
165
		taxon.removeTaxonRelation(relationship);
166
		assertTrue("There should be no taxon relationships", taxon.getTaxonRelations().size() == 0);
167
		taxon.addTaxonRelation(relationship);
168
		assertEquals("There should be exactly one taxon relationships", 1, taxon.getTaxonRelations().size());
169
	}
170

    
171
    @Test
172
    public void testAddHomotypicSynonymName(){
173
        TaxonName taxonName = TaxonNameFactory.NewBotanicalInstance(null);
174
        Taxon taxon = Taxon.NewInstance(taxonName, null);
175
        TaxonName synonymName1 = TaxonNameFactory.NewBotanicalInstance(null);
176
        // add a synonym to the taxon
177
        Synonym synonym1 = taxon.addHomotypicSynonymName(synonymName1);
178
        // get the homotypic group of that synonym
179
        HomotypicalGroup homotypicGroupOfSynonym = synonym1.getHomotypicGroup();
180
        // everything is fine
181
        Assert.assertEquals("We should have two names in the homotypic group",
182
                2, homotypicGroupOfSynonym.getTypifiedNames().size());
183
    }
184

    
185
    @Test
186
    public void testAddHomotypicSynonym(){
187
        TaxonName taxonName = TaxonNameFactory.NewBotanicalInstance(null);
188
        Taxon taxon = Taxon.NewInstance(taxonName, null);
189
        TaxonName synonymName1 = TaxonNameFactory.NewBotanicalInstance(null);
190
        Synonym synonym = Synonym.NewInstance(synonymName1, null);
191
        // add a synonym to the taxon
192
        taxon.addHomotypicSynonym(synonym);
193
        //synonym type must be homotypic
194
        Assert.assertEquals("Synonym must be homotypic", SynonymType.HOMOTYPIC_SYNONYM_OF(), synonym.getType());
195
        // get the homotypic group of that synonym
196
        HomotypicalGroup homotypicGroupOfSynonym = synonym.getHomotypicGroup();
197
        // everything is fine
198
        Assert.assertEquals("We should have two names in the homotypic group",
199
                2, homotypicGroupOfSynonym.getTypifiedNames().size());
200
    }
201

    
202
	@Test
203
	public void testAddRemoveSynonymInSameGroup(){
204
		TaxonName taxonName = TaxonNameFactory.NewBotanicalInstance(null);
205
		Taxon taxon = Taxon.NewInstance(taxonName, null);
206
		TaxonName synonymName1 = TaxonNameFactory.NewBotanicalInstance(null);
207
		TaxonName synonymName2 = TaxonNameFactory.NewBotanicalInstance(null);
208

    
209
		// add a synonym to the taxon
210
		Synonym synonym1 = taxon.addHeterotypicSynonymName(synonymName1);
211
		// get the homotypic group of that synonym
212
		HomotypicalGroup homotypicGroupOfSynonym = synonym1.getHomotypicGroup();
213
		// add another synonym into the homotypic group we just created
214
		Synonym synonym2 = taxon.addHeterotypicSynonymName(synonymName2, null, null, homotypicGroupOfSynonym);
215
		// everything is fine
216
		Assert.assertEquals("We should have two synonyms in the group", 2, taxon.getSynonymsInGroup(homotypicGroupOfSynonym).size());
217

    
218
		// removing the synonym from the taxon
219
		taxon.removeSynonym(synonym2);
220

    
221
		// get the homotypical group via the methods in Taxon
222
		HomotypicalGroup homotypicGroupViaTaxon = taxon.getHeterotypicSynonymyGroups().iterator().next();
223

    
224
		// the group is for sure the same as the synonyms one
225
		Assert.assertSame("Accessing the homotypic group via the taxon methods should result in the same object",
226
		        homotypicGroupOfSynonym, homotypicGroupViaTaxon);
227

    
228
		// although it might be correct that the synonym is not deleted from the taxonomic group
229
		// we would not expect it to be here, since we just deleted it from the taxon and are accessing synonyms
230
		// via methods in Taxon
231
		Assert.assertEquals("When accessing the homotypic groups via taxon we would not expect the synonym we just deleted",
232
				1, taxon.getSynonymsInGroup(homotypicGroupViaTaxon).size());
233
	}
234

    
235
	@Test
236
	public void testClone(){
237
		Taxon clone = child2.clone();
238
		assertNotNull(clone);
239
		assertEquals(0,clone.getTaxonNodes().size());
240
		assertSame(clone.getName(), child2.getName());
241
	}
242

    
243
	@Test
244
	public void beanTests(){
245
//	    #5307 Test that BeanUtils does not fail
246
	    BeanUtils.getPropertyDescriptors(Taxon.class);
247
	    BeanUtils.getPropertyDescriptors(Synonym.class);
248
	}
249
}
(5-5/5)