View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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 com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
24  
25  import java.io.File;
26  import java.nio.charset.StandardCharsets;
27  import java.nio.file.Files;
28  import java.nio.file.Paths;
29  
30  import org.junit.jupiter.api.Test;
31  
32  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
33  import com.puppycrawl.tools.checkstyle.api.DetailAST;
34  import com.puppycrawl.tools.checkstyle.api.FileText;
35  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
36  
37  public class AstTreeStringPrinterTest extends AbstractTreeTestSupport {
38  
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/asttreestringprinter";
42      }
43  
44      @Test
45      public void testIsProperUtilsClass() throws ReflectiveOperationException {
46          assertWithMessage("Constructor is not private")
47                  .that(isUtilsClassHasPrivateConstructor(AstTreeStringPrinter.class))
48                  .isTrue();
49      }
50  
51      @Test
52      public void testParseFileThrowable() throws Exception {
53          final File input = new File(getNonCompilablePath("InputAstTreeStringPrinter.java"));
54          try {
55              AstTreeStringPrinter.printFileAst(input, JavaParser.Options.WITHOUT_COMMENTS);
56              assertWithMessage("exception expected").fail();
57          }
58          catch (CheckstyleException ex) {
59              assertWithMessage("Invalid class")
60                  .that(ex.getCause())
61                  .isInstanceOf(IllegalStateException.class);
62              assertWithMessage("Invalid exception message")
63                  .that(ex.getCause().getMessage())
64                  .isEqualTo("2:0: no viable alternative at input 'classD'");
65          }
66      }
67  
68      @Test
69      public void testParseFile() throws Exception {
70          verifyAst(getPath("ExpectedAstTreeStringPrinter.txt"),
71                  getPath("InputAstTreeStringPrinterComments.java"),
72                  JavaParser.Options.WITHOUT_COMMENTS);
73      }
74  
75      @Test
76      public void testPrintBranch() throws Exception {
77          final DetailAST ast = JavaParser.parseFile(
78              new File(getPath("InputAstTreeStringPrinterPrintBranch.java")),
79              JavaParser.Options.WITH_COMMENTS);
80          final String expected = addEndOfLine(
81              "COMPILATION_UNIT -> COMPILATION_UNIT [1:0]",
82              "`--CLASS_DEF -> CLASS_DEF [3:0]",
83              "    |--MODIFIERS -> MODIFIERS [3:0]",
84              "    |   `--LITERAL_PUBLIC -> public [3:0]");
85          final DetailAST nodeToPrint = ast.getFirstChild().getNextSibling()
86                  .getFirstChild().getFirstChild();
87          final String result = AstTreeStringPrinter.printBranch(nodeToPrint);
88          assertWithMessage("Branches do not match")
89              .that(result)
90              .isEqualTo(expected);
91      }
92  
93      @Test
94      public void testPrintAst() throws Exception {
95          final FileText text = new FileText(
96                  new File(getPath("InputAstTreeStringPrinterComments.java")).getAbsoluteFile(),
97                  System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
98          final String actual = toLfLineEnding(AstTreeStringPrinter.printAst(text,
99                  JavaParser.Options.WITHOUT_COMMENTS));
100         final String expected = toLfLineEnding(Files.readString(Paths.get(
101                 getPath("ExpectedAstTreeStringPrinter.txt"))));
102 
103         assertWithMessage("Print AST output is invalid")
104             .that(actual)
105             .isEqualTo(expected);
106     }
107 
108     @Test
109     public void testParseFileWithComments() throws Exception {
110         verifyAst(getPath("ExpectedAstTreeStringPrinterComments.txt"),
111                 getPath("InputAstTreeStringPrinterComments.java"),
112                 JavaParser.Options.WITH_COMMENTS);
113     }
114 
115     @Test
116     public void testParseFileWithJavadoc1() throws Exception {
117         verifyJavaAndJavadocAst(getPath("ExpectedAstTreeStringPrinterJavadoc.txt"),
118                 getPath("InputAstTreeStringPrinterJavadoc.java"));
119     }
120 
121     @Test
122     public void testParseFileWithJavadoc2() throws Exception {
123         verifyJavaAndJavadocAst(getPath("ExpectedAstTreeStringPrinterJavaAndJavadoc.txt"),
124                 getPath("InputAstTreeStringPrinterJavaAndJavadoc.java"));
125     }
126 
127     @Test
128     public void testParseFileWithJavadoc3() throws Exception {
129         verifyJavaAndJavadocAst(
130                 getPath("ExpectedAstTreeStringPrinterAttributesAndMethodsJavadoc.txt"),
131                 getPath("InputAstTreeStringPrinterAttributesAndMethodsJavadoc.java")
132         );
133     }
134 
135     @Test
136     public void testJavadocPosition() throws Exception {
137         verifyJavaAndJavadocAst(getPath("ExpectedAstTreeStringPrinterJavadocPosition.txt"),
138                 getPath("InputAstTreeStringPrinterJavadocPosition.java"));
139     }
140 
141     @Test
142     public void testAstTreeBlockComments() throws Exception {
143         verifyAst(getPath("ExpectedAstTreeStringPrinterFullOfBlockComments.txt"),
144                 getPath("InputAstTreeStringPrinterFullOfBlockComments.java"),
145                 JavaParser.Options.WITH_COMMENTS);
146     }
147 
148     @Test
149     public void testAstTreeBlockCommentsCarriageReturn() throws Exception {
150         verifyAst(getPath("ExpectedAstTreeStringPrinterFullOfBlockCommentsCR.txt"),
151                 getPath("InputAstTreeStringPrinterFullOfBlockCommentsCR.java"),
152                 JavaParser.Options.WITH_COMMENTS);
153     }
154 
155     @Test
156     public void testAstTreeSingleLineComments() throws Exception {
157         verifyAst(getPath("ExpectedAstTreeStringPrinterFullOfSinglelineComments.txt"),
158                 getPath("InputAstTreeStringPrinterFullOfSinglelineComments.java"),
159                 JavaParser.Options.WITH_COMMENTS);
160     }
161 
162     @Test
163     public void testTextBlocksEscapesAreOneChar() throws Exception {
164         final String inputFilename = "InputAstTreeStringPrinterTextBlocksEscapesAreOneChar.java";
165         final DetailAST ast = JavaParser.parseFile(
166                 new File(getNonCompilablePath(inputFilename)), JavaParser.Options.WITHOUT_COMMENTS)
167                 .getFirstChild();
168 
169         final DetailAST objectBlockNode = ast.findFirstToken(TokenTypes.OBJBLOCK);
170         final DetailAST variableDefNode = objectBlockNode.findFirstToken(TokenTypes.VARIABLE_DEF);
171         final DetailAST textBlockContentNode =
172                 variableDefNode.findFirstToken(TokenTypes.ASSIGN)
173                         .findFirstToken(TokenTypes.EXPR)
174                         .getFirstChild()
175                         .findFirstToken(TokenTypes.TEXT_BLOCK_CONTENT);
176 
177         final String textBlockContent = textBlockContentNode.getText();
178 
179         assertWithMessage("Text block content contains \"\\n\" as substring")
180             .that(textBlockContent)
181             .doesNotContain("\\n");
182         assertWithMessage("Text block content line terminator is counted as one character")
183             .that(textBlockContent)
184             .hasLength(1);
185         assertWithMessage("Text block content contains only a line terminator")
186             .that(textBlockContent)
187             .matches("\n");
188     }
189 
190 }