|
| 1 | +/////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite. |
| 3 | +// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +/////////////////////////////////////////////////////////////////////////////////////////////// |
| 17 | + |
| 18 | +package org.checkstyle.autofix.recipe; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 21 | +import static org.openrewrite.java.Assertions.java; |
| 22 | + |
| 23 | +import java.io.ByteArrayOutputStream; |
| 24 | +import java.io.File; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import org.checkstyle.autofix.InputClassRenamer; |
| 31 | +import org.checkstyle.autofix.parser.CheckstyleReportParser; |
| 32 | +import org.checkstyle.autofix.parser.CheckstyleViolation; |
| 33 | +import org.openrewrite.Recipe; |
| 34 | +import org.openrewrite.test.RewriteTest; |
| 35 | + |
| 36 | +import com.puppycrawl.tools.checkstyle.AbstractXmlTestSupport; |
| 37 | +import com.puppycrawl.tools.checkstyle.Checker; |
| 38 | +import com.puppycrawl.tools.checkstyle.XMLLogger; |
| 39 | +import com.puppycrawl.tools.checkstyle.api.Configuration; |
| 40 | +import com.puppycrawl.tools.checkstyle.bdd.InlineConfigParser; |
| 41 | +import com.puppycrawl.tools.checkstyle.bdd.TestInputConfiguration; |
| 42 | + |
| 43 | +public abstract class AbstractRecipeTestSupport extends AbstractXmlTestSupport |
| 44 | + implements RewriteTest { |
| 45 | + |
| 46 | + protected abstract String getSubpackage(); |
| 47 | + |
| 48 | + protected abstract String getCheckName(); |
| 49 | + |
| 50 | + protected abstract Recipe createRecipe(List<CheckstyleViolation> violations); |
| 51 | + |
| 52 | + @Override |
| 53 | + protected String getPackageLocation() { |
| 54 | + return "org/checkstyle/autofix/recipe/" + getSubpackage(); |
| 55 | + } |
| 56 | + |
| 57 | + private Recipe createPreprocessingRecipe() { |
| 58 | + return new InputClassRenamer(); |
| 59 | + } |
| 60 | + |
| 61 | + protected void verify(String testCaseName) throws Exception { |
| 62 | + final String inputFileName = "Input" + testCaseName + ".java"; |
| 63 | + final String outputFileName = "Output" + testCaseName + ".java"; |
| 64 | + final String inputPath = testCaseName.toLowerCase() + "/" + inputFileName; |
| 65 | + final String outputPath = testCaseName.toLowerCase() + "/" + outputFileName; |
| 66 | + |
| 67 | + final Configuration config = getCheckConfigurations(inputPath); |
| 68 | + final List<CheckstyleViolation> violations = runCheckstyle(inputPath, config); |
| 69 | + |
| 70 | + final String beforeCode = readFile(getPath(inputPath)); |
| 71 | + final String expectedAfterCode = readFile(getPath(outputPath)); |
| 72 | + |
| 73 | + final Recipe mainRecipe = createRecipe(violations); |
| 74 | + |
| 75 | + testRecipe(beforeCode, expectedAfterCode, |
| 76 | + getPath(inputPath), createPreprocessingRecipe(), mainRecipe); |
| 77 | + } |
| 78 | + |
| 79 | + private List<CheckstyleViolation> runCheckstyle(String inputPath, |
| 80 | + Configuration config) throws Exception { |
| 81 | + |
| 82 | + final Checker checker = createChecker(config); |
| 83 | + final ByteArrayOutputStream xmlOutput = new ByteArrayOutputStream(); |
| 84 | + final XMLLogger logger = new XMLLogger(xmlOutput, XMLLogger.OutputStreamOptions.CLOSE); |
| 85 | + checker.addListener(logger); |
| 86 | + |
| 87 | + final List<File> filesToCheck = Collections.singletonList(new File(getPath(inputPath))); |
| 88 | + checker.process(filesToCheck); |
| 89 | + |
| 90 | + final Path tempXmlPath = Files.createTempFile("checkstyle-report", ".xml"); |
| 91 | + try { |
| 92 | + Files.write(tempXmlPath, xmlOutput.toByteArray()); |
| 93 | + return CheckstyleReportParser.parse(tempXmlPath); |
| 94 | + } |
| 95 | + finally { |
| 96 | + Files.deleteIfExists(tempXmlPath); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private Configuration getCheckConfigurations(String inputPath) throws Exception { |
| 101 | + final String configFilePath = getPath(inputPath); |
| 102 | + final TestInputConfiguration testInputConfiguration = |
| 103 | + InlineConfigParser.parse(configFilePath); |
| 104 | + return testInputConfiguration.createConfiguration(); |
| 105 | + } |
| 106 | + |
| 107 | + private void testRecipe(String beforeCode, String expectedAfterCode, |
| 108 | + String filePath, Recipe... recipes) { |
| 109 | + assertDoesNotThrow(() -> { |
| 110 | + rewriteRun( |
| 111 | + spec -> spec.recipes(recipes), |
| 112 | + java(beforeCode, expectedAfterCode, spec -> spec.path(filePath)) |
| 113 | + ); |
| 114 | + }); |
| 115 | + } |
| 116 | +} |
0 commit comments