Skip to content

Commit

Permalink
Issue #14653: support XML configs without detalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Lmh-java authored and romani committed Mar 16, 2024
1 parent 68366f7 commit 2126aa1
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,25 @@ protected final void verifyFilterWithInlineConfigParser(String filePath,
verify(configWithFilters, filePath, expectedFiltered);
}

/**
* Performs verification of the file with given file path using configurations parsed from
* xml header of the file and the array expected messages. Also performs verification of
* the config specified in input file.
*
* @param filePath file path to verify
* @param expected an array of expected messages
* @throws Exception if exception occurs
*/
protected final void verifyWithInlineXmlConfig(String filePath, String... expected)
throws Exception {
final TestInputConfiguration testInputConfiguration =
InlineConfigParser.parseWithXmlHeader(filePath);
final Configuration xmlConfig =
testInputConfiguration.getXmlConfiguration();
verifyViolations(xmlConfig, filePath, testInputConfiguration.getViolations());
verify(xmlConfig, filePath, expected);
}

/**
* Performs verification of the file with the given file path using specified configuration
* and the array expected messages. Also performs verification of the config specified in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public void testSetters() {
public void testSetSeverity() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

verifyWithInlineConfigParser(getPath("InputCheckerTestSeverity.java"), expected);
verifyWithInlineXmlConfig(getPath("InputCheckerTestSeverity.java"), expected);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,56 @@ public static TestInputConfiguration parseWithFilteredViolations(String inputFil
return parse(inputFilePath, true);
}

/**
* Parse the input file with configuration in xml header.
*
* @param inputFilePath the input file path.
* @throws Exception if unable to parse the xml header
*/
public static TestInputConfiguration parseWithXmlHeader(String inputFilePath)
throws Exception {

final Path filePath = Paths.get(inputFilePath);
final List<String> lines = readFile(filePath);
if (!checkIsXmlConfig(lines)) {
throw new CheckstyleException("Config cannot be parsed as xml.");
}

final List<String> inlineConfig = getInlineConfig(lines);
final String stringXmlConfig = LATEST_DTD + String.join("", inlineConfig);
final InputSource inputSource = new InputSource(new StringReader(stringXmlConfig));
final Configuration xmlConfig = ConfigurationLoader.loadConfiguration(
inputSource, new PropertiesExpander(System.getProperties()),
ConfigurationLoader.IgnoredModulesOptions.EXECUTE
);
final String configName = xmlConfig.getName();
if (!"Checker".equals(configName)) {
throw new CheckstyleException(
"First module should be Checker, but was " + configName);
}

final TestInputConfiguration.Builder testInputConfigBuilder =
new TestInputConfiguration.Builder();
testInputConfigBuilder.setXmlConfiguration(xmlConfig);
try {
setViolations(testInputConfigBuilder, lines, false);
}
catch (CheckstyleException ex) {
throw new CheckstyleException(ex.getMessage() + " in " + inputFilePath, ex);
}
return testInputConfigBuilder.buildWithXmlConfiguration();
}

/**
* Check whether a file provides xml configuration.
*
* @param lines lines of the file
* @return true if a file provides xml configuration, otherwise false.
*/
private static boolean checkIsXmlConfig(List<String> lines) {
return "/*xml".equals(lines.get(0));
}

