1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 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
24 import java.io.File;
25 import java.io.IOException;
26
27 public abstract class AbstractTreeTestSupport extends AbstractPathTestSupport {
28
29 /**
30 * Returns canonical path for the file with the given file name.
31 * The path is formed base on the non-compilable resources location.
32 * This implementation uses 'src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/'
33 * as a non-compilable resource location.
34 *
35 * @param filename file name.
36 * @return canonical path for the file with the given file name.
37 * @throws IOException if I/O exception occurs while forming the path.
38 */
39 protected final String getNonCompilablePath(String filename) throws IOException {
40 return new File("src/test/resources-noncompilable/" + getPackageLocation() + "/"
41 + filename).getCanonicalPath();
42 }
43
44 /**
45 * Performs verification of the given text ast tree representation.
46 *
47 * @param expectedTextPrintFileName expected text ast tree representation.
48 * @param actualJavaFileName actual text ast tree representation.
49 * @param withComments whether to perform verification of comment nodes in tree.
50 * @throws Exception if exception occurs during verification.
51 */
52 protected static void verifyAst(String expectedTextPrintFileName, String actualJavaFileName,
53 JavaParser.Options withComments)
54 throws Exception {
55 final String expectedContents = readFile(expectedTextPrintFileName);
56
57 final String actualContents = toLfLineEnding(AstTreeStringPrinter.printFileAst(
58 new File(actualJavaFileName), withComments));
59
60 assertWithMessage("Generated AST from Java file should match pre-defined AST")
61 .that(actualContents)
62 .isEqualTo(expectedContents);
63 }
64
65 /**
66 * Performs verification of the given text ast tree representation.
67 * This implementation uses
68 * {@link AbstractTreeTestSupport#verifyAst(String, String, JavaParser.Options)}
69 * method inside.
70 *
71 * @param expectedTextPrintFileName expected text ast tree representation.
72 * @param actualJavaFileName actual text ast tree representation.
73 * @throws Exception if exception occurs during verification.
74 */
75 protected static void verifyAst(String expectedTextPrintFileName, String actualJavaFileName)
76 throws Exception {
77 verifyAst(expectedTextPrintFileName, actualJavaFileName,
78 JavaParser.Options.WITHOUT_COMMENTS);
79 }
80
81 /**
82 * Verifies the java and javadoc AST generated for the supplied java file against
83 * the expected AST in supplied text file.
84 *
85 * @param expectedTextPrintFilename name of the file having the expected ast.
86 * @param actualJavaFilename name of the java file.
87 * @throws Exception if exception occurs during verification.
88 */
89 protected static void verifyJavaAndJavadocAst(String expectedTextPrintFilename,
90 String actualJavaFilename) throws Exception {
91 final String expectedContents = readFile(expectedTextPrintFilename);
92
93 final String actualContents = toLfLineEnding(AstTreeStringPrinter.printJavaAndJavadocTree(
94 new File(actualJavaFilename)));
95
96 assertWithMessage("Generated AST from the java file should match the pre-defined AST")
97 .that(actualContents)
98 .isEqualTo(expectedContents);
99 }
100
101 /**
102 * Verifies the javadoc tree generated for the supplied javadoc file against the expected tree
103 * in the supplied text file.
104 *
105 * @param expectedTextPrintFilename name of the text file having the expected tree.
106 * @param actualJavadocFilename name of the file containing the javadoc.
107 * @throws Exception if exception occurs during verification.
108 */
109 protected static void verifyJavadocTree(String expectedTextPrintFilename,
110 String actualJavadocFilename) throws Exception {
111 final String expectedContents = readFile(expectedTextPrintFilename);
112
113 final String actualContents = toLfLineEnding(DetailNodeTreeStringPrinter.printFileAst(
114 new File(actualJavadocFilename)));
115
116 assertWithMessage("Generated tree from the javadoc file should match the pre-defined tree")
117 .that(actualContents)
118 .isEqualTo(expectedContents);
119 }
120
121 }