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