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.imports;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
24  
25  import java.io.File;
26  import java.util.Arrays;
27  import java.util.List;
28  
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.api.TokenTypes;
35  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
36  
37  public class UnusedImportsCheckTest extends AbstractModuleTestSupport {
38  
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/imports/unusedimports";
42      }
43  
44      @Test
45      public void testReferencedStateIsCleared() throws Exception {
46          final DefaultConfiguration checkConfig = createModuleConfig(UnusedImportsCheck.class);
47          final String inputWithoutWarnings = getPath("InputUnusedImportsWithoutWarnings.java");
48          final String inputWithWarnings = getPath("InputUnusedImportsCheckClearState.java");
49          final List<String> expectedFirstInput = Arrays.asList(CommonUtils.EMPTY_STRING_ARRAY);
50          final List<String> expectedSecondInput = Arrays.asList(
51                  "3:8: " + getCheckMessage(MSG_KEY, "java.util.Arrays"),
52                  "4:8: " + getCheckMessage(MSG_KEY, "java.util.List"),
53                  "5:8: " + getCheckMessage(MSG_KEY, "java.util.Set")
54          );
55          final File[] inputsWithWarningsFirst =
56              {new File(inputWithWarnings), new File(inputWithoutWarnings)};
57          final File[] inputsWithoutWarningFirst =
58              {new File(inputWithoutWarnings), new File(inputWithWarnings)};
59  
60          verify(createChecker(checkConfig), inputsWithWarningsFirst, ImmutableMap.of(
61                  inputWithoutWarnings, expectedFirstInput,
62                  inputWithWarnings, expectedSecondInput));
63          verify(createChecker(checkConfig), inputsWithoutWarningFirst, ImmutableMap.of(
64                  inputWithoutWarnings, expectedFirstInput,
65                  inputWithWarnings, expectedSecondInput));
66      }
67  
68      @Test
69      public void testWithoutProcessJavadoc() throws Exception {
70          final DefaultConfiguration checkConfig = createModuleConfig(UnusedImportsCheck.class);
71          checkConfig.addAttribute("processJavadoc", "false");
72          final String[] expected = {
73              "8:45: " + getCheckMessage(MSG_KEY,
74                  "com.puppycrawl.tools.checkstyle.checks."
75                  + "imports.unusedimports.InputUnusedImportsBug"),
76              "11:8: " + getCheckMessage(MSG_KEY, "java.lang.String"),
77              "13:8: " + getCheckMessage(MSG_KEY, "java.util.List"),
78              "14:8: " + getCheckMessage(MSG_KEY, "java.util.List"),
79              "17:8: " + getCheckMessage(MSG_KEY, "java.util.Enumeration"),
80              "20:8: " + getCheckMessage(MSG_KEY, "javax.swing.JToggleButton"),
81              "22:8: " + getCheckMessage(MSG_KEY, "javax.swing.BorderFactory"),
82              "27:15: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
83              //"29:8: Unused import - java.awt.Component.", // Should be detected
84              "30:8: " + getCheckMessage(MSG_KEY, "java.awt.Graphics2D"),
85              "31:8: " + getCheckMessage(MSG_KEY, "java.awt.HeadlessException"),
86              "32:8: " + getCheckMessage(MSG_KEY, "java.awt.Label"),
87              "33:8: " + getCheckMessage(MSG_KEY, "java.util.Date"),
88              "34:8: " + getCheckMessage(MSG_KEY, "java.util.Calendar"),
89              "35:8: " + getCheckMessage(MSG_KEY, "java.util.BitSet"),
90              "37:8: " + getCheckMessage(MSG_KEY, "com.puppycrawl.tools.checkstyle.Checker"),
91              "38:8: " + getCheckMessage(MSG_KEY, "com.puppycrawl.tools.checkstyle.CheckerTest"),
92              "39:8: " + getCheckMessage(MSG_KEY, "com.puppycrawl.tools.checkstyle.Definitions"),
93              "40:8: " + getCheckMessage(MSG_KEY,
94                  "com.puppycrawl.tools.checkstyle.checks.imports.unusedimports."
95                          + "InputUnusedImports15Extensions"),
96              "41:8: " + getCheckMessage(MSG_KEY,
97                  "com.puppycrawl.tools.checkstyle.ConfigurationLoaderTest"),
98              "42:8: " + getCheckMessage(MSG_KEY,
99                  "com.puppycrawl.tools.checkstyle.PackageNamesLoader"),
100             "43:8: " + getCheckMessage(MSG_KEY,
101                 "com.puppycrawl.tools.checkstyle.DefaultConfiguration"),
102             "44:8: " + getCheckMessage(MSG_KEY, "com.puppycrawl.tools.checkstyle.DefaultLogger"),
103         };
104         verify(checkConfig, getPath("InputUnusedImports.java"), expected);
105     }
106 
107     @Test
108     public void testProcessJavadoc() throws Exception {
109         final DefaultConfiguration checkConfig = createModuleConfig(UnusedImportsCheck.class);
110         final String[] expected = {
111             "8:45: " + getCheckMessage(MSG_KEY,
112                 "com.puppycrawl.tools.checkstyle.checks."
113                         + "imports.unusedimports.InputUnusedImportsBug"),
114             "11:8: " + getCheckMessage(MSG_KEY, "java.lang.String"),
115             "13:8: " + getCheckMessage(MSG_KEY, "java.util.List"),
116             "14:8: " + getCheckMessage(MSG_KEY, "java.util.List"),
117             "17:8: " + getCheckMessage(MSG_KEY, "java.util.Enumeration"),
118             "20:8: " + getCheckMessage(MSG_KEY, "javax.swing.JToggleButton"),
119             "22:8: " + getCheckMessage(MSG_KEY, "javax.swing.BorderFactory"),
120             "27:15: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
121             //"29:8: Unused import - java.awt.Component.", // Should be detected
122             "32:8: " + getCheckMessage(MSG_KEY, "java.awt.Label"),
123             "44:8: " + getCheckMessage(MSG_KEY, "com.puppycrawl.tools.checkstyle.DefaultLogger"),
124         };
125         verify(checkConfig, getPath("InputUnusedImports.java"), expected);
126     }
127 
128     @Test
129     public void testProcessJavadocWithLinkTag() throws Exception {
130         final DefaultConfiguration checkConfig = createModuleConfig(UnusedImportsCheck.class);
131         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
132         verify(checkConfig, getPath("InputUnusedImportsWithValueTag.java"), expected);
133     }
134 
135     @Test
136     public void testProcessJavadocWithBlockTagContainingMethodParameters() throws Exception {
137         final DefaultConfiguration checkConfig = createModuleConfig(UnusedImportsCheck.class);
138         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
139         verify(checkConfig, getPath("InputUnusedImportsWithBlockMethodParameters.java"), expected);
140     }
141 
142     @Test
143     public void testAnnotations() throws Exception {
144         final DefaultConfiguration checkConfig = createModuleConfig(UnusedImportsCheck.class);
145         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
146         verify(checkConfig, getNonCompilablePath("InputUnusedImportsAnnotations.java"), expected);
147     }
148 
149     @Test
150     public void testBug() throws Exception {
151         final DefaultConfiguration checkConfig = createModuleConfig(UnusedImportsCheck.class);
152         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
153         verify(checkConfig, getPath("InputUnusedImportsBug.java"), expected);
154     }
155 
156     @Test
157     public void testNewlinesInsideTags() throws Exception {
158         final DefaultConfiguration checkConfig = createModuleConfig(UnusedImportsCheck.class);
159         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
160         verify(checkConfig, getPath("InputUnusedImportsWithNewlinesInsideTags.java"), expected);
161     }
162 
163     @Test
164     public void testGetRequiredTokens() {
165         final UnusedImportsCheck testCheckObject =
166                 new UnusedImportsCheck();
167         final int[] actual = testCheckObject.getRequiredTokens();
168         final int[] expected = {
169             TokenTypes.IDENT,
170             TokenTypes.IMPORT,
171             TokenTypes.STATIC_IMPORT,
172             // Definitions that may contain Javadoc...
173             TokenTypes.PACKAGE_DEF,
174             TokenTypes.ANNOTATION_DEF,
175             TokenTypes.ANNOTATION_FIELD_DEF,
176             TokenTypes.ENUM_DEF,
177             TokenTypes.ENUM_CONSTANT_DEF,
178             TokenTypes.CLASS_DEF,
179             TokenTypes.INTERFACE_DEF,
180             TokenTypes.METHOD_DEF,
181             TokenTypes.CTOR_DEF,
182             TokenTypes.VARIABLE_DEF,
183         };
184 
185         assertArrayEquals("Default required tokens are invalid", expected, actual);
186     }
187 
188     @Test
189     public void testGetAcceptableTokens() {
190         final UnusedImportsCheck testCheckObject =
191                 new UnusedImportsCheck();
192         final int[] actual = testCheckObject.getAcceptableTokens();
193         final int[] expected = {
194             TokenTypes.IDENT,
195             TokenTypes.IMPORT,
196             TokenTypes.STATIC_IMPORT,
197             // Definitions that may contain Javadoc...
198             TokenTypes.PACKAGE_DEF,
199             TokenTypes.ANNOTATION_DEF,
200             TokenTypes.ANNOTATION_FIELD_DEF,
201             TokenTypes.ENUM_DEF,
202             TokenTypes.ENUM_CONSTANT_DEF,
203             TokenTypes.CLASS_DEF,
204             TokenTypes.INTERFACE_DEF,
205             TokenTypes.METHOD_DEF,
206             TokenTypes.CTOR_DEF,
207             TokenTypes.VARIABLE_DEF,
208         };
209 
210         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
211     }
212 
213     @Test
214     public void testFileInUnnamedPackage() throws Exception {
215         final DefaultConfiguration checkConfig = createModuleConfig(UnusedImportsCheck.class);
216         final String[] expected = {
217             "5:8: " + getCheckMessage(MSG_KEY, "java.util.Arrays"),
218             "6:8: " + getCheckMessage(MSG_KEY, "java.lang.String"),
219         };
220         verify(checkConfig, getNonCompilablePath("InputUnusedImportsFileInUnnamedPackage.java"),
221             expected);
222     }
223 
224     @Test
225     public void testImportsFromJavaLang() throws Exception {
226         final DefaultConfiguration checkConfig = createModuleConfig(UnusedImportsCheck.class);
227         final String[] expected = {
228             "3:8: " + getCheckMessage(MSG_KEY, "java.lang.String"),
229             "4:8: " + getCheckMessage(MSG_KEY, "java.lang.Math"),
230             "5:8: " + getCheckMessage(MSG_KEY, "java.lang.Class"),
231             "6:8: " + getCheckMessage(MSG_KEY, "java.lang.Exception"),
232             "7:8: " + getCheckMessage(MSG_KEY, "java.lang.Runnable"),
233             "8:8: " + getCheckMessage(MSG_KEY, "java.lang.RuntimeException"),
234             "9:8: " + getCheckMessage(MSG_KEY, "java.lang.ProcessBuilder"),
235             "10:8: " + getCheckMessage(MSG_KEY, "java.lang.Double"),
236             "11:8: " + getCheckMessage(MSG_KEY, "java.lang.Integer"),
237             "12:8: " + getCheckMessage(MSG_KEY, "java.lang.Float"),
238             "13:8: " + getCheckMessage(MSG_KEY, "java.lang.Short"),
239         };
240         verify(checkConfig, getPath("InputUnusedImportsFromJavaLang.java"), expected);
241     }
242 
243     @Test
244     public void testImportsJavadocQualifiedName() throws Exception {
245         final DefaultConfiguration checkConfig = createModuleConfig(UnusedImportsCheck.class);
246         final String[] expected = {
247             "4:8: " + getCheckMessage(MSG_KEY, "java.util.List"),
248         };
249         verify(checkConfig, getPath("InputUnusedImportsJavadocQualifiedName.java"), expected);
250     }
251 
252 }