aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/puppycrawl/tools/checkstyle/gui/ParseTreeTablePresentationTest.java
blob: e95489cd17ac3f38b9f70c2395be74936d4d32b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2017 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////

package com.puppycrawl.tools.checkstyle.gui;

import java.io.File;
import java.nio.charset.StandardCharsets;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import antlr.collections.AST;
import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
import com.puppycrawl.tools.checkstyle.TreeWalker;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.DetailNode;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.gui.MainFrameModel.ParseMode;
import com.puppycrawl.tools.checkstyle.utils.TokenUtils;

public class ParseTreeTablePresentationTest extends AbstractPathTestSupport {

    private DetailAST tree;

    @Override
    protected String getPackageLocation() {
        return "com/puppycrawl/tools/checkstyle/gui/parsetreetablepresentation";
    }

    private static DetailAST parseFile(File file) throws Exception {
        final FileContents contents = new FileContents(
                new FileText(file.getAbsoluteFile(),
                        System.getProperty("file.encoding",
                        StandardCharsets.UTF_8.name())));
        return TreeWalker.parseWithComments(contents);
    }

    @Before
    public void loadTree() throws Exception {
        tree = parseFile(
                new File(getPath("InputParseTreeTablePresentation.java")));
    }

    @Test
    public void testRoot() {
        final Object root = new ParseTreeTablePresentation(tree).getRoot();
        final int childCount = new ParseTreeTablePresentation(null).getChildCount(root);
        Assert.assertEquals("Invalid child count", 1, childCount);
    }

    @Test
    public void testChildCount() {
        final int childCount = new ParseTreeTablePresentation(null).getChildCount(tree);
        Assert.assertEquals("Invalid child count", 5, childCount);
    }

    @Test
    public void testChildCountInJavaAndJavadocMode() {
        final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
        parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
        final int childCount = parseTree.getChildCount(tree);
        Assert.assertEquals("Invalid child count", 5, childCount);
    }

    @Test
    public void testChild() {
        final Object child = new ParseTreeTablePresentation(null).getChild(tree, 1);
        Assert.assertTrue("Invalid child type", child instanceof DetailAST);
        Assert.assertEquals("Invalid child token type",
                TokenTypes.BLOCK_COMMENT_BEGIN, ((AST) child).getType());
    }

    @Test
    public void testChildInJavaAndJavadocMode() {
        final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
        parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
        final Object child = parseTree.getChild(tree, 1);
        Assert.assertTrue("Invalid child type", child instanceof DetailAST);
        Assert.assertEquals("Invalid child token type",
                TokenTypes.BLOCK_COMMENT_BEGIN, ((AST) child).getType());
    }

    @Test
    public void testCommentChildCount() {
        final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
        final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
        parseTree.setParseMode(ParseMode.JAVA_WITH_COMMENTS);
        final int javadocCommentChildCount = parseTree.getChildCount(commentContentNode);
        Assert.assertEquals("Invalid child count", 0, javadocCommentChildCount);
    }

    @Test
    public void testCommentChildCountInJavaAndJavadocMode() {
        final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
        parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
        final DetailAST commentContentNode = tree.getLastChild().getLastChild()
                .getPreviousSibling().getLastChild().getFirstChild().getFirstChild();
        final int commentChildCount = parseTree.getChildCount(commentContentNode);
        Assert.assertEquals("Invalid child count", 0, commentChildCount);
    }

    @Test
    public void testCommentChildInJavaAndJavadocMode() {
        final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
        parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
        final DetailAST commentContentNode = tree.getLastChild().getLastChild()
                .getPreviousSibling().getLastChild().getFirstChild().getFirstChild();
        final Object commentChild = parseTree.getChild(commentContentNode, 0);
        Assert.assertNull("Child must be null", commentChild);
    }

    @Test
    public void testJavadocCommentChildCount() {
        final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
        final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
        final int commentChildCount = parseTree.getChildCount(commentContentNode);
        Assert.assertEquals("Invalid child count", 0, commentChildCount);
        parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
        final int javadocCommentChildCount = parseTree.getChildCount(commentContentNode);
        Assert.assertEquals("Invalid child count", 1, javadocCommentChildCount);
    }

    @Test
    public void testJavadocCommentChild() {
        final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
        final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
        parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
        final Object child = parseTree.getChild(commentContentNode, 0);
        Assert.assertTrue("Invalid child type", child instanceof DetailNode);
        Assert.assertEquals("Invalid child token type",
                JavadocTokenTypes.JAVADOC, ((DetailNode) child).getType());
        // get Child one more time to test cache of PModel
        final Object childSame = parseTree.getChild(commentContentNode, 0);
        Assert.assertTrue("Invalid child type", childSame instanceof DetailNode);
        Assert.assertEquals("Invalid child token type",
                JavadocTokenTypes.JAVADOC, ((DetailNode) childSame).getType());
    }

    @Test
    public void testJavadocChildCount() {
        final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
        final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
        parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
        final Object javadoc = parseTree.getChild(commentContentNode, 0);
        Assert.assertTrue("Invalid child type", javadoc instanceof DetailNode);
        Assert.assertEquals("Invalid child token type",
                JavadocTokenTypes.JAVADOC, ((DetailNode) javadoc).getType());
        final int javadocChildCount = parseTree.getChildCount(javadoc);
        Assert.assertEquals("Invalid child count", 5, javadocChildCount);
    }

