View Javadoc

    1   package net.sourceforge.pmd.dfa;
    2
    3   import static org.junit.Assert.assertEquals;
    4   import static org.junit.Assert.assertTrue;
    5   import net.sourceforge.pmd.PMD;
    6   import net.sourceforge.pmd.lang.Language;
    7   import net.sourceforge.pmd.lang.dfa.DataFlowNode;
    8   import net.sourceforge.pmd.lang.dfa.NodeType;
    9   import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
    10  import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
    11  import net.sourceforge.pmd.lang.java.ast.ASTExpression;
    12  import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
    13  import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
    14  import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
    15  import net.sourceforge.pmd.lang.java.dfa.StatementAndBraceFinder;
    16  import net.sourceforge.pmd.testframework.ParserTst;
    17
    18  import org.junit.Test;
    19
    20
    21  public class StatementAndBraceFinderTest extends ParserTst {
    22
    23      @Test
    24      public void testStatementExpressionParentChildLinks() throws Throwable {
    25          ASTStatementExpression se = getOrderedNodes(ASTStatementExpression.class, TEST1).get(0);
    26          ASTMethodDeclaration seParent = (ASTMethodDeclaration) ((DataFlowNode) se.getDataFlowNode().getParents().get(0)).getNode();
    27          assertEquals(se, ((DataFlowNode) seParent.getDataFlowNode().getChildren().get(0)).getNode());
    28          assertEquals(seParent, ((DataFlowNode) se.getDataFlowNode().getParents().get(0)).getNode());
    29      }
    30
    31      @Test
    32      public void testVariableDeclaratorParentChildLinks() throws Throwable {
    33          ASTVariableDeclarator vd = getOrderedNodes(ASTVariableDeclarator.class, TEST2).get(0);
    34          ASTMethodDeclaration vdParent = (ASTMethodDeclaration) ((DataFlowNode) vd.getDataFlowNode().getParents().get(0)).getNode();
    35          assertEquals(vd, ((DataFlowNode) vdParent.getDataFlowNode().getChildren().get(0)).getNode());
    36          assertEquals(vdParent, ((DataFlowNode) vd.getDataFlowNode().getParents().get(0)).getNode());
    37      }
    38
    39      @Test
    40      public void testIfStmtHasCorrectTypes() throws Throwable {
    41          ASTExpression exp = getOrderedNodes(ASTExpression.class, TEST3).get(0);
    42          DataFlowNode dfn = exp.getDataFlowNode().getFlow().get(2);
    43          assertTrue(dfn.isType(NodeType.IF_EXPR));
    44          assertTrue(dfn.isType(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE));
    45      }
    46
    47      @Test
    48      public void testWhileStmtHasCorrectTypes() throws Throwable {
    49          ASTExpression exp = getOrderedNodes(ASTExpression.class, TEST4).get(0);
    50          DataFlowNode dfn = exp.getDataFlowNode().getFlow().get(2);
    51          assertTrue(dfn.isType(NodeType.WHILE_EXPR));
    52          assertTrue(dfn.isType(NodeType.WHILE_LAST_STATEMENT));
    53      }
    54
    55      @Test
    56      public void testForStmtHasCorrectTypes() throws Throwable {
    57          ASTExpression exp = getOrderedNodes(ASTExpression.class, TEST5).get(0);
    58          DataFlowNode dfn = exp.getDataFlowNode().getFlow().get(2);
    59          assertTrue(dfn.isType(NodeType.FOR_INIT));
    60          dfn = exp.getDataFlowNode().getFlow().get(3);
    61          assertTrue(dfn.isType(NodeType.FOR_EXPR));
    62          dfn = exp.getDataFlowNode().getFlow().get(4);
    63          assertTrue(dfn.isType(NodeType.FOR_UPDATE));
    64          assertTrue(dfn.isType(NodeType.FOR_BEFORE_FIRST_STATEMENT));
    65          assertTrue(dfn.isType(NodeType.FOR_END));
    66      }
    67
    68      @Test(expected = RuntimeException.class)
    69      public void testOnlyWorksForMethodsAndConstructors() {
    70          StatementAndBraceFinder sbf = new StatementAndBraceFinder(Language.JAVA.getDefaultVersion().getLanguageVersionHandler().getDataFlowHandler());
    71          sbf.buildDataFlowFor(new ASTMethodDeclaration(1));
    72          sbf.buildDataFlowFor(new ASTConstructorDeclaration(1));
    73          sbf.buildDataFlowFor(new ASTCompilationUnit(1));
    74      }
    75
    76      private static final String TEST1 =
    77              "class Foo {" + PMD.EOL +
    78              " void bar() {" + PMD.EOL +
    79              "  x = 2;" + PMD.EOL +
    80              " }" + PMD.EOL +
    81              "}";
    82
    83      private static final String TEST2 =
    84              "class Foo {" + PMD.EOL +
    85              " void bar() {" + PMD.EOL +
    86              "  int x;" + PMD.EOL +
    87              " }" + PMD.EOL +
    88              "}";
    89
    90      private static final String TEST3 =
    91              "class Foo {" + PMD.EOL +
    92              " void bar() {" + PMD.EOL +
    93              "  if (x) {}" + PMD.EOL +
    94              " }" + PMD.EOL +
    95              "}";
    96
    97      private static final String TEST4 =
    98              "class Foo {" + PMD.EOL +
    99              " void bar() {" + PMD.EOL +
    100             "  while (x) {}" + PMD.EOL +
    101             " }" + PMD.EOL +
    102             "}";
    103
    104     private static final String TEST5 =
    105             "class Foo {" + PMD.EOL +
    106             " void bar() {" + PMD.EOL +
    107             "  for (int i=0; i<10; i++) {}" + PMD.EOL +
    108             " }" + PMD.EOL +
    109             "}";
    110
    111     public static junit.framework.Test suite() {
    112         return new junit.framework.JUnit4TestAdapter(StatementAndBraceFinderTest.class);
    113     }
    114 }