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