View Javadoc

1   /*
2    * Copyright 2011-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    // --- members --------------------------------------------------------------
40  
41    @Uut
42    private StringLiteralEncodingRule uut;
43  
44    // ****************************** Inner Classes *****************************
45  
46    // ********************************* Methods ********************************
47  
48    // --- prepare --------------------------------------------------------------
49  
50    @Before
51    public void setUp()
52    {
53      uut = new StringLiteralEncodingRule()
54      {
55  
56      };
57    }
58  
59    // --- helper ---------------------------------------------------------------
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    // --- tests ----------------------------------------------------------------
86  
87    @Test
88    public void detectsIsoDifferentEncoding()
89    {
90      final String literal = "รค";
91      runTest(literal, true);
92    }
93  
94  //  @Test
95  //  public void allowsEscapedEncoding()
96  //  {
97  //    final String literal = "\u00e4";
98  //    runTest(literal, false);
99  //  }
100 
101 }