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.coding;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.coding.IllegalInstantiationCheck.MSG_KEY;
23  
24  import java.io.File;
25  import java.nio.charset.StandardCharsets;
26  import java.util.SortedSet;
27  
28  import org.junit.Assert;
29  import org.junit.Test;
30  
31  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
33  import com.puppycrawl.tools.checkstyle.api.DetailAST;
34  import com.puppycrawl.tools.checkstyle.api.FileContents;
35  import com.puppycrawl.tools.checkstyle.api.FileText;
36  import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
37  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
38  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
39  
40  public class IllegalInstantiationCheckTest
41      extends AbstractModuleTestSupport {
42  
43      @Override
44      protected String getPackageLocation() {
45          return "com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation";
46      }
47  
48      @Test
49      public void testDefault() throws Exception {
50          final DefaultConfiguration checkConfig =
51              createModuleConfig(IllegalInstantiationCheck.class);
52          checkConfig.addAttribute(
53              "classes",
54              "java.lang.Boolean,"
55                  + "com.puppycrawl.tools.checkstyle.checks.coding."
56                  + "illegalinstantiation.InputModifier,"
57                  + "java.io.File,"
58                  + "java.awt.Color");
59          final String[] expected = {
60              "19:21: " + getCheckMessage(MSG_KEY, "java.lang.Boolean"),
61              "24:21: " + getCheckMessage(MSG_KEY, "java.lang.Boolean"),
62              "31:16: " + getCheckMessage(MSG_KEY, "java.lang.Boolean"),
63              "38:21: " + getCheckMessage(MSG_KEY,
64                  "com.puppycrawl.tools.checkstyle.checks.coding."
65                  + "illegalinstantiation.InputModifier"),
66              "41:18: " + getCheckMessage(MSG_KEY, "java.io.File"),
67              "44:21: " + getCheckMessage(MSG_KEY, "java.awt.Color"),
68          };
69          verify(checkConfig, getPath("InputIllegalInstantiationSemantic.java"), expected);
70      }
71  
72      @Test
73      public void testJava8() throws Exception {
74          final DefaultConfiguration checkConfig =
75                  createModuleConfig(IllegalInstantiationCheck.class);
76          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
77          verify(checkConfig,
78                  getPath("InputIllegalInstantiation.java"),
79                  expected);
80      }
81  
82      @Test
83      public void testNoPackage() throws Exception {
84          final DefaultConfiguration checkConfig =
85                  createModuleConfig(IllegalInstantiationCheck.class);
86          checkConfig.addAttribute(
87                  "classes",
88                  "java.lang.Boolean");
89          final String[] expected = {
90              "3:20: " + getCheckMessage(MSG_KEY, "java.lang.Boolean"),
91          };
92          verify(checkConfig,
93                  getNonCompilablePath("InputIllegalInstantiationNoPackage.java"),
94                  expected);
95      }
96  
97      @Test
98      public void testJavaLangPackage() throws Exception {
99          final DefaultConfiguration checkConfig =
100                 createModuleConfig(IllegalInstantiationCheck.class);
101         checkConfig.addAttribute(
102                 "classes",
103                 "java.lang.Boolean,java.lang.String");
104         final String[] expected = {
105             "4:19: " + getCheckMessage(MSG_KEY, "java.lang.Boolean"),
106             "12:20: " + getCheckMessage(MSG_KEY, "java.lang.String"),
107         };
108         verify(checkConfig,
109                 getNonCompilablePath("InputIllegalInstantiationLang.java"),
110                 expected);
111     }
112 
113     @Test
114     public void testWrongPackage() throws Exception {
115         final DefaultConfiguration checkConfig =
116                 createModuleConfig(IllegalInstantiationCheck.class);
117         checkConfig.addAttribute(
118                 "classes",
119                 "jjva.lang.Boolean,java.lang*Boolean");
120         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
121         verify(checkConfig,
122                 getNonCompilablePath("InputIllegalInstantiationLang.java"),
123                 expected);
124     }
125 
126     @Test
127     public void testNullClassLoader() throws Exception {
128         final DetailAST exprAst = new DetailAST();
129         exprAst.setType(TokenTypes.EXPR);
130 
131         final DetailAST newAst = new DetailAST();
132         newAst.setType(TokenTypes.LITERAL_NEW);
133         newAst.setLineNo(1);
134         newAst.setColumnNo(1);
135 
136         final DetailAST identAst = new DetailAST();
137         identAst.setType(TokenTypes.IDENT);
138         identAst.setText("Boolean");
139 
140         final DetailAST lparenAst = new DetailAST();
141         lparenAst.setType(TokenTypes.LPAREN);
142 
143         final DetailAST elistAst = new DetailAST();
144         elistAst.setType(TokenTypes.ELIST);
145 
146         final DetailAST rparenAst = new DetailAST();
147         rparenAst.setType(TokenTypes.RPAREN);
148 
149         exprAst.addChild(newAst);
150         newAst.addChild(identAst);
151         identAst.setNextSibling(lparenAst);
152         lparenAst.setNextSibling(elistAst);
153         elistAst.setNextSibling(rparenAst);
154 
155         final IllegalInstantiationCheck check = new IllegalInstantiationCheck();
156         final File inputFile = new File(getNonCompilablePath("InputIllegalInstantiationLang.java"));
157         check.setFileContents(new FileContents(new FileText(inputFile,
158                 StandardCharsets.UTF_8.name())));
159         check.configure(createModuleConfig(IllegalInstantiationCheck.class));
160         check.setClasses("java.lang.Boolean");
161 
162         check.visitToken(newAst);
163         final SortedSet<LocalizedMessage> messages1 = check.getMessages();
164 
165         Assert.assertEquals("No exception messages expected", 0, messages1.size());
166 
167         check.finishTree(newAst);
168         final SortedSet<LocalizedMessage> messages2 = check.getMessages();
169 
170         final LocalizedMessage addExceptionMessage = new LocalizedMessage(0,
171                 "com.puppycrawl.tools.checkstyle.checks.coding.messages", "instantiation.avoid",
172                 new String[] {"java.lang.Boolean"}, null,
173                 getClass(), null);
174         Assert.assertEquals("Invalid exception message",
175                 addExceptionMessage.getMessage(),
176             messages2.first().getMessage());
177     }
178 
179     @Test
180     public void testTokensNotNull() {
181         final IllegalInstantiationCheck check = new IllegalInstantiationCheck();
182         Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
183         Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
184         Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
185     }
186 
187     @Test
188     public void testImproperToken() {
189         final IllegalInstantiationCheck check = new IllegalInstantiationCheck();
190 
191         final DetailAST lambdaAst = new DetailAST();
192         lambdaAst.setType(TokenTypes.LAMBDA);
193 
194         try {
195             check.visitToken(lambdaAst);
196             Assert.fail("IllegalArgumentException is expected");
197         }
198         catch (IllegalArgumentException ex) {
199             // it is OK
200         }
201     }
202 
203 }