Skip to content

Fix import ambiguity regression by reordering AddImport/RemoveImport #731

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

Merged
merged 2 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -60,8 +60,8 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
return md;
}

maybeAddImport("org.mockito.Mockito", "verify");
maybeRemoveImport("org.easymock.EasyMock.verify");
maybeAddImport("org.mockito.Mockito", "verify");

int idx = 0;
for (Statement statement : md.getBody().getStatements()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
if (ASSERT_THAT_MATCHER.matches(mi)) {
Expression reasonArgument = mi.getArguments().get(0);
Expression booleanArgument = mi.getArguments().get(1);
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
maybeRemoveImport("org.hamcrest.MatcherAssert.assertThat");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
return JavaTemplate.builder("assertThat(#{any(boolean)}).as(#{any(String)}).isTrue()")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports("org.assertj.core.api.Assertions.assertThat")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ actual, assertion, getArgumentsTemplate(matcherArgumentMethod)))
"org.assertj.core.api.Assertions.assertThat",
"org.assertj.core.api.Assertions.within")
.build();
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
maybeAddImport("org.assertj.core.api.Assertions", "within");
maybeRemoveImport("org.hamcrest.Matchers." + matcher);
maybeRemoveImport("org.hamcrest.CoreMatchers." + matcher);
maybeRemoveImport("org.hamcrest.MatcherAssert");
maybeRemoveImport("org.hamcrest.MatcherAssert.assertThat");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
maybeAddImport("org.assertj.core.api.Assertions", "within");

List<Expression> templateArguments = new ArrayList<>();
templateArguments.add(actualArgument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ private J.MethodInvocation handleTwoArgumentCase(J.MethodInvocation mi, Expressi
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports("org.assertj.core.api.Assertions.assertThat")
.build();
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
maybeRemoveImport("org.hamcrest.Matchers.not");
maybeRemoveImport("org.hamcrest.Matchers." + notMatcher);
maybeRemoveImport("org.hamcrest.CoreMatchers.not");
maybeRemoveImport("org.hamcrest.CoreMatchers." + notMatcher);
maybeRemoveImport("org.hamcrest.MatcherAssert");
maybeRemoveImport("org.hamcrest.MatcherAssert.assertThat");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");

List<Expression> templateArguments = new ArrayList<>();
templateArguments.add(actualArgument);
Expand All @@ -139,11 +139,11 @@ private J.MethodInvocation handleThreeArgumentCase(J.MethodInvocation mi, Expres
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports("org.assertj.core.api.Assertions.assertThat")
.build();
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
maybeRemoveImport("org.hamcrest.Matchers.not");
maybeRemoveImport("org.hamcrest.Matchers." + notMatcher);
maybeRemoveImport("org.hamcrest.MatcherAssert");
maybeRemoveImport("org.hamcrest.MatcherAssert.assertThat");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");

List<Expression> templateArguments = new ArrayList<>();
templateArguments.add(actualArgument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ interface Dependency {
""",
"""
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.createNiceMock;
import static org.mockito.Mockito.verify;
import static org.easymock.EasyMock.createNiceMock;

public class ExampleTest {
public void testServiceMethod() {
Expand Down Expand Up @@ -126,8 +126,8 @@ interface Dependency {
""",
"""
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.createNiceMock;
import static org.mockito.Mockito.verify;
import static org.easymock.EasyMock.createNiceMock;

public class ExampleTest {
public void testServiceMethod() {
Expand Down Expand Up @@ -167,8 +167,8 @@ interface Dependency {
""",
"""
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.createNiceMock;
import static org.mockito.Mockito.verify;
import static org.easymock.EasyMock.createNiceMock;

public class ExampleTest {
public void testServiceMethod() {
Expand Down Expand Up @@ -210,8 +210,8 @@ interface Dependency {
""",
"""
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.createNiceMock;
import static org.mockito.Mockito.verify;
import static org.easymock.EasyMock.createNiceMock;

public class ExampleTest {
public void testServiceMethod() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,14 @@ void testMethod() {
"""
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasLength;

class MyTest {
@Test
void testMethod() {
assertThat("hello world")
org.assertj.core.api.Assertions.assertThat("hello world")
.satisfies(
arg -> assertThat(arg, equalTo("hello world")),
arg -> assertThat(arg, hasLength(12))
Expand Down Expand Up @@ -102,15 +101,14 @@ void testMethod() {
"""
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasLength;

class MyTest {
@Test
void testMethod() {
assertThat("hello world")
org.assertj.core.api.Assertions.assertThat("hello world")
.as("reason")
.satisfies(
arg -> assertThat(arg, equalTo("hello world")),
Expand Down Expand Up @@ -171,15 +169,14 @@ void testMethod() {
"""
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasLength;

class MyTest {
@Test
void testMethod() {
assertThat("hello world")
org.assertj.core.api.Assertions.assertThat("hello world")
.satisfiesAnyOf(
arg -> assertThat(arg, equalTo("hello world")),
arg -> assertThat(arg, hasLength(12))
Expand Down Expand Up @@ -214,15 +211,14 @@ void testMethod() {
"""
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasLength;

class MyTest {
@Test
void testMethod() {
assertThat("hello world")
org.assertj.core.api.Assertions.assertThat("hello world")
.satisfiesAnyOf(
arg -> assertThat(arg, equalTo("hello world")),
arg -> assertThat(arg, hasLength(12)),
Expand Down Expand Up @@ -285,15 +281,14 @@ void testMethod() {
"""
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasLength;

class MyTest {
@Test
void testMethod() {
assertThat("hello world")
org.assertj.core.api.Assertions.assertThat("hello world")
.as("reason")
.satisfiesAnyOf(
arg -> assertThat(arg, equalTo("hello world")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,10 @@ void bar() {
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;
import java.math.BigDecimal;

import static org.assertj.core.api.Assertions.assertThat;

class A {
void foo() {
var a = new BigDecimal("1");
Expand Down Expand Up @@ -709,10 +710,10 @@ void ba() {
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class DebugTest {
class Foo {
int i = 8;
Expand Down Expand Up @@ -753,10 +754,10 @@ void ba() {
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class DebugTest {
@Test
void ba() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public void feature1() {
import org.junit.jupiter.api.Test;

public class MyTest {
@org.junit.jupiter.api.Test
@Test
public void feature1() {
}
}
Expand Down Expand Up @@ -571,7 +571,7 @@ public void feature2() {
import org.junit.jupiter.api.Test;

public class MyTest {
@org.junit.jupiter.api.Test
@Test
public void feature1() {
}

Expand Down
Loading