Fix java cli tests

This commit is contained in:
Clément Fournier
2022-02-13 20:21:23 +01:00
parent 8613cbc5b2
commit cc834b175e
11 changed files with 94 additions and 73 deletions

View File

@ -4,16 +4,15 @@
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.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Assert;
import org.junit.Test;
import net.sourceforge.pmd.util.FileUtil;
/**
* @author Romain Pelisse <belaran@gmail.com>
*
@ -21,62 +20,58 @@ import net.sourceforge.pmd.util.FileUtil;
public class CLITest extends BaseCLITest {
@Test
public void minimalArgs() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/bestpractices.xml,category/java/design.xml", };
runTest(args, "minimalArgs");
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/bestpractices.xml,category/java/design.xml");
}
@Test
public void minimumPriority() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-min", "1", };
runTest(args, "minimumPriority");
runTest(args);
}
@Test
public void usingDebug() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-debug", };
runTest(args, "minimalArgsWithDebug");
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-debug");
}
@Test
public void usingDebugLongOption() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "--debug", };
runTest(args, "minimalArgsWithDebug");
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "--debug");
}
@Test
public void changeJavaVersion() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-version", "1.5", "-language",
"java", "-debug", };
String resultFilename = runTest(args, "chgJavaVersion");
assertTrue("Invalid Java version",
FileUtil.findPatternInFile(new File(resultFilename), "Using Java version: Java 1.5"));
"java", "--debug", };
String log = runTest(args);
Matcher matcher = Pattern.compile("Adding file .*\\.java \\(lang: java 1\\.5\\)").matcher(log);
assertTrue(matcher.find());
}
@Test
public void exitStatusNoViolations() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", };
runTest(args, "exitStatusNoViolations");
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml");
}
@Test
public void exitStatusWithViolations() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", };
String resultFilename = runTest(args, "exitStatusWithViolations", 4);
assertTrue(FileUtil.findPatternInFile(new File(resultFilename), "Avoid empty if"));
String log = runTest(4, args);
assertThat(log, containsString("Avoid empty if"));
}
@Test
public void exitStatusWithViolationsAndWithoutFailOnViolations() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", "-failOnViolation", "false", };
String resultFilename = runTest(args, "exitStatusWithViolationsAndWithoutFailOnViolations", 0);
assertTrue(FileUtil.findPatternInFile(new File(resultFilename), "Avoid empty if"));
String log = runTest(0, args);
assertThat(log, containsString("Avoid empty if"));
}
@Test
public void exitStatusWithViolationsAndWithoutFailOnViolationsLongOption() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", "--fail-on-violation", "false", };
String resultFilename = runTest(args, "exitStatusWithViolationsAndWithoutFailOnViolations", 0);
assertTrue(FileUtil.findPatternInFile(new File(resultFilename), "Avoid empty if"));
String log = runTest(0, args);
assertThat(log, containsString("Avoid empty if"));
}
/**
@ -85,12 +80,9 @@ public class CLITest extends BaseCLITest {
@Test
public void testWrongRuleset() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/designn.xml", };
String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
createTestOutputFile(filename);
runPMDWith(args);
Assert.assertEquals(1, getStatusCode());
assertTrue(FileUtil.findPatternInFile(new File(filename),
"Can't find resource 'category/java/designn.xml' for rule 'null'." + " Make sure the resource is a valid file"));
String log = runTest(1, args);
assertThat(log, containsString("Can't find resource 'category/java/designn.xml' for rule 'null'."
+ " Make sure the resource is a valid file"));
}
/**
@ -99,12 +91,9 @@ public class CLITest extends BaseCLITest {
@Test
public void testWrongRulesetWithRulename() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/designn.xml/UseCollectionIsEmpty", };
String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
createTestOutputFile(filename);
runPMDWith(args);
Assert.assertEquals(1, getStatusCode());
assertTrue(FileUtil.findPatternInFile(new File(filename),
"Can't find resource 'category/java/designn.xml' for rule " + "'UseCollectionIsEmpty'."));
String log = runTest(1, args);
assertThat(log, containsString("Can't find resource 'category/java/designn.xml' for rule "
+ "'UseCollectionIsEmpty'."));
}
/**
@ -113,11 +102,8 @@ public class CLITest extends BaseCLITest {
@Test
public void testWrongRulename() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml/ThisRuleDoesNotExist", };
String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
createTestOutputFile(filename);
runPMDWith(args);
Assert.assertEquals(1, getStatusCode());
assertTrue(FileUtil.findPatternInFile(new File(filename), Pattern
.quote("No rules found. Maybe you misspelled a rule name?" + " (category/java/design.xml/ThisRuleDoesNotExist)")));
String log = runTest(1, args);
assertThat(log, containsString("No rules found. Maybe you misspelled a rule name?"
+ " (category/java/design.xml/ThisRuleDoesNotExist)"));
}
}