View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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      @Override
38      protected String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/checks/coding/multiplestringliterals";
40      }
41  
42      @Test
43      public void testIt() throws Exception {
44          final DefaultConfiguration checkConfig =
45              createModuleConfig(MultipleStringLiteralsCheck.class);
46          checkConfig.addAttribute("allowedDuplicates", "2");
47          checkConfig.addAttribute("ignoreStringsRegexp", "");
48  
49          final String[] expected = {
50              "5:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
51              "8:17: " + getCheckMessage(MSG_KEY, "\"\"", 4),
52              "10:23: " + getCheckMessage(MSG_KEY, "\", \"", 3),
53          };
54  
55          verify(checkConfig,
56                 getPath("InputMultipleStringLiterals.java"),
57                 expected);
58      }
59  
60      @Test
61      public void testItIgnoreEmpty() throws Exception {
62          final DefaultConfiguration checkConfig =
63              createModuleConfig(MultipleStringLiteralsCheck.class);
64          checkConfig.addAttribute("allowedDuplicates", "2");
65  
66          final String[] expected = {
67              "5:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
68              "10:23: " + getCheckMessage(MSG_KEY, "\", \"", 3),
69          };
70  
71          verify(checkConfig,
72                 getPath("InputMultipleStringLiterals.java"),
73                 expected);
74      }
75  
76      @Test
77      public void testMultipleInputs() throws Exception {
78          final DefaultConfiguration checkConfig =
79              createModuleConfig(MultipleStringLiteralsCheck.class);
80          checkConfig.addAttribute("allowedDuplicates", "2");
81  
82          final String firstInput = getPath("InputMultipleStringLiterals.java");
83          final String secondInput = getPath("InputMultipleStringLiteralsNoWarnings.java");
84  
85          final File[] inputs = {new File(firstInput), new File(secondInput)};
86  
87          final List<String> expectedFirstInput = Arrays.asList(
88              "5:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
89              "10:23: " + getCheckMessage(MSG_KEY, "\", \"", 3)
90          );
91          final List<String> expectedSecondInput = Arrays.asList(CommonUtils.EMPTY_STRING_ARRAY);
92  
93          verify(createChecker(checkConfig), inputs,
94              ImmutableMap.of(firstInput, expectedFirstInput,
95                  secondInput, expectedSecondInput));
96      }
97  
98      @Test
99      public void testItIgnoreEmptyAndComspace() throws Exception {
100         final DefaultConfiguration checkConfig =
101             createModuleConfig(MultipleStringLiteralsCheck.class);
102         checkConfig.addAttribute("allowedDuplicates", "2");
103         checkConfig.addAttribute("ignoreStringsRegexp", "^((\"\")|(\", \"))$");
104 
105         final String[] expected = {
106             "5:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
107         };
108 
109         verify(checkConfig,
110                getPath("InputMultipleStringLiterals.java"),
111                expected);
112     }
113 
114     @Test
115     public void testItWithoutIgnoringAnnotations() throws Exception {
116         final DefaultConfiguration checkConfig =
117             createModuleConfig(MultipleStringLiteralsCheck.class);
118         checkConfig.addAttribute("allowedDuplicates", "3");
119         checkConfig.addAttribute("ignoreOccurrenceContext", "");
120 
121         final String[] expected = {
122             "19:23: " + getCheckMessage(MSG_KEY, "\"unchecked\"", 4),
123         };
124 
125         verify(checkConfig,
126                getPath("InputMultipleStringLiterals.java"),
127                expected);
128     }
129 
130     @Test
131     public void testTokensNotNull() {
132         final MultipleStringLiteralsCheck check = new MultipleStringLiteralsCheck();
133         Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
134         Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
135         Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
136     }
137 
138     @Test
139     public void testDefaultConfiguration() throws Exception {
140         final DefaultConfiguration checkConfig =
141             createModuleConfig(MultipleStringLiteralsCheck.class);
142         final String[] expected = {
143             "5:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
144             "7:17: " + getCheckMessage(MSG_KEY, "\"DoubleString\"", 2),
145             "10:23: " + getCheckMessage(MSG_KEY, "\", \"", 3),
146         };
147 
148         createChecker(checkConfig);
149         verify(checkConfig,
150             getPath("InputMultipleStringLiterals.java"),
151             expected);
152     }
153 
154     @Test
155     public void testIgnores() throws Exception {
156         final DefaultConfiguration checkConfig =
157             createModuleConfig(MultipleStringLiteralsCheck.class);
158         checkConfig.addAttribute("ignoreStringsRegexp", null);
159         checkConfig.addAttribute("ignoreOccurrenceContext", "VARIABLE_DEF");
160         final String[] expected = {
161             "19:23: " + getCheckMessage(MSG_KEY, "\"unchecked\"", 4),
162         };
163 
164         createChecker(checkConfig);
165         verify(checkConfig,
166             getPath("InputMultipleStringLiterals.java"),
167             expected);
168     }
169 }