1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.pmd.rules.strings;
17
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.never;
20 import static org.mockito.Mockito.spy;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 import net.sourceforge.pmd.RuleContext;
24 import net.sourceforge.pmd.ast.ASTLiteral;
25 import net.sourceforge.pmd.symboltable.ClassScope;
26 import net.sourceforge.pmd.symboltable.SourceFileScope;
27
28 import org.junit.Before;
29 import org.junit.Test;
30
31 import de.smartics.testdoc.annotations.Uut;
32
33 public class StringLiteralEncodingRuleTest
34 {
35
36
37
38
39
40
41 @Uut
42 private StringLiteralEncodingRule uut;
43
44
45
46
47
48
49
50 @Before
51 public void setUp()
52 {
53 uut = new StringLiteralEncodingRule()
54 {
55
56 };
57 }
58
59
60
61 private void runTest(final String literal, final boolean expectDetection)
62 {
63 final RuleContext ruleContext = new RuleContext();
64 final RuleContext data = spy(ruleContext);
65
66 final ASTLiteral node = mock(ASTLiteral.class);
67 when(node.isStringLiteral()).thenReturn(true);
68 when(node.getImage()).thenReturn(literal);
69 final ClassScope classScope = new ClassScope("de.smartics.test.Dummy");
70 classScope.setParent(new SourceFileScope("de.smartics.test"));
71 when(node.getScope()).thenReturn(classScope);
72
73 uut.visit(node, data);
74
75 if (expectDetection)
76 {
77 verify(data).getReport();
78 }
79 else
80 {
81 verify(data, never()).getReport();
82 }
83 }
84
85
86
87 @Test
88 public void detectsIsoDifferentEncoding()
89 {
90 final String literal = "รค";
91 runTest(literal, true);
92 }
93
94
95
96
97
98
99
100
101 }