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  
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.io.OutputStream;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions;
31  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
32  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
33  import com.puppycrawl.tools.checkstyle.api.Violation;
34  import com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream;
35  
36  public class MetadataGeneratorLoggerTest {
37  
38      @Test
39      public void testIgnoreSeverityLevel() {
40          final OutputStream outputStream = new ByteArrayOutputStream();
41          final MetadataGeneratorLogger logger = new MetadataGeneratorLogger(outputStream,
42                  OutputStreamOptions.CLOSE);
43          final AuditEvent event = new AuditEvent(this, "fileName",
44                  new Violation(1, 2, "bundle", "key",
45                          null, SeverityLevel.IGNORE, null, getClass(), "customViolation"));
46          logger.finishLocalSetup();
47          logger.addError(event);
48          logger.auditFinished(event);
49          assertWithMessage("Output stream should be empty")
50                  .that(outputStream.toString())
51                  .isEmpty();
52      }
53  
54      @Test
55      public void testAddException() {
56          final OutputStream outputStream = new ByteArrayOutputStream();
57          final MetadataGeneratorLogger logger = new MetadataGeneratorLogger(outputStream,
58                  OutputStreamOptions.CLOSE);
59          final AuditEvent event = new AuditEvent(1);
60          logger.addException(event, new IllegalStateException("Test Exception"));
61          logger.auditFinished(event);
62          assertWithMessage("Violation should contain exception message")
63                  .that(outputStream.toString())
64                  .contains("java.lang.IllegalStateException: Test Exception");
65      }
66  
67      @Test
68      public void testClose() throws IOException {
69          try (CloseAndFlushTestByteArrayOutputStream outputStream =
70                       new CloseAndFlushTestByteArrayOutputStream()) {
71              final MetadataGeneratorLogger logger = new MetadataGeneratorLogger(outputStream,
72                      OutputStreamOptions.CLOSE);
73              logger.auditFinished(new AuditEvent(1));
74              assertWithMessage("Unexpected close count")
75                      .that(outputStream.getCloseCount())
76                      .isEqualTo(1);
77          }
78      }
79  
80      @Test
81      public void testCloseOutputStreamOptionNone() throws IOException {
82          try (CloseAndFlushTestByteArrayOutputStream outputStream =
83                       new CloseAndFlushTestByteArrayOutputStream()) {
84              final MetadataGeneratorLogger logger = new MetadataGeneratorLogger(outputStream,
85                      OutputStreamOptions.NONE);
86              final AuditEvent event = new AuditEvent(1);
87              logger.auditFinished(event);
88              assertWithMessage("Unexpected close count")
89                      .that(outputStream.getCloseCount())
90                      .isEqualTo(0);
91          }
92      }
93  
94      @Test
95      public void testFlushStreams() throws Exception {
96          try (CloseAndFlushTestByteArrayOutputStream outputStream =
97                       new CloseAndFlushTestByteArrayOutputStream()) {
98              final MetadataGeneratorLogger logger = new MetadataGeneratorLogger(outputStream,
99                      OutputStreamOptions.NONE);
100             final AuditEvent event = new AuditEvent(1);
101             logger.auditStarted(event);
102             logger.fileFinished(event);
103             assertWithMessage("Unexpected flush count")
104                     .that(outputStream.getFlushCount())
105                     .isEqualTo(2);
106             logger.auditFinished(event);
107             assertWithMessage("Unexpected flush count")
108                     .that(outputStream.getFlushCount())
109                     .isEqualTo(3);
110         }
111     }
112 }