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.api;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
24  
25  import org.junit.jupiter.api.Test;
26  
27  public class AuditEventTest {
28  
29      @Test
30      public void test() {
31          final AuditEvent event = new AuditEvent(getClass());
32  
33          assertWithMessage("invalid file name")
34                  .that(event.getFileName())
35                  .isNull();
36          assertWithMessage("invalid violation")
37                  .that(event.getViolation())
38                  .isNull();
39          assertWithMessage("invalid source")
40                  .that(event.getSource())
41                  .isEqualTo(getClass());
42          assertWithMessage("invalid severity")
43                  .that(event.getSeverityLevel())
44                  .isEqualTo(SeverityLevel.INFO);
45      }
46  
47      @Test
48      public void testNoSource() {
49          final IllegalArgumentException ex = getExpectedThrowable(IllegalArgumentException.class,
50                  () -> new AuditEvent(null),
51                  "IllegalArgumentException expected");
52          assertWithMessage("Invalid exception message")
53              .that(ex.getMessage())
54              .isEqualTo("null source");
55      }
56  
57      @Test
58      public void testFullConstructor() {
59          final Violation message = new Violation(1, 2, 3, "bundle", "key", null,
60                  SeverityLevel.ERROR, "moduleId", getClass(), "customMessage");
61          final AuditEvent event = new AuditEvent(getClass(), "fileName", message);
62  
63          assertWithMessage("invalid file name")
64                  .that(event.getFileName())
65                  .isEqualTo("fileName");
66          assertWithMessage("invalid violation")
67                  .that(event.getViolation())
68                  .isEqualTo(message);
69          assertWithMessage("invalid violation")
70                  .that(event.getMessage())
71                  .isEqualTo("customMessage");
72          assertWithMessage("invalid source")
73                  .that(event.getSource())
74                  .isEqualTo(getClass());
75          assertWithMessage("invalid line")
76                  .that(event.getLine())
77                  .isEqualTo(1);
78          assertWithMessage("invalid column")
79                  .that(event.getColumn())
80                  .isEqualTo(2);
81          assertWithMessage("invalid severity")
82                  .that(event.getSeverityLevel())
83                  .isEqualTo(SeverityLevel.ERROR);
84          assertWithMessage("invalid module id")
85                  .that(event.getModuleId())
86                  .isEqualTo("moduleId");
87          assertWithMessage("invalid source name")
88                  .that(event.getSourceName())
89                  .isEqualTo("com.puppycrawl.tools.checkstyle.api.AuditEventTest");
90      }
91  }