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