1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package test.net.sourceforge.pmd.rules.strings;
5   
6   import net.sourceforge.pmd.PMD;
7   import net.sourceforge.pmd.Rule;
8   import net.sourceforge.pmd.rules.strings.AvoidDuplicateLiteralsRule;
9   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
11  
12  import java.util.Set;
13  
14  public class AvoidDuplicateLiteralsRuleTest extends SimpleAggregatorTst {
15  
16      private Rule rule;
17  
18      public void setUp() throws Exception {
19          rule = findRule("strings", "AvoidDuplicateLiterals");
20          rule.addProperty("threshold", "2");
21      }
22  
23      public void testAll() {
24          runTests(new TestDescriptor[]{
25              new TestDescriptor(TEST1, "duplicate literals in argument list", 1, rule),
26              new TestDescriptor(TEST2, "literal int argument, ok for now", 0, rule),
27              new TestDescriptor(TEST3, "duplicate literals in field decl", 1, rule),
28          });
29      }
30  
31      public void testStringParserEmptyString() {
32          AvoidDuplicateLiteralsRule.ExceptionParser p = new AvoidDuplicateLiteralsRule.ExceptionParser(',');
33          Set res = p.parse("");
34          assertTrue(res.isEmpty());
35      }
36  
37      public void testStringParserSimple() {
38          AvoidDuplicateLiteralsRule.ExceptionParser p = new AvoidDuplicateLiteralsRule.ExceptionParser(',');
39          Set res = p.parse("a,b,c");
40          assertEquals(3, res.size());
41          assertTrue(res.contains("a"));
42          assertTrue(res.contains("b"));
43          assertTrue(res.contains("c"));
44      }
45  
46      public void testStringParserEscapedChar() {
47          AvoidDuplicateLiteralsRule.ExceptionParser p = new AvoidDuplicateLiteralsRule.ExceptionParser(',');
48          Set res = p.parse("a,b,//,");
49          assertEquals(3, res.size());
50          assertTrue(res.contains("a"));
51          assertTrue(res.contains("b"));
52          assertTrue(res.contains(","));
53      }
54  
55      public void testStringParserEscapedEscapedChar() {
56          AvoidDuplicateLiteralsRule.ExceptionParser p = new AvoidDuplicateLiteralsRule.ExceptionParser(',');
57          Set res = p.parse("a,b,////");
58          assertEquals(3, res.size());
59          assertTrue(res.contains("a"));
60          assertTrue(res.contains("b"));
61          assertTrue(res.contains("//"));
62      }
63  
64      public static final String TEST1 =
65              "public class Foo {" + PMD.EOL +
66              " private void bar() {" + PMD.EOL +
67              "    buz(\"Howdy\");" + PMD.EOL +
68              "    buz(\"Howdy\");" + PMD.EOL +
69              "    buz(\"Howdy\");" + PMD.EOL +
70              "    buz(\"Howdy\");" + PMD.EOL +
71              " }" + PMD.EOL +
72              "}";
73  
74      public static final String TEST2 =
75              "public class Foo {" + PMD.EOL +
76              " private void bar() {" + PMD.EOL +
77              "    buz(2);" + PMD.EOL +
78              " }" + PMD.EOL +
79              "}";
80  
81      public static final String TEST3 =
82              "public class Foo {" + PMD.EOL +
83              " String[] FOO = {\"foo\", \"foo\", \"foo\", \"foo\", \"foo\", \"foo\", \"foo\", \"foo\", \"foo\"};" + PMD.EOL +
84              "}";
85  
86  }