e6c27f626d0dccab6fafc8855b5696f5a31c6649
[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.Set;
20
21 import org.apache.log4j.Logger;
22 import org.junit.After;
23 import org.junit.AfterClass;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27
28 import eu.etaxonomy.cdm.model.name.BotanicalName;
29 import eu.etaxonomy.cdm.model.name.Rank;
30 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
31 //import eu.etaxonomy.cdm.model.reference.Book;
32 //import eu.etaxonomy.cdm.model.reference.Journal;
33 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
34 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
35
36 /**
37 * @author a.mueller
38 * @created 01.04.2009
39 * @version 1.0
40 */
41 public class TaxonNodeTest {
42 @SuppressWarnings("unused")
43 private static final Logger logger = Logger.getLogger(TaxonNodeTest.class);
44 private static String viewName1;
45 private static TaxonomicTree taxonomicView1;
46 private static TaxonomicTree taxonomicView2;
47 private static Taxon taxon1;
48 private static Taxon taxon2;
49 private static Taxon taxon3;
50 private static TaxonNameBase<?,?> taxonName1;
51 private static TaxonNameBase<?,?> taxonName2;
52 private static TaxonNameBase<?,?> taxonName3;
53 private static ReferenceBase ref1;
54 private static ReferenceBase ref2;
55 private static ReferenceBase ref3;
56 private static Synonym syn1;
57 /**
58 * @throws java.lang.Exception
59 */
60 @BeforeClass
61 public static void setUpBeforeClass() throws Exception {
62 }
63
64 /**
65 * @throws java.lang.Exception
66 */
67 @AfterClass
68 public static void tearDownAfterClass() throws Exception {
69 }
70
71 /**
72 * @throws java.lang.Exception
73 */
74 @Before
75 public void setUp() throws Exception {
76 viewName1 = "Greuther, 1993";
77 ReferenceFactory refFactory = ReferenceFactory.newInstance();
78 taxonomicView1 = TaxonomicTree.NewInstance(viewName1);
79 taxonomicView2 = TaxonomicTree.NewInstance("Test View 2");
80 taxonName1 = BotanicalName.NewInstance(Rank.SPECIES());
81 taxonName1 = BotanicalName.NewInstance(Rank.SUBSPECIES());
82 taxonName3 = BotanicalName.NewInstance(Rank.SPECIES());
83 ref1 = refFactory.newJournal();
84 ref2 = refFactory.newBook();
85 ref3 = refFactory.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.TaxonomicTree)}.
105 */
106 @Test
107 public void testNewTaxonTaxonomicView() {
108 TaxonNode testNode = new TaxonNode(taxon1);
109 taxonomicView1.addChildNode(testNode, null, null, null);
110
111 assertNotNull("test node should not be null", testNode);
112 assertEquals(taxon1,testNode.getTaxon());
113 assertEquals(taxonomicView1,testNode.getTaxonomicTree());
114 assertTrue("taxon1 must become part of taxonomicView1", taxonomicView1.isTaxonInTree(taxon1));
115 }
116
117 /**
118 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonNode#addChild(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.reference.ReferenceBase, java.lang.String, eu.etaxonomy.cdm.model.taxon.Synonym)}.
119 */
120 @Test
121 public void testAddChild() {
122 TaxonNode root = taxonomicView1.addChildTaxon(taxon1, null, null, null);
123 assertEquals("Number of all nodes in view should be 1", 1, taxonomicView1.getAllNodes().size());
124
125 TaxonNode child = root.addChildTaxon(taxon2, ref2, "p33", syn1);
126 //test child properties
127 assertNotNull("Child should not be null", child);
128 assertEquals("Child taxon should be taxon2", taxon2, child.getTaxon());
129 assertEquals("Parent taxon should be taxon1", taxon1, (child.getParent()).getTaxon());
130 assertEquals("Reference should be ref2", ref2, child.getReference());
131 assertEquals("Microreference should be 'p33'", "p33", child.getMicroReference());
132 assertEquals("Synonym should be syn1", syn1, child.getSynonymToBeUsed());
133
134 //test parent properties
135 Set<TaxonNode> childList = root.getChildNodes();
136 assertFalse("parent child list must not be empty",childList.isEmpty());
137 assertEquals("size of child list be 1", 1, childList.size());
138 assertSame("taxa must be the same", taxon2, childList.iterator().next().getTaxon());
139
140 //test view properties
141 Set<TaxonNode> rootNodes = taxonomicView1.getChildNodes();
142 assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
143 Set<TaxonNode> allNodes = taxonomicView1.getAllNodes();
144 assertEquals("Number of all nodes should be 2", 2, allNodes.size());
145 assertTrue("Taxonomic view should include child", allNodes.contains(child));
146
147
148 //is part of taxon
149 Set<TaxonNode> nodes2 = taxon2.getTaxonNodes();
150 assertFalse("taxon2 must not be empty", nodes2.isEmpty());
151 assertEquals("size of nodes of taxon2 must be 1", 1, nodes2.size());
152 assertSame("taxa must be the same", taxon2, nodes2.iterator().next().getTaxon());
153 }
154
155 /**
156 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonNode#setTaxon(eu.etaxonomy.cdm.model.taxon.Taxon)}.
157 */
158 @Test
159 public void testSetTaxon() {
160 TaxonNode node = new TaxonNode(taxon1);
161 taxonomicView1.addChildNode(node, null, null, null);
162 assertNotNull(taxon2);
163 node.setTaxon(taxon2);
164 assertSame("taxon must be the same", taxon2, node.getTaxon());
165 assertTrue("taxon2 must contain node", taxon2.getTaxonNodes().contains(node));
166 }
167
168 /**
169 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonNode#setParent(eu.etaxonomy.cdm.model.taxon.TaxonNode)}.
170 */
171 @Test
172 public void testSetParent() {
173 TaxonNode node = new TaxonNode(taxon1);
174 assertNotNull(taxon2);
175 TaxonNode parent = new TaxonNode(taxon2);
176 assertSame("Taxon must be the same", taxon2, parent.getTaxon());
177 taxonomicView1.addChildNode(parent, null, null, null);
178 node.setParent(parent);
179 assertSame("taxon2 must contain node", parent, node.getParent());
180 assertTrue("setParent must not handle child list of parent", parent.getChildNodes().isEmpty());
181 }
182
183 /**
184 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonNode#getChildNodes()}.
185 */
186 @Test
187 public void testGetChildNodes() {
188 TaxonNode root = taxonomicView1.addChildTaxon(taxon1, null, null, null);
189 assertEquals("Number of all nodes in view should be 1", 1, taxonomicView1.getAllNodes().size());
190
191 TaxonNode child = root.addChildTaxon(taxon2, ref2, "p33", syn1);
192
193 Set<TaxonNode> childList = root.getChildNodes();
194 assertFalse("parent child list must not be empty",childList.isEmpty());
195 assertEquals("size of child list be 1", 1, childList.size());
196 assertSame("child must be in child list", child, childList.iterator().next());
197 assertSame("taxa must be the same", taxon2, childList.iterator().next().getTaxon());
198 }
199
200 @Test
201 public void testGetCountChildren(){
202 TaxonNode root = taxonomicView1.addChildTaxon(taxon1, null, null, null);
203 assertEquals("Count of children must be 0", 0, root.getCountChildren());
204 TaxonNode child = root.addChildTaxon(taxon2, ref2, "p33", syn1);
205 assertEquals("Count of children must be 1", 1, root.getCountChildren());
206 Taxon taxon3 = Taxon.NewInstance(null, null);
207 TaxonNode child2 = root.addChildTaxon(taxon3, null, null, null);
208 assertEquals("Count of children must be 2", 2, root.getCountChildren());
209 root.removeChildNode(child);
210 assertEquals("Count of children must be 1", 1, root.getCountChildren());
211 root.removeChildNode(child2);
212 assertEquals("Count of children must be 0", 0, root.getCountChildren());
213
214 }
215
216 @Test
217 public void testDelete(){
218 TaxonNode root = taxonomicView1.addChildTaxon(taxon1, null, null, null);
219 assertEquals("Number of all nodes in view should be 1", 1, taxonomicView1.getAllNodes().size());
220
221
222 TaxonNode childNode = root.addChildTaxon(taxon2, null, null, null);
223 assertEquals("Count of children must be 1", 1, root.getCountChildren());
224
225 childNode.delete();
226 assertEquals("Count of children must be 0", 0, root.getCountChildren());
227
228
229 root.delete();
230 assertEquals("Number of all nodes in view should be 0", 0, taxonomicView1.getAllNodes().size());
231
232
233 }
234
235 @Test
236 public void testMoveTaxonNodeToOtherTree(){
237 TaxonNode node = taxonomicView1.addChildTaxon(taxon1, null, null, null);
238 assertEquals("The node should be in the classification we added it to", taxonomicView1, node.getTaxonomicTree());
239
240 TaxonNode movedNode = taxonomicView2.addChildNode(node, null, null, null);
241 assertEquals("The node should be in the classification we moved it to", taxonomicView2, movedNode.getTaxonomicTree());
242 assertEquals("The old tree should be empty now", 0, taxonomicView1.getChildNodes().size());
243 }
244
245 @Test
246 public void testMoveTaxonNodeToOtherTaxonNodeInDifferentTree(){
247 TaxonNode node1 = taxonomicView1.addChildTaxon(taxon1, null, null, null);
248 TaxonNode node2 = node1.addChildTaxon(taxon3, null, null, null);
249
250 assertEquals("The node should have exactly one child", 1, node1.getChildNodes().size());
251 assertEquals("The child is not in the correct tree", taxonomicView1, node2.getTaxonomicTree());
252 assertEquals("The Classification should contain exactly two nodes", 2, taxonomicView1.getAllNodes().size());
253
254 TaxonNode node3 = taxonomicView2.addChildTaxon(taxon3, null, null, null);
255
256 // move node2 to node3 in other tree
257 node3.addChildNode(node2, null, null, null);
258
259 assertEquals("Old node should not have child nodes", 0, node1.getChildNodes().size());
260 assertEquals("Old tree should contain only one node now", 1, taxonomicView1.getAllNodes().size());
261 assertEquals("Moved node not in expected tree", taxonomicView2, node2.getTaxonomicTree());
262 assertEquals("Count of nodes in new tree:", 2, taxonomicView2.getAllNodes().size());
263
264 }
265
266 /**
267 * Basically tests setTaxonomicTreeRecursively(TaxonomicTree) which is a private method
268 */
269 @Test
270 public void testMoveTaxonNodesRecursivelyToOtherTaxonNodeInDifferentTree(){
271 TaxonNode node1 = taxonomicView1.addChildTaxon(taxon1, null, null, null);
272 TaxonNode node2 = node1.addChildTaxon(taxon2, null, null, null);
273 TaxonNode node3 = node2.addChildTaxon(taxon3, null, null, null);
274
275 //move the branch to a different tree
276 taxonomicView2.addChildNode(node1, null, null, null);
277
278 assertEquals("Old tree should be empty:", 0, taxonomicView1.getAllNodes().size());
279 assertEquals("Moved node not in expected tree:", taxonomicView2, node1.getTaxonomicTree());
280 assertEquals("Recursively moved node not in expected tree:", taxonomicView2, node2.getTaxonomicTree());
281 assertEquals("Recursively moved node not in expected tree:", taxonomicView2, node3.getTaxonomicTree());
282
283 assertEquals("Count of nodes in new tree:", 3, taxonomicView2.getAllNodes().size());
284
285 }
286
287 }