1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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, JavaParser.Options.WITHOUT_COMMENTS);
55 Assert.fail("exception expected");
56 }
57 catch (CheckstyleException ex) {
58 Assert.assertSame("Invalid class",
59 NoViableAltException.class, ex.getCause().getClass());
60 Assert.assertEquals("Invalid exception message",
61 input.getAbsolutePath() + ":1:1: unexpected token: classD",
62 ex.getCause().toString());
63 }
64 }
65
66 @Test
67 public void testParseFile() throws Exception {
68 verifyAst(getPath("ExpectedAstTreeStringPrinter.txt"),
69 getPath("InputAstTreeStringPrinterComments.java"),
70 JavaParser.Options.WITHOUT_COMMENTS);
71 }
72
73 @Test
74 public void testPrintAst() throws Exception {
75 final FileText text = new FileText(
76 new File(getPath("InputAstTreeStringPrinterComments.java")).getAbsoluteFile(),
77 System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
78 final String actual = AstTreeStringPrinter.printAst(text,
79 JavaParser.Options.WITHOUT_COMMENTS);
80 final String expected = new String(Files.readAllBytes(Paths.get(
81 getPath("ExpectedAstTreeStringPrinter.txt"))), StandardCharsets.UTF_8);
82
83 Assert.assertEquals("Print AST output is invalid", expected, actual);
84 }
85
86 @Test
87 public void testParseFileWithComments() throws Exception {
88 verifyAst(getPath("ExpectedAstTreeStringPrinterComments.txt"),
89 getPath("InputAstTreeStringPrinterComments.java"),
90 JavaParser.Options.WITH_COMMENTS);
91 }
92
93 @Test
94 public void testParseFileWithJavadoc1() throws Exception {
95 verifyJavaAndJavadocAst(getPath("ExpectedAstTreeStringPrinterJavadoc.txt"),
96 getPath("InputAstTreeStringPrinterJavadoc.java"));
97 }
98
99 @Test
100 public void testParseFileWithJavadoc2() throws Exception {
101 verifyJavaAndJavadocAst(getPath("ExpectedAstTreeStringPrinterJavaAndJavadoc.txt"),
102 getPath("InputAstTreeStringPrinterJavaAndJavadoc.java"));
103 }
104
105 @Test
106 public void testParseFileWithJavadoc3() throws Exception {
107 verifyJavaAndJavadocAst(
108 getPath("ExpectedAstTreeStringPrinterAttributesAndMethodsJavadoc.txt"),
109 getPath("InputAstTreeStringPrinterAttributesAndMethodsJavadoc.java")
110 );
111 }
112
113 @Test
114 public void testJavadocPosition() throws Exception {
115 verifyJavaAndJavadocAst(getPath("ExpectedAstTreeStringPrinterJavadocPosition.txt"),
116 getPath("InputAstTreeStringPrinterJavadocPosition.java"));
117 }
118
119 @Test
120 public void testAstTreeBlockComments() throws Exception {
121 verifyAst(getPath("ExpectedAstTreeStringPrinterFullOfBlockComments.txt"),
122 getPath("InputAstTreeStringPrinterFullOfBlockComments.java"),
123 JavaParser.Options.WITH_COMMENTS);
124 }
125
126 @Test
127 public void testAstTreeBlockCommentsCarriageReturn() throws Exception {
128 verifyAst(getPath("ExpectedAstTreeStringPrinterFullOfBlockCommentsCR.txt"),
129 getPath("InputAstTreeStringPrinterFullOfBlockCommentsCR.java"),
130 JavaParser.Options.WITH_COMMENTS);
131 }
132
133 @Test
134 public void testAstTreeSingleLineComments() throws Exception {
135 verifyAst(getPath("ExpectedAstTreeStringPrinterFullOfSinglelineComments.txt"),
136 getPath("InputAstTreeStringPrinterFullOfSinglelineComments.java"),
137 JavaParser.Options.WITH_COMMENTS);
138 }
139
140 }