View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck.MSG_DUPLICATE;
24  import static com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck.MSG_LANG;
25  import static com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck.MSG_SAME;
26  
27  import java.io.File;
28  import java.util.Arrays;
29  import java.util.List;
30  
31  import org.junit.jupiter.api.Test;
32  
33  import com.google.common.collect.ImmutableMap;
34  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
36  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
37  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
38  
39  public class RedundantImportCheckTest
40      extends AbstractModuleTestSupport {
41  
42      @Override
43      protected String getPackageLocation() {
44          return "com/puppycrawl/tools/checkstyle/checks/imports/redundantimport";
45      }
46  
47      @Test
48      public void testGetRequiredTokens() {
49          final RedundantImportCheck checkObj = new RedundantImportCheck();
50          final int[] expected = {
51              TokenTypes.IMPORT,
52              TokenTypes.STATIC_IMPORT,
53              TokenTypes.PACKAGE_DEF,
54              TokenTypes.MODULE_IMPORT,
55          };
56          assertWithMessage("Default required tokens are invalid")
57              .that(checkObj.getRequiredTokens())
58              .isEqualTo(expected);
59      }
60  
61      @Test
62      public void testStateIsClearedOnBeginTree1()
63              throws Exception {
64          final DefaultConfiguration checkConfig = createModuleConfig(RedundantImportCheck.class);
65          final String inputWithWarnings = getPath("InputRedundantImportCheckClearState.java");
66          final String inputWithoutWarnings = getPath("InputRedundantImportWithoutWarnings.java");
67          final List<String> expectedFirstInput = Arrays.asList(
68              "10:1: " + getCheckMessage(MSG_DUPLICATE, 9, "java.util.Arrays.asList"),
69              "13:1: " + getCheckMessage(MSG_DUPLICATE, 12, "java.util.List")
70          );
71          final List<String> expectedSecondInput = Arrays.asList(CommonUtil.EMPTY_STRING_ARRAY);
72          final File[] inputs = {new File(inputWithWarnings), new File(inputWithoutWarnings)};
73  
74          verify(createChecker(checkConfig), inputs, ImmutableMap.of(
75              inputWithWarnings, expectedFirstInput,
76              inputWithoutWarnings, expectedSecondInput));
77      }
78  
79      @Test
80      public void testWithChecker()
81              throws Exception {
82          final String[] expected = {
83              "9:1: " + getCheckMessage(MSG_SAME,
84                  "com.puppycrawl.tools.checkstyle.checks.imports.redundantimport.*"),
85              "10:1: " + getCheckMessage(MSG_SAME,
86                  "com.puppycrawl.tools.checkstyle.checks.imports.redundantimport."
87                          + "InputRedundantImportBug"),
88              "12:1: " + getCheckMessage(MSG_LANG, "java.lang.*"),
89              "13:1: " + getCheckMessage(MSG_LANG, "java.lang.String"),
90              "16:1: " + getCheckMessage(MSG_DUPLICATE, 15, "java.util.List"),
91              "28:1: " + getCheckMessage(MSG_DUPLICATE, 27, "javax.swing.WindowConstants.*"),
92          };
93          verifyWithInlineConfigParser(
94                  getPath("InputRedundantImportWithChecker.java"), expected);
95      }
96  
97      @Test
98      public void testUnnamedPackage()
99              throws Exception {
100         final String[] expected = {
101             "10:1: " + getCheckMessage(MSG_DUPLICATE, 9, "java.util.List"),
102             "12:1: " + getCheckMessage(MSG_LANG, "java.lang.String"),
103         };
104         verifyWithInlineConfigParser(
105                 getNonCompilablePath("InputRedundantImport_UnnamedPackage.java"),
106             expected);
107     }
108 
109     @Test
110     public void testGetAcceptableTokens() {
111         final RedundantImportCheck testCheckObject =
112                 new RedundantImportCheck();
113         final int[] actual = testCheckObject.getAcceptableTokens();
114         final int[] expected = {
115             TokenTypes.IMPORT,
116             TokenTypes.STATIC_IMPORT,
117             TokenTypes.PACKAGE_DEF,
118             TokenTypes.MODULE_IMPORT,
119         };
120 
121         assertWithMessage("Default acceptable tokens are invalid")
122             .that(actual)
123             .isEqualTo(expected);
124     }
125 
126     @Test
127     public void testBeginTreePackage() throws Exception {
128         final String file1 = getPath("InputRedundantImportCheckClearState.java");
129         final String file2 = getPath("InputRedundantImportWithoutPackage.java");
130         final List<String> expectedFirstInput = Arrays.asList(
131             "10:1: " + getCheckMessage(MSG_DUPLICATE, 9, "java.util.Arrays.asList"),
132             "13:1: " + getCheckMessage(MSG_DUPLICATE, 12, "java.util.List")
133         );
134         final List<String> expectedSecondInput = Arrays.asList(
135             "9:1: " + getCheckMessage(MSG_LANG, "java.lang.*"),
136             "10:1: " + getCheckMessage(MSG_LANG, "java.lang.String")
137         );
138         verifyWithInlineConfigParser(file1, file2, expectedFirstInput, expectedSecondInput);
139     }
140 
141     @Test
142     public void testModuleImportBuiltInModules() throws Exception {
143         final String[] expected = {
144             "11:1: " + getCheckMessage(MSG_DUPLICATE, 10, "java.base"),
145             "13:1: " + getCheckMessage(MSG_DUPLICATE, 12, "java.logging"),
146         };
147         verifyWithInlineConfigParser(
148                 getNonCompilablePath("InputRedundantImportBuiltInModules.java"), expected);
149     }
150 
151     @Test
152     public void testModuleImportCustomModules() throws Exception {
153         final String[] expected = {
154             "11:1: " + getCheckMessage(MSG_DUPLICATE, 10, "moduleA"),
155             "13:1: " + getCheckMessage(MSG_DUPLICATE, 12, "moduleB"),
156         };
157         verifyWithInlineConfigParser(
158                 getNonCompilablePath("InputRedundantImportCustomModules.java"), expected);
159     }
160 }