2 * Copyright (C) 2011 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.api
.service
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collections
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.junit
.Assert
;
16 import org
.junit
.Ignore
;
17 import org
.junit
.Test
;
19 import eu
.etaxonomy
.cdm
.model
.name
.IBotanicalName
;
20 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
21 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameFactory
;
22 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
23 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
24 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNaturalComparator
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNodeByNameComparator
;
29 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDto
;
32 * @author a.kohlbecker
36 public class TaxonNodeDtoByNameComparatorTest
{
38 private static final Logger logger
= Logger
.getLogger(TaxonNodeDtoByNameComparatorTest
.class);
42 * test method for {@link TaxonNodeByNameComparator#compare(eu.etaxonomy.cdm.model.taxon.TaxonNode
43 * , eu.etaxonomy.cdm.model.taxon.TaxonNode) }
47 public void testCompare() {
48 Classification classification
= Classification
.NewInstance("Greuther, 1993");
50 Reference sec
= ReferenceFactory
.newBook();
52 IBotanicalName botname_1
= TaxonNameFactory
.NewBotanicalInstance(Rank
.SPECIES());
53 String nameCache_1
= "Epilobium \u00D7aschersonianum Hausskn.";
54 botname_1
.setNameCache(nameCache_1
, true);
55 Taxon taxon_1
= Taxon
.NewInstance(botname_1
, sec
);
57 IBotanicalName botname_2
= TaxonNameFactory
.NewBotanicalInstance(Rank
.SPECIES());
58 String nameCache_2
= "\u00D7Epilobium \u00D7angustifolium";
59 botname_2
.setNameCache(nameCache_2
, true);
60 Taxon taxon_2
= Taxon
.NewInstance(botname_2
, sec
);
62 IBotanicalName botname_3
= TaxonNameFactory
.NewBotanicalInstance(Rank
.SPECIES());
63 String nameCache_3
= "Epilobium lamyi";
64 botname_3
.setNameCache(nameCache_3
, true);
65 Taxon taxon_3
= Taxon
.NewInstance(botname_3
, sec
);
67 IBotanicalName botname_4
= TaxonNameFactory
.NewBotanicalInstance(Rank
.SPECIES());
68 String nameCache_4
= "Epilobium tournefortii";
69 botname_4
.setNameCache(nameCache_4
, true);
70 Taxon taxon_4
= Taxon
.NewInstance(botname_4
, sec
);
72 classification
.addChildTaxon(taxon_1
, sec
, null);
73 classification
.addChildTaxon(taxon_2
, sec
, null);
74 classification
.addChildTaxon(taxon_3
, sec
, null);
75 classification
.addChildTaxon(taxon_4
, sec
, null);
77 classification
.getChildNodes();
78 ArrayList
<TaxonNode
> taxonNodes
= new ArrayList
<TaxonNode
>();
79 taxonNodes
.addAll(classification
.getChildNodes());
81 // order using default settings
82 TaxonNodeByNameComparator taxonNodeByNameComparator
= new TaxonNodeByNameComparator();
84 // Collections.sort(taxonNodes, taxonNodeByNameComparator);
86 logger
.debug("order using default settings");
87 logger
.debug(taxonNodes
.get(i
).getTaxon().getName().getNameCache());
88 Assert
.assertEquals(nameCache_2
, taxonNodes
.get(i
++).getTaxon().getName().getNameCache());
89 logger
.debug(taxonNodes
.get(i
).getTaxon().getName().getNameCache());
90 Assert
.assertEquals(nameCache_1
, taxonNodes
.get(i
++).getTaxon().getName().getNameCache());
91 logger
.debug(taxonNodes
.get(i
).getTaxon().getName().getNameCache());
92 Assert
.assertEquals(nameCache_3
, taxonNodes
.get(i
++).getTaxon().getName().getNameCache());
93 logger
.debug(taxonNodes
.get(i
).getTaxon().getName().getNameCache());
94 Assert
.assertEquals(nameCache_4
, taxonNodes
.get(i
++).getTaxon().getName().getNameCache());
96 // order without ignoring hybrid signs
97 taxonNodeByNameComparator
.setIgnoreHybridSign(false);
99 Collections
.sort(taxonNodes
, taxonNodeByNameComparator
);
102 logger
.debug("order without ignoring hybrid signs");
103 logger
.debug(taxonNodes
.get(i
).getTaxon().getName().getNameCache());
104 Assert
.assertEquals(nameCache_3
, taxonNodes
.get(i
++).getTaxon().getName().getNameCache());
105 logger
.debug(taxonNodes
.get(i
).getTaxon().getName().getNameCache());
106 Assert
.assertEquals(nameCache_4
, taxonNodes
.get(i
++).getTaxon().getName().getNameCache());
107 logger
.debug(taxonNodes
.get(i
).getTaxon().getName().getNameCache());
108 Assert
.assertEquals(nameCache_1
, taxonNodes
.get(i
++).getTaxon().getName().getNameCache());
109 logger
.debug(taxonNodes
.get(i
).getTaxon().getName().getNameCache());
110 Assert
.assertEquals(nameCache_2
, taxonNodes
.get(i
++).getTaxon().getName().getNameCache());
115 public void testNullSave() {
116 Classification classification
= Classification
.NewInstance("Greuther, 1993");
118 Reference sec
= ReferenceFactory
.newBook();
120 IBotanicalName botname_1
= TaxonNameFactory
.NewBotanicalInstance(Rank
.SPECIES());
121 String nameCache_1
= "Epilobium \u00D7aschersonianum Hausskn.";
122 botname_1
.setNameCache(nameCache_1
, true);
123 Taxon taxon_1
= Taxon
.NewInstance(botname_1
, sec
);
125 IBotanicalName botname_2
= TaxonNameFactory
.NewBotanicalInstance(Rank
.SPECIES());
126 String nameCache_2
= "\u00D7Epilobium \u00D7angustifolium";
127 botname_2
.setNameCache(nameCache_2
, true);
128 Taxon taxon_2
= Taxon
.NewInstance(botname_2
, sec
);
130 TaxonNodeDto node1
= new TaxonNodeDto(classification
.addChildTaxon(taxon_1
, sec
, null));
131 TaxonNodeDto node2
= new TaxonNodeDto(classification
.addChildTaxon(taxon_2
, sec
, null));
133 TaxonNodeDtoByNameComparator taxonNodeByNameComparator
= new TaxonNodeDtoByNameComparator();
135 Assert
.assertEquals(0, taxonNodeByNameComparator
.compare(null, null));
136 Assert
.assertEquals(-1, taxonNodeByNameComparator
.compare(node1
, null));
137 Assert
.assertEquals(1, taxonNodeByNameComparator
.compare(null, node1
));
142 * test method for {@link TaxonNodeByNameComparator#compare(eu.etaxonomy.cdm.model.taxon.TaxonNode
143 * , eu.etaxonomy.cdm.model.taxon.TaxonNode) }
147 public void testCompareNaturalOrder() {
148 Classification classification
= Classification
.NewInstance("Greuther, 1993");
150 Reference sec
= ReferenceFactory
.newBook();
152 IBotanicalName botname_1
= TaxonNameFactory
.NewBotanicalInstance(Rank
.SPECIES());
153 String nameCache_1
= "Epilobium \u00D7aschersonianum Hausskn.";
154 botname_1
.setNameCache(nameCache_1
, true);
155 Taxon taxon_1
= Taxon
.NewInstance(botname_1
, sec
);
157 IBotanicalName botname_2
= TaxonNameFactory
.NewBotanicalInstance(Rank
.SPECIES());
158 String nameCache_2
= "\u00D7Epilobium \u00D7angustifolium";
159 botname_2
.setNameCache(nameCache_2
, true);
160 Taxon taxon_2
= Taxon
.NewInstance(botname_2
, sec
);
162 IBotanicalName botname_3
= TaxonNameFactory
.NewBotanicalInstance(Rank
.SPECIES());
163 String nameCache_3
= "Epilobium lamyi";
164 botname_3
.setNameCache(nameCache_3
, true);
165 Taxon taxon_3
= Taxon
.NewInstance(botname_3
, sec
);
167 IBotanicalName botname_4
= TaxonNameFactory
.NewBotanicalInstance(Rank
.SPECIES());
168 String nameCache_4
= "Epilobium tournefortii";
169 botname_4
.setNameCache(nameCache_4
, true);
170 Taxon taxon_4
= Taxon
.NewInstance(botname_4
, sec
);
172 IBotanicalName botname_5
= TaxonNameFactory
.NewBotanicalInstance(Rank
.SPECIES());
173 String nameCache_5
= "Epilobium hirsutum L.";
174 botname_5
.setNameCache(nameCache_5
, true);
175 Taxon taxon_5
= Taxon
.NewInstance(botname_5
, sec
);
177 TaxonNode node1
= classification
.addChildTaxon(taxon_1
, sec
, null);
178 node1
.addChildTaxon(taxon_2
, sec
, null);
179 TaxonNode node2
= classification
.addChildTaxon(taxon_3
, sec
, null);
180 node2
.addChildTaxon(taxon_4
, sec
, null);
182 classification
.getChildNodes();
184 ArrayList
<TaxonNode
> taxonNodes
= new ArrayList
<TaxonNode
>();
185 taxonNodes
.addAll(classification
.getChildNodes());
187 // order using default settings
188 TaxonNaturalComparator taxonNodeComparator
= new TaxonNaturalComparator();
189 // Collections.sort(taxonNodes, taxonNodeComparator);
192 logger
.debug("order using default settings");
193 logger
.debug(taxonNodes
.get(i
).getTaxon().getName().getNameCache());
194 Assert
.assertEquals(nameCache_1
, taxonNodes
.get(i
).getTaxon().getName().getNameCache());
195 logger
.debug(taxonNodes
.get(i
++).getTaxon().getName().getNameCache());
196 Assert
.assertEquals(nameCache_2
, taxonNodes
.get(i
).getTaxon().getName().getNameCache());
197 logger
.debug(taxonNodes
.get(i
++).getTaxon().getName().getNameCache());
198 Assert
.assertEquals(nameCache_3
, taxonNodes
.get(i
).getTaxon().getName().getNameCache());
199 logger
.debug(taxonNodes
.get(i
++).getTaxon().getName().getNameCache());
200 Assert
.assertEquals(nameCache_4
, taxonNodes
.get(i
).getTaxon().getName().getNameCache());
202 TaxonNode result
= classification
.addChildTaxon(taxon_5
, 2, null, null);
203 Assert
.assertTrue(result
.getSortIndex() == 2);
207 logger
.debug(classification
.getChildNodes().get(i
).getTaxon().getName().getNameCache());
208 Assert
.assertEquals(nameCache_1
, classification
.getChildNodes().get(i
).getTaxon().getName().getNameCache());
209 logger
.debug(classification
.getChildNodes().get(i
++).getTaxon().getName().getNameCache());
210 Assert
.assertEquals(nameCache_2
, classification
.getChildNodes().get(i
).getTaxon().getName().getNameCache());
211 logger
.debug(classification
.getChildNodes().get(i
++).getTaxon().getName().getNameCache());
212 Assert
.assertEquals(nameCache_5
, classification
.getChildNodes().get(i
).getTaxon().getName().getNameCache());
213 logger
.debug(classification
.getChildNodes().get(i
++).getTaxon().getName().getNameCache());
214 Assert
.assertEquals(nameCache_3
, classification
.getChildNodes().get(i
).getTaxon().getName().getNameCache());