Skip to content

Commit

Permalink
Issue checkstyle#10141: Parse and extract check properties from input…
Browse files Browse the repository at this point in the history
… file
  • Loading branch information
shashwatj07 authored and timurt committed Jun 24, 2021
1 parent 7fd6f19 commit b605421
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -228,7 +229,7 @@ protected final void verifyWithInlineConfigParser(Configuration aConfig,
throws Exception {
final InputConfiguration inputConfiguration = InlineConfigParser.parse(filePath);
final Configuration parsedConfig = inputConfiguration.createConfiguration();
verifyConfig(aConfig, parsedConfig);
verifyConfig(aConfig, parsedConfig, inputConfiguration);
verify(parsedConfig, filePath, expected);
}

Expand Down Expand Up @@ -350,23 +351,24 @@ protected final void verify(Checker checker,
* @param testConfig hardcoded test config.
* @param parsedConfig parsed config from input file.
*/
private static void verifyConfig(Configuration testConfig, Configuration parsedConfig) {
private static void verifyConfig(Configuration testConfig, Configuration parsedConfig,
InputConfiguration inputConfiguration)
throws CheckstyleException {
assertWithMessage("Check name differs from expected.")
.that(testConfig.getName())
.isEqualTo(parsedConfig.getName());
assertWithMessage("Property keys differ from expected.")
.that(parsedConfig.getAttributeNames())
.isEqualTo(testConfig.getAttributeNames());
for (String attribute : testConfig.getAttributeNames()) {
try {
assertWithMessage("Property value for key %s differs from expected.", attribute)
.that(parsedConfig.getAttribute(attribute))
.isEqualTo(testConfig.getAttribute(attribute));
}
catch (CheckstyleException ex) {
assertWithMessage("Property value not specified for key %s.", attribute)
.fail();
}
for (String property : parsedConfig.getAttributeNames()) {
assertWithMessage("Property value for key %s differs from expected.", property)
.that(testConfig.getAttribute(property))
.isEqualTo(parsedConfig.getAttribute(property));
}
final List<String> testProperties =
new LinkedList<>(Arrays.asList(testConfig.getAttributeNames()));
testProperties.removeAll(Arrays.asList(parsedConfig.getAttributeNames()));
for (String property : testProperties) {
assertWithMessage("Property value for key %s differs from expected.", property)
.that(inputConfiguration.getDefaultPropertyValue(property))
.isEqualTo(testConfig.getAttribute(property));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand All @@ -45,13 +49,14 @@ private InlineConfigParser() {
* Parses the input file provided.
*
* @param inputFilePath the input file path.
* @throws CheckstyleException if unable to read file or file not formatted properly.
* @throws Exception if unable to read file or file not formatted properly.
*/
public static InputConfiguration parse(String inputFilePath) throws CheckstyleException {
public static InputConfiguration parse(String inputFilePath) throws Exception {
final Path filePath = Paths.get(inputFilePath);
final List<String> lines = readFile(filePath);
final InputConfiguration.Builder inputConfigBuilder = new InputConfiguration.Builder();
setCheckName(inputConfigBuilder, inputFilePath, lines);
setCheckProperties(inputConfigBuilder, lines);
return inputConfigBuilder.build();
}

Expand Down Expand Up @@ -86,4 +91,29 @@ private static void setCheckName(InputConfiguration.Builder inputConfigBuilder,
final String checkPath = getPackageFromFilePath(filePath) + "." + checkName + "Check";
inputConfigBuilder.setCheckName(checkPath);
}

private static void setCheckProperties(InputConfiguration.Builder inputConfigBuilder,
List<String> lines)
throws Exception {
final StringBuilder stringBuilder = new StringBuilder(128);
int lineNo = 2;
for (String line = lines.get(lineNo); !line.contains("*/"); line = lines.get(++lineNo)) {
stringBuilder.append(line).append('\n');
}
final Properties properties = new Properties();
try (Reader reader = new StringReader(stringBuilder.toString())) {
properties.load(reader);
}
for (final Map.Entry<Object, Object> entry : properties.entrySet()) {
final String key = entry.getKey().toString();
final String value = entry.getValue().toString();
if (value.startsWith("(default)")) {
inputConfigBuilder.addDefaultProperty(key,
value.substring(value.indexOf(')') + 1));
}
else {
inputConfigBuilder.addNonDefaultProperty(key, value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ public List<Integer> getViolations() {
}

public DefaultConfiguration createConfiguration() {
return new DefaultConfiguration(checkName);
final DefaultConfiguration parsedConfig = new DefaultConfiguration(checkName);
nonDefaultProperties.forEach(parsedConfig::addAttribute);
return parsedConfig;
}

public String getDefaultPropertyValue(String key) {
return defaultProperties.get(key);
}

public static final class Builder {
Expand All @@ -92,12 +98,12 @@ public void setCheckName(String checkName) {
this.checkName = checkName;
}

public void addDefaultProperty(String property, String value) {
defaultProperties.put(property, value);
public void addDefaultProperty(String key, String value) {
defaultProperties.put(key, value);
}

public void addNonDefaultProperty(String property, String value) {
nonDefaultProperties.put(property, value);
public void addNonDefaultProperty(String key, String value) {
nonDefaultProperties.put(key, value);
}

public void addViolation(int violationLine) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public void testBadOverrideFromObjectJ5Compatible() throws Exception {
"53:5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
};

verify(checkConfig, getPath("InputMissingOverrideBadOverrideFromObjectJava5.java"),
expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputMissingOverrideBadOverrideFromObjectJava5.java"), expected);
}

/**
Expand All @@ -94,7 +94,8 @@ public void testBadOverrideFromOther() throws Exception {
"68:5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
};

verify(checkConfig, getPath("InputMissingOverrideBadOverrideFromOther.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputMissingOverrideBadOverrideFromOther.java"), expected);
}

/**
Expand All @@ -108,8 +109,8 @@ public void testBadOverrideFromOtherJ5Compatible() throws Exception {

final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputMissingOverrideBadOverrideFromOtherJava5.java"),
expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputMissingOverrideBadOverrideFromOtherJava5.java"), expected);
}

/**
Expand All @@ -126,7 +127,8 @@ public void testBadAnnotationOverride() throws Exception {
"42:21: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
};

verify(checkConfig, getPath("InputMissingOverrideBadAnnotation.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputMissingOverrideBadAnnotation.java"), expected);
}

/**
Expand All @@ -139,7 +141,8 @@ public void testBadAnnotationOverrideJ5Compatible() throws Exception {
checkConfig.addAttribute("javaFiveCompatibility", "true");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputMissingOverrideBadAnnotationJava5.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputMissingOverrideBadAnnotationJava5.java"), expected);
}

/**
Expand All @@ -153,7 +156,8 @@ public void testNotOverride() throws Exception {
"20:5: " + getCheckMessage(MSG_KEY_TAG_NOT_VALID_ON, "{@inheritDoc}"),
};

verify(checkConfig, getPath("InputMissingOverrideNotOverride.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputMissingOverrideNotOverride.java"), expected);
}

/**
Expand All @@ -167,7 +171,8 @@ public void testGoodOverrideFromObject() throws Exception {

final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputMissingOverrideGoodOverrideFromObject.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputMissingOverrideGoodOverrideFromObject.java"), expected);
}

/**
Expand All @@ -181,8 +186,8 @@ public void testGoodOverrideFromObjectJ5Compatible() throws Exception {

final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputMissingOverrideGoodOverrideFromObjectJava5.java"),
expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputMissingOverrideGoodOverrideFromObjectJava5.java"), expected);
}

/**
Expand All @@ -194,7 +199,8 @@ public void testGoodOverrideFromOther() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputMissingOverrideGoodOverrideFromOther.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputMissingOverrideGoodOverrideFromOther.java"), expected);
}

/**
Expand All @@ -208,8 +214,8 @@ public void testGoodOverrideFromOtherJ5Compatible() throws Exception {

final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputMissingOverrideGoodOverrideFromOtherJava5.java"),
expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputMissingOverrideGoodOverrideFromOtherJava5.java"), expected);
}

/**
Expand All @@ -221,7 +227,8 @@ public void testGoodAnnotationOverride() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputMissingOverrideGoodOverride.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputMissingOverrideGoodOverride.java"), expected);
}

/**
Expand All @@ -234,7 +241,8 @@ public void testGoodAnnotationOverrideJ5Compatible() throws Exception {
checkConfig.addAttribute("javaFiveCompatibility", "true");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputMissingOverrideGoodOverrideJava5.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputMissingOverrideGoodOverrideJava5.java"), expected);
}

@Test
Expand Down

0 comments on commit b605421

Please sign in to comment.