Skip to content

Commit dbdd9d1

Browse files
authored
Rename implicit @MethodSource methods in RemoveTestPrefix (#961)
Fixes #462
1 parent 168f433 commit dbdd9d1

2 files changed

Lines changed: 109 additions & 3 deletions

File tree

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.openrewrite.java.search.UsesType;
2929
import org.openrewrite.java.tree.J;
3030
import org.openrewrite.java.tree.J.MethodDeclaration;
31+
import org.openrewrite.java.tree.Flag;
3132
import org.openrewrite.java.tree.JavaType;
3233
import org.openrewrite.java.tree.TypeUtils;
3334

@@ -125,11 +126,33 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
125126
return m;
126127
}
127128

128-
// Skip implied methodSource
129+
// Handle implied methodSource: rename both test method and source method
130+
boolean hasImpliedMethodSource = false;
129131
for (J.Annotation annotation : method.getLeadingAnnotations()) {
130132
if (ANNOTATION_MATCHER.matches(annotation) &&
131133
(annotation.getArguments() == null || annotation.getArguments().isEmpty())) {
132-
return m;
134+
hasImpliedMethodSource = true;
135+
break;
136+
}
137+
}
138+
if (hasImpliedMethodSource) {
139+
JavaType.FullyQualified declaringType = type.getDeclaringType();
140+
if (declaringType != null) {
141+
for (JavaType.Method declaredMethod : declaringType.getMethods()) {
142+
if (declaredMethod.getName().equals(simpleName) &&
143+
declaredMethod.hasFlags(Flag.Static)) {
144+
// Check for conflict with source method's new name
145+
if (TypeUtils.findDeclaredMethod(declaringType, newMethodName,
146+
declaredMethod.getParameterTypes()).isPresent()) {
147+
return m;
148+
}
149+
// Rename the source method too
150+
doAfterVisit(new ChangeMethodName(
151+
MethodMatcher.methodPattern(declaredMethod),
152+
newMethodName, false, false).getVisitor());
153+
break;
154+
}
155+
}
133156
}
134157
}
135158

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

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,9 @@ void myDoSomethingLogic() {}
276276
);
277277
}
278278

279+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/462")
279280
@Test
280-
void skipImpliedMethodSource() {
281+
void renameImpliedMethodSource() {
281282
//language=java
282283
rewriteRun(
283284
java(
@@ -297,6 +298,88 @@ static Stream<Arguments> testMyDoSomethingLogic() {
297298
return Stream.empty();
298299
}
299300
}
301+
""",
302+
"""
303+
import org.junit.jupiter.api.Test;
304+
import org.junit.jupiter.params.provider.Arguments;
305+
import org.junit.jupiter.params.provider.MethodSource;
306+
import java.util.stream.Stream;
307+
308+
class ATest {
309+
@Test
310+
@MethodSource
311+
void myDoSomethingLogic(Arguments args) {
312+
}
313+
314+
static Stream<Arguments> myDoSomethingLogic() {
315+
return Stream.empty();
316+
}
317+
}
318+
"""
319+
)
320+
);
321+
}
322+
323+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/462")
324+
@Test
325+
void renameImpliedMethodSourceWithoutSourceMethod() {
326+
//language=java
327+
rewriteRun(
328+
java(
329+
"""
330+
import org.junit.jupiter.api.Test;
331+
import org.junit.jupiter.params.provider.Arguments;
332+
import org.junit.jupiter.params.provider.MethodSource;
333+
334+
class ATest {
335+
@Test
336+
@MethodSource
337+
void testMyDoSomethingLogic(Arguments args) {
338+
}
339+
}
340+
""",
341+
"""
342+
import org.junit.jupiter.api.Test;
343+
import org.junit.jupiter.params.provider.Arguments;
344+
import org.junit.jupiter.params.provider.MethodSource;
345+
346+
class ATest {
347+
@Test
348+
@MethodSource
349+
void myDoSomethingLogic(Arguments args) {
350+
}
351+
}
352+
"""
353+
)
354+
);
355+
}
356+
357+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/462")
358+
@Test
359+
void skipImpliedMethodSourceWhenSourceMethodConflicts() {
360+
//language=java
361+
rewriteRun(
362+
java(
363+
"""
364+
import org.junit.jupiter.api.Test;
365+
import org.junit.jupiter.params.provider.Arguments;
366+
import org.junit.jupiter.params.provider.MethodSource;
367+
import java.util.stream.Stream;
368+
369+
class ATest {
370+
@Test
371+
@MethodSource
372+
void testMyDoSomethingLogic(Arguments args) {
373+
}
374+
375+
static Stream<Arguments> testMyDoSomethingLogic() {
376+
return Stream.empty();
377+
}
378+
379+
static Stream<Arguments> myDoSomethingLogic() {
380+
return Stream.empty();
381+
}
382+
}
300383
"""
301384
)
302385
);

0 commit comments

Comments
 (0)