View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.coding;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.coding.MultipleStringLiteralsCheck.MSG_KEY;
23  
24  import java.io.File;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.junit.Assert;
29  import org.junit.Test;
30  
31  import com.google.common.collect.ImmutableMap;
32  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
33  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
34  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
35  
36  public class MultipleStringLiteralsCheckTest extends AbstractModuleTestSupport {
37  
38      @Override
39      protected String getPackageLocation() {
40          return "com/puppycrawl/tools/checkstyle/checks/coding/multiplestringliterals";
41      }
42  
43      @Test
44      public void testIt() throws Exception {
45          final DefaultConfiguration checkConfig =
46              createModuleConfig(MultipleStringLiteralsCheck.class);
47          checkConfig.addAttribute("allowedDuplicates", "2");
48          checkConfig.addAttribute("ignoreStringsRegexp", "");
49  
50          final String[] expected = {
51              "5:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
52              "8:17: " + getCheckMessage(MSG_KEY, "\"\"", 4),
53              "10:23: " + getCheckMessage(MSG_KEY, "\", \"", 3),
54          };
55  
56          verify(checkConfig,
57                 getPath("InputMultipleStringLiterals.java"),
58                 expected);
59      }
60  
61      @Test
62      public void testItIgnoreEmpty() throws Exception {
63          final DefaultConfiguration checkConfig =
64              createModuleConfig(MultipleStringLiteralsCheck.class);
65          checkConfig.addAttribute("allowedDuplicates", "2");
66  
67          final String[] expected = {
68              "5:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
69              "10:23: " + getCheckMessage(MSG_KEY, "\", \"", 3),
70          };
71  
72          verify(checkConfig,
73                 getPath("InputMultipleStringLiterals.java"),
74                 expected);
75      }
76  
77      @Test
78      public void testMultipleInputs() throws Exception {
79          final DefaultConfiguration checkConfig =
80              createModuleConfig(MultipleStringLiteralsCheck.class);
81          checkConfig.addAttribute("allowedDuplicates", "2");
82  
83          final String firstInput = getPath("InputMultipleStringLiterals.java");
84          final String secondInput = getPath("InputMultipleStringLiteralsNoWarnings.java");
85  
86          final File[] inputs = {new File(firstInput), new File(secondInput)};
87  
88          final List<String> expectedFirstInput = Arrays.asList(
89              "5:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
90              "10:23: " + getCheckMessage(MSG_KEY, "\", \"", 3)
91          );
92          final List<String> expectedSecondInput = Arrays.asList(CommonUtils.EMPTY_STRING_ARRAY);
93  
94          verify(createChecker(checkConfig), inputs,
95              ImmutableMap.of(firstInput, expectedFirstInput,
96                  secondInput, expectedSecondInput));
97      }
98  
99      @Test
100     public void testItIgnoreEmptyAndComspace() throws Exception {
101         final DefaultConfiguration checkConfig =
102             createModuleConfig(MultipleStringLiteralsCheck.class);
103         checkConfig.addAttribute("allowedDuplicates", "2");
104         checkConfig.addAttribute("ignoreStringsRegexp", "^((\"\")|(\", \"))$");
105 
106         final String[] expected = {
107             "5:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
108         };
109 
110         verify(checkConfig,
111                getPath("InputMultipleStringLiterals.java"),
112                expected);
113     }
114 
115     @Test
116     public void testItWithoutIgnoringAnnotations() throws Exception {
117         final DefaultConfiguration checkConfig =
118             createModuleConfig(MultipleStringLiteralsCheck.class);
119         checkConfig.addAttribute("allowedDuplicates", "3");
120         checkConfig.addAttribute("ignoreOccurrenceContext", "");
121 
122         final String[] expected = {
123             "19:23: " + getCheckMessage(MSG_KEY, "\"unchecked\"", 4),
124         };
125 
126         verify(checkConfig,
127                getPath("InputMultipleStringLiterals.java"),
128                expected);
129     }
130 
131     @Test
132     public void testTokensNotNull() {
133         final MultipleStringLiteralsCheck check = new MultipleStringLiteralsCheck();
134         Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
135         Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
136         Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
137     }
138 
139     @Test
140     public void testDefaultConfiguration() throws Exception {
141         final DefaultConfiguration checkConfig =
142             createModuleConfig(MultipleStringLiteralsCheck.class);
143         final String[] expected = {
144             "5:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
145             "7:17: " + getCheckMessage(MSG_KEY, "\"DoubleString\"", 2),
146             "10:23: " + getCheckMessage(MSG_KEY, "\", \"", 3),
147         };
148 
149         createChecker(checkConfig);
150         verify(checkConfig,
151             getPath("InputMultipleStringLiterals.java"),
152             expected);
153     }
154 
155     @Test
156     public void testIgnores() throws Exception {
157         final DefaultConfiguration checkConfig =
158             createModuleConfig(MultipleStringLiteralsCheck.class);
159         checkConfig.addAttribute("ignoreStringsRegexp", null);
160         checkConfig.addAttribute("ignoreOccurrenceContext", "VARIABLE_DEF");
161         final String[] expected = {
162             "19:23: " + getCheckMessage(MSG_KEY, "\"unchecked\"", 4),
163         };
164 
165         createChecker(checkConfig);
166         verify(checkConfig,
167             getPath("InputMultipleStringLiterals.java"),
168             expected);
169     }
170 
171 }