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   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  
16  import org.junit.After;
17  import org.junit.Before;
18  import org.junit.BeforeClass;
19  
20  /**
21   * @author Romain Pelisse <belaran@gmail.com>
22   * 
23   */
24  public abstract class BaseCLITest {
25  
26      protected static final String TEST_OUPUT_DIRECTORY = "target/cli-tests/";
27  
28      // Points toward a folder with not many source files, to avoid actually PMD
29      // and slowing down tests
30      protected static final String SOURCE_FOLDER = "src/test/resources/net/sourceforge/pmd/cli";
31  
32      protected PrintStream originalOut;
33      protected PrintStream originalErr;
34  
35      /**
36       * @throws java.lang.Exception
37       */
38      @BeforeClass
39      public static void setUp() throws Exception {
40          System.setProperty(PMDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
41          File testOuputDir = new File(TEST_OUPUT_DIRECTORY);
42          if (!testOuputDir.exists()) {
43              assertTrue("failed to create output directory for test:" + testOuputDir.getAbsolutePath(),
44                      testOuputDir.mkdirs());
45          }
46      }
47  
48      @Before
49      public void setup() {
50          originalOut = System.out;
51          originalErr = System.err;
52      }
53  
54      @After
55      public void tearDown() {
56          System.setOut(originalOut);
57          System.setErr(originalErr);
58      }
59  
60      protected void createTestOutputFile(String filename) {
61          try {
62              PrintStream out = new PrintStream(new FileOutputStream(filename));
63              System.setOut(out);
64              System.setErr(out);
65          } catch (FileNotFoundException e) {
66              fail("Can't create file " + filename + " for test.");
67          }
68      }
69  
70      protected String runTest(String[] args, String testname) {
71          return runTest(args, testname, 0);
72      }
73      protected String runTest(String[] args, String testname, int expectedExitCode) {
74          String filename = TEST_OUPUT_DIRECTORY + testname + ".txt";
75          long start = System.currentTimeMillis();
76          createTestOutputFile(filename);
77          System.out.println("Start running test " + testname);
78          runPMDWith(args);
79          checkStatusCode(expectedExitCode);
80          System.out.println("Test finished successfully after " + (System.currentTimeMillis() - start) + "ms.");
81          return filename;
82      }
83  
84      protected void runPMDWith(String[] args) {
85          PMD.main(args);
86      }
87  
88      protected void checkStatusCode(int expectedExitCode) {
89          int statusCode = getStatusCode();
90          if (statusCode != expectedExitCode) {
91              fail("PMD failed with status code:" + statusCode);
92          }
93      }
94  
95      protected int getStatusCode() {
96          return Integer.parseInt(System.getProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY));
97      }
98  }