View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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_MISSED_HTML_CLOSE;
24  import static com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.MSG_JAVADOC_PARSE_RULE_ERROR;
25  import static com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.MSG_JAVADOC_WRONG_SINGLETON_TAG;
26  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
27  
28  import java.io.File;
29  
30  import org.junit.jupiter.api.Test;
31  
32  import com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.ParseErrorMessage;
33  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
34  
35  public class DetailNodeTreeStringPrinterTest extends AbstractTreeTestSupport {
36  
37      @Override
38      protected String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/detailnodetreestringprinter";
40      }
41  
42      @Test
43      public void testIsProperUtilsClass() throws ReflectiveOperationException {
44          assertWithMessage("Constructor is not private")
45                  .that(isUtilsClassHasPrivateConstructor(DetailNodeTreeStringPrinter.class))
46                  .isTrue();
47      }
48  
49      @Test
50      public void testParseFile() throws Exception {
51          verifyJavadocTree(getPath("ExpectedDetailNodeTreeStringPrinterJavadocComment.txt"),
52                  getPath("InputDetailNodeTreeStringPrinterJavadocComment.javadoc"));
53      }
54  
55      @Test
56      public void testParseFileWithError() throws Exception {
57          final File file = new File(
58                  getPath("InputDetailNodeTreeStringPrinterJavadocWithError.javadoc"));
59          try {
60              DetailNodeTreeStringPrinter.printFileAst(file);
61              assertWithMessage("Javadoc parser didn't fail on missing end tag").fail();
62          }
63          catch (IllegalArgumentException ex) {
64              final String expected = TestUtil.invokeStaticMethod(DetailNodeTreeStringPrinter.class,
65                      "getParseErrorMessage",
66                      new ParseErrorMessage(0, MSG_JAVADOC_MISSED_HTML_CLOSE, 1, "qwe"));
67              assertWithMessage("Generated and expected parse error messages don't match")
68                  .that(ex.getMessage())
69                  .isEqualTo(expected);
70          }
71      }
72  
73      @Test
74      public void testNoUnnecessaryTextInJavadocAst() throws Exception {
75          verifyJavadocTree(
76                  getPath("ExpectedDetailNodeTreeStringPrinterNoUnnecessaryTextInJavadocAst.txt"),
77                  getPath("InputDetailNodeTreeStringPrinterNoUnnecessaryTextInJavadocAst.javadoc"));
78      }
79  
80      @Test
81      public void testMissedHtmlTagParseErrorMessage() throws Exception {
82          final String actual = TestUtil.invokeStaticMethod(DetailNodeTreeStringPrinter.class,
83                  "getParseErrorMessage",
84                  new ParseErrorMessage(35, MSG_JAVADOC_MISSED_HTML_CLOSE, 7, "xyz"));
85          final LocalizedMessage violation = new LocalizedMessage(
86                  "com.puppycrawl.tools.checkstyle.checks.javadoc.messages",
87                  DetailNodeTreeStringPrinter.class,
88                  MSG_JAVADOC_MISSED_HTML_CLOSE,
89                  7,
90                  "xyz");
91          final String expected = "[ERROR:35] " + violation.getMessage();
92          assertWithMessage("Javadoc parse error violation for missed HTML tag "
93                  + "doesn't meet expectations")
94              .that(actual)
95              .isEqualTo(expected);
96      }
97  
98      @Test
99      public void testParseErrorMessage() throws Exception {
100         final String actual = TestUtil.invokeStaticMethod(DetailNodeTreeStringPrinter.class,
101                 "getParseErrorMessage",
102                 new ParseErrorMessage(10, MSG_JAVADOC_PARSE_RULE_ERROR,
103                         9, "no viable alternative at input ' xyz'", "SOME_JAVADOC_ELEMENT"));
104         final LocalizedMessage violation = new LocalizedMessage(
105                 "com.puppycrawl.tools.checkstyle.checks.javadoc.messages",
106                 DetailNodeTreeStringPrinter.class,
107                 MSG_JAVADOC_PARSE_RULE_ERROR,
108                 9,
109                 "no viable alternative at input ' xyz'", "SOME_JAVADOC_ELEMENT");
110         final String expected = "[ERROR:10] " + violation.getMessage();
111         assertWithMessage("Javadoc parse error violation doesn't meet expectations")
112             .that(actual)
113             .isEqualTo(expected);
114     }
115 
116     @Test
117     public void testWrongSingletonParseErrorMessage() throws Exception {
118         final String actual = TestUtil.invokeStaticMethod(DetailNodeTreeStringPrinter.class,
119                 "getParseErrorMessage",
120                 new ParseErrorMessage(100, MSG_JAVADOC_WRONG_SINGLETON_TAG,
121                         9, "tag"));
122         final LocalizedMessage violation = new LocalizedMessage(
123                 "com.puppycrawl.tools.checkstyle.checks.javadoc.messages",
124                 DetailNodeTreeStringPrinter.class,
125                 MSG_JAVADOC_WRONG_SINGLETON_TAG,
126                 9,
127                 "tag");
128         final String expected = "[ERROR:100] " + violation.getMessage();
129         assertWithMessage("Javadoc parse error violation for void elements with close tag "
130                     + "doesn't meet expectations")
131             .that(actual)
132             .isEqualTo(expected);
133     }
134 
135     @Test
136     public void testUnescapedJavaCodeWithGenericsInJavadoc() throws Exception {
137         final File file = new File(
138                 getPath("InputDetailNodeTreeStringPrinter"
139                         + "UnescapedJavaCodeWithGenericsInJavadoc.javadoc"));
140         try {
141             DetailNodeTreeStringPrinter.printFileAst(file);
142             assertWithMessage("Exception is expected").fail();
143         }
144         catch (IllegalArgumentException ex) {
145             final String expected = TestUtil.invokeStaticMethod(DetailNodeTreeStringPrinter.class,
146                     "getParseErrorMessage",
147                     new ParseErrorMessage(35, MSG_JAVADOC_MISSED_HTML_CLOSE, 7, "parsing"));
148             assertWithMessage("Generated and expected parse error messages don't match")
149                 .that(ex.getMessage())
150                 .isEqualTo(expected);
151         }
152     }
153 
154     @Test
155     public void testNoViableAltException() throws Exception {
156         final File file = new File(
157                 getPath("InputDetailNodeTreeStringPrinterNoViableAltException.javadoc"));
158         try {
159             DetailNodeTreeStringPrinter.printFileAst(file);
160             assertWithMessage("Exception is expected").fail();
161         }
162         catch (IllegalArgumentException ex) {
163             final String expected = TestUtil.invokeStaticMethod(DetailNodeTreeStringPrinter.class,
164                     "getParseErrorMessage",
165                     new ParseErrorMessage(0, MSG_JAVADOC_PARSE_RULE_ERROR,
166                             9, "no viable alternative at input '<<'", "HTML_ELEMENT"));
167             assertWithMessage("Generated and expected parse error messages don't match")
168                 .that(ex.getMessage())
169                 .isEqualTo(expected);
170         }
171     }
172 
173     @Test
174     public void testHtmlTagCloseBeforeTagOpen() throws Exception {
175         final File file = new File(
176                 getPath("InputDetailNodeTreeStringPrinterHtmlTagCloseBeforeTagOpen.javadoc"));
177         try {
178             DetailNodeTreeStringPrinter.printFileAst(file);
179             assertWithMessage("Exception is expected").fail();
180         }
181         catch (IllegalArgumentException ex) {
182             final String expected = TestUtil.invokeStaticMethod(DetailNodeTreeStringPrinter.class,
183                     "getParseErrorMessage",
184                     new ParseErrorMessage(0, MSG_JAVADOC_PARSE_RULE_ERROR,
185                             4, "no viable alternative at input '</tag'", "HTML_ELEMENT"));
186             assertWithMessage("Generated and expected parse error messages don't match")
187                 .that(ex.getMessage())
188                 .isEqualTo(expected);
189         }
190     }
191 
192     @Test
193     public void testWrongHtmlTagOrder() throws Exception {
194         final File file = new File(
195                 getPath("InputDetailNodeTreeStringPrinterWrongHtmlTagOrder.javadoc"));
196         try {
197             DetailNodeTreeStringPrinter.printFileAst(file);
198             assertWithMessage("Exception is expected").fail();
199         }
200         catch (IllegalArgumentException ex) {
201             final String expected = TestUtil.invokeStaticMethod(DetailNodeTreeStringPrinter.class,
202                     "getParseErrorMessage",
203                     new ParseErrorMessage(0, MSG_JAVADOC_MISSED_HTML_CLOSE, 10, "tag2"));
204             assertWithMessage("Generated and expected parse error messages don't match")
205                 .that(ex.getMessage())
206                 .isEqualTo(expected);
207         }
208     }
209 
210     @Test
211     public void testOmittedStartTagForHtmlElement() throws Exception {
212         final File file = new File(
213                 getPath("InputDetailNodeTreeStringPrinterOmittedStartTagForHtmlElement.javadoc"));
214         try {
215             DetailNodeTreeStringPrinter.printFileAst(file);
216             assertWithMessage("Exception is expected").fail();
217         }
218         catch (IllegalArgumentException ex) {
219             final String expected = TestUtil.invokeStaticMethod(DetailNodeTreeStringPrinter.class,
220                     "getParseErrorMessage",
221                     new ParseErrorMessage(0, MSG_JAVADOC_MISSED_HTML_CLOSE, 3, "a"));
222             assertWithMessage("Generated and expected parse error messages don't match")
223                 .that(ex.getMessage())
224                 .isEqualTo(expected);
225         }
226     }
227 
228 }