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