View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.dfa;
5   
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertFalse;
8   import static org.junit.Assert.assertTrue;
9   
10  import java.util.LinkedList;
11  
12  import net.sourceforge.pmd.lang.dfa.DataFlowNode;
13  import net.sourceforge.pmd.lang.dfa.NodeType;
14  import net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode;
15  
16  import org.junit.Test;
17  
18  public class DataFlowNodeTest {
19  
20      @Test
21      public void testAddPathToChild() {
22          DataFlowNode parent = new StartOrEndDataFlowNode(new LinkedList<DataFlowNode>(), 10, false);
23          DataFlowNode child = new StartOrEndDataFlowNode(new LinkedList<DataFlowNode>(), 12, false);
24          parent.addPathToChild(child);
25          assertEquals(parent.getChildren().size(), 1);
26          assertTrue(child.getParents().contains(parent));
27          assertTrue(parent.getChildren().contains(child));
28      }
29  
30      @Test
31      public void testRemovePathToChild() {
32          DataFlowNode parent = new StartOrEndDataFlowNode(new LinkedList<DataFlowNode>(), 10, false);
33          DataFlowNode child = new StartOrEndDataFlowNode(new LinkedList<DataFlowNode>(), 12, false);
34          parent.addPathToChild(child);
35  
36          assertTrue(parent.removePathToChild(child));
37          assertFalse(child.getParents().contains(parent));
38          assertFalse(parent.getChildren().contains(child));
39      }
40  
41      @Test
42      public void testRemovePathWithNonChild() {
43          DataFlowNode parent = new StartOrEndDataFlowNode(new LinkedList<DataFlowNode>(), 10, false);
44          DataFlowNode child = new StartOrEndDataFlowNode(new LinkedList<DataFlowNode>(), 12, false);
45          assertFalse(parent.removePathToChild(child));
46      }
47  
48      @Test
49      public void testReverseParentPathsTo() {
50          DataFlowNode parent1 = new StartOrEndDataFlowNode(new LinkedList<DataFlowNode>(), 10, false);
51          DataFlowNode parent2 = new StartOrEndDataFlowNode(new LinkedList<DataFlowNode>(), 12, false);
52          DataFlowNode child1 = new StartOrEndDataFlowNode(new LinkedList<DataFlowNode>(), 13, false);
53          DataFlowNode child2 = new StartOrEndDataFlowNode(new LinkedList<DataFlowNode>(), 13, false);
54          parent1.addPathToChild(child1);
55          parent2.addPathToChild(child1);
56          assertTrue(parent1.getChildren().contains(child1));
57  
58          child1.reverseParentPathsTo(child2);
59          assertTrue(parent1.getChildren().contains(child2));
60          assertFalse(parent1.getChildren().contains(child1));
61          assertTrue(parent2.getChildren().contains(child2));
62          assertFalse(parent2.getChildren().contains(child1));
63  
64          assertEquals(0, child1.getParents().size());
65          assertEquals(2, child2.getParents().size());
66      }
67  
68      @Test
69      public void testSetType() {
70          DataFlowNode node = new StartOrEndDataFlowNode(new LinkedList<DataFlowNode>(), 10, false);
71          node.setType(NodeType.BREAK_STATEMENT);
72          assertTrue(node.isType(NodeType.BREAK_STATEMENT));
73          assertFalse(node.isType(NodeType.CASE_LAST_STATEMENT));
74      }
75  
76      public static junit.framework.Test suite() {
77          return new junit.framework.JUnit4TestAdapter(DataFlowNodeTest.class);
78      }
79  }