Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Bit-Flipper
Last active March 2, 2024 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bit-Flipper/3c610973ce80666db062ed157a115437 to your computer and use it in GitHub Desktop.
Save Bit-Flipper/3c610973ce80666db062ed157a115437 to your computer and use it in GitHub Desktop.
package dev.bitflippers;
import org.junit.jupiter.api.Test;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import static org.openrewrite.java.Assertions.java;
public class TestDuplicateClass implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion().classpath("lombok"));
}
@Test
public void classIsDuplicatedWithNewPrefix() {
rewriteRun(
spec -> spec.recipe(new DuplicateClass("dev.bitflippers.example", "TestClass")),
java(
//language=java
"""
package dev.bitflippers.example;
import lombok.Getter;
public class TestClass {
@Getter
public String foo = "foo";
@Getter
public int a = 10;
private TestClass() {}
}
""",
spec -> spec.path("foo/bar/src/main/dev/bitflippers/example/TestClass.java")
),
java(null,
//language=java
"""
package dev.bitflippers.example;
import lombok.Getter;
public class CopyOfTestClass {
@Getter
public String foo = "foo";
@Getter
public int a = 10;
private CopyOfTestClass() {}
}
""",
spec -> spec.path("foo/bar/src/main/dev/bitflippers/example/CopyOfTestClass.java")
)
);
}
@Test
public void classNotDuplicatedIfDoesNotMatch() {
rewriteRun(
spec -> spec.recipe(new DuplicateClass("dev.bitflippers.example", "SomeOtherClass")),
java(
//language=java
"""
package dev.bitflippers.example;
import lombok.Getter;
public class TestClass {
@Getter
public String foo = "foo";
@Getter
public int a = 10;
}
""",
spec -> spec.path("foo/bar/src/main/dev/bitflippers/example/TestClass.java")
)
);
}
@Test
public void classIsDuplicatedNotDuplicatedIfClassWithSameNameAlreadyExists() {
rewriteRun(
spec -> spec.recipe(new DuplicateClass("dev.bitflippers.example", "TestClass")),
java(
//language=java
"""
package dev.bitflippers.example;
import lombok.Getter;
public class TestClass {
@Getter
public String foo = "foo";
@Getter
public int a = 10;
}
""",
spec -> spec.path("foo/bar/src/main/dev/bitflippers/example/TestClass.java")
),
java(
//language=java
"""
package dev.bitflippers.example;
import lombok.Getter;
public class CopyOfTestClass {
@Getter
public String foo = "foo";
@Getter
public int a = 10;
}
""",
spec -> spec.path("foo/bar/src/main/dev/bitflippers/example/CopyOfTestClass.java")
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment