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

@ -4,74 +4,20 @@
package net.sourceforge.pmd.cli;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.regex.Pattern;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.util.FileUtil;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author Romain Pelisse <belaran@gmail.com>
*
*/
public class CLITest {
private static final String TEST_OUPUT_DIRECTORY = "target/cli-tests/";
// Points toward a folder without any source files, to avoid actually PMD
// and slowing down tests
private static final String SOURCE_FOLDER = "src/main/resources";
private PrintStream originalOut;
private PrintStream originalErr;
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUp() throws Exception {
System.setProperty(PMDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
File testOuputDir = new File(TEST_OUPUT_DIRECTORY);
if (!testOuputDir.exists()) {
assertTrue("failed to create output directory for test:" + testOuputDir.getAbsolutePath(),
testOuputDir.mkdirs());
}
}
@Before
public void setup() {
originalOut = System.out;
originalErr = System.err;
}
@After
public void tearDown() {
System.setOut(originalOut);
System.setErr(originalErr);
}
private void createTestOutputFile(String filename) {
try {
PrintStream out = new PrintStream(new FileOutputStream(filename));
System.setOut(out);
System.setErr(out);
} catch (FileNotFoundException e) {
fail("Can't create file " + filename + " for test.");
}
}
public class CLITest extends BaseCLITest {
@Test
public void minimalArgs() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design" };
@ -99,15 +45,6 @@ public class CLITest {
FileUtil.findPatternInFile(new File(resultFilename), "Using Java version: Java 1.5"));
}
@Test
public void useEcmaScript() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "xml", "-R", "ecmascript-basic", "-version", "3", "-l",
"ecmascript", "-debug" };
String resultFilename = runTest(args, "useEcmaScript");
assertTrue("Invalid Java version",
FileUtil.findPatternInFile(new File(resultFilename), "Using Ecmascript version: Ecmascript 3"));
}
/**
* See https://sourceforge.net/p/pmd/bugs/1231/
*/
@ -149,30 +86,4 @@ public class CLITest {
assertTrue(FileUtil.findPatternInFile(new File(filename), Pattern.quote("No rules found. Maybe you mispelled a rule name?"
+ " (java-design/ThisRuleDoesNotExist)")));
}
private String runTest(String[] args, String testname) {
String filename = TEST_OUPUT_DIRECTORY + testname + ".txt";
long start = System.currentTimeMillis();
createTestOutputFile(filename);
System.out.println("Start running test " + testname);
runPMDWith(args);
checkStatusCode();
System.out.println("Test finished successfully after " + (System.currentTimeMillis() - start) + "ms.");
return filename;
}
private void runPMDWith(String[] args) {
PMD.main(args);
}
private void checkStatusCode() {
int statusCode = getStatusCode();
if (statusCode > 0)
fail("PMD failed with status code:" + statusCode);
}
private int getStatusCode() {
return Integer.parseInt(System.getProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY));
}
}

View File

@ -0,0 +1,27 @@
/**
* 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 net.sourceforge.pmd.util.FileUtil;
import org.junit.Test;
/**
* @author Romain Pelisse <belaran@gmail.com>
*
*/
public class CLITest extends BaseCLITest {
@Test
public void useEcmaScript() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "xml", "-R", "ecmascript-basic", "-version", "3", "-l",
"ecmascript", "-debug" };
String resultFilename = runTest(args, "useEcmaScript");
assertTrue("Invalid Java version",
FileUtil.findPatternInFile(new File(resultFilename), "Using Ecmascript version: Ecmascript 3"));
}
}

View File

@ -0,0 +1,94 @@
/**
* 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 static org.junit.Assert.fail;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import net.sourceforge.pmd.PMD;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
/**
* @author Romain Pelisse <belaran@gmail.com>
*
*/
public abstract class BaseCLITest {
protected static final String TEST_OUPUT_DIRECTORY = "target/cli-tests/";
// Points toward a folder without any source files, to avoid actually PMD
// and slowing down tests
protected static final String SOURCE_FOLDER = "src/main/resources";
private PrintStream originalOut;
private PrintStream originalErr;
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUp() throws Exception {
System.setProperty(PMDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
File testOuputDir = new File(TEST_OUPUT_DIRECTORY);
if (!testOuputDir.exists()) {
assertTrue("failed to create output directory for test:" + testOuputDir.getAbsolutePath(),
testOuputDir.mkdirs());
}
}
@Before
public void setup() {
originalOut = System.out;
originalErr = System.err;
}
@After
public void tearDown() {
System.setOut(originalOut);
System.setErr(originalErr);
}
protected void createTestOutputFile(String filename) {
try {
PrintStream out = new PrintStream(new FileOutputStream(filename));
System.setOut(out);
System.setErr(out);
} catch (FileNotFoundException e) {
fail("Can't create file " + filename + " for test.");
}
}
protected String runTest(String[] args, String testname) {
String filename = TEST_OUPUT_DIRECTORY + testname + ".txt";
long start = System.currentTimeMillis();
createTestOutputFile(filename);
System.out.println("Start running test " + testname);
runPMDWith(args);
checkStatusCode();
System.out.println("Test finished successfully after " + (System.currentTimeMillis() - start) + "ms.");
return filename;
}
protected void runPMDWith(String[] args) {
PMD.main(args);
}
protected void checkStatusCode() {
int statusCode = getStatusCode();
if (statusCode > 0)
fail("PMD failed with status code:" + statusCode);
}
protected int getStatusCode() {
return Integer.parseInt(System.getProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY));
}
}

View File

@ -8,13 +8,13 @@ import java.util.Properties;
import net.sourceforge.pmd.PMDConfiguration;
import net.sourceforge.pmd.RulePriority;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import com.beust.jcommander.IStringConverter;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.validators.PositiveInteger;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
public class PMDParameters {
@ -67,10 +67,10 @@ public class PMDParameters {
private String reportfile = null;
@Parameter(names = { "-version", "-v" }, description = "specify version of a language PMD should use")
private String version = LanguageRegistry.getDefaultLanguage().getDefaultVersion().getVersion();
private String version = null;
@Parameter(names = { "-language", "-l" }, description = "specify a language PMD should use")
private String language = LanguageRegistry.getDefaultLanguage().getTerseName();
private String language = null;
@Parameter(names = "-auxclasspath", description = "specifies the classpath for libraries used by the source code. This is used by the type resolution. Alternatively, a 'file://' URL to a text file containing path elements on consecutive lines can be specified.")
private String auxclasspath;
@ -192,11 +192,11 @@ public class PMDParameters {
}
public String getVersion() {
return version;
return version != null ? version : LanguageRegistry.getDefaultLanguage().getDefaultVersion().getVersion();
}
public String getLanguage() {
return language;
return language != null ? language : LanguageRegistry.getDefaultLanguage().getTerseName();
}
public String getAuxclasspath() {