private static void setModules(TestInputConfiguration.Builder testInputConfigBuilder,
String inputFilePath, List<String> lines)
throws Exception {
Expand All @@ -233,9 +283,8 @@ private static void setModules(TestInputConfiguration.Builder testInputConfigBui
}

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

if (isXmlConfig) {
if (checkIsXmlConfig(lines)) {
final String stringXmlConfig = LATEST_DTD + String.join("", inlineConfig);
final InputSource inputSource = new InputSource(new StringReader(stringXmlConfig));
final Configuration xmlConfig = ConfigurationLoader.loadConfiguration(
Expand All @@ -247,7 +296,7 @@ inputSource, new PropertiesExpander(System.getProperties()),
throw new CheckstyleException(
"First module should be Checker, but was " + configName);
}
handleXmlConfig(testInputConfigBuilder, inputFilePath, xmlConfig);
handleXmlConfig(testInputConfigBuilder, inputFilePath, xmlConfig.getChildren());
}
else {
handleKeyValueConfig(testInputConfigBuilder, inputFilePath, inlineConfig);
Expand All @@ -273,14 +322,10 @@ private static void handleXmlConfig(TestInputConfiguration.Builder testInputConf
}
else {
final ModuleInputConfiguration.Builder moduleInputConfigBuilder =
new ModuleInputConfiguration.Builder();
new ModuleInputConfiguration.Builder();
setModuleName(moduleInputConfigBuilder, inputFilePath, moduleName);
setProperties(inputFilePath, module, moduleInputConfigBuilder);
testInputConfigBuilder.addChildModule(moduleInputConfigBuilder.build());

if ("Checker".equals(moduleName)) {
handleXmlConfig(testInputConfigBuilder, inputFilePath, module.getChildren());
}
}
}
}
Expand Down Expand Up @@ -323,8 +368,6 @@ private static String getFullyQualifiedClassName(String filePath, String moduleN
"com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck");
moduleMappings.put("LineLength",
"com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck");
moduleMappings.put("Checker",
"com.puppycrawl.tools.checkstyle.CheckerCheck");

String fullyQualifiedClassName;
if (moduleMappings.containsKey(moduleName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.TreeWalker;
import com.puppycrawl.tools.checkstyle.api.Configuration;

public final class TestInputConfiguration {

Expand Down Expand Up @@ -68,12 +67,24 @@ public final class TestInputConfiguration {

private final List<TestInputViolation> filteredViolations;

private final Configuration xmlConfiguration;

private TestInputConfiguration(List<ModuleInputConfiguration> childrenModules,
List<TestInputViolation> violations,
List<TestInputViolation> filteredViolations) {
this.childrenModules = childrenModules;
this.violations = violations;
this.filteredViolations = filteredViolations;
xmlConfiguration = null;
}

private TestInputConfiguration(List<TestInputViolation> violations,
List<TestInputViolation> filteredViolations,
Configuration xmlConfiguration) {
childrenModules = null;
this.violations = violations;
this.filteredViolations = filteredViolations;
this.xmlConfiguration = xmlConfiguration;
}

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

final String checkerModuleName = "com.puppycrawl.tools.checkstyle.CheckerCheck";
final Optional<ModuleInputConfiguration> checkerModule = childrenModules.stream()
.filter(module -> module.getModuleName().equals(checkerModuleName))
.findFirst();

if (checkerModule.isPresent()) {
final ModuleInputConfiguration checkerConfig = checkerModule.get();
final Map<String, String> propertiesMap = checkerConfig.getNonDefaultProperties();

final Set<Map.Entry<String, String>> entrySet = propertiesMap.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
final String property = entry.getKey();
final String value = entry.getValue();
root.addProperty(property, value);
}

childrenModules.remove(checkerConfig);
}

final DefaultConfiguration treeWalker = createTreeWalker();

childrenModules
.stream()
.map(ModuleInputConfiguration::createConfiguration)
Expand All @@ -128,6 +119,10 @@ else if (!treeWalker.getName().equals(moduleConfig.getName())) {
return root;
}

public Configuration getXmlConfiguration() {
return xmlConfiguration;
}

public DefaultConfiguration createConfigurationWithoutFilters() {
final DefaultConfiguration root = new DefaultConfiguration(ROOT_MODULE_NAME);
root.addProperty("charset", StandardCharsets.UTF_8.name());
Expand Down Expand Up @@ -167,6 +162,8 @@ public static final class Builder {

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

private Configuration xmlConfiguration;

public void addChildModule(ModuleInputConfiguration childModule) {
childrenModules.add(childModule);
}
Expand All @@ -183,6 +180,18 @@ public void addFilteredViolation(int violationLine, String violationMessage) {
filteredViolations.add(new TestInputViolation(violationLine, violationMessage));
}

public void setXmlConfiguration(Configuration xmlConfiguration) {
this.xmlConfiguration = xmlConfiguration;
}

public TestInputConfiguration buildWithXmlConfiguration() {
return new TestInputConfiguration(
violations,
filteredViolations,
xmlConfiguration
);
}

public TestInputConfiguration build() {
return new TestInputConfiguration(
childrenModules,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void testExample1() throws Exception {
"31:15: " + getCheckMessage(MSG_KEY, "incrementGLOBAL", DEFAULT_EXPECTED_CAPITAL_COUNT),
};

verifyWithInlineConfigParser(getPath("Example1.java"), expected);
verifyWithInlineXmlConfig(getPath("Example1.java"), expected);
}

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

verifyWithInlineConfigParser(getPath("Example2.java"), expected);
verifyWithInlineXmlConfig(getPath("Example2.java"), expected);
}

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

verifyWithInlineConfigParser(getPath("Example3.java"), expected);
verifyWithInlineXmlConfig(getPath("Example3.java"), expected);
}

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

verifyWithInlineConfigParser(getPath("Example4.java"), expected);
verifyWithInlineXmlConfig(getPath("Example4.java"), expected);
}

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

verifyWithInlineConfigParser(getPath("Example5.java"), expected);
verifyWithInlineXmlConfig(getPath("Example5.java"), expected);
}

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

verifyWithInlineConfigParser(getPath("Example6.java"), expected);
verifyWithInlineXmlConfig(getPath("Example6.java"), expected);
}

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

verifyWithInlineConfigParser(getPath("Example7.java"), expected);
verifyWithInlineXmlConfig(getPath("Example7.java"), expected);
}
}

0 comments on commit 2126aa1

Please sign in to comment.