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.assertNotNull;
23  import static org.junit.Assert.assertTrue;
24  import static org.mockito.Mockito.spy;
25  import static org.mockito.Mockito.times;
26  import static org.mockito.Mockito.verify;
27  
28  import java.io.ByteArrayOutputStream;
29  import java.io.OutputStream;
30  import java.io.UnsupportedEncodingException;
31  import java.nio.charset.StandardCharsets;
32  
33  import org.junit.Test;
34  
35  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
36  import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
37  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
38  import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
39  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
40  
41  public class DefaultLoggerTest {
42  
43      @Test
44      public void testCtor() throws UnsupportedEncodingException {
45          final OutputStream infoStream = new ByteArrayOutputStream();
46          final ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
47          final DefaultLogger dl = new DefaultLogger(infoStream, true, errorStream, true);
48          dl.addException(new AuditEvent(5000, "myfile"), new IllegalStateException("upsss"));
49          dl.auditFinished(new AuditEvent(6000, "myfile"));
50          final String output = errorStream.toString(StandardCharsets.UTF_8.name());
51          final LocalizedMessage addExceptionMessage = new LocalizedMessage(0,
52                  Definitions.CHECKSTYLE_BUNDLE, DefaultLogger.ADD_EXCEPTION_MESSAGE,
53                  new String[] {"myfile"}, null,
54                  getClass(), null);
55  
56          assertTrue("Invalid exception", output.contains(addExceptionMessage.getMessage()));
57          assertTrue("Invalid exception class",
58              output.contains("java.lang.IllegalStateException: upsss"));
59      }
60  
61      @Test
62      public void testCtorWithTwoParameters() {
63          final OutputStream infoStream = new ByteArrayOutputStream();
64          final DefaultLogger dl = new DefaultLogger(infoStream, true);
65          dl.addException(new AuditEvent(5000, "myfile"), new IllegalStateException("upsss"));
66          dl.auditFinished(new AuditEvent(6000, "myfile"));
67          final String output = infoStream.toString();
68          assertTrue("Message should contain exception info, but was " + output,
69                  output.contains("java.lang.IllegalStateException: upsss"));
70      }
71  
72      @Test
73      public void testNewCtor() throws Exception {
74          final OutputStream infoStream = spy(new ByteArrayOutputStream());
75          final ByteArrayOutputStream errorStream = spy(new ByteArrayOutputStream());
76          final DefaultLogger dl = new DefaultLogger(infoStream,
77                  AutomaticBean.OutputStreamOptions.CLOSE, errorStream,
78                  AutomaticBean.OutputStreamOptions.CLOSE);
79          dl.auditStarted(null);
80          dl.addException(new AuditEvent(5000, "myfile"), new IllegalStateException("upsss"));
81          dl.auditFinished(new AuditEvent(6000, "myfile"));
82          final String output = errorStream.toString(StandardCharsets.UTF_8.name());
83          final LocalizedMessage addExceptionMessage = new LocalizedMessage(0,
84                  Definitions.CHECKSTYLE_BUNDLE, DefaultLogger.ADD_EXCEPTION_MESSAGE,
85                  new String[] {"myfile"}, null,
86                  getClass(), null);
87          final LocalizedMessage startMessage = new LocalizedMessage(0,
88                  Definitions.CHECKSTYLE_BUNDLE, DefaultLogger.AUDIT_STARTED_MESSAGE,
89                  CommonUtils.EMPTY_STRING_ARRAY, null,
90                  getClass(), null);
91          final LocalizedMessage finishMessage = new LocalizedMessage(0,
92                  Definitions.CHECKSTYLE_BUNDLE, DefaultLogger.AUDIT_FINISHED_MESSAGE,
93                  CommonUtils.EMPTY_STRING_ARRAY, null,
94                  getClass(), null);
95  
96          verify(infoStream, times(1)).close();
97          verify(errorStream, times(1)).close();
98          final String infoOutput = infoStream.toString();
99          assertTrue("Message should contain exception info, but was " + infoOutput,
100                 infoOutput.contains(startMessage.getMessage()));
101         assertTrue("Message should contain exception info, but was " + infoOutput,
102                 infoOutput.contains(finishMessage.getMessage()));
103         assertTrue("Message should contain exception info, but was " + output,
104                 output.contains(addExceptionMessage.getMessage()));
105         assertTrue("Message should contain exception info, but was " + output,
106                 output.contains("java.lang.IllegalStateException: upsss"));
107     }
108 
109     @Test
110     public void testNewCtorWithTwoParameters() {
111         final OutputStream infoStream = new ByteArrayOutputStream();
112         final DefaultLogger dl = new DefaultLogger(infoStream,
113                 AutomaticBean.OutputStreamOptions.NONE);
114         dl.addException(new AuditEvent(5000, "myfile"), new IllegalStateException("upsss"));
115         dl.auditFinished(new AuditEvent(6000, "myfile"));
116         assertTrue("Message should contain exception info, but was " + infoStream,
117                 infoStream.toString().contains("java.lang.IllegalStateException: upsss"));
118     }
119 
120     @Test
121     public void testFinishLocalSetup() throws CheckstyleException {
122         final OutputStream infoStream = new ByteArrayOutputStream();
123         final DefaultLogger dl = new DefaultLogger(infoStream,
124                 AutomaticBean.OutputStreamOptions.CLOSE);
125         dl.finishLocalSetup();
126         dl.auditStarted(null);
127         dl.auditFinished(null);
128         assertNotNull("instance should not be null", dl);
129     }
130 }