1
2
3
4 package net.sourceforge.pmd.cli;
5
6 import static org.junit.Assert.assertTrue;
7 import static org.junit.Assert.fail;
8
9 import java.io.File;
10 import java.io.FileNotFoundException;
11 import java.io.FileOutputStream;
12 import java.io.PrintStream;
13
14 import net.sourceforge.pmd.PMD;
15 import net.sourceforge.pmd.util.FileUtil;
16
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19
20
21
22
23
24 public class CLITest {
25
26 private static final String TEST_OUPUT_DIRECTORY = "target/cli-tests/";
27
28
29
30 private static final String SOURCE_FOLDER = "src/main/resources";
31
32
33
34
35 @BeforeClass
36 public static void setUp() throws Exception {
37 System.setProperty(PMDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
38 File testOuputDir = new File(TEST_OUPUT_DIRECTORY);
39 if (!testOuputDir.exists()) {
40 assertTrue("failed to create output directory for test:" + testOuputDir.getAbsolutePath(),
41 testOuputDir.mkdirs());
42 }
43 }
44
45 private void createTestOutputFile(String filename) {
46 try {
47 PrintStream out = new PrintStream(new FileOutputStream(filename));
48 System.setOut(out);
49 System.setErr(out);
50 } catch (FileNotFoundException e) {
51 fail("Can't create file " + filename + " for test.");
52 }
53 }
54
55 @Test
56 public void minimalArgs() {
57 String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design" };
58 runTest(args, "minimalArgs");
59 }
60
61 @Test
62 public void minimumPriority() {
63 String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design", "-min", "1"};
64 runTest(args,"minimumPriority");
65 }
66
67 @Test
68 public void usingDebug() {
69 String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design", "-debug" };
70 runTest(args, "minimalArgsWithDebug");
71 }
72
73 @Test
74 public void changeJavaVersion() {
75 String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design", "-version", "1.5",
76 "-language", "java", "-debug" };
77 String resultFilename = runTest(args, "chgJavaVersion");
78 assertTrue("Invalid Java version",
79 FileUtil.findPatternInFile(new File(resultFilename), "Using Java version: Java 1.5"));
80 }
81
82 @Test
83 public void useEcmaScript() {
84 String[] args = { "-d", SOURCE_FOLDER, "-f", "xml", "-R", "ecmascript-basic", "-version", "3", "-l",
85 "ecmascript", "-debug" };
86 String resultFilename = runTest(args, "useEcmaScript");
87 assertTrue("Invalid Java version",
88 FileUtil.findPatternInFile(new File(resultFilename), "Using Ecmascript version: Ecmascript 3"));
89 }
90
91 private String runTest(String[] args, String testname) {
92 String filename = TEST_OUPUT_DIRECTORY + testname + ".txt";
93 long start = System.currentTimeMillis();
94 createTestOutputFile(filename);
95 System.out.println("Start running test " + testname);
96 runPMDWith(args);
97 checkStatusCode();
98 System.out.println("Test finished successfully after " + (System.currentTimeMillis() - start) + "ms.");
99 return filename;
100 }
101
102 private void runPMDWith(String[] args) {
103 PMD.main(args);
104 }
105
106 private void checkStatusCode() {
107 int statusCode = Integer.valueOf(System.getProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY));
108 if (statusCode > 0)
109 fail("PMD failed with status code:" + statusCode);
110 }
111
112 }