Project

General

Profile

Download (4.11 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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
package eu.etaxonomy.cdm.model.description;
10

    
11
import static org.junit.Assert.assertEquals;
12
import static org.junit.Assert.assertNotNull;
13
import static org.junit.Assert.assertTrue;
14

    
15
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
16
import org.junit.Assert;
17
import org.junit.Before;
18
import org.junit.Test;
19

    
20
import eu.etaxonomy.cdm.model.taxon.Taxon;
21
import eu.etaxonomy.cdm.test.unit.EntityTestBase;
22

    
23
public class PolytomousKeyNodeTest extends EntityTestBase {
24

    
25
	@SuppressWarnings("unused")
26
	private static Logger logger = LogManager.getLogger(PolytomousKeyNodeTest.class);
27

    
28
	private PolytomousKey key1;
29
	private Taxon taxon1;
30

    
31
	@Before
32
	public void setUp() throws Exception {
33
		key1 = PolytomousKey.NewInstance();
34
		key1.setTitleCache("My Test Key", true);
35
		PolytomousKeyNode root = key1.getRoot();
36
		root.setQuestion(KeyStatement.NewInstance("Is this Aus bus?"));
37

    
38
		// child1
39
		taxon1 = Taxon.NewInstance(null, null);
40
		taxon1.setTitleCache("Aus bus L.", true);
41
		PolytomousKeyNode child1 = PolytomousKeyNode.NewInstance("Yes", null, taxon1, null);
42
		Feature feature1 = Feature.NewInstance(null, "Leaf", null);
43
		child1.setFeature(feature1);
44
		root.addChild(child1);
45

    
46
		// child2
47
		Taxon taxon2 = Taxon.NewInstance(null, null);
48
		taxon2.setTitleCache("Cus dus Mill.", true);
49
		PolytomousKeyNode child2 = PolytomousKeyNode.NewInstance("No");
50
		child2.setTaxon(taxon2);
51
		root.addChild(child2);
52

    
53
		// child3
54
		Taxon taxon3 = Taxon.NewInstance(null, null);
55
		taxon3.setTitleCache("Cus dus subs. rus L.", true);
56
		PolytomousKeyNode child3 = PolytomousKeyNode.NewInstance("Long and wide");
57
		child3.setTaxon(taxon3);
58
		child1.addChild(child3);
59

    
60
		// child4
61
		Taxon taxon4 = Taxon.NewInstance(null, null);
62
		taxon4.setTitleCache("Cus dus subs. zus L.", true);
63
		PolytomousKeyNode child4 = PolytomousKeyNode.NewInstance("Small and narrow");
64
		child4.setTaxon(taxon4);
65
		child1.addChild(child4);
66

    
67
		PolytomousKey key2 = PolytomousKey.NewTitledInstance("Second Key");
68
		child3.setSubkey(key2);
69

    
70
		child4.setOtherNode(key2.getRoot());
71

    
72
		PolytomousKeyNode child5 = PolytomousKeyNode.NewInstance("Long and narrow");
73
		child3.addChild(child5);
74

    
75
	}
76

    
77
	// ********************* Tests ***************************************/
78

    
79
	@Test
80
	public void testNodeNumber() {
81
		PolytomousKeyNode root = key1.getRoot();
82
		Assert.assertEquals("Root should have node number = 1", Integer.valueOf(1), root.getNodeNumber());
83
		PolytomousKeyNode child1 = root.getChildAt(0);
84
		Assert.assertEquals("Child1 should have node number = 2", Integer.valueOf(2), child1.getNodeNumber());
85
		PolytomousKeyNode child2 = root.getChildAt(1);
86
		Assert.assertEquals("Child2 should have node number = null", null,child2.getNodeNumber());
87
		PolytomousKeyNode child3 = child1.getChildAt(0);
88
		Assert.assertEquals("Child3 should have node number = 3", Integer.valueOf(3),child3.getNodeNumber());
89
		PolytomousKeyNode child4 = child1.getChildAt(1);
90
		Assert.assertEquals("Child4 should have node number  = null", null,child4.getNodeNumber());
91
	}
92

    
93
	@Test
94
	public void testClone(){
95
		PolytomousKeyNode rootNode = key1.getRoot();
96
		PolytomousKeyNode clone = rootNode.clone();
97
		assertNotNull(clone);
98
		assertEquals(clone.getFeature(), rootNode.getFeature());
99
		assertEquals(clone.getKey(), rootNode.getKey());
100
		assertTrue(clone.getChildren().size() == 0);
101
		assertTrue(rootNode.getChildren().size()>0);
102
	}
103

    
104
	@Test
105
	public void testRemoveChild() {
106
		PolytomousKey key = PolytomousKey.NewInstance();
107
		PolytomousKeyNode parent = key.getRoot();
108
		PolytomousKeyNode child = PolytomousKeyNode.NewInstance();
109
		parent.addChild(child);
110

    
111
		Assert.assertEquals("Parent node should have one child", 1, parent.getChildren().size());
112

    
113
		parent.removeChild(child);
114

    
115
		Assert.assertEquals("Parent node should have no children", 0, parent.getChildren().size());
116
	}
117
}
(4-4/9)