Project

General

Profile

Download (6.9 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

    
10
package eu.etaxonomy.cdm.model.taxon;
11

    
12
import java.util.ArrayList;
13
import java.util.Calendar;
14
import java.util.Collections;
15
import java.util.List;
16

    
17
import org.apache.log4j.Logger;
18
import org.junit.Assert;
19
import org.junit.BeforeClass;
20
import org.junit.Test;
21

    
22
import eu.etaxonomy.cdm.model.agent.Person;
23
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
24
import eu.etaxonomy.cdm.model.common.TimePeriod;
25
import eu.etaxonomy.cdm.model.name.IBotanicalName;
26
import eu.etaxonomy.cdm.model.name.IZoologicalName;
27
import eu.etaxonomy.cdm.model.name.Rank;
28
import eu.etaxonomy.cdm.model.name.TaxonName;
29
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
30
//import eu.etaxonomy.cdm.model.reference.Book;
31
import eu.etaxonomy.cdm.model.reference.Reference;
32
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
33

    
34
/**
35
 * @author a.mueller
36
 * @since 11.06.2008
37
 */
38
public class TaxonComparatorTest {
39
    private static final Logger logger = Logger.getLogger(TaxonComparatorTest.class);
40

    
41
    @BeforeClass
42
    public static void setUpBeforeClass() {
43
        DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
44
        vocabularyStore.initialize();
45
    }
46

    
47

    
48
/******************** TESTS *****************************************************/
49

    
50
    /**
51
     * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonComparator#compare(eu.etaxonomy.cdm.model.taxon.TaxonBase, eu.etaxonomy.cdm.model.taxon.TaxonBase)}.
52
     */
53
    @Test
54
    public void testCompare() {
55

    
56
        List<TaxonBase<?>> list = createTestData();
57
        Collections.sort(list, new TaxonComparator());
58

    
59
        //Order should be
60
//        4: 1823
61
//        3: 1943
62
//        1: 1945
63
//        2:
64
        Assert.assertEquals(list.get(0).getId(), 3);
65
        Assert.assertEquals(getYear(list.get(0)), "1759");
66
        Assert.assertEquals(list.get(1).getId(), 1);
67
        Assert.assertEquals(getYear(list.get(1)), "1759");
68
        Assert.assertEquals(list.get(2).getId(), 5);
69
        Assert.assertEquals(getYear(list.get(2)), "1823");
70
        Assert.assertEquals(list.get(3).getId(), 4);
71
        Assert.assertEquals(getYear(list.get(3)), "1943");
72
        Assert.assertEquals(list.get(4).getId(), 2);
73
        Assert.assertEquals(getYear(list.get(4)), "");
74

    
75

    
76

    
77
    }
78

    
79

    
80
    /**
81
     * @return
82
     */
83
    private List<TaxonBase<?>> createTestData() {
84
        Reference sec = ReferenceFactory.newBook();
85

    
86
        Reference ref1 = ReferenceFactory.newArticle();
87
        ref1.setAbbrevTitle("Gard. Dict. ed. 7 110, 2 1759");
88
        Reference ref2 = ReferenceFactory.newBook();
89
        Reference ref3 = ReferenceFactory.newBook();
90
        Calendar cal1 = Calendar.getInstance();
91
        Calendar cal2 = Calendar.getInstance();
92
        Calendar cal3 = Calendar.getInstance();
93
        cal1.set(1759, 3, 2);
94
        cal2.set(1856, 3, 2);
95
        cal3.set(1943, 3, 2);
96

    
97
        ref1.setDatePublished(TimePeriod.NewInstance(cal1));
98
//		ref2.setDatePublished(TimePeriod.NewInstance(cal2));
99
        ref3.setDatePublished(TimePeriod.NewInstance(cal3));
100

    
101
        Person author1 = Person.NewTitledInstance("Rehder");
102
        author1.setNomenclaturalTitle("Rehder");
103

    
104
        Person author4 = Person.NewTitledInstance("Mill.");
105
        author1.setNomenclaturalTitle("Mill.");
106

    
107
        Person author2 = Person.NewTitledInstance("A.Murray");
108
        author2.setNomenclaturalTitle("A.Murray");
109
        Person author3 = Person.NewTitledInstance("Lemmon");
110
        author3.setNomenclaturalTitle("Lemmon");
111

    
112
        IBotanicalName botName1 =  TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES(),
113
                "Abies", null, "procera",null,
114
                author1, ref1, null, null);
115
        IBotanicalName botName2 =  TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES(),
116
                "Abies", null, "magnifica", null,
117
                author2, ref2, null, null);
118
        IBotanicalName botName3 =   TaxonNameFactory.NewBotanicalInstance(Rank.GENUS(),
119
                "Abies", null, null,null,
120
                author4, ref1, null, null);
121
        IBotanicalName botName4 =   TaxonNameFactory.NewBotanicalInstance(Rank.VARIETY(),
122
                "Abies", null, "magnifica","shastensis",
123
                author3, ref3, null, null);
124

    
125
        IZoologicalName zooName1 = TaxonNameFactory.NewZoologicalInstance(null);
126

    
127

    
128
       // botName3.setNomenclaturalReference(ref3);
129
        zooName1.setPublicationYear(1823);
130

    
131
        List<TaxonBase<?>> list = new ArrayList<TaxonBase<?>>();
132

    
133
        Taxon taxon1 = Taxon.NewInstance(botName1, sec);
134
        Taxon taxon2 = Taxon.NewInstance(botName2, sec);
135
        Taxon taxon3 = Taxon.NewInstance(botName3, sec);
136
        Taxon taxon4 = Taxon.NewInstance(botName4, sec);
137
        Taxon zooTaxon4 = Taxon.NewInstance(zooName1, sec);
138

    
139
        taxon1.setId(1);
140
        taxon2.setId(2);
141
        taxon3.setId(3);
142
        taxon4.setId(4);
143
        zooTaxon4.setId(5);
144

    
145
        list.add(taxon3);
146
        list.add(taxon2);
147
        list.add(taxon1);
148
        list.add(taxon4);
149
        list.add(zooTaxon4);
150
        return list;
151
    }
