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