Skip to content
Merged
Show file tree
Hide file tree
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 @@ -146,7 +146,6 @@ private boolean isZero(Expression isEqualToArg) {
private J.MethodInvocation applyTemplate(ExecutionContext ctx, J.MethodInvocation m, String template, String assertions, Object... parameters) {
maybeAddImport(assertions, "assertThat");
J.MethodInvocation invocation = JavaTemplate.builder(template)
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports(assertions + ".assertThat")
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
TypedTree clazz = (TypedTree) instanceOf.getClazz();
TypedTree rawClazz = clazz instanceof J.ParameterizedType ? ((J.ParameterizedType) clazz).getClazz() : clazz;
return JavaTemplate.builder("#{any()}." + dedicatedAssertion + "(#{any(java.lang.Class)})")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.build()
.apply(getCursor(), mi.getCoordinates().replace(), newAssertThat, toClassLiteral(rawClazz));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,11 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
JavaTemplate t;
if (mi.getSelect() == null) {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.staticImports("org.junit.jupiter.api.Assertions.assertEquals")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
.build();
} else {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.imports("org.junit.jupiter.api.Assertions")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
.build();
Expand All @@ -88,13 +86,11 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
JavaTemplate t;
if (mi.getSelect() == null) {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.staticImports("org.junit.jupiter.api.Assertions.assertEquals")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
.build();
} else {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.imports("org.junit.jupiter.api.Assertions")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,11 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
JavaTemplate t;
if (mi.getSelect() == null) {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.staticImports("org.junit.jupiter.api.Assertions.assertNotEquals")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
.build();
} else {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.imports("org.junit.jupiter.api.Assertions")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
JavaTemplate t;
if (mi.getSelect() == null) {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.staticImports("org.junit.jupiter.api.Assertions.assertTrue")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
.build();
} else {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.imports("org.junit.jupiter.api.Assertions")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,12 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
JavaTemplate t;
if (mi.getSelect() == null) {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.staticImports("org.junit.jupiter.api.Assertions.assertNotNull")
.javaParser(JavaParser.fromJavaVersion()
.classpathFromResources(ctx, "junit-jupiter-api-5"))
.build();
} else {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.imports("org.junit.jupiter.api.Assertions")
.javaParser(JavaParser.fromJavaVersion()
.classpathFromResources(ctx, "junit-jupiter-api-5"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
JavaTemplate t;
if (mi.getSelect() == null) {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.staticImports("org.junit.jupiter.api.Assertions.assertEquals")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
.build();
} else {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.imports("org.junit.jupiter.api.Assertions")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
new Object[]{actualArgument, expectedArgument};

return JavaTemplate.builder(template)
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports("org.assertj.core.api.Assertions.assertThat")
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ private J.MethodInvocation replace(J.MethodInvocation mi, ExecutionContext ctx)
(reasonArgument != null ? ".as(#{any(String)})" : "") +
".%s(%s)",
actual, assertion, argsTemplate))
.contextSensitive()
.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 @@ -103,7 +103,6 @@ private J.MethodInvocation handleTwoArgumentCase(J.MethodInvocation mi, Expressi
.collect(joining(", "));
JavaTemplate template = JavaTemplate.builder(String.format("assertThat(%s).%s(%s)",
actual, assertion, argumentsTemplate))
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports("org.assertj.core.api.Assertions.assertThat")
.build();
Expand Down Expand Up @@ -136,7 +135,6 @@ private J.MethodInvocation handleThreeArgumentCase(J.MethodInvocation mi, Expres
.collect(joining(", "));
JavaTemplate template = JavaTemplate.builder(String.format("assertThat(%s).as(#{any(String)}).%s(%s)",
actual, assertion, argumentsTemplate))
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports("org.assertj.core.api.Assertions.assertThat")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ private J.MethodInvocation replaceWithAssertDoesNotThrowWithoutStringExpression(
maybeAddImport("org.junit.jupiter.api.Assertions");
maybeRemoveCatchTypes(try_);
return JavaTemplate.builder("Assertions.assertDoesNotThrow(() -> #{any()});")
.contextSensitive()
.imports("org.junit.jupiter.api.Assertions")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
for (Statement statement : cd.getBody().getStatements()) {
if (statement == afterEachMethod) {
cd = JavaTemplate.builder("#{any(okhttp3.mockwebserver.MockWebServer)}.close();")
.contextSensitive()
.imports(AFTER_EACH_FQN, MOCK_WEB_SERVER_FQN, IO_EXCEPTION_FQN)
.javaParser(JavaParser.fromJavaVersion()
.classpathFromResources(ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ private <T extends J> T disable(T target, Framework framework, ExecutionContext
maybeAddImport(framework.disabledAnnotation);
return JavaTemplate.builder("@" + simpleName(framework.disabledAnnotation) +
"(\"PowerMock test disabled by migration: rework it not to rely on private members\")")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, framework.classpathResource))
.imports(framework.disabledAnnotation)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ public J visitBlock(J.Block block, ExecutionContext ctx) {

// Create try-with-resources
J.Try tryWithResources = JavaTemplate.builder("try (#{any()}) {}")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-5"))
.build()
.apply(new Cursor(getCursor(), varDecl), varDecl.getCoordinates().replace(), varDecl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
J.VariableDeclarations withoutInitializer = multiVariable.withVariables(
ListUtils.map(multiVariable.getVariables(), v -> v.withInitializer(null)));
J.VariableDeclarations annotated = JavaTemplate.builder("@InjectMocks")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-3.12"))
.imports(INJECT_MOCKS)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ private static J.ClassDeclaration appendToMethodAnnotatedWith(JavaVisitor<Execut
J.MethodDeclaration m = (J.MethodDeclaration) statements.get(i);
if (m.getBody() != null && m.getLeadingAnnotations().stream().anyMatch(matcher::matches)) {
m = JavaTemplate.builder(code)
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-3.12"))
.build()
.apply(new Cursor(bodyCursor, m), m.getBody().getCoordinates().lastStatement(), parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,6 @@ private J.MethodDeclaration wrapBodyInTryCatch(J.MethodDeclaration md, Execution
}
J.MethodDeclaration templated = JavaTemplate
.builder("try {\n} catch (ReflectiveOperationException e) {\n throw new RuntimeException(e);\n}")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion())
.build()
.apply(updateCursor(md), md.getCoordinates().replaceBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,35 @@ void testMethod(List<String> list) {
)
);
}

@Test
void arrayOfClassDeclaredInTestFile() {
rewriteRun(
//language=java
java(
"""
import static org.assertj.core.api.Assertions.assertThat;

class MyTest {
static class Item {}

void testMethod(Item[] items) {
assertThat(items.length).isEqualTo(2);
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;

class MyTest {
static class Item {}

void testMethod(Item[] items) {
assertThat(items).hasSize(2);
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -871,4 +871,40 @@ void testMethod(List<String> list) {
)
);
}

@Test
void collectionOfClassDeclaredInTestFile() {
rewriteRun(
spec -> spec.recipe(new SimplifyChainedAssertJAssertion("isEmpty", "isTrue", "isEmpty", "java.util.Collection")),
//language=java
java(
"""
import java.util.List;

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

class MyTest {
static class Item {}

void testMethod(List<Item> items) {
assertThat(items.isEmpty()).isTrue();
}
}
""",
"""
import java.util.List;

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

class MyTest {
static class Item {}

void testMethod(List<Item> items) {
assertThat(items).isEmpty();
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,39 @@ void testMethod(List<String> list) {
)
);
}

@Test
void collectionOfClassDeclaredInTestFile() {
rewriteRun(
//language=java
java(
"""
import java.util.List;

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

class MyTest {
static class Item {}

void testMethod(List<Item> items) {
assertThat(2).isEqualTo(items.size());
}
}
""",
"""
import java.util.List;

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

class MyTest {
static class Item {}

void testMethod(List<Item> items) {
assertThat(items).hasSize(2);
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,41 @@ void testMethod() {
)
);
}

@Test
void collectionOfClassDeclaredInTestFile() {
rewriteRun(
//language=java
java(
"""
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.instanceOf;

class MyTest {
static class Item {}

void testMethod(List<Item> items) {
assertThat(items, everyItem(instanceOf(Item.class)));
}
}
""",
"""
import java.util.List;

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

class MyTest {
static class Item {}

void testMethod(List<Item> items) {
assertThat(items).hasOnlyElementsOfType(Item.class);
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,41 @@ void testMethod() {
)
);
}

@Test
void collectionOfClassDeclaredInTestFile() {
rewriteRun(
//language=java
java(
"""
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf;

class MyTest {
static class Item {}

void testMethod(List<Item> items) {
assertThat(items, hasItem(instanceOf(Item.class)));
}
}
""",
"""
import java.util.List;

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

class MyTest {
static class Item {}

void testMethod(List<Item> items) {
assertThat(items).hasAtLeastOneElementOfType(Item.class);
}
}
"""
)
);
}
}
Loading
Loading