Skip to content

JavaTemplateSemanticallyEqual fails to match AssertJ's assertThat("foo").isEqualTo("") #4423

@timtebeek

Description

@timtebeek

What version of OpenRewrite are you using?

https://github.com/openrewrite/rewrite/releases/tag/v8.33.4

Problem statement

This recipe replicates what's generated from our Refaster template generator:

Based on a Refaster rule defined at Picnic

We might want to quickly double check the String passed in to JavaTemplate.builder.

What is the smallest, simplest way to reproduce the problem?

import org.junit.jupiter.api.Test;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.tree.J;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.test.RewriteTest.toRecipe;

class JavaTemplateMatcherTest implements RewriteTest {

    @Test
    @Issue("https://github.com/openrewrite/rewrite-templating/pull/91")
    void shouldMatchAbstracTStringAssertIsEqualToEmptyString() {
        rewriteRun(
          spec -> spec
            .parser(JavaParser.fromJavaVersion().classpath("assertj-core"))
            .recipe(toRecipe(() -> new JavaIsoVisitor<>() {
                // Mimics what we saw in rewrite-templating
                final JavaTemplate before = JavaTemplate
                  .builder("#{stringAssert:any(org.assertj.core.api.AbstractStringAssert<?>)}.isEqualTo(\"\");")
                  .javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
                  .build();

                @Override
                public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
                    J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
                    return before.matches(getCursor()) ? SearchResult.found(mi) : mi;
                }
            })),
          //language=java
          java(
            """
              import static org.assertj.core.api.Assertions.assertThat;
              class Foo {
                  void test() {
                      assertThat("foo").isEqualTo("");
                  }
              }
              """,
            """
              import static org.assertj.core.api.Assertions.assertThat;
              class Foo {
                  void test() {
                      /*~~>*/assertThat("foo").isEqualTo("");
                  }
              }
              """
          )
        );

    }
}

What is the full stack trace of any errors you encountered?

java.lang.AssertionError: Recipe was expected to make a change but made no changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions