View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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 java.io.File;
23  import java.nio.charset.StandardCharsets;
24  
25  import org.junit.Assert;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import antlr.collections.AST;
30  import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
31  import com.puppycrawl.tools.checkstyle.TreeWalker;
32  import com.puppycrawl.tools.checkstyle.api.DetailAST;
33  import com.puppycrawl.tools.checkstyle.api.DetailNode;
34  import com.puppycrawl.tools.checkstyle.api.FileContents;
35  import com.puppycrawl.tools.checkstyle.api.FileText;
36  import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
37  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
38  import com.puppycrawl.tools.checkstyle.gui.MainFrameModel.ParseMode;
39  import com.puppycrawl.tools.checkstyle.utils.TokenUtils;
40  
41  public class ParseTreeTablePresentationTest extends AbstractPathTestSupport {
42  
43      private DetailAST tree;
44  
45      @Override
46      protected String getPackageLocation() {
47          return "com/puppycrawl/tools/checkstyle/gui/parsetreetablepresentation";
48      }
49  
50      private static DetailAST parseFile(File file) throws Exception {
51          final FileContents contents = new FileContents(
52                  new FileText(file.getAbsoluteFile(),
53                          System.getProperty("file.encoding",
54                          StandardCharsets.UTF_8.name())));
55          return TreeWalker.parseWithComments(contents);
56      }
57  
58      @Before
59      public void loadTree() throws Exception {
60          tree = parseFile(
61                  new File(getPath("InputParseTreeTablePresentation.java")));
62      }
63  
64      @Test
65      public void testRoot() {
66          final Object root = new ParseTreeTablePresentation(tree).getRoot();
67          final int childCount = new ParseTreeTablePresentation(null).getChildCount(root);
68          Assert.assertEquals("Invalid child count", 1, childCount);
69      }
70  
71      @Test
72      public void testChildCount() {
73          final int childCount = new ParseTreeTablePresentation(null).getChildCount(tree);
74          Assert.assertEquals("Invalid child count", 5, childCount);
75      }
76  
77      @Test
78      public void testChildCountInJavaAndJavadocMode() {
79          final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
80          parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
81          final int childCount = parseTree.getChildCount(tree);
82          Assert.assertEquals("Invalid child count", 5, childCount);
83      }
84  
85      @Test
86      public void testChild() {
87          final Object child = new ParseTreeTablePresentation(null).getChild(tree, 1);
88          Assert.assertTrue("Invalid child type", child instanceof DetailAST);
89          Assert.assertEquals("Invalid child token type",
90                  TokenTypes.BLOCK_COMMENT_BEGIN, ((AST) child).getType());
91      }
92  
93      @Test
94      public void testChildInJavaAndJavadocMode() {
95          final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
96          parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
97          final Object child = parseTree.getChild(tree, 1);
98          Assert.assertTrue("Invalid child type", child instanceof DetailAST);
99          Assert.assertEquals("Invalid child token type",
100                 TokenTypes.BLOCK_COMMENT_BEGIN, ((AST) child).getType());
101     }
102 
103     @Test
104     public void testCommentChildCount() {
105         final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
106         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
107         parseTree.setParseMode(ParseMode.JAVA_WITH_COMMENTS);
108         final int javadocCommentChildCount = parseTree.getChildCount(commentContentNode);
109         Assert.assertEquals("Invalid child count", 0, javadocCommentChildCount);
110     }
111 
112     @Test
113     public void testCommentChildCountInJavaAndJavadocMode() {
114         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
115         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
116         final DetailAST commentContentNode = tree.getLastChild().getLastChild()
117                 .getPreviousSibling().getLastChild().getFirstChild().getFirstChild();
118         final int commentChildCount = parseTree.getChildCount(commentContentNode);
119         Assert.assertEquals("Invalid child count", 0, commentChildCount);
120     }
121 
122     @Test
123     public void testCommentChildInJavaAndJavadocMode() {
124         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
125         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
126         final DetailAST commentContentNode = tree.getLastChild().getLastChild()
127                 .getPreviousSibling().getLastChild().getFirstChild().getFirstChild();
128         final Object commentChild = parseTree.getChild(commentContentNode, 0);
129         Assert.assertNull("Child must be null", commentChild);
130     }
131 
132     @Test
133     public void testJavadocCommentChildCount() {
134         final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
135         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
136         final int commentChildCount = parseTree.getChildCount(commentContentNode);
137         Assert.assertEquals("Invalid child count", 0, commentChildCount);
138         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
139         final int javadocCommentChildCount = parseTree.getChildCount(commentContentNode);
140         Assert.assertEquals("Invalid child count", 1, javadocCommentChildCount);
141     }
142 
143     @Test
144     public void testJavadocCommentChild() {
145         final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
146         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
147         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
148         final Object child = parseTree.getChild(commentContentNode, 0);
149         Assert.assertTrue("Invalid child type", child instanceof DetailNode);
150         Assert.assertEquals("Invalid child token type",
151                 JavadocTokenTypes.JAVADOC, ((DetailNode) child).getType());
152         // get Child one more time to test cache of PModel
153         final Object childSame = parseTree.getChild(commentContentNode, 0);
154         Assert.assertTrue("Invalid child type", childSame instanceof DetailNode);
155         Assert.assertEquals("Invalid child token type",
156                 JavadocTokenTypes.JAVADOC, ((DetailNode) childSame).getType());
157     }
158 
159     @Test
160     public void testJavadocChildCount() {
161         final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
162         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
163         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
164         final Object javadoc = parseTree.getChild(commentContentNode, 0);
165         Assert.assertTrue("Invalid child type", javadoc instanceof DetailNode);
166         Assert.assertEquals("Invalid child token type",
167                 JavadocTokenTypes.JAVADOC, ((DetailNode) javadoc).getType());
168         final int javadocChildCount = parseTree.getChildCount(javadoc);
169         Assert.assertEquals("Invalid child count", 5, javadocChildCount);
170     }
171 
172     @Test
173     public void testJavadocChild() {
174         final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
175         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
176         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
177         final Object javadoc = parseTree.getChild(commentContentNode, 0);
178         Assert.assertTrue("Invalid child type", javadoc instanceof DetailNode);
179         Assert.assertEquals("Invalid child token type",
180                 JavadocTokenTypes.JAVADOC, ((DetailNode) javadoc).getType());
181         final Object javadocChild = parseTree.getChild(javadoc, 2);
182         Assert.assertTrue("Invalid child type", javadocChild instanceof DetailNode);
183         Assert.assertEquals("Invalid child token type",
184                 JavadocTokenTypes.TEXT, ((DetailNode) javadocChild).getType());
185     }
186 
187     @Test
188     public void testGetIndexOfChild() {
189         DetailAST ithChild = tree.getFirstChild();
190         Assert.assertNotNull("Child must not be null", ithChild);
191         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
192         int index = 0;
193         while (ithChild != null) {
194             Assert.assertEquals("Invalid child index",
195                     index, parseTree.getIndexOfChild(tree, ithChild));
196             ithChild = ithChild.getNextSibling();
197             index++;
198         }
199 
200         Assert.assertEquals("Invalid child index",
201                 -1, parseTree.getIndexOfChild(tree, new DetailAST()));
202     }
203 
204     /**
205      * The path to class name in InputJavadocAttributesAndMethods.java.
206      * <pre>
207      * CLASS_DEF
208      *  - MODIFIERS
209      *  - Comment node
210      *  - LITERAL_CLASS
211      *  - IDENT -> this is the node that holds the class name
212      *  Line number 4 - first three lines are taken by javadoc
213      *  Column 6 - first five columns taken by 'class '
214      *  </pre>
215      */
216     @Test
217     public void testGetValueAt() {
218 
219         final DetailAST node = tree.getFirstChild()
220             .getNextSibling()
221             .getNextSibling()
222             .getNextSibling();
223 
224         Assert.assertNotNull("Expected a non-null identifier node here", node);
225         Assert.assertEquals("Expected identifier token",
226             TokenTypes.IDENT, node.getType());
227 
228         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
229         final Object treeModel = parseTree.getValueAt(node, 0);
230         final String type = (String) parseTree.getValueAt(node, 1);
231         final int line = (int) parseTree.getValueAt(node, 2);
232         final int column = (int) parseTree.getValueAt(node, 3);
233         final String text = (String) parseTree.getValueAt(node, 4);
234 
235         Assert.assertEquals("Node should be an Identifier", "IDENT", type);
236         Assert.assertEquals("Class identifier should start on line 4", 4, line);
237         Assert.assertEquals("Class name should start from column 6", 6, column);
238         Assert.assertEquals("Wrong class name", "InputParseTreeTablePresentation", text);
239         Assert.assertNull("Root node should have null value", treeModel);
240 
241         try {
242             parseTree.getValueAt(node, parseTree.getColumnCount());
243             Assert.fail("IllegalStateException expected");
244         }
245         catch (IllegalStateException ex) {
246             Assert.assertEquals("Invalid error message", "Unknown column", ex.getMessage());
247         }
248 
249     }
250 
251     @Test
252     public void testGetValueAtDetailNode() {
253         final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
254         Assert.assertNotNull("Comment node cannot be null", commentContentNode);
255         final int nodeType = commentContentNode.getType();
256         Assert.assertTrue("Comment node should be a comment type",
257             TokenUtils.isCommentType(nodeType));
258         Assert.assertEquals("This should be a javadoc comment",
259             "/*", commentContentNode.getParent().getText());
260         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
261         parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
262         final Object child = parseTree.getChild(commentContentNode, 0);
263 
264         Assert.assertFalse("Child has not to be leaf", parseTree.isLeaf(child));
265         Assert.assertTrue("Child has to be leaf", parseTree.isLeaf(tree.getFirstChild()));
266 
267         final Object treeModel = parseTree.getValueAt(child, 0);
268         final String type = (String) parseTree.getValueAt(child, 1);
269         final int line = (int) parseTree.getValueAt(child, 2);
270         final int column = (int) parseTree.getValueAt(child, 3);
271         final String text = (String) parseTree.getValueAt(child, 4);
272         final String expectedText = "JAVADOC";
273 
274         Assert.assertNull("Tree model must be null", treeModel);
275         Assert.assertEquals("Invalid type", "JAVADOC", type);
276         Assert.assertEquals("Invalid line", 1, line);
277         Assert.assertEquals("Invalid column", 3, column);
278         Assert.assertEquals("Invalid text", expectedText, text);
279 
280         try {
281             parseTree.getValueAt(child, parseTree.getColumnCount());
282             Assert.fail("IllegalStateException expected");
283         }
284         catch (IllegalStateException ex) {
285             Assert.assertEquals("Invalid error message", "Unknown column", ex.getMessage());
286         }
287 
288     }
289 
290     @Test
291     public void testColumnMethods() {
292         final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
293         Assert.assertSame("Invalid type", ParseTreeTableModel.class, parseTree.getColumnClass(0));
294         Assert.assertSame("Invalid type", String.class, parseTree.getColumnClass(1));
295         Assert.assertSame("Invalid type", Integer.class, parseTree.getColumnClass(2));
296         Assert.assertSame("Invalid type", Integer.class, parseTree.getColumnClass(3));
297         Assert.assertSame("Invalid type", String.class, parseTree.getColumnClass(4));
298 
299         try {
300             parseTree.getColumnClass(parseTree.getColumnCount());
301             Assert.fail("IllegalStateException expected");
302         }
303         catch (IllegalStateException ex) {
304             Assert.assertEquals("Invalid error message", "Unknown column", ex.getMessage());
305         }
306 
307         Assert.assertFalse("Invalid cell editable status", parseTree.isCellEditable(1));
308 
309         Assert.assertEquals("Invalid column count", 5, parseTree.getColumnCount());
310         Assert.assertEquals("Invalid column name", "Tree", parseTree.getColumnName(0));
311         Assert.assertEquals("Invalid column name", "Type", parseTree.getColumnName(1));
312         Assert.assertEquals("Invalid column name", "Line", parseTree.getColumnName(2));
313         Assert.assertEquals("Invalid column name", "Column", parseTree.getColumnName(3));
314         Assert.assertEquals("Invalid column name", "Text", parseTree.getColumnName(4));
315 
316     }
317 
318 }