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 java.util.Set;
26  import java.util.regex.Pattern;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.filefilters.BeforeExecutionExclusionFileFilter;
31  
32  public class BeforeExecutionFileFilterSetTest {
33  
34      @Test
35      public void testRemoveFilters() {
36          final BeforeExecutionFileFilterSet filterSet = new BeforeExecutionFileFilterSet();
37          final BeforeExecutionFileFilter filter = new BeforeExecutionExclusionFileFilter();
38          filterSet.addBeforeExecutionFileFilter(filter);
39          filterSet.removeBeforeExecutionFileFilter(filter);
40          assertWithMessage("size is the same")
41                  .that(filterSet.getBeforeExecutionFileFilters())
42                  .isEmpty();
43      }
44  
45      @Test
46      public void testAccept() {
47          final String fileName = "BAD";
48          final BeforeExecutionExclusionFileFilter filter = new BeforeExecutionExclusionFileFilter();
49          filter.setFileNamePattern(Pattern.compile(fileName));
50          final BeforeExecutionFileFilterSet set = new BeforeExecutionFileFilterSet();
51          set.addBeforeExecutionFileFilter(filter);
52  
53          assertWithMessage("Invalid accept state, should accept")
54                  .that(set.accept("ATest.java"))
55                  .isTrue();
56      }
57  
58      @Test
59      public void testReject() {
60          final String fileName = "Test";
61          final BeforeExecutionExclusionFileFilter filter = new BeforeExecutionExclusionFileFilter();
62          filter.setFileNamePattern(Pattern.compile(fileName));
63          final BeforeExecutionFileFilterSet set = new BeforeExecutionFileFilterSet();
64          set.addBeforeExecutionFileFilter(filter);
65  
66          assertWithMessage("Invalid accept state, should not accept")
67                  .that(set.accept("ATest.java"))
68                  .isFalse();
69      }
70  
71      @Test
72      public void testGetFilters2() {
73          final BeforeExecutionFileFilterSet filterSet = new BeforeExecutionFileFilterSet();
74          filterSet.addBeforeExecutionFileFilter(new BeforeExecutionExclusionFileFilter());
75          assertWithMessage("size is the same")
76                  .that(filterSet.getBeforeExecutionFileFilters())
77                  .hasSize(1);
78      }
79  
80      @Test
81      public void testToString2() {
82          final BeforeExecutionFileFilterSet filterSet = new BeforeExecutionFileFilterSet();
83          filterSet.addBeforeExecutionFileFilter(new BeforeExecutionExclusionFileFilter());
84          assertWithMessage("size is the same")
85                  .that(filterSet.toString())
86                  .isNotNull();
87      }
88  
89      @Test
90      public void testClear() {
91          final BeforeExecutionFileFilterSet filterSet = new BeforeExecutionFileFilterSet();
92          filterSet.addBeforeExecutionFileFilter(new BeforeExecutionExclusionFileFilter());
93  
94          assertWithMessage("Invalid filter set size")
95                  .that(filterSet.getBeforeExecutionFileFilters())
96                  .hasSize(1);
97  
98          filterSet.clear();
99  
100         assertWithMessage("Invalid filter set size")
101                 .that(filterSet.getBeforeExecutionFileFilters())
102                 .isEmpty();
103     }
104 
105     /*
106       Due to low level configuration setup of BeforeExecutionFileFilterSet, conventional
107       input validation cannot be done here hence, pure JUnit testing has been
108       done for the time being
109     */
110     @Test
111     public void testUnmodifiableSet() {
112         final BeforeExecutionFileFilterSet filterSet = new BeforeExecutionFileFilterSet();
113         final BeforeExecutionFileFilter filter = new BeforeExecutionExclusionFileFilter();
114         filterSet.addBeforeExecutionFileFilter(filter);
115         final Set<BeforeExecutionFileFilter> excFilterSet =
116             filterSet.getBeforeExecutionFileFilters();
117         final Exception ex = getExpectedThrowable(UnsupportedOperationException.class,
118             () -> excFilterSet.add(filter));
119         assertWithMessage("Exception message is not expected")
120                 .that(ex.getClass())
121                 .isEqualTo(UnsupportedOperationException.class);
122     }
123 
124     /*
125       Input based test does not call toString, but this method might be
126       useful for third party integrations.
127     */
128     @Test
129     public void testEmptyToString() {
130         final BeforeExecutionFileFilterSet filterSet = new BeforeExecutionFileFilterSet();
131         assertWithMessage("toString() result shouldn't be an empty string")
132                 .that(filterSet.toString())
133                 .isNotEmpty();
134     }
135 
136 }