summaryrefslogtreecommitdiff
path: root/plugins/IntentionPowerPak/src/com/siyeh/ipp/junit/CreateAssertIntention.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/IntentionPowerPak/src/com/siyeh/ipp/junit/CreateAssertIntention.java')
-rw-r--r--plugins/IntentionPowerPak/src/com/siyeh/ipp/junit/CreateAssertIntention.java133
1 files changed, 66 insertions, 67 deletions
diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/junit/CreateAssertIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/junit/CreateAssertIntention.java
index 55ae45e3f3bf..4be5442d03c1 100644
--- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/junit/CreateAssertIntention.java
+++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/junit/CreateAssertIntention.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2006 Dave Griffith, Bas Leijdekkers
+ * Copyright 2003-2014 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,16 +16,15 @@
package com.siyeh.ipp.junit;
import com.intellij.psi.*;
-import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.tree.IElementType;
-import com.intellij.util.IncorrectOperationException;
-import com.intellij.codeInsight.AnnotationUtil;
import com.siyeh.ig.PsiReplacementUtil;
+import com.siyeh.ig.psiutils.ExpressionUtils;
+import com.siyeh.ig.psiutils.ImportUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.BoolUtils;
-import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.NonNls;
+import org.jetbrains.annotations.NotNull;
public class CreateAssertIntention extends Intention {
@@ -34,28 +33,12 @@ public class CreateAssertIntention extends Intention {
return new CreateAssertPredicate();
}
- public void processIntention(PsiElement element)
- throws IncorrectOperationException {
- final PsiExpressionStatement statement =
- (PsiExpressionStatement)element;
- assert statement != null;
+ public void processIntention(PsiElement element) {
+ final PsiExpressionStatement statement = (PsiExpressionStatement)element;
final PsiExpression expression = statement.getExpression();
- final PsiMethod containingMethod =
- PsiTreeUtil.getParentOfType(statement, PsiMethod.class);
- final String specifierString;
- if (containingMethod != null &&
- AnnotationUtil.isAnnotated(containingMethod,
- "org.junit.Test", true)) {
- specifierString = "org.junit.Assert.";
- }
- else {
- specifierString = "";
- }
+ final String newStatement;
if (BoolUtils.isNegation(expression)) {
- @NonNls final String newExpression =
- specifierString + "assertFalse(" +
- BoolUtils.getNegatedExpressionText(expression) + ");";
- PsiReplacementUtil.replaceStatementAndShortenClassNames(statement, newExpression);
+ newStatement = buildNewStatement("assertFalse", element, BoolUtils.getNegatedExpressionText(expression));
}
else if (isNullComparison(expression)) {
final PsiBinaryExpression binaryExpression =
@@ -63,16 +46,19 @@ public class CreateAssertIntention extends Intention {
final PsiExpression lhs = binaryExpression.getLOperand();
final PsiExpression rhs = binaryExpression.getROperand();
final PsiExpression comparedExpression;
- if (isNull(lhs)) {
+ if (ExpressionUtils.isNullLiteral(lhs)) {
comparedExpression = rhs;
}
else {
comparedExpression = lhs;
}
assert comparedExpression != null;
- @NonNls final String newExpression = specifierString +
- "assertNull(" + comparedExpression.getText() + ");";
- PsiReplacementUtil.replaceStatementAndShortenClassNames(statement, newExpression);
+ if (JavaTokenType.EQEQ.equals(binaryExpression.getOperationTokenType())) {
+ newStatement = buildNewStatement("assertNull", element, comparedExpression.getText());
+ }
+ else {
+ newStatement = buildNewStatement("assertNotNull", element, comparedExpression.getText());
+ }
}
else if (isEqualityComparison(expression)) {
final PsiBinaryExpression binaryExpression =
@@ -91,23 +77,16 @@ public class CreateAssertIntention extends Intention {
}
assert comparingExpression != null;
final PsiType type = lhs.getType();
- @NonNls final String newExpression;
if (PsiType.DOUBLE.equals(type) || PsiType.FLOAT.equals(type)) {
- newExpression = specifierString + "assertEquals(" +
- comparedExpression.getText() + ", " +
- comparingExpression.getText() + ", 0.0);";
+ newStatement = buildNewStatement("assertEquals",
+ element, comparedExpression.getText(), comparingExpression.getText(), "0.0");
}
else if (type instanceof PsiPrimitiveType) {
- newExpression = specifierString + "assertEquals(" +
- comparedExpression.getText() + ", " +
- comparingExpression.getText() + ");";
+ newStatement = buildNewStatement("assertEquals", element, comparedExpression.getText(), comparingExpression.getText());
}
else {
- newExpression = specifierString + "assertSame(" +
- comparedExpression.getText() + ", " +
- comparingExpression.getText() + ");";
+ newStatement = buildNewStatement("assertSame", element, comparedExpression.getText(), comparingExpression.getText());
}
- PsiReplacementUtil.replaceStatementAndShortenClassNames(statement, newExpression);
}
else if (isEqualsExpression(expression)) {
final PsiMethodCallExpression call =
@@ -118,26 +97,55 @@ public class CreateAssertIntention extends Intention {
methodExpression.getQualifierExpression();
assert comparedExpression != null;
final PsiExpressionList argList = call.getArgumentList();
- final PsiExpression comparingExpression =
- argList.getExpressions()[0];
- @NonNls final String newExpression;
+ final PsiExpression comparingExpression = argList.getExpressions()[0];
if (comparingExpression instanceof PsiLiteralExpression) {
- newExpression = specifierString + "assertEquals(" +
- comparingExpression.getText() + ", " +
- comparedExpression.getText() + ");";
+ newStatement = buildNewStatement("assertEquals", element, comparingExpression.getText(), comparedExpression.getText());
+ }
+ else {
+ newStatement = buildNewStatement("assertEquals", element, comparedExpression.getText(), comparingExpression.getText());
+ }
+ }
+ else {
+ newStatement = buildNewStatement("assertTrue", element, expression.getText());
+ }
+ PsiReplacementUtil.replaceStatementAndShortenClassNames(statement, newStatement);
+ }
+
+ @NonNls
+ private static String buildNewStatement(@NonNls String memberName, PsiElement context, String... argumentTexts) {
+ final PsiElementFactory factory = JavaPsiFacade.getElementFactory(context.getProject());
+ final StringBuilder builder = new StringBuilder(memberName).append('(');
+ boolean comma = false;
+ for (String argumentText : argumentTexts) {
+ if (comma) {
+ builder.append(',');
}
else {
- newExpression = specifierString + "assertEquals(" +
- comparedExpression.getText() + ", " +
- comparingExpression.getText() + ");";
+ comma = true;
}
- PsiReplacementUtil.replaceStatementAndShortenClassNames(statement, newExpression);
+ builder.append(argumentText);
+ }
+ builder.append(')');
+ final String text = builder.toString();
+
+ final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)factory.createExpressionFromText(text, context);
+ final PsiMethod method = methodCallExpression.resolveMethod();
+ if (method != null || hasStaticImports(context) && ImportUtils.addStaticImport("org.junit.Assert", memberName, context)) {
+ return text + ';';
}
else {
- @NonNls final String newExpression =
- specifierString + "assertTrue(" + expression.getText() + ");";
- PsiReplacementUtil.replaceStatementAndShortenClassNames(statement, newExpression);
+ return "org.junit.Assert." + text + ';';
+ }
+ }
+
+ private static boolean hasStaticImports(PsiElement element) {
+ final PsiFile file = element.getContainingFile();
+ if (!(file instanceof PsiJavaFile)) {
+ return false;
}
+ final PsiJavaFile javaFile = (PsiJavaFile)file;
+ final PsiImportList importList = javaFile.getImportList();
+ return importList != null && importList.getImportStaticStatements().length > 0;
}
private static boolean isEqualsExpression(PsiExpression expression) {
@@ -176,25 +184,16 @@ public class CreateAssertIntention extends Intention {
if (!(expression instanceof PsiBinaryExpression)) {
return false;
}
- final PsiBinaryExpression binaryExpression =
- (PsiBinaryExpression)expression;
+ final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)expression;
final IElementType tokenType = binaryExpression.getOperationTokenType();
- if (!JavaTokenType.EQEQ.equals(tokenType)) {
+ if (!JavaTokenType.EQEQ.equals(tokenType) && !JavaTokenType.NE.equals(tokenType)) {
return false;
}
final PsiExpression lhs = binaryExpression.getLOperand();
- if (isNull(lhs)) {
+ if (ExpressionUtils.isNullLiteral(lhs)) {
return true;
}
- final PsiExpression Rhs = binaryExpression.getROperand();
- return isNull(Rhs);
- }
-
- private static boolean isNull(PsiExpression expression) {
- if (!(expression instanceof PsiLiteralExpression)) {
- return false;
- }
- @NonNls final String text = expression.getText();
- return PsiKeyword.NULL.equals(text);
+ final PsiExpression rhs = binaryExpression.getROperand();
+ return ExpressionUtils.isNullLiteral(rhs);
}
}