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