View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.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.header.MultiFileRegexpHeaderCheck",
50              "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck",
51              "com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck",
52              "com.puppycrawl.tools.checkstyle.checks.LineEndingCheck",
53              "com.puppycrawl.tools.checkstyle.checks.UniquePropertiesCheck",
54              "com.puppycrawl.tools.checkstyle.checks.OrderedPropertiesCheck",
55              "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpMultilineCheck",
56              "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck",
57              "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck",
58              "com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck",
59              "com.puppycrawl.tools.checkstyle.checks.TranslationCheck",
60              "com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck",
61              "com.puppycrawl.tools.checkstyle.checks.whitespace.FileTabCharacterCheck",
62              "com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheckTest$ViolationFileSetCheck",
63              "com.puppycrawl.tools.checkstyle.api.FileSetCheckTest$TestFileSetCheck",
64              "com.puppycrawl.tools.checkstyle.internal.testmodules"
65                  + ".VerifyPositionAfterLastTabFileSet",
66              "com.puppycrawl.tools.checkstyle.CheckerTest$VerifyPositionAfterTabFileSet"
67      ));
68  
69      private final List<ModuleInputConfiguration> childrenModules;
70  
71      private final List<TestInputViolation> violations;
72  
73      private final List<TestInputViolation> filteredViolations;
74  
75      private final Configuration xmlConfiguration;
76  
77      private TestInputConfiguration(List<ModuleInputConfiguration> childrenModules,
78                                     List<TestInputViolation> violations,
79                                     List<TestInputViolation> filteredViolations) {
80          this.childrenModules = childrenModules;
81          this.violations = violations;
82          this.filteredViolations = filteredViolations;
83          xmlConfiguration = null;
84      }
85  
86      private TestInputConfiguration(List<TestInputViolation> violations,
87                                     List<TestInputViolation> filteredViolations,
88                                     Configuration xmlConfiguration) {
89          childrenModules = null;
90          this.violations = violations;
91          this.filteredViolations = filteredViolations;
92          this.xmlConfiguration = xmlConfiguration;
93      }
94  
95      public List<ModuleInputConfiguration> getChildrenModules() {
96          return Collections.unmodifiableList(childrenModules);
97      }
98  
99      public List<TestInputViolation> getViolations() {
100         return Collections.unmodifiableList(violations);
101     }
102 
103     public List<TestInputViolation> getFilteredViolations() {
104         return Collections.unmodifiableList(filteredViolations);
105     }
106 
107     public DefaultConfiguration createConfiguration() {
108         final DefaultConfiguration root = new DefaultConfiguration(ROOT_MODULE_NAME);
109         root.addProperty("charset", StandardCharsets.UTF_8.name());
110 
111         final DefaultConfiguration treeWalker = createTreeWalker();
112         childrenModules
113                 .stream()
114                 .map(ModuleInputConfiguration::createConfiguration)
115                 .forEach(moduleConfig -> {
116                     if (CHECKER_CHILDREN.contains(moduleConfig.getName())) {
117                         root.addChild(moduleConfig);
118                     }
119                     else if (!treeWalker.getName().equals(moduleConfig.getName())) {
120                         treeWalker.addChild(moduleConfig);
121                     }
122                 });
123         root.addChild(treeWalker);
124         return root;
125     }
126 
127     public Configuration getXmlConfiguration() {
128         return xmlConfiguration;
129     }
130 
131     public DefaultConfiguration createConfigurationWithoutFilters() {
132         final DefaultConfiguration root = new DefaultConfiguration(ROOT_MODULE_NAME);
133         root.addProperty("charset", StandardCharsets.UTF_8.name());
134         final DefaultConfiguration treeWalker = createTreeWalker();
135         childrenModules
136                 .stream()
137                 .map(ModuleInputConfiguration::createConfiguration)
138                 .filter(moduleConfig -> !moduleConfig.getName().endsWith("Filter"))
139                 .forEach(moduleConfig -> {
140                     if (CHECKER_CHILDREN.contains(moduleConfig.getName())) {
141                         root.addChild(moduleConfig);
142                     }
143                     else if (!treeWalker.getName().equals(moduleConfig.getName())) {
144                         treeWalker.addChild(moduleConfig);
145                     }
146                 });
147         root.addChild(treeWalker);
148         return root;
149     }
150 
151     private DefaultConfiguration createTreeWalker() {
152         final DefaultConfiguration treeWalker;
153         if (childrenModules.getFirst().getModuleName().equals(TreeWalker.class.getName())) {
154             treeWalker = childrenModules.getFirst().createConfiguration();
155         }
156         else {
157             treeWalker = new DefaultConfiguration(TreeWalker.class.getName());
158         }
159         return treeWalker;
160     }
161 
162     public static final class Builder {
163 
164         private final List<ModuleInputConfiguration> childrenModules = new ArrayList<>();
165 
166         private final List<TestInputViolation> violations = new ArrayList<>();
167 
168         private final List<TestInputViolation> filteredViolations = new ArrayList<>();
169 
170         private Configuration xmlConfiguration;
171 
172         public void addChildModule(ModuleInputConfiguration childModule) {
173             childrenModules.add(childModule);
174         }
175 
176         public void addViolation(int violationLine, String violationMessage) {
177             violations.add(new TestInputViolation(violationLine, violationMessage));
178         }
179 
180         public void addViolations(List<TestInputViolation> inputViolations) {
181             violations.addAll(inputViolations);
182         }
183 
184         public void addFilteredViolation(int violationLine, String violationMessage) {
185             filteredViolations.add(new TestInputViolation(violationLine, violationMessage));
186         }
187 
188         public void setXmlConfiguration(Configuration xmlConfiguration) {
189             this.xmlConfiguration = xmlConfiguration;
190         }
191 
192         public TestInputConfiguration buildWithXmlConfiguration() {
193             return new TestInputConfiguration(
194                     violations,
195                     filteredViolations,
196                     xmlConfiguration
197             );
198         }
199 
200         public TestInputConfiguration build() {
201             return new TestInputConfiguration(
202                     childrenModules,
203                     violations,
204                     filteredViolations
205             );
206         }
207 
208         public List<ModuleInputConfiguration> getChildrenModules() {
209             return Collections.unmodifiableList(childrenModules);
210         }
211     }
212 }