1 //////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code for adherence to a set of rules. 3 // Copyright (C) 2001-2019 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 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 * @param filename file name. 35 * @return canonical path for the file with the given file name. 36 * @throws IOException if I/O exception occurs while forming the path. 37 */ 38 protected final String getNonCompilablePath(String filename) throws IOException { 39 return new File("src/test/resources-noncompilable/" + getPackageLocation() + "/" 40 + filename).getCanonicalPath(); 41 } 42 43 /** 44 * Performs verification of the given text ast tree representation. 45 * @param expectedTextPrintFileName expected text ast tree representation. 46 * @param actualJavaFileName actual text ast tree representation. 47 * @param withComments whether to perform verification of comment nodes in tree. 48 * @throws Exception if exception occurs during verification. 49 */ 50 protected static void verifyAst(String expectedTextPrintFileName, String actualJavaFileName, 51 JavaParser.Options withComments) 52 throws Exception { 53 final String expectedContents = readFile(expectedTextPrintFileName); 54 55 final String actualContents = AstTreeStringPrinter.printFileAst( 56 new File(actualJavaFileName), withComments).replaceAll(CRLF_REGEX, LF_REGEX); 57 58 assertEquals("Generated AST from Java file should match pre-defined AST", expectedContents, 59 actualContents); 60 } 61 62 /** 63 * Performs verification of the given text ast tree representation. 64 * This implementation uses 65 * {@link AbstractTreeTestSupport#verifyAst(String, String, JavaParser.Options)} 66 * method inside. 67 * @param expectedTextPrintFileName expected text ast tree representation. 68 * @param actualJavaFileName actual text ast tree representation. 69 * @throws Exception if exception occurs during verification. 70 */ 71 protected static void verifyAst(String expectedTextPrintFileName, String actualJavaFileName) 72 throws Exception { 73 verifyAst(expectedTextPrintFileName, actualJavaFileName, 74 JavaParser.Options.WITHOUT_COMMENTS); 75 } 76 77 /** 78 * Verifies the java and javadoc AST generated for the supplied java file against 79 * the expected AST in supplied text file. 80 * @param expectedTextPrintFilename name of the file having the expected ast. 81 * @param actualJavaFilename name of the java file. 82 * @throws Exception if exception occurs during verification. 83 */ 84 protected static void verifyJavaAndJavadocAst(String expectedTextPrintFilename, 85 String actualJavaFilename) throws Exception { 86 final String expectedContents = readFile(expectedTextPrintFilename); 87 88 final String actualContents = AstTreeStringPrinter.printJavaAndJavadocTree( 89 new File(actualJavaFilename)).replaceAll(CRLF_REGEX, LF_REGEX); 90 91 assertEquals("Generated AST from the java file should match the pre-defined AST", 92 expectedContents, actualContents); 93 } 94 95 /** 96 * Verifies the javadoc tree generated for the supplied javadoc file against the expected tree 97 * in the supplied text file. 98 * @param expectedTextPrintFilename name of the text file having the expected tree. 99 * @param actualJavadocFilename name of the file containing the javadoc. 100 * @throws Exception if exception occurs during verification. 101 */ 102 protected static void verifyJavadocTree(String expectedTextPrintFilename, 103 String actualJavadocFilename) throws Exception { 104 final String expectedContents = readFile(expectedTextPrintFilename); 105 106 final String actualContents = DetailNodeTreeStringPrinter.printFileAst( 107 new File(actualJavadocFilename)).replaceAll(CRLF_REGEX, LF_REGEX); 108 109 assertEquals("Generated tree from the javadoc file should match the pre-defined tree", 110 expectedContents, actualContents); 111 } 112 113 }