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