Fix merge

This commit is contained in:
Clément Fournier 2022-07-16 17:35:52 +02:00
parent 4eb1e40a8a
commit a56ba97e29
No known key found for this signature in database
GPG Key ID: 4D8D42402E4F47E2
11 changed files with 84 additions and 86 deletions

View File

@ -73,7 +73,7 @@ public class AbstractRuleTest {
DummyRootNode s = DummyLanguageModule.parse("abc()", "filename");
RuleViolation rv = new ParametricRuleViolation(r, s, r.getMessage());
assertEquals(5, rv.getBeginLine(), "Line number mismatch!");
assertEquals(1, rv.getBeginLine(), "Line number mismatch!");
assertEquals("filename", rv.getFilename(), "Filename mismatch!");
assertEquals(r, rv.getRule(), "Rule object mismatch!");
assertEquals("my rule msg", rv.getDescription(), "Rule msg mismatch!");
@ -85,14 +85,14 @@ public class AbstractRuleTest {
MyRule r = new MyRule();
DummyRootNode s = DummyLanguageModule.parse("abc()", "filename");
RuleViolation rv = new ParametricRuleViolation(r, s, "specificdescription");
assertEquals(5, rv.getBeginLine(), "Line number mismatch!");
assertEquals(1, rv.getBeginLine(), "Line number mismatch!");
assertEquals("filename", rv.getFilename(), "Filename mismatch!");
assertEquals(r, rv.getRule(), "Rule object mismatch!");
assertEquals("specificdescription", rv.getDescription(), "Rule description mismatch!");
}
@Test
void testRuleWithVariableInMessage() throws Exception {
void testRuleWithVariableInMessage() {
MyRule r = new MyRule() {
@Override
public void apply(Node target, RuleContext ctx) {

View File

@ -31,7 +31,7 @@ public class ReportTest {
// Files are grouped together now.
@Test
void testSortedReportFile() {
void testSortedReportFile() {
Renderer rend = new XMLRenderer();
String result = render(rend, r -> {
FileLocation s = getNode(10, 5, "foo");

View File

@ -9,7 +9,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Comparator;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import net.sourceforge.pmd.lang.DummyLanguageModule;
@ -29,7 +28,7 @@ class RuleViolationTest {
DummyRootNode s = DummyLanguageModule.parse("abcd", "filename");
RuleViolation r = new ParametricRuleViolation(rule, s, rule.getMessage());
assertEquals(rule, r.getRule(), "object mismatch");
assertEquals(2, r.getBeginLine(), "line number is wrong");
assertEquals(1, r.getBeginLine(), "line number is wrong");
assertEquals("filename", r.getFilename(), "filename is wrong");
}
@ -39,7 +38,7 @@ class RuleViolationTest {
DummyRootNode s = DummyLanguageModule.parse("abcd", "filename");
RuleViolation r = new ParametricRuleViolation(rule, s, "description");
assertEquals(rule, r.getRule(), "object mismatch");
assertEquals(2, r.getBeginLine(), "line number is wrong");
assertEquals(1, r.getBeginLine(), "line number is wrong");
assertEquals("filename", r.getFilename(), "filename is wrong");
assertEquals("description", r.getDescription(), "description is wrong");
}

View File

@ -5,13 +5,13 @@
package net.sourceforge.pmd.lang.document;
import static net.sourceforge.pmd.util.CollectionUtil.listOf;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.io.Reader;
@ -19,7 +19,7 @@ import java.io.StringWriter;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import net.sourceforge.pmd.util.CollectionUtil;
@ -415,7 +415,7 @@ public class CharsTest {
// ------------
try (Reader reader = bc.newReader()) {
assertTrue("markSupported", reader.markSupported());
assertTrue(reader.markSupported(), "markSupported");
assertEquals('b', reader.read());
assertEquals('c', reader.read());
@ -445,7 +445,7 @@ public class CharsTest {
// ------------
try (Reader reader = bc.newReader()) {
assertTrue("markSupported", reader.markSupported());
assertTrue(reader.markSupported(), "markSupported");
assertEquals('b', reader.read());
assertThrows(IOException.class, reader::reset);
@ -483,7 +483,7 @@ public class CharsTest {
char[] cbuf = new char[4];
try (Reader reader = bc.newReader()) {
assertTrue("markSupported", reader.markSupported());
assertTrue(reader.markSupported(), "markSupported");
assertEquals('b', reader.read());
assertThrows(NullPointerException.class, () -> reader.read(null, 0, 0));

View File

@ -5,10 +5,10 @@
package net.sourceforge.pmd.lang.document;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* @author Clément Fournier

View File

@ -4,10 +4,10 @@
package net.sourceforge.pmd.lang.document;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* Unit test for {@link SourceCodePositioner}.

View File

@ -8,7 +8,6 @@ import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.contains;
import static org.mockito.Mockito.eq;
@ -49,6 +48,8 @@ import net.sourceforge.pmd.lang.rule.AbstractRule;
import net.sourceforge.pmd.processor.MonoThreadProcessor.MonothreadRunnable;
import net.sourceforge.pmd.util.log.MessageReporter;
import com.github.stefanbirkner.systemlambda.SystemLambda;
public class PmdRunnableTest {
public static final String TEST_MESSAGE_SEMANTIC_ERROR = "An error occurred!";
@ -91,37 +92,44 @@ public class PmdRunnableTest {
}
@Test
public void inErrorRecoveryModeErrorsShouldBeLoggedByParser() {
System.setProperty(SystemProps.PMD_ERROR_RECOVERY, "");
public void inErrorRecoveryModeErrorsShouldBeLoggedByParser() throws Exception {
SystemLambda.restoreSystemProperties(() -> {
System.setProperty(SystemProps.PMD_ERROR_RECOVERY, "");
Report report = process(versionWithParserThatThrowsAssertionError());
Report report = process(versionWithParserThatThrowsAssertionError());
assertEquals(1, report.getProcessingErrors().size());
assertEquals(1, report.getProcessingErrors().size());
});
}
@Test
public void inErrorRecoveryModeErrorsShouldBeLoggedByRule() {
System.setProperty(SystemProps.PMD_ERROR_RECOVERY, "");
public void inErrorRecoveryModeErrorsShouldBeLoggedByRule() throws Exception {
SystemLambda.restoreSystemProperties(() -> {
System.setProperty(SystemProps.PMD_ERROR_RECOVERY, "");
Report report = process(DummyLanguageModule.getInstance().getDefaultVersion());
Report report = process(DummyLanguageModule.getInstance().getDefaultVersion());
List<ProcessingError> errors = report.getProcessingErrors();
assertThat(errors, hasSize(1));
assertThat(errors.get(0).getError(), instanceOf(ContextedAssertionError.class));
});
List<ProcessingError> errors = report.getProcessingErrors();
assertThat(errors, hasSize(1));
assertThat(errors.get(0).getError(), instanceOf(ContextedAssertionError.class));
}
@Test
public void withoutErrorRecoveryModeProcessingShouldBeAbortedByParser() {
assertNull(System.getProperty(SystemProps.PMD_ERROR_RECOVERY));
assertThrows(AssertionError.class, () -> process(versionWithParserThatThrowsAssertionError()));
public void withoutErrorRecoveryModeProcessingShouldBeAbortedByParser() throws Exception {
SystemLambda.restoreSystemProperties(() -> {
System.clearProperty(SystemProps.PMD_ERROR_RECOVERY);
assertThrows(AssertionError.class, () -> process(versionWithParserThatThrowsAssertionError()));
});
}
@Test
public void withoutErrorRecoveryModeProcessingShouldBeAbortedByRule() {
assertNull(System.getProperty(SystemProps.PMD_ERROR_RECOVERY));
assertThrows(AssertionError.class, () -> process(DummyLanguageModule.getInstance().getDefaultVersion()));
public void withoutErrorRecoveryModeProcessingShouldBeAbortedByRule() throws Exception {
SystemLambda.restoreSystemProperties(() -> {
System.clearProperty(SystemProps.PMD_ERROR_RECOVERY);
assertThrows(AssertionError.class, () -> process(DummyLanguageModule.getInstance().getDefaultVersion()));
});
}

View File

@ -5,10 +5,9 @@
package net.sourceforge.pmd.lang.gherkin.cpd;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Lexer;
import net.sourceforge.pmd.cpd.SourceCode;
import net.sourceforge.pmd.cpd.internal.AntlrTokenizer;
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrTokenManager;
import net.sourceforge.pmd.lang.gherkin.ast.GherkinLexer;
/**
@ -17,8 +16,7 @@ import net.sourceforge.pmd.lang.gherkin.ast.GherkinLexer;
public class GherkinTokenizer extends AntlrTokenizer {
@Override
protected AntlrTokenManager getLexerForSource(SourceCode sourceCode) {
CharStream charStream = AntlrTokenizer.getCharStreamFromSourceCode(sourceCode);
return new AntlrTokenManager(new GherkinLexer(charStream), sourceCode.getFileName());
protected Lexer getLexerForSource(CharStream charStream) {
return new GherkinLexer(charStream);
}
}

View File

@ -4,19 +4,15 @@
package net.sourceforge.pmd.lang.html;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.Parser;
import net.sourceforge.pmd.lang.ast.Parser.ParserTask;
import net.sourceforge.pmd.lang.ast.SemanticErrorReporter;
import net.sourceforge.pmd.lang.html.ast.ASTHtmlComment;
import net.sourceforge.pmd.lang.html.ast.ASTHtmlDocument;
import net.sourceforge.pmd.lang.html.ast.ASTHtmlTextNode;
@ -48,29 +44,29 @@ public class HtmlXPathRuleTest {
String xpath = "//text()[contains(., '{ ')]";
List<RuleViolation> violations = runXPath(LIGHTNING_WEB_COMPONENT, xpath);
Assert.assertEquals(1, violations.size());
Assert.assertEquals(3, violations.get(0).getBeginLine());
assertEquals(1, violations.size());
assertEquals(3, violations.get(0).getBeginLine());
}
@Test
public void selectTextNodeByNodeNameShouldNotWork() {
String xpath = "//*[local-name() = '#text']";
List<RuleViolation> violations = runXPath(LIGHTNING_WEB_COMPONENT, xpath);
Assert.assertEquals(0, violations.size());
assertEquals(0, violations.size());
}
@Test
public void verifyTextNodeName() {
ASTHtmlDocument document = HtmlParsingHelper.DEFAULT.parse("<p>foobar</p>");
ASTHtmlTextNode textNode = document.getFirstDescendantOfType(ASTHtmlTextNode.class);
Assert.assertEquals("#text", textNode.getXPathNodeName());
assertEquals("#text", textNode.getXPathNodeName());
}
@Test
public void verifyCommentNodeName() {
ASTHtmlDocument document = HtmlParsingHelper.DEFAULT.parse("<p><!-- a comment --></p>");
ASTHtmlComment comment = document.getFirstDescendantOfType(ASTHtmlComment.class);
Assert.assertEquals("#comment", comment.getXPathNodeName());
assertEquals("#comment", comment.getXPathNodeName());
}
@Test
@ -80,8 +76,8 @@ public class HtmlXPathRuleTest {
String xpath = "//*[@value = '{']";
List<RuleViolation> violations = runXPath(LIGHTNING_WEB_COMPONENT, xpath);
Assert.assertEquals(1, violations.size());
Assert.assertEquals(4, violations.get(0).getBeginLine());
assertEquals(1, violations.size());
assertEquals(4, violations.get(0).getBeginLine());
}
@Test
@ -91,9 +87,9 @@ public class HtmlXPathRuleTest {
String xpath = "//*[@*[local-name() = ('value', 'onchange')] = '{']";
List<RuleViolation> violations = runXPath(LIGHTNING_WEB_COMPONENT, xpath);
Assert.assertEquals(2, violations.size());
Assert.assertEquals(4, violations.get(0).getBeginLine());
Assert.assertEquals(6, violations.get(1).getBeginLine());
assertEquals(2, violations.size());
assertEquals(4, violations.get(0).getBeginLine());
assertEquals(6, violations.get(1).getBeginLine());
}
@Test
@ -101,8 +97,8 @@ public class HtmlXPathRuleTest {
String xpath = "//*[@*[local-name() = 'if:true']]";
List<RuleViolation> violations = runXPath(LIGHTNING_WEB_COMPONENT, xpath);
Assert.assertEquals(1, violations.size());
Assert.assertEquals(10, violations.get(0).getBeginLine());
assertEquals(1, violations.size());
assertEquals(10, violations.get(0).getBeginLine());
}
private List<RuleViolation> runXPath(String html, String xpath) {

View File

@ -5,12 +5,11 @@
package net.sourceforge.pmd.cpd;
import static net.sourceforge.pmd.cli.BaseCLITest.containsPattern;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.regex.Pattern;
import org.junit.Assert;
import org.junit.Test;
import net.sourceforge.pmd.cli.BaseCPDCLITest;
@ -27,40 +26,40 @@ public class CPDCommandLineInterfaceTest extends BaseCPDCLITest {
public void testIgnoreIdentifiers() {
String out = runTest(CPD.StatusCode.DUPLICATE_CODE_FOUND, "--minimum-tokens", "34", "--language", "java", "--files",
"src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--ignore-identifiers");
Assert.assertTrue(out.contains("Found a 7 line (36 tokens) duplication"));
assertThat(out, containsString("Found a 7 line (36 tokens) duplication"));
}
/**
* Test ignore identifiers argument with failOnViolation=false
*/
@Test
public void testIgnoreIdentifiersFailOnViolationFalse() throws Exception {
public void testIgnoreIdentifiersFailOnViolationFalse() {
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "34", "--language", "java", "--files",
"src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--ignore-identifiers", "--failOnViolation",
"false");
Assert.assertTrue(out.contains("Found a 7 line (36 tokens) duplication"));
assertThat(out, containsString("Found a 7 line (36 tokens) duplication"));
}
/**
* Test ignore identifiers argument with failOnViolation=false with changed long options
*/
@Test
public void testIgnoreIdentifiersFailOnViolationFalseLongOption() throws Exception {
public void testIgnoreIdentifiersFailOnViolationFalseLongOption() {
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "34", "--language", "java", "--files",
"src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--ignore-identifiers", "--fail-on-violation",
"false");
Assert.assertTrue(out.contains("Found a 7 line (36 tokens) duplication"));
assertThat(out, containsString("Found a 7 line (36 tokens) duplication"));
}
/**
* Test excludes option.
*/
@Test
public void testExcludes() throws Exception {
public void testExcludes() {
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "34", "--language", "java", "--ignore-identifiers", "--files",
"src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--exclude",
"src/test/resources/net/sourceforge/pmd/cpd/clitest/File2.java");
Assert.assertFalse(out.contains("Found a 7 line (34 tokens) duplication"));
assertThat(out, not(containsString("Found a 7 line (34 tokens) duplication")));
}
/**
@ -89,25 +88,24 @@ public class CPDCommandLineInterfaceTest extends BaseCPDCLITest {
*
*/
@Test
public void testBrokenAndValidFile() throws IOException {
public void testBrokenAndValidFile() {
String out = runTest(CPD.StatusCode.DUPLICATE_CODE_FOUND, "--minimum-tokens", "10", "--language", "java", "--files",
"src/test/resources/net/sourceforge/pmd/cpd/badandgood/", "--format", "text", "--skip-lexical-errors");
Assert.assertTrue(
Pattern.compile("Skipping .*?BadFile\\.java\\. Reason: Lexical error in file").matcher(out).find());
Assert.assertTrue(out.contains("Found a 5 line (13 tokens) duplication"));
assertThat(out, containsPattern("Skipping .*?BadFile\\.java\\. Reason: Lexical error in file"));
assertThat(out, containsString("Found a 5 line (13 tokens) duplication"));
}
@Test
public void testFormatXmlWithoutEncoding() throws Exception {
public void testFormatXmlWithoutEncoding() {
String out = runTest(CPD.StatusCode.DUPLICATE_CODE_FOUND, "--minimum-tokens", "10", "--language", "java", "--files",
"src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--format", "xml");
Assert.assertTrue(out.contains("<duplication lines=\"3\" tokens=\"10\">"));
assertThat(out, containsString("<duplication lines=\"3\" tokens=\"10\">"));
}
@Test
public void testCSVFormat() throws Exception {
public void testCSVFormat() {
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "100", "--files", "src/test/resources/net/sourceforge/pmd/cpd/badandgood/",
"--language", "c", "--format", "csv");
Assert.assertFalse(out.contains("Couldn't instantiate renderer"));
assertThat(out, not(containsString("Couldn't instantiate renderer")));
}
}

View File

@ -25,7 +25,6 @@ import net.sourceforge.pmd.lang.ast.SemanticErrorReporter;
import net.sourceforge.pmd.lang.document.TextDocument;
import net.sourceforge.pmd.lang.document.TextFile;
import net.sourceforge.pmd.lang.vf.DataType;
import net.sourceforge.pmd.util.IOUtil;
import apex.jorje.semantic.symbol.type.BasicType;