Move visitor to VfParser#parse

LanguageVersionHandler#getTypeResolutionFacade is deprecated. Moved the VfExpressionTypeVisitor creation and execution to VfParser#parse instead.

ParsingOptionsTest located in pmd-test wasn't running previously because it was in the src/main hierarchy. Moved this test into the src/test hierarchy and consolidated the methods from the
similarly named class from pmd-core.
This commit is contained in:
Jeff Bartolotta 2020-11-12 20:13:19 -08:00
parent 0348b2c0d2
commit e1c42a10ec
No known key found for this signature in database
GPG Key ID: 60F0A95A8B456DBC
9 changed files with 97 additions and 94 deletions

View File

@ -4,7 +4,7 @@
package net.sourceforge.pmd.lang.ecmascript;
import static net.sourceforge.pmd.lang.ParserOptionsTest.verifyOptionsEqualsHashcode;
import static net.sourceforge.pmd.lang.ParserOptionsTestUtils.verifyOptionsEqualsHashcode;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

View File

@ -5,38 +5,9 @@
package net.sourceforge.pmd.lang;
import org.junit.Assert;
import org.junit.Test;
/**
* Unit tests for {@link ParserOptions}.
*/
public class ParserOptionsTest {
/**
* SuppressMarker should be initially null and changeable.
*/
@Test
public void testSuppressMarker() {
ParserOptions parserOptions = new ParserOptions();
Assert.assertNull(parserOptions.getSuppressMarker());
parserOptions.setSuppressMarker("foo");
Assert.assertEquals("foo", parserOptions.getSuppressMarker());
}
/**
* Verify that the equals and hashCode methods work as expected.
*/
@Test
public void testEqualsHashcode() {
ParserOptions options1 = new ParserOptions();
options1.setSuppressMarker("foo");
ParserOptions options2 = new ParserOptions();
options2.setSuppressMarker("bar");
ParserOptions options3 = new ParserOptions();
options3.setSuppressMarker("foo");
ParserOptions options4 = new ParserOptions();
options4.setSuppressMarker("bar");
verifyOptionsEqualsHashcode(options1, options2, options3, options4);
public final class ParserOptionsTestUtils {
private ParserOptionsTestUtils() {
}
/**

View File

@ -6,20 +6,24 @@ package net.sourceforge.pmd.lang;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
import net.sourceforge.pmd.test.lang.DummyLanguageModule;
/**
* Unit tests for {@link ParserOptions}.
* This class is located in the pmd-test project instead of pmd-core so that it can invoke
* {@link ParserOptionsTestUtils#verifyOptionsEqualsHashcode}
*/
public class ParserOptionsTest {
private static final List<String> DEFAULT_LIST = Arrays.asList("value1", "value2");
private static final String DEFAULT_STRING = "value3";
@ -48,6 +52,17 @@ public class ParserOptionsTest {
}
}
/**
* SuppressMarker should be initially null and changeable.
*/
@Test
public void testSuppressMarker() {
ParserOptions parserOptions = new ParserOptions();
Assert.assertNull(parserOptions.getSuppressMarker());
parserOptions.setSuppressMarker("foo");
Assert.assertEquals("foo", parserOptions.getSuppressMarker());
}
@Test
public void testDefaultPropertyDescriptors() {
TestParserOptions parserOptions = new TestParserOptions();
@ -104,37 +119,81 @@ public class ParserOptionsTest {
assertEquals("", vfParserOptions.getProperty(TestParserOptions.STRING_DESCRIPTOR));
}
/**
* Verify that the equals and hashCode methods work as expected.
* TODO: Consider using Guava's EqualsTester
*/
@Test
public void testEqualsAndHashCode() {
ParserOptions parserOptions = new ParserOptions();
TestParserOptions testParserOptions1 = new TestParserOptions();
TestParserOptions testParserOptions2 = new TestParserOptions();
public void testSuppressMarkerEqualsHashCode() {
ParserOptions options1;
ParserOptions options2;
ParserOptions options3;
ParserOptions options4;
// Differences based on Language
assertNotNull(parserOptions.hashCode());
assertFalse(parserOptions.equals(testParserOptions1));
assertNotEquals(parserOptions.hashCode(), testParserOptions1.hashCode());
// SuppressMarker
options1 = new ParserOptions();
options2 = new ParserOptions();
options3 = new ParserOptions();
options4 = new ParserOptions();
options1.setSuppressMarker("foo");
options2.setSuppressMarker("bar");
options3.setSuppressMarker("foo");
options4.setSuppressMarker("bar");
ParserOptionsTestUtils.verifyOptionsEqualsHashcode(options1, options2, options3, options4);
// Differences based on Properties
assertNotNull(testParserOptions1.hashCode());
assertTrue(testParserOptions1.equals(testParserOptions2));
assertEquals(testParserOptions1.hashCode(), testParserOptions2.hashCode());
// PropertyDescriptor
options1 = new ParserOptions();
options2 = new ParserOptions();
options3 = new ParserOptions();
options4 = new ParserOptions();
options1.definePropertyDescriptor(TestParserOptions.LIST_DESCRIPTOR);
options2.definePropertyDescriptor(TestParserOptions.STRING_DESCRIPTOR);
options3.definePropertyDescriptor(TestParserOptions.LIST_DESCRIPTOR);
options4.definePropertyDescriptor(TestParserOptions.STRING_DESCRIPTOR);
ParserOptionsTestUtils.verifyOptionsEqualsHashcode(options1, options2, options3, options4);
testParserOptions1.setProperty(TestParserOptions.LIST_DESCRIPTOR, OVERRIDDEN_LIST);
assertFalse(testParserOptions1.equals(testParserOptions2));
assertNotEquals(testParserOptions1.hashCode(), testParserOptions2.hashCode());
// PropertyValue
options1 = new ParserOptions();
options2 = new ParserOptions();
options3 = new ParserOptions();
options4 = new ParserOptions();
options1.definePropertyDescriptor(TestParserOptions.STRING_DESCRIPTOR);
options1.setProperty(TestParserOptions.STRING_DESCRIPTOR, DEFAULT_STRING);
options2.definePropertyDescriptor(TestParserOptions.STRING_DESCRIPTOR);
options2.setProperty(TestParserOptions.STRING_DESCRIPTOR, OVERRIDDEN_STRING);
options3.definePropertyDescriptor(TestParserOptions.STRING_DESCRIPTOR);
options3.setProperty(TestParserOptions.STRING_DESCRIPTOR, DEFAULT_STRING);
options4.definePropertyDescriptor(TestParserOptions.STRING_DESCRIPTOR);
options4.setProperty(TestParserOptions.STRING_DESCRIPTOR, OVERRIDDEN_STRING);
ParserOptionsTestUtils.verifyOptionsEqualsHashcode(options1, options2, options3, options4);
testParserOptions1.setProperty(TestParserOptions.STRING_DESCRIPTOR, OVERRIDDEN_STRING);
assertFalse(testParserOptions1.equals(testParserOptions2));
assertNotEquals(testParserOptions1.hashCode(), testParserOptions2.hashCode());
// Language
options1 = new ParserOptions(new DummyLanguageModule());
options2 = new ParserOptions();
options3 = new ParserOptions(new DummyLanguageModule());
options4 = new ParserOptions();
ParserOptionsTestUtils.verifyOptionsEqualsHashcode(options1, options2, options3, options4);
testParserOptions2.setProperty(TestParserOptions.LIST_DESCRIPTOR, OVERRIDDEN_LIST);
assertFalse(testParserOptions1.equals(testParserOptions2));
assertNotEquals(testParserOptions1.hashCode(), testParserOptions2.hashCode());
// SuppressMarker, PropertyDescriptor, PropertyValue, Language
options1 = new ParserOptions(new DummyLanguageModule());
options2 = new ParserOptions();
options3 = new ParserOptions(new DummyLanguageModule());
options4 = new ParserOptions();
options1.setSuppressMarker("foo");
options2.setSuppressMarker("bar");
options3.setSuppressMarker("foo");
options4.setSuppressMarker("bar");
options1.definePropertyDescriptor(TestParserOptions.LIST_DESCRIPTOR);
options1.setProperty(TestParserOptions.LIST_DESCRIPTOR, DEFAULT_LIST);
options2.definePropertyDescriptor(TestParserOptions.STRING_DESCRIPTOR);
options2.setProperty(TestParserOptions.STRING_DESCRIPTOR, OVERRIDDEN_STRING);
options3.definePropertyDescriptor(TestParserOptions.LIST_DESCRIPTOR);
options3.setProperty(TestParserOptions.LIST_DESCRIPTOR, DEFAULT_LIST);
options4.definePropertyDescriptor(TestParserOptions.STRING_DESCRIPTOR);
options4.setProperty(TestParserOptions.STRING_DESCRIPTOR, OVERRIDDEN_STRING);
ParserOptionsTestUtils.verifyOptionsEqualsHashcode(options1, options2, options3, options4);
testParserOptions2.setProperty(TestParserOptions.STRING_DESCRIPTOR, OVERRIDDEN_STRING);
assertTrue(testParserOptions1.equals(testParserOptions2));
assertEquals(testParserOptions1.hashCode(), testParserOptions2.hashCode());
assertFalse(options1.equals(null));
}
@Test

View File

@ -86,6 +86,10 @@ public class VfExpressionTypeVisitor extends VfParserVisitorAdapter {
@Override
public Object visit(ASTCompilationUnit node, Object data) {
if (apexDirectories.isEmpty() && objectsDirectories.isEmpty()) {
// Skip visiting if there aren't any directories to look in
return data;
}
this.fileName = AbstractTokenManager.getFileName();
return super.visit(node, data);
}

View File

@ -12,7 +12,6 @@ import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.VisitorStarter;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.lang.vf.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.vf.ast.DumpFacade;
import net.sourceforge.pmd.lang.vf.ast.VfNode;
import net.sourceforge.pmd.lang.vf.rule.VfRuleViolationFactory;
@ -29,18 +28,6 @@ public class VfHandler extends AbstractLanguageVersionHandler {
return new VfParser(parserOptions);
}
@Override
public VisitorStarter getTypeResolutionFacade(ClassLoader classLoader) {
return new VisitorStarter() {
@Override
public void start(Node rootNode) {
ASTCompilationUnit astCompilationUnit = (ASTCompilationUnit) rootNode;
VfExpressionTypeVisitor visitor = new VfExpressionTypeVisitor(astCompilationUnit.getPropertySource());
visitor.visit((ASTCompilationUnit) rootNode, null);
}
};
}
@Override
public ParserOptions getDefaultParserOptions() {
return new VfParserOptions();

View File

@ -46,7 +46,9 @@ public class VfParser extends AbstractParser {
AbstractTokenManager.setFileName(fileName);
ASTCompilationUnit astCompilationUnit = new net.sourceforge.pmd.lang.vf.ast.VfParser(
new SimpleCharStream(source)).CompilationUnit();
astCompilationUnit.setPropertySource(this.getParserOptions());
// Add type information to the AST
VfExpressionTypeVisitor visitor = new VfExpressionTypeVisitor(this.getParserOptions());
visitor.visit(astCompilationUnit, null);
return astCompilationUnit;
}

View File

@ -6,16 +6,8 @@ package net.sourceforge.pmd.lang.vf.ast;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.lang.ast.RootNode;
import net.sourceforge.pmd.properties.PropertySource;
public class ASTCompilationUnit extends AbstractVFNode implements RootNode {
/**
* Holds the properties contained in {@code VfParserOptions}. This class acts as an intermediary since it is
* accessible by {@code VFParser} and {@code VFHandler}. {@code VfHandler} uses this value to initialize the
* {@code VfExpressionTypeVisitor} for type resolution.
*/
private PropertySource propertySource;
@Deprecated
@InternalApi
public ASTCompilationUnit(int id) {
@ -32,12 +24,4 @@ public class ASTCompilationUnit extends AbstractVFNode implements RootNode {
public Object jjtAccept(VfParserVisitor visitor, Object data) {
return visitor.visit(this, data);
}
public PropertySource getPropertySource() {
return propertySource;
}
public void setPropertySource(PropertySource propertySource) {
this.propertySource = propertySource;
}
}

View File

@ -51,10 +51,6 @@ public class VfUnescapeElRule extends AbstractVfRule {
private static final Pattern ON_EVENT = Pattern.compile("^on(\\w)+$");
private static final Pattern PLACEHOLDERS = Pattern.compile("\\{(\\w|,|\\.|'|:|\\s)*\\}");
public VfUnescapeElRule() {
this.setTypeResolution(true);
}
@Override
public Object visit(ASTHtmlScript node, Object data) {
checkIfCorrectlyEscaped(node, data);

View File

@ -4,7 +4,7 @@
package net.sourceforge.pmd.lang.xml;
import static net.sourceforge.pmd.lang.ParserOptionsTest.verifyOptionsEqualsHashcode;
import static net.sourceforge.pmd.lang.ParserOptionsTestUtils.verifyOptionsEqualsHashcode;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;