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 org.junit.jupiter.api.Assertions.assertThrows;
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       Due to low level configuration setup of FilterSet, conventional
109       input validation cannot be done here hence, pure JUnit testing has been
110       done for the time being
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         assertThrows(UnsupportedOperationException.class,
119             () -> subFilterSet.add(filter));
120     }
121 
122     /*
123       Input based test does not call toString, but this method might
124       be useful for third party integrations
125     */
126     @Test
127     public void testEmptyToString() {
128         final FilterSet filterSet = new FilterSet();
129         assertWithMessage("toString() result shouldn't be an empty string")
130                 .that(filterSet.toString())
131                 .isNotEmpty();
132     }
133 
134     private static final class DummyFilter implements Filter {
135 
136         private final boolean acceptValue;
137 
138         private DummyFilter(boolean accept) {
139             acceptValue = accept;
140         }
141 
142         @Override
143         public boolean accept(AuditEvent event) {
144             return acceptValue;
145         }
146 
147         @Override
148         public int hashCode() {
149             return Objects.hash(!acceptValue);
150         }
151 
152         @Override
153         public boolean equals(Object object) {
154             if (getClass() != object.getClass()) {
155                 return false;
156             }
157             final DummyFilter other = (DummyFilter) object;
158             return Boolean.compare(acceptValue, other.acceptValue) == 0;
159         }
160 
161     }
162 
163 }