Fix js & xml tests

This commit is contained in:
Clément Fournier
2022-02-14 00:02:59 +01:00
parent cc834b175e
commit c3aa845a6a
4 changed files with 49 additions and 41 deletions

View File

@ -6,10 +6,6 @@ package net.sourceforge.pmd.cli;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertTrue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
@ -44,8 +40,7 @@ public class CLITest extends BaseCLITest {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-version", "1.5", "-language",
"java", "--debug", };
String log = runTest(args);
Matcher matcher = Pattern.compile("Adding file .*\\.java \\(lang: java 1\\.5\\)").matcher(log);
assertTrue(matcher.find());
assertThat(log, containsPattern("Adding file .*\\.java \\(lang: java 1\\.5\\)"));
}
@Test

View File

@ -4,14 +4,10 @@
package net.sourceforge.pmd.cli;
import static org.junit.Assert.assertTrue;
import java.io.File;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
import net.sourceforge.pmd.util.FileUtil;
/**
* @author Romain Pelisse <belaran@gmail.com>
*
@ -21,8 +17,7 @@ public class CLITest extends BaseCLITest {
public void useEcmaScript() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "xml", "-R", "ecmascript-basic", "-version", "3", "-l",
"ecmascript", "-debug", };
String resultFilename = runTest(args);
assertTrue("Invalid JavaScript version",
FileUtil.findPatternInFile(new File(resultFilename), "Using Ecmascript version: Ecmascript 3"));
String log = runTest(args);
assertThat(log, containsPattern("Adding file .*\\.js \\(lang: ecmascript 3\\)"));
}
}

View File

@ -13,8 +13,12 @@ import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
@ -92,10 +96,16 @@ public abstract class BaseCLITest {
return filename;
}
/**
* Returns the log output.
*/
protected String runTest(String... args) {
return runTest(0, args);
}
/**
* Returns the log output.
*/
protected String runTest(int expectedExitCode, String... args) {
ByteArrayOutputStream console = new ByteArrayOutputStream();
PrintStream out = new PrintStream(console);
@ -124,4 +134,20 @@ public abstract class BaseCLITest {
protected int getStatusCode() {
return Integer.parseInt(System.getProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY));
}
public static Matcher<String> containsPattern(final String regex) {
return new BaseMatcher<String>() {
final Pattern pattern = Pattern.compile(regex);
@Override
public void describeTo(Description description) {
description.appendText("a string containing the pattern '" + this.pattern + "'");
}
@Override
public boolean matches(Object o) {
return o instanceof String && pattern.matcher((String) o).find();
}
};
}
}

View File

@ -4,14 +4,12 @@
package net.sourceforge.pmd.lang.xml;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import static net.sourceforge.pmd.util.CollectionUtil.listOf;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;
@ -23,44 +21,38 @@ public class XmlCliTest extends BaseCLITest {
private static final String RULE_MESSAGE = "A tags are not allowed";
private String[] createArgs(String directory, String... args) {
List<String> arguments = new ArrayList<>();
arguments.add("-f");
arguments.add("text");
arguments.add("-no-cache");
arguments.add("-R");
arguments.add(BASE_DIR + "/ruleset.xml");
arguments.add("-d");
arguments.add(BASE_DIR + directory);
List<String> arguments = new ArrayList<>(listOf(
"-f",
"text",
"-no-cache",
"-R",
BASE_DIR + "/ruleset.xml",
"-d",
BASE_DIR + directory
));
arguments.addAll(Arrays.asList(args));
return arguments.toArray(new String[0]);
}
@Test
public void analyzeSingleXmlWithoutForceLanguage() {
String resultFilename = runTest(createArgs("/src/file1.ext"), 0);
assertRuleMessage(0, resultFilename);
String log = runTest(0, createArgs("/src/file1.ext"));
assertRuleMessage(0, log);
}
@Test
public void analyzeSingleXmlWithForceLanguage() {
String resultFilename = runTest(createArgs("/src/file1.ext", "-force-language", "xml"),
4);
assertRuleMessage(1, resultFilename);
String log = runTest(4, createArgs("/src/file1.ext", "-force-language", "xml"));
assertRuleMessage(1, log);
}
@Test
public void analyzeDirectoryWithForceLanguage() {
String resultFilename = runTest(createArgs("/src/", "-force-language", "xml"),
4);
assertRuleMessage(3, resultFilename);
String log = runTest(4, createArgs("/src/", "-force-language", "xml"));
assertRuleMessage(3, log);
}
private void assertRuleMessage(int expectedCount, String resultFilename) {
try {
String result = FileUtils.readFileToString(new File(resultFilename), StandardCharsets.UTF_8);
Assert.assertEquals(expectedCount, StringUtils.countMatches(result, RULE_MESSAGE));
} catch (IOException e) {
throw new AssertionError(e);
}
private void assertRuleMessage(int expectedCount, String log) {
Assert.assertEquals(expectedCount, StringUtils.countMatches(log, RULE_MESSAGE));
}
}