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.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
107
108
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
126
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 }