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.filters;
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.DefaultConfiguration;
27  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
28  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
29  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
30  import com.puppycrawl.tools.checkstyle.api.Violation;
31  
32  public class SeverityMatchFilterTest {
33  
34      private final SeverityMatchFilter filter = new SeverityMatchFilter();
35  
36      @Test
37      public void testDefault() {
38          final AuditEvent ev = new AuditEvent(this, "Test.java");
39          assertWithMessage("no message")
40              .that(filter.accept(ev))
41              .isFalse();
42          final SeverityLevel errorLevel = SeverityLevel.ERROR;
43          final Violation errorMessage =
44              new Violation(1, 0, "", "", null,
45                  errorLevel, null, getClass(), null);
46          final AuditEvent ev2 = new AuditEvent(this, "ATest.java", errorMessage);
47          assertWithMessage("level:" + errorLevel)
48                  .that(filter.accept(ev2))
49                  .isTrue();
50          final SeverityLevel infoLevel = SeverityLevel.INFO;
51          final Violation infoViolation =
52                  new Violation(1, 0, "", "", null, infoLevel, null, getClass(), null);
53          final AuditEvent ev3 = new AuditEvent(this, "ATest.java", infoViolation);
54          assertWithMessage("level:" + infoLevel)
55              .that(filter.accept(ev3))
56              .isFalse();
57      }
58  
59      @Test
60      public void testSeverity() {
61          filter.setSeverity(SeverityLevel.INFO);
62          final AuditEvent ev = new AuditEvent(this, "Test.java");
63          // event with no message has severity level INFO
64          assertWithMessage("no message")
65                  .that(filter.accept(ev))
66                  .isTrue();
67          final SeverityLevel errorLevel = SeverityLevel.ERROR;
68          final Violation errorMessage =
69              new Violation(1, 0, "", "", null,
70                  errorLevel, null, getClass(), null);
71          final AuditEvent ev2 = new AuditEvent(this, "ATest.java", errorMessage);
72          assertWithMessage("level:" + errorLevel)
73              .that(filter.accept(ev2))
74              .isFalse();
75          final SeverityLevel infoLevel = SeverityLevel.INFO;
76          final Violation infoMessage =
77                  new Violation(1, 0, "", "", null, infoLevel, null, getClass(), null);
78          final AuditEvent ev3 = new AuditEvent(this, "ATest.java", infoMessage);
79          assertWithMessage("level:" + infoLevel)
80                  .that(filter.accept(ev3))
81                  .isTrue();
82      }
83  
84      @Test
85      public void testAcceptOnMatch() {
86          filter.setSeverity(SeverityLevel.INFO);
87          filter.setAcceptOnMatch(false);
88          final AuditEvent ev = new AuditEvent(this, "Test.java");
89          // event with no message has severity level INFO
90          assertWithMessage("no message")
91              .that(filter.accept(ev))
92              .isFalse();
93          final SeverityLevel errorLevel = SeverityLevel.ERROR;
94          final Violation errorViolation =
95              new Violation(1, 0, "", "", null,
96                  errorLevel, null, getClass(), null);
97          final AuditEvent ev2 = new AuditEvent(this, "ATest.java", errorViolation);
98          assertWithMessage("level:" + errorLevel)
99                  .that(filter.accept(ev2))
100                 .isTrue();
101         final SeverityLevel infoLevel = SeverityLevel.INFO;
102         final Violation infoViolation = new Violation(1, 0, "", "", null, infoLevel,
103             null, getClass(), null);
104         final AuditEvent ev3 = new AuditEvent(this, "ATest.java", infoViolation);
105         assertWithMessage("level:" + infoLevel)
106             .that(filter.accept(ev3))
107             .isFalse();
108     }
109 
110     @Test
111     public void testConfigure() throws CheckstyleException {
112         filter.configure(new DefaultConfiguration("test"));
113         assertWithMessage("object exists")
114             .that(filter)
115             .isNotNull();
116     }
117 
118 }