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   import java.io.UnsupportedEncodingException;
10  import java.util.regex.Pattern;
11  
12  import org.junit.After;
13  import org.junit.Assert;
14  import org.junit.Before;
15  import org.junit.Test;
16  
17  /**
18   * Unit test for {@link CPDCommandLineInterface}.
19   *
20   */
21  public class CPDCommandLineInterfaceTest {
22      private ByteArrayOutputStream bufferStdout;
23      private PrintStream originalStdout;
24      private PrintStream originalStderr;
25  
26      @Before
27      public void setup() throws UnsupportedEncodingException {
28          originalStdout = System.out;
29          originalStderr = System.err;
30          bufferStdout = new ByteArrayOutputStream();
31          System.setOut(new PrintStream(bufferStdout, false, "UTF-8"));
32          System.setErr(System.out);
33      }
34  
35      @After
36      public void teardown() {
37          System.setOut(originalStdout);
38          System.setErr(originalStderr);
39      }
40  
41      /**
42       * Test ignore identifiers argument.
43       */
44      @Test
45      public void testIgnoreIdentifiers() throws Exception {
46          runCPD("--minimum-tokens", "34", "--language", "java", "--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--ignore-identifiers");
47  
48          String out = bufferStdout.toString("UTF-8");
49          Assert.assertTrue(out.contains("Found a 7 line (36 tokens) duplication"));
50      }
51  
52      /**
53       * Test excludes option.
54       */
55      @Test
56      public void testExcludes() throws Exception {
57          runCPD("--minimum-tokens", "34", "--language", "java",
58                  "--ignore-identifiers",
59                  "--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/",
60                  "--exclude", "src/test/resources/net/sourceforge/pmd/cpd/clitest/File2.java"
61                  );
62  
63          String out = bufferStdout.toString("UTF-8");
64          Assert.assertFalse(out.contains("Found a 7 line (34 tokens) duplication"));
65      }
66  
67      /**
68       * #1144 CPD encoding argument has no effect
69       */
70      @Test
71      public void testEncodingOption() throws Exception {
72          String origEncoding = System.getProperty("file.encoding");
73  
74          // set the default encoding under Windows
75          System.setProperty("file.encoding", "Cp1252");
76  
77          runCPD("--minimum-tokens", "34", "--language", "java",
78                  "--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/",
79                  "--ignore-identifiers",
80                  "--format", "xml",
81          // request UTF-8 for CPD
82                  "--encoding", "UTF-8");
83          // reset default encoding
84          System.setProperty("file.encoding", origEncoding);
85  
86          String out = bufferStdout.toString("UTF-8");
87          Assert.assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
88          Assert.assertTrue(Pattern.compile("System\\.out\\.println\\([ij] \\+ \"รค\"\\);").matcher(out).find());
89      }
90  
91      /**
92       * See: https://sourceforge.net/p/pmd/bugs/1178/
93       * @throws IOException any error
94       */
95      @Test
96      public void testBrokenAndValidFile() throws IOException {
97          runCPD("--minimum-tokens", "10",
98                 "--language", "java",
99                 "--files", "src/test/resources/net/sourceforge/pmd/cpd/badandgood/",
100                "--format", "text",
101                "--skip-lexical-errors");
102         String out = bufferStdout.toString("UTF-8");
103         Assert.assertTrue(Pattern.compile("Skipping .*?BadFile\\.java\\. Reason: Lexical error in file").matcher(out).find());
104         Assert.assertTrue(out.contains("Found a 5 line (13 tokens) duplication"));
105     }
106 
107     @Test
108     public void testFormatXmlWithoutEncoding() throws Exception {
109         runCPD("--minimum-tokens", "10",
110                "--language", "java",
111                "--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/",
112                "--format", "xml");
113         String out = bufferStdout.toString("UTF-8");
114         Assert.assertTrue(out.contains("<duplication lines=\"3\" tokens=\"10\">"));
115     }
116 
117     @Test
118     public void testCSVFormat() throws Exception {
119         runCPD("--minimum-tokens", "100",
120                "--files", "src/test/resources/net/sourceforge/pmd/cpd/badandgood/",
121                "--language", "c",
122                "--format", "csv");
123         String out = bufferStdout.toString("UTF-8");
124         Assert.assertFalse(out.contains("Couldn't instantiate renderer"));
125     }
126 
127     private void runCPD(String... args) {
128         System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
129         CPD.main(args);
130     }
131 }