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 org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  
27  import java.io.IOException;
28  import java.io.PrintWriter;
29  
30  import org.junit.Test;
31  
32  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
33  import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
34  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
35  import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
36  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
37  import com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream;
38  
39  /**
40   * Enter a description of class XMLLoggerTest.java.
41   * @author Rick Giles
42   */
43  // -@cs[AbbreviationAsWordInName] Test should be named as its main class.
44  public class XMLLoggerTest extends AbstractXmlTestSupport {
45      private final CloseAndFlushTestByteArrayOutputStream outStream =
46          new CloseAndFlushTestByteArrayOutputStream();
47  
48      @Override
49      protected String getPackageLocation() {
50          return "com/puppycrawl/tools/checkstyle/xmllogger";
51      }
52  
53      @Test
54      public void testEncode()
55              throws IOException {
56          final XMLLogger test = new XMLLogger(outStream, false);
57          assertNotNull("should be able to create XMLLogger without issue", test);
58          final String[][] encodings = {
59              {"<", "&lt;"},
60              {">", "&gt;"},
61              {"'", "&apos;"},
62              {"\"", "&quot;"},
63              {"&", "&amp;"},
64              {"&lt;", "&amp;lt;"},
65              {"abc;", "abc;"},
66              {"&#0;", "&amp;#0;"},
67              {"&#0", "&amp;#0"},
68              {"&#X0;", "&amp;#X0;"},
69              {"\u0001", "#x1;"},
70              {"\u0080", "#x80;"},
71          };
72          for (String[] encoding : encodings) {
73              final String encoded = XMLLogger.encode(encoding[0]);
74              assertEquals("\"" + encoding[0] + "\"", encoding[1], encoded);
75          }
76          outStream.close();
77      }
78  
79      @Test
80      public void testIsReference()
81              throws IOException {
82          final XMLLogger test = new XMLLogger(outStream, false);
83          assertNotNull("should be able to create XMLLogger without issue", test);
84          final String[] references = {
85              "&#0;",
86              "&#x0;",
87              "&lt;",
88              "&gt;",
89              "&apos;",
90              "&quot;",
91              "&amp;",
92          };
93          for (String reference : references) {
94              assertTrue("reference: " + reference,
95                      XMLLogger.isReference(reference));
96          }
97          final String[] noReferences = {
98              "&",
99              "&;",
100             "&#;",
101             "&#a;",
102             "&#X0;",
103             "&#x;",
104             "&#xg;",
105             "ref",
106         };
107         for (String noReference : noReferences) {
108             assertFalse("no reference: " + noReference,
109                     XMLLogger.isReference(noReference));
110         }
111 
112         outStream.close();
113     }
114 
115     @Test
116     public void testCloseStream()
117             throws Exception {
118         final XMLLogger logger = new XMLLogger(outStream,
119                 AutomaticBean.OutputStreamOptions.CLOSE);
120         logger.auditStarted(null);
121         logger.auditFinished(null);
122 
123         assertEquals("Invalid close count", 1, outStream.getCloseCount());
124 
125         verifyXml(getPath("ExpectedXMLLoggerEmpty.xml"), outStream);
126     }
127 
128     @Test
129     public void testNoCloseStream()
130             throws Exception {
131         final XMLLogger logger = new XMLLogger(outStream,
132                 AutomaticBean.OutputStreamOptions.NONE);
133         logger.auditStarted(null);
134         logger.auditFinished(null);
135 
136         assertEquals("Invalid close count", 0, outStream.getCloseCount());
137 
138         outStream.close();
139         verifyXml(getPath("ExpectedXMLLoggerEmpty.xml"), outStream);
140     }
141 
142     @Test
143     public void testFileStarted()
144             throws Exception {
145         final XMLLogger logger = new XMLLogger(outStream, true);
146         logger.auditStarted(null);
147         final AuditEvent ev = new AuditEvent(this, "Test.java");
148         logger.fileStarted(ev);
149         logger.fileFinished(ev);
150         logger.auditFinished(null);
151         verifyXml(getPath("ExpectedXMLLogger.xml"), outStream);
152     }
153 
154     @Test
155     public void testFileFinished()
156             throws Exception {
157         final XMLLogger logger = new XMLLogger(outStream, true);
158         logger.auditStarted(null);
159         final AuditEvent ev = new AuditEvent(this, "Test.java");
160         logger.fileFinished(ev);
161         logger.auditFinished(null);
162         verifyXml(getPath("ExpectedXMLLogger.xml"), outStream);
163     }
164 
165     @Test
166     public void testAddError() throws Exception {
167         final XMLLogger logger = new XMLLogger(outStream, true);
168         logger.auditStarted(null);
169         final LocalizedMessage message =
170             new LocalizedMessage(1, 1,
171                 "messages.properties", "key", null, SeverityLevel.ERROR, null,
172                     getClass(), null);
173         final AuditEvent ev = new AuditEvent(this, "Test.java", message);
174         logger.fileStarted(ev);
175         logger.addError(ev);
176         logger.fileFinished(ev);
177         logger.auditFinished(null);
178         verifyXml(getPath("ExpectedXMLLoggerError.xml"), outStream, message.getMessage());
179     }
180 
181     @Test
182     public void testAddErrorWithNullFileName() throws Exception {
183         final XMLLogger logger = new XMLLogger(outStream, true);
184         logger.auditStarted(null);
185         final LocalizedMessage message =
186                 new LocalizedMessage(1, 1,
187                         "messages.properties", "key", null, SeverityLevel.ERROR, null,
188                         getClass(), null);
189         final AuditEvent ev = new AuditEvent(this, null, message);
190         logger.addError(ev);
191         logger.auditFinished(null);
192         verifyXml(getPath("ExpectedXMLLoggerErrorNullFileName.xml"), outStream,
193                 message.getMessage());
194     }
195 
196     @Test
197     public void testAddErrorModuleId() throws Exception {
198         final XMLLogger logger = new XMLLogger(outStream, true);
199         logger.auditStarted(null);
200         final LocalizedMessage message =
201             new LocalizedMessage(1, 1,
202                 "messages.properties", "key", null, SeverityLevel.ERROR, "module",
203                     getClass(), null);
204         final AuditEvent ev = new AuditEvent(this, "Test.java", message);
205         logger.addError(ev);
206         logger.auditFinished(null);
207         verifyXml(getPath("ExpectedXMLLoggerErrorModuleId.xml"), outStream, message.getMessage());
208     }
209 
210     @Test
211     public void testAddErrorOnZeroColumns() throws Exception {
212         final XMLLogger logger = new XMLLogger(outStream, true);
213         logger.auditStarted(null);
214         final LocalizedMessage message =
215                 new LocalizedMessage(1, 0,
216                         "messages.properties", "key", null, SeverityLevel.ERROR, null,
217                         getClass(), null);
218         final AuditEvent ev = new AuditEvent(this, "Test.java", message);
219         logger.fileStarted(ev);
220         logger.addError(ev);
221         logger.fileFinished(ev);
222         logger.auditFinished(null);
223         verifyXml(getPath("ExpectedXMLLoggerErrorZeroColumn.xml"), outStream,
224                 message.getMessage());
225     }
226 
227     @Test
228     public void testAddIgnored() throws Exception {
229         final XMLLogger logger = new XMLLogger(outStream, true);
230         logger.auditStarted(null);
231         final LocalizedMessage message =
232                 new LocalizedMessage(1, 1,
233                         "messages.properties", "key", null, SeverityLevel.IGNORE, null,
234                         getClass(), null);
235         final AuditEvent ev = new AuditEvent(this, "Test.java", message);
236         logger.addError(ev);
237         logger.auditFinished(null);
238         verifyXml(getPath("ExpectedXMLLoggerEmpty.xml"), outStream);
239     }
240 
241     @Test
242     public void testAddException()
243             throws Exception {
244         final XMLLogger logger = new XMLLogger(outStream, true);
245         logger.auditStarted(null);
246         final LocalizedMessage message =
247             new LocalizedMessage(1, 1,
248                 "messages.properties", null, null, null, getClass(), null);
249         final AuditEvent ev = new AuditEvent(this, "Test.java", message);
250         logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
251         logger.auditFinished(null);
252         verifyXml(getPath("ExpectedXMLLoggerException.xml"), outStream);
253         assertEquals("Invalid close count", 1, outStream.getCloseCount());
254     }
255 
256     @Test
257     public void testAddExceptionWithNullFileName()
258             throws Exception {
259         final XMLLogger logger = new XMLLogger(outStream, true);
260         logger.auditStarted(null);
261         final LocalizedMessage message =
262                 new LocalizedMessage(1, 1,
263                         "messages.properties", null, null, null, getClass(), null);
264         final AuditEvent ev = new AuditEvent(this, null, message);
265         logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
266         logger.auditFinished(null);
267         verifyXml(getPath("ExpectedXMLLoggerExceptionNullFileName.xml"), outStream);
268         assertEquals("Invalid close count", 1, outStream.getCloseCount());
269     }
270 
271     @Test
272     public void testAddExceptionAfterFileStarted()
273             throws Exception {
274         final XMLLogger logger = new XMLLogger(outStream, true);
275         logger.auditStarted(null);
276 
277         final AuditEvent fileStartedEvent = new AuditEvent(this, "Test.java");
278         logger.fileStarted(fileStartedEvent);
279 
280         final LocalizedMessage message =
281                 new LocalizedMessage(1, 1,
282                         "messages.properties", null, null, null, getClass(), null);
283         final AuditEvent ev = new AuditEvent(this, "Test.java", message);
284         logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
285 
286         logger.fileFinished(ev);
287         logger.auditFinished(null);
288         verifyXml(getPath("ExpectedXMLLoggerException2.xml"), outStream);
289         assertEquals("Invalid close count", 1, outStream.getCloseCount());
290     }
291 
292     @Test
293     public void testAddExceptionBeforeFileFinished()
294             throws Exception {
295         final XMLLogger logger = new XMLLogger(outStream, true);
296         logger.auditStarted(null);
297         final LocalizedMessage message =
298                 new LocalizedMessage(1, 1,
299                         "messages.properties", null, null, null, getClass(), null);
300         final AuditEvent ev = new AuditEvent(this, "Test.java", message);
301         logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
302         final AuditEvent fileFinishedEvent = new AuditEvent(this, "Test.java");
303         logger.fileFinished(fileFinishedEvent);
304         logger.auditFinished(null);
305         verifyXml(getPath("ExpectedXMLLoggerException3.xml"), outStream);
306         assertEquals("Invalid close count", 1, outStream.getCloseCount());
307     }
308 
309     @Test
310     public void testAddExceptionBetweenFileStartedAndFinished()
311             throws Exception {
312         final XMLLogger logger = new XMLLogger(outStream, true);
313         logger.auditStarted(null);
314         final LocalizedMessage message =
315                 new LocalizedMessage(1, 1,
316                         "messages.properties", null, null, null, getClass(), null);
317         final AuditEvent fileStartedEvent = new AuditEvent(this, "Test.java");
318         logger.fileStarted(fileStartedEvent);
319         final AuditEvent ev = new AuditEvent(this, "Test.java", message);
320         logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
321         final AuditEvent fileFinishedEvent = new AuditEvent(this, "Test.java");
322         logger.fileFinished(fileFinishedEvent);
323         logger.auditFinished(null);
324         verifyXml(getPath("ExpectedXMLLoggerException2.xml"), outStream);
325         assertEquals("Invalid close count", 1, outStream.getCloseCount());
326     }
327 
328     @Test
329     public void testAuditFinishedWithoutFileFinished() throws Exception {
330         final XMLLogger logger = new XMLLogger(outStream, true);
331         logger.auditStarted(null);
332         final AuditEvent fileStartedEvent = new AuditEvent(this, "Test.java");
333         logger.fileStarted(fileStartedEvent);
334 
335         final LocalizedMessage message =
336                 new LocalizedMessage(1, 1,
337                         "messages.properties", "key", null, SeverityLevel.ERROR, null,
338                         getClass(), null);
339         final AuditEvent errorEvent = new AuditEvent(this, "Test.java", message);
340         logger.addError(errorEvent);
341 
342         logger.fileFinished(errorEvent);
343         logger.auditFinished(null);
344         verifyXml(getPath("ExpectedXMLLoggerError.xml"), outStream, message.getMessage());
345     }
346 
347     @Test
348     public void testFinishLocalSetup() throws CheckstyleException {
349         final XMLLogger logger = new XMLLogger(outStream, true);
350         logger.finishLocalSetup();
351         logger.auditStarted(null);
352         logger.auditFinished(null);
353         assertNotNull("instance should not be null", logger);
354     }
355 
356     private static class TestException extends RuntimeException {
357 
358         private static final long serialVersionUID = 1L;
359 
360         TestException(String msg, Throwable cause) {
361             super(msg, cause);
362         }
363 
364         @Override
365         public void printStackTrace(PrintWriter printWriter) {
366             printWriter.print("stackTrace\r\nexample");
367         }
368     }
369 }