View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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          };
55          assertWithMessage("Default required tokens are invalid")
56              .that(checkObj.getRequiredTokens())
57              .isEqualTo(expected);
58      }
59  
60      @Test
61      public void testStateIsClearedOnBeginTree1()
62              throws Exception {
63          final DefaultConfiguration checkConfig = createModuleConfig(RedundantImportCheck.class);
64          final String inputWithWarnings = getPath("InputRedundantImportCheckClearState.java");
65          final String inputWithoutWarnings = getPath("InputRedundantImportWithoutWarnings.java");
66          final List<String> expectedFirstInput = Arrays.asList(
67              "10:1: " + getCheckMessage(MSG_DUPLICATE, 9, "java.util.Arrays.asList"),
68              "13:1: " + getCheckMessage(MSG_DUPLICATE, 12, "java.util.List")
69          );
70          final List<String> expectedSecondInput = Arrays.asList(CommonUtil.EMPTY_STRING_ARRAY);
71          final File[] inputs = {new File(inputWithWarnings), new File(inputWithoutWarnings)};
72  
73          verify(createChecker(checkConfig), inputs, ImmutableMap.of(
74              inputWithWarnings, expectedFirstInput,
75              inputWithoutWarnings, expectedSecondInput));
76      }
77  
78      @Test
79      public void testWithChecker()
80              throws Exception {
81          final String[] expected = {
82              "9:1: " + getCheckMessage(MSG_SAME,
83                  "com.puppycrawl.tools.checkstyle.checks.imports.redundantimport.*"),
84              "10:1: " + getCheckMessage(MSG_SAME,
85                  "com.puppycrawl.tools.checkstyle.checks.imports.redundantimport."
86                          + "InputRedundantImportBug"),
87              "12:1: " + getCheckMessage(MSG_LANG, "java.lang.*"),
88              "13:1: " + getCheckMessage(MSG_LANG, "java.lang.String"),
89              "16:1: " + getCheckMessage(MSG_DUPLICATE, 15, "java.util.List"),
90              "28:1: " + getCheckMessage(MSG_DUPLICATE, 27, "javax.swing.WindowConstants.*"),
91          };
92          verifyWithInlineConfigParser(
93                  getPath("InputRedundantImportWithChecker.java"), expected);
94      }
95  
96      @Test
97      public void testUnnamedPackage()
98              throws Exception {
99          final String[] expected = {
100             "10:1: " + getCheckMessage(MSG_DUPLICATE, 9, "java.util.List"),
101             "12:1: " + getCheckMessage(MSG_LANG, "java.lang.String"),
102         };
103         verifyWithInlineConfigParser(
104                 getNonCompilablePath("InputRedundantImport_UnnamedPackage.java"),
105             expected);
106     }
107 
108     @Test
109     public void testGetAcceptableTokens() {
110         final RedundantImportCheck testCheckObject =
111                 new RedundantImportCheck();
112         final int[] actual = testCheckObject.getAcceptableTokens();
113         final int[] expected = {
114             TokenTypes.IMPORT,
115             TokenTypes.STATIC_IMPORT,
116             TokenTypes.PACKAGE_DEF,
117         };
118 
119         assertWithMessage("Default acceptable tokens are invalid")
120             .that(actual)
121             .isEqualTo(expected);
122     }
123 
124     @Test
125     public void testBeginTreePackage() throws Exception {
126         final String file1 = getPath("InputRedundantImportCheckClearState.java");
127         final String file2 = getPath("InputRedundantImportWithoutPackage.java");
128         final List<String> expectedFirstInput = Arrays.asList(
129             "10:1: " + getCheckMessage(MSG_DUPLICATE, 9, "java.util.Arrays.asList"),
130             "13:1: " + getCheckMessage(MSG_DUPLICATE, 12, "java.util.List")
131         );
132         final List<String> expectedSecondInput = Arrays.asList(
133             "9:1: " + getCheckMessage(MSG_LANG, "java.lang.*"),
134             "10:1: " + getCheckMessage(MSG_LANG, "java.lang.String")
135         );
136         verifyWithInlineConfigParser(file1, file2, expectedFirstInput, expectedSecondInput);
137     }
138 }