152

    
153
    private List<TaxonNode> createTestDataWithTaxonNodes(){
154
        List<TaxonBase<?>> taxa = createTestData();
155
        List<TaxonNode> result = new ArrayList();
156
        TaxonNode node;
157
        Classification classification = Classification.NewInstance("TestClassification");
158
        int index = 0;
159
        for (TaxonBase<?> taxon: taxa){
160
            if (taxon instanceof Taxon){
161
                node = classification.addChildTaxon((Taxon)taxon, null, null);
162
                node.setId(++index);
163
                result.add(node);
164
            }
165
        }
166
        return result;
167
    }
168

    
169

    
170
    /**
171
     * @param taxonBase
172
     * @return
173
     */
174
    private String getYear(TaxonBase<?> taxon) {
175
        String year = "";
176
        TaxonName tnb = taxon.getName();
177
        if (tnb.isZoological()){
178
            year = String.valueOf(tnb.getPublicationYear());
179
        }else{
180
            year = tnb.getNomenclaturalReference().getYear();
181
        }
182
        return year;
183
    }
184

    
185
    @Test
186
    public void testCompareTaxonNodeByName(){
187
        List<TaxonNode> list = createTestDataWithTaxonNodes();
188

    
189
        Collections.sort(list, new TaxonNodeByNameComparator());
190
        for(TaxonNode node: list){
191
            System.out.println(node.getTaxon().getName().getFullTitleCache());
192
        }
193

    
194
        //Order should be
195
//        5: without rank
196
//        1: Genus
197
//        2: Species (Abies magnifica A.Murray)
198
//        4: subSpecies (Abies magnifica var. shastensis Lemmon)
199
//        3: Species (Abies procera Rehder)
200
        Assert.assertEquals(list.get(0).getId(), 5);
201
        Assert.assertEquals(list.get(1).getId(), 1);
202
        Assert.assertEquals(list.get(2).getId(), 2);
203
        Assert.assertEquals(list.get(3).getId(), 4);
204
        Assert.assertEquals(list.get(4).getId(), 3);
205

    
206

    
207

    
208
    }
209
}
(4-4/7)