View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.cpd;
5   
6   import java.io.ByteArrayOutputStream;
7   import java.io.IOException;
8   import java.io.PrintStream;
9   
10  import junit.framework.Assert;
11  
12  import org.junit.After;
13  import org.junit.Before;
14  import org.junit.Test;
15  
16  /**
17   * Unit test for {@link CPDCommandLineInterface}.
18   *
19   */
20  public class CPDCommandLineInterfaceTest {
21      private ByteArrayOutputStream bufferStdout;
22      private PrintStream originalStdout;
23      private PrintStream originalStderr;
24  
25      @Before
26      public void setup() {
27          originalStdout = System.out;
28          originalStderr = System.err;
29          bufferStdout = new ByteArrayOutputStream();
30          System.setOut(new PrintStream(bufferStdout));
31          System.setErr(System.out);
32      }
33  
34      @After
35      public void teardown() {
36          System.setOut(originalStdout);
37          System.setErr(originalStderr);
38      }
39      
40      /**
41       * Test ignore identifiers argument.
42       */
43      @Test
44      public void testIgnoreIdentifiers() throws Exception {
45          runCPD("--minimum-tokens", "34", "--language", "java", "--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--ignore-identifiers");
46  
47          String out = bufferStdout.toString("UTF-8");
48          Assert.assertTrue(out.contains("Found a 7 line (34 tokens) duplication"));
49      }
50  
51      /**
52       * Test excludes option.
53       */
54      @Test
55      public void testExcludes() throws Exception {
56          runCPD("--minimum-tokens", "34", "--language", "java",
57                  "--ignore-identifiers",
58                  "--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/",
59                  "--exclude", "src/test/resources/net/sourceforge/pmd/cpd/clitest/File2.java"
60                  );
61  
62          String out = bufferStdout.toString("UTF-8");
63          Assert.assertFalse(out.contains("Found a 7 line (34 tokens) duplication"));
64      }
65  
66      /**
67       * #1144 CPD encoding argument has no effect
68       */
69      @Test
70      public void testEncodingOption() throws Exception {
71          String origEncoding = System.getProperty("file.encoding");
72  
73          // set the default encoding under Windows
74          System.setProperty("file.encoding", "Cp1252");
75  
76          runCPD("--minimum-tokens", "34", "--language", "java",
77                  "--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/",
78                  "--ignore-identifiers",
79                  "--format", "xml",
80          // request UTF-8 for CPD
81                  "--encoding", "UTF-8");
82          // reset default encoding
83          System.setProperty("file.encoding", origEncoding);
84  
85          String out = bufferStdout.toString("UTF-8");
86          Assert.assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
87      }
88  
89      /**
90       * See: https://sourceforge.net/p/pmd/bugs/1178/
91       * @throws IOException any error
92       */
93      @Test
94      public void testBrokenAndValidFile() throws IOException {
95          runCPD("--minimum-tokens", "10",
96                 "--language", "java",
97                 "--files", "src/test/resources/net/sourceforge/pmd/cpd/badandgood/",
98                 "--format", "text",
99                 "--skip-lexical-errors");
100         String out = bufferStdout.toString("UTF-8");
101         Assert.assertTrue(out.contains("Skipping Lexical error in file"));
102         Assert.assertTrue(out.contains("Found a 5 line (13 tokens) duplication"));
103     }
104 
105     private void runCPD(String... args) {
106         System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
107         CPD.main(args);
108     }
109 }