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 java.io.ByteArrayOutputStream;
7   import java.io.PrintStream;
8   import java.io.UnsupportedEncodingException;
9   
10  import net.sourceforge.pmd.cpd.CPD;
11  import net.sourceforge.pmd.cpd.CPDCommandLineInterface;
12  
13  import org.junit.After;
14  import org.junit.Before;
15  
16  public abstract class BaseCPDCLITest {
17      private ByteArrayOutputStream bufferStdout;
18      private PrintStream originalStdout;
19      private PrintStream originalStderr;
20  
21      @Before
22      public void setup() throws UnsupportedEncodingException {
23          originalStdout = System.out;
24          originalStderr = System.err;
25          bufferStdout = new ByteArrayOutputStream();
26          System.setOut(new PrintStream(bufferStdout, false, "UTF-8"));
27          System.setErr(System.out);
28      }
29  
30      @After
31      public void teardown() {
32          System.setOut(originalStdout);
33          System.setErr(originalStderr);
34      }
35  
36      public final String getOutput() {
37          try {
38              return bufferStdout.toString("UTF-8");
39          } catch (UnsupportedEncodingException e) {
40              throw new RuntimeException(e);
41          }
42      }
43  
44      protected void runCPD(String ... args) {
45          System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
46          CPD.main(args);
47      }
48  }