View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.AvoidStarImportCheck.MSG_COUNT;
24  import static com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck.MSG_KEY;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31  
32  public class AvoidStarImportCheckTest
33      extends AbstractModuleTestSupport {
34  
35      @Override
36      public String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/imports/avoidstarimport";
38      }
39  
40      @Test
41      public void testDefaultOperation()
42              throws Exception {
43          final String[] expected = {
44              "12:15: " + getCheckMessage(MSG_KEY, "java.io.*"),
45              "13:17: " + getCheckMessage(MSG_KEY, "java.lang.*"),
46              "28:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
47              "29:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
48              "31:27: " + getCheckMessage(MSG_KEY, "java.io.File.*"),
49          };
50  
51          verifyWithInlineConfigParser(
52                  getPath("InputAvoidStarImportDefault.java"),
53                  expected);
54      }
55  
56      @Test
57      public void testExcludes()
58              throws Exception {
59          // allow the java.io/java.lang,javax.swing.WindowConstants star imports
60          final String[] expected2 = {
61              "31:27: " + getCheckMessage(MSG_KEY, "java.io.File.*"),
62          };
63          verifyWithInlineConfigParser(
64                  getPath("InputAvoidStarImportExcludes.java"),
65                  expected2);
66      }
67  
68      @Test
69      public void testAllowClassImports() throws Exception {
70          // allow all class star imports
71          final String[] expected2 = {
72              "28:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
73              "29:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
74              "31:27: " + getCheckMessage(MSG_KEY, "java.io.File.*"), };
75          verifyWithInlineConfigParser(
76                  getPath("InputAvoidStarImportAllowClass.java"), expected2);
77      }
78  
79      @Test
80      public void testAllowStaticMemberImports() throws Exception {
81          // allow all static star imports
82          final String[] expected2 = {
83              "12:15: " + getCheckMessage(MSG_KEY, "java.io.*"),
84              "13:17: " + getCheckMessage(MSG_KEY, "java.lang.*"),
85          };
86          verifyWithInlineConfigParser(
87                  getPath("InputAvoidStarImportAllowStaticMember.java"), expected2);
88      }
89  
90      @Test
91      public void testGetAcceptableTokens() {
92          final AvoidStarImportCheck testCheckObject =
93                  new AvoidStarImportCheck();
94          final int[] actual = testCheckObject.getAcceptableTokens();
95          final int[] expected = {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
96          assertWithMessage("Default acceptable tokens are invalid")
97              .that(actual)
98              .isEqualTo(expected);
99      }
100 
101     @Test
102     public void testGetRequiredTokens() {
103         final AvoidStarImportCheck testCheckObject =
104                 new AvoidStarImportCheck();
105         final int[] actual = testCheckObject.getRequiredTokens();
106         final int[] expected = {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
107 
108         assertWithMessage("Default required tokens are invalid")
109             .that(actual)
110             .isEqualTo(expected);
111     }
112 
113     @Test
114     public void testMaxAllowedStarImports() throws Exception {
115         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
116 
117         verifyWithInlineConfigParser(
118                 getPath("InputAvoidStarImportMaxAllowed.java"), expected);
119     }
120 
121     @Test
122     public void testMaxAllowedStarImports3() throws Exception {
123         final String[] expected = {
124             "13:17: " + getCheckMessage(MSG_COUNT, 1),
125         };
126 
127         verifyWithInlineConfigParser(
128                 getPath("InputAvoidStarImportMaxAllowed3.java"), expected);
129     }
130 
131     @Test
132     public void testMaxAllowedMultipleFiles() throws Exception {
133         final String[] excepted = CommonUtil.EMPTY_STRING_ARRAY;
134 
135         verifyWithInlineConfigParser(
136                 getPath("InputAvoidStarImportMaxAllowed.java"),
137                 getPath("InputAvoidStarImportMaxAllowed2.java"),
138                 excepted);
139     }
140 
141 }