Merge branch 'pr-2177'

[lang-test] Factorize parsing logic for tests
This commit is contained in:
Andreas Dangel
2020-01-10 12:29:38 +01:00
134 changed files with 1786 additions and 2534 deletions

View File

@@ -93,10 +93,20 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-lang-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -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 <T extends JspNode> void assertNumberOfNodes(Class<T> clazz, String source, int number) {
Set<T> 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 <T extends JspNode> Set<T> getNodes(Class<T> clazz, String source) {
JspParser parser = new JspParser(new JavaCharStream(new StringReader(source)));
Node rootNode = parser.CompilationUnit();
Set<T> 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 <T extends JspNode> Set<T> getNodesOfType(Class<T> clazz, Set<JspNode> allNodes) {
Set<T> 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 <T extends JspNode> void addNodeAndSubnodes(Node node, Set<T> nodes, Class<T> 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());
}

View File

File diff suppressed because it is too large Load Diff

View File

@@ -6,11 +6,7 @@ package net.sourceforge.pmd.lang.jsp.ast;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import org.junit.Test;
@@ -21,7 +17,7 @@ public class JspPageStyleTest extends AbstractJspNodesTst {
*/
@Test
public void testComment() {
Set<ASTJspComment> comments = getNodes(ASTJspComment.class, JSP_COMMENT);
List<ASTJspComment> comments = jsp.getNodes(ASTJspComment.class, JSP_COMMENT);
assertEquals("One comment expected!", 1, comments.size());
ASTJspComment comment = comments.iterator().next();
assertEquals("Correct comment content expected!", "some comment", comment.getImage());
@@ -32,28 +28,21 @@ public class JspPageStyleTest extends AbstractJspNodesTst {
*/
@Test
public void testDirective() {
Set<JspNode> nodes = getNodes(null, JSP_DIRECTIVE);
ASTCompilationUnit root = jsp.parse(JSP_DIRECTIVE);
Set<ASTJspDirective> directives = getNodesOfType(ASTJspDirective.class, nodes);
List<ASTJspDirective> directives = root.findDescendantsOfType(ASTJspDirective.class);
assertEquals("One directive expected!", 1, directives.size());
ASTJspDirective directive = directives.iterator().next();
assertEquals("Correct directive name expected!", "page", directive.getName());
Set<ASTJspDirectiveAttribute> directiveAttrs = getNodesOfType(ASTJspDirectiveAttribute.class, nodes);
List<ASTJspDirectiveAttribute> directiveAttrs = root.findDescendantsOfType(ASTJspDirectiveAttribute.class);
assertEquals("Two directive attributes expected!", 2, directiveAttrs.size());
List<ASTJspDirectiveAttribute> attrsList = new ArrayList<>(directiveAttrs);
Collections.sort(attrsList, new Comparator<ASTJspDirectiveAttribute>() {
public int compare(ASTJspDirectiveAttribute arg0, ASTJspDirectiveAttribute arg1) {
return arg0.getName().compareTo(arg1.getName());
}
});
ASTJspDirectiveAttribute attr = attrsList.get(0);
ASTJspDirectiveAttribute attr = directiveAttrs.get(0);
assertEquals("Correct directive attribute name expected!", "language", attr.getName());
assertEquals("Correct directive attribute value expected!", "java", attr.getValue());
attr = attrsList.get(1);
attr = directiveAttrs.get(1);
assertEquals("Correct directive attribute name expected!", "session", attr.getName());
assertEquals("Correct directive attribute value expected!", "true", attr.getValue());
@@ -64,7 +53,7 @@ public class JspPageStyleTest extends AbstractJspNodesTst {
*/
@Test
public void testDeclaration() {
Set<ASTJspDeclaration> declarations = getNodes(ASTJspDeclaration.class, JSP_DECLARATION);
List<ASTJspDeclaration> declarations = jsp.getNodes(ASTJspDeclaration.class, JSP_DECLARATION);
assertEquals("One declaration expected!", 1, declarations.size());
ASTJspDeclaration declaration = declarations.iterator().next();
assertEquals("Correct declaration content expected!", "String someString = \"s\";", declaration.getImage());
@@ -75,7 +64,7 @@ public class JspPageStyleTest extends AbstractJspNodesTst {
*/
@Test
public void testScriptlet() {
Set<ASTJspScriptlet> scriptlets = getNodes(ASTJspScriptlet.class, JSP_SCRIPTLET);
List<ASTJspScriptlet> scriptlets = jsp.getNodes(ASTJspScriptlet.class, JSP_SCRIPTLET);
assertEquals("One scriptlet expected!", 1, scriptlets.size());
ASTJspScriptlet scriptlet = scriptlets.iterator().next();
assertEquals("Correct scriptlet content expected!", "someString = someString + \"suffix\";",
@@ -87,7 +76,7 @@ public class JspPageStyleTest extends AbstractJspNodesTst {
*/
@Test
public void testExpression() {
Set<ASTJspExpression> expressions = getNodes(ASTJspExpression.class, JSP_EXPRESSION);
List<ASTJspExpression> expressions = jsp.getNodes(ASTJspExpression.class, JSP_EXPRESSION);
assertEquals("One expression expected!", 1, expressions.size());
ASTJspExpression expression = expressions.iterator().next();
assertEquals("Correct expression content expected!", "someString", expression.getImage());
@@ -98,8 +87,7 @@ public class JspPageStyleTest extends AbstractJspNodesTst {
*/
@Test
public void testExpressionInAttribute() {
Set<ASTJspExpressionInAttribute> expressions = getNodes(ASTJspExpressionInAttribute.class,
JSP_EXPRESSION_IN_ATTRIBUTE);
List<ASTJspExpressionInAttribute> expressions = jsp.getNodes(ASTJspExpressionInAttribute.class, JSP_EXPRESSION_IN_ATTRIBUTE);
assertEquals("One expression expected!", 1, expressions.size());
ASTJspExpressionInAttribute expression = expressions.iterator().next();
assertEquals("Correct expression content expected!", "style.getClass()", expression.getImage());
@@ -110,7 +98,7 @@ public class JspPageStyleTest extends AbstractJspNodesTst {
*/
@Test
public void testElExpression() {
Set<ASTElExpression> expressions = getNodes(ASTElExpression.class, JSP_EL_EXPRESSION);
List<ASTElExpression> expressions = jsp.getNodes(ASTElExpression.class, JSP_EL_EXPRESSION);
assertEquals("One expression expected!", 1, expressions.size());
ASTElExpression expression = expressions.iterator().next();
assertEquals("Correct expression content expected!", "myBean.get(\"${ World }\")", expression.getImage());
@@ -121,7 +109,7 @@ public class JspPageStyleTest extends AbstractJspNodesTst {
*/
@Test
public void testElExpressionInAttribute() {
Set<ASTElExpression> expressions = getNodes(ASTElExpression.class, JSP_EL_EXPRESSION_IN_ATTRIBUTE);
List<ASTElExpression> expressions = jsp.getNodes(ASTElExpression.class, JSP_EL_EXPRESSION_IN_ATTRIBUTE);
assertEquals("One expression expected!", 1, expressions.size());
ASTElExpression expression = expressions.iterator().next();
assertEquals("Correct expression content expected!", "myValidator.find(\"'jsp'\")", expression.getImage());
@@ -132,7 +120,7 @@ public class JspPageStyleTest extends AbstractJspNodesTst {
*/
@Test
public void testJsfValueBinding() {
Set<ASTValueBinding> valueBindings = getNodes(ASTValueBinding.class, JSF_VALUE_BINDING);
List<ASTValueBinding> valueBindings = jsp.getNodes(ASTValueBinding.class, JSF_VALUE_BINDING);
assertEquals("One value binding expected!", 1, valueBindings.size());
ASTValueBinding valueBinding = valueBindings.iterator().next();
assertEquals("Correct expression content expected!", "myValidator.find(\"'jsf'\")", valueBinding.getImage());

View File

@@ -0,0 +1,22 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.jsp.ast;
import net.sourceforge.pmd.lang.ast.test.BaseParsingHelper;
import net.sourceforge.pmd.lang.jsp.JspLanguageModule;
public final class JspParsingHelper extends BaseParsingHelper<JspParsingHelper, ASTCompilationUnit> {
public static final JspParsingHelper DEFAULT = new JspParsingHelper(Params.getDefaultProcess());
private JspParsingHelper(Params params) {
super(JspLanguageModule.NAME, ASTCompilationUnit.class, params);
}
@Override
protected JspParsingHelper clone(Params params) {
return new JspParsingHelper(params);
}
}