aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinterTest.java
blob: 6d77ef350aed4fa4f5487ab1b7f1597db73eab5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2017 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////

package com.puppycrawl.tools.checkstyle;

import static com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.MSG_JAVADOC_MISSED_HTML_CLOSE;
import static com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.MSG_JAVADOC_PARSE_RULE_ERROR;
import static com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.MSG_JAVADOC_WRONG_SINGLETON_TAG;
import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.lang.reflect.Method;

import org.junit.Assert;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

import com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.ParseErrorMessage;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;

public class DetailNodeTreeStringPrinterTest extends AbstractTreeTestSupport {

    // [REFLECTION]
    // DetailNodeTreeStringPrinter#getParseErrorMessage is used for creating error messages
    // for validating those obtained in UTs against the ones created.
    private static final Method GET_PARSE_ERROR_MESSAGE = Whitebox.getMethod(
            DetailNodeTreeStringPrinter.class, "getParseErrorMessage", ParseErrorMessage.class);

    @Override
    protected String getPackageLocation() {
        return "com/puppycrawl/tools/checkstyle/detailnodetreestringprinter";
    }

    @Test
    public void testIsProperUtilsClass() throws ReflectiveOperationException {
        assertTrue("Constructor is not private",
                isUtilsClassHasPrivateConstructor(DetailNodeTreeStringPrinter.class, true));
    }

    @Test
    public void testParseFile() throws Exception {
        verifyJavadocTree(getPath("ExpectedDetailNodeTreeStringPrinterJavadocComment.txt"),
                getPath("InputDetailNodeTreeStringPrinterJavadocComment.javadoc"));
    }

    @Test
    public void testParseFileWithError() throws Exception {
        try {
            DetailNodeTreeStringPrinter.printFileAst(
                    new File(getPath("InputDetailNodeTreeStringPrinterJavadocWithError.javadoc")));
            Assert.fail("Javadoc parser didn't fail on missing end tag");
        }
        catch (IllegalArgumentException ex) {
            final String expected = (String) GET_PARSE_ERROR_MESSAGE.invoke(null,
                    new ParseErrorMessage(0, MSG_JAVADOC_MISSED_HTML_CLOSE, 1, "qwe"));
            assertEquals("Generated and expected parse error messages don't match",
                    expected, ex.getMessage());
        }
    }

    @Test
    public void testNoUnnecessaryTextInJavadocAst() throws Exception {
        verifyJavadocTree(
                getPath("ExpectedDetailNodeTreeStringPrinterNoUnnecessaryTextInJavadocAst.txt"),
                getPath("InputDetailNodeTreeStringPrinterNoUnnecessaryTextInJavadocAst.javadoc"));
    }

    @Test
    public void testMissedHtmlTagParseErrorMessage() throws Exception {
        final String actual = (String) GET_PARSE_ERROR_MESSAGE.invoke(null,
                new ParseErrorMessage(35, MSG_JAVADOC_MISSED_HTML_CLOSE, 7, "xyz"));
        final LocalizedMessage localizedMessage = new LocalizedMessage(
                35,
                "com.puppycrawl.tools.checkstyle.checks.javadoc.messages",
                MSG_JAVADOC_MISSED_HTML_CLOSE,
                new Object[] {7, "xyz"},
                "",
                DetailNodeTreeStringPrinter.class,
                null);
        final String expected = "[ERROR:35] " + localizedMessage.getMessage();
        assertEquals("Javadoc parse error message for missed HTML tag doesn't meet expectations",
                expected, actual);
    }

    @Test
    public void testParseErrorMessage() throws Exception {
        final String actual = (String) GET_PARSE_ERROR_MESSAGE.invoke(null,
                new ParseErrorMessage(10, MSG_JAVADOC_PARSE_RULE_ERROR,
                        9, "no viable alternative at input ' xyz'", "SOME_JAVADOC_ELEMENT"));
        final LocalizedMessage localizedMessage = new LocalizedMessage(
                10,
                "com.puppycrawl.tools.checkstyle.checks.javadoc.messages",
                MSG_JAVADOC_PARSE_RULE_ERROR,
                new Object[] {9, "no viable alternative at input ' xyz'", "SOME_JAVADOC_ELEMENT"},
                "",
                DetailNodeTreeStringPrinter.class,
                null);
        final String expected = "[ERROR:10] " + localizedMessage.getMessage();
        assertEquals("Javadoc parse error message doesn't meet expectations", expected, actual);
    }

