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.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 java.util.Objects;
26 import java.util.Set;
27
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.filters.SeverityMatchFilter;
31
32 public class FilterSetTest {
33
34 @Test
35 public void testGetFilters() {
36 final FilterSet filterSet = new FilterSet();
37 filterSet.addFilter(new SeverityMatchFilter());
38 assertWithMessage("Invalid filter set size")
39 .that(filterSet.getFilters())
40 .hasSize(1);
41 }
42
43 @Test
44 public void testRemoveFilters() {
45 final FilterSet filterSet = new FilterSet();
46 final Filter filter = new SeverityMatchFilter();
47 filterSet.addFilter(filter);
48 filterSet.removeFilter(filter);
49 assertWithMessage("Invalid filter set size")
50 .that(filterSet.getFilters())
51 .hasSize(0);
52 }
53
54 @Test
55 public void testToString() {
56 final FilterSet filterSet = new FilterSet();
57 filterSet.addFilter(new SeverityMatchFilter());
58 assertWithMessage("Invalid filter set size")
59 .that(filterSet.toString())
60 .isNotNull();
61 }
62
63 @Test
64 public void testClear() {
65 final FilterSet filterSet = new FilterSet();
66 filterSet.addFilter(new SeverityMatchFilter());
67
68 assertWithMessage("Invalid filter set size")
69 .that(filterSet.getFilters())
70 .hasSize(1);
71
72 filterSet.clear();
73
74 assertWithMessage("Invalid filter set size")
75 .that(filterSet.getFilters())
76 .hasSize(0);
77 }
78
79 @Test
80 public void testAccept() {
81 final FilterSet filterSet = new FilterSet();
82 filterSet.addFilter(new DummyFilter(true));
83 assertWithMessage("invalid accept response")
84 .that(filterSet.accept(null))
85 .isTrue();
86 }
87
88 @Test
89 public void testNotAccept() {
90 final FilterSet filterSet = new FilterSet();
91 filterSet.addFilter(new DummyFilter(false));
92 assertWithMessage("invalid accept response")
93 .that(filterSet.accept(null))
94 .isFalse();
95 }
96
97 @Test
98 public void testNotAcceptEvenIfOneAccepts() {
99 final FilterSet filterSet = new FilterSet();
100 filterSet.addFilter(new DummyFilter(true));
101 filterSet.addFilter(new DummyFilter(false));
102 assertWithMessage("invalid accept response")
103 .that(filterSet.accept(null))
104 .isFalse();
105 }
106
107
108
109
110
111
112 @Test
113 public void testUnmodifiableSet() {
114 final FilterSet filterSet = new FilterSet();
115 final Filter filter = new FilterSet();
116 filterSet.addFilter(filter);
117 final Set<Filter> subFilterSet = filterSet.getFilters();
118 final Exception ex = getExpectedThrowable(UnsupportedOperationException.class,
119 () -> subFilterSet.add(filter));
120 assertWithMessage("Exception message not expected")
121 .that(ex.getClass())
122 .isEqualTo(UnsupportedOperationException.class);
123 }
124
125
126
127
128
129 @Test
130 public void testEmptyToString() {
131 final FilterSet filterSet = new FilterSet();
132 assertWithMessage("toString() result shouldn't be an empty string")
133 .that(filterSet.toString())
134 .isNotEmpty();
135 }
136
137 private static final class DummyFilter implements Filter {
138
139 private final boolean acceptValue;
140
141 private DummyFilter(boolean accept) {
142 acceptValue = accept;
143 }
144
145 @Override
146 public boolean accept(AuditEvent event) {
147 return acceptValue;
148 }
149
150 @Override
151 public int hashCode() {
152 return Objects.hash(!acceptValue);
153 }
154
155 @Override
156 public boolean equals(Object object) {
157 if (getClass() != object.getClass()) {
158 return false;
159 }
160 final DummyFilter other = (DummyFilter) object;
161 return Boolean.compare(acceptValue, other.acceptValue) == 0;
162 }
163
164 }
165
166 }