1
|
package eu.etaxonomy.cdm.model.description;
|
2
|
|
3
|
import static org.junit.Assert.assertEquals;
|
4
|
import static org.junit.Assert.assertNotNull;
|
5
|
import static org.junit.Assert.assertTrue;
|
6
|
|
7
|
import org.apache.log4j.Logger;
|
8
|
import org.junit.Assert;
|
9
|
import org.junit.Before;
|
10
|
import org.junit.BeforeClass;
|
11
|
import org.junit.Test;
|
12
|
|
13
|
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
|
14
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
15
|
|
16
|
public class PolytomousKeyNodeTest {
|
17
|
@SuppressWarnings("unused")
|
18
|
private static Logger logger = Logger.getLogger(PolytomousKeyNodeTest.class);
|
19
|
|
20
|
private PolytomousKey key1;
|
21
|
private Taxon taxon1;
|
22
|
|
23
|
@BeforeClass
|
24
|
public static void setUpBeforeClass() {
|
25
|
DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
|
26
|
vocabularyStore.initialize();
|
27
|
}
|
28
|
|
29
|
@Before
|
30
|
public void setUp() throws Exception {
|
31
|
key1 = PolytomousKey.NewInstance();
|
32
|
key1.setTitleCache("My Test Key", true);
|
33
|
PolytomousKeyNode root = key1.getRoot();
|
34
|
root.setQuestion(KeyStatement.NewInstance("Is this Aus bus?"));
|
35
|
|
36
|
// child1
|
37
|
taxon1 = Taxon.NewInstance(null, null);
|
38
|
taxon1.setTitleCache("Aus bus L.", true);
|
39
|
PolytomousKeyNode child1 = PolytomousKeyNode.NewInstance("Yes", null, taxon1, null);
|
40
|
Feature feature1 = Feature.NewInstance(null, "Leaf", null);
|
41
|
child1.setFeature(feature1);
|
42
|
root.addChild(child1);
|
43
|
|
44
|
// child2
|
45
|
Taxon taxon2 = Taxon.NewInstance(null, null);
|
46
|
taxon2.setTitleCache("Cus dus Mill.", true);
|
47
|
PolytomousKeyNode child2 = PolytomousKeyNode.NewInstance("No");
|
48
|
child2.setTaxon(taxon2);
|
49
|
root.addChild(child2);
|
50
|
|
51
|
// child3
|
52
|
Taxon taxon3 = Taxon.NewInstance(null, null);
|
53
|
taxon3.setTitleCache("Cus dus subs. rus L.", true);
|
54
|
PolytomousKeyNode child3 = PolytomousKeyNode.NewInstance("Long and wide");
|
55
|
child3.setTaxon(taxon3);
|
56
|
child1.addChild(child3);
|
57
|
|
58
|
// child4
|
59
|
Taxon taxon4 = Taxon.NewInstance(null, null);
|
60
|
taxon4.setTitleCache("Cus dus subs. zus L.", true);
|
61
|
PolytomousKeyNode child4 = PolytomousKeyNode.NewInstance("Small and narrow");
|
62
|
child4.setTaxon(taxon4);
|
63
|
child1.addChild(child4);
|
64
|
|
65
|
PolytomousKey key2 = PolytomousKey.NewTitledInstance("Second Key");
|
66
|
child3.setSubkey(key2);
|
67
|
|
68
|
child4.setOtherNode(key2.getRoot());
|
69
|
|
70
|
PolytomousKeyNode child5 = PolytomousKeyNode.NewInstance("Long and narrow");
|
71
|
child3.addChild(child5);
|
72
|
|
73
|
}
|
74
|
|
75
|
// ********************* Tests
|
76
|
// *******************************************************/
|
77
|
|
78
|
@Test
|
79
|
public void testNodeNumber() {
|
80
|
PolytomousKeyNode root = key1.getRoot();
|
81
|
Assert.assertEquals("Root should have node number = 1", Integer.valueOf(1), root.getNodeNumber());
|
82
|
PolytomousKeyNode child1 = root.getChildAt(0);
|
83
|
Assert.assertEquals("Child1 should have node number = 2", Integer.valueOf(2), child1.getNodeNumber());
|
84
|
PolytomousKeyNode child2 = root.getChildAt(1);
|
85
|
Assert.assertEquals("Child2 should have node number = null", null,child2.getNodeNumber());
|
86
|
PolytomousKeyNode child3 = child1.getChildAt(0);
|
87
|
Assert.assertEquals("Child3 should have node number = 3", Integer.valueOf(3),child3.getNodeNumber());
|
88
|
PolytomousKeyNode child4 = child1.getChildAt(1);
|
89
|
Assert.assertEquals("Child4 should have node number = null", null,child4.getNodeNumber());
|
90
|
|
91
|
}
|
92
|
|
93
|
@Test
|
94
|
public void testClone(){
|
95
|
PolytomousKeyNode rootNode = key1.getRoot();
|
96
|
PolytomousKeyNode clone = (PolytomousKeyNode)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
|
|
105
|
@Test
|
106
|
public void testRemoveChild() {
|
107
|
PolytomousKey key = PolytomousKey.NewInstance();
|
108
|
PolytomousKeyNode parent = key.getRoot();
|
109
|
PolytomousKeyNode child = PolytomousKeyNode.NewInstance();
|
110
|
parent.addChild(child);
|
111
|
|
112
|
Assert.assertEquals("Parent node should have one child", 1, parent.getChildren().size());
|
113
|
|
114
|
parent.removeChild(child);
|
115
|
|
116
|
Assert.assertEquals("Parent node should have no children", 0, parent.getChildren().size());
|
117
|
}
|
118
|
|
119
|
}
|