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