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.bdd;
21
22 import java.nio.charset.StandardCharsets;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29
30 import com.puppycrawl.tools.checkstyle.Checker;
31 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32 import com.puppycrawl.tools.checkstyle.TreeWalker;
33 import com.puppycrawl.tools.checkstyle.api.Configuration;
34
35 public final class TestInputConfiguration {
36
37 private static final String ROOT_MODULE_NAME = Checker.class.getSimpleName();
38
39 private static final Set<String> CHECKER_CHILDREN = new HashSet<>(Arrays.asList(
40 "com.puppycrawl.tools.checkstyle.filefilters.BeforeExecutionExclusionFileFilter",
41 "com.puppycrawl.tools.checkstyle.filters.SeverityMatchFilter",
42 "com.puppycrawl.tools.checkstyle.filters.SuppressionFilter",
43 "com.puppycrawl.tools.checkstyle.filters.SuppressionSingleFilter",
44 "com.puppycrawl.tools.checkstyle.filters.SuppressWarningsFilter",
45 "com.puppycrawl.tools.checkstyle.filters.SuppressWithNearbyTextFilter",
46 "com.puppycrawl.tools.checkstyle.filters.SuppressWithPlainTextCommentFilter",
47 "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck",
48 "com.puppycrawl.tools.checkstyle.checks.header.RegexpHeaderCheck",
49 "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck",
50 "com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck",
51 "com.puppycrawl.tools.checkstyle.checks.UniquePropertiesCheck",
52 "com.puppycrawl.tools.checkstyle.checks.OrderedPropertiesCheck",
53 "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpMultilineCheck",
54 "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck",
55 "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck",
56 "com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck",
57 "com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck",
58 "com.puppycrawl.tools.checkstyle.checks.whitespace.FileTabCharacterCheck",
59 "com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheckTest$ViolationFileSetCheck",
60 "com.puppycrawl.tools.checkstyle.api.FileSetCheckTest$TestFileSetCheck",
61 "com.puppycrawl.tools.checkstyle.internal.testmodules"
62 + ".VerifyPositionAfterLastTabFileSet",
63 "com.puppycrawl.tools.checkstyle.CheckerTest$VerifyPositionAfterTabFileSet"
64 ));
65
66 private final List<ModuleInputConfiguration> childrenModules;
67
68 private final List<TestInputViolation> violations;
69
70 private final List<TestInputViolation> filteredViolations;
71
72 private final Configuration xmlConfiguration;
73
74 private TestInputConfiguration(List<ModuleInputConfiguration> childrenModules,
75 List<TestInputViolation> violations,
76 List<TestInputViolation> filteredViolations) {
77 this.childrenModules = childrenModules;
78 this.violations = violations;
79 this.filteredViolations = filteredViolations;
80 xmlConfiguration = null;
81 }
82
83 private TestInputConfiguration(List<TestInputViolation> violations,
84 List<TestInputViolation> filteredViolations,
85 Configuration xmlConfiguration) {
86 childrenModules = null;
87 this.violations = violations;
88 this.filteredViolations = filteredViolations;
89 this.xmlConfiguration = xmlConfiguration;
90 }
91
92 public List<ModuleInputConfiguration> getChildrenModules() {
93 return Collections.unmodifiableList(childrenModules);
94 }
95
96 public List<TestInputViolation> getViolations() {
97 return Collections.unmodifiableList(violations);
98 }
99
100 public List<TestInputViolation> getFilteredViolations() {
101 return Collections.unmodifiableList(filteredViolations);
102 }
103
104 public DefaultConfiguration createConfiguration() {
105 final DefaultConfiguration root = new DefaultConfiguration(ROOT_MODULE_NAME);
106 root.addProperty("charset", StandardCharsets.UTF_8.name());
107
108 final DefaultConfiguration treeWalker = createTreeWalker();
109 childrenModules
110 .stream()
111 .map(ModuleInputConfiguration::createConfiguration)
112 .forEach(moduleConfig -> {
113 if (CHECKER_CHILDREN.contains(moduleConfig.getName())) {
114 root.addChild(moduleConfig);
115 }
116 else if (!treeWalker.getName().equals(moduleConfig.getName())) {
117 treeWalker.addChild(moduleConfig);
118 }
119 });
120 root.addChild(treeWalker);
121 return root;
122 }
123
124 public Configuration getXmlConfiguration() {
125 return xmlConfiguration;
126 }
127
128 public DefaultConfiguration createConfigurationWithoutFilters() {
129 final DefaultConfiguration root = new DefaultConfiguration(ROOT_MODULE_NAME);
130 root.addProperty("charset", StandardCharsets.UTF_8.name());
131 final DefaultConfiguration treeWalker = createTreeWalker();
132 childrenModules
133 .stream()
134 .map(ModuleInputConfiguration::createConfiguration)
135 .filter(moduleConfig -> !moduleConfig.getName().endsWith("Filter"))
136 .forEach(moduleConfig -> {
137 if (CHECKER_CHILDREN.contains(moduleConfig.getName())) {
138 root.addChild(moduleConfig);
139 }
140 else if (!treeWalker.getName().equals(moduleConfig.getName())) {
141 treeWalker.addChild(moduleConfig);
142 }
143 });
144 root.addChild(treeWalker);
145 return root;
146 }
147
148 private DefaultConfiguration createTreeWalker() {
149 final DefaultConfiguration treeWalker;
150 if (childrenModules.get(0).getModuleName().equals(TreeWalker.class.getName())) {
151 treeWalker = childrenModules.get(0).createConfiguration();
152 }
153 else {
154 treeWalker = new DefaultConfiguration(TreeWalker.class.getName());
155 }
156 return treeWalker;
157 }
158
159 public static final class Builder {
160
161 private final List<ModuleInputConfiguration> childrenModules = new ArrayList<>();
162
163 private final List<TestInputViolation> violations = new ArrayList<>();
164
165 private final List<TestInputViolation> filteredViolations = new ArrayList<>();
166
167 private Configuration xmlConfiguration;
168
169 public void addChildModule(ModuleInputConfiguration childModule) {
170 childrenModules.add(childModule);
171 }
172
173 public void addViolation(int violationLine, String violationMessage) {
174 violations.add(new TestInputViolation(violationLine, violationMessage));
175 }
176
177 public void addViolations(List<TestInputViolation> inputViolations) {
178 violations.addAll(inputViolations);
179 }
180
181 public void addFilteredViolation(int violationLine, String violationMessage) {
182 filteredViolations.add(new TestInputViolation(violationLine, violationMessage));
183 }
184
185 public void setXmlConfiguration(Configuration xmlConfiguration) {
186 this.xmlConfiguration = xmlConfiguration;
187 }
188
189 public TestInputConfiguration buildWithXmlConfiguration() {
190 return new TestInputConfiguration(
191 violations,
192 filteredViolations,
193 xmlConfiguration
194 );
195 }
196
197 public TestInputConfiguration build() {
198 return new TestInputConfiguration(
199 childrenModules,
200 violations,
201 filteredViolations
202 );
203 }
204
205 public List<ModuleInputConfiguration> getChildrenModules() {
206 return Collections.unmodifiableList(childrenModules);
207 }
208 }
209 }