This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
forked from checkstyle/checkstyle
/
CheckUtilsTest.java
310 lines (248 loc) · 12.3 KB
/
CheckUtilsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2017 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.utils;
import static com.puppycrawl.tools.checkstyle.internal.TestUtils.assertUtilsClassHasPrivateConstructor;
import static com.puppycrawl.tools.checkstyle.internal.TestUtils.findTokenInAstByPredicate;
import static com.puppycrawl.tools.checkstyle.internal.TestUtils.parseFile;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.junit.Test;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifier;
public class CheckUtilsTest {
@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(CheckUtils.class);
}
@Test
public void testParseDoubleWithIncorrectToken() {
final double parsedDouble = CheckUtils.parseDouble("1_02", TokenTypes.ASSIGN);
assertEquals(0.0, parsedDouble, 0.0);
}
@Test
public void testElseWithCurly() {
final DetailAST ast = new DetailAST();
ast.setType(TokenTypes.ASSIGN);
ast.setText("ASSIGN");
assertFalse(CheckUtils.isElseIf(ast));
final DetailAST parentAst = new DetailAST();
parentAst.setType(TokenTypes.LCURLY);
parentAst.setText("LCURLY");
final DetailAST ifAst = new DetailAST();
ifAst.setType(TokenTypes.LITERAL_IF);
ifAst.setText("IF");
parentAst.addChild(ifAst);
assertFalse(CheckUtils.isElseIf(ifAst));
final DetailAST parentAst2 = new DetailAST();
parentAst2.setType(TokenTypes.SLIST);
parentAst2.setText("SLIST");
parentAst2.addChild(ifAst);
assertFalse(CheckUtils.isElseIf(ifAst));
final DetailAST elseAst = new DetailAST();
elseAst.setType(TokenTypes.LITERAL_ELSE);
elseAst.setFirstChild(ifAst);
assertTrue(CheckUtils.isElseIf(ifAst));
}
@Test
public void testEquals() {
final DetailAST litStatic = new DetailAST();
litStatic.setType(TokenTypes.LITERAL_STATIC);
final DetailAST modifiers = new DetailAST();
modifiers.setType(TokenTypes.MODIFIERS);
modifiers.addChild(litStatic);
final DetailAST metDef = new DetailAST();
metDef.setType(TokenTypes.METHOD_DEF);
metDef.addChild(modifiers);
assertFalse(CheckUtils.isEqualsMethod(metDef));
metDef.removeChildren();
final DetailAST metName = new DetailAST();
metName.setType(TokenTypes.IDENT);
metName.setText("equals");
metDef.addChild(metName);
final DetailAST modifiers2 = new DetailAST();
modifiers2.setType(TokenTypes.MODIFIERS);
metDef.addChild(modifiers2);
final DetailAST parameter1 = new DetailAST();
final DetailAST parameter2 = new DetailAST();
final DetailAST parameters = new DetailAST();
parameters.setType(TokenTypes.PARAMETERS);
parameters.addChild(parameter2);
parameters.addChild(parameter1);
metDef.addChild(parameters);
assertFalse(CheckUtils.isEqualsMethod(metDef));
}
@Test
public void testGetAccessModifierFromModifiersTokenWrongTokenType() {
final DetailAST modifiers = new DetailAST();
modifiers.setType(TokenTypes.METHOD_DEF);
try {
CheckUtils.getAccessModifierFromModifiersToken(modifiers);
fail(IllegalArgumentException.class.getSimpleName() + " was expected.");
}
catch (IllegalArgumentException exc) {
final String expectedExceptionMsg = "expected non-null AST-token with type 'MODIFIERS'";
final String actualExceptionMsg = exc.getMessage();
assertEquals(expectedExceptionMsg, actualExceptionMsg);
}
}
@Test
public void testGetAccessModifierFromModifiersTokenWithNullParameter() {
try {
CheckUtils.getAccessModifierFromModifiersToken(null);
fail(IllegalArgumentException.class.getSimpleName() + " was expected.");
}
catch (IllegalArgumentException exc) {
final String expectedExceptionMsg = "expected non-null AST-token with type 'MODIFIERS'";
final String actualExceptionMsg = exc.getMessage();
assertEquals(expectedExceptionMsg, actualExceptionMsg);
}
}
@Test
public void testCreateFullType() throws Exception {
final DetailAST typeNode = getNodeFromFile(TokenTypes.TYPE);
assertEquals("Map[12x12]", CheckUtils.createFullType(typeNode).toString());
}
@Test
public void testCreateFullTypeOfArray() throws Exception {
final DetailAST arrayTypeNode = getNodeFromFile(TokenTypes.VARIABLE_DEF)
.getNextSibling().getFirstChild().getNextSibling();
assertEquals("int[13x14]", CheckUtils.createFullType(arrayTypeNode).toString());
}
@Test
public void testGetTypeParameterNames() throws Exception {
final DetailAST parameterisedClassNode = getNodeFromFile(TokenTypes.CLASS_DEF);
final List<String> expected = Arrays.asList("V", "C");
assertEquals(expected, CheckUtils.getTypeParameterNames(parameterisedClassNode));
}
@Test
public void testGetTypeParameters() throws Exception {
final DetailAST parameterisedClassNode = getNodeFromFile(TokenTypes.CLASS_DEF);
final DetailAST firstTypeParameter =
getNode(parameterisedClassNode, TokenTypes.TYPE_PARAMETER);
final List<DetailAST> expected = Arrays.asList(firstTypeParameter,
firstTypeParameter.getNextSibling().getNextSibling());
assertEquals(expected, CheckUtils.getTypeParameters(parameterisedClassNode));
}
@Test
public void testIsEqualsMethod() throws Exception {
final DetailAST equalsMethodNode = getNodeFromFile(TokenTypes.METHOD_DEF);
final DetailAST someOtherMethod = equalsMethodNode.getNextSibling();
assertTrue(CheckUtils.isEqualsMethod(equalsMethodNode));
assertFalse(CheckUtils.isEqualsMethod(someOtherMethod));
}
@Test
public void testIsElseIf() throws Exception {
final DetailAST targetMethodNode = getNodeFromFile(TokenTypes.METHOD_DEF).getNextSibling();
final DetailAST firstElseNode = getNode(targetMethodNode, TokenTypes.LITERAL_ELSE);
final DetailAST ifElseWithCurlyBraces = firstElseNode.getFirstChild().getFirstChild();
final DetailAST ifElse = getNode(firstElseNode.getParent().getNextSibling(),
TokenTypes.LITERAL_ELSE).getFirstChild();
final DetailAST ifWithoutElse =
firstElseNode.getParent().getNextSibling().getNextSibling();
assertTrue(CheckUtils.isElseIf(ifElseWithCurlyBraces));
assertTrue(CheckUtils.isElseIf(ifElse));
assertFalse(CheckUtils.isElseIf(ifWithoutElse));
}
@Test
public void testIsNonViodMethod() throws Exception {
final DetailAST nonVoidMethod = getNodeFromFile(TokenTypes.METHOD_DEF);
final DetailAST voidMethod = nonVoidMethod.getNextSibling();
assertTrue(CheckUtils.isNonVoidMethod(nonVoidMethod));
assertFalse(CheckUtils.isNonVoidMethod(voidMethod));
}
@Test
public void testIsGetterMethod() throws Exception {
final DetailAST notGetterMethod = getNodeFromFile(TokenTypes.METHOD_DEF);
final DetailAST getterMethod = notGetterMethod.getNextSibling().getNextSibling();
assertTrue(CheckUtils.isGetterMethod(getterMethod));
assertFalse(CheckUtils.isGetterMethod(notGetterMethod));
}
@Test
public void testIsSetterMethod() throws Exception {
final DetailAST firstClassMethod = getNodeFromFile(TokenTypes.METHOD_DEF);
final DetailAST setterMethod =
firstClassMethod.getNextSibling().getNextSibling().getNextSibling();
final DetailAST notSetterMethod = setterMethod.getNextSibling();
assertTrue(CheckUtils.isSetterMethod(setterMethod));
assertFalse(CheckUtils.isSetterMethod(notSetterMethod));
}
@Test
public void testGetAccessModifierFromModifiersToken() throws Exception {
final DetailAST privateVariable = getNodeFromFile(TokenTypes.VARIABLE_DEF);
final DetailAST protectedVariable = privateVariable.getNextSibling();
final DetailAST publicVariable = protectedVariable.getNextSibling();
final DetailAST packageVariable = publicVariable.getNextSibling();
assertEquals(AccessModifier.PRIVATE,
CheckUtils.getAccessModifierFromModifiersToken(privateVariable.getFirstChild()));
assertEquals(AccessModifier.PROTECTED,
CheckUtils.getAccessModifierFromModifiersToken(protectedVariable.getFirstChild()));
assertEquals(AccessModifier.PUBLIC,
CheckUtils.getAccessModifierFromModifiersToken(publicVariable.getFirstChild()));
assertEquals(AccessModifier.PACKAGE,
CheckUtils.getAccessModifierFromModifiersToken(packageVariable.getFirstChild()));
}
@Test
public void testGetFirstNode() throws Exception {
final DetailAST classDef = getNodeFromFile(TokenTypes.CLASS_DEF);
assertEquals(classDef.getFirstChild().getFirstChild(), CheckUtils.getFirstNode(classDef));
}
@Test
public void testIsReceiverParameter() throws Exception {
final DetailAST objBlock = getNodeFromFile(TokenTypes.OBJBLOCK);
final DetailAST methodWithReceiverParameter = objBlock.getLastChild().getPreviousSibling();
final DetailAST receiverParameter =
getNode(methodWithReceiverParameter, TokenTypes.PARAMETER_DEF);
final DetailAST simpleParameter =
receiverParameter.getNextSibling().getNextSibling();
assertTrue(CheckUtils.isReceiverParameter(receiverParameter));
assertFalse(CheckUtils.isReceiverParameter(simpleParameter));
}
@Test
public void testParseDouble() throws Exception {
assertEquals(1.0, CheckUtils.parseDouble("1", TokenTypes.NUM_INT), 0);
assertEquals(-0.05, CheckUtils.parseDouble("-0.05f", TokenTypes.NUM_FLOAT), 0);
assertEquals(8.0, CheckUtils.parseDouble("8L", TokenTypes.NUM_LONG), 0);
assertEquals(0.0, CheckUtils.parseDouble("0.0", TokenTypes.NUM_DOUBLE), 0);
assertEquals(2915.0, CheckUtils.parseDouble("0x0B63", TokenTypes.NUM_INT), 0);
assertEquals(2.147_483_647E10,
CheckUtils.parseDouble("21474836470", TokenTypes.NUM_LONG), 0);
assertEquals(59.0, CheckUtils.parseDouble("073L", TokenTypes.NUM_LONG), 0);
}
private static File getPath(String filename) {
return new File("src/test/resources/com/puppycrawl/tools/checkstyle/utils/" + filename);
}
private static DetailAST getNodeFromFile(int type) throws Exception {
return getNode(parseFile(getPath("checkutils/InputCheckUtilsTest.java")), type);
}
private static DetailAST getNode(DetailAST root, int type) {
final Optional<DetailAST> node = findTokenInAstByPredicate(root,
ast -> ast.getType() == type);
if (!node.isPresent()) {
fail("Cannot find node of specified type: " + type);
}
return node.get();
}
}