Skip to content

Commit 2126aa1

Browse files
Lmh-javaromani
authored andcommitted
Issue #14653: support XML configs without detalization
1 parent 68366f7 commit 2126aa1

5 files changed

Lines changed: 111 additions & 40 deletions

File tree

src/test/java/com/puppycrawl/tools/checkstyle/AbstractModuleTestSupport.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,25 @@ protected final void verifyFilterWithInlineConfigParser(String filePath,
220220
verify(configWithFilters, filePath, expectedFiltered);
221221
}
222222

223+
/**
224+
* Performs verification of the file with given file path using configurations parsed from
225+
* xml header of the file and the array expected messages. Also performs verification of
226+
* the config specified in input file.
227+
*
228+
* @param filePath file path to verify
229+
* @param expected an array of expected messages
230+
* @throws Exception if exception occurs
231+
*/
232+
protected final void verifyWithInlineXmlConfig(String filePath, String... expected)
233+
throws Exception {
234+
final TestInputConfiguration testInputConfiguration =
235+
InlineConfigParser.parseWithXmlHeader(filePath);
236+
final Configuration xmlConfig =
237+
testInputConfiguration.getXmlConfiguration();
238+
verifyViolations(xmlConfig, filePath, testInputConfiguration.getViolations());
239+
verify(xmlConfig, filePath, expected);
240+
}
241+
223242
/**
224243
* Performs verification of the file with the given file path using specified configuration
225244
* and the array expected messages. Also performs verification of the config specified in

src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ public void testSetters() {
443443
public void testSetSeverity() throws Exception {
444444
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
445445

446-
verifyWithInlineConfigParser(getPath("InputCheckerTestSeverity.java"), expected);
446+
verifyWithInlineXmlConfig(getPath("InputCheckerTestSeverity.java"), expected);
447447
}
448448

449449
@Test

src/test/java/com/puppycrawl/tools/checkstyle/bdd/InlineConfigParser.java

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,56 @@ public static TestInputConfiguration parseWithFilteredViolations(String inputFil
224224
return parse(inputFilePath, true);
225225
}
226226

227+
/**
228+
* Parse the input file with configuration in xml header.
229+
*
230+
* @param inputFilePath the input file path.
231+
* @throws Exception if unable to parse the xml header
232+
*/
233+
public static TestInputConfiguration parseWithXmlHeader(String inputFilePath)
234+
throws Exception {
235+
236+
final Path filePath = Paths.get(inputFilePath);
237+
final List<String> lines = readFile(filePath);
238+
if (!checkIsXmlConfig(lines)) {
239+
throw new CheckstyleException("Config cannot be parsed as xml.");
240+
}
241+
242+
final List<String> inlineConfig = getInlineConfig(lines);
243+
final String stringXmlConfig = LATEST_DTD + String.join("", inlineConfig);
244+
final InputSource inputSource = new InputSource(new StringReader(stringXmlConfig));
245+
final Configuration xmlConfig = ConfigurationLoader.loadConfiguration(
246+
inputSource, new PropertiesExpander(System.getProperties()),
247+
ConfigurationLoader.IgnoredModulesOptions.EXECUTE
248+
);
249+
final String configName = xmlConfig.getName();
250+
if (!"Checker".equals(configName)) {
251+
throw new CheckstyleException(
252+
"First module should be Checker, but was " + configName);
253+
}
254+
255+
final TestInputConfiguration.Builder testInputConfigBuilder =
256+
new TestInputConfiguration.Builder();
257+
testInputConfigBuilder.setXmlConfiguration(xmlConfig);
258+
try {
259+
setViolations(testInputConfigBuilder, lines, false);
260+
}
261+
catch (CheckstyleException ex) {
262+
throw new CheckstyleException(ex.getMessage() + " in " + inputFilePath, ex);
263+
}
264+
return testInputConfigBuilder.buildWithXmlConfiguration();
265+
}
266+
267+
/**
268+
* Check whether a file provides xml configuration.
269+
*
270+
* @param lines lines of the file
271+
* @return true if a file provides xml configuration, otherwise false.
272+
*/
273+
private static boolean checkIsXmlConfig(List<String> lines) {
274+
return "/*xml".equals(lines.get(0));
275+
}
276+
227277
private static void setModules(TestInputConfiguration.Builder testInputConfigBuilder,
228278
String inputFilePath, List<String> lines)
229279
throws Exception {
@@ -233,9 +283,8 @@ private static void setModules(TestInputConfiguration.Builder testInputConfigBui
233283
}
234284

235285
final List<String> inlineConfig = getInlineConfig(lines);
236-
final boolean isXmlConfig = "/*xml".equals(lines.get(0));
237286

238-
if (isXmlConfig) {
287+
if (checkIsXmlConfig(lines)) {
239288
final String stringXmlConfig = LATEST_DTD + String.join("", inlineConfig);
240289
final InputSource inputSource = new InputSource(new StringReader(stringXmlConfig));
241290
final Configuration xmlConfig = ConfigurationLoader.loadConfiguration(
@@ -247,7 +296,7 @@ inputSource, new PropertiesExpander(System.getProperties()),
247296
throw new CheckstyleException(
248297
"First module should be Checker, but was " + configName);
249298
}
250-
handleXmlConfig(testInputConfigBuilder, inputFilePath, xmlConfig);
299+
handleXmlConfig(testInputConfigBuilder, inputFilePath, xmlConfig.getChildren());
251300
}
252301
else {
253302
handleKeyValueConfig(testInputConfigBuilder, inputFilePath, inlineConfig);
@@ -273,14 +322,10 @@ private static void handleXmlConfig(TestInputConfiguration.Builder testInputConf
273322
}
274323
else {
275324
final ModuleInputConfiguration.Builder moduleInputConfigBuilder =
276-
new ModuleInputConfiguration.Builder();
325+
new ModuleInputConfiguration.Builder();
277326
setModuleName(moduleInputConfigBuilder, inputFilePath, moduleName);
278327
setProperties(inputFilePath, module, moduleInputConfigBuilder);
279328
testInputConfigBuilder.addChildModule(moduleInputConfigBuilder.build());
280-
281-
if ("Checker".equals(moduleName)) {
282-
handleXmlConfig(testInputConfigBuilder, inputFilePath, module.getChildren());
283-
}
284329
}
285330
}
286331
}
@@ -323,8 +368,6 @@ private static String getFullyQualifiedClassName(String filePath, String moduleN
323368
"com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck");
324369
moduleMappings.put("LineLength",
325370
"com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck");
326-
moduleMappings.put("Checker",
327-
"com.puppycrawl.tools.checkstyle.CheckerCheck");
328371

329372
String fullyQualifiedClassName;
330373
if (moduleMappings.containsKey(moduleName)) {

src/test/java/com/puppycrawl/tools/checkstyle/bdd/TestInputConfiguration.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@
2525
import java.util.Collections;
2626
import java.util.HashSet;
2727
import java.util.List;
28-
import java.util.Map;
29-
import java.util.Optional;
3028
import java.util.Set;
3129

3230
import com.puppycrawl.tools.checkstyle.Checker;
3331
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
3432
import com.puppycrawl.tools.checkstyle.TreeWalker;
33+
import com.puppycrawl.tools.checkstyle.api.Configuration;
3534

3635
public final class TestInputConfiguration {
3736

@@ -68,12 +67,24 @@ public final class TestInputConfiguration {
6867

6968
private final List<TestInputViolation> filteredViolations;
7069

70+
private final Configuration xmlConfiguration;
71+
7172
private TestInputConfiguration(List<ModuleInputConfiguration> childrenModules,
7273
List<TestInputViolation> violations,
7374
List<TestInputViolation> filteredViolations) {
7475
this.childrenModules = childrenModules;
7576
this.violations = violations;
7677
this.filteredViolations = filteredViolations;
78+
xmlConfiguration = null;
79+
}
80+
81+
private TestInputConfiguration(List<TestInputViolation> violations,
82+
List<TestInputViolation> filteredViolations,
83+
Configuration xmlConfiguration) {
84+
childrenModules = null;
85+
this.violations = violations;
86+
this.filteredViolations = filteredViolations;
87+
this.xmlConfiguration = xmlConfiguration;
7788
}
7889

7990
public List<ModuleInputConfiguration> getChildrenModules() {
@@ -92,27 +103,7 @@ public DefaultConfiguration createConfiguration() {
92103
final DefaultConfiguration root = new DefaultConfiguration(ROOT_MODULE_NAME);
93104
root.addProperty("charset", StandardCharsets.UTF_8.name());
94105

95-
final String checkerModuleName = "com.puppycrawl.tools.checkstyle.CheckerCheck";
96-
final Optional<ModuleInputConfiguration> checkerModule = childrenModules.stream()
97-
.filter(module -> module.getModuleName().equals(checkerModuleName))
98-
.findFirst();
99-
100-
if (checkerModule.isPresent()) {
101-
final ModuleInputConfiguration checkerConfig = checkerModule.get();
102-
final Map<String, String> propertiesMap = checkerConfig.getNonDefaultProperties();
103-
104-
final Set<Map.Entry<String, String>> entrySet = propertiesMap.entrySet();
105-
for (Map.Entry<String, String> entry : entrySet) {
106-
final String property = entry.getKey();
107-
final String value = entry.getValue();
108-
root.addProperty(property, value);
109-
}
110-
111-
childrenModules.remove(checkerConfig);
112-
}
113-
114106
final DefaultConfiguration treeWalker = createTreeWalker();
115-
116107
childrenModules
117108
.stream()
118109
.map(ModuleInputConfiguration::createConfiguration)
@@ -128,6 +119,10 @@ else if (!treeWalker.getName().equals(moduleConfig.getName())) {
128119
return root;
129120
}
130121

122+
public Configuration getXmlConfiguration() {
123+
return xmlConfiguration;
124+
}
125+
131126
public DefaultConfiguration createConfigurationWithoutFilters() {
132127
final DefaultConfiguration root = new DefaultConfiguration(ROOT_MODULE_NAME);
133128
root.addProperty("charset", StandardCharsets.UTF_8.name());
@@ -167,6 +162,8 @@ public static final class Builder {
167162

168163
private final List<TestInputViolation> filteredViolations = new ArrayList<>();
169164

165+
private Configuration xmlConfiguration;
166+
170167
public void addChildModule(ModuleInputConfiguration childModule) {
171168
childrenModules.add(childModule);
172169
}
@@ -183,6 +180,18 @@ public void addFilteredViolation(int violationLine, String violationMessage) {
183180
filteredViolations.add(new TestInputViolation(violationLine, violationMessage));
184181
}
185182

183+
public void setXmlConfiguration(Configuration xmlConfiguration) {
184+
this.xmlConfiguration = xmlConfiguration;
185+
}
186+
187+
public TestInputConfiguration buildWithXmlConfiguration() {
188+
return new TestInputConfiguration(
189+
violations,
190+
filteredViolations,
191+
xmlConfiguration
192+
);
193+
}
194+
186195
public TestInputConfiguration build() {
187196
return new TestInputConfiguration(
188197
childrenModules,

src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameExamplesTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void testExample1() throws Exception {
4242
"31:15: " + getCheckMessage(MSG_KEY, "incrementGLOBAL", DEFAULT_EXPECTED_CAPITAL_COUNT),
4343
};
4444

45-
verifyWithInlineConfigParser(getPath("Example1.java"), expected);
45+
verifyWithInlineXmlConfig(getPath("Example1.java"), expected);
4646
}
4747

4848
@Test
@@ -55,7 +55,7 @@ public void testExample2() throws Exception {
5555
"35:15: " + getCheckMessage(MSG_KEY, "incrementGLOBAL", DEFAULT_EXPECTED_CAPITAL_COUNT),
5656
};
5757

58-
verifyWithInlineConfigParser(getPath("Example2.java"), expected);
58+
verifyWithInlineXmlConfig(getPath("Example2.java"), expected);
5959
}
6060

6161
@Test
@@ -67,7 +67,7 @@ public void testExample3() throws Exception {
6767
"23:14: " + getCheckMessage(MSG_KEY, "fourthNUm", expectedCapitalCount),
6868
};
6969

70-
verifyWithInlineConfigParser(getPath("Example3.java"), expected);
70+
verifyWithInlineXmlConfig(getPath("Example3.java"), expected);
7171
}
7272

7373
@Test
@@ -80,7 +80,7 @@ public void testExample4() throws Exception {
8080
"26:10: " + getCheckMessage(MSG_KEY, "firstXML", expectedCapitalCount),
8181
};
8282

83-
verifyWithInlineConfigParser(getPath("Example4.java"), expected);
83+
verifyWithInlineXmlConfig(getPath("Example4.java"), expected);
8484
}
8585

8686
@Test
@@ -93,7 +93,7 @@ public void testExample5() throws Exception {
9393
"24:14: " + getCheckMessage(MSG_KEY, "nextID", expectedCapitalCount),
9494
};
9595

96-
verifyWithInlineConfigParser(getPath("Example5.java"), expected);
96+
verifyWithInlineXmlConfig(getPath("Example5.java"), expected);
9797
}
9898

9999
@Test
@@ -106,13 +106,13 @@ public void testExample6() throws Exception {
106106
"26:20: " + getCheckMessage(MSG_KEY, "MAX_ALLOWED", expectedCapitalCount),
107107
};
108108

109-
verifyWithInlineConfigParser(getPath("Example6.java"), expected);
109+
verifyWithInlineXmlConfig(getPath("Example6.java"), expected);
110110
}
111111

112112
@Test
113113
public void testExample7() throws Exception {
114114
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
115115

116-
verifyWithInlineConfigParser(getPath("Example7.java"), expected);
116+
verifyWithInlineXmlConfig(getPath("Example7.java"), expected);
117117
}
118118
}

0 commit comments

Comments
 (0)