View Javadoc

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