Skip to content

Commit 898f863

Browse files
committed
Reduce excessive reformatting of source files in various junit recipes
1 parent 594a3df commit 898f863

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublic.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public ClassDeclaration visitClassDeclaration(ClassDeclaration classDecl, Execut
128128
if (!modifierComments.isEmpty()) {
129129
c = c.withComments(ListUtils.concatAll(c.getComments(), modifierComments));
130130
}
131-
c = maybeAutoFormat(c, c.withModifiers(modifiers), executionContext, getCursor().dropParentUntil(J.class::isInstance));
131+
c = maybeAutoFormat(c, c.withModifiers(modifiers), c.getName(), executionContext, getCursor().getParentTreeCursor());
132132
}
133133
}
134134
return c;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
7373
maybeRemoveImport("org.junit.experimental.categories.Category");
7474
maybeAddImport(tagType);
7575
}
76-
cd = maybeAutoFormat(classDecl, cd, ctx);
76+
cd = maybeAutoFormat(classDecl, cd, cd.getName(), ctx, getCursor().getParentTreeCursor());
7777
return cd;
7878
}
7979

@@ -89,7 +89,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
8989
maybeRemoveImport("org.junit.experimental.categories.Category");
9090
maybeAddImport(tagType);
9191
}
92-
m = maybeAutoFormat(method, m, ctx);
92+
m = maybeAutoFormat(method, m, m.getName(), ctx, getCursor().getParentTreeCursor());
9393
return m;
9494
}
9595

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
271271
if (initMethods.contains(m.getSimpleName()) || initMethodReferences.containsValue(m.getSimpleName())) {
272272
if (m.getModifiers().stream().noneMatch(it -> J.Modifier.Type.Static.equals(it.getType()))) {
273273
J.Modifier staticModifier = new J.Modifier(UUID.randomUUID(), Space.format(" "), Markers.EMPTY, J.Modifier.Type.Static, new ArrayList<>());
274-
m = maybeAutoFormat(m, m.withModifiers(ListUtils.concat(m.getModifiers(), staticModifier)), executionContext, getCursor().dropParentUntil(J.class::isInstance));
274+
m = maybeAutoFormat(m, m.withModifiers(ListUtils.concat(m.getModifiers(), staticModifier)), executionContext, getCursor().getParentTreeCursor());
275275
}
276276
}
277277
return m;

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,14 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
275275
if (initMethodDeclarationTemplate != null) {
276276
cd = cd.withBody(cd.getBody().withTemplate(initMethodDeclarationTemplate,
277277
cd.getBody().getCoordinates().lastStatement()));
278+
J.Block finalBody = cd.getBody();
278279
cd = cd.withBody(cd.getBody().withStatements(ListUtils.map(cd.getBody().getStatements(), stmt -> {
279280
if (stmt instanceof J.MethodDeclaration) {
280281
J.MethodDeclaration md = (J.MethodDeclaration) stmt;
281282
if (md.getName().getSimpleName().equals(initMethodName)) {
282-
return md.withParameters(parameterizedTestMethodParameters);
283+
J.Block body = md.getBody(); // Preserve body formatting
284+
return autoFormat(md.withParameters(parameterizedTestMethodParameters).withBody(null),
285+
executionContext, new Cursor(getCursor(), finalBody)).withBody(body);
283286
}
284287
}
285288
return stmt;
@@ -289,18 +292,19 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
289292
// if a constructor was converted to an init method then remove final modifiers from any associated field variables.
290293
final Set<String> fieldNames = getCursor().pollMessage("INIT_VARS");
291294
if (fieldNames != null && !fieldNames.isEmpty()) {
295+
J.Block finalBody = cd.getBody();
292296
cd = cd.withBody(cd.getBody().withStatements(ListUtils.map(cd.getBody().getStatements(), statement -> {
293297
if (statement instanceof J.VariableDeclarations) {
294298
J.VariableDeclarations varDecls = (J.VariableDeclarations) statement;
295299
if (varDecls.getVariables().stream().anyMatch(it -> fieldNames.contains(it.getSimpleName()))
296300
&& (varDecls.hasModifier(J.Modifier.Type.Final))) {
297-
statement = varDecls.withModifiers(ListUtils.map(varDecls.getModifiers(), mod -> mod.getType() == J.Modifier.Type.Final ? null : mod));
301+
varDecls = varDecls.withModifiers(ListUtils.map(varDecls.getModifiers(), mod -> mod.getType() == J.Modifier.Type.Final ? null : mod));
302+
statement = maybeAutoFormat(statement, varDecls, executionContext, new Cursor(getCursor(), finalBody));
298303
}
299304
}
300305
return statement;
301306
})));
302307
}
303-
cd = maybeAutoFormat(classDecl, cd, executionContext);
304308
return cd;
305309
}
306310

@@ -356,14 +360,14 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
356360
assert m.getBody() != null;
357361
JavaCoordinates newStatementCoordinates = !m.getBody().getStatements().isEmpty() ? m.getBody().getStatements().get(0).getCoordinates().before() : m.getBody().getCoordinates().lastStatement();
358362
m = m.withTemplate(initMethodStatementTemplate, newStatementCoordinates, initStatementParamString);
359-
m = maybeAutoFormat(m, m.withParameters(parameterizedTestMethodParameters), executionContext, getCursor().dropParentUntil(J.class::isInstance));
363+
m = maybeAutoFormat(m, m.withParameters(parameterizedTestMethodParameters), m.getName(), executionContext, getCursor().getParentTreeCursor());
360364
}
361365

362366
// Change constructor to test init method
363367
if (initMethodDeclarationTemplate == null && m.isConstructor()) {
364368
m = m.withName(m.getName().withSimpleName(initMethodName));
365369
m = maybeAutoFormat(m, m.withReturnTypeExpression(new J.Primitive(randomId(), Space.EMPTY, Markers.EMPTY, JavaType.Primitive.Void)),
366-
executionContext, getCursor().dropParentUntil(J.class::isInstance));
370+
m.getName(), executionContext, getCursor().getParentTreeCursor());
367371

368372
// converting a constructor to a void init method may require removing final modifiers from field vars.
369373
if (m.getBody() != null) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ public void testSerialization(String firstName, String lastName, Integer id) {
131131
132132
public static List<Object[]> parameters() {
133133
return Arrays.asList(
134-
new Object[]{"Otis", "TheDog", 124},
135-
new Object[]{"Garfield", "TheBoss", 126});
134+
new Object[] { "Otis", "TheDog", 124 },
135+
new Object[] { "Garfield", "TheBoss", 126 });
136136
}
137137
}
138138
"""
@@ -215,8 +215,8 @@ public void testSerialization(String firstName, String lastName, Integer id) {
215215
216216
public static List<Object[]> parameters() {
217217
return Arrays.asList(
218-
new Object[]{"Otis", "TheDog", 124},
219-
new Object[]{"Garfield", "TheBoss", 126});
218+
new Object[] { "Otis", "TheDog", 124 },
219+
new Object[] { "Garfield", "TheBoss", 126 });
220220
}
221221
}
222222
"""

0 commit comments

Comments
 (0)