Skip to content

Commit dc5e689

Browse files
velogithub-actions[bot]timtebeek
authored
More best pratices for AssertJ (#548)
* Added ChainedAssertions for Iterator hasNext Signed-off-by: Marvin Froeder <[email protected]> * Create dedicated test for IsEqualToEmptyString * AssertJ has a more idiomatic way of asserting that a boolean is true. This recipe will find instances of: -`assertThat(boolean).isEqualTo(true)` and replace them with `isTrue()`. -`assertThat(boolean).isEqualTo(false)` and replace them with `isFalse()`. Signed-off-by: Marvin Froeder <[email protected]> * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply formatter * Add missing `@Test` annotations --------- Signed-off-by: Marvin Froeder <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <[email protected]>
1 parent f1fda26 commit dc5e689

File tree

5 files changed

+314
-41
lines changed

5 files changed

+314
-41
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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.MethodMatcher;
24+
import org.openrewrite.java.search.UsesMethod;
25+
import org.openrewrite.java.tree.J;
26+
import org.openrewrite.java.tree.J.MethodInvocation;
27+
import org.openrewrite.java.tree.JavaType.Method;
28+
29+
import java.util.Collections;
30+
31+
/**
32+
* AssertJ has a more idiomatic way of asserting that a boolean is true. This
33+
* recipe will find instances of:
34+
* <p>
35+
* -`assertThat(boolean).isEqualTo(true)` and replace them with `isTrue()`.
36+
* -`assertThat(boolean).isEqualTo(false)` and replace them with `isFalse()`.
37+
*/
38+
public class IsEqualToBoolean extends Recipe {
39+
40+
private static final MethodMatcher IS_EQUAL_TO = new MethodMatcher(
41+
"org.assertj.core.api.AbstractBooleanAssert isEqualTo(boolean)");
42+
43+
@Override
44+
public String getDisplayName() {
45+
return "Convert `assertThat(String).isEqualTo(true)` to `isTrue()` and `isEqualTo(false)` to `isFalse()`";
46+
}
47+
48+
@Override
49+
public String getDescription() {
50+
return "Adopt idiomatic AssertJ assertion for true booleans.";
51+
}
52+
53+
@Override
54+
public TreeVisitor<?, ExecutionContext> getVisitor() {
55+
return Preconditions.check(new UsesMethod<>(IS_EQUAL_TO), new JavaIsoVisitor<ExecutionContext>() {
56+
@Override
57+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
58+
MethodInvocation mi = super.visitMethodInvocation(method, ctx);
59+
if (IS_EQUAL_TO.matches(mi)) {
60+
String methodName;
61+
if (J.Literal.isLiteralValue(mi.getArguments().get(0), true)) {
62+
methodName = "isTrue";
63+
} else {
64+
methodName = "isFalse";
65+
}
66+
Method isBooleanMethod = mi.getMethodType().withName(methodName);
67+
return mi.withName(mi.getName().withSimpleName(methodName).withType(isBooleanMethod))
68+
.withMethodType(isBooleanMethod).withArguments(Collections.emptyList());
69+
}
70+
return mi;
71+
}
72+
});
73+
}
74+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ recipeList:
2525
- org.openrewrite.java.testing.hamcrest.MigrateHamcrestToAssertJ
2626
- org.openrewrite.java.testing.assertj.JUnitToAssertj
2727
- org.openrewrite.java.testing.assertj.StaticImports
28+
- org.openrewrite.java.testing.assertj.IsEqualToBoolean
2829
- org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions
2930
- org.openrewrite.java.testing.assertj.IsEqualToEmptyString
3031

@@ -337,6 +338,16 @@ recipeList:
337338
assertToReplace: isSameAs
338339
dedicatedAssertion: containsSame
339340
requiredType: java.util.Optional
341+
- org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion:
342+
chainedAssertion: hasNext
343+
assertToReplace: isTrue
344+
dedicatedAssertion: hasNext
345+
requiredType: java.util.Iterator
346+
- org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion:
347+
chainedAssertion: hasNext
348+
assertToReplace: isFalse
349+
dedicatedAssertion: isExhausted
350+
requiredType: java.util.Iterator
340351

341352
---
342353
type: specs.openrewrite.org/v1beta/recipe
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 IsEqualToBooleanTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "assertj-core-3.24"))
33+
.recipe(new IsEqualToBoolean());
34+
}
35+
36+
@DocumentExample
37+
@Test
38+
void convertsIsEqualToTrue() {
39+
rewriteRun(
40+
// language=java
41+
java(
42+
"""
43+
import static org.assertj.core.api.Assertions.assertThat;
44+
class Test {
45+
void test() {
46+
assertThat(true).isEqualTo(true);
47+
}
48+
}
49+
""",
50+
"""
51+
import static org.assertj.core.api.Assertions.assertThat;
52+
class Test {
53+
void test() {
54+
assertThat(true).isTrue();
55+
}
56+
}
57+
"""
58+
)
59+
);
60+
}
61+
62+
@Test
63+
void convertsIsEqualToFalse() {
64+
rewriteRun(
65+
// language=java
66+
java(
67+
"""
68+
import static org.assertj.core.api.Assertions.assertThat;
69+
class Test {
70+
void test() {
71+
assertThat(false).isEqualTo(false);
72+
}
73+
}
74+
""",
75+
"""
76+
import static org.assertj.core.api.Assertions.assertThat;
77+
class Test {
78+
void test() {
79+
assertThat(false).isFalse();
80+
}
81+
}
82+
"""
83+
)
84+
);
85+
}
86+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 IsEqualToEmptyStringTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "assertj-core-3.24"))
33+
.recipe(new IsEqualToEmptyString());
34+
}
35+
36+
@DocumentExample
37+
@Test
38+
void convertsIsEqualToEmptyString() {
39+
rewriteRun(
40+
// language=java
41+
java(
42+
"""
43+
import static org.assertj.core.api.Assertions.assertThat;
44+
class Test {
45+
void test() {
46+
assertThat("test").isEqualTo("");
47+
}
48+
}
49+
""",
50+
"""
51+
import static org.assertj.core.api.Assertions.assertThat;
52+
class Test {
53+
void test() {
54+
assertThat("test").isEmpty();
55+
}
56+
}
57+
"""
58+
)
59+
);
60+
}
61+
}

0 commit comments

Comments
 (0)