Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ChangeType tests to adhere to testing framework #4673

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.PathUtils;
import org.openrewrite.*;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
Expand All @@ -31,7 +28,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.xml.Assertions.xml;

Expand Down Expand Up @@ -1986,35 +1982,35 @@ void shouldNotFullyQualifyWhenNewTypeIsAlreadyUsed() {
true)),
java(
"""
package org.a;

public class A {
public static String A = "A";
}
"""),
package org.a;
public class A {
public static String A = "A";
}
"""),
java(
"""
package org.ab;

public class AB {
public static String A = "A";
public static String B = "B";
}
"""),
package org.ab;
public class AB {
public static String A = "A";
public static String B = "B";
}
"""),
// language=java
java(
"""
import org.a.A;
import org.ab.AB;

class Letters {
String a = A.A;
String b = AB.B;
}
""",
"""
import org.ab.AB;

class Letters {
String a = AB.A;
String b = AB.B;
Expand Down Expand Up @@ -2047,43 +2043,23 @@ void changeTypeInSpringXml() {
}

@Test
void compilationUnitTest() {
@Language("java")
String helloClass = """
package hello;
public class HelloClass {}
""";

J.CompilationUnit compilationUnit = (J.CompilationUnit) JavaParser
.fromJavaVersion()
.build()
.parse(helloClass)
.findFirst()
.get();

// Check that ChangeType changes the class name for a J.CompilationUnit
compilationUnit = (J.CompilationUnit) new ChangeType("hello.HelloClass", "hello.GoodbyeClass", false).getVisitor().visit(compilationUnit, new InMemoryExecutionContext());
assertEquals("GoodbyeClass", compilationUnit.getClasses().get(0).getSimpleName());
}

@Test
void classDeclarationTest() {
@Language("java")
String helloClass = """
package hello;
public class HelloClass {}
""";
void changeTypeWorksOnDirectInvocations() {
rewriteRun(
spec -> spec.recipe(Recipe.noop()), // do not run the default recipe
java(
"""
package hello;
public class HelloClass {}
""",
spec -> spec.beforeRecipe((source) -> {
TreeVisitor<?, ExecutionContext> visitor = new ChangeType("hello.HelloClass", "hello.GoodbyeClass", false).getVisitor();

J.CompilationUnit compilationUnit = (J.CompilationUnit) JavaParser
.fromJavaVersion()
.build()
.parse(helloClass)
.findFirst()
.get();
J.ClassDeclaration classDeclaration = compilationUnit.getClasses().get(0);
J.CompilationUnit cu = (J.CompilationUnit) visitor.visit(source, new InMemoryExecutionContext());
assertEquals("GoodbyeClass", cu.getClasses().get(0).getSimpleName());

// Check that ChangeType changes the class name for a J.ClassDeclaration
classDeclaration = (J.ClassDeclaration) new ChangeType("hello.HelloClass", "hello.GoodbyeClass", false).getVisitor().visit(classDeclaration, new InMemoryExecutionContext());
assertEquals("GoodbyeClass", classDeclaration.getSimpleName());
J.ClassDeclaration cd = (J.ClassDeclaration) visitor.visit(source.getClasses().get(0), new InMemoryExecutionContext());
assertEquals("GoodbyeClass", cd.getSimpleName());
}))
);
}
}