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.utils;
21  
22  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.findTokenInAstByPredicate;
23  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
24  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.parseFile;
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertFalse;
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.fail;
29  
30  import java.io.File;
31  import java.util.Arrays;
32  import java.util.HashSet;
33  import java.util.List;
34  import java.util.Optional;
35  import java.util.Set;
36  
37  import org.junit.Test;
38  
39  import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
40  import com.puppycrawl.tools.checkstyle.api.DetailAST;
41  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
42  import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifier;
43  
44  public class CheckUtilsTest extends AbstractPathTestSupport {
45  
46      @Override
47      protected String getPackageLocation() {
48          return "com/puppycrawl/tools/checkstyle/utils/checkutils";
49      }
50  
51      @Test
52      public void testIsProperUtilsClass() throws ReflectiveOperationException {
53          assertTrue("Constructor is not private",
54                  isUtilsClassHasPrivateConstructor(CheckUtils.class, true));
55      }
56  
57      @Test
58      public void testParseDoubleWithIncorrectToken() {
59          final double parsedDouble = CheckUtils.parseDouble("1_02", TokenTypes.ASSIGN);
60          assertEquals("Invalid parse result", 0.0, parsedDouble, 0.0);
61      }
62  
63      @Test
64      public void testElseWithCurly() {
65          final DetailAST ast = new DetailAST();
66          ast.setType(TokenTypes.ASSIGN);
67          ast.setText("ASSIGN");
68          assertFalse("Invalid elseIf check result 'ASSIGN' is not 'else if'",
69                  CheckUtils.isElseIf(ast));
70  
71          final DetailAST parentAst = new DetailAST();
72          parentAst.setType(TokenTypes.LCURLY);
73          parentAst.setText("LCURLY");
74  
75          final DetailAST ifAst = new DetailAST();
76          ifAst.setType(TokenTypes.LITERAL_IF);
77          ifAst.setText("IF");
78          parentAst.addChild(ifAst);
79  
80          assertFalse("Invalid elseIf check result: 'IF' is not 'else if'",
81                  CheckUtils.isElseIf(ifAst));
82  
83          final DetailAST parentAst2 = new DetailAST();
84          parentAst2.setType(TokenTypes.SLIST);
85          parentAst2.setText("SLIST");
86  
87          parentAst2.addChild(ifAst);
88  
89          assertFalse("Invalid elseIf check result: 'SLIST' is not 'else if'",
90                  CheckUtils.isElseIf(ifAst));
91  
92          final DetailAST elseAst = new DetailAST();
93          elseAst.setType(TokenTypes.LITERAL_ELSE);
94  
95          elseAst.setFirstChild(ifAst);
96          assertTrue("Invalid elseIf check result", CheckUtils.isElseIf(ifAst));
97      }
98  
99      @Test
100     public void testEquals() {
101         final DetailAST litStatic = new DetailAST();
102         litStatic.setType(TokenTypes.LITERAL_STATIC);
103 
104         final DetailAST modifiers = new DetailAST();
105         modifiers.setType(TokenTypes.MODIFIERS);
106         modifiers.addChild(litStatic);
107 
108         final DetailAST metDef = new DetailAST();
109         metDef.setType(TokenTypes.METHOD_DEF);
110         metDef.addChild(modifiers);
111 
112         assertFalse("Invalid result: ast is not equals method",
113                 CheckUtils.isEqualsMethod(metDef));
114 
115         metDef.removeChildren();
116 
117         final DetailAST metName = new DetailAST();
118         metName.setType(TokenTypes.IDENT);
119         metName.setText("equals");
120         metDef.addChild(metName);
121 
122         final DetailAST modifiers2 = new DetailAST();
123         modifiers2.setType(TokenTypes.MODIFIERS);
124         metDef.addChild(modifiers2);
125 
126         final DetailAST parameter1 = new DetailAST();
127         final DetailAST parameter2 = new DetailAST();
128 
129         final DetailAST parameters = new DetailAST();
130         parameters.setType(TokenTypes.PARAMETERS);
131 
132         parameters.addChild(parameter2);
133 
134         parameters.addChild(parameter1);
135         metDef.addChild(parameters);
136 
137         assertFalse("Invalid result: ast is not equals method",
138                 CheckUtils.isEqualsMethod(metDef));
139     }
140 
141     @Test
142     public void testGetAccessModifierFromModifiersTokenWrongTokenType() {
143         final DetailAST modifiers = new DetailAST();
144         modifiers.setType(TokenTypes.METHOD_DEF);
145 
146         try {
147             CheckUtils.getAccessModifierFromModifiersToken(modifiers);
148             fail(IllegalArgumentException.class.getSimpleName() + " was expected.");
149         }
150         catch (IllegalArgumentException exc) {
151             final String expectedExceptionMsg = "expected non-null AST-token with type 'MODIFIERS'";
152             final String actualExceptionMsg = exc.getMessage();
153             assertEquals("Invalid exception message", expectedExceptionMsg, actualExceptionMsg);
154         }
155     }
156 
157     @Test
158     public void testGetAccessModifierFromModifiersTokenWithNullParameter() {
159         try {
160             CheckUtils.getAccessModifierFromModifiersToken(null);
161             fail(IllegalArgumentException.class.getSimpleName() + " was expected.");
162         }
163         catch (IllegalArgumentException exc) {
164             final String expectedExceptionMsg = "expected non-null AST-token with type 'MODIFIERS'";
165             final String actualExceptionMsg = exc.getMessage();
166             assertEquals("Invalid exception message", expectedExceptionMsg, actualExceptionMsg);
167         }
168     }
169 
170     @Test
171     public void testCreateFullType() throws Exception {
172         final DetailAST typeNode = getNodeFromFile(TokenTypes.TYPE);
173 
174         assertEquals("Invalid full type", "Map[13x12]",
175                 CheckUtils.createFullType(typeNode).toString());
176     }
177 
178     @Test
179     public void testCreateFullTypeOfArray() throws Exception {
180         final DetailAST arrayTypeNode = getNodeFromFile(TokenTypes.VARIABLE_DEF)
181                 .getNextSibling().getFirstChild().getNextSibling();
182 
183         assertEquals("Invalid full type", "int[14x14]",
184                 CheckUtils.createFullType(arrayTypeNode).toString());
185     }
186 
187     @Test
188     public void testGetTypeParameterNames() throws Exception {
189         final DetailAST parameterizedClassNode = getNodeFromFile(TokenTypes.CLASS_DEF);
190         final List<String> expected = Arrays.asList("V", "C");
191 
192         assertEquals("Invalid type parameters",
193                 expected, CheckUtils.getTypeParameterNames(parameterizedClassNode));
194     }
195 
196     @Test
197     public void testGetTypeParameters() throws Exception {
198         final DetailAST parameterizedClassNode = getNodeFromFile(TokenTypes.CLASS_DEF);
199         final DetailAST firstTypeParameter =
200                 getNode(parameterizedClassNode, TokenTypes.TYPE_PARAMETER);
201         final List<DetailAST> expected = Arrays.asList(firstTypeParameter,
202                 firstTypeParameter.getNextSibling().getNextSibling());
203 
204         assertEquals("Invalid type parameters", expected,
205                 CheckUtils.getTypeParameters(parameterizedClassNode));
206     }
207 
208     @Test
209     public void testIsEqualsMethod() throws Exception {
210         final DetailAST equalsMethodNode = getNodeFromFile(TokenTypes.METHOD_DEF);
211         final DetailAST someOtherMethod = equalsMethodNode.getNextSibling();
212 
213         assertTrue("Invalid result: AST provided is not equals method",
214                 CheckUtils.isEqualsMethod(equalsMethodNode));
215         assertFalse("Invalid result: AST provided is equals method",
216                 CheckUtils.isEqualsMethod(someOtherMethod));
217     }
218 
219     @Test
220     public void testIsElseIf() throws Exception {
221         final DetailAST targetMethodNode = getNodeFromFile(TokenTypes.METHOD_DEF).getNextSibling();
222         final DetailAST firstElseNode = getNode(targetMethodNode, TokenTypes.LITERAL_ELSE);
223         final DetailAST ifElseWithCurlyBraces = firstElseNode.getFirstChild().getFirstChild();
224         final DetailAST ifElse = getNode(firstElseNode.getParent().getNextSibling(),
225                 TokenTypes.LITERAL_ELSE).getFirstChild();
226         final DetailAST ifWithoutElse =
227                 firstElseNode.getParent().getNextSibling().getNextSibling();
228 
229         assertTrue("Invalid result: AST provided is not else if with curly",
230                 CheckUtils.isElseIf(ifElseWithCurlyBraces));
231         assertTrue("Invalid result: AST provided is not else if with curly",
232                 CheckUtils.isElseIf(ifElse));
233         assertFalse("Invalid result: AST provided is else if with curly",
234                 CheckUtils.isElseIf(ifWithoutElse));
235     }
236 
237     @Test
238     public void testIsNonVoidMethod() throws Exception {
239         final DetailAST nonVoidMethod = getNodeFromFile(TokenTypes.METHOD_DEF);
240         final DetailAST voidMethod = nonVoidMethod.getNextSibling();
241 
242         assertTrue("Invalid result: AST provided is void method",
243                 CheckUtils.isNonVoidMethod(nonVoidMethod));
244         assertFalse("Invalid result: AST provided is non void method",
245                 CheckUtils.isNonVoidMethod(voidMethod));
246     }
247 
248     @Test
249     public void testIsGetterMethod() throws Exception {
250         final DetailAST notGetterMethod = getNodeFromFile(TokenTypes.METHOD_DEF);
251         final DetailAST getterMethod = notGetterMethod.getNextSibling().getNextSibling();
252 
253         assertTrue("Invalid result: AST provided is getter method",
254                 CheckUtils.isGetterMethod(getterMethod));
255         assertFalse("Invalid result: AST provided is not getter method",
256                 CheckUtils.isGetterMethod(notGetterMethod));
257     }
258 
259     @Test
260     public void testIsSetterMethod() throws Exception {
261         final DetailAST firstClassMethod = getNodeFromFile(TokenTypes.METHOD_DEF);
262         final DetailAST setterMethod =
263                 firstClassMethod.getNextSibling().getNextSibling().getNextSibling();
264         final DetailAST notSetterMethod = setterMethod.getNextSibling();
265 
266         assertTrue("Invalid result: AST provided is setter method",
267                 CheckUtils.isSetterMethod(setterMethod));
268         assertFalse("Invalid result: AST provided is not setter method",
269                 CheckUtils.isSetterMethod(notSetterMethod));
270     }
271 
272     @Test
273     public void testGetAccessModifierFromModifiersToken() throws Exception {
274         final DetailAST privateVariable = getNodeFromFile(TokenTypes.VARIABLE_DEF);
275         final DetailAST protectedVariable = privateVariable.getNextSibling();
276         final DetailAST publicVariable = protectedVariable.getNextSibling();
277         final DetailAST packageVariable = publicVariable.getNextSibling();
278 
279         assertEquals("Invalid access modifier", AccessModifier.PRIVATE,
280                 CheckUtils.getAccessModifierFromModifiersToken(privateVariable.getFirstChild()));
281         assertEquals("Invalid access modifier", AccessModifier.PROTECTED,
282                 CheckUtils.getAccessModifierFromModifiersToken(protectedVariable.getFirstChild()));
283         assertEquals("Invalid access modifier", AccessModifier.PUBLIC,
284                 CheckUtils.getAccessModifierFromModifiersToken(publicVariable.getFirstChild()));
285         assertEquals("Invalid access modifier", AccessModifier.PACKAGE,
286                 CheckUtils.getAccessModifierFromModifiersToken(packageVariable.getFirstChild()));
287     }
288 
289     @Test
290     public void testGetFirstNode() throws Exception {
291         final DetailAST classDef = getNodeFromFile(TokenTypes.CLASS_DEF);
292 
293         assertEquals("Invalid first node", classDef.getFirstChild().getFirstChild(),
294                 CheckUtils.getFirstNode(classDef));
295     }
296 
297     @Test
298     public void testGetFirstNode1() {
299         final DetailAST child = new DetailAST();
300         child.setLineNo(5);
301         child.setColumnNo(6);
302 
303         final DetailAST root = new DetailAST();
304         root.setLineNo(5);
305         root.setColumnNo(6);
306 
307         root.addChild(child);
308 
309         assertEquals("Unexpected node", root, CheckUtils.getFirstNode(root));
310     }
311 
312     @Test
313     public void testGetFirstNode2() {
314         final DetailAST child = new DetailAST();
315         child.setLineNo(6);
316         child.setColumnNo(5);
317 
318         final DetailAST root = new DetailAST();
319         root.setLineNo(5);
320         root.setColumnNo(6);
321 
322         root.addChild(child);
323 
324         assertEquals("Unexpected node", root, CheckUtils.getFirstNode(root));
325     }
326 
327     @Test
328     public void testIsReceiverParameter() throws Exception {
329         final DetailAST objBlock = getNodeFromFile(TokenTypes.OBJBLOCK);
330         final DetailAST methodWithReceiverParameter = objBlock.getLastChild().getPreviousSibling();
331         final DetailAST receiverParameter =
332                 getNode(methodWithReceiverParameter, TokenTypes.PARAMETER_DEF);
333         final DetailAST simpleParameter =
334                 receiverParameter.getNextSibling().getNextSibling();
335 
336         assertTrue("Invalid result: parameter provided is receiver parameter",
337                 CheckUtils.isReceiverParameter(receiverParameter));
338         assertFalse("Invalid result: parameter provided is not receiver parameter",
339                 CheckUtils.isReceiverParameter(simpleParameter));
340     }
341 
342     @Test
343     public void testParseDouble() {
344         assertEquals("Invalid parse result", 1.0,
345                 CheckUtils.parseDouble("1", TokenTypes.NUM_INT), 0);
346         assertEquals("Invalid parse result", -0.05,
347                 CheckUtils.parseDouble("-0.05f", TokenTypes.NUM_FLOAT), 0);
348         assertEquals("Invalid parse result", 8.0,
349                 CheckUtils.parseDouble("8L", TokenTypes.NUM_LONG), 0);
350         assertEquals("Invalid parse result", 0.0,
351                 CheckUtils.parseDouble("0.0", TokenTypes.NUM_DOUBLE), 0);
352         assertEquals("Invalid parse result", 2915.0,
353                 CheckUtils.parseDouble("0x0B63", TokenTypes.NUM_INT), 0);
354         assertEquals("Invalid parse result", 2.147_483_647E10,
355                 CheckUtils.parseDouble("21474836470", TokenTypes.NUM_LONG), 0);
356         assertEquals("Invalid parse result", 59.0,
357                 CheckUtils.parseDouble("073L", TokenTypes.NUM_LONG), 0);
358     }
359 
360     @Test
361     public void testParseClassNames() {
362         final Set<String> actual = CheckUtils.parseClassNames(
363                 "I.am.class.name.with.dot.in.the.end.", "ClassOnly", "my.Class");
364         final Set<String> expected = new HashSet<>();
365         expected.add("I.am.class.name.with.dot.in.the.end.");
366         expected.add("ClassOnly");
367         expected.add("my.Class");
368         expected.add("Class");
369         assertEquals("Result is not expected", expected, actual);
370     }
371 
372     private DetailAST getNodeFromFile(int type) throws Exception {
373         return getNode(parseFile(new File(getPath("InputCheckUtilsTest.java"))), type);
374     }
375 
376     private static DetailAST getNode(DetailAST root, int type) {
377         final Optional<DetailAST> node = findTokenInAstByPredicate(root,
378             ast -> ast.getType() == type);
379 
380         if (!node.isPresent()) {
381             fail("Cannot find node of specified type: " + type);
382         }
383 
384         return node.get();
385     }
386 }