Project

General

Profile

Download (12.7 KB) Statistics
| Branch: | Tag: | Revision:
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
import org.springframework.beans.BeanUtils;
29

    
30
import eu.etaxonomy.cdm.model.name.BotanicalName;
31
import eu.etaxonomy.cdm.model.name.Rank;
32
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
33
//import eu.etaxonomy.cdm.model.reference.Book;
34
//import eu.etaxonomy.cdm.model.reference.Journal;
35
import eu.etaxonomy.cdm.model.reference.Reference;
36
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
37

    
38
/**
39
 * @author a.mueller
40
 * @created 01.04.2009
41
 * @version 1.0
42
 */
43
public class TaxonNodeTest {
44
	@SuppressWarnings("unused")
45
	private static final Logger logger = Logger.getLogger(TaxonNodeTest.class);
46
	private static String viewName1;
47
	private static Classification classification1;
48
	private static Classification classification2;
49
	private static Taxon taxon1;
50
	private static Taxon taxon2;
51
	private static Taxon taxon3;
52
	private static TaxonNameBase<?,?> taxonName1;
53
	private static TaxonNameBase<?,?> taxonName2;
54
	private static TaxonNameBase<?,?> taxonName3;
55
	private static Reference<?> ref1;
56
	private static Reference<?> ref2;
57
	private static Reference<?> ref3;
58
	private static Synonym syn1;
59
	/**
60
	 * @throws java.lang.Exception
61
	 */
62
	@BeforeClass
63
	public static void setUpBeforeClass() throws Exception {
64
	}
65

    
66
	/**
67
	 * @throws java.lang.Exception
68
	 */
69
	@AfterClass
70
	public static void tearDownAfterClass() throws Exception {
71
	}
72

    
73
	/**
74
	 * @throws java.lang.Exception
75
	 */
76
	@Before
77
	public void setUp() throws Exception {
78
		viewName1 = "Greuther, 1993";
79
		classification1 = Classification.NewInstance(viewName1);
80
		classification2 = Classification.NewInstance("Test View 2");
81
		taxonName1 = BotanicalName.NewInstance(Rank.SPECIES());
82
		taxonName1 = BotanicalName.NewInstance(Rank.SUBSPECIES());
83
		taxonName3 = BotanicalName.NewInstance(Rank.SPECIES());
84
		ref1 = ReferenceFactory.newJournal();
85
		ref2 = ReferenceFactory.newBook();
86
		ref3 = ReferenceFactory.newGeneric();
87
		taxon1 = Taxon.NewInstance(taxonName1, ref1);
88
		taxon2 = Taxon.NewInstance(taxonName2, ref1);
89
		taxon3 = Taxon.NewInstance(taxonName3, ref3);
90
		//taxonNode1 = new TaxonNode(taxon1, taxonomicView1);
91
		syn1 = Synonym.NewInstance(null, null);
92
	}
93

    
94
	/**
95
	 * @throws java.lang.Exception
96
	 */
97
	@After
98
	public void tearDown() throws Exception {
99
	}
100

    
101
//****************************** TESTS *****************************************/
102

    
103

    
104
	/**
105
	 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonNode#NewInstance(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.Classification)}.
106
	 */
107
	@Test
108
	public void testNewTaxonTaxonomicView() {
109
		TaxonNode testNode = new TaxonNode(taxon1);
110
		classification1.addChildNode(testNode, null, null);
111

    
112
		assertNotNull("test node should not be null", testNode);
113
		assertEquals(taxon1,testNode.getTaxon());
114
		assertEquals(classification1,testNode.getClassification());
115
		assertTrue("taxon1 must become part of taxonomicView1", classification1.isTaxonInTree(taxon1));
116
	}
117

    
118
	/**
119
	 * 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)}.
120
	 */
121
	@Test
122
	public void testAddChild() {
123
		TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
124
		assertEquals("Number of all nodes in view should be 1", 1, classification1.getAllNodes().size());
125

    
126
		TaxonNode child = root.addChildTaxon(taxon2, ref2, "p33");
127
		child.setSynonymToBeUsed(syn1); //originally synToBeUsed was part of addChildTaxon
128
		//test child properties
129
		assertNotNull("Child should not be null", child);
130
		assertEquals("Child taxon should be taxon2", taxon2, child.getTaxon());
131
		assertEquals("Parent taxon should be taxon1", taxon1, (child.getParent()).getTaxon());
132
		assertEquals("Reference should be ref2", ref2, child.getReference());
133
		assertEquals("Microreference should be 'p33'", "p33", child.getMicroReference());
134
		assertEquals("Synonym should be syn1", syn1, child.getSynonymToBeUsed());
135

    
136
		//test parent properties
137
		List<TaxonNode> childList = root.getChildNodes();
138
		assertFalse("parent child list must not be empty",childList.isEmpty());
139
		assertEquals("size of child list be 1", 1, childList.size());
140
		assertSame("taxa must be the same", taxon2, childList.iterator().next().getTaxon());
141

    
142
		//test view properties
143
		List<TaxonNode> rootNodes = classification1.getChildNodes();
144
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
145
		Set<TaxonNode> allNodes = classification1.getAllNodes();
146
		assertEquals("Number of all nodes should be 2", 2, allNodes.size());
147
		assertTrue("Taxonomic view should include child", allNodes.contains(child));
148

    
149

    
150
		//is part of taxon
151
		Set<TaxonNode> nodes2 = taxon2.getTaxonNodes();
152
		assertFalse("taxon2 must not be empty", nodes2.isEmpty());
153
		assertEquals("size of nodes of taxon2 must be 1", 1, nodes2.size());
154
		assertSame("taxa must be the same", taxon2, nodes2.iterator().next().getTaxon());
155
	}
156

    
157
	/**
158
	 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonNode#setTaxon(eu.etaxonomy.cdm.model.taxon.Taxon)}.
159
	 */
160
	@Test
161
	public void testSetTaxon() {
162
		TaxonNode node = new TaxonNode(taxon1);
163
		classification1.addChildNode(node, null, null);
164
		assertNotNull(taxon2);
165
		node.setTaxon(taxon2);
166
		assertSame("taxon must be the same", taxon2, node.getTaxon());
167
		assertTrue("taxon2 must contain node", taxon2.getTaxonNodes().contains(node));
168
	}
169

    
170
	/**
171
	 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonNode#setParent(eu.etaxonomy.cdm.model.taxon.TaxonNode)}.
172
	 */
173
	@Test
174
	public void testSetParent() {
175
		TaxonNode node = new TaxonNode(taxon1);
176
		assertNotNull(taxon2);
177
		TaxonNode parent = new TaxonNode(taxon2);
178
		assertSame("Taxon must be the same", taxon2, parent.getTaxon());
179
		classification1.addChildNode(parent, null, null);
180
		node.setParent(parent);
181
		assertSame("taxon2 must contain node", parent, node.getParent());
182
		assertTrue("setParent must not handle child list of parent", parent.getChildNodes().isEmpty());
183
	}
184

    
185
	/**
186
	 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonNode#getChildNodes()}.
187
	 */
188
	@Test
189
	public void testGetChildNodes() {
190
		TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
191
		assertEquals("Number of all nodes in view should be 1", 1, classification1.getAllNodes().size());
192

    
193
		TaxonNode child = root.addChildTaxon(taxon2, ref2, "p33");
194
		child.setSynonymToBeUsed(syn1);
195

    
196
		List<TaxonNode> childList = root.getChildNodes();
197
		assertFalse("parent child list must not be empty",childList.isEmpty());
198
		assertEquals("size of child list be 1", 1, childList.size());
199
		assertSame("child must be in child list", child, childList.iterator().next());
200
		assertSame("taxa must be the same", taxon2, childList.iterator().next().getTaxon());
201
	}
202

    
203
	@Test
204
	public void testGetCountChildren(){
205
		TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
206
		assertEquals("Count of children must be 0", 0, root.getCountChildren());
207
		TaxonNode child = root.addChildTaxon(taxon2, ref2, "p33");
208
		child.setSynonymToBeUsed(syn1);
209
		assertEquals("Count of children must be 1", 1, root.getCountChildren());
210
		Taxon taxon3 = Taxon.NewInstance(null, null);
211
		TaxonNode child2 = root.addChildTaxon(taxon3, null, null);
212
		assertEquals("Count of children must be 2", 2, root.getCountChildren());
213
		root.removeChildNode(child);
214
		assertEquals("Count of children must be 1", 1, root.getCountChildren());
215
		root.removeChildNode(child2);
216
		assertEquals("Count of children must be 0", 0, root.getCountChildren());
217

    
218
	}
219

    
220
	@Test
221
	public void testDelete(){
222
		TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
223
		assertEquals("Number of all nodes in view should be 1", 1, classification1.getAllNodes().size());
224

    
225

    
226
		TaxonNode childNode = root.addChildTaxon(taxon2, null, null);
227
		assertEquals("Count of children must be 1", 1, root.getCountChildren());
228

    
229
		childNode.delete();
230
		assertEquals("Count of children must be 0", 0, root.getCountChildren());
231

    
232

    
233
		root.delete();
234
		assertEquals("Number of all nodes in view should be 0", 0, classification1.getAllNodes().size());
235

    
236

    
237
	}
238

    
239
	@Test
240
	public void testMoveTaxonNodeToOtherTree(){
241
		TaxonNode node = classification1.addChildTaxon(taxon1, null, null);
242
		assertEquals("The node should be in the classification we added it to", classification1, node.getClassification());
243

    
244
		TaxonNode movedNode = classification2.addChildNode(node, null, null);
245
		assertEquals("The node should be in the classification we moved it to", classification2, movedNode.getClassification());
246
		assertEquals("The old tree should be empty now", 0, classification1.getChildNodes().size());
247
	}
248

    
249
	@Test
250
	public void testMoveTaxonNodeToOtherTaxonNodeInDifferentTree(){
251
		TaxonNode node1 = classification1.addChildTaxon(taxon1, null, null);
252
		TaxonNode node2 = node1.addChildTaxon(taxon3, null, null);
253

    
254
		assertEquals("The node should have exactly one child", 1, node1.getChildNodes().size());
255
		assertEquals("The child is not in the correct tree", classification1, node2.getClassification());
256
		assertEquals("The Classification should contain exactly two nodes", 2, classification1.getAllNodes().size());
257

    
258
		TaxonNode node3 = classification2.addChildTaxon(taxon3, null, null);
259

    
260
		// move node2 to node3 in other tree
261
		node3.addChildNode(node2, null, null);
262

    
263
		assertEquals("Old node should not have child nodes", 0, node1.getChildNodes().size());
264
		assertEquals("Old tree should contain only one node now", 1, classification1.getAllNodes().size());
265
		assertEquals("Moved node not in expected tree", classification2, node2.getClassification());
266
		assertEquals("Count of nodes in new tree:", 2, classification2.getAllNodes().size());
267

    
268
	}
269

    
270
	/**
271
	 * Basically tests #setClassificationRecursively(Classification) which is a private method
272
	 */
273
	@Test
274
	public void testMoveTaxonNodesRecursivelyToOtherTaxonNodeInDifferentTree(){
275
		TaxonNode node1 = classification1.addChildTaxon(taxon1, null, null);
276
		TaxonNode node2 = node1.addChildTaxon(taxon2, null, null);
277
		TaxonNode node3 = node2.addChildTaxon(taxon3, null, null);
278

    
279
		//move the branch to a different tree
280
		classification2.addChildNode(node1, null, null);
281

    
282
		assertEquals("Old tree should be empty:", 0, classification1.getAllNodes().size());
283
		assertEquals("Moved node not in expected tree:", classification2, node1.getClassification());
284
		assertEquals("Recursively moved node not in expected tree:", classification2, node2.getClassification());
285
		assertEquals("Recursively moved node not in expected tree:", classification2, node3.getClassification());
286

    
287
		assertEquals("Count of nodes in new tree:", 3, classification2.getAllNodes().size());
288

    
289
	}
290

    
291
	@Test
292
	public void testAddChildNode(){
293
		TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
294
		assertEquals("Number of all nodes in cla should be 1", 1, classification1.getAllNodes().size());
295

    
296
		TaxonNode child = root.addChildTaxon(taxon2, ref2, "p33");
297
		child.setSynonymToBeUsed(syn1); //originally synToBeUsed was part of addChildTaxon
298
		//test child properties
299
		assertNotNull("Child should not be null", child);
300
		assertEquals("Child taxon should be taxon2", taxon2, child.getTaxon());
301
		assertEquals("Parent taxon should be taxon1", taxon1, (child.getParent()).getTaxon());
302
		assertEquals("Reference should be ref2", ref2, child.getReference());
303
		assertEquals("Microreference should be 'p33'", "p33", child.getMicroReference());
304
		assertEquals("Synonym should be syn1", syn1, child.getSynonymToBeUsed());
305

    
306
		//test parent properties
307
		List<TaxonNode> childList = root.getChildNodes();
308
		assertFalse("parent child list must not be empty",childList.isEmpty());
309
		assertEquals("size of child list be 1", 1, childList.size());
310
		assertSame("taxa must be the same", taxon2, childList.iterator().next().getTaxon());
311

    
312
		//test view properties
313
		List<TaxonNode> rootNodes = classification1.getChildNodes();
314
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
315
		Set<TaxonNode> allNodes = classification1.getAllNodes();
316
		assertEquals("Number of all nodes should be 2", 2, allNodes.size());
317
		assertTrue("Taxonomic view should include child", allNodes.contains(child));
318
	}
319

    
320
    @Test
321
    public void beanTests(){
322
//      #5307 Test that BeanUtils does not fail
323
        BeanUtils.getPropertyDescriptors(TaxonNode.class);
324
    }
325

    
326
}
(5-5/6)