1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }