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 Node ch1 = m_tree.getForest().create();
|
|
26 Node ch2 = m_tree.getForest().create();
|
|
27
|
|
28 Node ch11 = m_tree.getForest().create();
|
|
29 Node ch12 = m_tree.getForest().create();
|
|
30
|
|
31 LinkedList<Node> list = new LinkedList<Node>();
|
|
32 list.add(ch1);
|
|
33 list.add(ch2);
|
|
34
|
|
35 m_tree.getRoot().getData().add(list);
|
|
36
|
|
37 LinkedList<Node> ch1list = new LinkedList<Node>();
|
|
38 ch1list.add(ch11);
|
|
39 ch1.getData().add(ch1list);
|
|
40
|
|
41 LinkedList<Node> ch2list = new LinkedList<Node>();
|
|
42 ch2list.add(ch12);
|
|
43 ch2.getData().add(ch2list);
|
|
44
|
|
45 LinkedList<Node> before = findPath(m_tree.getRoot(),ch11);
|
|
46 NodeData newData = ch11.getData().deepCopy();
|
|
47 m_tree.updateTree(ch11,newData);
|
|
48 LinkedList<Node> after = findPath(m_tree.getRoot(),ch11);
|
|
49
|
|
50 for(int i = 0;i < before.size();i ++){
|
|
51 boolean result = before.get(i).getID().isFamily(after.get(i).getID());
|
|
52 Assert.assertEquals(true,result);
|
|
53 }
|
|
54 }
|
|
55
|
|
56 public LinkedList<Node> findPath(Node _from,Node _target)
|
|
57 {
|
|
58 if(_from.getID().isFamily(_target.getID())){
|
|
59 LinkedList<Node> path = new LinkedList<Node>();
|
|
60 path.add(_from);
|
|
61 return path;
|
|
62 }
|
|
63
|
|
64 for(Node child : _from.getData().list()){
|
|
65 LinkedList<Node> path = findPath(child,_target);
|
|
66 if(path != null){
|
|
67 path.add(_from);
|
|
68 }
|
|
69 }
|
|
70
|
|
71 return null;
|
|
72 }
|
|
73 }
|