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