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