5
|
1 package treecms.test;
|
|
2
|
|
3 import java.util.LinkedList;
|
|
4
|
|
5 import junit.framework.Assert;
|
|
6
|
|
7 import org.junit.Test;
|
|
8
|
|
9 import treecms.api.Node;
|
|
10 import treecms.api.NodeData;
|
|
11 import treecms.api.Tree;
|
|
12
|
|
13 public class TreeTest
|
|
14 {
|
|
15 Tree m_tree;
|
|
16
|
|
17 public TreeTest(Tree _tree)
|
|
18 {
|
|
19 m_tree = _tree;
|
|
20 }
|
|
21
|
|
22 @Test
|
|
23 public void testUpdateTree()
|
|
24 {
|
|
25 /**
|
|
26 * Root
|
|
27 * + 1
|
|
28 * + + 1-1 <- update this.
|
|
29 * +
|
|
30 * + 2
|
|
31 * + 2-1
|
|
32 */
|
|
33
|
|
34 Node ch1 = m_tree.getForest().create();
|
|
35 Node ch2 = m_tree.getForest().create();
|
|
36
|
|
37 Node ch11 = m_tree.getForest().create();
|
|
38 Node ch12 = m_tree.getForest().create();
|
|
39
|
|
40 LinkedList<Node> list = new LinkedList<Node>();
|
|
41 list.add(ch1);
|
|
42 list.add(ch2);
|
|
43
|
|
44 m_tree.getRoot().getData().add(list);
|
|
45
|
|
46 LinkedList<Node> ch1list = new LinkedList<Node>();
|
|
47 ch1list.add(ch11);
|
|
48 ch1.getData().add(ch1list);
|
|
49
|
|
50 LinkedList<Node> ch2list = new LinkedList<Node>();
|
|
51 ch2list.add(ch12);
|
|
52 ch2.getData().add(ch2list);
|
|
53
|
|
54 LinkedList<Node> before = findPath(m_tree.getRoot(),ch11);
|
|
55 NodeData newData = ch11.getData().deepCopy();
|
|
56 m_tree.updateTree(ch11,newData);
|
|
57 LinkedList<Node> after = findPath(m_tree.getRoot(),ch11);
|
|
58
|
|
59 for(int i = 0;i < before.size();i ++){
|
|
60 boolean result = before.get(i).getID().isFamily(after.get(i).getID());
|
|
61 Assert.assertEquals(true,result);
|
|
62 }
|
|
63 }
|
|
64
|
|
65 public LinkedList<Node> findPath(Node _from,Node _target)
|
|
66 {
|
|
67 if(_from.getID().isFamily(_target.getID())){
|
|
68 LinkedList<Node> path = new LinkedList<Node>();
|
|
69 path.add(_from);
|
|
70 return path;
|
|
71 }
|
|
72
|
|
73 for(Node child : _from.getData().list()){
|
|
74 LinkedList<Node> path = findPath(child,_target);
|
|
75 if(path != null){
|
|
76 path.add(_from);
|
|
77 }
|
|
78 }
|
|
79
|
|
80 return null;
|
|
81 }
|
|
82 }
|