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.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString; 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; 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", String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-version", "1.5", "-language",
"java", "--debug", }; "java", "--debug", };
String log = runTest(args); String log = runTest(args);
Matcher matcher = Pattern.compile("Adding file .*\\.java \\(lang: java 1\\.5\\)").matcher(log); assertThat(log, containsPattern("Adding file .*\\.java \\(lang: java 1\\.5\\)"));
assertTrue(matcher.find());
} }
@Test @Test

View File

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

View File

@ -13,8 +13,12 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils; 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.After;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -92,10 +96,16 @@ public abstract class BaseCLITest {
return filename; return filename;
} }
/**
* Returns the log output.
*/
protected String runTest(String... args) { protected String runTest(String... args) {
return runTest(0, args); return runTest(0, args);
} }
/**
* Returns the log output.
*/
protected String runTest(int expectedExitCode, String... args) { protected String runTest(int expectedExitCode, String... args) {
ByteArrayOutputStream console = new ByteArrayOutputStream(); ByteArrayOutputStream console = new ByteArrayOutputStream();
PrintStream out = new PrintStream(console); PrintStream out = new PrintStream(console);
@ -124,4 +134,20 @@ public abstract class BaseCLITest {
protected int getStatusCode() { protected int getStatusCode() {
return Integer.parseInt(System.getProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY)); 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; package net.sourceforge.pmd.lang.xml;
import java.io.File; import static net.sourceforge.pmd.util.CollectionUtil.listOf;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; 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 static final String RULE_MESSAGE = "A tags are not allowed";
private String[] createArgs(String directory, String... args) { private String[] createArgs(String directory, String... args) {
List<String> arguments = new ArrayList<>(); List<String> arguments = new ArrayList<>(listOf(
arguments.add("-f"); "-f",
arguments.add("text"); "text",
arguments.add("-no-cache"); "-no-cache",
arguments.add("-R"); "-R",
arguments.add(BASE_DIR + "/ruleset.xml"); BASE_DIR + "/ruleset.xml",
arguments.add("-d"); "-d",
arguments.add(BASE_DIR + directory); BASE_DIR + directory
));
arguments.addAll(Arrays.asList(args)); arguments.addAll(Arrays.asList(args));
return arguments.toArray(new String[0]); return arguments.toArray(new String[0]);
} }
@Test @Test
public void analyzeSingleXmlWithoutForceLanguage() { public void analyzeSingleXmlWithoutForceLanguage() {
String resultFilename = runTest(createArgs("/src/file1.ext"), 0); String log = runTest(0, createArgs("/src/file1.ext"));
assertRuleMessage(0, resultFilename); assertRuleMessage(0, log);
} }
@Test @Test
public void analyzeSingleXmlWithForceLanguage() { public void analyzeSingleXmlWithForceLanguage() {
String resultFilename = runTest(createArgs("/src/file1.ext", "-force-language", "xml"), String log = runTest(4, createArgs("/src/file1.ext", "-force-language", "xml"));
4); assertRuleMessage(1, log);
assertRuleMessage(1, resultFilename);
} }
@Test @Test
public void analyzeDirectoryWithForceLanguage() { public void analyzeDirectoryWithForceLanguage() {
String resultFilename = runTest(createArgs("/src/", "-force-language", "xml"), String log = runTest(4, createArgs("/src/", "-force-language", "xml"));
4); assertRuleMessage(3, log);
assertRuleMessage(3, resultFilename);
} }
private void assertRuleMessage(int expectedCount, String resultFilename) { private void assertRuleMessage(int expectedCount, String log) {
try { Assert.assertEquals(expectedCount, StringUtils.countMatches(log, RULE_MESSAGE));
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);
}
} }
} }