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