55import com .github .javaparser .StaticJavaParser ;
66import com .github .javaparser .ast .CompilationUnit ;
77import com .github .javaparser .printer .lexicalpreservation .LexicalPreservingPrinter ;
8+ import io .codemodder .CodemodFileScanningResult ;
89import io .codemodder .codetf .DetectorRule ;
910import io .codemodder .remediation .FixCandidateSearcher ;
1011import io .codemodder .remediation .SearcherStrategyRemediator ;
@@ -133,7 +134,14 @@ void it_fixes_obvious_response_write_methods(final String beforeCode, final Stri
133134 CompilationUnit cu = StaticJavaParser .parse (beforeCode );
134135 LexicalPreservingPrinter .setup (cu );
135136
136- XSSFinding finding = new XSSFinding ("should_be_fixed" , 3 , null );
137+ var result = scanAndFix (cu , 3 );
138+ assertThat (result .changes ()).isNotEmpty ();
139+ String actualCode = LexicalPreservingPrinter .print (cu );
140+ assertThat (actualCode ).isEqualToIgnoringWhitespace (afterCode );
141+ }
142+
143+ private CodemodFileScanningResult scanAndFix (final CompilationUnit cu , final int line ) {
144+ XSSFinding finding = new XSSFinding ("should_be_fixed" , line , null );
137145 var remediator =
138146 new SearcherStrategyRemediator .Builder <XSSFinding >()
139147 .withSearcherStrategyPair (
@@ -142,18 +150,58 @@ void it_fixes_obvious_response_write_methods(final String beforeCode, final Stri
142150 .build (),
143151 fixer )
144152 .build ();
145- var result =
146- remediator .remediateAll (
147- cu ,
148- "path" ,
149- rule ,
150- List .of (finding ),
151- XSSFinding ::key ,
152- XSSFinding ::line ,
153- x -> Optional .empty (),
154- x -> Optional .ofNullable (x .column ()));
155- assertThat (result .changes ()).isNotEmpty ();
156- String actualCode = LexicalPreservingPrinter .print (cu );
157- assertThat (actualCode ).isEqualToIgnoringWhitespace (afterCode );
153+ return remediator .remediateAll (
154+ cu ,
155+ "path" ,
156+ rule ,
157+ List .of (finding ),
158+ XSSFinding ::key ,
159+ XSSFinding ::line ,
160+ x -> Optional .empty (),
161+ x -> Optional .ofNullable (x .column ()));
162+ }
163+
164+ @ ParameterizedTest
165+ @ MethodSource ("unfixableSamples" )
166+ void it_does_not_fix_unfixable_response_write_methods (final String beforeCode , final int line ) {
167+ CompilationUnit cu = StaticJavaParser .parse (beforeCode );
168+ LexicalPreservingPrinter .setup (cu );
169+ var result = scanAndFix (cu , line );
170+ assertThat (result .changes ()).isEmpty ();
171+ }
172+
173+ private static Stream <Arguments > unfixableSamples () {
174+ return Stream .of (
175+ // this is all string literals -- ignore
176+ Arguments .of (
177+ """
178+ class Samples {
179+ void should_be_fixed(String s) {
180+ getWriter().write("<div>" + "<b>" + "</div>");
181+ }
182+ }
183+ """ ,
184+ 3 ),
185+ // this is ambiguous which value to encode
186+ Arguments .of (
187+ """
188+ class Samples {
189+ void should_be_fixed(String s) {
190+ getWriter().write("<div>" + a + b + "</div>");
191+ }
192+ }
193+ """ ,
194+ 3 ),
195+ // this is the wrong line
196+ Arguments .of (
197+ """
198+ class Samples {
199+ void should_be_fixed(String s) {
200+ // extra line, right line is 4
201+ getWriter().write("<div>" + a + "</div>");
202+ }
203+ }
204+ """ ,
205+ 3 ));
158206 }
159207}
0 commit comments