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