some fixes for modell change branche
[cdmlib.git] / cdmlib-model / src / test / java / eu / etaxonomy / cdm / model / taxon / TaxonNodeTest.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.cdm.model.taxon;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertSame;
17 import static org.junit.Assert.assertTrue;
18
19 import java.util.List;
20 import java.util.Set;
21
22 import org.apache.log4j.Logger;
23 import org.junit.After;
24 import org.junit.AfterClass;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28
29 import eu.etaxonomy.cdm.model.name.BotanicalName;
30 import eu.etaxonomy.cdm.model.name.Rank;
31 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
32 //import eu.etaxonomy.cdm.model.reference.Book;
33 //import eu.etaxonomy.cdm.model.reference.Journal;
34 import eu.etaxonomy.cdm.model.reference.Reference;
35 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
36
37 /**
38 * @author a.mueller
39 * @created 01.04.2009
40 * @version 1.0
41 */
42 public class TaxonNodeTest {
43 @SuppressWarnings("unused")
44 private static final Logger logger = Logger.getLogger(TaxonNodeTest.class);
45 private static String viewName1;
46 private static Classification classification1;
47 private static Classification classification2;
48 private static Taxon taxon1;
49 private static Taxon taxon2;
50 private static Taxon taxon3;
51 private static TaxonNameBase<?,?> taxonName1;
52 private static TaxonNameBase<?,?> taxonName2;
53 private static TaxonNameBase<?,?> taxonName3;
54 private static Reference ref1;
55 private static Reference ref2;
56 private static Reference ref3;
57 private static Synonym syn1;
58 /**
59 * @throws java.lang.Exception
60 */
61 @BeforeClass
62 public static void setUpBeforeClass() throws Exception {
63 }
64
65 /**
66 * @throws java.lang.Exception
67 */
68 @AfterClass
69 public static void tearDownAfterClass() throws Exception {
70 }
71
72 /**
73 * @throws java.lang.Exception
74 */
75 @Before
76 public void setUp() throws Exception {
77 viewName1 = "Greuther, 1993";
78 classification1 = Classification.NewInstance(viewName1);
79 classification2 = Classification.NewInstance("Test View 2");
80 taxonName1 = BotanicalName.NewInstance(Rank.SPECIES());
81 taxonName1 = BotanicalName.NewInstance(Rank.SUBSPECIES());
82 taxonName3 = BotanicalName.NewInstance(Rank.SPECIES());
83 ref1 = ReferenceFactory.newJournal();
84 ref2 = ReferenceFactory.newBook();
85 ref3 = ReferenceFactory.newGeneric();
86 taxon1 = Taxon.NewInstance(taxonName1, ref1);
87 taxon2 = Taxon.NewInstance(taxonName2, ref1);
88 taxon3 = Taxon.NewInstance(taxonName3, ref3);
89 //taxonNode1 = new TaxonNode(taxon1, taxonomicView1);
90 syn1 = Synonym.NewInstance(null, null);
91 }
92
93 /**
94 * @throws java.lang.Exception
95 */
96 @After
97 public void tearDown() throws Exception {
98 }
99
100 //****************************** TESTS *****************************************/
101
102
103 /**
104 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonNode#NewInstance(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.Classification)}.
105 */
106 @Test
107 public void testNewTaxonTaxonomicView() {
108 TaxonNode testNode = new TaxonNode(taxon1);
109 classification1.addChildNode(testNode, null, null);
110
111 assertNotNull("test node should not be null", testNode);
112 assertEquals(taxon1,testNode.getTaxon());
113 assertEquals(classification1,testNode.getClassification());
114 assertTrue("taxon1 must become part of taxonomicView1", classification1.isTaxonInTree(taxon1));
115 }
116
117 /**
118 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonNode#addChildNode(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.reference.Reference, java.lang.String, eu.etaxonomy.cdm.model.taxon.Synonym)}.
119 */
120 @Test
121 public void testAddChild() {
122 TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
123 assertEquals("Number of all nodes in view should be 1", 1, classification1.getAllNodes().size());
124
125 TaxonNode child = root.addChildTaxon(taxon2, ref2, "p33");
126 child.setSynonymToBeUsed(syn1); //originally synToBeUsed was part of addChildTaxon
127 //test child properties
128 assertNotNull("Child should not be null", child);
129 assertEquals("Child taxon should be taxon2", taxon2, child.getTaxon());
130 assertEquals("Parent taxon should be taxon1", taxon1, (child.getParent()).getTaxon());
131 assertEquals("Reference should be ref2", ref2, child.getReference());
132 assertEquals("Microreference should be 'p33'", "p33", child.getMicroReference());
133 assertEquals("Synonym should be syn1", syn1, child.getSynonymToBeUsed());
134
135 //test parent properties
136 List<TaxonNode> childList = root.getChildNodes();
137 assertFalse("parent child list must not be empty",childList.isEmpty());
138 assertEquals("size of child list be 1", 1, childList.size());
139 assertSame("taxa must be the same", taxon2, childList.iterator().next().getTaxon());
140
141 //test view properties
142 List<TaxonNode> rootNodes = classification1.getChildNodes();
143 assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
144 Set<TaxonNode> allNodes = classification1.getAllNodes();
145 assertEquals("Number of all nodes should be 2", 2, allNodes.size());
146 assertTrue("Taxonomic view should include child", allNodes.contains(child));
147
148
149 //is part of taxon
150 Set<TaxonNode> nodes2 = taxon2.getTaxonNodes();
151 assertFalse("taxon2 must not be empty", nodes2.isEmpty());
152 assertEquals("size of nodes of taxon2 must be 1", 1, nodes2.size());
153 assertSame("taxa must be the same", taxon2, nodes2.iterator().next().getTaxon());
154 }
155
156 /**
157 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonNode#setTaxon(eu.etaxonomy.cdm.model.taxon.Taxon)}.
158 */
159 @Test
160 public void testSetTaxon() {
161 TaxonNode node = new TaxonNode(taxon1);
162 classification1.addChildNode(node, null, null);
163 assertNotNull(taxon2);
164 node.setTaxon(taxon2);
165 assertSame("taxon must be the same", taxon2, node.getTaxon());
166 assertTrue("taxon2 must contain node", taxon2.getTaxonNodes().contains(node));
167 }
168
169 /**
170 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonNode#setParent(eu.etaxonomy.cdm.model.taxon.TaxonNode)}.
171 */
172 @Test
173 public void testSetParent() {
174 TaxonNode node = new TaxonNode(taxon1);
175 assertNotNull(taxon2);
176 TaxonNode parent = new TaxonNode(taxon2);
177 assertSame("Taxon must be the same", taxon2, parent.getTaxon());
178 classification1.addChildNode(parent, null, null);
179 node.setParent(parent);
180 assertSame("taxon2 must contain node", parent, node.getParent());
181 assertTrue("setParent must not handle child list of parent", parent.getChildNodes().isEmpty());
182 }
183
184 /**
185 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonNode#getChildNodes()}.
186 */
187 @Test
188 public void testGetChildNodes() {
189 TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
190 assertEquals("Number of all nodes in view should be 1", 1, classification1.getAllNodes().size());
191
192 TaxonNode child = root.addChildTaxon(taxon2, ref2, "p33");
193 child.setSynonymToBeUsed(syn1);
194
195 List<TaxonNode> childList = root.getChildNodes();
196 assertFalse("parent child list must not be empty",childList.isEmpty());
197 assertEquals("size of child list be 1", 1, childList.size());
198 assertSame("child must be in child list", child, childList.iterator().next());
199 assertSame("taxa must be the same", taxon2, childList.iterator().next().getTaxon());
200 }
201
202 @Test
203 public void testGetCountChildren(){
204 TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
205 assertEquals("Count of children must be 0", 0, root.getCountChildren());
206 TaxonNode child = root.addChildTaxon(taxon2, ref2, "p33");
207 child.setSynonymToBeUsed(syn1);
208 assertEquals("Count of children must be 1", 1, root.getCountChildren());
209 Taxon taxon3 = Taxon.NewInstance(null, null);
210 TaxonNode child2 = root.addChildTaxon(taxon3, null, null);
211 assertEquals("Count of children must be 2", 2, root.getCountChildren());
212 root.removeChildNode(child);
213 assertEquals("Count of children must be 1", 1, root.getCountChildren());
214 root.removeChildNode(child2);
215 assertEquals("Count of children must be 0", 0, root.getCountChildren());
216
217 }
218
219 @Test
220 public void testDelete(){
221 TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
222 assertEquals("Number of all nodes in view should be 1", 1, classification1.getAllNodes().size());
223
224
225 TaxonNode childNode = root.addChildTaxon(taxon2, null, null);
226 assertEquals("Count of children must be 1", 1, root.getCountChildren());
227
228 childNode.delete();
229 assertEquals("Count of children must be 0", 0, root.getCountChildren());
230
231
232 root.delete();
233 assertEquals("Number of all nodes in view should be 0", 0, classification1.getAllNodes().size());
234
235
236 }
237
238 @Test
239 public void testMoveTaxonNodeToOtherTree(){
240 TaxonNode node = classification1.addChildTaxon(taxon1, null, null);
241 assertEquals("The node should be in the classification we added it to", classification1, node.getClassification());
242
243 TaxonNode movedNode = classification2.addChildNode(node, null, null);
244 assertEquals("The node should be in the classification we moved it to", classification2, movedNode.getClassification());
245 assertEquals("The old tree should be empty now", 0, classification1.getChildNodes().size());
246 }
247
248 @Test
249 public void testMoveTaxonNodeToOtherTaxonNodeInDifferentTree(){
250 TaxonNode node1 = classification1.addChildTaxon(taxon1, null, null);
251 TaxonNode node2 = node1.addChildTaxon(taxon3, null, null);
252
253 assertEquals("The node should have exactly one child", 1, node1.getChildNodes().size());
254 assertEquals("The child is not in the correct tree", classification1, node2.getClassification());
255 assertEquals("The Classification should contain exactly two nodes", 2, classification1.getAllNodes().size());
256
257 TaxonNode node3 = classification2.addChildTaxon(taxon3, null, null);
258
259 // move node2 to node3 in other tree
260 node3.addChildNode(node2, null, null);
261
262 assertEquals("Old node should not have child nodes", 0, node1.getChildNodes().size());
263 assertEquals("Old tree should contain only one node now", 1, classification1.getAllNodes().size());
264 assertEquals("Moved node not in expected tree", classification2, node2.getClassification());
265 assertEquals("Count of nodes in new tree:", 2, classification2.getAllNodes().size());
266
267 }
268
269 /**
270 * Basically tests #setClassificationRecursively(Classification) which is a private method
271 */
272 @Test
273 public void testMoveTaxonNodesRecursivelyToOtherTaxonNodeInDifferentTree(){
274 TaxonNode node1 = classification1.addChildTaxon(taxon1, null, null);
275 TaxonNode node2 = node1.addChildTaxon(taxon2, null, null);
276 TaxonNode node3 = node2.addChildTaxon(taxon3, null, null);
277
278 //move the branch to a different tree
279 classification2.addChildNode(node1, null, null);
280
281 assertEquals("Old tree should be empty:", 0, classification1.getAllNodes().size());
282 assertEquals("Moved node not in expected tree:", classification2, node1.getClassification());
283 assertEquals("Recursively moved node not in expected tree:", classification2, node2.getClassification());
284 assertEquals("Recursively moved node not in expected tree:", classification2, node3.getClassification());
285
286 assertEquals("Count of nodes in new tree:", 3, classification2.getAllNodes().size());
287
288 }
289
290 @Test
291 public void testAddChildNode(){
292 TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
293 assertEquals("Number of all nodes in cla should be 1", 1, classification1.getAllNodes().size());
294
295 TaxonNode child = root.addChildTaxon(taxon2, ref2, "p33");
296 child.setSynonymToBeUsed(syn1); //originally synToBeUsed was part of addChildTaxon
297 //test child properties
298 assertNotNull("Child should not be null", child);
299 assertEquals("Child taxon should be taxon2", taxon2, child.getTaxon());
300 assertEquals("Parent taxon should be taxon1", taxon1, (child.getParent()).getTaxon());
301 assertEquals("Reference should be ref2", ref2, child.getReference());
302 assertEquals("Microreference should be 'p33'", "p33", child.getMicroReference());
303 assertEquals("Synonym should be syn1", syn1, child.getSynonymToBeUsed());
304
305 //test parent properties
306 List<TaxonNode> childList = root.getChildNodes();
307 assertFalse("parent child list must not be empty",childList.isEmpty());
308 assertEquals("size of child list be 1", 1, childList.size());
309 assertSame("taxa must be the same", taxon2, childList.iterator().next().getTaxon());
310
311 //test view properties
312 List<TaxonNode> rootNodes = classification1.getChildNodes();
313 assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
314 Set<TaxonNode> allNodes = classification1.getAllNodes();
315 assertEquals("Number of all nodes should be 2", 2, allNodes.size());
316 assertTrue("Taxonomic view should include child", allNodes.contains(child));
317
318 }
319
320 }