2020import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
2121import static org .openrewrite .java .Assertions .java ;
2222
23+ import java .io .BufferedWriter ;
2324import java .io .ByteArrayOutputStream ;
2425import java .io .File ;
26+ import java .io .FileWriter ;
27+ import java .io .IOException ;
2528import java .nio .file .Files ;
2629import java .nio .file .Path ;
2730import java .util .Collections ;
2831import java .util .List ;
2932
33+ import org .checkstyle .autofix .CheckstyleAutoFix ;
3034import org .checkstyle .autofix .InputClassRenamer ;
3135import org .checkstyle .autofix .RemoveViolationComments ;
3236import org .checkstyle .autofix .parser .CheckConfiguration ;
@@ -48,9 +52,6 @@ public abstract class AbstractRecipeTestSupport extends AbstractXmlTestSupport
4852
4953 protected abstract String getSubpackage ();
5054
51- protected abstract Recipe createRecipe (List <CheckstyleViolation > violations ,
52- CheckConfiguration configuration );
53-
5455 @ Override
5556 protected String getPackageLocation () {
5657 return "org/checkstyle/autofix/recipe/" + getSubpackage ();
@@ -63,38 +64,55 @@ protected void verify(String testCaseName) throws Exception {
6364 final Configuration config = getCheckConfigurations (inputPath );
6465 verifyOutputFile (outputPath , config );
6566
66- final List <CheckstyleViolation > violations = runCheckstyle (inputPath , config );
67+ final Path violations = runCheckstyle (inputPath , config );
68+ final Path configPath = createConfigFile (config );
6769
68- verifyWithInlineConfigParser (getPath (inputPath ), convertToExpectedMessages (violations ));
70+ verifyWithInlineConfigParser (getPath (inputPath ),
71+ convertToExpectedMessages (CheckstyleReportParser .parse (violations )));
6972
7073 final String beforeCode = readFile (getPath (inputPath ));
7174 final String expectedAfterCode = readFile (getPath (outputPath ));
72- final CheckConfiguration checkConfig = ConfigurationLoader .mapConfiguration (config );
73- final Recipe mainRecipe = createRecipe (violations , checkConfig );
74- testRecipe (beforeCode , expectedAfterCode ,
75- getPath (inputPath ), new InputClassRenamer (),
76- mainRecipe , new RemoveViolationComments ());
75+
76+ try {
77+ final Recipe mainRecipe = new CheckstyleAutoFix (violations .toString (),
78+ configPath .toString ());
79+ testRecipe (beforeCode , expectedAfterCode ,
80+ getPath (inputPath ), new InputClassRenamer (),
81+ mainRecipe , new RemoveViolationComments ());
82+ }
83+ finally {
84+ Files .deleteIfExists (configPath );
85+ Files .deleteIfExists (violations );
86+ }
7787 }
7888
7989 protected void verify (Configuration config , String testCaseName ) throws Exception {
80-
8190 final String inputPath = getInputFilePath (testCaseName );
8291 final String outputPath = getOutputFilePath (testCaseName );
8392
8493 verifyOutputFile (outputPath , config );
8594
86- final List < CheckstyleViolation > violations = runCheckstyle (inputPath , config );
95+ final Path violationReportPath = runCheckstyle (inputPath , config );
8796
8897 final String beforeCode = readFile (getPath (inputPath ));
8998 final String expectedAfterCode = readFile (getPath (outputPath ));
90- final CheckConfiguration checkConfig = ConfigurationLoader .mapConfiguration (config );
91- final Recipe mainRecipe = createRecipe (violations , checkConfig );
92- testRecipe (beforeCode , expectedAfterCode ,
93- getPath (inputPath ), new InputClassRenamer (), mainRecipe );
99+
100+ final Path configPath = createConfigFile (config );
101+
102+ try {
103+ final Recipe mainRecipe = new CheckstyleAutoFix (violationReportPath .toString (),
104+ configPath .toString ());
105+ testRecipe (beforeCode , expectedAfterCode ,
106+ getPath (inputPath ), new InputClassRenamer (), mainRecipe );
107+ }
108+ finally {
109+ Files .deleteIfExists (configPath );
110+ Files .deleteIfExists (violationReportPath );
111+ }
94112 }
95113
96- private List < CheckstyleViolation > runCheckstyle (String inputPath ,
97- Configuration config ) throws Exception {
114+ private Path runCheckstyle (String inputPath ,
115+ Configuration config ) throws Exception {
98116
99117 final Checker checker = createChecker (config );
100118 final ByteArrayOutputStream xmlOutput = new ByteArrayOutputStream ();
@@ -105,34 +123,50 @@ private List<CheckstyleViolation> runCheckstyle(String inputPath,
105123 checker .process (filesToCheck );
106124
107125 final Path tempXmlPath = Files .createTempFile ("checkstyle-report" , ".xml" );
108- try {
109- Files .write (tempXmlPath , xmlOutput .toByteArray ());
110- return CheckstyleReportParser .parse (tempXmlPath );
111- }
112- finally {
113- Files .deleteIfExists (tempXmlPath );
114- }
126+
127+ Files .write (tempXmlPath , xmlOutput .toByteArray ());
128+ return tempXmlPath ;
115129 }
116130
117- private void verifyOutputFile (String outputPath , Configuration config ) throws Exception {
131+ private Path createConfigFile (Configuration config ) throws Exception {
132+ final CheckConfiguration checkConfig = ConfigurationLoader .mapConfiguration (config );
133+ final Path configPath = Files .createTempFile ("checkstyle-config" , ".xml" );
134+ writeConfigurationToXml (checkConfig , configPath .toString ());
135+ return configPath ;
136+ }
118137
119- final List <CheckstyleViolation > violations = runCheckstyle (outputPath , config );
120- if (!violations .isEmpty ()) {
121- final StringBuilder violationMessage =
122- new StringBuilder ("Checkstyle violations found in the output file:\n " );
138+ public static void writeConfigurationToXml (CheckConfiguration config , String filePath )
139+ throws IOException {
140+ try (BufferedWriter writer = new BufferedWriter (new FileWriter (filePath ))) {
141+ writer .write ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " );
142+ writer .write ("<!DOCTYPE module PUBLIC \" -//Checkstyle//DTD Checkstyle "
143+ + "Configuration 1.3//EN\" \" configuration_1_3.dtd\" >\n " );
144+ writeModule (config , writer );
145+ }
146+ }
147+
148+ private static void writeModule (CheckConfiguration config , BufferedWriter writer )
149+ throws IOException {
123150
124- violationMessage . append ( "outputFile: " ). append ( getPath ( outputPath )). append ( " \n " );
151+ writer . write ( "<module name= \" " + config . getName () + " \" > \n " );
125152
126- for (CheckstyleViolation violation : violations ) {
127- violationMessage
128- .append ("line: " ).append (violation .getLine ())
129- .append (", col: " ).append (violation .getColumn ())
130- .append (", message: " ).append (violation .getMessage ())
131- .append ("\n " );
132- }
153+ for (String propName : config .getPropertyNames ()) {
154+ writer .write (" <property name=\" " + propName + "\" value=\" "
155+ + config .getProperty (propName ) + "\" />\n " );
156+ }
133157
134- throw new IllegalStateException (violationMessage .toString ());
158+ for (CheckConfiguration child : config .getChildren ()) {
159+ writeModule (child , writer );
135160 }
161+
162+ writer .write ("</module>\n " );
163+ }
164+
165+ private Configuration getCheckConfigurations (String inputPath ) throws Exception {
166+ final String configFilePath = getPath (inputPath );
167+ final TestInputConfiguration testInputConfiguration =
168+ InlineConfigParser .parse (configFilePath );
169+ return testInputConfiguration .createConfiguration ();
136170 }
137171
138172 private String [] convertToExpectedMessages (List <CheckstyleViolation > violations ) {
@@ -146,13 +180,6 @@ private String[] convertToExpectedMessages(List<CheckstyleViolation> violations)
146180 .toArray (String []::new );
147181 }
148182
149- private Configuration getCheckConfigurations (String inputPath ) throws Exception {
150- final String configFilePath = getPath (inputPath );
151- final TestInputConfiguration testInputConfiguration =
152- InlineConfigParser .parse (configFilePath );
153- return testInputConfiguration .createConfiguration ();
154- }
155-
156183 private void testRecipe (String beforeCode , String expectedAfterCode ,
157184 String filePath , Recipe ... recipes ) {
158185 assertDoesNotThrow (() -> {
@@ -173,4 +200,8 @@ private String getOutputFilePath(String testCaseName) {
173200 return testCaseName .toLowerCase () + "/" + inputFileName ;
174201 }
175202
203+ private void verifyOutputFile (String outputPath , Configuration config ) throws Exception {
204+ verify (config , getPath (outputPath ), new String [0 ]);
205+ }
206+
176207}
0 commit comments