org.openrewrite.java.testing.assertj.AdoptAssertJDurationAssertions Maven / Gradle / Ivy
Show all versions of rewrite-testing-frameworks Show documentation
/*
* Copyright 2024 the original author or authors.
*
* 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
*
* https://docs.moderne.io/licensing/moderne-source-available-license
*
* 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;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.staticanalysis.SimplifyDurationCreationUnits;
import java.util.*;
import static org.openrewrite.Preconditions.or;
public class AdoptAssertJDurationAssertions extends Recipe {
private static final String DURATION_ASSERT_HAS_LONG = "org.assertj.core.api.AbstractDurationAssert has*(long)";
private static final String INTEGER_ASSERT_IS_EQUAL_TO = "org.assertj.core.api.AbstractIntegerAssert isEqualTo(..)";
private static final String INTEGER_ASSERT_IS_GREATER_THAN = "org.assertj.core.api.AbstractIntegerAssert isGreaterThan(..)";
private static final String INTEGER_ASSERT_IS_LESS_THAN = "org.assertj.core.api.AbstractIntegerAssert isLessThan(..)";
private static final String LONG_ASSERT_IS_LESS_THAN = "org.assertj.core.api.AbstractLongAssert isLessThan(..)";
private static final String LONG_ASSERT_IS_GREATER_THAN = "org.assertj.core.api.AbstractLongAssert isGreaterThan(..)";
private static final String LONG_ASSERT_IS_EQUAL_TO = "org.assertj.core.api.AbstractLongAssert isEqualTo(..)";
private static final MethodMatcher ASSERT_THAT_MATCHER = new MethodMatcher("org.assertj.core.api.Assertions assertThat(..)");
private static final MethodMatcher GET_NANO_MATCHER = new MethodMatcher("java.time.Duration getNano()");
private static final MethodMatcher GET_SECONDS_MATCHER = new MethodMatcher("java.time.Duration getSeconds()");
private static final MethodMatcher AS_MATCHER = new MethodMatcher("org.assertj.core.api.AbstractObjectAssert as(..)");
private static final MethodMatcher TIME_UNIT_MATCHERS = new MethodMatcher(DURATION_ASSERT_HAS_LONG, true);
private static final List IS_MATCHERS = Arrays.asList(
new MethodMatcher(INTEGER_ASSERT_IS_EQUAL_TO, true),
new MethodMatcher(INTEGER_ASSERT_IS_GREATER_THAN, true),
new MethodMatcher(INTEGER_ASSERT_IS_LESS_THAN, true),
new MethodMatcher(LONG_ASSERT_IS_EQUAL_TO, true),
new MethodMatcher(LONG_ASSERT_IS_GREATER_THAN, true),
new MethodMatcher(LONG_ASSERT_IS_LESS_THAN, true)
);
private static final Map METHOD_MAP = new HashMap() {{
put("getSeconds", "hasSeconds");
put("getNano", "hasNanos");
put("hasNanos", "hasMillis");
put("hasMillis", "hasSeconds");
put("hasSeconds", "hasMinutes");
put("hasMinutes", "hasHours");
put("hasHours", "hasDays");
put("isGreaterThan", "isPositive");
put("isLessThan", "isNegative");
put("isEqualTo", "isZero");
}};
@Override
public String getDisplayName() {
return "Adopt AssertJ Duration assertions";
}
@Override
public String getDescription() {
return "Adopt AssertJ `DurationAssert` assertions for more expressive messages.";
}
@Override
public TreeVisitor, ExecutionContext> getVisitor() {
return Preconditions.check(
or(
new UsesMethod<>(DURATION_ASSERT_HAS_LONG, true),
new UsesMethod<>(INTEGER_ASSERT_IS_EQUAL_TO, true),
new UsesMethod<>(INTEGER_ASSERT_IS_GREATER_THAN, true),
new UsesMethod<>(INTEGER_ASSERT_IS_LESS_THAN, true),
new UsesMethod<>(LONG_ASSERT_IS_EQUAL_TO, true),
new UsesMethod<>(LONG_ASSERT_IS_GREATER_THAN, true),
new UsesMethod<>(LONG_ASSERT_IS_LESS_THAN, true)
), new JavaIsoVisitor() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (TIME_UNIT_MATCHERS.matches(mi)) {
return simplifyTimeUnits(mi, ctx);
} else if (IS_MATCHERS.stream().anyMatch(matcher -> matcher.matches(mi))) {
return simplifyMultipleAssertions(mi, ctx);
}
return mi;
}
private J.MethodInvocation simplifyMultipleAssertions(J.MethodInvocation m, ExecutionContext ctx) {
Expression isEqualToArg = m.getArguments().get(0);
Expression select = m.getSelect();
List