Revision d5d79aea
Added by Andreas Müller about 8 years ago
cdmlib-model/src/test/java/eu/etaxonomy/cdm/model/taxon/TaxonComparatorTest.java | ||
---|---|---|
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.After; |
|
19 |
import org.junit.AfterClass; |
|
20 |
import org.junit.Before; |
|
21 |
import org.junit.BeforeClass; |
|
22 |
import org.junit.Test; |
|
23 |
|
|
24 |
import eu.etaxonomy.cdm.model.common.TimePeriod; |
|
25 |
import eu.etaxonomy.cdm.model.name.BotanicalName; |
|
26 |
import eu.etaxonomy.cdm.model.name.TaxonNameBase; |
|
27 |
import eu.etaxonomy.cdm.model.name.ZoologicalName; |
|
28 |
//import eu.etaxonomy.cdm.model.reference.Book; |
|
29 |
import eu.etaxonomy.cdm.model.reference.Reference; |
|
30 |
import eu.etaxonomy.cdm.model.reference.ReferenceFactory; |
|
31 |
|
|
32 |
/** |
|
33 |
* @author a.mueller |
|
34 |
* @created 11.06.2008 |
|
35 |
* @version 1.0 |
|
36 |
*/ |
|
37 |
public class TaxonComparatorTest { |
|
38 |
private static final Logger logger = Logger.getLogger(TaxonComparatorTest.class); |
|
39 |
|
|
40 |
/** |
|
41 |
* @throws java.lang.Exception |
|
42 |
*/ |
|
43 |
@BeforeClass |
|
44 |
public static void setUpBeforeClass() throws Exception { |
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* @throws java.lang.Exception |
|
49 |
*/ |
|
50 |
@AfterClass |
|
51 |
public static void tearDownAfterClass() throws Exception { |
|
52 |
} |
|
53 |
|
|
54 |
/** |
|
55 |
* @throws java.lang.Exception |
|
56 |
*/ |
|
57 |
@Before |
|
58 |
public void setUp() throws Exception { |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* @throws java.lang.Exception |
|
63 |
*/ |
|
64 |
@After |
|
65 |
public void tearDown() throws Exception { |
|
66 |
} |
|
67 |
|
|
68 |
/******************** TESTS *****************************************************/ |
|
69 |
|
|
70 |
/** |
|
71 |
* Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonComparator#compare(eu.etaxonomy.cdm.model.taxon.TaxonBase, eu.etaxonomy.cdm.model.taxon.TaxonBase)}. |
|
72 |
*/ |
|
73 |
@Test |
|
74 |
public void testCompare() { |
|
75 |
logger.debug("start testCompare"); |
|
76 |
|
|
77 |
Reference sec = ReferenceFactory.newBook(); |
|
78 |
|
|
79 |
Reference ref1 = ReferenceFactory.newBook(); |
|
80 |
Reference ref2 = ReferenceFactory.newBook(); |
|
81 |
Reference ref3 = ReferenceFactory.newBook(); |
|
82 |
Calendar cal1 = Calendar.getInstance(); |
|
83 |
Calendar cal2 = Calendar.getInstance(); |
|
84 |
Calendar cal3 = Calendar.getInstance(); |
|
85 |
cal1.set(1945, 3, 2); |
|
86 |
cal2.set(1856, 3, 2); |
|
87 |
cal3.set(1943, 3, 2); |
|
88 |
|
|
89 |
ref1.setDatePublished(TimePeriod.NewInstance(cal1)); |
|
90 |
// ref2.setDatePublished(TimePeriod.NewInstance(cal2)); |
|
91 |
ref3.setDatePublished(TimePeriod.NewInstance(cal3)); |
|
92 |
|
|
93 |
BotanicalName botName1 = BotanicalName.NewInstance(null); |
|
94 |
BotanicalName botName2 = BotanicalName.NewInstance(null); |
|
95 |
BotanicalName botName3 = BotanicalName.NewInstance(null); |
|
96 |
ZoologicalName zooName1 = ZoologicalName.NewInstance(null); |
|
97 |
|
|
98 |
botName1.setNomenclaturalReference(ref1); |
|
99 |
botName2.setNomenclaturalReference(ref2); |
|
100 |
botName3.setNomenclaturalReference(ref3); |
|
101 |
zooName1.setPublicationYear(1823); |
|
102 |
|
|
103 |
List<TaxonBase> list = new ArrayList<TaxonBase>(); |
|
104 |
|
|
105 |
Taxon taxon1 = Taxon.NewInstance(botName1, sec); |
|
106 |
Taxon taxon2 = Taxon.NewInstance(botName2, sec); |
|
107 |
Taxon taxon3 = Taxon.NewInstance(botName3, sec); |
|
108 |
Taxon zooTaxon4 = Taxon.NewInstance(zooName1, sec); |
|
109 |
|
|
110 |
taxon1.setId(1); |
|
111 |
taxon2.setId(2); |
|
112 |
taxon3.setId(3); |
|
113 |
zooTaxon4.setId(4); |
|
114 |
|
|
115 |
|
|
116 |
list.add(taxon3); |
|
117 |
list.add(taxon2); |
|
118 |
list.add(taxon1); |
|
119 |
list.add(zooTaxon4); |
|
120 |
Collections.sort(list, new TaxonComparator()); |
|
121 |
|
|
122 |
for (TaxonBase taxon : list){ |
|
123 |
String year = ""; |
|
124 |
TaxonNameBase<?,?> tnb = taxon.getName(); |
|
125 |
if (tnb instanceof ZoologicalName){ |
|
126 |
year = String.valueOf(((ZoologicalName)tnb).getPublicationYear()); |
|
127 |
}else{ |
|
128 |
year = tnb.getNomenclaturalReference().getYear(); |
|
129 |
} |
|
130 |
System.out.println(taxon.getId() + ": " + year); |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
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 |
} |
Also available in: Unified diff
Improve sort order within a homotypic group (new Comparator) #3338