View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.AvoidStaticImportCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
24  
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  
31  public class AvoidStaticImportCheckTest
32      extends AbstractModuleTestSupport {
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          assertArrayEquals("Default required tokens are invalid",
43              expected, checkObj.getRequiredTokens());
44      }
45  
46      @Test
47      public void testDefaultOperation()
48              throws Exception {
49          final DefaultConfiguration checkConfig =
50              createModuleConfig(AvoidStaticImportCheck.class);
51          final String[] expected = {
52              "23: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
53              "25: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
54              "26: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
55              "27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
56              "28: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
57              "29: " + getCheckMessage(MSG_KEY,
58                  "com.puppycrawl.tools.checkstyle.checks.imports."
59                      + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
60              "30: " + getCheckMessage(MSG_KEY,
61                  "com.puppycrawl.tools.checkstyle.checks.imports."
62                      + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.one"),
63          };
64  
65          verify(checkConfig, getPath("InputAvoidStaticImportDefault.java"), expected);
66      }
67  
68      @Test
69      public void testStarExcludes()
70              throws Exception {
71          final DefaultConfiguration checkConfig =
72              createModuleConfig(AvoidStaticImportCheck.class);
73          checkConfig.addAttribute("excludes", "java.io.File.*,sun.net.ftpclient.FtpClient.*");
74          // allow the "java.io.File.*" AND "sun.net.ftpclient.FtpClient.*" star imports
75          final String[] expected = {
76              "25: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
77              "26: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
78              "29: " + getCheckMessage(MSG_KEY,
79                  "com.puppycrawl.tools.checkstyle.checks.imports."
80                      + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
81              "30: " + getCheckMessage(MSG_KEY,
82                  "com.puppycrawl.tools.checkstyle.checks.imports."
83                      + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.one"),
84          };
85          verify(checkConfig, getPath("InputAvoidStaticImportDefault.java"), expected);
86      }
87  
88      @Test
89      public void testMemberExcludes()
90              throws Exception {
91          final DefaultConfiguration checkConfig =
92              createModuleConfig(AvoidStaticImportCheck.class);
93          checkConfig.addAttribute("excludes", "java.io.File.listRoots");
94          // allow the java.io.File.listRoots member imports
95          final String[] expected = {
96              "25: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
97              "26: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
98              "27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
99              "28: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
100             "29: " + getCheckMessage(MSG_KEY,
101                 "com.puppycrawl.tools.checkstyle.checks.imports."
102                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
103             "30: " + getCheckMessage(MSG_KEY,
104                 "com.puppycrawl.tools.checkstyle.checks.imports."
105                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.one"),
106         };
107         verify(checkConfig, getPath("InputAvoidStaticImportDefault.java"), expected);
108     }
109 
110     @Test
111     public void testBogusMemberExcludes()
112             throws Exception {
113         final DefaultConfiguration checkConfig =
114             createModuleConfig(AvoidStaticImportCheck.class);
115 
116         // should NOT mask anything
117         checkConfig.addAttribute(
118             "excludes",
119             "java.io.File.listRoots.listRoots, javax.swing.WindowConstants, javax.swing.*,"
120             + "sun.net.ftpclient.FtpClient.*FtpClient, sun.net.ftpclient.FtpClientjunk,"
121             + " java.io.File.listRootsmorejunk");
122         final String[] expected = {
123             "23: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
124             "25: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
125             "26: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
126             "27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
127             "28: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
128             "29: " + getCheckMessage(MSG_KEY,
129                 "com.puppycrawl.tools.checkstyle.checks.imports."
130                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
131             "30: " + getCheckMessage(MSG_KEY,
132                 "com.puppycrawl.tools.checkstyle.checks.imports."
133                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.one"),
134         };
135         verify(checkConfig, getPath("InputAvoidStaticImportDefault.java"), expected);
136     }
137 
138     @Test
139     public void testInnerClassMemberExcludesStar()
140             throws Exception {
141         final DefaultConfiguration checkConfig =
142             createModuleConfig(AvoidStaticImportCheck.class);
143 
144         // should mask com.puppycrawl.tools.checkstyle.imports.avoidstaticimport.
145         // InputAvoidStaticImportNestedClass.InnerClass.one
146         checkConfig.addAttribute(
147             "excludes",
148             "com.puppycrawl.tools.checkstyle.checks.imports."
149                 + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.*");
150         final String[] expected = {
151             "23: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
152             "25: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
153             "26: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
154             "27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
155             "28: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
156             "29: " + getCheckMessage(MSG_KEY,
157                 "com.puppycrawl.tools.checkstyle.checks.imports."
158                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
159         };
160         verify(checkConfig, getPath("InputAvoidStaticImportDefault.java"), expected);
161     }
162 
163     @Test
164     public void testGetAcceptableTokens() {
165         final AvoidStaticImportCheck testCheckObject =
166                 new AvoidStaticImportCheck();
167         final int[] actual = testCheckObject.getAcceptableTokens();
168         final int[] expected = {TokenTypes.STATIC_IMPORT};
169 
170         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
171     }
172 }