From 25c9dda30ff686dff6348c440979b741f601a655 Mon Sep 17 00:00:00 2001 From: Lukas Poos Date: Sun, 11 Jun 2023 11:20:37 +0200 Subject: [PATCH] #3156: Organize test cases --- .../java/LombokUtilityClassTest.java | 457 +++++++++--------- 1 file changed, 227 insertions(+), 230 deletions(-) diff --git a/rewrite-java/src/test/java/org/openrewrite/java/LombokUtilityClassTest.java b/rewrite-java/src/test/java/org/openrewrite/java/LombokUtilityClassTest.java index 95922296fc9..091f8562530 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/LombokUtilityClassTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/LombokUtilityClassTest.java @@ -1,137 +1,245 @@ package org.openrewrite.java; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.java; +/** + * Cases to test: + * - Interfaces + * - Empty classes & interfaces + * - inheritance + * - abstract classes + * - instantiations of changed classes + * - constructor + */ class LombokUtilityClassTest implements RewriteTest { - @Test - void happyPathSimpleMethod() { - rewriteRun( - recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), - java( - """ - public class A { - public static int add(final int x, final int y) { - return x + y; - } - } - """, - """ - import lombok.experimental.UtilityClass; - - @UtilityClass - public class A { - public int add(final int x, final int y) { - return x + y; - } - } - """ - ) - ); - } + @Nested + class ShouldApplyLombokUtility implements RewriteTest { - @Test - void happyPathSimpleField() { - rewriteRun( - recipeSpec -> recipeSpec - .recipe(new LombokUtilityClass() - ), - java( - """ - public class A { - public static final int C = 0; - } - """, - """ - import lombok.experimental.UtilityClass; - - @UtilityClass - public class A { - public final int c = 0; - } - """ - ) - ); - } + @Test + void givenOneStaticMethod() { + rewriteRun( + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), + java( + """ + public class A { + public static int add(final int x, final int y) { + return x + y; + } + } + """, + """ + import lombok.experimental.UtilityClass; + + @UtilityClass + public class A { + public int add(final int x, final int y) { + return x + y; + } + } + """ + ) + ); + } - @Test - void doNotChangeFieldIfNotFinal() { - rewriteRun( - recipeSpec -> recipeSpec - .recipe(new LombokUtilityClass() - ), - java( - """ - public class A { - public static int C = 0; - } - """ - ) - ); - } + @Test + void givenOneStaticFinalMember() { + rewriteRun( + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), + java( + """ + public class A { + public static final int C = 0; + } + """, + """ + import lombok.experimental.UtilityClass; + + @UtilityClass + public class A { + public final int c = 0; + } + """ + ) + ); + } - @Test - void happyPathMultiVariableField() { - rewriteRun( - recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), - java( - """ - public class A { - public static final int A, B, C = 0; - } - """, - """ - import lombok.experimental.UtilityClass; - - @UtilityClass - public class A { - public final int a, b, c = 0; - } - """ - ) - ); - } + @Test + void givenMultipleStaticFinalMembers() { + rewriteRun( + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), + java( + """ + public class A { + public static final int A, B, C = 0; + } + """, + """ + import lombok.experimental.UtilityClass; + + @UtilityClass + public class A { + public final int a, b, c = 0; + } + """ + ) + ); + } + @Test + void givenStaticInnerClassWithOneStaticMethod() { + rewriteRun( + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), + java( + """ + public class A { + public int add(final int x, final int y) { + return x + y; + } + + private static class B { + private static int substract(final int x, final int y) { + return x - y; + } + } + } + """, + """ + import lombok.experimental.UtilityClass; + + public class A { + public int add(final int x, final int y) { + return x + y; + } + + @UtilityClass + private static class B { + private int substract(final int x, final int y) { + return x - y; + } + } + } + """ + ) + ); + } - @Test - void doNotUpgradeToUtilityClassIfNonStaticVariables() { - rewriteRun( - recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), - java( - """ - public class A { - private final int x = 0; - public static int add(final int x, final int y) { - return x + y; - } - } - """ - ) - ); + @Test + void givenInnerClassWithOneStaticMethod() { + rewriteRun( + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), + java( + """ + public class A { + public int add(final int x, final int y) { + return x + y; + } + + private class B { + private static int substract(final int x, final int y) { + return x - y; + } + } + } + """, + """ + import lombok.experimental.UtilityClass; + + public class A { + public int add(final int x, final int y) { + return x + y; + } + + @UtilityClass + private class B { + private int substract(final int x, final int y) { + return x - y; + } + } + } + """ + ) + ); + } + + @Test + void givenNotPublicClassWithOneStaticMethod() { + rewriteRun( + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), + java( + """ + public class A { + public int add(final int x, final int y) { + return x + y; + } + } + class B { + public static int substract(final int x, final int y) { + return x - y; + } + } + """, + """ + import lombok.experimental.UtilityClass; + + public class A { + public int add(final int x, final int y) { + return x + y; + } + } + + @UtilityClass + class B { + public int substract(final int x, final int y) { + return x - y; + } + } + """ + ) + ); + } } + @Nested + class ShouldNotApplyLombokUtility implements RewriteTest { + @Test + void givenStaticMemberIsNotFinal() { + rewriteRun( + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), + java( + """ + public class A { + public static int C = 0; + } + """ + ) + ); + } + + @Test + void givenMethodIsNotStatic() { + rewriteRun( + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), + java( + """ + public class A { + public int add(final int x, final int y) { + return x + y; + } + } + """ + ) + ); + } - @Test - void doNotUpgradeToUtilityClassIfNonStaticMethods() { - rewriteRun( - recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), - java( - """ - public class A { - public int add(final int x, final int y) { - return x + y; - } - } - """ - ) - ); } @Test - void onlyUpgradeRelevantToUtilityClass() { + void shouldNotChangeClassWhenStaticMethodOfChangedClassIsCalled() { rewriteRun( recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), java( @@ -156,7 +264,7 @@ public int add(final int x, final int y) { java(""" public class B { public int add(final int x, final int y) { - return x + y; + return A.add(x, y); } } """ @@ -164,8 +272,9 @@ public int add(final int x, final int y) { ); } + @Test - void doNotChangeReferenced() { + void shoulOnlyUpgradeRelevantToUtilityClass() { rewriteRun( recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), java( @@ -190,7 +299,7 @@ public int add(final int x, final int y) { java(""" public class B { public int add(final int x, final int y) { - return A.add(x, y); + return x + y; } } """ @@ -198,119 +307,7 @@ public int add(final int x, final int y) { ); } - @Test - void happyPathInner() { - rewriteRun( - recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), - java( - """ - public class A { - public int add(final int x, final int y) { - return x + y; - } - - private class B { - private static int substract(final int x, final int y) { - return x - y; - } - } - } - """, - """ - import lombok.experimental.UtilityClass; - - public class A { - public int add(final int x, final int y) { - return x + y; - } - - @UtilityClass - private class B { - private int substract(final int x, final int y) { - return x - y; - } - } - } - """ - ) - ); - } - /** - * Nested ~ inner static - */ - @Test - void happyPathNested() { - rewriteRun( - recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), - java( - """ - public class A { - public int add(final int x, final int y) { - return x + y; - } - - private static class B { - private static int substract(final int x, final int y) { - return x - y; - } - } - } - """, - """ - import lombok.experimental.UtilityClass; - - public class A { - public int add(final int x, final int y) { - return x + y; - } - - @UtilityClass - private static class B { - private int substract(final int x, final int y) { - return x - y; - } - } - } - """ - ) - ); - } - @Test - void happyPathNonPublic() { - rewriteRun( - recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), - java( - """ - public class A { - public int add(final int x, final int y) { - return x + y; - } - } - class B { - public static int substract(final int x, final int y) { - return x - y; - } - } - """, - """ - import lombok.experimental.UtilityClass; - - public class A { - public int add(final int x, final int y) { - return x + y; - } - } - - @UtilityClass - class B { - public int substract(final int x, final int y) { - return x - y; - } - } - """ - ) - ); - } + } \ No newline at end of file