Skip to content

Commit 5e08a00

Browse files
amishra-ugithub-actions[bot]timtebeek
authored
Fix ParameterizedRunnerToParameterized: Remove redundant super call (#718)
* Fix ParameterizedRunnerToParameterized: Remove redundant super call * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix bot changes * Remove one additional super call no longer allowed * Trim how we remove super call --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <[email protected]>
1 parent 2c28b9e commit 5e08a00

File tree

2 files changed

+93
-4
lines changed

2 files changed

+93
-4
lines changed

src/main/java/org/openrewrite/java/testing/junit5/ParameterizedRunnerToParameterized.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
7676

7777
// Constructor Injected Test
7878
if (parametersMethodName != null && constructorParams != null && constructorParams.stream().anyMatch(org.openrewrite.java.tree.J.VariableDeclarations.class::isInstance)) {
79-
doAfterVisit(new ParameterizedRunnerToParameterizedTestsVisitor(classDecl, parametersMethodName, initMethodName, parametersAnnotationArguments, constructorParams, true, beforeMethodName,ctx));
79+
doAfterVisit(new ParameterizedRunnerToParameterizedTestsVisitor(classDecl, parametersMethodName, initMethodName, parametersAnnotationArguments, constructorParams, true, beforeMethodName, ctx));
8080
}
8181

8282
// Field Injected Test
@@ -266,7 +266,6 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
266266
return cd;
267267
}
268268

269-
270269
if (initMethodDeclarationTemplate != null) {
271270
cd = initMethodDeclarationTemplate.apply(updateCursor(cd), cd.getBody().getCoordinates().lastStatement());
272271
J.Block finalBody = cd.getBody();
@@ -291,7 +290,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
291290
if (statement instanceof J.VariableDeclarations) {
292291
J.VariableDeclarations varDecls = (J.VariableDeclarations) statement;
293292
if (varDecls.getVariables().stream().anyMatch(it -> fieldNames.contains(it.getSimpleName())) &&
294-
(varDecls.hasModifier(J.Modifier.Type.Final))) {
293+
(varDecls.hasModifier(J.Modifier.Type.Final))) {
295294
varDecls = varDecls.withModifiers(ListUtils.map(varDecls.getModifiers(), mod -> mod.getType() == J.Modifier.Type.Final ? null : mod));
296295
statement = maybeAutoFormat(statement, varDecls, ctx, new Cursor(getCursor(), finalBody));
297296
}
@@ -382,6 +381,12 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
382381
})
383382
.collect(Collectors.toSet());
384383
getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance).putMessage("INIT_VARS", fieldNames);
384+
385+
// Remove any potential super call
386+
m = m.withBody(m.getBody().withStatements(ListUtils.mapFirst(m.getBody().getStatements(),
387+
stmt -> stmt instanceof J.MethodInvocation &&
388+
"super".equals(((J.MethodInvocation) stmt).getSimpleName()) ?
389+
null : stmt)));
385390
}
386391
}
387392
return m;

src/test/java/org/openrewrite/java/testing/junit5/ParameterizedRunnerToParameterizedTest.java

+85-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,6 @@ public static Collection<Object[]> data1() {
497497
}
498498
499499
public void initI1(String path) {
500-
super(path);
501500
}
502501
503502
@MethodSource("data1")
@@ -590,4 +589,89 @@ public void initSampleTestClass(int num1, int num2, int num3) {
590589
"""
591590
));
592591
}
592+
593+
@Test
594+
void methodSourceWithSuperCall() {
595+
//language=java
596+
rewriteRun(
597+
spec -> spec.typeValidationOptions(TypeValidation.none()),
598+
java(
599+
"""
600+
import org.junit.Test;
601+
import org.junit.runner.RunWith;
602+
import org.junit.runners.Parameterized;
603+
import org.junit.runners.Parameterized.Parameters;
604+
605+
import java.util.Arrays;
606+
import java.util.List;
607+
608+
@RunWith(Parameterized.class)
609+
public class VetTests {
610+
611+
private String firstName;
612+
private String lastName;
613+
private Integer id;
614+
615+
public VetTests(String firstName, String lastName, Integer id) {
616+
super();
617+
this.firstName = firstName;
618+
this.lastName = lastName;
619+
this.id = id;
620+
}
621+
622+
@Test
623+
public void testSerialization() {
624+
Vet vet = new Vet();
625+
vet.setFirstName(firstName);
626+
vet.setLastName(lastName);
627+
vet.setId(id);
628+
}
629+
630+
@Parameters
631+
public static List<Object[]> parameters() {
632+
return Arrays.asList(
633+
new Object[] { "Otis", "TheDog", 124 },
634+
new Object[] { "Garfield", "TheBoss", 126 });
635+
}
636+
}
637+
""",
638+
"""
639+
import org.junit.jupiter.params.ParameterizedTest;
640+
import org.junit.jupiter.params.provider.MethodSource;
641+
642+
import java.util.Arrays;
643+
import java.util.List;
644+
645+
public class VetTests {
646+
647+
private String firstName;
648+
private String lastName;
649+
private Integer id;
650+
651+
public void initVetTests(String firstName, String lastName, Integer id) {
652+
this.firstName = firstName;
653+
this.lastName = lastName;
654+
this.id = id;
655+
}
656+
657+
@MethodSource("parameters")
658+
@ParameterizedTest
659+
public void testSerialization(String firstName, String lastName, Integer id) {
660+
initVetTests(firstName, lastName, id);
661+
Vet vet = new Vet();
662+
vet.setFirstName(firstName);
663+
vet.setLastName(lastName);
664+
vet.setId(id);
665+
}
666+
667+
public static List<Object[]> parameters() {
668+
return Arrays.asList(
669+
new Object[] { "Otis", "TheDog", 124 },
670+
new Object[] { "Garfield", "TheBoss", 126 });
671+
}
672+
}
673+
"""
674+
)
675+
);
676+
}
593677
}

0 commit comments

Comments
 (0)