Project

General

Profile

Revision d5240fab

IDd5240fab5d07d1663fb0fcfa3bc83e57d19b7971
Parent 689cc1b7
Child 62ae841b

Added by Katja Luther almost 3 years ago

add comparator test for sortByName

View differences:

cdmlib-model/src/test/java/eu/etaxonomy/cdm/model/taxon/TaxonComparatorTest.java
19 19
import org.junit.BeforeClass;
20 20
import org.junit.Test;
21 21

  
22
import eu.etaxonomy.cdm.model.agent.Person;
23
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
22 24
import eu.etaxonomy.cdm.model.common.TimePeriod;
23 25
import eu.etaxonomy.cdm.model.name.IBotanicalName;
24 26
import eu.etaxonomy.cdm.model.name.IZoologicalName;
27
import eu.etaxonomy.cdm.model.name.Rank;
25 28
import eu.etaxonomy.cdm.model.name.TaxonName;
26 29
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
27 30
//import eu.etaxonomy.cdm.model.reference.Book;
......
35 38
public class TaxonComparatorTest {
36 39
    private static final Logger logger = Logger.getLogger(TaxonComparatorTest.class);
37 40

  
38
    /**
39
     * @throws java.lang.Exception
40
     */
41 41
    @BeforeClass
42
    public static void setUpBeforeClass() throws Exception {
42
    public static void setUpBeforeClass() {
43
        DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
44
        vocabularyStore.initialize();
43 45
    }
44 46

  
45 47

  
......
51 53
    @Test
52 54
    public void testCompare() {
53 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() {
54 84
        Reference sec = ReferenceFactory.newBook();
55 85

  
56
        Reference ref1 = ReferenceFactory.newBook();
86
        Reference ref1 = ReferenceFactory.newArticle();
87
        ref1.setAbbrevTitle("Gard. Dict. ed. 7 110, 2 1759");
57 88
        Reference ref2 = ReferenceFactory.newBook();
58 89
        Reference ref3 = ReferenceFactory.newBook();
59 90
        Calendar cal1 = Calendar.getInstance();
60 91
        Calendar cal2 = Calendar.getInstance();
61 92
        Calendar cal3 = Calendar.getInstance();
62
        cal1.set(1945, 3, 2);
93
        cal1.set(1759, 3, 2);
63 94
        cal2.set(1856, 3, 2);
64 95
        cal3.set(1943, 3, 2);
65 96

  
......
67 98
//		ref2.setDatePublished(TimePeriod.NewInstance(cal2));
68 99
        ref3.setDatePublished(TimePeriod.NewInstance(cal3));
69 100

  
70
        IBotanicalName botName1 =  TaxonNameFactory.NewBotanicalInstance(null);
71
        IBotanicalName botName2 =  TaxonNameFactory.NewBotanicalInstance(null);
72
        IBotanicalName botName3 =  TaxonNameFactory.NewBotanicalInstance(null);
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

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

  
75
        botName1.setNomenclaturalReference(ref1);
76
        botName2.setNomenclaturalReference(ref2);
77
        botName3.setNomenclaturalReference(ref3);
127

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

  
80 131
        List<TaxonBase<?>> list = new ArrayList<TaxonBase<?>>();
......
82 133
        Taxon taxon1 = Taxon.NewInstance(botName1, sec);
83 134
        Taxon taxon2 = Taxon.NewInstance(botName2, sec);
84 135
        Taxon taxon3 = Taxon.NewInstance(botName3, sec);
136
        Taxon taxon4 = Taxon.NewInstance(botName4, sec);
85 137
        Taxon zooTaxon4 = Taxon.NewInstance(zooName1, sec);
86 138

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

  
92 145
        list.add(taxon3);
93 146
        list.add(taxon2);
94 147
        list.add(taxon1);
148
        list.add(taxon4);
95 149
        list.add(zooTaxon4);
96
        Collections.sort(list, new TaxonComparator());
97

  
98
        //Order should be
99
//        4: 1823
100
//        3: 1943
101
//        1: 1945
102
//        2:
103
        Assert.assertEquals(list.get(0).getId(), 4);
104
        Assert.assertEquals(getYear(list.get(0)), "1823");
105
        Assert.assertEquals(list.get(1).getId(), 3);
106
        Assert.assertEquals(getYear(list.get(1)), "1943");
107
        Assert.assertEquals(list.get(2).getId(), 1);
108
        Assert.assertEquals(getYear(list.get(2)), "1945");
109
        Assert.assertEquals(list.get(3).getId(), 2);
110
        Assert.assertEquals(getYear(list.get(3)), "");
111

  
150
        return list;
151
    }
112 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;
113 167
    }
114 168

  
115 169

  
......
127 181
        }
128 182
        return year;
129 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
    }
130 209
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)