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 testAddErrorAndFileStart() {
56 final OutputStream outputStream = new ByteArrayOutputStream();
57 final MetadataGeneratorLogger logger = new MetadataGeneratorLogger(outputStream,
58 OutputStreamOptions.CLOSE);
59 final AuditEvent event = new AuditEvent(this, "fileName",
60 new Violation(1, 2, "bundle", "key",
61 null, SeverityLevel.ERROR, null, getClass(), "customViolation"));
62 logger.finishLocalSetup();
63 logger.fileStarted(event);
64 logger.addError(event);
65 logger.auditFinished(event);
66 assertWithMessage("Output stream should be empty")
67 .that(outputStream.toString())
68 .contains(event.getMessage());
69 logger.fileFinished(event);
70 }
71
72 @Test
73 public void testAddException() {
74 final OutputStream outputStream = new ByteArrayOutputStream();
75 final MetadataGeneratorLogger logger = new MetadataGeneratorLogger(outputStream,
76 OutputStreamOptions.CLOSE);
77 final AuditEvent event = new AuditEvent(1);
78 logger.addException(event, new IllegalStateException("Test Exception"));
79 logger.auditFinished(event);
80 assertWithMessage("Violation should contain exception message")
81 .that(outputStream.toString())
82 .contains("java.lang.IllegalStateException: Test Exception");
83 }
84
85 @Test
86 public void testClose() throws IOException {
87 try (CloseAndFlushTestByteArrayOutputStream outputStream =
88 new CloseAndFlushTestByteArrayOutputStream()) {
89 final MetadataGeneratorLogger logger = new MetadataGeneratorLogger(outputStream,
90 OutputStreamOptions.CLOSE);
91 logger.auditFinished(new AuditEvent(1));
92 assertWithMessage("Unexpected close count")
93 .that(outputStream.getCloseCount())
94 .isEqualTo(1);
95 }
96 }
97
98 @Test
99 public void testCloseOutputStreamOptionNone() throws IOException {
100 try (CloseAndFlushTestByteArrayOutputStream outputStream =
101 new CloseAndFlushTestByteArrayOutputStream()) {
102 final MetadataGeneratorLogger logger = new MetadataGeneratorLogger(outputStream,
103 OutputStreamOptions.NONE);
104 final AuditEvent event = new AuditEvent(1);
105 logger.auditFinished(event);
106 assertWithMessage("Unexpected close count")
107 .that(outputStream.getCloseCount())
108 .isEqualTo(0);
109 }
110 }
111
112 @Test
113 public void testFlushStreams() throws Exception {
114 try (CloseAndFlushTestByteArrayOutputStream outputStream =
115 new CloseAndFlushTestByteArrayOutputStream()) {
116 final MetadataGeneratorLogger logger = new MetadataGeneratorLogger(outputStream,
117 OutputStreamOptions.NONE);
118 final AuditEvent event = new AuditEvent(1);
119 logger.auditStarted(event);
120 logger.fileFinished(event);
121 assertWithMessage("Unexpected flush count")
122 .that(outputStream.getFlushCount())
123 .isEqualTo(2);
124 logger.auditFinished(event);
125 assertWithMessage("Unexpected flush count")
126 .that(outputStream.getFlushCount())
127 .isEqualTo(3);
128 }
129 }
130 }