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;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.File;
27  import java.lang.reflect.Method;
28  import java.util.Optional;
29  
30  import org.junit.Test;
31  import org.powermock.reflect.Whitebox;
32  
33  import com.puppycrawl.tools.checkstyle.api.DetailAST;
34  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
36  
37  public class JavaParserTest extends AbstractModuleTestSupport {
38  
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/javaparser";
42      }
43  
44      @Test
45      public void testIsProperUtilsClass() throws ReflectiveOperationException {
46          assertTrue("Constructor is not private", TestUtil.isUtilsClassHasPrivateConstructor(
47              JavaParser.class, false));
48      }
49  
50      @Test
51      public void testAppendHiddenBlockCommentNodes() throws Exception {
52          final DetailAST root =
53              JavaParser.parseFile(new File(getPath("InputJavaParserHiddenComments.java")),
54                  JavaParser.Options.WITH_COMMENTS);
55  
56          final Optional<DetailAST> blockComment = TestUtil.findTokenInAstByPredicate(root,
57              ast -> ast.getType() == TokenTypes.BLOCK_COMMENT_BEGIN);
58  
59          assertTrue("Block comment should be present", blockComment.isPresent());
60  
61          final DetailAST commentContent = blockComment.get().getFirstChild();
62          final DetailAST commentEnd = blockComment.get().getLastChild();
63  
64          assertEquals("Unexpected line number", 3, commentContent.getLineNo());
65          assertEquals("Unexpected column number", 2, commentContent.getColumnNo());
66          assertEquals("Unexpected line number", 9, commentEnd.getLineNo());
67          assertEquals("Unexpected column number", 1, commentEnd.getColumnNo());
68      }
69  
70      @Test
71      public void testAppendHiddenSingleLineCommentNodes() throws Exception {
72          final DetailAST root =
73              JavaParser.parseFile(new File(getPath("InputJavaParserHiddenComments.java")),
74                  JavaParser.Options.WITH_COMMENTS);
75  
76          final Optional<DetailAST> singleLineComment = TestUtil.findTokenInAstByPredicate(root,
77              ast -> ast.getType() == TokenTypes.SINGLE_LINE_COMMENT);
78          assertTrue("Single line comment should be present", singleLineComment.isPresent());
79  
80          final DetailAST commentContent = singleLineComment.get().getFirstChild();
81  
82          assertEquals("Unexpected token type", TokenTypes.COMMENT_CONTENT, commentContent.getType());
83          assertEquals("Unexpected line number", 13, commentContent.getLineNo());
84          assertEquals("Unexpected column number", 2, commentContent.getColumnNo());
85          assertTrue("Unexpected comment content",
86              commentContent.getText().startsWith(" inline comment"));
87      }
88  
89      /**
90       * Could not find proper test case to test pitest mutations functionally.
91       * Should be rewritten during grammar update.
92       *
93       * @throws Exception when code tested throws exception
94       */
95      @Test
96      public void testIsPositionGreater() throws Exception {
97          final DetailAST ast1 = createAst(1, 3);
98          final DetailAST ast2 = createAst(1, 2);
99          final DetailAST ast3 = createAst(2, 2);
100 
101         final TreeWalker treeWalker = new TreeWalker();
102         final Method isPositionGreater = Whitebox.getMethod(JavaParser.class,
103                 "isPositionGreater", DetailAST.class, DetailAST.class);
104 
105         assertTrue("Should return true when lines are equal and column is greater",
106                 (boolean) isPositionGreater.invoke(treeWalker, ast1, ast2));
107         assertFalse("Should return false when lines are equal columns are equal",
108                 (boolean) isPositionGreater.invoke(treeWalker, ast1, ast1));
109         assertTrue("Should return true when line is greater",
110                 (boolean) isPositionGreater.invoke(treeWalker, ast3, ast1));
111     }
112 
113     private static DetailAST createAst(int line, int column) {
114         final DetailAST ast = new DetailAST();
115         ast.setLineNo(line);
116         ast.setColumnNo(column);
117         return ast;
118     }
119 
120 }