1 /***
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package test.net.sourceforge.pmd.ast;
5
6 import net.sourceforge.pmd.PMD;
7 import net.sourceforge.pmd.ast.ASTBlock;
8 import net.sourceforge.pmd.ast.ASTBlockStatement;
9 import net.sourceforge.pmd.ast.ASTExpression;
10 import net.sourceforge.pmd.ast.ASTMethodDeclaration;
11 import net.sourceforge.pmd.ast.ASTName;
12 import net.sourceforge.pmd.ast.ASTReturnStatement;
13 import net.sourceforge.pmd.ast.ASTUnmodifiedClassDeclaration;
14 import net.sourceforge.pmd.ast.ASTVariableInitializer;
15 import net.sourceforge.pmd.ast.SimpleNode;
16
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Set;
21
22 public class SimpleNodeTest extends ParserTst {
23
24 public void testMethodDiffLines() throws Throwable {
25 Set methods = getNodes(ASTMethodDeclaration.class, METHOD_DIFF_LINES);
26 Iterator iter = methods.iterator();
27 verifyNode((SimpleNode) iter.next(), 2, 2, 4, 2);
28 }
29
30 public void testMethodSameLine() throws Throwable {
31 Set methods = getNodes(ASTMethodDeclaration.class, METHOD_SAME_LINE);
32 verifyNode((SimpleNode) methods.iterator().next(), 2, 2, 2, 21);
33 }
34
35 public void testNoLookahead() throws Throwable {
36 String code = NO_LOOKAHEAD; // 1, 8 -> 1, 20
37 Set uCD = getNodes(ASTUnmodifiedClassDeclaration.class, code);
38 verifyNode((SimpleNode) uCD.iterator().next(), 1, 8, 1, 20);
39 }
40
41 public void testHasExplicitExtends() throws Throwable {
42 String code = HAS_EXPLICIT_EXTENDS;
43 ASTUnmodifiedClassDeclaration ucd = (ASTUnmodifiedClassDeclaration)(getNodes(ASTUnmodifiedClassDeclaration.class, code).iterator().next());
44 assertTrue(ucd.hasExplicitExtends());
45 }
46
47 public void testNoExplicitExtends() throws Throwable {
48 String code = NO_EXPLICIT_EXTENDS;
49 ASTUnmodifiedClassDeclaration ucd = (ASTUnmodifiedClassDeclaration)(getNodes(ASTUnmodifiedClassDeclaration.class, code).iterator().next());
50 assertTrue(!ucd.hasExplicitExtends());
51 }
52
53 public void testHasExplicitImplements() throws Throwable {
54 String code = HAS_EXPLICIT_IMPLEMENTS;
55 ASTUnmodifiedClassDeclaration ucd = (ASTUnmodifiedClassDeclaration)(getNodes(ASTUnmodifiedClassDeclaration.class, code).iterator().next());
56 assertTrue(ucd.hasExplicitImplements());
57 }
58
59 public void testNoExplicitImplements() throws Throwable {
60 String code = NO_EXPLICIT_IMPLEMENTS;
61 ASTUnmodifiedClassDeclaration ucd = (ASTUnmodifiedClassDeclaration)(getNodes(ASTUnmodifiedClassDeclaration.class, code).iterator().next());
62 assertTrue(!ucd.hasExplicitImplements());
63 }
64
65 public void testColumnsOnQualifiedName() throws Throwable {
66 Set name = getNodes(ASTName.class, QUALIFIED_NAME);
67 Iterator i = name.iterator();
68 while (i.hasNext()) {
69 SimpleNode node = (SimpleNode) i.next();
70 if (node.getImage().equals("java.io.File")) {
71 verifyNode(node, 1, 8, 1, 19);
72 }
73 }
74 }
75
76 public void testLineNumbersForNameSplitOverTwoLines() throws Throwable {
77 Set name = getNodes(ASTName.class, BROKEN_LINE_IN_NAME);
78 Iterator i = name.iterator();
79 while (i.hasNext()) {
80 SimpleNode node = (SimpleNode) i.next();
81 if (node.getImage().equals("java.io.File")) {
82 verifyNode(node, 1, 8, 2, 4);
83 }
84 if (node.getImage().equals("Foo")) {
85 verifyNode(node, 2, 15, 2, 18);
86 }
87 }
88 }
89
90 public void testLineNumbersAreSetOnAllSiblings() throws Throwable {
91 Set blocks = getNodes(ASTBlock.class, LINE_NUMBERS_ON_SIBLINGS);
92 Iterator i = blocks.iterator();
93 while (i.hasNext()) {
94 ASTBlock b = (ASTBlock)i.next();
95 assertTrue(b.getBeginLine() > 0);
96 }
97 blocks = getNodes(ASTVariableInitializer.class, LINE_NUMBERS_ON_SIBLINGS);
98 i = blocks.iterator();
99 while (i.hasNext()) {
100 ASTVariableInitializer b = (ASTVariableInitializer)i.next();
101 assertTrue(b.getBeginLine() > 0);
102 }
103 blocks = getNodes(ASTExpression.class, LINE_NUMBERS_ON_SIBLINGS);
104 i = blocks.iterator();
105 while (i.hasNext()) {
106 ASTExpression b = (ASTExpression)i.next();
107 assertTrue(b.getBeginLine() > 0);
108 }
109 }
110
111 public void testFindChildrenOfType() {
112 ASTBlock block = new ASTBlock(2);
113 block.jjtAddChild(new ASTReturnStatement(1), 0);
114 assertEquals(1, block.findChildrenOfType(ASTReturnStatement.class).size());
115 }
116
117 public void testFindChildrenOfTypeMultiple() {
118 ASTBlock block = new ASTBlock(1);
119 block.jjtAddChild(new ASTBlockStatement(2), 0);
120 block.jjtAddChild(new ASTBlockStatement(3), 1);
121
122 List nodes = new ArrayList();
123 block.findChildrenOfType(ASTBlockStatement.class, nodes);
124 assertEquals(2, nodes.size());
125 }
126
127 public void testFindChildrenOfTypeRecurse() {
128 ASTBlock block = new ASTBlock(1);
129 ASTBlock childBlock = new ASTBlock(2);
130 block.jjtAddChild(childBlock, 0);
131 childBlock.jjtAddChild(new ASTMethodDeclaration(3), 0);
132
133 List nodes = new ArrayList();
134 block.findChildrenOfType(ASTMethodDeclaration.class, nodes);
135 assertEquals(1, nodes.size());
136 }
137
138 private void verifyNode(SimpleNode node, int beginLine, int beginCol, int endLine, int endCol) {
139 assertEquals("Wrong beginning line: ", beginLine, node.getBeginLine());
140 assertEquals("Wrong beginning column: ", beginCol, node.getBeginColumn());
141 assertEquals("Wrong ending line:", endLine, node.getEndLine());
142 assertEquals("Wrong ending column:", endCol, node.getEndColumn());
143 }
144
145 private static final String HAS_EXPLICIT_EXTENDS =
146 "public class Test extends Foo {}";
147
148 private static final String NO_EXPLICIT_EXTENDS =
149 "public class Test {}";
150
151 private static final String HAS_EXPLICIT_IMPLEMENTS =
152 "public class Test implements Foo {}";
153
154 private static final String NO_EXPLICIT_IMPLEMENTS =
155 "public class Test {}";
156
157 private static final String METHOD_DIFF_LINES =
158 "public class Test {" + PMD.EOL +
159 " public void foo() {" + PMD.EOL +
160 " int x;" + PMD.EOL +
161 " }" + PMD.EOL +
162 "}";
163
164 private static final String METHOD_SAME_LINE =
165 "public class Test {" + PMD.EOL +
166 " public void foo() {}" + PMD.EOL +
167 "}";
168
169 private static final String QUALIFIED_NAME =
170 "import java.io.File;" + PMD.EOL +
171 "public class Foo{}";
172
173 private static final String BROKEN_LINE_IN_NAME =
174 "import java.io." + PMD.EOL +
175 "File;" + PMD.EOL +
176 "public class Foo{}";
177
178 private static final String LINE_NUMBERS_ON_SIBLINGS =
179 "public class Foo {" + PMD.EOL +
180 " void bar() {" + PMD.EOL +
181 " try {" + PMD.EOL +
182 " } catch (Exception1 e) {" + PMD.EOL +
183 " int x =2;" + PMD.EOL +
184 " }" + PMD.EOL +
185 " if (x != null) {}" + PMD.EOL +
186 " }" + PMD.EOL +
187 "}";
188
189 private static final String NO_LOOKAHEAD = "public class Foo { }";
190 }
This page was automatically generated by Maven