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 import java.util.regex.Pattern;
14
15 import net.sourceforge.pmd.PMD;
16 import net.sourceforge.pmd.util.FileUtil;
17
18 import org.apache.commons.io.FileUtils;
19 import org.junit.After;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24
25
26
27
28
29 public class CLITest {
30
31 private static final String TEST_OUPUT_DIRECTORY = "target/cli-tests/";
32
33
34
35 private static final String SOURCE_FOLDER = "src/main/resources";
36
37 private PrintStream originalOut;
38 private PrintStream originalErr;
39
40
41
42
43 @BeforeClass
44 public static void setUp() throws Exception {
45 System.setProperty(PMDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
46 File testOuputDir = new File(TEST_OUPUT_DIRECTORY);
47 if (!testOuputDir.exists()) {
48 assertTrue("failed to create output directory for test:" + testOuputDir.getAbsolutePath(),
49 testOuputDir.mkdirs());
50 }
51 }
52
53 @Before
54 public void setup() {
55 originalOut = System.out;
56 originalErr = System.err;
57 }
58
59 @After
60 public void tearDown() {
61 System.setOut(originalOut);
62 System.setErr(originalErr);
63 }
64
65 private void createTestOutputFile(String filename) {
66 try {
67 PrintStream out = new PrintStream(new FileOutputStream(filename));
68 System.setOut(out);
69 System.setErr(out);
70 } catch (FileNotFoundException e) {
71 fail("Can't create file " + filename + " for test.");
72 }
73 }
74
75 @Test
76 public void minimalArgs() {
77 String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design" };
78 runTest(args, "minimalArgs");
79 }
80
81 @Test
82 public void minimumPriority() {
83 String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design", "-min", "1"};
84 runTest(args,"minimumPriority");
85 }
86
87 @Test
88 public void usingDebug() {
89 String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design", "-debug" };
90 runTest(args, "minimalArgsWithDebug");
91 }
92
93 @Test
94 public void changeJavaVersion() {
95 String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design", "-version", "1.5",
96 "-language", "java", "-debug" };
97 String resultFilename = runTest(args, "chgJavaVersion");
98 assertTrue("Invalid Java version",
99 FileUtil.findPatternInFile(new File(resultFilename), "Using Java version: Java 1.5"));
100 }
101
102 @Test
103 public void useEcmaScript() {
104 String[] args = { "-d", SOURCE_FOLDER, "-f", "xml", "-R", "ecmascript-basic", "-version", "3", "-l",
105 "ecmascript", "-debug" };
106 String resultFilename = runTest(args, "useEcmaScript");
107 assertTrue("Invalid Java version",
108 FileUtil.findPatternInFile(new File(resultFilename), "Using Ecmascript version: Ecmascript 3"));
109 }
110
111
112
113
114 @Test
115 public void testWrongRuleset() throws Exception {
116 String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-designn" };
117 String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
118 createTestOutputFile(filename);
119 runPMDWith(args);
120 Assert.assertEquals(1, getStatusCode());
121 assertTrue(FileUtil.findPatternInFile(new File(filename), "Can't find resource 'null' for rule 'java-designn'."
122 + " Make sure the resource is a valid file"));
123 }
124
125
126
127
128 @Test
129 public void testWrongRulesetWithRulename() throws Exception {
130 String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-designn/UseCollectionIsEmpty" };
131 String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
132 createTestOutputFile(filename);
133 runPMDWith(args);
134 Assert.assertEquals(1, getStatusCode());
135 assertTrue(FileUtil.findPatternInFile(new File(filename), "Can't find resource 'null' for rule "
136 + "'java-designn/UseCollectionIsEmpty'."));
137 }
138
139
140
141
142 @Test
143 public void testWrongRulename() throws Exception {
144 String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-design/ThisRuleDoesNotExist" };
145 String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
146 createTestOutputFile(filename);
147 runPMDWith(args);
148 Assert.assertEquals(1, getStatusCode());
149 assertTrue(FileUtil.findPatternInFile(new File(filename), Pattern.quote("No rules found. Maybe you mispelled a rule name?"
150 + " (java-design/ThisRuleDoesNotExist)")));
151 }
152
153 private String runTest(String[] args, String testname) {
154 String filename = TEST_OUPUT_DIRECTORY + testname + ".txt";
155 long start = System.currentTimeMillis();
156 createTestOutputFile(filename);
157 System.out.println("Start running test " + testname);
158 runPMDWith(args);
159 checkStatusCode();
160 System.out.println("Test finished successfully after " + (System.currentTimeMillis() - start) + "ms.");
161 return filename;
162 }
163
164 private void runPMDWith(String[] args) {
165 PMD.main(args);
166 }
167
168 private void checkStatusCode() {
169 int statusCode = getStatusCode();
170 if (statusCode > 0)
171 fail("PMD failed with status code:" + statusCode);
172 }
173
174 private int getStatusCode() {
175 return Integer.parseInt(System.getProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY));
176 }
177
178 }