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.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
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
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 }