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.common.TimePeriod;
|
23
|
import eu.etaxonomy.cdm.model.name.IBotanicalName;
|
24
|
import eu.etaxonomy.cdm.model.name.IZoologicalName;
|
25
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
26
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
27
|
//import eu.etaxonomy.cdm.model.reference.Book;
|
28
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
29
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
30
|
|
31
|
/**
|
32
|
* @author a.mueller
|
33
|
* @created 11.06.2008
|
34
|
*/
|
35
|
public class TaxonComparatorTest {
|
36
|
private static final Logger logger = Logger.getLogger(TaxonComparatorTest.class);
|
37
|
|
38
|
/**
|
39
|
* @throws java.lang.Exception
|
40
|
*/
|
41
|
@BeforeClass
|
42
|
public static void setUpBeforeClass() throws Exception {
|
43
|
}
|
44
|
|
45
|
|
46
|
/******************** TESTS *****************************************************/
|
47
|
|
48
|
/**
|
49
|
* Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonComparator#compare(eu.etaxonomy.cdm.model.taxon.TaxonBase, eu.etaxonomy.cdm.model.taxon.TaxonBase)}.
|
50
|
*/
|
51
|
@Test
|
52
|
public void testCompare() {
|
53
|
|
54
|
Reference sec = ReferenceFactory.newBook();
|
55
|
|
56
|
Reference ref1 = ReferenceFactory.newBook();
|
57
|
Reference ref2 = ReferenceFactory.newBook();
|
58
|
Reference ref3 = ReferenceFactory.newBook();
|
59
|
Calendar cal1 = Calendar.getInstance();
|
60
|
Calendar cal2 = Calendar.getInstance();
|
61
|
Calendar cal3 = Calendar.getInstance();
|
62
|
cal1.set(1945, 3, 2);
|
63
|
cal2.set(1856, 3, 2);
|
64
|
cal3.set(1943, 3, 2);
|
65
|
|
66
|
ref1.setDatePublished(TimePeriod.NewInstance(cal1));
|
67
|
// ref2.setDatePublished(TimePeriod.NewInstance(cal2));
|
68
|
ref3.setDatePublished(TimePeriod.NewInstance(cal3));
|
69
|
|
70
|
IBotanicalName botName1 = TaxonNameFactory.NewBotanicalInstance(null);
|
71
|
IBotanicalName botName2 = TaxonNameFactory.NewBotanicalInstance(null);
|
72
|
IBotanicalName botName3 = TaxonNameFactory.NewBotanicalInstance(null);
|
73
|
IZoologicalName zooName1 = TaxonNameFactory.NewZoologicalInstance(null);
|
74
|
|
75
|
botName1.setNomenclaturalReference(ref1);
|
76
|
botName2.setNomenclaturalReference(ref2);
|
77
|
botName3.setNomenclaturalReference(ref3);
|
78
|
zooName1.setPublicationYear(1823);
|
79
|
|
80
|
List<TaxonBase<?>> list = new ArrayList<TaxonBase<?>>();
|
81
|
|
82
|
Taxon taxon1 = Taxon.NewInstance(botName1, sec);
|
83
|
Taxon taxon2 = Taxon.NewInstance(botName2, sec);
|
84
|
Taxon taxon3 = Taxon.NewInstance(botName3, sec);
|
85
|
Taxon zooTaxon4 = Taxon.NewInstance(zooName1, sec);
|
86
|
|
87
|
taxon1.setId(1);
|
88
|
taxon2.setId(2);
|
89
|
taxon3.setId(3);
|
90
|
zooTaxon4.setId(4);
|
91
|
|
92
|
list.add(taxon3);
|
93
|
list.add(taxon2);
|
94
|
list.add(taxon1);
|
95
|
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
|
|
112
|
|
113
|
}
|
114
|
|
115
|
|
116
|
/**
|
117
|
* @param taxonBase
|
118
|
* @return
|
119
|
*/
|
120
|
private String getYear(TaxonBase<?> taxon) {
|
121
|
String year = "";
|
122
|
TaxonName tnb = taxon.getName();
|
123
|
if (tnb.isZoological()){
|
124
|
year = String.valueOf(tnb.getPublicationYear());
|
125
|
}else{
|
126
|
year = tnb.getNomenclaturalReference().getYear();
|
127
|
}
|
128
|
return year;
|
129
|
}
|
130
|
}
|