1 package test.net.sourceforge.pmd.ast;
2
3 import net.sourceforge.pmd.PMD;
4 import net.sourceforge.pmd.ast.ASTBlock;
5 import net.sourceforge.pmd.ast.ASTBlockStatement;
6 import net.sourceforge.pmd.ast.ASTMethodDeclaration;
7 import net.sourceforge.pmd.ast.ASTName;
8 import net.sourceforge.pmd.ast.ASTReturnStatement;
9 import net.sourceforge.pmd.ast.ASTUnmodifiedClassDeclaration;
10 import net.sourceforge.pmd.ast.SimpleNode;
11
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Set;
16
17 public class SimpleNodeTest extends ParserTst {
18
19 private static final String METHOD_DIFF_LINES =
20 "public class Test {" + PMD.EOL +
21 " public void foo() {" + PMD.EOL +
22 " int x;" + PMD.EOL +
23 " }" + PMD.EOL +
24 "}";
25
26 public void testMethodDiffLines() throws Throwable {
27 Set methods = getNodes(ASTMethodDeclaration.class, METHOD_DIFF_LINES);
28 Iterator iter = methods.iterator();
29 verifyNode((SimpleNode) iter.next(), 2, 2, 4, 2);
30 }
31
32 private static final String METHOD_SAME_LINE =
33 "public class Test {" + PMD.EOL +
34 " public void foo() {}" + PMD.EOL +
35 "}";
36
37 public void testMethodSameLine() throws Throwable {
38 Set methods = getNodes(ASTMethodDeclaration.class, METHOD_SAME_LINE);
39 Iterator iter = methods.iterator();
40 verifyNode((SimpleNode) iter.next(), 2, 2, 2, 21);
41 }
42
43
44 public void testNoLookahead() throws Throwable {
45 String code = "public class Foo { }"; // 1, 8 -> 1, 20
46 Set uCD = getNodes(ASTUnmodifiedClassDeclaration.class, code);
47 Iterator iter = uCD.iterator();
48 verifyNode((SimpleNode) iter.next(), 1, 8, 1, 20);
49 }
50
51 private static final String QUALIFIED_NAME =
52 "import java.io.File;" + PMD.EOL +
53 "public class Foo{}";
54
55 public void testNames() throws Throwable {
56 Set name = getNodes(ASTName.class, QUALIFIED_NAME);
57 Iterator i = name.iterator();
58 while (i.hasNext()) {
59 SimpleNode node = (SimpleNode) i.next();
60 if (node.getImage().equals("java.io.File")) {
61 verifyNode(node, 1, 8, 1, 19);
62 }
63 }
64 }
65
66 public void testNames2() throws Throwable {
67 String code = "import java.io.\nFile; \n public class Foo{}";
68 Set name = getNodes(ASTName.class, code);
69 Iterator i = name.iterator();
70 assertTrue(i.hasNext());
71
72 while (i.hasNext()) {
73 SimpleNode node = (SimpleNode) i.next();
74 if (node.getImage().equals("java.io.File")) {
75 verifyNode(node, 1, 8, 2, 4);
76 }
77
78 if (node.getImage().equals("Foo")) {
79 verifyNode(node, 2, 15, 2, 18);
80 }
81
82 }
83 }
84
85 public void verifyNode(SimpleNode node, int beginLine, int beginCol, int endLine, int endCol) {
86 assertEquals("Wrong beginning line: ", beginLine, node.getBeginLine());
87 assertEquals("Wrong beginning column: ", beginCol, node.getBeginColumn());
88 assertEquals("Wrong ending line:", endLine, node.getEndLine());
89 assertEquals("Wrong ending column:", endCol, node.getEndColumn());
90 }
91
92 public void testFindChildrenOfType() {
93 ASTBlock block = new ASTBlock(2);
94 block.jjtAddChild(new ASTReturnStatement(1), 0);
95 assertEquals(1, block.findChildrenOfType(ASTReturnStatement.class).size());
96 }
97
98 public void testFindChildrenOfTypeMultiple() {
99 ASTBlock block = new ASTBlock(1);
100 block.jjtAddChild(new ASTBlockStatement(2), 0);
101 block.jjtAddChild(new ASTBlockStatement(3), 1);
102
103 List nodes = new ArrayList();
104 block.findChildrenOfType(ASTBlockStatement.class, nodes);
105 assertEquals(2, nodes.size());
106 }
107
108 public void testFindChildrenOfTypeRecurse() {
109 ASTBlock block = new ASTBlock(1);
110 ASTBlock childBlock = new ASTBlock(2);
111 block.jjtAddChild(childBlock, 0);
112 childBlock.jjtAddChild(new ASTMethodDeclaration(3), 0);
113
114 List nodes = new ArrayList();
115 block.findChildrenOfType(ASTMethodDeclaration.class, nodes);
116 assertEquals(1, nodes.size());
117 }
118 }
This page was automatically generated by Maven