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
@@ -0,0 +1,89 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.testing.assertj;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;

public class SimplifySequencedCollectionAssertions extends Recipe {

private static final MethodMatcher ASSERT_THAT_MATCHER = new MethodMatcher("org.assertj.core.api.Assertions assertThat(..)");
private static final MethodMatcher GET_FIRST_MATCHER = new MethodMatcher("java.util.* getFirst()");
private static final MethodMatcher GET_LAST_MATCHER = new MethodMatcher("java.util.* getLast()");

@Override
public String getDisplayName() {
return "Simplify AssertJ assertions on SequencedCollection";
}

@Override
public String getDescription() {
return "Simplify AssertJ assertions on SequencedCollection by using dedicated assertion methods. " +
"For example, `assertThat(sequencedCollection.getLast())` can be simplified to `assertThat(sequencedCollection).last()`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
Preconditions.or(
new UsesMethod<>(GET_FIRST_MATCHER),
new UsesMethod<>(GET_LAST_MATCHER)
),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);

// Check if this is an assertThat call
if (!ASSERT_THAT_MATCHER.matches(mi) || mi.getArguments().size() != 1) {
return mi;
}

// Check if the argument is a method invocation
Expression arg = mi.getArguments().get(0);
if (arg instanceof J.MethodInvocation) {
// Check if the method is getFirst() or getLast() on a SequencedCollection
if (GET_FIRST_MATCHER.matches(arg)) {
return assertThat(mi, (J.MethodInvocation) arg, "first", ctx);
}
if (GET_LAST_MATCHER.matches(arg)) {
return assertThat(mi, (J.MethodInvocation) arg, "last", ctx);
}
}

return mi;
}

private J.MethodInvocation assertThat(J.MethodInvocation mi, J.MethodInvocation argMethod, String dedicatedAssertion, ExecutionContext ctx) {
return JavaTemplate.builder("assertThat(#{any(java.lang.Iterable)})." + dedicatedAssertion + "()")
.staticImports("org.assertj.core.api.Assertions.assertThat")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.build()
.apply(getCursor(), mi.getCoordinates().replace(), argMethod.getSelect());
}
}
);
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/assertj.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ recipeList:
- org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions
- org.openrewrite.java.testing.assertj.SimplifyAssertJAssertions
- org.openrewrite.java.testing.assertj.SimplifyHasSizeAssertion
- org.openrewrite.java.testing.assertj.SimplifySequencedCollectionAssertions

- tech.picnic.errorprone.refasterrules.AssertJBigDecimalRulesRecipes
- org.openrewrite.java.testing.assertj.AssertJBigIntegerRulesRecipes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.testing.assertj;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class SimplifySequencedCollectionAssertionsTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "assertj-core-3"))
.recipe(new SimplifySequencedCollectionAssertions());
}

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

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

class MyTest {
void testMethod() {
List<String> list = List.of("a", "b", "c");
assertThat(list.getLast()).isEqualTo("c");
}
}
""",
"""
import java.util.List;

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

class MyTest {
void testMethod() {
List<String> list = List.of("a", "b", "c");
assertThat(list).last().isEqualTo("c");
}
}
"""
)
);
}

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

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

class MyTest {
void testMethod() {
List<String> list = List.of("a", "b", "c");
assertThat(list.getFirst()).isEqualTo("a");
}
}
""",
"""
import java.util.List;

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

class MyTest {
void testMethod() {
List<String> list = List.of("a", "b", "c");
assertThat(list).first().isEqualTo("a");
}
}
"""
)
);
}

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

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

class MyTest {
void testMethod() {
List<String> list = List.of("a", "b", "c");
assertThat(list.getLast()).isNotNull();
assertThat(list.getFirst()).isNotEmpty();
assertThat(list.getLast()).contains("c");
}
}
""",
"""
import java.util.List;

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

class MyTest {
void testMethod() {
List<String> list = List.of("a", "b", "c");
assertThat(list).last().isNotNull();
assertThat(list).first().isNotEmpty();
assertThat(list).last().contains("c");
}
}
"""
)
);
}
}