PageRenderTime 60ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/pmd-4.2.5/regress/test/net/sourceforge/pmd/ast/SimpleNodeTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 343 lines | 275 code | 45 blank | 23 comment | 8 complexity | 031702bc207d883dd5d30f2e8141573f MD5 | raw file
  1. /**
  2. * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
  3. */
  4. package test.net.sourceforge.pmd.ast;
  5. import static org.junit.Assert.assertEquals;
  6. import static org.junit.Assert.assertFalse;
  7. import static org.junit.Assert.assertNotNull;
  8. import static org.junit.Assert.assertTrue;
  9. import net.sourceforge.pmd.PMD;
  10. import net.sourceforge.pmd.ast.ASTAssignmentOperator;
  11. import net.sourceforge.pmd.ast.ASTBlock;
  12. import net.sourceforge.pmd.ast.ASTBlockStatement;
  13. import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
  14. import net.sourceforge.pmd.ast.ASTCompilationUnit;
  15. import net.sourceforge.pmd.ast.ASTExpression;
  16. import net.sourceforge.pmd.ast.ASTExtendsList;
  17. import net.sourceforge.pmd.ast.ASTFieldDeclaration;
  18. import net.sourceforge.pmd.ast.ASTImplementsList;
  19. import net.sourceforge.pmd.ast.ASTMethodDeclaration;
  20. import net.sourceforge.pmd.ast.ASTName;
  21. import net.sourceforge.pmd.ast.ASTReturnStatement;
  22. import net.sourceforge.pmd.ast.ASTStatement;
  23. import net.sourceforge.pmd.ast.ASTVariableInitializer;
  24. import net.sourceforge.pmd.ast.Node;
  25. import net.sourceforge.pmd.ast.SimpleNode;
  26. import org.junit.Ignore;
  27. import org.junit.Test;
  28. import test.net.sourceforge.pmd.testframework.ParserTst;
  29. import java.util.ArrayList;
  30. import java.util.Iterator;
  31. import java.util.List;
  32. import java.util.Set;
  33. public class SimpleNodeTest extends ParserTst {
  34. @Test
  35. public void testMethodDiffLines() throws Throwable {
  36. Set methods = getNodes(ASTMethodDeclaration.class, METHOD_DIFF_LINES);
  37. Iterator iter = methods.iterator();
  38. verifyNode((SimpleNode) iter.next(), 2, 9, 4, 2);
  39. }
  40. @Test
  41. public void testMethodSameLine() throws Throwable {
  42. Set methods = getNodes(ASTMethodDeclaration.class, METHOD_SAME_LINE);
  43. verifyNode((SimpleNode) methods.iterator().next(), 2, 9, 2, 21);
  44. }
  45. @Test
  46. public void testNoLookahead() throws Throwable {
  47. String code = NO_LOOKAHEAD; // 1, 8 -> 1, 20
  48. Set uCD = getNodes(ASTClassOrInterfaceDeclaration.class, code);
  49. verifyNode((SimpleNode) uCD.iterator().next(), 1, 8, 1, 20);
  50. }
  51. @Test
  52. public void testHasExplicitExtends() throws Throwable {
  53. String code = HAS_EXPLICIT_EXTENDS;
  54. ASTClassOrInterfaceDeclaration ucd = getNodes(ASTClassOrInterfaceDeclaration.class, code).iterator().next();
  55. assertTrue(ucd.jjtGetChild(0) instanceof ASTExtendsList);
  56. }
  57. @Test
  58. public void testNoExplicitExtends() throws Throwable {
  59. String code = NO_EXPLICIT_EXTENDS;
  60. ASTClassOrInterfaceDeclaration ucd = getNodes(ASTClassOrInterfaceDeclaration.class, code).iterator().next();
  61. assertFalse(ucd.jjtGetChild(0) instanceof ASTExtendsList);
  62. }
  63. @Test
  64. public void testHasExplicitImplements() throws Throwable {
  65. String code = HAS_EXPLICIT_IMPLEMENTS;
  66. ASTClassOrInterfaceDeclaration ucd = getNodes(ASTClassOrInterfaceDeclaration.class, code).iterator().next();
  67. assertTrue(ucd.jjtGetChild(0) instanceof ASTImplementsList);
  68. }
  69. @Test
  70. public void testNoExplicitImplements() throws Throwable {
  71. String code = NO_EXPLICIT_IMPLEMENTS;
  72. ASTClassOrInterfaceDeclaration ucd = getNodes(ASTClassOrInterfaceDeclaration.class, code).iterator().next();
  73. assertFalse(ucd.jjtGetChild(0) instanceof ASTImplementsList);
  74. }
  75. @Test
  76. public void testColumnsOnQualifiedName() throws Throwable {
  77. Set name = getNodes(ASTName.class, QUALIFIED_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, 1, 19);
  83. }
  84. }
  85. }
  86. @Test
  87. public void testLineNumbersForNameSplitOverTwoLines() throws Throwable {
  88. Set name = getNodes(ASTName.class, BROKEN_LINE_IN_NAME);
  89. Iterator i = name.iterator();
  90. while (i.hasNext()) {
  91. SimpleNode node = (SimpleNode) i.next();
  92. if (node.getImage().equals("java.io.File")) {
  93. verifyNode(node, 1, 8, 2, 4);
  94. }
  95. if (node.getImage().equals("Foo")) {
  96. verifyNode(node, 2, 15, 2, 18);
  97. }
  98. }
  99. }
  100. @Test
  101. public void testLineNumbersAreSetOnAllSiblings() throws Throwable {
  102. Set blocks = getNodes(ASTBlock.class, LINE_NUMBERS_ON_SIBLINGS);
  103. Iterator i = blocks.iterator();
  104. while (i.hasNext()) {
  105. ASTBlock b = (ASTBlock) i.next();
  106. assertTrue(b.getBeginLine() > 0);
  107. }
  108. blocks = getNodes(ASTVariableInitializer.class, LINE_NUMBERS_ON_SIBLINGS);
  109. i = blocks.iterator();
  110. while (i.hasNext()) {
  111. ASTVariableInitializer b = (ASTVariableInitializer) i.next();
  112. assertTrue(b.getBeginLine() > 0);
  113. }
  114. blocks = getNodes(ASTExpression.class, LINE_NUMBERS_ON_SIBLINGS);
  115. i = blocks.iterator();
  116. while (i.hasNext()) {
  117. ASTExpression b = (ASTExpression) i.next();
  118. assertTrue(b.getBeginLine() > 0);
  119. }
  120. }
  121. @Test
  122. public void testFindChildrenOfType() {
  123. ASTBlock block = new ASTBlock(2);
  124. block.jjtAddChild(new ASTReturnStatement(1), 0);
  125. assertEquals(1, block.findChildrenOfType(ASTReturnStatement.class).size());
  126. }
  127. @Test
  128. public void testFindChildrenOfTypeMultiple() {
  129. ASTBlock block = new ASTBlock(1);
  130. block.jjtAddChild(new ASTBlockStatement(2), 0);
  131. block.jjtAddChild(new ASTBlockStatement(3), 1);
  132. List<ASTBlockStatement> nodes = new ArrayList<ASTBlockStatement>();
  133. block.findChildrenOfType(ASTBlockStatement.class, nodes);
  134. assertEquals(2, nodes.size());
  135. }
  136. @Test
  137. public void testFindChildrenOfTypeRecurse() {
  138. ASTBlock block = new ASTBlock(1);
  139. ASTBlock childBlock = new ASTBlock(2);
  140. block.jjtAddChild(childBlock, 0);
  141. childBlock.jjtAddChild(new ASTMethodDeclaration(3), 0);
  142. List<ASTMethodDeclaration> nodes = new ArrayList<ASTMethodDeclaration>();
  143. block.findChildrenOfType(ASTMethodDeclaration.class, nodes);
  144. assertEquals(1, nodes.size());
  145. }
  146. @Test
  147. public void testGetFirstChild() {
  148. ASTBlock block = new ASTBlock(1);
  149. ASTStatement x = new ASTStatement(2);
  150. block.jjtAddChild(x, 0);
  151. block.jjtAddChild(new ASTStatement(3), 1);
  152. Node n = block.getFirstChildOfType(ASTStatement.class);
  153. assertNotNull(n);
  154. assertTrue(n instanceof ASTStatement);
  155. assertEquals(x, n);
  156. }
  157. @Test
  158. public void testGetFirstChildNested() {
  159. ASTBlock block = new ASTBlock(1);
  160. ASTStatement x = new ASTStatement(2);
  161. ASTAssignmentOperator x1 = new ASTAssignmentOperator(4);
  162. x.jjtAddChild(x1, 1);
  163. block.jjtAddChild(x, 0);
  164. block.jjtAddChild(new ASTStatement(3), 1);
  165. Node n = block.getFirstChildOfType(ASTAssignmentOperator.class);
  166. assertNotNull(n);
  167. assertTrue(n instanceof ASTAssignmentOperator);
  168. assertEquals(x1, n);
  169. }
  170. @Test
  171. public void testGetFirstChildNestedDeeper() {
  172. ASTBlock block = new ASTBlock(1);
  173. ASTStatement x = new ASTStatement(2);
  174. ASTAssignmentOperator x1 = new ASTAssignmentOperator(4);
  175. ASTName x2 = new ASTName(5);
  176. x.jjtAddChild(x1, 1);
  177. x1.jjtAddChild(x2, 0);
  178. block.jjtAddChild(x, 0);
  179. block.jjtAddChild(new ASTStatement(3), 1);
  180. Node n = block.getFirstChildOfType(ASTName.class);
  181. assertNotNull(n);
  182. assertTrue(n instanceof ASTName);
  183. assertEquals(x2, n);
  184. }
  185. @Ignore
  186. @Test
  187. public void testContainsNoInner() throws Throwable {
  188. ASTCompilationUnit c = getNodes(ASTCompilationUnit.class, CONTAINS_NO_INNER).iterator().next();
  189. List<ASTFieldDeclaration> res = new ArrayList<ASTFieldDeclaration>();
  190. c.findChildrenOfType(ASTFieldDeclaration.class, res, false);
  191. assertTrue(res.isEmpty());
  192. /* String expectedXml = "<CompilationUnit BeginColumn=\"1\" BeginLine=\"5\" EndColumn=\"1\" EndLine=\"5\">" +
  193. "<TypeDeclaration BeginColumn=\"1\" BeginLine=\"1\" EndColumn=\"1\" EndLine=\"5\">" +
  194. "<ClassOrInterfaceDeclaration Abstract=\"false\" BeginColumn=\"8\" BeginLine=\"1\" EndColumn=\"1\" " +
  195. "EndLine=\"5\" Final=\"false\" Image=\"Test\" Interface=\"false\" Native=\"false\" Nested=\"false\" PackagePrivate=\"false\" Private=\"false\" Protected=\"false\" Public=\"true\" Static=\"false\" Strictfp=\"false\" Synchronized=\"false\" Transient=\"false\" Volatile=\"false\">" +
  196. "<ClassOrInterfaceBody BeginColumn=\"19\" BeginLine=\"1\" EndColumn=\"1\" EndLine=\"5\">" +
  197. "<ClassOrInterfaceBodyDeclaration AnonymousInnerClass=\"false\" BeginColumn=\"3\" BeginLine=\"2\" EndColumn=\"3\" EndLine=\"4\">" +
  198. "<ClassOrInterfaceDeclaration Abstract=\"false\" BeginColumn=\"10\" BeginLine=\"2\" EndColumn=\"3\" EndLine=\"4\" Final=\"false\" " +
  199. "Image=\"Inner\" Interface=\"false\" Native=\"false\" Nested=\"true\" PackagePrivate=\"false\" Private=\"false\" Protected=\"false\" " +
  200. "Public=\"true\" Static=\"false\" Strictfp=\"false\" Synchronized=\"false\" Transient=\"false\" Volatile=\"false\">" +
  201. "<ClassOrInterfaceBody BeginColumn=\"22\" BeginLine=\"2\" EndColumn=\"3\" EndLine=\"4\">" +
  202. "<ClassOrInterfaceBodyDeclaration AnonymousInnerClass=\"false\" BeginColumn=\"4\" BeginLine=\"3\" EndColumn=\"11\" EndLine=\"3\">" +
  203. "<FieldDeclaration Abstract=\"false\" Array=\"false\" ArrayDepth=\"0\" BeginColumn=\"4\" BeginLine=\"3\" EndColumn=\"11\" EndLine=\"3\" Final=\"false\" Native=\"false\" PackagePrivate=\"true\" Private=\"false\" Protected=\"false\" Public=\"false\" Static=\"false\" Strictfp=\"false\" Synchronized=\"false\" Transient=\"false\" VariableName=\"foo\" Volatile=\"false\"><Type Array=\"false\" ArrayDepth=\"0\" BeginColumn=\"4\" BeginLine=\"3\" EndColumn=\"6\" EndLine=\"3\">" +
  204. "<PrimitiveType Array=\"false\" ArrayDepth=\"0\" BeginColumn=\"4\" BeginLine=\"3\" Boolean=\"false\" EndColumn=\"6\" EndLine=\"3\" Image=\"int\"/>" +
  205. "</Type>" +
  206. "<VariableDeclarator BeginColumn=\"8\" BeginLine=\"3\" EndColumn=\"10\" EndLine=\"3\">" +
  207. "<VariableDeclaratorId Array=\"false\" ArrayDepth=\"0\" BeginColumn=\"8\" BeginLine=\"3\" EndColumn=\"10\" EndLine=\"3\" ExceptionBlockParameter=\"false\" Image=\"foo\"/>" +
  208. "</VariableDeclarator></FieldDeclaration></ClassOrInterfaceBodyDeclaration></ClassOrInterfaceBody>" +
  209. "</ClassOrInterfaceDeclaration></ClassOrInterfaceBodyDeclaration></ClassOrInterfaceBody></ClassOrInterfaceDeclaration>" +
  210. "</TypeDeclaration></CompilationUnit>";
  211. assertEquals( expectedXml, getXmlString( c ) );
  212. */ }
  213. @Test
  214. public void testContainsNoInnerWithAnonInner() throws Throwable {
  215. ASTCompilationUnit c = getNodes(ASTCompilationUnit.class, CONTAINS_NO_INNER_WITH_ANON_INNER).iterator().next();
  216. List<ASTFieldDeclaration> res = new ArrayList<ASTFieldDeclaration>();
  217. c.findChildrenOfType(ASTFieldDeclaration.class, res, false);
  218. assertTrue(res.isEmpty());
  219. }
  220. @Test
  221. public void testContainsChildOfType() throws Throwable {
  222. ASTClassOrInterfaceDeclaration c = getNodes(ASTClassOrInterfaceDeclaration.class, CONTAINS_CHILDREN_OF_TYPE).iterator().next();
  223. assertTrue(c.containsChildOfType(ASTFieldDeclaration.class));
  224. }
  225. @Test
  226. public void testXPathNodeSelect() throws Throwable {
  227. ASTClassOrInterfaceDeclaration c = getNodes(ASTClassOrInterfaceDeclaration.class, TEST_XPATH).iterator().next();
  228. List nodes = c.findChildNodesWithXPath("//FieldDeclaration");
  229. assertEquals(2, nodes.size());
  230. assertTrue(nodes.get(0) instanceof ASTFieldDeclaration);
  231. }
  232. private void verifyNode(SimpleNode node, int beginLine, int beginCol, int endLine, int endCol) {
  233. assertEquals("Unexpected beginning line: ", beginLine, node.getBeginLine());
  234. assertEquals("Unexpected beginning column: ", beginCol, node.getBeginColumn());
  235. assertEquals("Unexpected ending line:", endLine, node.getEndLine());
  236. assertEquals("Unexpected ending column:", endCol, node.getEndColumn());
  237. }
  238. private static final String HAS_EXPLICIT_EXTENDS =
  239. "public class Test extends Foo {}";
  240. private static final String NO_EXPLICIT_EXTENDS =
  241. "public class Test {}";
  242. private static final String HAS_EXPLICIT_IMPLEMENTS =
  243. "public class Test implements Foo {}";
  244. private static final String NO_EXPLICIT_IMPLEMENTS =
  245. "public class Test {}";
  246. private static final String METHOD_SAME_LINE =
  247. "public class Test {" + PMD.EOL +
  248. " public void foo() {}" + PMD.EOL +
  249. "}";
  250. private static final String QUALIFIED_NAME =
  251. "import java.io.File;" + PMD.EOL +
  252. "public class Foo{}";
  253. private static final String BROKEN_LINE_IN_NAME =
  254. "import java.io." + PMD.EOL +
  255. "File;" + PMD.EOL +
  256. "public class Foo{}";
  257. private static final String LINE_NUMBERS_ON_SIBLINGS =
  258. "public class Foo {" + PMD.EOL +
  259. " void bar() {" + PMD.EOL +
  260. " try {" + PMD.EOL +
  261. " } catch (Exception1 e) {" + PMD.EOL +
  262. " int x =2;" + PMD.EOL +
  263. " }" + PMD.EOL +
  264. " if (x != null) {}" + PMD.EOL +
  265. " }" + PMD.EOL +
  266. "}";
  267. private static final String NO_LOOKAHEAD = "public class Foo { }";
  268. private static final String METHOD_DIFF_LINES =
  269. "public class Test {" + PMD.EOL +
  270. " public void foo() {" + PMD.EOL +
  271. " int x;" + PMD.EOL +
  272. " }" + PMD.EOL +
  273. "}";
  274. private static final String CONTAINS_CHILDREN_OF_TYPE =
  275. "public class Test {" + PMD.EOL +
  276. " int x;" + PMD.EOL +
  277. "}";
  278. private static final String CONTAINS_NO_INNER =
  279. "public class Test {" + PMD.EOL +
  280. " public class Inner {" + PMD.EOL +
  281. " int foo;" + PMD.EOL +
  282. " }" + PMD.EOL +
  283. "}";
  284. private static final String CONTAINS_NO_INNER_WITH_ANON_INNER =
  285. "public class Test {" + PMD.EOL +
  286. " void bar() {" + PMD.EOL +
  287. " foo(new Fuz() { int x = 2;});" + PMD.EOL +
  288. " }" + PMD.EOL +
  289. "}";
  290. private static final String TEST_XPATH =
  291. "public class Test {" + PMD.EOL +
  292. " int x = 2;" + PMD.EOL +
  293. " int y = 42;" + PMD.EOL +
  294. "}";
  295. public static junit.framework.Test suite() {
  296. return new junit.framework.JUnit4TestAdapter(SimpleNodeTest.class);
  297. }
  298. }