Extended JUnitAssertToAssertJ inspection to convert JUnit assume-Statements, too.

Improved JUnitAssertToAssertJ quick fix to swap expected and actual expressions if the actual one is a constant.
This commit is contained in:
Chris Hodges 2019-05-17 12:17:01 +02:00
parent e797dc2515
commit 178b7d368a
12 changed files with 239 additions and 35 deletions

View File

@ -85,7 +85,8 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
except for method calls with known side-effect methods such as ```Iterator.next()``` and
pre/post-increment/decrement operations -- please notify me about others.
The comments of the statements will be preserved. When using ```.extracting()``` or similar, the statements will not be merged.
The comments of the statements will be preserved. When using ```extracting()``` or similar,
the statements will not be merged.
- AssertThatObjectIsNullOrNotNull
@ -366,7 +367,7 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
as being skipped.
Return statements in ```if``` statements in main test methods (must be annotated with JUnit 4 or
Jupiter @Test annotations) will be verified to have at least one ```assertThat()``` statement in the code flow.
Jupiter ```@Test``` annotations) will be verified to have at least one ```assertThat()``` statement in the code flow.
Method calls within the same class will be examined for ```assertThat()``` statements, too.
However, at most 50 statements and down to five recursions will be tolerated before giving up.
@ -402,7 +403,10 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
- JUnitAssertToAssertJ
Tries to convert most of the JUnit 4 assertions to AssertJ format.
Tries to convert most of the JUnit 4 assertions and assumptions to AssertJ format.
Sometimes the expected and actual expressions are specified in wrong order --
Cajon tries to swap these when it detects the supposed actual expression to be a
constant while the expected one is not.
Does not support Hamcrest-Matchers.
If you need that kind of conversion, you might want to check out the
@ -415,8 +419,8 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
assertFalse(message, condition);
assertNull(object);
assertNull(message, object);
assertNonNull(object);
assertNonNull(message, object);
assertNotNull(object);
assertNotNull(message, object);
assertEquals(expected, actual);
assertEquals(message, expected, actual);
assertEquals(expectedDoubleOrFloat, actualDoubleOrFloat, delta);
@ -433,6 +437,14 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
assertArrayEquals(message, expectedArray, actualArray);
assertArrayEquals(expectedDoubleOrFloatArray, actualDoubleOrFloatArray, delta);
assertArrayEquals(message, expectedDoubleOrFloatArray, actualDoubleOrFloatArray, delta);
assumeTrue(condition);
assumeTrue(message, condition);
assumeFalse(condition);
assumeFalse(message, condition);
assumeNotNull(object); // single argument only!
assumeNoException(throwable);
assumeNoException(message, throwable);
```
### Implemented referencing
@ -478,6 +490,8 @@ Feel free to use the code (in package ```de.platon42.intellij.jupiter```) for yo
- Improved JoinAssertThatStatements detection of expressions with side-effects and added pre/post-increment/decrement detection.
- Added Guava Optional ```opt.orNull() == null``` case. You know, I'm not making this stuff up, people actually write this kind of code.
- Added Java 8 Optional ```opt.orElse(null) == null``` case, too.
- Extended JUnitAssertToAssertJ inspection to convert JUnit ```assume```-Statements, too.
- Improved JUnitAssertToAssertJ quick fix to swap expected and actual expressions if the actual one is a constant.
#### V1.0 (06-May-19)
- First release to be considered stable enough for production use.

View File

@ -47,6 +47,8 @@ patchPluginXml {
<li>Improved JoinAssertThatStatements detection of expressions with side-effects and added pre/post-increment/decrement detection.
<li>Added Guava Optional opt.orNull() == null case. You know, I'm not making this stuff up, people actually write this kind of code.
<li>Added Java 8 Optional opt.orElse(null) == null case, too.
<li>Extended JUnitAssertToAssertJ inspection to convert JUnit assume-Statements, too.
<li>Improved JUnitAssertToAssertJ quick fix to swap expected and actual expressions if the actual one is a constant.
</ul>
<h4>V1.0 (06-May-19)</h4>
<ul>

View File

@ -117,11 +117,14 @@ fun PsiMethodCallExpression.getExpectedNullNonNullResult(): Boolean? {
fun PsiMethodCallExpression.calculateConstantParameterValue(argIndex: Int): Any? {
if (argIndex >= argumentList.expressions.size) return null
val valueExpression = getArg(argIndex)
return getArg(argIndex).calculateConstantValue()
}
fun PsiExpression.calculateConstantValue(): Any? {
val constantEvaluationHelper = JavaPsiFacade.getInstance(project).constantEvaluationHelper
val value = constantEvaluationHelper.computeConstantExpression(valueExpression)
val value = constantEvaluationHelper.computeConstantExpression(this)
if (value == null) {
val field = (valueExpression as? PsiReferenceExpression)?.resolve() as? PsiField
val field = (this as? PsiReferenceExpression)?.resolve() as? PsiField
if (field?.containingClass?.qualifiedName == CommonClassNames.JAVA_LANG_BOOLEAN) {
return when (field.name) {
"TRUE" -> true

View File

@ -18,6 +18,10 @@ fun createAssertThat(context: PsiElement, baseclass: String, actualExpression: P
return createMethodCall(context, "$baseclass.${MethodNames.ASSERT_THAT}", actualExpression)
}
fun createAssumeThat(context: PsiElement, actualExpression: PsiExpression): PsiMethodCallExpression {
return createMethodCall(context, "${AssertJClassNames.ASSUMPTIONS_CLASSNAME}.${MethodNames.ASSUME_THAT}", actualExpression)
}
fun createExpectedMethodCall(context: PsiElement, methodName: String, vararg arguments: PsiElement): PsiMethodCallExpression {
return createMethodCall(context, "a.$methodName", *arguments)
}

View File

@ -9,6 +9,8 @@ open class AbstractJUnitAssertInspection : AbstractBaseJavaLocalInspectionTool()
companion object {
@NonNls
const val JUNIT_ASSERT_CLASSNAME = "org.junit.Assert"
@NonNls
const val JUNIT_ASSUME_CLASSNAME = "org.junit.Assume"
@NonNls
const val ASSERT_TRUE_METHOD = "assertTrue"
@ -28,6 +30,16 @@ open class AbstractJUnitAssertInspection : AbstractBaseJavaLocalInspectionTool()
const val ASSERT_NOT_SAME_METHOD = "assertNotSame"
@NonNls
const val ASSERT_ARRAY_EQUALS_METHOD = "assertArrayEquals"
@NonNls
const val ASSUME_TRUE_METHOD = "assumeTrue"
@NonNls
const val ASSUME_FALSE_METHOD = "assumeFalse"
@NonNls
const val ASSUME_NOT_NULL_METHOD = "assumeNotNull"
@NonNls
const val ASSUME_NO_EXCEPTION = "assumeNoException"
}
override fun getGroupDisplayName(): String {

View File

@ -10,43 +10,44 @@ import com.siyeh.ig.callMatcher.CallMatcher.staticCall
import de.platon42.intellij.plugins.cajon.AssertJClassNames
import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceJUnitAssertMethodCallQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceJUnitAssumeMethodCallQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceJUnitDeltaAssertMethodCallQuickFix
class JUnitAssertToAssertJInspection : AbstractJUnitAssertInspection() {
companion object {
private const val DISPLAY_NAME = "Convert JUnit assertions to AssertJ"
private const val DISPLAY_NAME = "Convert JUnit assertions/assumptions to AssertJ"
private const val CONVERT_MESSAGE_TEMPLATE = "%s can be converted to AssertJ style"
private const val CONVERT_DESCRIPTION_TEMPLATE = "Convert %s() to assertThat().%s()"
private const val CONVERT_DESCRIPTION_TEMPLATE = "Convert %s() to %s().%s()"
private val MAPPINGS = listOf(
private val ASSERT_MAPPINGS = listOf(
Mapping(
anyOf(
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_TRUE_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "boolean"),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_TRUE_METHOD).parameterTypes("boolean")
),
MethodNames.IS_TRUE, false
MethodNames.IS_TRUE, hasExpected = false
),
Mapping(
anyOf(
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_FALSE_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "boolean"),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_FALSE_METHOD).parameterTypes("boolean")
),
MethodNames.IS_FALSE, false
MethodNames.IS_FALSE, hasExpected = false
),
Mapping(
anyOf(
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NULL_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, CommonClassNames.JAVA_LANG_OBJECT),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NULL_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)
),
MethodNames.IS_NULL, false
MethodNames.IS_NULL, hasExpected = false
),
Mapping(
anyOf(
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_NULL_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, CommonClassNames.JAVA_LANG_OBJECT),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_NULL_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)
),
MethodNames.IS_NOT_NULL, false
MethodNames.IS_NOT_NULL, hasExpected = false
),
Mapping(
anyOf(
@ -111,6 +112,34 @@ class JUnitAssertToAssertJInspection : AbstractJUnitAssertInspection() {
MethodNames.CONTAINS_EXACTLY
)
)
private val ASSUME_MAPPINGS = listOf(
Mapping(
anyOf(
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_TRUE_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "boolean"),
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_TRUE_METHOD).parameterTypes("boolean")
),
MethodNames.IS_TRUE, hasExpected = false
),
Mapping(
anyOf(
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_FALSE_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "boolean"),
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_FALSE_METHOD).parameterTypes("boolean")
),
MethodNames.IS_FALSE, hasExpected = false
),
Mapping(
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_NOT_NULL_METHOD).parameterCount(1),
MethodNames.IS_NOT_NULL, hasExpected = false, singleArgument = true
),
Mapping(
anyOf(
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_NO_EXCEPTION).parameterTypes(CommonClassNames.JAVA_LANG_STRING, CommonClassNames.JAVA_LANG_THROWABLE),
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_NO_EXCEPTION).parameterTypes(CommonClassNames.JAVA_LANG_THROWABLE)
),
"doesNotThrowAnyException", hasExpected = false
)
)
}
override fun getDisplayName() = DISPLAY_NAME
@ -119,18 +148,28 @@ class JUnitAssertToAssertJInspection : AbstractJUnitAssertInspection() {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
val isJUnitAssertCall = expression.resolveMethod()?.containingClass?.qualifiedName == JUNIT_ASSERT_CLASSNAME
if (!isJUnitAssertCall) {
return // early exit
}
JavaPsiFacade.getInstance(expression.project)
.findClass(AssertJClassNames.ASSERTIONS_CLASSNAME, GlobalSearchScope.allScope(expression.project)) ?: return
val mapping = MAPPINGS.firstOrNull { it.callMatcher.test(expression) } ?: return
if (mapping.hasDelta) {
registerConvertMethod(holder, expression, mapping.replacement, ::ReplaceJUnitDeltaAssertMethodCallQuickFix)
} else {
registerConvertMethod(holder, expression, mapping.replacement) { desc, method ->
ReplaceJUnitAssertMethodCallQuickFix(desc, method, !mapping.hasExpected)
when (expression.resolveMethod()?.containingClass?.qualifiedName) {
JUNIT_ASSERT_CLASSNAME -> {
JavaPsiFacade.getInstance(expression.project)
.findClass(AssertJClassNames.ASSERTIONS_CLASSNAME, GlobalSearchScope.allScope(expression.project)) ?: return
val mapping = ASSERT_MAPPINGS.firstOrNull { it.callMatcher.test(expression) } ?: return
if (mapping.hasDelta) {
registerConvertMethod(holder, expression, mapping.replacement, MethodNames.ASSERT_THAT, ::ReplaceJUnitDeltaAssertMethodCallQuickFix)
} else {
registerConvertMethod(holder, expression, mapping.replacement, MethodNames.ASSERT_THAT) { desc, method ->
ReplaceJUnitAssertMethodCallQuickFix(desc, method, !mapping.hasExpected)
}
}
}
JUNIT_ASSUME_CLASSNAME -> {
JavaPsiFacade.getInstance(expression.project)
.findClass(AssertJClassNames.ASSUMPTIONS_CLASSNAME, GlobalSearchScope.allScope(expression.project)) ?: return
val mapping = ASSUME_MAPPINGS.firstOrNull { it.callMatcher.test(expression) } ?: return
if (!mapping.singleArgument || expression.argumentList.expressions.size == 1) {
registerConvertMethod(holder, expression, mapping.replacement, MethodNames.ASSUME_THAT) { desc, method ->
ReplaceJUnitAssumeMethodCallQuickFix(desc, method)
}
}
}
}
}
@ -141,10 +180,11 @@ class JUnitAssertToAssertJInspection : AbstractJUnitAssertInspection() {
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
replacementMethod: String,
staticMethod: String,
quickFixSupplier: (String, String) -> LocalQuickFix
) {
val originalMethod = getOriginalMethodName(expression) ?: return
val description = CONVERT_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod)
val description = CONVERT_DESCRIPTION_TEMPLATE.format(originalMethod, staticMethod, replacementMethod)
val message = CONVERT_MESSAGE_TEMPLATE.format(originalMethod)
val quickfix = quickFixSupplier(description, replacementMethod)
holder.registerProblem(expression, message, quickfix)
@ -154,6 +194,7 @@ class JUnitAssertToAssertJInspection : AbstractJUnitAssertInspection() {
val callMatcher: CallMatcher,
val replacement: String,
val hasExpected: Boolean = true,
val hasDelta: Boolean = false
val hasDelta: Boolean = false,
val singleArgument: Boolean = false
)
}

View File

@ -24,7 +24,7 @@ class ReplaceJUnitAssertMethodCallQuickFix(description: String, private val repl
val args = methodCallExpression.argumentList
val count = args.expressions.size
val actualExpression = args.expressions[count - 1] ?: return
val (expectedExpression, messageExpression) = if (noExpectedExpression) {
val (expectedExpressions, messageExpression) = if (noExpectedExpression) {
val message = args.expressions.getOrNull(count - 2)
emptyArray<PsiExpression>() to message
} else {
@ -33,8 +33,15 @@ class ReplaceJUnitAssertMethodCallQuickFix(description: String, private val repl
arrayOf(expected) to message
}
val expectedMethodCall = createExpectedMethodCall(element, replacementMethod, *expectedExpression)
val newMethodCall = createAssertThat(element, actualExpression)
val swapActualAndExpected = ((expectedExpressions.getOrNull(0)?.calculateConstantValue() == null)
&& (actualExpression.calculateConstantValue() != null))
val (expectedMethodCall, newMethodCall) = if (swapActualAndExpected) {
createExpectedMethodCall(element, replacementMethod, actualExpression) to
createAssertThat(element, expectedExpressions.single())
} else {
createExpectedMethodCall(element, replacementMethod, *expectedExpressions) to
createAssertThat(element, actualExpression)
}
if (messageExpression != null) {
val asExpression = createExpectedMethodCall(element, MethodNames.AS, messageExpression)

View File

@ -0,0 +1,41 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiMethodCallExpression
import de.platon42.intellij.plugins.cajon.*
class ReplaceJUnitAssumeMethodCallQuickFix(description: String, private val replacementMethod: String) :
AbstractCommonQuickFix(description) {
companion object {
private const val CONVERT_DESCRIPTION = "Convert JUnit assumptions to assertJ"
}
override fun getFamilyName(): String {
return CONVERT_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement
val methodCallExpression = element as? PsiMethodCallExpression ?: return
val args = methodCallExpression.argumentList
val count = args.expressions.size
val actualExpression = args.expressions[count - 1] ?: return
val messageExpression = args.expressions.getOrNull(count - 2)
val expectedMethodCall = createExpectedMethodCall(element, replacementMethod)
val newMethodCall = createAssumeThat(element, actualExpression)
if (messageExpression != null) {
val asExpression = createExpectedMethodCall(element, MethodNames.AS, messageExpression)
asExpression.replaceQualifier(newMethodCall)
expectedMethodCall.replaceQualifier(asExpression)
} else {
expectedMethodCall.replaceQualifier(newMethodCall)
}
newMethodCall.resolveMethod()?.addAsStaticImport(element)
element.replace(expectedMethodCall).shortenAndReformat()
}
}

View File

@ -27,8 +27,16 @@ class ReplaceJUnitDeltaAssertMethodCallQuickFix(description: String, private val
val deltaExpression = args.expressions[count - 1] ?: return
val offsetMethodCall = createMethodCall(element, "org.assertj.core.data.Offset.offset", deltaExpression)
val expectedMethodCall = createExpectedMethodCall(element, replacementMethod, expectedExpression, offsetMethodCall)
val newMethodCall = createAssertThat(element, actualExpression)
val swapActualAndExpected = ((expectedExpression.calculateConstantValue() == null)
&& (actualExpression.calculateConstantValue() != null))
val (expectedMethodCall, newMethodCall) = if (swapActualAndExpected) {
createExpectedMethodCall(element, replacementMethod, actualExpression, offsetMethodCall) to
createAssertThat(element, expectedExpression)
} else {
createExpectedMethodCall(element, replacementMethod, expectedExpression, offsetMethodCall) to
createAssertThat(element, actualExpression)
}
if (messageExpression != null) {
val asExpression = createExpectedMethodCall(element, MethodNames.AS, messageExpression)

View File

@ -18,7 +18,8 @@ internal class JUnitAssertToAssertJInspectionTest : AbstractCajonTest() {
runTest {
myFixture.enableInspections(JUnitAssertToAssertJInspection::class.java)
myFixture.configureByFile("JUnitAssertToAssertJInspectionBefore.java")
executeQuickFixes(myFixture, Regex("Convert assert.*\\(\\) to assertThat\\(\\).*"), 38)
executeQuickFixes(myFixture, Regex("Convert assert.*\\(\\) to assertThat\\(\\).*"), 48)
executeQuickFixes(myFixture, Regex("Convert assume.*\\(\\) to assumeThat\\(\\).*"), 7)
myFixture.checkResultByFile("JUnitAssertToAssertJInspectionAfter.java")
}
}

View File

@ -1,12 +1,18 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.assertj.core.data.Offset.offset;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
public class JUnitAssertToAssertJ {
private void jUnitAssertToAssertJ() {
String foo = "foo";
String bar = "bar";
int someInt = 1;
double someDouble = 1.0;
float someFloat = 1.0f;
assertThat(foo == "foo").isTrue();
assertThat(foo == "foo").as("oh no!").isTrue();
assertThat(foo == "bar").isFalse();
@ -52,5 +58,35 @@ public class JUnitAssertToAssertJ {
assertThat(new double[1]).as("array equals").containsExactly(new double[2], offset(1.0));
assertThat(new float[1]).containsExactly(new float[2], offset(1.0f));
assertThat(new float[1]).as("array equals").containsExactly(new float[2], offset(1.0f));
assertThat(foo).isEqualTo("bar");
assertThat(bar).as("equals").isEqualTo("foo");
assertThat(bar).isNotEqualTo("foo");
assertThat(foo).as("equals").isNotEqualTo("bar");
assertThat(someInt).isEqualTo(2);
assertThat(someDouble).isCloseTo(2.0, offset(0.1));
assertThat(someDouble).as("equals").isEqualTo(1.0);
assertThat(someDouble).as("equals").isCloseTo(1.0, offset(0.1));
assertThat(someFloat).isEqualTo(1.0f);
assertThat(someFloat).isCloseTo(2.0f, offset(0.1f));
fail();
fail("oh no!")
}
private void jUnitAssumeToAssertJ() {
String foo = "foo";
String bar = "bar";
assumeThat(foo == "foo").isTrue();
assumeThat(foo == "foo").as("oh no!").isTrue();
assumeThat(foo == "bar").isFalse();
assumeThat(foo == "bar").as("boom!").isFalse();
assumeThat(foo).isNotNull();
assumeNotNull(foo, bar);
assumeThat(new IllegalArgumentException()).doesNotThrowAnyException();
assumeThat(new IllegalArgumentException()).as("oh no!").doesNotThrowAnyException();
}
}

View File

@ -1,10 +1,15 @@
import static org.junit.Assert.*;
import static org.junit.Assume.*;
public class JUnitAssertToAssertJ {
private void jUnitAssertToAssertJ() {
String foo = "foo";
String bar = "bar";
int someInt = 1;
double someDouble = 1.0;
float someFloat = 1.0f;
assertTrue(foo == "foo");
assertTrue("oh no!", foo == "foo");
assertFalse(foo == "bar");
@ -50,5 +55,35 @@ public class JUnitAssertToAssertJ {
assertArrayEquals("array equals", new double[2], new double[1], 1.0);
assertArrayEquals(new float[2], new float[1], 1.0f);
assertArrayEquals("array equals", new float[2], new float[1], 1.0f);
assertEquals("bar", foo);
assertEquals("equals", bar, "foo");
assertNotEquals(bar, "foo");
assertNotEquals("equals", "bar", foo);
assertEquals(someInt, 2);
assertEquals(someDouble, 2.0, 0.1);
assertEquals("equals",1.0, someDouble);
assertEquals("equals",1.0, someDouble, 0.1);
assertEquals(1.0f, someFloat);
assertEquals(someFloat, 2.0f, 0.1f);
fail();
fail("oh no!")
}
private void jUnitAssumeToAssertJ() {
String foo = "foo";
String bar = "bar";
assumeTrue(foo == "foo");
assumeTrue("oh no!", foo == "foo");
assumeFalse(foo == "bar");
assumeFalse("boom!", foo == "bar");
assumeNotNull(foo);
assumeNotNull(foo, bar);
assumeNoException(new IllegalArgumentException());
assumeNoException("oh no!", new IllegalArgumentException());
}
}