From c3aa845a6addf74ca459a77cc4ac7e8ff034009a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Mon, 14 Feb 2022 00:02:59 +0100 Subject: [PATCH] Fix js & xml tests --- .../java/net/sourceforge/pmd/cli/CLITest.java | 7 +-- .../java/net/sourceforge/pmd/cli/CLITest.java | 11 ++--- .../net/sourceforge/pmd/cli/BaseCLITest.java | 26 +++++++++++ .../sourceforge/pmd/lang/xml/XmlCliTest.java | 46 ++++++++----------- 4 files changed, 49 insertions(+), 41 deletions(-) diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/cli/CLITest.java b/pmd-java/src/test/java/net/sourceforge/pmd/cli/CLITest.java index 1197569d6f..601542675a 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/cli/CLITest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/cli/CLITest.java @@ -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 diff --git a/pmd-javascript/src/test/java/net/sourceforge/pmd/cli/CLITest.java b/pmd-javascript/src/test/java/net/sourceforge/pmd/cli/CLITest.java index 05606c0b9c..3b83aaa055 100644 --- a/pmd-javascript/src/test/java/net/sourceforge/pmd/cli/CLITest.java +++ b/pmd-javascript/src/test/java/net/sourceforge/pmd/cli/CLITest.java @@ -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\\)")); } } diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCLITest.java b/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCLITest.java index 710b07bb6c..e6544c5d9c 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCLITest.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCLITest.java @@ -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 containsPattern(final String regex) { + return new BaseMatcher() { + 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(); + } + }; + } } diff --git a/pmd-xml/src/test/java/net/sourceforge/pmd/lang/xml/XmlCliTest.java b/pmd-xml/src/test/java/net/sourceforge/pmd/lang/xml/XmlCliTest.java index 80382ddd51..2c9af24104 100644 --- a/pmd-xml/src/test/java/net/sourceforge/pmd/lang/xml/XmlCliTest.java +++ b/pmd-xml/src/test/java/net/sourceforge/pmd/lang/xml/XmlCliTest.java @@ -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 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 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)); } }