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   
8   import java.util.Iterator;
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.dfa.VariableAccess;
14  import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
15  import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator;
16  import net.sourceforge.pmd.testframework.ParserTst;
17  
18  import org.junit.Test;
19  
20  
21  public class GeneralFiddlingTest extends ParserTst {
22  
23      @Test
24      public void test1() throws Throwable {
25          ASTCompilationUnit acu = buildDFA(TEST1);
26          ASTMethodDeclarator meth = acu.findDescendantsOfType(ASTMethodDeclarator.class).get(0);
27          DataFlowNode n = meth.getDataFlowNode();
28          List<DataFlowNode> f = n.getFlow();
29  
30          assertEquals(6, f.size());
31          assertEquals("Undefinition(x)", String.valueOf(f.get(0).getVariableAccess().get(0)));
32          assertEquals(0, f.get(1).getVariableAccess().size());
33          assertEquals("Definition(x)", String.valueOf(f.get(2).getVariableAccess().get(0)));
34          assertEquals("Reference(x)", String.valueOf(f.get(3).getVariableAccess().get(0)));
35          assertEquals("Definition(x)", String.valueOf(f.get(4).getVariableAccess().get(0)));
36          assertEquals("Undefinition(x)", String.valueOf(f.get(5).getVariableAccess().get(0)));
37  
38  //        for (DataFlowNode dfan : f) {
39  //            System.out.println("Flow starting on line " + dfan.getLine());
40  //            List<VariableAccess> va = dfan.getVariableAccess();
41  //            for (VariableAccess o : va) {
42  //                System.out.println("  variable: " + o);
43  //            }
44  //        }
45      }
46  
47      private static final String TEST1 =
48              "class Foo {" + PMD.EOL +
49              " void bar() {" + PMD.EOL +
50              "  int x = 2;" + PMD.EOL +
51              "  foo(x);" + PMD.EOL +
52              "  x = 3;" + PMD.EOL +
53              " }" + PMD.EOL +
54              "}";
55  }