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