View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.gui;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  
24  import java.io.File;
25  
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
30  import com.puppycrawl.tools.checkstyle.DetailAstImpl;
31  import com.puppycrawl.tools.checkstyle.JavaParser;
32  import com.puppycrawl.tools.checkstyle.api.DetailAST;
33  import com.puppycrawl.tools.checkstyle.api.DetailNode;
34  import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
35  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
36  import com.puppycrawl.tools.checkstyle.gui.MainFrameModel.ParseMode;
37  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
38  
39  public class ParseTreeTablePresentationTest extends AbstractPathTestSupport {
40  
41      private DetailAST tree;
42  
43      @Override
44      protected String getPackageLocation() {
45          return "com/puppycrawl/tools/checkstyle/gui/parsetreetablepresentation";
46      }
47  
48      @BeforeEach
49      public void loadTree() throws Exception {
50          tree = JavaParser.parseFile(new File(getPath("InputParseTreeTablePresentation.java")),
51              JavaParser.Options.WITH_COMMENTS).getFirstChild().getNextSibling();
52      }
53  
54      @Test
55      public void testRoot() throws Exception {
56          final DetailAST root = JavaParser.parseFile(
57                  new File(getPath("InputParseTreeTablePresentation.java")),
58                  JavaParser.Options.WITH_COMMENTS);
59          assertWithMessage("Root node should have 2 children")
60                  .that(root.getChildCount())
61                  .isEqualTo(2);
62      }
63  
64      @Test
65      public void testChildCount() {
66          final int childCount = new ParseTreeTablePresentation(null).getChildCount(tree);
67          assertWithMessage("Invalid child count")
68              .that(childCount)
69              .isEqualTo(5);
70      }
71  
72      @Test
73      public void testChildCountInJavaAndJavadocMode() {
74          final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
75          parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
76          final int childCount = parseTree.getChildCount(tree);
77          assertWithMessage("Invalid child count")
78              .that(childCount)
79              .isEqualTo(5);
80      }
81  
82      @Test
83      public void testChild() {
84          final Object child = new ParseTreeTablePresentation(null).getChild(tree, 1);
85          assertWithMessage("Invalid child type")
86                  .that(child)
87                  .isInstanceOf(DetailAST.class);
88          final int type = ((DetailAST) child).getType();
89          assertWithMessage("Invalid child token type")
90              .that(type)
91              .isEqualTo(TokenTypes.BLOCK_COMMENT_BEGIN);
92      }
93  
94      @Test
95      public void testChildInJavaAndJavadocMode() {
96          final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
97          parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
98          final Object child = parseTree.getChild(tree, 1);
99          assertWithMessage("Invalid child type")
100                 .that(child)
101                 .isInstanceOf(DetailAST.class);
102         final int type = ((DetailAST) child).getType();
103         assertWithMessage("Invalid child token type")
104             .that(type)
105             .isEqualTo(TokenTypes.BLOCK_COMMENT_BEGIN);
106     }
107 
108     @Test
109     public void testCommentChildCount() {
110         final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
111         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
112         parseTree.setParseMode(ParseMode.JAVA_WITH_COMMENTS);
113         final int javadocCommentChildCount = parseTree.getChildCount(commentContentNode);
114         assertWithMessage("Invalid child count")
115             .that(javadocCommentChildCount)
116             .isEqualTo(0);
117     }
118 
119     @Test
120     public void testCommentChildCountInJavaAndJavadocMode() {
121         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
122         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
123         final DetailAST commentContentNode = tree.getLastChild().getLastChild()
124                 .getPreviousSibling().getLastChild().getFirstChild().getFirstChild();
125         final int commentChildCount = parseTree.getChildCount(commentContentNode);
126         assertWithMessage("Invalid child count")
127             .that(commentChildCount)
128             .isEqualTo(0);
129     }
130 
131     @Test
132     public void testCommentChildInJavaAndJavadocMode() {
133         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
134         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
135         final DetailAST commentContentNode = tree.getLastChild().getLastChild()
136                 .getPreviousSibling().getLastChild().getFirstChild().getFirstChild();
137         final Object commentChild = parseTree.getChild(commentContentNode, 0);
138         assertWithMessage("Child must be null")
139             .that(commentChild)
140             .isNull();
141     }
142 
143     @Test
144     public void testJavadocCommentChildCount() {
145         final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
146         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
147         final int commentChildCount = parseTree.getChildCount(commentContentNode);
148         assertWithMessage("Invalid child count")
149             .that(commentChildCount)
150             .isEqualTo(0);
151         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
152         final int javadocCommentChildCount = parseTree.getChildCount(commentContentNode);
153         assertWithMessage("Invalid child count")
154             .that(javadocCommentChildCount)
155             .isEqualTo(1);
156     }
157 
158     @Test
159     public void testJavadocCommentChild() {
160         final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
161         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
162         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
163         final Object child = parseTree.getChild(commentContentNode, 0);
164         assertWithMessage("Invalid child type")
165                 .that(child)
166                 .isInstanceOf(DetailNode.class);
167         final int type = ((DetailNode) child).getType();
168         assertWithMessage("Invalid child token type")
169             .that(type)
170             .isEqualTo(JavadocTokenTypes.JAVADOC);
171         // get Child one more time to test cache of PModel
172         final Object childSame = parseTree.getChild(commentContentNode, 0);
173         assertWithMessage("Invalid child type")
174                 .that(childSame)
175                 .isInstanceOf(DetailNode.class);
176         final int sameType = ((DetailNode) childSame).getType();
177         assertWithMessage("Invalid child token type")
178             .that(sameType)
179             .isEqualTo(JavadocTokenTypes.JAVADOC);
180     }
181 
182     @Test
183     public void testJavadocChildCount() {
184         final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
185         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
186         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
187         final Object javadoc = parseTree.getChild(commentContentNode, 0);
188         assertWithMessage("Invalid child type")
189                 .that(javadoc)
190                 .isInstanceOf(DetailNode.class);
191         final int type = ((DetailNode) javadoc).getType();
192         assertWithMessage("Invalid child token type")
193             .that(type)
194             .isEqualTo(JavadocTokenTypes.JAVADOC);
195         final int javadocChildCount = parseTree.getChildCount(javadoc);
196         assertWithMessage("Invalid child count")
197             .that(javadocChildCount)
198             .isEqualTo(5);
199     }
200 
201     @Test
202     public void testJavadocChild() {
203         final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
204         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
205         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
206         final Object javadoc = parseTree.getChild(commentContentNode, 0);
207         assertWithMessage("Invalid child type")
208                 .that(javadoc)
209                 .isInstanceOf(DetailNode.class);
210         final int type = ((DetailNode) javadoc).getType();
211         assertWithMessage("Invalid child token type")
212             .that(type)
213             .isEqualTo(JavadocTokenTypes.JAVADOC);
214         final Object javadocChild = parseTree.getChild(javadoc, 2);
215         assertWithMessage("Invalid child type")
216                 .that(javadocChild)
217                 .isInstanceOf(DetailNode.class);
218         final int childType = ((DetailNode) javadocChild).getType();
219         assertWithMessage("Invalid child token type")
220             .that(childType)
221             .isEqualTo(JavadocTokenTypes.TEXT);
222     }
223 
224     @Test
225     public void testGetIndexOfChild() {
226         DetailAST ithChild = tree.getFirstChild();
227         assertWithMessage("Child must not be null")
228             .that(ithChild)
229             .isNotNull();
230         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
231         int index = 0;
232         while (ithChild != null) {
233             final int indexOfChild = parseTree.getIndexOfChild(tree, ithChild);
234             assertWithMessage("Invalid child index")
235                 .that(indexOfChild)
236                 .isEqualTo(index);
237             ithChild = ithChild.getNextSibling();
238             index++;
239         }
240 
241         final int indexOfChild = parseTree.getIndexOfChild(tree, new DetailAstImpl());
242         assertWithMessage("Invalid child index")
243             .that(indexOfChild)
244             .isEqualTo(-1);
245     }
246 
247     /**
248      * The path to class name in InputJavadocAttributesAndMethods.java.
249      * <pre>
250      * CLASS_DEF
251      *  - MODIFIERS
252      *  - Comment node
253      *  - LITERAL_CLASS
254      *  - IDENT -> this is the node that holds the class name
255      *  Line number 4 - first three lines are taken by javadoc
256      *  Column 6 - first five columns taken by 'class '
257      *  </pre>
258      */
259     @Test
260     public void testGetValueAt() {
261         final DetailAST node = tree.getFirstChild()
262             .getNextSibling()
263             .getNextSibling()
264             .getNextSibling();
265 
266         assertWithMessage("Expected a non-null identifier node here")
267             .that(node)
268             .isNotNull();
269         assertWithMessage("Expected identifier token")
270             .that(node.getType())
271             .isEqualTo(TokenTypes.IDENT);
272 
273         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
274         final Object treeModel = parseTree.getValueAt(node, 0);
275         final String type = (String) parseTree.getValueAt(node, 1);
276         final int line = (int) parseTree.getValueAt(node, 2);
277         final int column = (int) parseTree.getValueAt(node, 3);
278         final String text = (String) parseTree.getValueAt(node, 4);
279 
280         assertWithMessage("Node should be an Identifier")
281             .that(type)
282             .isEqualTo("IDENT");
283         assertWithMessage("Class identifier should start on line 6")
284             .that(line)
285             .isEqualTo(6);
286         assertWithMessage("Class name should start from column 6")
287             .that(column)
288             .isEqualTo(6);
289         assertWithMessage("Wrong class name")
290             .that(text)
291             .isEqualTo("InputParseTreeTablePresentation");
292         assertWithMessage("Root node should have null value")
293             .that(treeModel)
294             .isNull();
295 
296         try {
297             parseTree.getValueAt(node, parseTree.getColumnCount());
298             assertWithMessage("IllegalStateException expected").fail();
299         }
300         catch (IllegalStateException ex) {
301             assertWithMessage("Invalid error message")
302                 .that(ex.getMessage())
303                 .isEqualTo("Unknown column");
304         }
305     }
306 
307     @Test
308     public void testGetValueAtDetailNode() {
309         final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
310         assertWithMessage("Comment node cannot be null")
311             .that(commentContentNode)
312             .isNotNull();
313         final int nodeType = commentContentNode.getType();
314         assertWithMessage("Comment node should be a comment type")
315                 .that(TokenUtil.isCommentType(nodeType))
316                 .isTrue();
317         assertWithMessage("This should be a javadoc comment")
318             .that(commentContentNode.getParent().getText())
319             .isEqualTo("/*");
320         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
321         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
322         final Object child = parseTree.getChild(commentContentNode, 0);
323 
324         assertWithMessage("Child has not to be leaf")
325                 .that(parseTree.isLeaf(child))
326                 .isFalse();
327         assertWithMessage("Child has to be leaf")
328                 .that(parseTree.isLeaf(tree.getFirstChild()))
329                 .isTrue();
330 
331         final Object treeModel = parseTree.getValueAt(child, 0);
332         final String type = (String) parseTree.getValueAt(child, 1);
333         final int line = (int) parseTree.getValueAt(child, 2);
334         final int column = (int) parseTree.getValueAt(child, 3);
335         final String text = (String) parseTree.getValueAt(child, 4);
336         final String expectedText = "JAVADOC";
337 
338         assertWithMessage("Tree model must be null")
339             .that(treeModel)
340             .isNull();
341         assertWithMessage("Invalid type")
342             .that(type)
343             .isEqualTo("JAVADOC");
344         assertWithMessage("Invalid line")
345             .that(line)
346             .isEqualTo(3);
347         assertWithMessage("Invalid column")
348             .that(column)
349             .isEqualTo(3);
350         assertWithMessage("Invalid text")
351             .that(text)
352             .isEqualTo(expectedText);
353 
354         try {
355             parseTree.getValueAt(child, parseTree.getColumnCount());
356             assertWithMessage("IllegalStateException expected").fail();
357         }
358         catch (IllegalStateException ex) {
359             assertWithMessage("Invalid error message")
360                 .that(ex.getMessage())
361                 .isEqualTo("Unknown column");
362         }
363     }
364 
365     @Test
366     public void testColumnMethods() {
367         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
368         assertWithMessage("Invalid type")
369             .that(parseTree.getColumnClass(0))
370             .isEqualTo(ParseTreeTableModel.class);
371         assertWithMessage("Invalid type")
372             .that(parseTree.getColumnClass(1))
373             .isEqualTo(String.class);
374         assertWithMessage("Invalid type")
375             .that(parseTree.getColumnClass(2))
376             .isEqualTo(Integer.class);
377         assertWithMessage("Invalid type")
378             .that(parseTree.getColumnClass(3))
379             .isEqualTo(Integer.class);
380         assertWithMessage("Invalid type")
381             .that(parseTree.getColumnClass(4))
382             .isEqualTo(String.class);
383 
384         try {
385             parseTree.getColumnClass(parseTree.getColumnCount());
386             assertWithMessage("IllegalStateException expected").fail();
387         }
388         catch (IllegalStateException ex) {
389             assertWithMessage("Invalid error message")
390                 .that(ex.getMessage())
391                 .isEqualTo("Unknown column");
392         }
393 
394         assertWithMessage("Invalid cell editable status")
395                 .that(parseTree.isCellEditable(1))
396                 .isFalse();
397     }
398 
399     @Test
400     public void testColumnNames() {
401         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
402         assertWithMessage("Invalid column count")
403             .that(parseTree.getColumnCount())
404             .isEqualTo(5);
405         assertWithMessage("Invalid column name")
406             .that(parseTree.getColumnName(0))
407             .isEqualTo("Tree");
408         assertWithMessage("Invalid column name")
409             .that(parseTree.getColumnName(1))
410             .isEqualTo("Type");
411         assertWithMessage("Invalid column name")
412             .that(parseTree.getColumnName(2))
413             .isEqualTo("Line");
414         assertWithMessage("Invalid column name")
415             .that(parseTree.getColumnName(3))
416             .isEqualTo("Column");
417         assertWithMessage("Invalid column name")
418             .that(parseTree.getColumnName(4))
419             .isEqualTo("Text");
420     }
421 
422 }