View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.cli;
5   
6   import static org.junit.Assert.assertTrue;
7   
8   import java.io.File;
9   import java.util.regex.Pattern;
10  
11  import net.sourceforge.pmd.util.FileUtil;
12  
13  import org.junit.Assert;
14  import org.junit.Test;
15  
16  /**
17   * @author Romain Pelisse <belaran@gmail.com>
18   * 
19   */
20  public class CLITest extends BaseCLITest {
21      @Test
22      public void minimalArgs() {
23          String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-unnecessary,java-design" };
24          runTest(args, "minimalArgs");
25      }
26  
27      @Test
28      public void minimumPriority() {
29          String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-design", "-min", "1"};
30          runTest(args,"minimumPriority");
31      }
32  
33      @Test
34      public void usingDebug() {
35          String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-design", "-debug" };
36          runTest(args, "minimalArgsWithDebug");
37      }
38  
39      @Test
40      public void changeJavaVersion() {
41          String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-design", "-version", "1.5",
42                  "-language", "java", "-debug" };
43          String resultFilename = runTest(args, "chgJavaVersion");
44          assertTrue("Invalid Java version",
45                  FileUtil.findPatternInFile(new File(resultFilename), "Using Java version: Java 1.5"));
46      }
47  
48      @Test
49      public void exitStatusNoViolations() {
50          String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-design" };
51          runTest(args, "exitStatusNoViolations");
52      }
53  
54      @Test
55      public void exitStatusWithViolations() {
56          String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-empty" };
57          String resultFilename = runTest(args, "exitStatusWithViolations", 4);
58          assertTrue(FileUtil.findPatternInFile(new File(resultFilename), "Avoid empty if"));
59      }
60  
61      /**
62       * See https://sourceforge.net/p/pmd/bugs/1231/
63       */
64      @Test
65      public void testWrongRuleset() throws Exception {
66          String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-designn" };
67          String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
68          createTestOutputFile(filename);
69          runPMDWith(args);
70          Assert.assertEquals(1, getStatusCode());
71          assertTrue(FileUtil.findPatternInFile(new File(filename), "Can't find resource 'null' for rule 'java-designn'."
72                  + "  Make sure the resource is a valid file"));
73      }
74  
75      /**
76       * See https://sourceforge.net/p/pmd/bugs/1231/
77       */
78      @Test
79      public void testWrongRulesetWithRulename() throws Exception {
80          String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-designn/UseCollectionIsEmpty" };
81          String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
82          createTestOutputFile(filename);
83          runPMDWith(args);
84          Assert.assertEquals(1, getStatusCode());
85          assertTrue(FileUtil.findPatternInFile(new File(filename), "Can't find resource 'null' for rule "
86                  + "'java-designn/UseCollectionIsEmpty'."));
87      }
88  
89      /**
90       * See https://sourceforge.net/p/pmd/bugs/1231/
91       */
92      @Test
93      public void testWrongRulename() throws Exception {
94          String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-design/ThisRuleDoesNotExist" };
95          String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
96          createTestOutputFile(filename);
97          runPMDWith(args);
98          Assert.assertEquals(1, getStatusCode());
99          assertTrue(FileUtil.findPatternInFile(new File(filename), Pattern.quote("No rules found. Maybe you mispelled a rule name?"
100                 + " (java-design/ThisRuleDoesNotExist)")));
101     }
102 }