View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
23  import static org.junit.Assert.assertTrue;
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.Assert;
31  import org.junit.Test;
32  
33  import antlr.NoViableAltException;
34  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
35  import com.puppycrawl.tools.checkstyle.api.FileText;
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          assertTrue("Constructor is not private",
47                  isUtilsClassHasPrivateConstructor(AstTreeStringPrinter.class, true));
48      }
49  
50      @Test
51      public void testParseFileThrowable() throws Exception {
52          final File input = new File(getNonCompilablePath("InputAstTreeStringPrinter.java"));
53          try {
54              AstTreeStringPrinter.printFileAst(input,
55                      AstTreeStringPrinter.PrintOptions.WITHOUT_COMMENTS);
56              Assert.fail("exception expected");
57          }
58          catch (CheckstyleException ex) {
59              Assert.assertSame("Invalid class",
60                      NoViableAltException.class, ex.getCause().getClass());
61              Assert.assertEquals("Invalid exception message",
62                      input.getAbsolutePath() + ":1:1: unexpected token: classD",
63                      ex.getCause().toString());
64          }
65      }
66  
67      @Test
68      public void testParseFile() throws Exception {
69          verifyAst(getPath("ExpectedAstTreeStringPrinter.txt"),
70                  getPath("InputAstTreeStringPrinterComments.java"),
71                  AstTreeStringPrinter.PrintOptions.WITHOUT_COMMENTS);
72      }
73  
74      @Test
75      public void testPrintAst() throws Exception {
76          final FileText text = new FileText(
77                  new File(getPath("InputAstTreeStringPrinterComments.java")).getAbsoluteFile(),
78                  System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
79          final String actual = AstTreeStringPrinter.printAst(text,
80                  AstTreeStringPrinter.PrintOptions.WITHOUT_COMMENTS);
81          final String expected = new String(Files.readAllBytes(Paths.get(
82                  getPath("ExpectedAstTreeStringPrinter.txt"))), StandardCharsets.UTF_8);
83  
84          Assert.assertEquals("Print AST output is invalid", expected, actual);
85      }
86  
87      @Test
88      public void testParseFileWithComments() throws Exception {
89          verifyAst(getPath("ExpectedAstTreeStringPrinterComments.txt"),
90                  getPath("InputAstTreeStringPrinterComments.java"),
91                  AstTreeStringPrinter.PrintOptions.WITH_COMMENTS);
92      }
93  
94      @Test
95      public void testParseFileWithJavadoc1() throws Exception {
96          verifyJavaAndJavadocAst(getPath("ExpectedAstTreeStringPrinterJavadoc.txt"),
97                  getPath("InputAstTreeStringPrinterJavadoc.java"));
98      }
99  
100     @Test
101     public void testParseFileWithJavadoc2() throws Exception {
102         verifyJavaAndJavadocAst(getPath("ExpectedAstTreeStringPrinterJavaAndJavadoc.txt"),
103                 getPath("InputAstTreeStringPrinterJavaAndJavadoc.java"));
104     }
105 
106     @Test
107     public void testParseFileWithJavadoc3() throws Exception {
108         verifyJavaAndJavadocAst(
109                 getPath("ExpectedAstTreeStringPrinterAttributesAndMethodsJavadoc.txt"),
110                 getPath("InputAstTreeStringPrinterAttributesAndMethodsJavadoc.java")
111         );
112     }
113 
114     @Test
115     public void testJavadocPosition() throws Exception {
116         verifyJavaAndJavadocAst(getPath("ExpectedAstTreeStringPrinterJavadocPosition.txt"),
117                 getPath("InputAstTreeStringPrinterJavadocPosition.java"));
118     }
119 
120     @Test
121     public void testAstTreeBlockComments() throws Exception {
122         verifyAst(getPath("ExpectedAstTreeStringPrinterFullOfBlockComments.txt"),
123                 getPath("InputAstTreeStringPrinterFullOfBlockComments.java"),
124                 AstTreeStringPrinter.PrintOptions.WITH_COMMENTS);
125     }
126 
127     @Test
128     public void testAstTreeBlockCommentsCarriageReturn() throws Exception {
129         verifyAst(getPath("ExpectedAstTreeStringPrinterFullOfBlockCommentsCR.txt"),
130                 getPath("InputAstTreeStringPrinterFullOfBlockCommentsCR.java"),
131                 AstTreeStringPrinter.PrintOptions.WITH_COMMENTS);
132     }
133 
134     @Test
135     public void testAstTreeSingleLineComments() throws Exception {
136         verifyAst(getPath("ExpectedAstTreeStringPrinterFullOfSinglelineComments.txt"),
137                 getPath("InputAstTreeStringPrinterFullOfSinglelineComments.java"),
138                 AstTreeStringPrinter.PrintOptions.WITH_COMMENTS);
139     }
140 
141 }