Skip to content

Commit

Permalink
Refactor ChangeType tests to adhere to testing framework (#4673)
Browse files Browse the repository at this point in the history
* Polish tests

* Polish

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
Laurens-W and timtebeek authored Nov 14, 2024
1 parent a59638e commit e591869
Showing 1 changed file with 32 additions and 56 deletions.
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());
}))
);
}
}

0 comments on commit e591869

Please sign in to comment.