View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.JavadocDetailNodeParser.MSG_JAVADOC_PARSE_RULE_ERROR;
24  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
25  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
26  
27  import java.io.File;
28  
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.ParseErrorMessage;
32  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
33  
34  public class DetailNodeTreeStringPrinterTest extends AbstractTreeTestSupport {
35  
36      @Override
37      public String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/detailnodetreestringprinter";
39      }
40  
41      @Test
42      public void testIsProperUtilsClass() throws ReflectiveOperationException {
43          assertWithMessage("Constructor is not private")
44                  .that(isUtilsClassHasPrivateConstructor(DetailNodeTreeStringPrinter.class))
45                  .isTrue();
46      }
47  
48      @Test
49      public void testParseFile() throws Exception {
50          verifyJavadocTree(getPath("ExpectedDetailNodeTreeStringPrinterJavadocComment.txt"),
51                  getPath("InputDetailNodeTreeStringPrinterJavadocComment.javadoc"));
52      }
53  
54      @Test
55      public void testNoUnnecessaryTextInJavadocAst() throws Exception {
56          verifyJavadocTree(
57                  getPath("ExpectedDetailNodeTreeStringPrinterNoUnnecessaryTextInJavadocAst.txt"),
58                  getPath("InputDetailNodeTreeStringPrinterNoUnnecessaryTextInJavadocAst.javadoc"));
59      }
60  
61      @Test
62      public void testParseErrorMessage() throws Exception {
63          final String actual = TestUtil.invokeStaticMethod(DetailNodeTreeStringPrinter.class,
64                  "getParseErrorMessage", String.class,
65                  new ParseErrorMessage(10, MSG_JAVADOC_PARSE_RULE_ERROR,
66                          9, "no viable alternative at input ' xyz'", "SOME_JAVADOC_ELEMENT"));
67          final LocalizedMessage violation = new LocalizedMessage(
68                  "com.puppycrawl.tools.checkstyle.checks.javadoc.messages",
69                  DetailNodeTreeStringPrinter.class,
70                  MSG_JAVADOC_PARSE_RULE_ERROR,
71                  9,
72                  "no viable alternative at input ' xyz'", "SOME_JAVADOC_ELEMENT");
73          final String expected = "[ERROR:10] " + violation.getMessage();
74          assertWithMessage("Javadoc parse error violation doesn't meet expectations")
75              .that(actual)
76              .isEqualTo(expected);
77      }
78  
79      @Test
80      public void testNoViableAltException() throws Exception {
81          final File file = new File(
82                  getPath("InputDetailNodeTreeStringPrinterNoViableAltException.javadoc"));
83          final IllegalArgumentException exc =
84                  getExpectedThrowable(IllegalArgumentException.class, () -> {
85                      DetailNodeTreeStringPrinter.printFileAst(file);
86                  }, "Exception is expected");
87          final String expected = TestUtil.invokeStaticMethod(DetailNodeTreeStringPrinter.class,
88                  "getParseErrorMessage", String.class,
89                  new ParseErrorMessage(0, MSG_JAVADOC_PARSE_RULE_ERROR,
90                          8, "no viable alternative at input 'see <'", "SEE_TAG"));
91          assertWithMessage("Generated and expected parse error messages don't match")
92              .that(exc.getMessage())
93              .isEqualTo(expected);
94      }
95  
96      @Test
97      public void testHtmlTagCloseBeforeTagOpen() throws Exception {
98          final File file = new File(
99                  getPath("InputDetailNodeTreeStringPrinterHtmlTagCloseBeforeTagOpen.javadoc"));
100         final IllegalArgumentException exc =
101                 getExpectedThrowable(IllegalArgumentException.class, () -> {
102                     DetailNodeTreeStringPrinter.printFileAst(file);
103                 }, "Exception is expected");
104         final String expected = TestUtil.invokeStaticMethod(DetailNodeTreeStringPrinter.class,
105                 "getParseErrorMessage", String.class,
106                 new ParseErrorMessage(0, MSG_JAVADOC_PARSE_RULE_ERROR,
107                         3, "no viable alternative at input '</'", "HTML_ELEMENT"));
108         assertWithMessage("Generated and expected parse error messages don't match")
109             .that(exc.getMessage())
110             .isEqualTo(expected);
111     }
112 }