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.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck.MSG_JAVADOC_MISSING;
23 import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
24
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.stream.Collectors;
28
29 import org.junit.Test;
30
31 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
33 import com.puppycrawl.tools.checkstyle.TreeWalker;
34 import com.puppycrawl.tools.checkstyle.api.Configuration;
35 import com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder;
36 import com.puppycrawl.tools.checkstyle.checks.UncommentedMainCheck;
37 import com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck;
38 import com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck;
39 import com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck;
40 import com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck;
41 import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck;
42 import com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck;
43 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
44
45 public class SuppressWarningsFilterTest
46 extends AbstractModuleTestSupport {
47
48 private static final String[] ALL_MESSAGES = {
49 "16: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
50 "17: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
51 "19: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
52 "22:45: "
53 + getCheckMessage(AbstractNameCheck.class,
54 MSG_INVALID_PATTERN, "I", "^[a-z][a-zA-Z0-9]*$"),
55 "24:17: "
56 + getCheckMessage(AbstractNameCheck.class,
57 MSG_INVALID_PATTERN, "J", "^[a-z][a-zA-Z0-9]*$"),
58 "25:17: "
59 + getCheckMessage(AbstractNameCheck.class,
60 MSG_INVALID_PATTERN, "K", "^[a-z][a-zA-Z0-9]*$"),
61 "29:17: "
62 + getCheckMessage(AbstractNameCheck.class,
63 MSG_INVALID_PATTERN, "L", "^[a-z][a-zA-Z0-9]*$"),
64 "29:32: "
65 + getCheckMessage(AbstractNameCheck.class,
66 MSG_INVALID_PATTERN, "X", "^[a-z][a-zA-Z0-9]*$"),
67 "33:30: "
68 + getCheckMessage(AbstractNameCheck.class,
69 MSG_INVALID_PATTERN, "m", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"),
70 "34:30: "
71 + getCheckMessage(AbstractNameCheck.class,
72 MSG_INVALID_PATTERN, "n", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"),
73 "39:17: "
74 + getCheckMessage(ParameterNumberCheck.class, ParameterNumberCheck.MSG_KEY, 7, 8),
75 "45:9: "
76 + getCheckMessage(IllegalCatchCheck.class, IllegalCatchCheck.MSG_KEY, "Exception"),
77 "56:9: "
78 + getCheckMessage(IllegalCatchCheck.class, IllegalCatchCheck.MSG_KEY, "Exception"),
79 "61: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
80 "71: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
81 "76: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
82 "77: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
83 "83: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
84 "84: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
85 "90: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
86 "91: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
87 "97: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
88 };
89
90 @Override
91 protected String getPackageLocation() {
92 return "com/puppycrawl/tools/checkstyle/filters/suppresswarningsfilter";
93 }
94
95 @Test
96 public void testNone() throws Exception {
97 final DefaultConfiguration filterConfig = null;
98 final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
99 verifySuppressed(filterConfig, suppressed);
100 }
101
102 @Test
103 public void testDefault() throws Exception {
104 final DefaultConfiguration filterConfig =
105 createModuleConfig(SuppressWarningsFilter.class);
106 final String[] suppressed = {
107 "24:17: "
108 + getCheckMessage(AbstractNameCheck.class,
109 MSG_INVALID_PATTERN, "J", "^[a-z][a-zA-Z0-9]*$"),
110 "29:17: "
111 + getCheckMessage(AbstractNameCheck.class,
112 MSG_INVALID_PATTERN, "L", "^[a-z][a-zA-Z0-9]*$"),
113 "33:30: "
114 + getCheckMessage(AbstractNameCheck.class,
115 MSG_INVALID_PATTERN, "m", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"),
116 "39:17: "
117 + getCheckMessage(ParameterNumberCheck.class, ParameterNumberCheck.MSG_KEY, 7, 8),
118 "56:9: "
119 + getCheckMessage(IllegalCatchCheck.class, IllegalCatchCheck.MSG_KEY, "Exception"),
120 "71: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
121 "77: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
122 "84: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
123 "91: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
124 };
125 verifySuppressed(filterConfig, suppressed);
126 }
127
128 private void verifySuppressed(Configuration moduleConfig,
129 String... aSuppressed)
130 throws Exception {
131 verifySuppressed(moduleConfig, getPath("InputSuppressWarningsFilter.java"),
132 ALL_MESSAGES, aSuppressed);
133 }
134
135 private void verifySuppressed(Configuration moduleConfig, String fileName,
136 String[] expectedViolations, String... suppressedViolations) throws Exception {
137 final DefaultConfiguration holderConfig =
138 createModuleConfig(SuppressWarningsHolder.class);
139 holderConfig.addAttribute("aliasList",
140 "com.puppycrawl.tools.checkstyle.checks.sizes."
141 + "ParameterNumberCheck=paramnum");
142
143 final DefaultConfiguration memberNameCheckConfig =
144 createModuleConfig(MemberNameCheck.class);
145 memberNameCheckConfig.addAttribute("id", "ignore");
146
147 final DefaultConfiguration constantNameCheckConfig =
148 createModuleConfig(ConstantNameCheck.class);
149 constantNameCheckConfig.addAttribute("id", "");
150
151 final DefaultConfiguration uncommentedMainCheckConfig =
152 createModuleConfig(UncommentedMainCheck.class);
153 uncommentedMainCheckConfig.addAttribute("id", "ignore");
154
155 final DefaultConfiguration treewalkerConfig =
156 createModuleConfig(TreeWalker.class);
157 treewalkerConfig.addChild(holderConfig);
158 treewalkerConfig.addChild(memberNameCheckConfig);
159 treewalkerConfig.addChild(constantNameCheckConfig);
160 treewalkerConfig.addChild(createModuleConfig(ParameterNumberCheck.class));
161 treewalkerConfig.addChild(createModuleConfig(IllegalCatchCheck.class));
162 treewalkerConfig.addChild(uncommentedMainCheckConfig);
163
164 final DefaultConfiguration missingJavadocConfig =
165 createModuleConfig(MissingJavadocTypeCheck.class);
166 missingJavadocConfig.addAttribute("scope", "private");
167 treewalkerConfig.addChild(missingJavadocConfig);
168
169 final DefaultConfiguration checkerConfig =
170 createRootConfig(treewalkerConfig);
171 if (moduleConfig != null) {
172 checkerConfig.addChild(moduleConfig);
173 }
174
175 verify(checkerConfig,
176 fileName,
177 removeSuppressed(expectedViolations, suppressedViolations));
178 }
179
180 private static String[] removeSuppressed(String[] from, String... remove) {
181 final Collection<String> coll = Arrays.stream(from).collect(Collectors.toList());
182 coll.removeAll(Arrays.asList(remove));
183 return coll.toArray(CommonUtil.EMPTY_STRING_ARRAY);
184 }
185
186 @Test
187 public void testSuppressById() throws Exception {
188 final DefaultConfiguration filterConfig =
189 createModuleConfig(SuppressWarningsFilter.class);
190 final String[] suppressedViolationMessages = {
191 "6:17: "
192 + getCheckMessage(AbstractNameCheck.class,
193 MSG_INVALID_PATTERN, "A1", "^[a-z][a-zA-Z0-9]*$"),
194 "8: "
195 + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
196 };
197 final String[] expectedViolationMessages = {
198 "3: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
199 "6:17: "
200 + getCheckMessage(AbstractNameCheck.class,
201 MSG_INVALID_PATTERN, "A1", "^[a-z][a-zA-Z0-9]*$"),
202 "8: "
203 + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
204 };
205
206 verifySuppressed(filterConfig, getPath("InputSuppressWarningsFilterById.java"),
207 expectedViolationMessages, suppressedViolationMessages);
208 }
209
210 }