Skip to content

Commit

Permalink
Fix ReplaceAnnotationTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Jun 2, 2024
1 parent 9472d87 commit c2e1279
Showing 1 changed file with 78 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,81 +18,88 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class ReplaceAnnotationTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion().classpath("lombok", "annotations"));
}

@Nested
class OnMatch {
@Test
void matchNoPrams() {
rewriteRun(
spec -> spec.recipe(new ReplaceAnnotation("@org.jetbrains.annotations.NotNull", "@lombok.NonNull", null)),
java("""
import org.jetbrains.annotations.NotNull;
class A {
@NotNull
String testMethod() {}
}
""", """
import lombok.NonNull;
class A {
@NonNull
String testMethod() {}
}
"""));
java(
"""
import org.jetbrains.annotations.NotNull;
class A {
@NotNull
String testMethod() {}
}
""",
"""
import lombok.NonNull;
class A {
@NonNull
String testMethod() {}
}
"""
)
);
}

@Test
@DocumentExample
void matchWithPrams() {
rewriteRun(
spec -> spec.recipe(new ReplaceAnnotation("@org.jetbrains.annotations.NotNull(\"Test\")", "@lombok.NonNull", null)),
java("""
import org.jetbrains.annotations.NotNull;
class A {
@NotNull("Test")
String testMethod() {}
}
""", """
import lombok.NonNull;
class A {
@NonNull
String testMethod() {}
}
"""));
java(
"""
import org.jetbrains.annotations.NotNull;
class A {
@NotNull("Test")
String testMethod() {}
}
""",
"""
import lombok.NonNull;
class A {
@NonNull
String testMethod() {}
}
"""
)
);
}

@Test
void insertWithParams() {
rewriteRun(
spec -> spec.recipe(new ReplaceAnnotation("@lombok.NonNull", "@org.jetbrains.annotations.NotNull(\"Test\")", null)),
java("""
import lombok.NonNull;
class A {
@NonNull
String testMethod() {}
}
""", """
import org.jetbrains.annotations.NotNull;
class A {
@NotNull("Test")
String testMethod() {}
}
"""));
java(
"""
import lombok.NonNull;
class A {
@NonNull
String testMethod() {}
}
""",
"""
import org.jetbrains.annotations.NotNull;
class A {
@NotNull("Test")
String testMethod() {}
}
"""
)
);
}
}

Expand All @@ -102,28 +109,33 @@ class NoMatch {
void noMatchOtherType() {
rewriteRun(
spec -> spec.recipe(new ReplaceAnnotation("@org.jetbrains.annotations.NotNull", "@lombok.NonNull", null)),
java("""
import org.jetbrains.annotations.Nullable;
class A {
@Nullable("Test")
String testMethod() {}
}
"""));
java(
"""
import org.jetbrains.annotations.Nullable;
class A {
@Nullable("Test")
String testMethod() {}
}
"""
)
);
}

@Test
void noMatchParameter() {
rewriteRun(
spec -> spec.recipe(new ReplaceAnnotation("@org.jetbrains.annotations.NotNull(\"Test\")", "@lombok.NonNull", null)),
java("""
import org.jetbrains.annotations.Nullable;
class A {
@Nullable("Other")
String testMethod() {}
}
""")
java(
"""
import org.jetbrains.annotations.Nullable;
class A {
@Nullable("Other")
String testMethod() {}
}
"""
)
);
}
}
Expand Down

0 comments on commit c2e1279

Please sign in to comment.