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 org.junit.jupiter.api.Test;
25
26 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
27 import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
28 import com.puppycrawl.tools.checkstyle.api.Violation;
29 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
30
31 public class AuditEventDefaultFormatterTest {
32
33 @Test
34 public void testFormatFullyQualifiedModuleNameContainsCheckSuffix() {
35 final Violation violation = new Violation(1, 1, null, null, null,
36 SeverityLevel.WARNING, null, TestModuleCheck.class, "Mocked violation.");
37 final AuditEvent event = new AuditEvent("", "InputMockFile.java", violation);
38 final AuditEventFormatter formatter = new AuditEventDefaultFormatter();
39
40 final String expected = "[WARN] InputMockFile.java:1:1: Mocked violation. "
41 + "[AuditEventDefaultFormatterTest$TestModule]";
42
43 assertWithMessage("Invalid format")
44 .that(formatter.format(event))
45 .isEqualTo(expected);
46 }
47
48 @Test
49 public void testFormatFullyQualifiedModuleNameDoesNotContainCheckSuffix() {
50 final Violation violation = new Violation(1, 1, null, null, null,
51 SeverityLevel.WARNING, null, TestModule.class, "Mocked violation.");
52 final AuditEvent event = new AuditEvent("", "InputMockFile.java", violation);
53 final AuditEventFormatter formatter = new AuditEventDefaultFormatter();
54
55 final String expected = "[WARN] InputMockFile.java:1:1: Mocked violation. "
56 + "[AuditEventDefaultFormatterTest$TestModule]";
57
58 assertWithMessage("Invalid format")
59 .that(formatter.format(event))
60 .isEqualTo(expected);
61 }
62
63 @Test
64 public void testFormatModuleWithModuleId() {
65 final Violation violation = new Violation(1, 1, null, null, null,
66 SeverityLevel.WARNING, "ModuleId", TestModule.class, "Mocked violation.");
67 final AuditEvent event = new AuditEvent("", "InputMockFile.java", violation);
68 final AuditEventFormatter formatter = new AuditEventDefaultFormatter();
69
70 final String expected = "[WARN] InputMockFile.java:1:1: Mocked violation. [ModuleId]";
71
72 assertWithMessage("Invalid format")
73 .that(formatter.format(event))
74 .isEqualTo(expected);
75 }
76
77 @Test
78 public void testCalculateBufferLength() throws Exception {
79 final Violation violation = new Violation(1, 1,
80 "messages.properties", "key", null, SeverityLevel.ERROR, null,
81 getClass(), null);
82 final AuditEvent auditEvent = new AuditEvent(new Object(), "fileName", violation);
83 final StringBuilder result = TestUtil.invokeStaticMethod(AuditEventDefaultFormatter.class,
84 "initStringBuilderWithOptimalBuffer", auditEvent, SeverityLevel.ERROR.toString());
85
86 assertWithMessage("Buffer length is not expected")
87 .that(result.capacity())
88 .isEqualTo(56);
89 }
90
91 private static final class TestModuleCheck {
92
93
94
95 }
96
97 private static final class TestModule {
98
99
100
101 }
102
103 }