Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private ConfigurationLoader() {
// utility class
}

private static CheckConfiguration mapConfiguration(Configuration config) {
public static CheckConfiguration mapConfiguration(Configuration config) {
final Map<String, String> properties = new HashMap<>();
final String[] propertyNames = config.getPropertyNames();
for (String propertyName : propertyNames) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/checkstyle/autofix/recipe/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class Header extends Recipe {
private static final String HEADER_FILE_PROPERTY = "headerFile";
private static final String IGNORE_LINES_PROPERTY = "ignoreLines";
private static final String CHARSET_PROPERTY = "charset";
private static final String LINE_SEPARATOR = "\n";

private final List<CheckstyleViolation> violations;
private final CheckConfiguration config;
Expand Down Expand Up @@ -129,7 +130,7 @@ public J visit(Tree tree, ExecutionContext ctx) {
currentHeader, ignoreLines);

sourceFile = sourceFile.withPrefix(
Space.format(fixedHeader + System.lineSeparator()));
Space.format(fixedHeader + LINE_SEPARATOR));
}
result = super.visit(sourceFile, ctx);
}
Expand Down Expand Up @@ -166,7 +167,7 @@ private static String fixHeaderLines(String licenseHeader,
}
}

return String.join(System.lineSeparator(), currentLines);
return String.join(LINE_SEPARATOR, currentLines);
}

private boolean hasViolation(Path filePath) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@

import org.checkstyle.autofix.InputClassRenamer;
import org.checkstyle.autofix.RemoveViolationComments;
import org.checkstyle.autofix.parser.CheckConfiguration;
import org.checkstyle.autofix.parser.CheckstyleReportParser;
import org.checkstyle.autofix.parser.CheckstyleViolation;
import org.checkstyle.autofix.parser.ConfigurationLoader;
import org.openrewrite.Recipe;
import org.openrewrite.test.RewriteTest;

Expand All @@ -46,29 +48,31 @@ public abstract class AbstractRecipeTestSupport extends AbstractXmlTestSupport

protected abstract String getSubpackage();

protected abstract Recipe createRecipe(List<CheckstyleViolation> violations);
protected abstract Recipe createRecipe(List<CheckstyleViolation> violations,
CheckConfiguration configuration);

@Override
protected String getPackageLocation() {
return "org/checkstyle/autofix/recipe/" + getSubpackage();
}

protected void verify(String testCaseName) throws Exception {
final String inputFileName = "Input" + testCaseName + ".java";
final String outputFileName = "Output" + testCaseName + ".java";
final String inputPath = testCaseName.toLowerCase() + "/" + inputFileName;
final String outputPath = testCaseName.toLowerCase() + "/" + outputFileName;
verify(getCheckConfigurations(getInputFilePath(testCaseName)), testCaseName);
}

protected void verify(Configuration config, String testCaseName) throws Exception {

verifyOutputFile(outputPath);
final String inputPath = getInputFilePath(testCaseName);
final String outputPath = getOutputFilePath(testCaseName);

verifyOutputFile(outputPath, config);

final Configuration config = getCheckConfigurations(inputPath);
final List<CheckstyleViolation> violations = runCheckstyle(inputPath, config);

final String beforeCode = readFile(getPath(inputPath));
final String expectedAfterCode = readFile(getPath(outputPath));

final Recipe mainRecipe = createRecipe(violations);

final CheckConfiguration checkConfig = ConfigurationLoader.mapConfiguration(config);
final Recipe mainRecipe = createRecipe(violations, checkConfig);
testRecipe(beforeCode, expectedAfterCode,
getPath(inputPath), new InputClassRenamer(),
new RemoveViolationComments(), mainRecipe);
Expand All @@ -95,9 +99,8 @@ private List<CheckstyleViolation> runCheckstyle(String inputPath,
}
}

