Skip to content

Commit a426e40

Browse files
committed
RemoveTestPrefix should skip implied MethodSource
For #462
1 parent 216983b commit a426e40

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.openrewrite.Preconditions;
2020
import org.openrewrite.Recipe;
2121
import org.openrewrite.TreeVisitor;
22+
import org.openrewrite.java.AnnotationMatcher;
2223
import org.openrewrite.java.JavaIsoVisitor;
2324
import org.openrewrite.java.search.UsesType;
2425
import org.openrewrite.java.tree.J;
@@ -70,6 +71,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
7071
}
7172

7273
private static class RemoveTestPrefixVisitor extends JavaIsoVisitor<ExecutionContext> {
74+
75+
private static final AnnotationMatcher ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.params.provider.MethodSource");
76+
7377
@Override
7478
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
7579
ExecutionContext ctx) {
@@ -93,19 +97,29 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
9397
return m;
9498
}
9599

96-
// Rename method
100+
// Avoid reserved keywords
97101
String newMethodName = snakecase
98102
? Character.toLowerCase(simpleName.charAt(5)) + simpleName.substring(6)
99103
: Character.toLowerCase(simpleName.charAt(4)) + simpleName.substring(5);
100104
if (RESERVED_KEYWORDS.contains(newMethodName)) {
101105
return m;
102106
}
103107

108+
// Prevent conflicts with existing methods
104109
JavaType.Method type = m.getMethodType();
105110
if (type == null || methodExists(type, newMethodName)) {
106111
return m;
107112
}
108113

114+
// Skip implied methodSource
115+
for (J.Annotation annotation : method.getLeadingAnnotations()) {
116+
if (ANNOTATION_MATCHER.matches(annotation) &&
117+
(annotation.getArguments() == null || annotation.getArguments().isEmpty())) {
118+
return m;
119+
}
120+
}
121+
122+
// Rename method and return
109123
type = type.withName(newMethodName);
110124
return m.withName(m.getName().withSimpleName(newMethodName).withType(type))
111125
.withMethodType(type);

src/test/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefixTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class RemoveTestPrefixTest implements RewriteTest {
2929
@Override
3030
public void defaults(RecipeSpec spec) {
3131
spec
32-
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9"))
32+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(),
33+
"junit-jupiter-api-5.9", "junit-jupiter-params-5.9"))
3334
.recipe(new RemoveTestPrefix());
3435
}
3536

@@ -265,4 +266,30 @@ void myDoSomethingLogic() {}
265266
)
266267
);
267268
}
269+
270+
@Test
271+
void skipImpliedMethodSource() {
272+
//language=java
273+
rewriteRun(
274+
java(
275+
"""
276+
import org.junit.jupiter.api.Test;
277+
import org.junit.jupiter.params.provider.Arguments;
278+
import org.junit.jupiter.params.provider.MethodSource;
279+
import java.util.stream.Stream;
280+
281+
class ATest {
282+
@Test
283+
@MethodSource
284+
void testMyDoSomethingLogic(Arguments args) {
285+
}
286+
287+
static Stream<Arguments> testMyDoSomethingLogic() {
288+
return Stream.empty();
289+
}
290+
}
291+
"""
292+
)
293+
);
294+
}
268295
}

0 commit comments

Comments
 (0)