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.AvoidStaticImportCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  
30  public class AvoidStaticImportCheckTest
31      extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/imports/avoidstaticimport";
36      }
37  
38      @Test
39      public void testGetRequiredTokens() {
40          final AvoidStaticImportCheck checkObj = new AvoidStaticImportCheck();
41          final int[] expected = {TokenTypes.STATIC_IMPORT};
42          assertWithMessage("Default required tokens are invalid")
43              .that(checkObj.getRequiredTokens())
44              .isEqualTo(expected);
45      }
46  
47      @Test
48      public void testDefaultOperation()
49              throws Exception {
50          final String[] expected = {
51              "24:27: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
52              "26:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
53              "27:27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
54              "28:27: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
55              "29:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.E"),
56              "30:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
57              "31:113: " + getCheckMessage(MSG_KEY,
58                      "com.puppycrawl.tools.checkstyle.checks.imports."
59                      + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
60              "32:124: " + getCheckMessage(MSG_KEY,
61                      "com.puppycrawl.tools.checkstyle.checks.imports."
62                      + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.one"),
63          };
64  
65          verifyWithInlineConfigParser(
66                  getPath("InputAvoidStaticImportDefault.java"), expected);
67      }
68  
69      @Test
70      public void testStarExcludes()
71              throws Exception {
72          // allow the "java.io.File.*" AND "sun.net.ftpclient.FtpClient.*" star imports
73          final String[] expected = {
74              "26:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
75              "29:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.E"),
76              "30:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
77              "31:113: " + getCheckMessage(MSG_KEY,
78                      "com.puppycrawl.tools.checkstyle.checks.imports."
79                      + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
80              "32:124: " + getCheckMessage(MSG_KEY,
81                      "com.puppycrawl.tools.checkstyle.checks.imports."
82                      + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.one"),
83          };
84          verifyWithInlineConfigParser(
85                  getPath("InputAvoidStaticImportDefault2.java"), expected);
86      }
87  
88      @Test
89      public void testMemberExcludes()
90              throws Exception {
91          // allow the java.io.File.listRoots and java.lang.Math.E member imports
92          final String[] expected = {
93              "26:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
94              "27:27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
95              "28:27: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
96              "30:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
97              "31:113: " + getCheckMessage(MSG_KEY,
98                      "com.puppycrawl.tools.checkstyle.checks.imports."
99                      + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
100             "32:124: " + getCheckMessage(MSG_KEY,
101                     "com.puppycrawl.tools.checkstyle.checks.imports."
102                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.one"),
103         };
104         verifyWithInlineConfigParser(
105                 getPath("InputAvoidStaticImportDefault3.java"), expected);
106     }
107 
108     @Test
109     public void testBogusMemberExcludes()
110             throws Exception {
111         // should NOT mask anything
112         final String[] expected = {
113             "26:27: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
114             "28:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
115             "29:27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
116             "30:27: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
117             "31:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.E"),
118             "32:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
119             "33:113: " + getCheckMessage(MSG_KEY,
120                     "com.puppycrawl.tools.checkstyle.checks.imports."
121                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
122             "34:124: " + getCheckMessage(MSG_KEY,
123                     "com.puppycrawl.tools.checkstyle.checks.imports."
124                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.one"),
125         };
126         verifyWithInlineConfigParser(
127                 getPath("InputAvoidStaticImportDefault4.java"), expected);
128     }
129 
130     @Test
131     public void testInnerClassMemberExcludesStar()
132             throws Exception {
133         // should mask com.puppycrawl.tools.checkstyle.imports.avoidstaticimport.
134         // InputAvoidStaticImportNestedClass.InnerClass.one
135         final String[] expected = {
136             "25:27: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
137             "27:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
138             "28:27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
139             "29:27: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
140             "30:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.E"),
141             "31:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
142             "32:113: " + getCheckMessage(MSG_KEY,
143                     "com.puppycrawl.tools.checkstyle.checks.imports."
144                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
145         };
146         verifyWithInlineConfigParser(
147                 getPath("InputAvoidStaticImportDefault5.java"), expected);
148     }
149 
150     @Test
151     public void testGetAcceptableTokens() {
152         final AvoidStaticImportCheck testCheckObject =
153                 new AvoidStaticImportCheck();
154         final int[] actual = testCheckObject.getAcceptableTokens();
155         final int[] expected = {TokenTypes.STATIC_IMPORT};
156 
157         assertWithMessage("Default acceptable tokens are invalid")
158             .that(actual)
159             .isEqualTo(expected);
160     }
161 
162 }
163