2020import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
2121import static org .openrewrite .java .Assertions .java ;
2222
23- import java .io .ByteArrayOutputStream ;
24- import java .io .File ;
23+ import java .io .*;
2524import java .nio .file .Files ;
2625import java .nio .file .Path ;
2726import java .util .Collections ;
2827import java .util .List ;
2928
29+ import com .puppycrawl .tools .checkstyle .api .CheckstyleException ;
30+ import org .checkstyle .autofix .CheckstyleAutoFix ;
3031import org .checkstyle .autofix .InputClassRenamer ;
3132import org .checkstyle .autofix .RemoveViolationComments ;
3233import org .checkstyle .autofix .parser .CheckConfiguration ;
33- import org .checkstyle .autofix .parser .CheckstyleReportParser ;
3434import org .checkstyle .autofix .parser .CheckstyleViolation ;
35- import org .checkstyle .autofix .parser .ConfigurationLoader ;
3635import org .openrewrite .Recipe ;
3736import org .openrewrite .test .RewriteTest ;
3837
@@ -57,7 +56,25 @@ protected String getPackageLocation() {
5756 }
5857
5958 protected void verify (String testCaseName ) throws Exception {
60- verify (getCheckConfigurations (getInputFilePath (testCaseName )), testCaseName );
59+
60+ final String inputPath = getInputFilePath (testCaseName );
61+ final String outputPath = getOutputFilePath (testCaseName );
62+ final Configuration config = getCheckConfigurations (inputPath );
63+ verifyOutputFile (outputPath , config );
64+
65+ final Path violationPath = runCheckstyle (inputPath , config );
66+ List <String > lines = Files .readAllLines (Path .of (getPath (inputPath )));
67+ List <String > inlineConfig = getInlineConfig (lines );
68+ final Path configPath = Files .createTempFile ("checkstyle-config" , ".xml" );
69+ Files .write (configPath , inlineConfig );
70+
71+ final String beforeCode = readFile (getPath (inputPath ));
72+ final String expectedAfterCode = readFile (getPath (outputPath ));
73+
74+ final Recipe recipe = new CheckstyleAutoFix (violationPath .toString (), configPath .toString ());
75+
76+ testRecipe (beforeCode , expectedAfterCode ,
77+ getPath (inputPath ), new InputClassRenamer (), recipe , new RemoveViolationComments ());
6178 }
6279
6380 protected void verify (Configuration config , String testCaseName ) throws Exception {
@@ -67,18 +84,27 @@ protected void verify(Configuration config, String testCaseName) throws Exceptio
6784
6885 verifyOutputFile (outputPath , config );
6986
70- final List <CheckstyleViolation > violations = runCheckstyle (inputPath , config );
87+ final Path violationPath = runCheckstyle (inputPath , config );
88+
89+
7190
7291 final String beforeCode = readFile (getPath (inputPath ));
7392 final String expectedAfterCode = readFile (getPath (outputPath ));
74- final CheckConfiguration checkConfig = ConfigurationLoader .mapConfiguration (config );
75- final Recipe mainRecipe = createRecipe (violations , checkConfig );
93+ final Path configPath = Files .createTempFile ("checkstyle-config" , ".xml" );
94+ writeConfigurationToXml (config , configPath .toString ());
95+
96+ List <String > lines = Files .readAllLines (configPath );
97+ for (String line : lines ) {
98+ System .out .println (line );
99+ }
100+
101+ final Recipe recipe = new CheckstyleAutoFix (violationPath .toString (), configPath .toString ());
76102 testRecipe (beforeCode , expectedAfterCode ,
77- getPath (inputPath ), new InputClassRenamer (),
78- new RemoveViolationComments (), mainRecipe );
103+ getPath (inputPath ), new InputClassRenamer (), recipe ,
104+ new RemoveViolationComments ());
79105 }
80106
81- private List < CheckstyleViolation > runCheckstyle (String inputPath ,
107+ private Path runCheckstyle (String inputPath ,
82108 Configuration config ) throws Exception {
83109
84110 final Checker checker = createChecker (config );
@@ -90,34 +116,14 @@ private List<CheckstyleViolation> runCheckstyle(String inputPath,
90116 checker .process (filesToCheck );
91117
92118 final Path tempXmlPath = Files .createTempFile ("checkstyle-report" , ".xml" );
93- try {
94- Files .write (tempXmlPath , xmlOutput .toByteArray ());
95- return CheckstyleReportParser .parse (tempXmlPath );
96- }
97- finally {
98- Files .deleteIfExists (tempXmlPath );
99- }
100- }
101-
102- private void verifyOutputFile (String outputPath , Configuration config ) throws Exception {
103-
104- final List <CheckstyleViolation > violations = runCheckstyle (outputPath , config );
105- if (!violations .isEmpty ()) {
106- final StringBuilder violationMessage =
107- new StringBuilder ("Checkstyle violations found in the output file:\n " );
108119
109- violationMessage .append ("outputFile: " ).append (getPath (outputPath )).append ("\n " );
120+ Files .write (tempXmlPath , xmlOutput .toByteArray ());
121+ return tempXmlPath ;
110122
111- for (CheckstyleViolation violation : violations ) {
112- violationMessage
113- .append ("line: " ).append (violation .getLine ())
114- .append (", col: " ).append (violation .getColumn ())
115- .append (", message: " ).append (violation .getMessage ())
116- .append ("\n " );
117- }
123+ }
118124
119- throw new IllegalStateException ( violationMessage . toString ());
120- }
125+ private void verifyOutputFile ( String outputPath , Configuration config ) throws Exception {
126+ verify ( config , getPath ( outputPath ), new String [ 0 ]);
121127 }
122128
123129 private Configuration getCheckConfigurations (String inputPath ) throws Exception {
@@ -137,6 +143,33 @@ private void testRecipe(String beforeCode, String expectedAfterCode,
137143 });
138144 }
139145
146+ public static void writeConfigurationToXml (Configuration config , String filePath ) throws IOException , CheckstyleException {
147+ try (BufferedWriter writer = new BufferedWriter (new FileWriter (filePath ))) {
148+ writer .write ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " );
149+ writeModule (config , writer );
150+ }
151+ }
152+
153+ private static void writeModule (Configuration config , BufferedWriter writer ) throws IOException , CheckstyleException {
154+ writer .write ("<module name=\" " + config .getName () + "\" >\n " );
155+ for (String propName : config .getPropertyNames ())
156+ writer .write (" <property name=\" " + propName + "\" value=\" "
157+ + config .getProperty (propName ).replace ("&" , "&" )
158+ .replace ("<" , "<" ).replace (">" , ">" )
159+ .replace ("\" " , """ ).replace ("'" , "'" )
160+ + "\" />\n " );
161+ for (Configuration child : config .getChildren ())
162+ writeModule (child , writer );
163+ writer .write ("</module>\n " );
164+ }
165+
166+ private List <String > getInlineConfig (List <String > lines ) {
167+ return lines .stream ()
168+ .skip (1L )
169+ .takeWhile ((line ) -> !line .startsWith ("*/" ))
170+ .toList ();
171+ }
172+
140173 private String getInputFilePath (String testCaseName ) {
141174 final String inputFileName = "Input" + testCaseName + ".java" ;
142175 return testCaseName .toLowerCase () + "/" + inputFileName ;
0 commit comments