1
2
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
18 @Test
19 public void testAll() {
20 Rule rule = findRule("java-strings", "AvoidDuplicateLiterals");
21 rule.setProperty(AvoidDuplicateLiteralsRule.THRESHOLD_DESCRIPTOR, 2);
22 runTests(rule);
23 }
24
25 @Test
26 public void testStringParserEmptyString() {
27 AvoidDuplicateLiteralsRule.ExceptionParser p = new AvoidDuplicateLiteralsRule.ExceptionParser(',');
28 Set res = p.parse("");
29 assertTrue(res.isEmpty());
30 }
31
32 @Test
33 public void testStringParserSimple() {
34 AvoidDuplicateLiteralsRule.ExceptionParser p = new AvoidDuplicateLiteralsRule.ExceptionParser(',');
35 Set res = p.parse("a,b,c");
36 assertEquals(3, res.size());
37 assertTrue(res.contains("a"));
38 assertTrue(res.contains("b"));
39 assertTrue(res.contains("c"));
40 }
41
42 @Test
43 public void testStringParserEscapedChar() {
44 AvoidDuplicateLiteralsRule.ExceptionParser p = new AvoidDuplicateLiteralsRule.ExceptionParser(',');
45 Set res = p.parse("a,b,\\,");
46 assertEquals(3, res.size());
47 assertTrue(res.contains("a"));
48 assertTrue(res.contains("b"));
49 assertTrue(res.contains(","));
50 }
51
52 @Test
53 public void testStringParserEscapedEscapedChar() {
54 AvoidDuplicateLiteralsRule.ExceptionParser p = new AvoidDuplicateLiteralsRule.ExceptionParser(',');
55 Set res = p.parse("a,b,\\\\");
56 assertEquals(3, res.size());
57 assertTrue(res.contains("a"));
58 assertTrue(res.contains("b"));
59 assertTrue(res.contains("\\"));
60 }
61
62 public static junit.framework.Test suite() {
63 return new junit.framework.JUnit4TestAdapter(AvoidDuplicateLiteralsRuleTest.class);
64 }
65 }