|
20 | 20 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
21 | 21 | import static org.openrewrite.java.Assertions.java; |
22 | 22 |
|
23 | | -import java.io.IOException; |
| 23 | +import java.io.ByteArrayOutputStream; |
| 24 | +import java.io.File; |
24 | 25 | import java.nio.charset.Charset; |
25 | 26 | import java.nio.file.Files; |
26 | | -import java.nio.file.Paths; |
| 27 | +import java.nio.file.Path; |
27 | 28 | import java.util.Arrays; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Optional; |
28 | 32 |
|
29 | 33 | import org.checkstyle.autofix.InputClassRenamer; |
| 34 | +import org.checkstyle.autofix.parser.CheckstyleReportParser; |
| 35 | +import org.checkstyle.autofix.parser.CheckstyleViolation; |
30 | 36 | import org.openrewrite.Recipe; |
31 | 37 | import org.openrewrite.test.RewriteTest; |
32 | 38 |
|
| 39 | +import com.puppycrawl.tools.checkstyle.AbstractXmlTestSupport; |
| 40 | +import com.puppycrawl.tools.checkstyle.Checker; |
| 41 | +import com.puppycrawl.tools.checkstyle.XMLLogger; |
33 | 42 | import com.puppycrawl.tools.checkstyle.api.CheckstyleException; |
34 | 43 | import com.puppycrawl.tools.checkstyle.api.Configuration; |
| 44 | +import com.puppycrawl.tools.checkstyle.bdd.InlineConfigParser; |
| 45 | +import com.puppycrawl.tools.checkstyle.bdd.TestInputConfiguration; |
35 | 46 |
|
36 | | -public abstract class AbstractRecipeTest implements RewriteTest { |
| 47 | +public abstract class AbstractRecipeTest extends AbstractXmlTestSupport implements RewriteTest { |
37 | 48 |
|
38 | | - private static final String BASE_TEST_RESOURCES_PATH = "src/test/resources/org" |
39 | | - + "/checkstyle/autofix/recipe/"; |
| 49 | + protected abstract String getSubpackage(); |
| 50 | + |
| 51 | + protected abstract String getCheckName(); |
| 52 | + |
| 53 | + protected abstract Recipe createRecipe(List<CheckstyleViolation> violations, |
| 54 | + Configuration configuration); |
| 55 | + |
| 56 | + @Override |
| 57 | + protected String getPackageLocation() { |
| 58 | + return "org/checkstyle/autofix/recipe/" + getSubpackage(); |
| 59 | + } |
40 | 60 |
|
41 | 61 | private Recipe createPreprocessingRecipe() { |
42 | 62 | return new InputClassRenamer(); |
43 | 63 | } |
44 | 64 |
|
45 | | - protected abstract Recipe getRecipe() throws CheckstyleException; |
| 65 | + protected void verify(String testCaseName) throws Exception { |
| 66 | + final String inputFileName = "Input" + testCaseName + ".java"; |
| 67 | + final String outputFileName = "Output" + testCaseName + ".java"; |
| 68 | + final String inputPath = testCaseName.toLowerCase() + "/" + inputFileName; |
| 69 | + final String outputPath = testCaseName.toLowerCase() + "/" + outputFileName; |
| 70 | + |
| 71 | + final Configuration config = getAllCheckConfigurations(inputPath); |
| 72 | + final List<CheckstyleViolation> violations = runCheckstyle(inputPath, config); |
| 73 | + final Configuration checkConfig = extractCheckConfiguration(config, getCheckName()); |
| 74 | + |
| 75 | + final String beforeCode = readFile(getPath(inputPath)); |
| 76 | + final String expectedAfterCode = readFile(getPath(outputPath)); |
| 77 | + final Recipe mainRecipe = createRecipe(violations, checkConfig); |
| 78 | + |
| 79 | + testRecipe(beforeCode, expectedAfterCode, |
| 80 | + getPath(inputPath), createPreprocessingRecipe(), mainRecipe); |
| 81 | + } |
46 | 82 |
|
47 | | - protected void testRecipe(String recipePath, String testCaseName) throws IOException, |
48 | | - CheckstyleException { |
49 | | - final String testCaseDir = testCaseName.toLowerCase(); |
| 83 | + protected void verify(Configuration checkConfig, String testCaseName) throws Exception { |
50 | 84 | final String inputFileName = "Input" + testCaseName + ".java"; |
51 | 85 | final String outputFileName = "Output" + testCaseName + ".java"; |
| 86 | + final String inputPath = testCaseName.toLowerCase() + "/" + inputFileName; |
| 87 | + final String outputPath = testCaseName.toLowerCase() + "/" + outputFileName; |
| 88 | + |
| 89 | + final List<CheckstyleViolation> violations = runCheckstyle(inputPath, checkConfig); |
52 | 90 |
|
53 | | - final String beforeCode = Files.readString(Paths.get(BASE_TEST_RESOURCES_PATH |
54 | | - + recipePath + "/" + testCaseDir + "/" + inputFileName)); |
| 91 | + final String beforeCode = readFile(getPath(inputPath)); |
| 92 | + final String expectedAfterCode = readFile(getPath(outputPath)); |
55 | 93 |
|
56 | | - final String afterCode = Files.readString(Paths.get(BASE_TEST_RESOURCES_PATH |
57 | | - + recipePath + "/" + testCaseDir + "/" + outputFileName)); |
| 94 | + final Recipe mainRecipe = createRecipe(violations, checkConfig); |
58 | 95 |
|
59 | | - final Recipe preprocessing = createPreprocessingRecipe(); |
60 | | - final Recipe mainRecipe = getRecipe(); |
| 96 | + testRecipe(beforeCode, expectedAfterCode, |
| 97 | + getPath(inputPath), createPreprocessingRecipe(), mainRecipe); |
| 98 | + } |
61 | 99 |
|
| 100 | + private List<CheckstyleViolation> runCheckstyle(String inputPath, |
| 101 | + Configuration config) throws Exception { |
| 102 | + |
| 103 | + final Checker checker = createChecker(config); |
| 104 | + final ByteArrayOutputStream xmlOutput = new ByteArrayOutputStream(); |
| 105 | + final XMLLogger logger = new XMLLogger(xmlOutput, XMLLogger.OutputStreamOptions.CLOSE); |
| 106 | + checker.addListener(logger); |
| 107 | + |
| 108 | + final List<File> filesToCheck = Collections.singletonList(new File(getPath(inputPath))); |
| 109 | + checker.process(filesToCheck); |
| 110 | + |
| 111 | + final Path tempXmlPath = Files.createTempFile("checkstyle-report", ".xml"); |
| 112 | + try { |
| 113 | + Files.write(tempXmlPath, xmlOutput.toByteArray()); |
| 114 | + return CheckstyleReportParser.parse(tempXmlPath); |
| 115 | + } |
| 116 | + finally { |
| 117 | + Files.deleteIfExists(tempXmlPath); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private Configuration getAllCheckConfigurations(String inputPath) throws Exception { |
| 122 | + final String configFilePath = getPath(inputPath); |
| 123 | + final TestInputConfiguration testInputConfiguration = |
| 124 | + InlineConfigParser.parse(configFilePath); |
| 125 | + return testInputConfiguration.createConfiguration(); |
| 126 | + } |
| 127 | + |
| 128 | + private void testRecipe(String beforeCode, String expectedAfterCode, |
| 129 | + String filePath, Recipe... recipes) { |
62 | 130 | assertDoesNotThrow(() -> { |
63 | 131 | rewriteRun( |
64 | | - spec -> spec.recipes(preprocessing, mainRecipe), |
65 | | - java(beforeCode, afterCode) |
| 132 | + spec -> spec.recipes(recipes), |
| 133 | + java(beforeCode, expectedAfterCode, spec -> spec.path(filePath)) |
66 | 134 | ); |
67 | 135 | }); |
68 | 136 | } |
69 | 137 |
|
70 | 138 | protected Configuration extractCheckConfiguration(Configuration config, String checkName) { |
71 | | - |
72 | | - return Arrays.stream(config.getChildren()) |
| 139 | + return Optional.of(config) |
73 | 140 | .filter(child -> checkName.equals(child.getName())) |
74 | | - .findFirst() |
75 | | - .orElseThrow(() -> { |
76 | | - return new IllegalArgumentException(checkName + "configuration not " |
77 | | - + "found"); |
78 | | - }); |
| 141 | + .orElse(Arrays.stream(config.getChildren()) |
| 142 | + .filter(child -> { |
| 143 | + return "com.puppycrawl.tools.checkstyle.TreeWalker" |
| 144 | + .equals(child.getName()); |
| 145 | + }) |
| 146 | + .flatMap(treeWalker -> Arrays.stream(treeWalker.getChildren())) |
| 147 | + .filter(child -> checkName.equals(child.getName())) |
| 148 | + .findFirst() |
| 149 | + .orElseThrow(() -> { |
| 150 | + return new IllegalArgumentException(checkName |
| 151 | + + " configuration not found"); |
| 152 | + })); |
79 | 153 | } |
80 | 154 |
|
81 | 155 | protected Charset getCharset(Configuration config) { |
|
0 commit comments