Fix CLI Tests, move coverage test to java

This commit is contained in:
Andreas Dangel
2014-10-06 20:14:43 +02:00
parent d038fed36e
commit f120134b6d
7 changed files with 128 additions and 96 deletions

View File

@ -0,0 +1,89 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cli;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.regex.Pattern;
import net.sourceforge.pmd.util.FileUtil;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Romain Pelisse <belaran@gmail.com>
*
*/
public class CLITest extends BaseCLITest {
@Test
public void minimalArgs() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design" };
runTest(args, "minimalArgs");
}
@Test
public void minimumPriority() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design", "-min", "1"};
runTest(args,"minimumPriority");
}
@Test
public void usingDebug() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design", "-debug" };
runTest(args, "minimalArgsWithDebug");
}
@Test
public void changeJavaVersion() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design", "-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"));
}
/**
* See https://sourceforge.net/p/pmd/bugs/1231/
*/
@Test
public void testWrongRuleset() throws Exception {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-designn" };
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 'null' for rule 'java-designn'."
+ " Make sure the resource is a valid file"));
}
/**
* See https://sourceforge.net/p/pmd/bugs/1231/
*/
@Test
public void testWrongRulesetWithRulename() throws Exception {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-designn/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 'null' for rule "
+ "'java-designn/UseCollectionIsEmpty'."));
}
/**
* See https://sourceforge.net/p/pmd/bugs/1231/
*/
@Test
public void testWrongRulename() throws Exception {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-design/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 mispelled a rule name?"
+ " (java-design/ThisRuleDoesNotExist)")));
}
}

View File

@ -0,0 +1,35 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cli;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import junit.framework.Assert;
import org.junit.Test;
public class XPathCLITest {
@Test
public void runXPath() throws Exception {
PrintStream oldOut = System.out;
ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));
try {
XPathCLI.main(new String[] {
"-xpath",
"//ClassOrInterfaceDeclaration",
"-filename",
"src/test/java/net/sourceforge/pmd/cli/XPathCLITest.java"
});
System.out.flush();
} finally {
System.setOut(oldOut);
}
Assert.assertTrue(output.toString("UTF-8").startsWith("Match at line "));
}
}

View File

@ -0,0 +1,96 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.coverage;
import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import net.sourceforge.pmd.PMD;
import org.junit.Test;
public class PMDCoverageTest {
/**
* Test some of the PMD command line options
*/
@Test
public void testPmdOptions() {
runPmd("-d src/main/java/net/sourceforge/pmd/lang/java/rule/design -f text -R rulesets/internal/all-java.xml -version 1.5 -language java -stress -benchmark");
}
/**
* Run the PMD command line tool, i.e. call PMD.main().
*
* @param commandLine
*/
private void runPmd(String commandLine) {
String[] args;
args = commandLine.split("\\s");
File f = null;
try {
f = File.createTempFile("pmd", ".txt");
int n = args.length;
String[] a = new String[n + 2];
System.arraycopy(args, 0, a, 0, n);
a[n] = "-reportfile";
a[n + 1] = f.getAbsolutePath();
args = a;
PMD.run(args);
// FIXME: check that output doesn't have parsing errors
} catch (IOException ioe) {
fail("Problem creating temporary file: " + ioe.getLocalizedMessage());
} finally {
if (f != null) f.delete();
}
}
/**
* Name of the configuration file used by testResourceFileCommands().
*/
private static final String PMD_CONFIG_FILE = "pmd_tests.conf";
/**
* Run PMD using the command lines found in PMD_CONFIG_FILE.
*/
@Test
public void testResourceFileCommands() {
InputStream is = getClass().getResourceAsStream(PMD_CONFIG_FILE);
if (is != null) {
try {
BufferedReader r = new BufferedReader(new InputStreamReader(is));
String l;
while ((l = r.readLine()) != null) {
l = l.trim();
if (l.length() == 0 || l.charAt(0) == '#') {
continue;
}
runPmd(l);
}
r.close();
} catch (IOException ioe) {
fail("Problem reading config file: " + ioe.getLocalizedMessage());
}
} else {
fail("Missing config file: " + PMD_CONFIG_FILE);
}
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(PMDCoverageTest.class);
}
}

View File

@ -0,0 +1,109 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Unit test for {@link CPDCommandLineInterface}.
*
*/
public class CPDCommandLineInterfaceTest {
private ByteArrayOutputStream bufferStdout;
private PrintStream originalStdout;
private PrintStream originalStderr;
@Before
public void setup() {
originalStdout = System.out;
originalStderr = System.err;
bufferStdout = new ByteArrayOutputStream();
System.setOut(new PrintStream(bufferStdout));
System.setErr(System.out);
}
@After
public void teardown() {
System.setOut(originalStdout);
System.setErr(originalStderr);
}
/**
* Test ignore identifiers argument.
*/
@Test
public void testIgnoreIdentifiers() throws Exception {
runCPD("--minimum-tokens", "34", "--language", "java", "--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--ignore-identifiers");
String out = bufferStdout.toString("UTF-8");
Assert.assertTrue(out.contains("Found a 7 line (34 tokens) duplication"));
}
/**
* Test excludes option.
*/
@Test
public void testExcludes() throws Exception {
runCPD("--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"
);
String out = bufferStdout.toString("UTF-8");
Assert.assertFalse(out.contains("Found a 7 line (34 tokens) duplication"));
}
/**
* #1144 CPD encoding argument has no effect
*/
@Test
public void testEncodingOption() throws Exception {
String origEncoding = System.getProperty("file.encoding");
// set the default encoding under Windows
System.setProperty("file.encoding", "Cp1252");
runCPD("--minimum-tokens", "34", "--language", "java",
"--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/",
"--ignore-identifiers",
"--format", "xml",
// request UTF-8 for CPD
"--encoding", "UTF-8");
// reset default encoding
System.setProperty("file.encoding", origEncoding);
String out = bufferStdout.toString("UTF-8");
Assert.assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
}
/**
* See: https://sourceforge.net/p/pmd/bugs/1178/
* @throws IOException any error
*/
@Test
public void testBrokenAndValidFile() throws IOException {
runCPD("--minimum-tokens", "10",
"--language", "java",
"--files", "src/test/resources/net/sourceforge/pmd/cpd/badandgood/",
"--format", "text",
"--skip-lexical-errors");
String out = bufferStdout.toString("UTF-8");
Assert.assertTrue(out.contains("Skipping Lexical error in file"));
Assert.assertTrue(out.contains("Found a 5 line (13 tokens) duplication"));
}
private void runCPD(String... args) {
System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
CPD.main(args);
}
}