convert junit testcase to test-ng improved and fixed according (IDEA-138767)

This commit is contained in:
Dmitry Batkovich
2015-11-04 11:13:08 +03:00
parent 4f6d7e2079
commit b6fcfb989c
14 changed files with 270 additions and 124 deletions
@@ -11,6 +11,7 @@ import com.intellij.structuralsearch.MatchOptions;
import com.intellij.structuralsearch.plugin.replace.ReplaceOptions;
import com.intellij.structuralsearch.plugin.replace.impl.Replacer;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
@@ -81,14 +82,21 @@ public class TypeConversionDescriptor extends TypeConversionDescriptorBase {
@Override
public PsiExpression replace(PsiExpression expression) {
if (getExpression() != null) expression = getExpression();
final Project project = expression.getProject();
return replaceExpression(expression, getStringToReplace(), getReplaceByString());
}
@NotNull
public static PsiExpression replaceExpression(@NotNull PsiExpression expression,
String stringToReplace,
String replaceByString) {
Project project = expression.getProject();
final ReplaceOptions options = new ReplaceOptions();
final MatchOptions matchOptions = options.getMatchOptions();
matchOptions.setFileType(StdFileTypes.JAVA);
final Replacer replacer = new Replacer(project, null);
final String replacement = replacer.testReplace(expression.getText(), getStringToReplace(), getReplaceByString(), options);
final String replacement = replacer.testReplace(expression.getText(), stringToReplace, replaceByString, options);
return (PsiExpression)JavaCodeStyleManager.getInstance(project).shortenClassReferences(expression.replace(
JavaPsiFacade.getInstance(project).getElementFactory().createExpressionFromText(replacement, expression)));
JavaPsiFacade.getInstance(project).getElementFactory().createExpressionFromText(replacement, expression)));
}
@Override
@@ -24,12 +24,20 @@ import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.util.PsiElementFilter;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.typeMigration.TypeConversionDescriptor;
import com.intellij.util.Function;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.HashMap;
import com.theoryinpractice.testng.util.TestNGUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map;
/**
* @author Hani Suleiman Date: Aug 3, 2005 Time: 3:34:56 AM
*/
@@ -37,8 +45,19 @@ public class JUnitConvertTool extends BaseJavaLocalInspectionTool {
private static final Logger LOG = Logger.getInstance("TestNG QuickFix");
private static final String DISPLAY_NAME = "Convert JUnit Tests to TestNG";
private static final Map<String, String> ANNOTATIONS_MAP;
public static final String QUICKFIX_NAME = "Convert TestCase to TestNG";
static {
ANNOTATIONS_MAP = new HashMap<String, String>();
ANNOTATIONS_MAP.put("org.junit.Test", "@org.testng.annotations.Test");
ANNOTATIONS_MAP.put("org.junit.BeforeClass", "@org.testng.annotations.BeforeClass");
ANNOTATIONS_MAP.put("org.junit.Before", "@org.testng.annotations.BeforeMethod");
ANNOTATIONS_MAP.put("org.junit.AfterClass", "@org.testng.annotations.AfterClass");
ANNOTATIONS_MAP.put("org.junit.After", "@org.testng.annotations.AfterMethod");
}
@NotNull
@Override
public String getGroupDisplayName() {
@@ -91,7 +110,11 @@ public class JUnitConvertTool extends BaseJavaLocalInspectionTool {
final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
final PsiJavaFile javaFile = (PsiJavaFile)psiClass.getContainingFile();
final List<PsiElement> convertedElements = new SmartList<PsiElement>();
for (PsiMethod method : psiClass.getMethods()) {
final PsiMethodCallExpression[] methodCalls = getTestCaseCalls(method);
if (method.isConstructor()) {
convertJUnitConstructor(method);
}
@@ -101,94 +124,55 @@ public class JUnitConvertTool extends BaseJavaLocalInspectionTool {
}
else {
if (TestNGUtil.containsJunitAnnotions(method)) {
convertJunitAnnotions(factory, method);
convertedElements.addAll(convertJunitAnnotations(factory, method));
} else {
addMethodAnnotations(factory, method);
convertedElements.add(addMethodAnnotations(factory, method));
}
}
}
final PsiMethodCallExpression[] methodCalls = getTestCaseCalls(method);
for (PsiMethodCallExpression methodCall : methodCalls) {
PsiMethod assertMethod = methodCall.resolveMethod();
if (assertMethod == null) {
continue;
}
PsiAssertStatement assertStatement = null;
@NonNls String methodName = assertMethod.getName();
PsiExpression[] expressions = methodCall.getArgumentList().getExpressions();
final PsiStatement methodCallStatement = PsiTreeUtil.getParentOfType(methodCall, PsiStatement.class);
LOG.assertTrue(methodCallStatement != null);
if ("assertTrue".equals(methodName) || "assertFalse".equals(methodName)) {
if (expressions.length == 1) {
assertStatement = createAssert(factory, null, methodCall);
final PsiExpression assertCondition = assertStatement.getAssertCondition();
LOG.assertTrue(assertCondition != null);
assertCondition.replace(expressions[0]);
}
else if (expressions.length == 2) {
assertStatement = createAssert(factory, expressions[0], methodCall);
final PsiExpression assertCondition = assertStatement.getAssertCondition();
LOG.assertTrue(assertCondition != null);
assertCondition.replace(expressions[1]);
}
if ("assertFalse".equals(methodName) && assertStatement != null) {
PsiExpression assertCondition = assertStatement.getAssertCondition();
LOG.assertTrue(assertCondition != null);
assertCondition.replace(factory.createExpressionFromText("!(" + assertCondition.getText() + ')',
PsiTreeUtil.getParentOfType(assertCondition,
PsiMethodCallExpression.class)));
}
}
else if ("assertNull".equals(methodName) || "assertNotNull".equals(methodName)) {
String operator = "assertNull".equals(methodName) ? "==" : "!=";
if (expressions.length == 1) {
assertStatement = createAssert(factory, null, methodCall);
PsiExpression expression =
factory.createExpressionFromText(expressions[0].getText() + ' ' + operator + " null", assertStatement);
final PsiExpression assertCondition = assertStatement.getAssertCondition();
LOG.assertTrue(assertCondition != null);
assertCondition.replace(expression);
}
else if (expressions.length == 2) {
assertStatement = createAssert(factory, expressions[0], methodCall.getParent());
PsiExpression expression =
factory.createExpressionFromText(expressions[1].getText() + ' ' + operator + " null", assertStatement);
final PsiExpression assertCondition = assertStatement.getAssertCondition();
LOG.assertTrue(assertCondition != null);
assertCondition.replace(expression);
}
final String qualifierTemplate = methodCall.getMethodExpression().getQualifierExpression() != null ? "$qualifier$." : "";
final String searchTemplate;
final String replaceTemplate;
if ("assertNull".equals(methodName) || "assertNotNull".equals(methodName) || "assertTrue".equals(methodName) || "assertFalse".equals(methodName)) {
boolean hasMessage = expressions.length == 2;
searchTemplate = qualifierTemplate + "$method$($object$ " + (hasMessage ? ",$msg$" : "") + ")";
replaceTemplate = "org.testng.Assert.$method$(" + (hasMessage ? "$msg$," : "") + "$object$)";
}
else if ("fail".equals(methodName)) {
if (expressions.length == 0) {
assertStatement = createAssert(factory, null, methodCall);
}
else if (expressions.length == 1) {
assertStatement = createAssert(factory, expressions[0], methodCall);
}
boolean hasMessage = expressions.length == 1;
searchTemplate = qualifierTemplate + "$method$(" + (hasMessage ? "$msg$" : "") + ")";
replaceTemplate = "org.testng.Assert.$method$(" + (hasMessage ? "$msg$" : "") + ")";
}
else if ("assertThat".equals(methodName)) {
String paramTemplate = (expressions.length == 3 ? "$msg$," : "") + "$actual$, $matcher$";
searchTemplate = qualifierTemplate + "assertThat(" + paramTemplate + ")";
replaceTemplate = "org.hamcrest.MatcherAssert.assertThat(" + paramTemplate +")";
}
else {
//if it's a 3 arg, the error message goes at the end
PsiElement inserted = null;
if (expressions.length == 2) {
final PsiExpression qualifierExpression = methodCall.getMethodExpression().getQualifierExpression();
final String text = "org.testng." + (qualifierExpression == null ? "Assert." : "") + methodCall.getText() + ";";
inserted = methodCallStatement
.replace(factory.createStatementFromText(text, methodCall.getParent()));
}
else if (expressions.length == 3) {
@NonNls String call = "org.testng.Assert." + methodName + '(' + expressions[2].getText() + ", " + expressions[1].getText() +
", " + expressions[0].getText() + ");";
inserted = methodCallStatement.replace(factory.createStatementFromText(call, methodCall.getParent()));
}
if (inserted != null) {
JavaCodeStyleManager.getInstance(project).shortenClassReferences(inserted);
boolean hasMessage = hasMessage(methodCall);
if ((hasMessage && expressions.length == 4) || (!hasMessage && expressions.length == 3)) {
searchTemplate = qualifierTemplate + "$method$";
replaceTemplate = "org.testng.AssertJUnit.$method$";
} else {
String replaceMethodWildCard = "$method$";
if (methodName.equals("assertArrayEquals")) {
replaceMethodWildCard = "assertEquals";
}
searchTemplate = qualifierTemplate + "$method$(" + (hasMessage ? "$msg$, " : "") + "$expected$, $actual$" + ")";
replaceTemplate = "org.testng.Assert." + replaceMethodWildCard + "($actual$, $expected$ " + (hasMessage ? ", $msg$" : "") + ")";
}
}
if (assertStatement != null) {
methodCallStatement.replace(assertStatement);
}
convertedElements.add(TypeConversionDescriptor.replaceExpression(methodCall, searchTemplate, replaceTemplate));
}
}
final PsiClass superClass = psiClass.getSuperClass();
@@ -199,38 +183,47 @@ public class JUnitConvertTool extends BaseJavaLocalInspectionTool {
element.delete();
}
}
JavaCodeStyleManager.getInstance(project).optimizeImports(javaFile);//delete unused imports
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
codeStyleManager.optimizeImports(javaFile);//delete unused imports
codeStyleManager.shortenClassReferences(javaFile);
}
catch (IncorrectOperationException e) {
LOG.error("Error converting testcase", e);
}
}
private static void convertJunitAnnotions(PsiElementFactory factory, PsiMethod method) throws IncorrectOperationException {
PsiAnnotation[] annotations = method.getModifierList().getAnnotations();
for (PsiAnnotation annotation : annotations) {
PsiAnnotation newAnnotation = null;
if ("org.junit.Test".equals(annotation.getQualifiedName())) {
newAnnotation = factory.createAnnotationFromText("@org.testng.annotations.Test", method);
}
else if ("org.junit.BeforeClass".equals(annotation.getQualifiedName())) {
newAnnotation = factory.createAnnotationFromText("@org.testng.annotations.BeforeClass", method);
}
else if ("org.junit.Before".equals(annotation.getQualifiedName())) {
newAnnotation = factory.createAnnotationFromText("@org.testng.annotations.BeforeMethod", method);
}
else if ("org.junit.AfterClass".equals(annotation.getQualifiedName())) {
newAnnotation = factory.createAnnotationFromText("@org.testng.annotations.AfterClass", method);
}
else if ("org.junit.After".equals(annotation.getQualifiedName())) {
newAnnotation = factory.createAnnotationFromText("@org.testng.annotations.AfterMethod", method);
}
if (newAnnotation != null) {
JavaCodeStyleManager.getInstance(annotation.getProject()).shortenClassReferences(annotation.replace(newAnnotation));
private static boolean hasMessage(PsiMethodCallExpression expression) {
final PsiExpression[] expressions = expression.getArgumentList().getExpressions();
if (expressions.length == 4) {
return true;
}
final PsiMethod method = expression.resolveMethod();
LOG.assertTrue(method != null);
for (PsiParameter parameter : method.getParameterList().getParameters()) {
final PsiType type = parameter.getType();
if (type instanceof PsiClassType) {
final PsiClass resolvedClass = ((PsiClassType)type).resolve();
if (resolvedClass != null && CommonClassNames.JAVA_LANG_STRING.equals(resolvedClass.getQualifiedName())) {
return true;
}
}
}
return false;
}
private static List<PsiElement> convertJunitAnnotations(final PsiElementFactory factory, final PsiMethod method) throws IncorrectOperationException {
PsiAnnotation[] annotations = method.getModifierList().getAnnotations();
return ContainerUtil.mapNotNull(annotations, new Function<PsiAnnotation, PsiElement>() {
@Override
public PsiElement fun(PsiAnnotation annotation) {
final String testNgAnnotation = ANNOTATIONS_MAP.get(annotation.getQualifiedName());
if (testNgAnnotation != null) {
final PsiAnnotation newAnnotation = factory.createAnnotationFromText("@org.testng.annotations.Test", method);
return annotation.replace(newAnnotation);
}
return null;
}
});
}
private static void convertJUnitConstructor(PsiMethod method) {
@@ -336,7 +329,7 @@ public class JUnitConvertTool extends BaseJavaLocalInspectionTool {
}
}
private static void addMethodAnnotations(PsiElementFactory factory, PsiMethod method) throws IncorrectOperationException {
private static PsiElement addMethodAnnotations(PsiElementFactory factory, PsiMethod method) throws IncorrectOperationException {
PsiAnnotation annotation = null;
if (method.getName().startsWith("test")) {
annotation = factory.createAnnotationFromText("@org.testng.annotations.Test", method);
@@ -348,24 +341,9 @@ public class JUnitConvertTool extends BaseJavaLocalInspectionTool {
annotation = factory.createAnnotationFromText("@org.testng.annotations.AfterMethod", method);
}
if (annotation != null) {
JavaCodeStyleManager.getInstance(annotation.getProject()).shortenClassReferences(method.getModifierList().addAfter(annotation, null));
return method.getModifierList().addAfter(annotation, null);
}
}
private static PsiAssertStatement createAssert(PsiElementFactory factory, PsiExpression description, PsiElement context)
throws IncorrectOperationException {
PsiAssertStatement assertStatement;
if (description == null) {
assertStatement = (PsiAssertStatement)factory.createStatementFromText("assert false;", context.getParent());
return assertStatement;
}
else {
assertStatement = (PsiAssertStatement)factory.createStatementFromText("assert false : \"x\";", context.getParent());
final PsiExpression assertDescription = assertStatement.getAssertDescription();
assert assertDescription != null;
assertDescription.replace(description);
}
return assertStatement;
return null;
}
}
}
@@ -0,0 +1,23 @@
import org.testng.Assert;
import org.testng.annotations.Test;
public class SampleTest {
@Test
public void differentAssertions() {
Assert.assertEquals(true ? new Integer(1) : null, new Integer(1), "message");
Assert.assertEquals(true ? new Integer(1) : null, new Integer(1));
Assert.assertNotEquals(new Integer(2), new Integer(1), "message");
Assert.assertNotEquals(new Integer(2), new Integer(1));
Assert.assertEquals(true ? new long[0] : null, new long[0], "message");
Assert.assertEquals(true ? new long[0] : null, new long[0]);
Assert.assertEquals(true ? 1L : 0, 1L);
Assert.assertEquals(true ? 1L : 0, 1L, "message");
Assert.assertSame(true ? Integer.valueOf(1) : null, (Object) Integer.valueOf(1), "message");
Assert.assertSame(true ? Integer.valueOf(1) : null, (Object) Integer.valueOf(1));
Assert.assertNotSame(true ? new Object() : null, new Object(), "message");
Assert.assertNotSame(true ? new Object() : null, new Object());
Assert.assertEquals(true ? new Object[0] : null, new Object[0], "message");
Assert.assertEquals(true ? new Object[0] : null, new Object[0]);
}
}
@@ -0,0 +1,14 @@
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
public class SampleTest {
@Test
public void differentAssertions() {
AssertJUnit.assertArrayEquals("message", new double[0], true ? new double[0] : null, 0d);
AssertJUnit.assertArrayEquals(new double[0], true ? new double[0] : null, 0d);
AssertJUnit.assertEquals("message", 0d, 1d, 2d);
AssertJUnit.assertEquals(1d, 2d, 0d);
}
}
+10 -10
View File
@@ -1,17 +1,17 @@
import org.testng.Assert;
import org.testng.annotations.Test;
public class <caret>Testt {
public class Testt {
@Test
public void test() {
Assert.assertEquals("description", "2", "1");
assert "" != null;
assert !(false);
assert true : "true";
Assert.assertNotSame("2", "1", "not same");
assert null == null;
assert null == null : "description";
Assert.assertSame("2", "1", "description");
assert false : "fail";
Assert.assertEquals("description", "2", "1");
Assert.assertNotNull("");
Assert.assertFalse(false);
Assert.assertTrue(true, "true");
Assert.assertNotSame("2", "1", "not same");
Assert.assertNull(null);
Assert.assertNull(null, "description");
Assert.assertSame("2", "1", "description");
Assert.fail("fail");
}
}
@@ -0,0 +1,11 @@
import org.hamcrest.Matchers;
import org.testng.annotations.Test;
public class SampleTest {
@Test
public void differentAssertions() {
org.hamcrest.MatcherAssert.assertThat(1, Matchers.is(1));
org.hamcrest.MatcherAssert.assertThat("reason", 1, Matchers.is(1));
}
}
@@ -0,0 +1,17 @@
import org.testng.Assert;
import org.testng.annotations.Test;
public class SampleTest {
@Test
public void differentAssertions() {
Assert.assertTrue(true, "message");
Assert.assertTrue(true);
Assert.assertFalse(false, "message");
Assert.assertFalse(false);
Assert.assertNotNull(new Object(), "message");
Assert.assertNotNull(new Object());
Assert.assertNull(null, "message");
Assert.assertNull(null);
}
}
@@ -0,0 +1,28 @@
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
public class SampleT<caret>est {
@Test
public void differentAssertions() {
assertEquals("message", new Integer(1), true ? new Integer(1) : null);
assertEquals(new Integer(1), true ? new Integer(1) : null);
assertNotEquals("message", new Integer(1), new Integer(2));
assertNotEquals(new Integer(1), new Integer(2));
assertArrayEquals("message", new long[0], true ? new long[0] : null);
assertArrayEquals(new long[0], true ? new long[0] : null);
assertEquals(1L, true ? 1L : 0);
assertEquals("message", 1L, true ? 1L : 0);
assertSame("message", (Object) Integer.valueOf(1), true ? Integer.valueOf(1) : null);
assertSame((Object) Integer.valueOf(1), true ? Integer.valueOf(1) : null);
assertNotSame("message", new Object(), true ? new Object() : null);
assertNotSame(new Object(), true ? new Object() : null);
assertEquals("message", new Object[0], true ? new Object[0] : null);
assertEquals(new Object[0], true ? new Object[0] : null);
}
}
@@ -0,0 +1,16 @@
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class Sample<caret>Test {
@Test
public void differentAssertions() {
assertArrayEquals("message", new double[0], true ? new double[0] : null, 0d);
assertArrayEquals(new double[0], true ? new double[0] : null, 0d);
assertEquals("message", 0d, 1d, 2d);
assertEquals(1d, 2d, 0d);
}
}
@@ -0,0 +1,13 @@
import org.hamcrest.Matchers;
import org.junit.Test;
import static org.junit.Assert.assertThat;
public class Sample<caret>Test {
@Test
public void differentAssertions() {
assertThat(1, Matchers.is(1));
assertThat("reason", 1, Matchers.is(1));
}
}
@@ -0,0 +1,21 @@
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class Sample<caret>Test {
@Test
public void differentAssertions() {
assertTrue("message", true);
assertTrue(true);
assertFalse("message", false);
assertFalse(false);
assertNotNull("message", new Object());
assertNotNull(new Object());
assertNull("message", null);
assertNull(null);
}
}
@@ -56,7 +56,7 @@ public abstract class BaseTestNGInspectionsTest extends JavaCodeInsightFixtureTe
break;
}
}
Assert.assertNotNull(resultAction);
Assert.assertNotNull(resultAction, "action isn't found");
myFixture.launchAction(resultAction);
myFixture.checkResultByFile(AFTER + testName + ".java");
}
@@ -40,6 +40,22 @@ public class ConvertJUnitInspectionTest extends BaseTestNGInspectionsTest {
doTest();
}
public void testUnaryAssertions() {
doTest();
}
public void testBinaryAssertions() {
doTest();
}
public void testIncompatible() {
doTest();
}
public void testDelta() {
doTest();
}
@Override
protected String getBasePath() {
return PluginPathManager.getPluginHomePathRelative("testng") + "/testData/junit";
+1
View File
@@ -25,6 +25,7 @@
<orderEntry type="module" module-name="java-indexing-api" />
<orderEntry type="module" module-name="xml" scope="TEST" />
<orderEntry type="module" module-name="smRunner" exported="" />
<orderEntry type="module" module-name="typeMigration" />
</component>
<component name="copyright">
<Base>