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