View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.dfa;
5   
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertNotNull;
8   
9   import java.util.List;
10  
11  import net.sourceforge.pmd.PMD;
12  import net.sourceforge.pmd.lang.dfa.DataFlowNode;
13  import net.sourceforge.pmd.lang.java.ParserTst;
14  import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
15  import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator;
16  
17  import org.junit.Test;
18  
19  
20  public class GeneralFiddlingTest extends ParserTst {
21  
22      /**
23       * Unit test for https://sourceforge.net/p/pmd/bugs/1325/
24       * @throws Throwable any error
25       */
26      @Test
27      public void innerClassShouldWork() throws Throwable {
28          ASTCompilationUnit acu = buildDFA(
29                    "class Foo {"
30                  + "    void bar() {"
31                  + "        class X {}"
32                  + "        int i;"
33                  + "    }"
34                  + "}");
35          assertNotNull(acu);
36      }
37  
38      @Test
39      public void test1() throws Throwable {
40          ASTCompilationUnit acu = buildDFA(TEST1);
41          ASTMethodDeclarator meth = acu.findDescendantsOfType(ASTMethodDeclarator.class).get(0);
42          DataFlowNode n = meth.getDataFlowNode();
43          List<DataFlowNode> f = n.getFlow();
44  
45          assertEquals(6, f.size());
46          assertEquals("Undefinition(x)", String.valueOf(f.get(0).getVariableAccess().get(0)));
47          assertEquals(0, f.get(1).getVariableAccess().size());
48          assertEquals("Definition(x)", String.valueOf(f.get(2).getVariableAccess().get(0)));
49          assertEquals("Reference(x)", String.valueOf(f.get(3).getVariableAccess().get(0)));
50          assertEquals("Definition(x)", String.valueOf(f.get(4).getVariableAccess().get(0)));
51          assertEquals("Undefinition(x)", String.valueOf(f.get(5).getVariableAccess().get(0)));
52  
53  //        for (DataFlowNode dfan : f) {
54  //            System.out.println("Flow starting on line " + dfan.getLine());
55  //            List<VariableAccess> va = dfan.getVariableAccess();
56  //            for (VariableAccess o : va) {
57  //                System.out.println("  variable: " + o);
58  //            }
59  //        }
60      }
61  
62      private static final String TEST1 =
63              "class Foo {" + PMD.EOL +
64              " void bar() {" + PMD.EOL +
65              "  int x = 2;" + PMD.EOL +
66              "  foo(x);" + PMD.EOL +
67              "  x = 3;" + PMD.EOL +
68              " }" + PMD.EOL +
69              "}";
70  }