diff --git a/pmd-javascript/pom.xml b/pmd-javascript/pom.xml index 28115957d3c..a2df98aeff7 100644 --- a/pmd-javascript/pom.xml +++ b/pmd-javascript/pom.xml @@ -96,5 +96,15 @@ pmd-test test + + org.junit.vintage + junit-vintage-engine + test + + + net.sourceforge.pmd + pmd-lang-test + test + diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTAstRoot.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTAstRoot.java index 1da66dbed00..b4976f84e1c 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTAstRoot.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTAstRoot.java @@ -6,7 +6,9 @@ import org.mozilla.javascript.ast.AstRoot; -public class ASTAstRoot extends AbstractEcmascriptNode { +import net.sourceforge.pmd.lang.ast.RootNode; + +public class ASTAstRoot extends AbstractEcmascriptNode implements RootNode { public ASTAstRoot(AstRoot astRoot) { super(astRoot); } diff --git a/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTFunctionNodeTest.java b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTFunctionNodeTest.java index ef040c68b2c..40726ff26af 100644 --- a/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTFunctionNodeTest.java +++ b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTFunctionNodeTest.java @@ -11,7 +11,7 @@ public class ASTFunctionNodeTest extends EcmascriptParserTestBase { @Test public void testGetBody() { - ASTAstRoot node = parse("function foo() { var a = 'a'; }"); + ASTAstRoot node = js.parse("function foo() { var a = 'a'; }"); ASTFunctionNode fn = node.getFirstDescendantOfType(ASTFunctionNode.class); Assert.assertFalse(fn.isClosure()); EcmascriptNode body = fn.getBody(); @@ -20,7 +20,7 @@ public void testGetBody() { @Test public void testGetBodyFunctionClosureExpression() { - ASTAstRoot node = parse18("(function(x) x*x)"); + ASTAstRoot node = js18.parse("(function(x) x*x)"); ASTFunctionNode fn = node.getFirstDescendantOfType(ASTFunctionNode.class); Assert.assertTrue(fn.isClosure()); EcmascriptNode body = fn.getBody(); diff --git a/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTTryStatementTest.java b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTTryStatementTest.java index 738676eabcb..40dcb6c0973 100644 --- a/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTTryStatementTest.java +++ b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTTryStatementTest.java @@ -17,7 +17,7 @@ public class ASTTryStatementTest extends EcmascriptParserTestBase { private ASTTryStatement getTryStmt(String js) { - EcmascriptNode node = parse(js); + EcmascriptNode node = this.js.parse(js); List trys = node.findDescendantsOfType(ASTTryStatement.class); Assert.assertEquals(1, trys.size()); ASTTryStatement tryStmt = trys.get(0); diff --git a/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserTest.java b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserTest.java index 3c6c300e598..15d69e3f728 100644 --- a/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserTest.java +++ b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserTest.java @@ -32,7 +32,7 @@ public class EcmascriptParserTest extends EcmascriptParserTestBase { @Test public void testLineNumbers() { final String SOURCE_CODE = "function a() {" + PMD.EOL + " alert('hello');" + PMD.EOL + "}" + PMD.EOL; - EcmascriptNode node = parse(SOURCE_CODE); + EcmascriptNode node = js.parse(SOURCE_CODE); assertEquals(1, node.getBeginLine()); assertEquals(1, node.getBeginColumn()); assertEquals(3, node.getEndLine()); @@ -69,7 +69,7 @@ public Object visit(ASTScope node, Object data) { MyEcmascriptRule rule = new MyEcmascriptRule(); RuleContext ctx = new RuleContext(); - rule.apply(Arrays.asList(parse(source)), ctx); + rule.apply(Arrays.asList(js.parse(source)), ctx); assertEquals("Scope from 2 to 4", output.get(0)); assertEquals("Scope from 4 to 6", output.get(1)); @@ -80,7 +80,7 @@ public Object visit(ASTScope node, Object data) { */ @Test public void testArrayAccess() { - EcmascriptNode node = parse("function a() { b['a'] = 1; c[1] = 2; }"); + EcmascriptNode node = js.parse("function a() { b['a'] = 1; c[1] = 2; }"); List arrays = node.findDescendantsOfType(ASTElementGet.class); assertEquals("b", arrays.get(0).getTarget().getImage()); assertEquals("a", arrays.get(0).getElement().getImage()); @@ -94,9 +94,9 @@ public void testArrayAccess() { */ @Test public void testArrayMethod() { - EcmascriptNode rootNode = parse( - "function test(){\n" + " a(); // OK\n" + " b.c(); // OK\n" + " d[0](); // OK\n" - + " e[0].f(); // OK\n" + " y.z[0](); // FAIL ==> java.lang.NullPointerException\n" + "}"); + EcmascriptNode rootNode = js.parse( + "function test(){\n" + " a(); // OK\n" + " b.c(); // OK\n" + " d[0](); // OK\n" + + " e[0].f(); // OK\n" + " y.z[0](); // FAIL ==> java.lang.NullPointerException\n" + "}"); List calls = rootNode.findDescendantsOfType(ASTFunctionCall.class); List results = new ArrayList<>(); @@ -129,7 +129,7 @@ private String getName(Node node) { */ @Test public void testCaseAsIdentifier() { - ASTAstRoot rootNode = parse("function f(a){\n" + " a.case.flag = 1;\n" + " return;\n" + "}"); + ASTAstRoot rootNode = js.parse("function f(a){\n" + " a.case.flag = 1;\n" + " return;\n" + "}"); ASTBlock block = rootNode.getFirstDescendantOfType(ASTBlock.class); assertFalse(block.jjtGetChild(0) instanceof ASTEmptyExpression); assertTrue(block.jjtGetChild(0) instanceof ASTExpressionStatement); @@ -163,9 +163,9 @@ public void testSuppresionComment() { */ @Test public void testVoidKeyword() { - ASTAstRoot rootNode = parse("function f(matchFn, fieldval, n){\n" - + " return (matchFn)?(matcharray = eval(matchFn+\"('\"+fieldval+\"','\"+n.id+\"')\")):void(0);\n" - + "}\n"); + ASTAstRoot rootNode = js.parse("function f(matchFn, fieldval, n){\n" + + " return (matchFn)?(matcharray = eval(matchFn+\"('\"+fieldval+\"','\"+n.id+\"')\")):void(0);\n" + + "}\n"); ASTUnaryExpression unary = rootNode.getFirstDescendantOfType(ASTUnaryExpression.class); assertEquals("void", unary.getImage()); } @@ -175,9 +175,9 @@ public void testVoidKeyword() { */ @Test public void testXorAssignment() { - ASTAstRoot rootNode = parse("function f() { var x = 2; x ^= 2; x &= 2; x |= 2; " - + "x &&= true; x ||= false; x *= 2; x /= 2; x %= 2; x += 2; x -= 2; " - + "x <<= 2; x >>= 2; x >>>= 2; }"); + ASTAstRoot rootNode = js.parse("function f() { var x = 2; x ^= 2; x &= 2; x |= 2; " + + "x &&= true; x ||= false; x *= 2; x /= 2; x %= 2; x += 2; x -= 2; " + + "x <<= 2; x >>= 2; x >>>= 2; }"); ASTAssignment infix = rootNode.getFirstDescendantOfType(ASTAssignment.class); assertEquals("^=", infix.getImage()); } diff --git a/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserTestBase.java b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserTestBase.java index bb9df1d72db..cf6eade47e6 100644 --- a/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserTestBase.java +++ b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserTestBase.java @@ -4,32 +4,21 @@ package net.sourceforge.pmd.lang.ecmascript.ast; -import java.io.Reader; -import java.io.StringReader; -import java.io.StringWriter; - +import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.ecmascript.EcmascriptParserOptions; +import net.sourceforge.pmd.lang.ecmascript.EcmascriptParserOptions.Version; public abstract class EcmascriptParserTestBase { - public ASTAstRoot parse(String code) { - EcmascriptParser parser = new EcmascriptParser(new EcmascriptParserOptions()); - Reader sourceCode = new StringReader(code); - return (ASTAstRoot) parser.parse(sourceCode); - } - public ASTAstRoot parse18(String code) { + protected final JsParsingHelper js = JsParsingHelper.DEFAULT.withResourceContext(getClass()); + + protected final JsParsingHelper js18 = JsParsingHelper.DEFAULT.withResourceContext(getClass()) + .withParserOptions(parserVersion(Version.VERSION_1_8)); + + public ParserOptions parserVersion(EcmascriptParserOptions.Version version) { EcmascriptParserOptions parserOptions = new EcmascriptParserOptions(); - parserOptions.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_1_8); - EcmascriptParser parser = new EcmascriptParser(parserOptions); - Reader sourceCode = new StringReader(code); - return (ASTAstRoot) parser.parse(sourceCode); + parserOptions.setRhinoLanguageVersion(version); + return parserOptions; } - public String dump(EcmascriptNode node) { - DumpFacade dumpFacade = new DumpFacade(); - StringWriter writer = new StringWriter(); - dumpFacade.initializeWith(writer, "", true, node); - dumpFacade.visit(node, ""); - return writer.toString(); - } } diff --git a/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/JsParsingHelper.java b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/JsParsingHelper.java new file mode 100644 index 00000000000..833d9e62ff5 --- /dev/null +++ b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/JsParsingHelper.java @@ -0,0 +1,25 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ecmascript.ast; + +import org.jetbrains.annotations.NotNull; + +import net.sourceforge.pmd.lang.ast.test.BaseParsingHelper; +import net.sourceforge.pmd.lang.ecmascript.EcmascriptLanguageModule; + +public final class JsParsingHelper extends BaseParsingHelper { + + public static final JsParsingHelper DEFAULT = new JsParsingHelper(Params.getDefaultProcess()); + + private JsParsingHelper(@NotNull Params params) { + super(EcmascriptLanguageModule.NAME, ASTAstRoot.class, params); + } + + @NotNull + @Override + protected JsParsingHelper clone(@NotNull Params params) { + return new JsParsingHelper(params); + } +} diff --git a/pmd-jsp/pom.xml b/pmd-jsp/pom.xml index a0052f7229b..d0dbb6455bc 100644 --- a/pmd-jsp/pom.xml +++ b/pmd-jsp/pom.xml @@ -93,10 +93,20 @@ junit test + + org.junit.vintage + junit-vintage-engine + test + net.sourceforge.pmd pmd-test test + + net.sourceforge.pmd + pmd-lang-test + test + diff --git a/pmd-jsp/src/test/java/net/sourceforge/pmd/lang/jsp/ast/AbstractJspNodesTst.java b/pmd-jsp/src/test/java/net/sourceforge/pmd/lang/jsp/ast/AbstractJspNodesTst.java index 3cd7977511f..fd8c4082fb9 100644 --- a/pmd-jsp/src/test/java/net/sourceforge/pmd/lang/jsp/ast/AbstractJspNodesTst.java +++ b/pmd-jsp/src/test/java/net/sourceforge/pmd/lang/jsp/ast/AbstractJspNodesTst.java @@ -4,68 +4,8 @@ package net.sourceforge.pmd.lang.jsp.ast; -import static org.junit.Assert.assertEquals; - -import java.io.StringReader; -import java.util.HashSet; -import java.util.Set; - -import net.sourceforge.pmd.lang.ast.JavaCharStream; -import net.sourceforge.pmd.lang.ast.Node; - public abstract class AbstractJspNodesTst { - public void assertNumberOfNodes(Class clazz, String source, int number) { - Set nodes = getNodes(clazz, source); - assertEquals("Exactly " + number + " element(s) expected", number, nodes.size()); - } - - /** - * Run the JSP parser on the source, and return the nodes of type clazz. - * - * @param clazz - * @param source - * @return Set - */ - public Set getNodes(Class clazz, String source) { - JspParser parser = new JspParser(new JavaCharStream(new StringReader(source))); - Node rootNode = parser.CompilationUnit(); - Set nodes = new HashSet<>(); - addNodeAndSubnodes(rootNode, nodes, clazz); - return nodes; - } - - /** - * Return a subset of allNodes, containing the items in allNodes that are of - * the given type. - * - * @param clazz - * @param allNodes - * @return Set - */ - public Set getNodesOfType(Class clazz, Set allNodes) { - Set result = new HashSet<>(); - for (Node node : allNodes) { - if (clazz.equals(node.getClass())) { - result.add((T) node); - } - } - return result; - } - - /** - * Add the given node and its subnodes to the set of nodes. If clazz is not - * null, only nodes of the given class are put in the set of nodes. - */ - private void addNodeAndSubnodes(Node node, Set nodes, Class clazz) { - if (null != node) { - if ((null == clazz) || (clazz.equals(node.getClass()))) { - nodes.add((T) node); - } - for (int i = 0; i < node.jjtGetNumChildren(); i++) { - addNodeAndSubnodes(node.jjtGetChild(i), nodes, clazz); - } - } - } + protected JspParsingHelper jsp = JspParsingHelper.DEFAULT.withResourceContext(getClass()); } diff --git a/pmd-jsp/src/test/java/net/sourceforge/pmd/lang/jsp/ast/JspDocStyleTest.java b/pmd-jsp/src/test/java/net/sourceforge/pmd/lang/jsp/ast/JspDocStyleTest.java index c62d1fc5b51..26a54a3f8b2 100644 --- a/pmd-jsp/src/test/java/net/sourceforge/pmd/lang/jsp/ast/JspDocStyleTest.java +++ b/pmd-jsp/src/test/java/net/sourceforge/pmd/lang/jsp/ast/JspDocStyleTest.java @@ -9,11 +9,11 @@ import static org.junit.Assert.assertTrue; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; -import java.util.Set; import org.junit.Ignore; import org.junit.Test; @@ -33,7 +33,8 @@ public class JspDocStyleTest extends AbstractJspNodesTst { */ @Test public void testSimplestJsp() { - assertNumberOfNodes(ASTElement.class, TEST_SIMPLEST_HTML, 1); + List nodes = jsp.getNodes(ASTElement.class, TEST_SIMPLEST_HTML); + assertEquals("Exactly " + 1 + " element(s) expected", 1, nodes.size()); } /** @@ -41,22 +42,22 @@ public void testSimplestJsp() { */ @Test public void testElementAttributeAndNamespace() { - Set nodes = getNodes(null, TEST_ELEMENT_AND_NAMESPACE); + ASTCompilationUnit root = jsp.parse(TEST_ELEMENT_AND_NAMESPACE); - Set elementNodes = getNodesOfType(ASTElement.class, nodes); + List elementNodes = root.findDescendantsOfType(ASTElement.class); assertEquals("One element node expected!", 1, elementNodes.size()); - ASTElement element = elementNodes.iterator().next(); + ASTElement element = elementNodes.get(0); assertEquals("Correct name expected!", "h:html", element.getName()); - assertEquals("Has namespace prefix!", true, element.isHasNamespacePrefix()); - assertEquals("Element is empty!", true, element.isEmpty()); + assertTrue("Has namespace prefix!", element.isHasNamespacePrefix()); + assertTrue("Element is empty!", element.isEmpty()); assertEquals("Correct namespace prefix of element expected!", "h", element.getNamespacePrefix()); assertEquals("Correct local name of element expected!", "html", element.getLocalName()); - Set attributeNodes = getNodesOfType(ASTAttribute.class, nodes); + List attributeNodes = root.findDescendantsOfType(ASTAttribute.class); assertEquals("One attribute node expected!", 1, attributeNodes.size()); - ASTAttribute attribute = attributeNodes.iterator().next(); + ASTAttribute attribute = attributeNodes.get(0); assertEquals("Correct name expected!", "MyNsPrefix:MyAttr", attribute.getName()); - assertEquals("Has namespace prefix!", true, attribute.isHasNamespacePrefix()); + assertTrue("Has namespace prefix!", attribute.isHasNamespacePrefix()); assertEquals("Correct namespace prefix of element expected!", "MyNsPrefix", attribute.getNamespacePrefix()); assertEquals("Correct local name of element expected!", "MyAttr", attribute.getLocalName()); @@ -69,32 +70,26 @@ public void testElementAttributeAndNamespace() { */ @Test public void testAttributeValueContainingHash() { - Set nodes = getNodes(null, TEST_ATTRIBUTE_VALUE_CONTAINING_HASH); + ASTCompilationUnit root = jsp.parse(TEST_ATTRIBUTE_VALUE_CONTAINING_HASH); - Set attributes = getNodesOfType(ASTAttribute.class, nodes); - assertEquals("Three attributes expected!", 3, attributes.size()); - - List attrsList = new ArrayList<>(attributes); - Collections.sort(attrsList, new Comparator() { - public int compare(ASTAttribute arg0, ASTAttribute arg1) { - return arg0.getName().compareTo(arg1.getName()); - } - }); + List attrsList = root.findDescendantsOfType(ASTAttribute.class); + assertEquals("Three attributes expected!", 3, attrsList.size()); ASTAttribute attr = attrsList.get(0); + + assertEquals("Correct attribute name expected!", "something", attr.getName()); + assertEquals("Correct attribute value expected!", "#yes#", + attr.getFirstDescendantOfType(ASTAttributeValue.class).getImage()); + + attr = attrsList.get(1); assertEquals("Correct attribute name expected!", "foo", attr.getName()); assertEquals("Correct attribute value expected!", "CREATE", - attr.getFirstDescendantOfType(ASTAttributeValue.class).getImage()); + attr.getFirstDescendantOfType(ASTAttributeValue.class).getImage()); - attr = attrsList.get(1); + attr = attrsList.get(2); assertEquals("Correct attribute name expected!", "href", attr.getName()); assertEquals("Correct attribute value expected!", "#", - attr.getFirstDescendantOfType(ASTAttributeValue.class).getImage()); - - attr = attrsList.get(2); - assertEquals("Correct attribute name expected!", "something", attr.getName()); - assertEquals("Correct attribute value expected!", "#yes#", - attr.getFirstDescendantOfType(ASTAttributeValue.class).getImage()); + attr.getFirstDescendantOfType(ASTAttributeValue.class).getImage()); } /** @@ -102,10 +97,10 @@ public int compare(ASTAttribute arg0, ASTAttribute arg1) { */ @Test public void testCData() { - Set cdataNodes = getNodes(ASTCData.class, TEST_CDATA); + List cdataNodes = jsp.getNodes(ASTCData.class, TEST_CDATA); assertEquals("One CDATA node expected!", 1, cdataNodes.size()); - ASTCData cdata = cdataNodes.iterator().next(); + ASTCData cdata = cdataNodes.get(0); assertEquals("Content incorrectly parsed!", " some ]] ]> ", cdata.getImage()); } @@ -114,14 +109,14 @@ public void testCData() { */ @Test public void testDoctype() { - Set nodes = getNodes(null, TEST_DOCTYPE); + ASTCompilationUnit root = jsp.parse(TEST_DOCTYPE); - Set docTypeDeclarations = getNodesOfType(ASTDoctypeDeclaration.class, nodes); + List docTypeDeclarations = root.findDescendantsOfType(ASTDoctypeDeclaration.class); assertEquals("One doctype declaration expected!", 1, docTypeDeclarations.size()); ASTDoctypeDeclaration docTypeDecl = docTypeDeclarations.iterator().next(); assertEquals("Correct doctype-name expected!", "html", docTypeDecl.getName()); - Set externalIds = getNodesOfType(ASTDoctypeExternalId.class, nodes); + List externalIds = root.findDescendantsOfType(ASTDoctypeExternalId.class); assertEquals("One doctype external id expected!", 1, externalIds.size()); ASTDoctypeExternalId externalId = externalIds.iterator().next(); assertEquals("Correct external public id expected!", "-//W3C//DTD XHTML 1.1//EN", externalId.getPublicId()); @@ -136,7 +131,7 @@ public void testDoctype() { */ @Test public void testComment() { - Set comments = getNodes(ASTCommentTag.class, TEST_COMMENT); + List comments = jsp.getNodes(ASTCommentTag.class, TEST_COMMENT); assertEquals("One comment expected!", 1, comments.size()); ASTCommentTag comment = comments.iterator().next(); assertEquals("Correct comment content expected!", "comment", comment.getImage()); @@ -147,7 +142,7 @@ public void testComment() { */ @Test public void testHtmlScript() { - Set scripts = getNodes(ASTHtmlScript.class, TEST_HTML_SCRIPT); + List scripts = jsp.getNodes(ASTHtmlScript.class, TEST_HTML_SCRIPT); assertEquals("One script expected!", 1, scripts.size()); ASTHtmlScript script = scripts.iterator().next(); assertEquals("Correct script content expected!", "Script!", script.getImage()); @@ -159,7 +154,7 @@ public void testHtmlScript() { */ @Test public void testImportHtmlScript() { - Set scripts = getNodes(ASTHtmlScript.class, TEST_IMPORT_JAVASCRIPT); + List scripts = jsp.getNodes(ASTHtmlScript.class, TEST_IMPORT_JAVASCRIPT); assertEquals("One script expected!", 1, scripts.size()); ASTHtmlScript script = scripts.iterator().next(); List value = script.findDescendantsOfType(ASTAttributeValue.class); @@ -171,7 +166,7 @@ public void testImportHtmlScript() { */ @Test public void testHtmlScriptWithAttribute() { - Set scripts = getNodes(ASTHtmlScript.class, TEST_HTML_SCRIPT_WITH_ATTRIBUTE); + List scripts = jsp.getNodes(ASTHtmlScript.class, TEST_HTML_SCRIPT_WITH_ATTRIBUTE); assertEquals("One script expected!", 1, scripts.size()); ASTHtmlScript script = scripts.iterator().next(); assertEquals("Correct script content expected!", "Script!", script.getImage()); @@ -184,11 +179,11 @@ public void testHtmlScriptWithAttribute() { */ @Test public void testComplexHtmlScript() { - Set script = getNodes(ASTHtmlScript.class, TEST_COMPLEX_SCRIPT); + List script = jsp.getNodes(ASTHtmlScript.class, TEST_COMPLEX_SCRIPT); assertEquals("One script expected!", 1, script.size()); ASTHtmlScript next = script.iterator().next(); assertTrue(next.getImage().contains("