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 org.junit.Assert;
7   import org.junit.Before;
8   import org.junit.Rule;
9   import org.junit.Test;
10  import org.junit.contrib.java.lang.system.ExpectedSystemExit;
11  import org.junit.contrib.java.lang.system.RestoreSystemProperties;
12  
13  /**
14   * Unit test for {@link PMDCommandLineInterface}
15   */
16  public class PMDCommandLineInterfaceTest {
17      @Rule
18      public final ExpectedSystemExit exit = ExpectedSystemExit.none();
19  
20      @Rule //Restores system properties after test
21      public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();
22  
23      @Before
24      public void clearSystemProperties () {
25          System.clearProperty(PMDCommandLineInterface.NO_EXIT_AFTER_RUN);
26          System.clearProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY);
27      }
28  
29      @Test
30      public void testProperties() {
31          PMDParameters params = new PMDParameters();
32          String[] args = { "-d", "source_folder", "-f", "yahtml", "-P", "outputDir=output_folder",
33                  "-R", "java-empty" };
34          PMDCommandLineInterface.extractParameters(params, args, "PMD");
35  
36          Assert.assertEquals("output_folder", params.getProperties().getProperty("outputDir"));
37      }
38  
39      @Test
40      public void testMultipleProperties() {
41          PMDParameters params = new PMDParameters();
42          String[] args = { "-d", "source_folder", "-f", "ideaj", "-P", "sourcePath=/home/user/source/",
43                  "-P", "fileName=Foo.java",
44                  "-P", "classAndMethodName=Foo.method",
45                  "-R", "java-empty" };
46          PMDCommandLineInterface.extractParameters(params, args, "PMD");
47  
48          Assert.assertEquals("/home/user/source/", params.getProperties().getProperty("sourcePath"));
49          Assert.assertEquals("Foo.java", params.getProperties().getProperty("fileName"));
50          Assert.assertEquals("Foo.method", params.getProperties().getProperty("classAndMethodName"));
51      }
52  
53      @Test
54      public void testSetStatusCodeOrExit_DoExit() {
55          exit.expectSystemExitWithStatus(0);
56  
57          PMDCommandLineInterface.setStatusCodeOrExit(0);
58      }
59  
60      @Test
61      public void testSetStatusCodeOrExit_SetStatus() {
62          System.setProperty(PMDCommandLineInterface.NO_EXIT_AFTER_RUN, "1");
63  
64          PMDCommandLineInterface.setStatusCodeOrExit(0);
65          Assert.assertEquals(System.getProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY), "0");
66      }
67  }