    @Test
    public void testJavadocChild() {
        final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
        final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
        parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
        final Object javadoc = parseTree.getChild(commentContentNode, 0);
        Assert.assertTrue("Invalid child type", javadoc instanceof DetailNode);
        Assert.assertEquals("Invalid child token type",
                JavadocTokenTypes.JAVADOC, ((DetailNode) javadoc).getType());
        final Object javadocChild = parseTree.getChild(javadoc, 2);
        Assert.assertTrue("Invalid child type", javadocChild instanceof DetailNode);
        Assert.assertEquals("Invalid child token type",
                JavadocTokenTypes.TEXT, ((DetailNode) javadocChild).getType());
    }

    @Test
    public void testGetIndexOfChild() {
        DetailAST ithChild = tree.getFirstChild();
        Assert.assertNotNull("Child must not be null", ithChild);
        final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
        int index = 0;
        while (ithChild != null) {
            Assert.assertEquals("Invalud child index",
                    index, parseTree.getIndexOfChild(tree, ithChild));
            ithChild = ithChild.getNextSibling();
            index++;
        }

        Assert.assertEquals("Invalud child index",
                -1, parseTree.getIndexOfChild(tree, new DetailAST()));
    }

    /**
     * The path to class name in InputJavadocAttributesAndMethods.java.
     * <pre>
     * CLASS_DEF
     *  - MODIFIERS
     *  - Comment node
     *  - LITERAL_CLASS
     *  - IDENT -> this is the node that holds the class name
     *  Line number 4 - first three lines are taken by javadoc
     *  Column 6 - first five columns taken by 'class '
     *  </pre>
     */
    @Test
    public void testGetValueAt() {

        final DetailAST node = tree.getFirstChild()
            .getNextSibling()
            .getNextSibling()
            .getNextSibling();

        Assert.assertNotNull("Expected a non-null identifier node here", node);
        Assert.assertEquals("Expected identifier token",
            TokenTypes.IDENT, node.getType());

        final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
        final Object treeModel = parseTree.getValueAt(node, 0);
        final String type = (String) parseTree.getValueAt(node, 1);
        final int line = (int) parseTree.getValueAt(node, 2);
        final int column = (int) parseTree.getValueAt(node, 3);
        final String text = (String) parseTree.getValueAt(node, 4);

        Assert.assertEquals("Node should be an Identifier", "IDENT", type);
        Assert.assertEquals("Class identifier should start on line 4", 4, line);
        Assert.assertEquals("Class name should start from column 6", 6, column);
        Assert.assertEquals("Wrong class name", "InputParseTreeTablePresentation", text);
        Assert.assertNull("Root node should have null value", treeModel);

        try {
            parseTree.getValueAt(node, parseTree.getColumnCount());
            Assert.fail("IllegalStateException expected");
        }
        catch (IllegalStateException ex) {
            Assert.assertEquals("Invalid error message", "Unknown column", ex.getMessage());
        }

    }

    @Test
    public void testGetValueAtDetailNode() {
        final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
        Assert.assertNotNull("Comment node cannot be null", commentContentNode);
        final int nodeType = commentContentNode.getType();
        Assert.assertTrue("Comment node should be a comment type",
            TokenUtils.isCommentType(nodeType));
        Assert.assertEquals("This should be a javadoc comment",
            "/*", commentContentNode.getParent().getText());
        final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
        parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
        final Object child = parseTree.getChild(commentContentNode, 0);

        Assert.assertFalse("Child has not to be leaf", parseTree.isLeaf(child));
        Assert.assertTrue("Child has to be leaf", parseTree.isLeaf(tree.getFirstChild()));

        final Object treeModel = parseTree.getValueAt(child, 0);
        final String type = (String) parseTree.getValueAt(child, 1);
        final int line = (int) parseTree.getValueAt(child, 2);
        final int column = (int) parseTree.getValueAt(child, 3);
        final String text = (String) parseTree.getValueAt(child, 4);
        final String expectedText = "JAVADOC";

        Assert.assertNull("Tree model must be null", treeModel);
        Assert.assertEquals("Invalid type", "JAVADOC", type);
        Assert.assertEquals("Invalid line", 1, line);
        Assert.assertEquals("Invalid column", 3, column);
        Assert.assertEquals("Invalid text", expectedText, text);

        try {
            parseTree.getValueAt(child, parseTree.getColumnCount());
            Assert.fail("IllegalStateException expected");
        }
        catch (IllegalStateException ex) {
            Assert.assertEquals("Invalid error message", "Unknown column", ex.getMessage());
        }

    }

    @Test
    public void testColumnMethods() {
        final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
        Assert.assertSame("Invalid type", ParseTreeTableModel.class, parseTree.getColumnClass(0));
        Assert.assertSame("Invalid type", String.class, parseTree.getColumnClass(1));
        Assert.assertSame("Invalid type", Integer.class, parseTree.getColumnClass(2));
        Assert.assertSame("Invalid type", Integer.class, parseTree.getColumnClass(3));
        Assert.assertSame("Invalid type", String.class, parseTree.getColumnClass(4));

        try {
            parseTree.getColumnClass(parseTree.getColumnCount());
            Assert.fail("IllegalStateException expected");
        }
        catch (IllegalStateException ex) {
            Assert.assertEquals("Invalid error message", "Unknown column", ex.getMessage());
        }

        Assert.assertFalse("Invalid cell editable status", parseTree.isCellEditable(1));

        Assert.assertEquals("Invalid column count", 5, parseTree.getColumnCount());
        Assert.assertEquals("Invalid column name", "Tree", parseTree.getColumnName(0));
        Assert.assertEquals("Invalid column name", "Type", parseTree.getColumnName(1));
        Assert.assertEquals("Invalid column name", "Line", parseTree.getColumnName(2));
        Assert.assertEquals("Invalid column name", "Column", parseTree.getColumnName(3));
        Assert.assertEquals("Invalid column name", "Text", parseTree.getColumnName(4));

    }

}