Refactor command line tests for CPD and create a BaseCPDCLITest class

This commit is contained in:
Andreas Dangel
2016-01-20 22:13:25 +01:00
parent 7a33d59a95
commit 8c4d307ddd
3 changed files with 63 additions and 70 deletions

View File

@ -3,41 +3,19 @@
*/
package net.sourceforge.pmd.cpd;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;
import org.junit.After;
import net.sourceforge.pmd.cli.BaseCPDCLITest;
import org.junit.Assert;
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() throws UnsupportedEncodingException {
originalStdout = System.out;
originalStderr = System.err;
bufferStdout = new ByteArrayOutputStream();
System.setOut(new PrintStream(bufferStdout, false, "UTF-8"));
System.setErr(System.out);
}
@After
public void teardown() {
System.setOut(originalStdout);
System.setErr(originalStderr);
}
public class CPDCommandLineInterfaceTest extends BaseCPDCLITest {
/**
* Test ignore identifiers argument.
*/
@ -45,7 +23,7 @@ public class CPDCommandLineInterfaceTest {
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");
String out = getOutput();
Assert.assertTrue(out.contains("Found a 7 line (36 tokens) duplication"));
}
@ -60,7 +38,7 @@ public class CPDCommandLineInterfaceTest {
"--exclude", "src/test/resources/net/sourceforge/pmd/cpd/clitest/File2.java"
);
String out = bufferStdout.toString("UTF-8");
String out = getOutput();
Assert.assertFalse(out.contains("Found a 7 line (34 tokens) duplication"));
}
@ -83,7 +61,7 @@ public class CPDCommandLineInterfaceTest {
// reset default encoding
System.setProperty("file.encoding", origEncoding);
String out = bufferStdout.toString("UTF-8");
String out = getOutput();
Assert.assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
Assert.assertTrue(Pattern.compile("System\\.out\\.println\\([ij] \\+ \"ä\"\\);").matcher(out).find());
}
@ -99,7 +77,7 @@ public class CPDCommandLineInterfaceTest {
"--files", "src/test/resources/net/sourceforge/pmd/cpd/badandgood/",
"--format", "text",
"--skip-lexical-errors");
String out = bufferStdout.toString("UTF-8");
String out = getOutput();
Assert.assertTrue(Pattern.compile("Skipping .*?BadFile\\.java\\. Reason: Lexical error in file").matcher(out).find());
Assert.assertTrue(out.contains("Found a 5 line (13 tokens) duplication"));
}
@ -110,7 +88,7 @@ public class CPDCommandLineInterfaceTest {
"--language", "java",
"--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/",
"--format", "xml");
String out = bufferStdout.toString("UTF-8");
String out = getOutput();
Assert.assertTrue(out.contains("<duplication lines=\"3\" tokens=\"10\">"));
}
@ -120,12 +98,7 @@ public class CPDCommandLineInterfaceTest {
"--files", "src/test/resources/net/sourceforge/pmd/cpd/badandgood/",
"--language", "c",
"--format", "csv");
String out = bufferStdout.toString("UTF-8");
String out = getOutput();
Assert.assertFalse(out.contains("Couldn't instantiate renderer"));
}
private void runCPD(String... args) {
System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
CPD.main(args);
}
}

View File

@ -3,54 +3,26 @@
*/
package net.sourceforge.pmd.cpd;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import net.sourceforge.pmd.cli.BaseCPDCLITest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class CPDCommandLineInterfaceTest {
private ByteArrayOutputStream bufferStdout;
private PrintStream originalStdout;
private PrintStream originalStderr;
@Before
public void setup() throws UnsupportedEncodingException {
originalStdout = System.out;
originalStderr = System.err;
bufferStdout = new ByteArrayOutputStream();
System.setOut(new PrintStream(bufferStdout, false, "UTF-8"));
System.setErr(System.out);
}
@After
public void teardown() {
System.setOut(originalStdout);
System.setErr(originalStderr);
}
public class CPDCommandLineInterfaceTest extends BaseCPDCLITest {
@Test
public void shouldFindDuplicatesWithDifferentFileExtensions() throws Exception {
public void shouldFindDuplicatesWithDifferentFileExtensions() {
runCPD("--minimum-tokens", "5", "--language", "js", "--files", "src/test/resources/net/sourceforge/pmd/cpd/ts/File1.ts",
"src/test/resources/net/sourceforge/pmd/cpd/ts/File2.ts");
String out = bufferStdout.toString("UTF-8");
String out = getOutput();
Assert.assertTrue(out.contains("Found a 9 line (30 tokens) duplication in the following files"));
}
@Test
public void shouldFindNoDuplicatesWithDifferentFileExtensions() throws Exception {
public void shouldFindNoDuplicatesWithDifferentFileExtensions() {
runCPD("--minimum-tokens", "5", "--language", "js", "--files", "src/test/resources/net/sourceforge/pmd/cpd/ts/");
String out = bufferStdout.toString("UTF-8");
String out = getOutput();
Assert.assertTrue(out.isEmpty());
}
private void runCPD(String ... args) {
System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
CPD.main(args);
}
}

View File

@ -0,0 +1,48 @@
/**
* 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 java.io.UnsupportedEncodingException;
import net.sourceforge.pmd.cpd.CPD;
import net.sourceforge.pmd.cpd.CPDCommandLineInterface;
import org.junit.After;
import org.junit.Before;
public abstract class BaseCPDCLITest {
private ByteArrayOutputStream bufferStdout;
private PrintStream originalStdout;
private PrintStream originalStderr;
@Before
public void setup() throws UnsupportedEncodingException {
originalStdout = System.out;
originalStderr = System.err;
bufferStdout = new ByteArrayOutputStream();
System.setOut(new PrintStream(bufferStdout, false, "UTF-8"));
System.setErr(System.out);
}
@After
public void teardown() {
System.setOut(originalStdout);
System.setErr(originalStderr);
}
public final String getOutput() {
try {
return bufferStdout.toString("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
protected void runCPD(String ... args) {
System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
CPD.main(args);
}
}