    @Test
    public void testWrongSingletonParseErrorMessage() throws Exception {
        final String actual = (String) GET_PARSE_ERROR_MESSAGE.invoke(null,
                new ParseErrorMessage(100, MSG_JAVADOC_WRONG_SINGLETON_TAG,
                        9, "tag"));
        final LocalizedMessage localizedMessage = new LocalizedMessage(
                100,
                "com.puppycrawl.tools.checkstyle.checks.javadoc.messages",
                MSG_JAVADOC_WRONG_SINGLETON_TAG,
                new Object[] {9, "tag"},
                "",
                DetailNodeTreeStringPrinter.class,
                null);
        final String expected = "[ERROR:100] " + localizedMessage.getMessage();
        assertEquals("Javadoc parse error message for void elements with close tag "
                + "doesn't meet expectations", expected, actual);
    }

    @Test
    public void testUnescapedJavaCodeWithGenericsInJavadoc() throws Exception {
        try {
            DetailNodeTreeStringPrinter.printFileAst(new File(
                    getPath("InputDetailNodeTreeStringPrinter"
                            + "UnescapedJavaCodeWithGenericsInJavadoc.javadoc")));
            Assert.fail("Exception is expected");
        }
        catch (IllegalArgumentException ex) {
            final String expected = (String) GET_PARSE_ERROR_MESSAGE.invoke(null,
                    new ParseErrorMessage(35, MSG_JAVADOC_MISSED_HTML_CLOSE, 7, "parsing"));
            assertEquals("Generated and expected parse error messages don't match",
                    expected, ex.getMessage());
        }
    }

    @Test
    public void testNoViableAltException() throws Exception {
        try {
            DetailNodeTreeStringPrinter.printFileAst(new File(getPath(
                    "InputDetailNodeTreeStringPrinterNoViableAltException.javadoc")));
            Assert.fail("Exception is expected");
        }
        catch (IllegalArgumentException ex) {
            final String expected = (String) GET_PARSE_ERROR_MESSAGE.invoke(null,
                    new ParseErrorMessage(0, MSG_JAVADOC_PARSE_RULE_ERROR,
                            9, "no viable alternative at input '<<'", "HTML_ELEMENT"));
            assertEquals("Generated and expected parse error messages don't match",
                    expected, ex.getMessage());
        }
    }

    @Test
    public void testHtmlTagCloseBeforeTagOpen() throws Exception {
        try {
            DetailNodeTreeStringPrinter.printFileAst(new File(getPath(
                    "InputDetailNodeTreeStringPrinterHtmlTagCloseBeforeTagOpen.javadoc"
            )));
            Assert.fail("Exception is expected");
        }
        catch (IllegalArgumentException ex) {
            final String expected = (String) GET_PARSE_ERROR_MESSAGE.invoke(null,
                    new ParseErrorMessage(0, MSG_JAVADOC_PARSE_RULE_ERROR,
                            4, "no viable alternative at input '</tag'", "HTML_ELEMENT"));
            assertEquals("Generated and expected parse error messages don't match",
                    expected, ex.getMessage());
        }
    }

    @Test
    public void testWrongHtmlTagOrder() throws Exception {
        try {
            DetailNodeTreeStringPrinter.printFileAst(new File(getPath(
                    "InputDetailNodeTreeStringPrinterWrongHtmlTagOrder.javadoc"
            )));
            Assert.fail("Exception is expected");
        }
        catch (IllegalArgumentException ex) {
            final String expected = (String) GET_PARSE_ERROR_MESSAGE.invoke(null,
                    new ParseErrorMessage(0, MSG_JAVADOC_MISSED_HTML_CLOSE, 10, "tag2"));
            assertEquals("Generated and expected parse error messages don't match",
                    expected, ex.getMessage());
        }
    }

    @Test
    public void testOmittedStartTagForHtmlElement() throws Exception {
        try {
            DetailNodeTreeStringPrinter.printFileAst(new File(getPath(
                    "InputDetailNodeTreeStringPrinterOmittedStartTagForHtmlElement.javadoc"
            )));
            Assert.fail("Exception is expected");
        }
        catch (IllegalArgumentException ex) {
            final String expected = (String) GET_PARSE_ERROR_MESSAGE.invoke(null,
                    new ParseErrorMessage(0, MSG_JAVADOC_MISSED_HTML_CLOSE, 3, "a"));
            assertEquals("Generated and expected parse error messages don't match",
                    expected, ex.getMessage());
        }
    }

}