private void verifyOutputFile(String outputPath) throws Exception {
private void verifyOutputFile(String outputPath, Configuration config) throws Exception {

final Configuration config = getCheckConfigurations(outputPath);
final List<CheckstyleViolation> violations = runCheckstyle(outputPath, config);
if (!violations.isEmpty()) {
final StringBuilder violationMessage =
Expand Down Expand Up @@ -133,4 +136,15 @@ private void testRecipe(String beforeCode, String expectedAfterCode,
);
});
}

private String getInputFilePath(String testCaseName) {
final String inputFileName = "Input" + testCaseName + ".java";
return testCaseName.toLowerCase() + "/" + inputFileName;
}

private String getOutputFilePath(String testCaseName) {
final String inputFileName = "Output" + testCaseName + ".java";
return testCaseName.toLowerCase() + "/" + inputFileName;
}

}
49 changes: 24 additions & 25 deletions src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,52 @@

package org.checkstyle.autofix.recipe;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import org.checkstyle.autofix.parser.CheckConfiguration;
import org.checkstyle.autofix.parser.CheckstyleReportParser;
import org.checkstyle.autofix.parser.CheckstyleViolation;
import org.checkstyle.autofix.parser.ConfigurationLoader;
import org.junit.jupiter.api.Test;
import org.openrewrite.Recipe;

import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck;

public class HeaderTest extends AbstractRecipeTest {
public class HeaderTest extends AbstractRecipeTestSupport {

@Override
protected Recipe getRecipe() throws CheckstyleException {
final String reportPath = "src/test/resources/org/checkstyle/autofix/recipe/header"
+ "/report.xml";

final String configPath = "src/test/resources/org/checkstyle/autofix/recipe/header"
+ "/config.xml";

final CheckConfiguration config = ConfigurationLoader.loadConfiguration(configPath, null);
protected String getSubpackage() {
return "header";
}

final List<CheckstyleViolation> violations =
CheckstyleReportParser.parse(Path.of(reportPath));
@Override
protected Recipe createRecipe(List<CheckstyleViolation> violations, CheckConfiguration config) {

final CheckConfiguration checkConfig = config
.getChildConfig("Header");
return new Header(violations, config);

return new Header(violations, checkConfig);
}

@Test
void headerTest() throws IOException, CheckstyleException {
testRecipe("header", "HeaderBlankLines");
void headerTest() throws Exception {
verify(getHeaderConfig(), "HeaderBlankLines");
}

@Test
void headerCommentTest() throws IOException, CheckstyleException {
testRecipe("header", "HeaderComments");
void headerCommentTest() throws Exception {
verify(getHeaderConfig(), "HeaderComments");
}

@Test
void headerIncorrect() throws IOException, CheckstyleException {
testRecipe("header", "HeaderIncorrect");
void headerIncorrect() throws Exception {
verify(getHeaderConfig(), "HeaderIncorrect");
}

private DefaultConfiguration getHeaderConfig() {
final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
final String headerPath = "src/test/resources/org/checkstyle/"
+ "autofix/recipe/header/header.txt";
checkConfig.addProperty("headerFile", headerPath);
checkConfig.addProperty("ignoreLines", "3");
return checkConfig;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.List;

import org.checkstyle.autofix.parser.CheckConfiguration;
import org.checkstyle.autofix.parser.CheckstyleViolation;
import org.junit.jupiter.api.Test;
import org.openrewrite.Recipe;
Expand All @@ -31,7 +32,7 @@ protected String getSubpackage() {
}

@Override
protected Recipe createRecipe(List<CheckstyleViolation> violations) {
protected Recipe createRecipe(List<CheckstyleViolation> violations, CheckConfiguration config) {

return new UpperEll(violations);
}
Expand Down
15 changes: 0 additions & 15 deletions src/test/resources/org/checkstyle/autofix/recipe/header/config.xml

This file was deleted.

19 changes: 0 additions & 19 deletions src/test/resources/org/checkstyle/autofix/recipe/header/report.xml

This file was deleted.

Loading