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 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          // no code
94  
95      }
96  
97      private static final class TestModule {
98  
99          // no code
100 
101     }
102 
103 }