Skip to content

Commit 345ac0a

Browse files
timtebeekclaude
andauthored
Add recipe to simplify AssertJ SequencedCollection assertions (#837)
Transforms assertThat(collection.getLast()) to assertThat(collection).last() and assertThat(collection.getFirst()) to assertThat(collection).first() for cleaner and more idiomatic AssertJ assertions on SequencedCollections. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
1 parent 37e86b7 commit 345ac0a

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.testing.assertj;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Preconditions;
20+
import org.openrewrite.Recipe;
21+
import org.openrewrite.TreeVisitor;
22+
import org.openrewrite.java.JavaIsoVisitor;
23+
import org.openrewrite.java.JavaParser;
24+
import org.openrewrite.java.JavaTemplate;
25+
import org.openrewrite.java.MethodMatcher;
26+
import org.openrewrite.java.search.UsesMethod;
27+
import org.openrewrite.java.tree.Expression;
28+
import org.openrewrite.java.tree.J;
29+
30+
public class SimplifySequencedCollectionAssertions extends Recipe {
31+
32+
private static final MethodMatcher ASSERT_THAT_MATCHER = new MethodMatcher("org.assertj.core.api.Assertions assertThat(..)");
33+
private static final MethodMatcher GET_FIRST_MATCHER = new MethodMatcher("java.util.* getFirst()");
34+
private static final MethodMatcher GET_LAST_MATCHER = new MethodMatcher("java.util.* getLast()");
35+
36+
@Override
37+
public String getDisplayName() {
38+
return "Simplify AssertJ assertions on SequencedCollection";
39+
}
40+
41+
@Override
42+
public String getDescription() {
43+
return "Simplify AssertJ assertions on SequencedCollection by using dedicated assertion methods. " +
44+
"For example, `assertThat(sequencedCollection.getLast())` can be simplified to `assertThat(sequencedCollection).last()`.";
45+
}
46+
47+
@Override
48+
public TreeVisitor<?, ExecutionContext> getVisitor() {
49+
return Preconditions.check(
50+
Preconditions.or(
51+
new UsesMethod<>(GET_FIRST_MATCHER),
52+
new UsesMethod<>(GET_LAST_MATCHER)
53+
),
54+
new JavaIsoVisitor<ExecutionContext>() {
55+
@Override
56+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
57+
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
58+
59+
// Check if this is an assertThat call
60+
if (!ASSERT_THAT_MATCHER.matches(mi) || mi.getArguments().size() != 1) {
61+
return mi;
62+
}
63+
64+
// Check if the argument is a method invocation
65+
Expression arg = mi.getArguments().get(0);
66+
if (arg instanceof J.MethodInvocation) {
67+
// Check if the method is getFirst() or getLast() on a SequencedCollection
68+
if (GET_FIRST_MATCHER.matches(arg)) {
69+
return assertThat(mi, (J.MethodInvocation) arg, "first", ctx);
70+
}
71+
if (GET_LAST_MATCHER.matches(arg)) {
72+
return assertThat(mi, (J.MethodInvocation) arg, "last", ctx);
73+
}
74+
}
75+
76+
return mi;
77+
}
78+
79+
private J.MethodInvocation assertThat(J.MethodInvocation mi, J.MethodInvocation argMethod, String dedicatedAssertion, ExecutionContext ctx) {
80+
return JavaTemplate.builder("assertThat(#{any(java.lang.Iterable)})." + dedicatedAssertion + "()")
81+
.staticImports("org.assertj.core.api.Assertions.assertThat")
82+
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
83+
.build()
84+
.apply(getCursor(), mi.getCoordinates().replace(), argMethod.getSelect());
85+
}
86+
}
87+
);
88+
}
89+
}

src/main/resources/META-INF/rewrite/assertj.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ recipeList:
4747
- org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions
4848
- org.openrewrite.java.testing.assertj.SimplifyAssertJAssertions
4949
- org.openrewrite.java.testing.assertj.SimplifyHasSizeAssertion
50+
- org.openrewrite.java.testing.assertj.SimplifySequencedCollectionAssertions
5051

5152
- tech.picnic.errorprone.refasterrules.AssertJBigDecimalRulesRecipes
5253
- org.openrewrite.java.testing.assertj.AssertJBigIntegerRulesRecipes
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.testing.assertj;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.InMemoryExecutionContext;
21+
import org.openrewrite.java.JavaParser;
22+
import org.openrewrite.test.RecipeSpec;
23+
import org.openrewrite.test.RewriteTest;
24+
25+
import static org.openrewrite.java.Assertions.java;
26+
27+
class SimplifySequencedCollectionAssertionsTest implements RewriteTest {
28+
@Override
29+
public void defaults(RecipeSpec spec) {
30+
spec
31+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "assertj-core-3"))
32+
.recipe(new SimplifySequencedCollectionAssertions());
33+
}
34+
35+
@DocumentExample
36+
@Test
37+
void getLast() {
38+
rewriteRun(
39+
//language=java
40+
java(
41+
"""
42+
import java.util.List;
43+
44+
import static org.assertj.core.api.Assertions.assertThat;
45+
46+
class MyTest {
47+
void testMethod() {
48+
List<String> list = List.of("a", "b", "c");
49+
assertThat(list.getLast()).isEqualTo("c");
50+
}
51+
}
52+
""",
53+
"""
54+
import java.util.List;
55+
56+
import static org.assertj.core.api.Assertions.assertThat;
57+
58+
class MyTest {
59+
void testMethod() {
60+
List<String> list = List.of("a", "b", "c");
61+
assertThat(list).last().isEqualTo("c");
62+
}
63+
}
64+
"""
65+
)
66+
);
67+
}
68+
69+
@Test
70+
void getFirst() {
71+
rewriteRun(
72+
//language=java
73+
java(
74+
"""
75+
import java.util.List;
76+
77+
import static org.assertj.core.api.Assertions.assertThat;
78+
79+
class MyTest {
80+
void testMethod() {
81+
List<String> list = List.of("a", "b", "c");
82+
assertThat(list.getFirst()).isEqualTo("a");
83+
}
84+
}
85+
""",
86+
"""
87+
import java.util.List;
88+
89+
import static org.assertj.core.api.Assertions.assertThat;
90+
91+
class MyTest {
92+
void testMethod() {
93+
List<String> list = List.of("a", "b", "c");
94+
assertThat(list).first().isEqualTo("a");
95+
}
96+
}
97+
"""
98+
)
99+
);
100+
}
101+
102+
@Test
103+
void withDifferentAssertions() {
104+
rewriteRun(
105+
//language=java
106+
java(
107+
"""
108+
import java.util.List;
109+
110+
import static org.assertj.core.api.Assertions.assertThat;
111+
112+
class MyTest {
113+
void testMethod() {
114+
List<String> list = List.of("a", "b", "c");
115+
assertThat(list.getLast()).isNotNull();
116+
assertThat(list.getFirst()).isNotEmpty();
117+
assertThat(list.getLast()).contains("c");
118+
}
119+
}
120+
""",
121+
"""
122+
import java.util.List;
123+
124+
import static org.assertj.core.api.Assertions.assertThat;
125+
126+
class MyTest {
127+
void testMethod() {
128+
List<String> list = List.of("a", "b", "c");
129+
assertThat(list).last().isNotNull();
130+
assertThat(list).first().isNotEmpty();
131+
assertThat(list).last().contains("c");
132+
}
133+
}
134+
"""
135+
)
136+
);
137+
}
138+
}

0 commit comments

Comments
 (0)