1   package test.net.sourceforge.pmd.dfa;
2   
3   import junit.framework.TestCase;
4   import net.sourceforge.pmd.dfa.DataFlowNode;
5   import net.sourceforge.pmd.dfa.IDataFlowNode;
6   import net.sourceforge.pmd.dfa.NodeType;
7   import net.sourceforge.pmd.dfa.StartOrEndDataFlowNode;
8   
9   import java.util.LinkedList;
10  
11  public class DataFlowNodeTest extends TestCase {
12  
13      public void testAddPathToChild() {
14          DataFlowNode parent = new StartOrEndDataFlowNode(new LinkedList(), 10, false);
15          IDataFlowNode child = new StartOrEndDataFlowNode(new LinkedList(), 12, false);
16          parent.addPathToChild(child);
17          assertEquals(parent.getChildren().size(), 1);
18          assertTrue(child.getParents().contains(parent));
19          assertTrue(parent.getChildren().contains(child));
20      }
21  
22      public void testRemovePathToChild() {
23          DataFlowNode parent = new StartOrEndDataFlowNode(new LinkedList(), 10, false);
24          IDataFlowNode child = new StartOrEndDataFlowNode(new LinkedList(), 12, false);
25          parent.addPathToChild(child);
26  
27          assertTrue(parent.removePathToChild(child));
28          assertFalse(child.getParents().contains(parent));
29          assertFalse(parent.getChildren().contains(child));
30      }
31  
32      public void testRemovePathWithNonChild() {
33          DataFlowNode parent = new StartOrEndDataFlowNode(new LinkedList(), 10, false);
34          IDataFlowNode child = new StartOrEndDataFlowNode(new LinkedList(), 12, false);
35          assertFalse(parent.removePathToChild(child));
36      }
37  
38      public void testReverseParentPathsTo() {
39          DataFlowNode parent1 = new StartOrEndDataFlowNode(new LinkedList(), 10, false);
40          DataFlowNode parent2 = new StartOrEndDataFlowNode(new LinkedList(), 12, false);
41          IDataFlowNode child1 = new StartOrEndDataFlowNode(new LinkedList(), 13, false);
42          IDataFlowNode child2 = new StartOrEndDataFlowNode(new LinkedList(), 13, false);
43          parent1.addPathToChild(child1);
44          parent2.addPathToChild(child1);
45          assertTrue(parent1.getChildren().contains(child1));
46  
47          child1.reverseParentPathsTo(child2);
48          assertTrue(parent1.getChildren().contains(child2));
49          assertFalse(parent1.getChildren().contains(child1));
50          assertTrue(parent2.getChildren().contains(child2));
51          assertFalse(parent2.getChildren().contains(child1));
52  
53          assertEquals(0, child1.getParents().size());
54          assertEquals(2, child2.getParents().size());
55      }
56  
57      public void testSetType() {
58          DataFlowNode node = new StartOrEndDataFlowNode(new LinkedList(), 10, false);
59          node.setType(NodeType.BREAK_STATEMENT);
60          assertTrue(node.isType(NodeType.BREAK_STATEMENT));
61          assertFalse(node.isType(NodeType.CASE_LAST_STATEMENT));
62      }
63  }