1616package org .openrewrite .java .testing .cleanup ;
1717
1818import java .time .Duration ;
19+ import java .util .Arrays ;
20+ import java .util .List ;
1921
2022import org .openrewrite .ExecutionContext ;
2123import org .openrewrite .Recipe ;
3234
3335public class RemoveTestPrefix extends Recipe {
3436
37+ private static final List <String > RESERVED_KEYWORDS = Arrays .asList ("abstract" , "continue" , "for" , "new" , "switch" ,
38+ "assert" , "default" , "if" , "package" , "synchronized" , "boolean" , "do" , "goto" , "private" , "this" , "break" ,
39+ "double" , "implements" , "protected" , "throw" , "byte" , "else" , "import" , "public" , "throws" , "case" , "enum" ,
40+ "instanceof" , "return" , "transient" , "catch" , "extends" , "int" , "short" , "try" , "char" , "final" ,
41+ "interface" , "static" , "void" , "class" , "finally" , "long" , "strictfp" , "volatile" , "const" , "float" ,
42+ "native" , "super" , "while" , "null" );
43+
3544 @ Override
3645 public String getDisplayName () {
3746 return "Remove `test` prefix from JUnit5 tests" ;
@@ -48,6 +57,7 @@ public String getDescription() {
4857 @ Override
4958 public J visitJavaSourceFile (JavaSourceFile cu , ExecutionContext executionContext ) {
5059 doAfterVisit (new UsesType <>("org.junit.jupiter.api.Test" ));
60+ doAfterVisit (new UsesType <>("org.junit.jupiter.api.TestTemplate" ));
5161 doAfterVisit (new UsesType <>("org.junit.jupiter.api.RepeatedTest" ));
5262 doAfterVisit (new UsesType <>("org.junit.jupiter.params.ParameterizedTest" ));
5363 doAfterVisit (new UsesType <>("org.junit.jupiter.api.TestFactory" ));
@@ -78,6 +88,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
7888 && Boolean .FALSE .equals (TypeUtils .isOverride (method .getMethodType ()))
7989 && hasJUnit5MethodAnnotation (m )) {
8090 String newMethodName = Character .toLowerCase (simpleName .charAt (4 )) + simpleName .substring (5 );
91+ if (RESERVED_KEYWORDS .contains (newMethodName )) {
92+ return m ;
93+ }
8194 JavaType .Method type = m .getMethodType ();
8295 if (type != null ) {
8396 type = type .withName (newMethodName );
@@ -92,6 +105,7 @@ && hasJUnit5MethodAnnotation(m)) {
92105 private static boolean hasJUnit5MethodAnnotation (MethodDeclaration method ) {
93106 for (J .Annotation a : method .getLeadingAnnotations ()) {
94107 if (TypeUtils .isOfClassType (a .getType (), "org.junit.jupiter.api.Test" )
108+ || TypeUtils .isOfClassType (a .getType (), "org.junit.jupiter.api.TestTemplate" )
95109 || TypeUtils .isOfClassType (a .getType (), "org.junit.jupiter.api.RepeatedTest" )
96110 || TypeUtils .isOfClassType (a .getType (), "org.junit.jupiter.params.ParameterizedTest" )
97111 || TypeUtils .isOfClassType (a .getType (), "org.junit.jupiter.api.TestFactory" )) {
0 commit comments