View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.strings;
5   
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertTrue;
8   
9   import java.util.Set;
10  
11  import net.sourceforge.pmd.Rule;
12  import net.sourceforge.pmd.testframework.SimpleAggregatorTst;
13  
14  import org.junit.Test;
15  
16  public class AvoidDuplicateLiteralsRuleTest extends SimpleAggregatorTst {
17      @Test
18      public void testAll() {
19          Rule rule = findRule("java-strings", "AvoidDuplicateLiterals");
20          rule.setProperty(AvoidDuplicateLiteralsRule.THRESHOLD_DESCRIPTOR, 2);
21          runTests(rule);
22      }
23  
24      @Test
25      public void testStringParserEmptyString() {
26          AvoidDuplicateLiteralsRule.ExceptionParser p = new AvoidDuplicateLiteralsRule.ExceptionParser(',');
27          Set<String> res = p.parse("");
28          assertTrue(res.isEmpty());
29      }
30  
31      @Test
32      public void testStringParserSimple() {
33          AvoidDuplicateLiteralsRule.ExceptionParser p = new AvoidDuplicateLiteralsRule.ExceptionParser(',');
34          Set<String> res = p.parse("a,b,c");
35          assertEquals(3, res.size());
36          assertTrue(res.contains("a"));
37          assertTrue(res.contains("b"));
38          assertTrue(res.contains("c"));
39      }
40  
41      @Test
42      public void testStringParserEscapedChar() {
43          AvoidDuplicateLiteralsRule.ExceptionParser p = new AvoidDuplicateLiteralsRule.ExceptionParser(',');
44          Set<String> res = p.parse("a,b,\\,");
45          assertEquals(3, res.size());
46          assertTrue(res.contains("a"));
47          assertTrue(res.contains("b"));
48          assertTrue(res.contains(","));
49      }
50  
51      @Test
52      public void testStringParserEscapedEscapedChar() {
53          AvoidDuplicateLiteralsRule.ExceptionParser p = new AvoidDuplicateLiteralsRule.ExceptionParser(',');
54          Set<String> res = p.parse("a,b,\\\\");
55          assertEquals(3, res.size());
56          assertTrue(res.contains("a"));
57          assertTrue(res.contains("b"));
58          assertTrue(res.contains("\\"));
59      }
60  }