1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.api;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck.MSG_ORDERING;
24
25 import java.io.File;
26 import java.nio.charset.StandardCharsets;
27
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31 import com.puppycrawl.tools.checkstyle.DetailAstImpl;
32 import com.puppycrawl.tools.checkstyle.JavaParser;
33 import com.puppycrawl.tools.checkstyle.checks.coding.UnusedLocalVariableCheck;
34 import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck;
35
36 public class FullIdentTest extends AbstractModuleTestSupport {
37
38 @Override
39 protected String getPackageLocation() {
40 return "com/puppycrawl/tools/checkstyle/api/fullident/";
41 }
42
43 @Test
44 public void testToString() {
45 final DetailAstImpl ast = new DetailAstImpl();
46 ast.setType(TokenTypes.LITERAL_NEW);
47 ast.setColumnNo(14);
48 ast.setLineNo(15);
49 ast.setText("MyTest");
50
51 final DetailAstImpl parent = new DetailAstImpl();
52 parent.setType(TokenTypes.OBJBLOCK);
53 parent.setColumnNo(4);
54 parent.setLineNo(4);
55 parent.setText("MyParent");
56 parent.setFirstChild(ast);
57
58 final FullIdent indent = FullIdent.createFullIdent(ast);
59 assertWithMessage("Invalid full indent")
60 .that(indent.toString())
61 .isEqualTo("MyTest[15x14]");
62 assertWithMessage("Invalid text")
63 .that(indent.getText())
64 .isEqualTo("MyTest");
65 assertWithMessage("Invalid line")
66 .that(indent.getLineNo())
67 .isEqualTo(15);
68 assertWithMessage("Invalid column")
69 .that(indent.getColumnNo())
70 .isEqualTo(14);
71 }
72
73 @Test
74 public void testCreateFullIdentBelow() {
75 final DetailAST ast = new DetailAstImpl();
76
77 final FullIdent indent = FullIdent.createFullIdentBelow(ast);
78 assertWithMessage("Invalid full indent")
79 .that(indent.getText())
80 .isEqualTo("");
81 }
82
83 @Test
84 public void testGetDetailAst() throws Exception {
85 final FileText testFileText = new FileText(
86 new File(getPath("InputFullIdentTestArrayType.java")).getAbsoluteFile(),
87 System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
88 final DetailAST packageDefinitionNode =
89 JavaParser.parse(new FileContents(testFileText)).getFirstChild();
90 final DetailAST packageName = packageDefinitionNode.getFirstChild().getNextSibling();
91 final FullIdent ident = FullIdent.createFullIdent(packageName);
92 assertWithMessage("Invalid full indent")
93 .that(ident.getDetailAst().toString())
94 .isEqualTo("com[1x8]");
95 }
96
97 @Test
98 public void testNonValidCoordinatesWithNegative() {
99 final FullIdent fullIdent = prepareFullIdentWithCoordinates(14, 15);
100 assertWithMessage("Invalid full indent")
101 .that(fullIdent.toString())
102 .isEqualTo("MyTest.MyTestik[15x14]");
103 }
104
105 @Test
106 public void testNonValidCoordinatesWithZero() {
107 final FullIdent fullIdent = prepareFullIdentWithCoordinates(0, 0);
108 assertWithMessage("Invalid full indent")
109 .that(fullIdent.toString())
110 .isEqualTo("MyTest.MyTestik[15x14]");
111 }
112
113 @Test
114 public void testWithArrayCreateFullIdentWithArrayDeclare() throws Exception {
115 final FileText testFileText = new FileText(
116 new File(getPath("InputFullIdentTestArrayType.java")).getAbsoluteFile(),
117 System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
118 final DetailAST packageDefinitionNode =
119 JavaParser.parse(new FileContents(testFileText)).getFirstChild();
120 final DetailAST arrayDeclarator = packageDefinitionNode.getNextSibling()
121 .findFirstToken(TokenTypes.OBJBLOCK)
122 .findFirstToken(TokenTypes.VARIABLE_DEF)
123 .findFirstToken(TokenTypes.TYPE)
124 .getFirstChild();
125 final FullIdent ident = FullIdent.createFullIdent(arrayDeclarator);
126 assertWithMessage("Invalid full indent")
127 .that(ident.toString())
128 .isEqualTo("int[][][5x12]");
129 }
130
131 @Test
132 public void testFullIdentAnnotation() throws Exception {
133 final FileText testFileText = new FileText(
134 new File(getPath("InputFullIdentAnnotation.java")).getAbsoluteFile(),
135 System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
136 final DetailAST packageDefinitionNode =
137 JavaParser.parse(new FileContents(testFileText)).getFirstChild();
138 final DetailAST methodDef = packageDefinitionNode
139 .getNextSibling()
140 .getNextSibling()
141 .getNextSibling()
142 .getLastChild()
143 .findFirstToken(TokenTypes.METHOD_DEF);
144
145 final DetailAST parameter = methodDef
146 .findFirstToken(TokenTypes.PARAMETERS)
147 .getFirstChild()
148 .getFirstChild()
149 .getNextSibling()
150 .getFirstChild();
151
152 final FullIdent ident = FullIdent.createFullIdent(parameter);
153 assertWithMessage("Invalid full indent")
154 .that(ident.toString())
155 .isEqualTo("char[][7x29]");
156 }
157
158 @Test
159 public void testFullIdentArrayInit() throws Exception {
160 final FileText testFileText = new FileText(
161 new File(getPath("InputFullIdentArrayInit.java")).getAbsoluteFile(),
162 System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
163 final DetailAST packageDefinitionNode =
164 JavaParser.parse(new FileContents(testFileText)).getFirstChild();
165 final DetailAST variableDef = packageDefinitionNode
166 .getNextSibling()
167 .getLastChild()
168 .findFirstToken(TokenTypes.VARIABLE_DEF);
169
170 final DetailAST literalInt = variableDef
171 .findFirstToken(TokenTypes.ASSIGN)
172 .getFirstChild()
173 .getFirstChild()
174 .getFirstChild();
175
176 final FullIdent ident = FullIdent.createFullIdent(literalInt);
177 assertWithMessage("Invalid full indent")
178 .that(ident.toString())
179 .isEqualTo("int[4x32]");
180 }
181
182 private static FullIdent prepareFullIdentWithCoordinates(int columnNo, int lineNo) {
183 final DetailAstImpl parent = new DetailAstImpl();
184 parent.setType(TokenTypes.TYPE);
185 parent.setColumnNo(1);
186 parent.setLineNo(1);
187 parent.setText("Parent");
188
189 final DetailAstImpl ast = new DetailAstImpl();
190 ast.setType(TokenTypes.DOT);
191 ast.setColumnNo(1);
192 ast.setLineNo(2);
193 ast.setText("Root");
194
195 final DetailAstImpl ast2 = new DetailAstImpl();
196 ast2.setType(TokenTypes.LE);
197 ast2.setColumnNo(columnNo);
198 ast2.setLineNo(lineNo);
199 ast2.setText("MyTestik");
200
201 final DetailAstImpl ast1 = new DetailAstImpl();
202 ast1.setType(TokenTypes.LITERAL_NEW);
203 ast1.setColumnNo(14);
204 ast1.setLineNo(15);
205 ast1.setText("MyTest");
206
207 parent.addChild(ast);
208 ast.addChild(ast1);
209 ast.addChild(ast2);
210
211 return FullIdent.createFullIdent(ast);
212 }
213
214 @Test
215 public void testReturnNoAnnotation() throws Exception {
216 final FileText testFileText = new FileText(
217 new File(getNonCompilablePath("InputFullIdentReturnNoAnnotation.java"))
218 .getAbsoluteFile(),
219 System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
220 final DetailAST packageDefinitionNode =
221 JavaParser.parse(new FileContents(testFileText)).getFirstChild();
222 final DetailAST annotationNode = packageDefinitionNode.getFirstChild();
223 final FullIdent ident = FullIdent.createFullIdent(annotationNode);
224 assertWithMessage("Full ident text should be empty.")
225 .that(ident.getText())
226 .isEmpty();
227 }
228
229 @Test
230 public void testFullyQualifiedStringArray() throws Exception {
231 final FileText testFileText = new FileText(
232 new File(getPath("InputFullIdentFullyQualifiedStringArray.java")).getAbsoluteFile(),
233 System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
234 final DetailAST packageDefinitionNode =
235 JavaParser.parse(new FileContents(testFileText)).getFirstChild();
236 final DetailAST objectBlock = packageDefinitionNode.getNextSibling().getLastChild();
237 final DetailAST mainMethodNode = objectBlock.findFirstToken(TokenTypes.METHOD_DEF);
238 final DetailAST parameter = mainMethodNode
239 .findFirstToken(TokenTypes.PARAMETERS).getFirstChild();
240 final DetailAST parameterType = parameter.findFirstToken(TokenTypes.TYPE);
241 final FullIdent ident = FullIdent.createFullIdent(parameterType.getFirstChild());
242
243 assertWithMessage("Full ident should match expected.")
244 .that(ident.getText())
245 .isEqualTo(String[].class.getCanonicalName());
246 }
247
248 @Test
249 public void testCreateFullIdentBelow2() throws Exception {
250 final String[] expected = {
251 "9:1: " + getCheckMessage(ImportOrderCheck.class,
252 MSG_ORDERING, "java.util.HashMap"),
253 };
254
255 verifyWithInlineConfigParser(getPath("InputFullIdent.java"),
256 expected);
257 }
258
259 @Test
260 public void testLiteralNewCondition() throws Exception {
261 final String[] expected = {
262 "11:9: " + getCheckMessage(UnusedLocalVariableCheck.class,
263 UnusedLocalVariableCheck.MSG_UNUSED_LOCAL_VARIABLE, "j"),
264 };
265
266 verifyWithInlineConfigParser(getPath("InputFullIdentLiteralNewCondition.java"),
267 expected);
268 }
269 }