/*
- * Copyright 2008-2012 Bas Leijdekkers
+ * Copyright 2008-2016 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.application.WriteAction;
-import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
+import com.intellij.psi.util.InheritanceUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
}
final PsiMember member = (PsiMember)parent;
final PsiClass containingClass = member.getContainingClass();
- if (containingClass == null) {
- return;
- }
- final PsiReferenceList extendsList = containingClass.getExtendsList();
- if (extendsList == null) {
- return;
- }
- final PsiMethod[] methods = containingClass.getMethods();
- for (PsiMethod method : methods) {
- @NonNls final String name = method.getName();
- if (method.hasModifierProperty(PsiModifier.STATIC)) {
- continue;
- }
- final PsiType returnType = method.getReturnType();
- if (!PsiType.VOID.equals(returnType)) {
- continue;
- }
+ convertJUnit3ClassToJUnit4(containingClass);
+ }
+ }
+
+ public static void convertJUnit3ClassToJUnit4(PsiClass containingClass) {
+ if (containingClass == null) {
+ return;
+ }
+ final PsiReferenceList extendsList = containingClass.getExtendsList();
+ if (extendsList == null) {
+ return;
+ }
+ for (PsiMethod method : containingClass.getMethods()) {
+ @NonNls final String name = method.getName();
+ if (!method.hasModifierProperty(PsiModifier.STATIC) &&
+ PsiType.VOID.equals(method.getReturnType()) &&
+ method.getParameterList().getParametersCount() == 0) {
final PsiModifierList modifierList = method.getModifierList();
if (name.startsWith("test")) {
addAnnotationIfNotPresent(modifierList, "org.junit.Test");
transformSetUpOrTearDownMethod(method);
addAnnotationIfNotPresent(modifierList, "org.junit.After");
}
- method.accept(new MethodCallModifier());
- }
- final PsiJavaCodeReferenceElement[] referenceElements = extendsList.getReferenceElements();
- for (PsiJavaCodeReferenceElement referenceElement : referenceElements) {
- referenceElement.delete();
}
+ method.accept(new MethodCallModifier());
+ }
+ final PsiJavaCodeReferenceElement[] referenceElements = extendsList.getReferenceElements();
+ for (PsiJavaCodeReferenceElement referenceElement : referenceElements) {
+ referenceElement.delete();
}
+ }
- private static void addAnnotationIfNotPresent(PsiModifierList modifierList, String qualifiedAnnotationName) {
- if (modifierList.findAnnotation(qualifiedAnnotationName) != null) {
- return;
- }
- final PsiAnnotation annotation = modifierList.addAnnotation(qualifiedAnnotationName);
- final Project project = modifierList.getProject();
- final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
- codeStyleManager.shortenClassReferences(annotation);
+ private static void addAnnotationIfNotPresent(PsiModifierList modifierList, String qualifiedAnnotationName) {
+ if (modifierList.findAnnotation(qualifiedAnnotationName) != null) {
+ return;
}
+ final PsiAnnotation annotation = modifierList.addAnnotation(qualifiedAnnotationName);
+ final Project project = modifierList.getProject();
+ final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
+ codeStyleManager.shortenClassReferences(annotation);
+ }
- private static void transformSetUpOrTearDownMethod(PsiMethod method) {
- final PsiModifierList modifierList = method.getModifierList();
- if (modifierList.hasModifierProperty(PsiModifier.PROTECTED)) {
- modifierList.setModifierProperty(PsiModifier.PROTECTED, false);
- }
- if (!modifierList.hasModifierProperty(PsiModifier.PUBLIC)) {
- modifierList.setModifierProperty(PsiModifier.PUBLIC, true);
- }
- final PsiAnnotation overrideAnnotation = modifierList.findAnnotation("java.lang.Override");
- if (overrideAnnotation != null) {
- overrideAnnotation.delete();
- }
- method.accept(new SuperLifeCycleCallRemover(method.getName()));
+ private static void transformSetUpOrTearDownMethod(PsiMethod method) {
+ final PsiModifierList modifierList = method.getModifierList();
+ if (modifierList.hasModifierProperty(PsiModifier.PROTECTED)) {
+ modifierList.setModifierProperty(PsiModifier.PROTECTED, false);
}
+ if (!modifierList.hasModifierProperty(PsiModifier.PUBLIC)) {
+ modifierList.setModifierProperty(PsiModifier.PUBLIC, true);
+ }
+ final PsiAnnotation overrideAnnotation = modifierList.findAnnotation("java.lang.Override");
+ if (overrideAnnotation != null) {
+ overrideAnnotation.delete();
+ }
+ method.accept(new SuperLifeCycleCallRemover(method.getName()));
+ }
- private static class SuperLifeCycleCallRemover extends JavaRecursiveElementVisitor {
+ private static class SuperLifeCycleCallRemover extends JavaRecursiveElementVisitor {
- @NotNull private final String myLifeCycleMethodName;
+ @NotNull private final String myLifeCycleMethodName;
- private SuperLifeCycleCallRemover(@NotNull String lifeCycleMethodName) {
- myLifeCycleMethodName = lifeCycleMethodName;
- }
+ private SuperLifeCycleCallRemover(@NotNull String lifeCycleMethodName) {
+ myLifeCycleMethodName = lifeCycleMethodName;
+ }
- @Override
- public void visitMethodCallExpression(PsiMethodCallExpression expression) {
- super.visitMethodCallExpression(expression);
- final PsiReferenceExpression methodExpression = expression.getMethodExpression();
- final String methodName = methodExpression.getReferenceName();
- if (!myLifeCycleMethodName.equals(methodName)) {
- return;
- }
- final PsiExpression target = methodExpression.getQualifierExpression();
- if (!(target instanceof PsiSuperExpression)) {
- return;
- }
- expression.delete();
+ @Override
+ public void visitMethodCallExpression(PsiMethodCallExpression expression) {
+ super.visitMethodCallExpression(expression);
+ final PsiReferenceExpression methodExpression = expression.getMethodExpression();
+ final String methodName = methodExpression.getReferenceName();
+ if (!myLifeCycleMethodName.equals(methodName)) {
+ return;
+ }
+ final PsiExpression target = methodExpression.getQualifierExpression();
+ if (!(target instanceof PsiSuperExpression)) {
+ return;
}
+ expression.delete();
}
+ }
- private static class MethodCallModifier extends JavaRecursiveElementVisitor {
+ private static class MethodCallModifier extends JavaRecursiveElementVisitor {
- @Override
- public void visitMethodCallExpression(PsiMethodCallExpression expression) {
- super.visitMethodCallExpression(expression);
- final PsiReferenceExpression methodExpression = expression.getMethodExpression();
- if (methodExpression.getQualifierExpression() != null) {
- return;
- }
- final PsiMethod method = expression.resolveMethod();
- if (method == null) {
- return;
- }
- final PsiClass aClass = method.getContainingClass();
- if (aClass == null) {
- return;
- }
- final String name = aClass.getQualifiedName();
- if (!"junit.framework.Assert".equals(name)) {
- return;
- }
- @NonNls final String newExpressionText = "org.junit.Assert." + expression.getText();
- final Project project = expression.getProject();
- final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
- final PsiExpression newExpression = factory.createExpressionFromText(newExpressionText, expression);
- final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
- final PsiElement replacedExpression = expression.replace(newExpression);
- codeStyleManager.shortenClassReferences(replacedExpression);
+ @Override
+ public void visitMethodCallExpression(PsiMethodCallExpression expression) {
+ super.visitMethodCallExpression(expression);
+ final PsiReferenceExpression methodExpression = expression.getMethodExpression();
+ if (methodExpression.getQualifierExpression() != null) {
+ return;
+ }
+ final PsiMethod method = expression.resolveMethod();
+ if (method == null) {
+ return;
+ }
+ final PsiClass aClass = method.getContainingClass();
+ if (aClass == null) {
+ return;
}
+ if (!InheritanceUtil.isInheritor(aClass, "junit.framework.Assert")) {
+ return;
+ }
+ @NonNls final String newExpressionText = "org.junit.Assert." + expression.getText();
+ final Project project = expression.getProject();
+ final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
+ final PsiExpression newExpression = factory.createExpressionFromText(newExpressionText, expression);
+ final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
+ final PsiElement replacedExpression = expression.replace(newExpression);
+ codeStyleManager.shortenClassReferences(replacedExpression);
}
}
--- /dev/null
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 com.siyeh.ig.fixes.junit;
+
+import com.siyeh.InspectionGadgetsBundle;
+import com.siyeh.ig.IGQuickFixesTestCase;
+import com.siyeh.ig.junit.JUnit4AnnotatedMethodInJUnit3TestCaseInspection;
+
+/**
+ * @author Bas Leijdekkers
+ */
+public class ConvertToJUnit4FixTest extends IGQuickFixesTestCase {
+
+ public void testOtherMethods() { doTest(InspectionGadgetsBundle.message("convert.junit3.test.class.quickfix", getTestName(false))); }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ myFixture.enableInspections(new JUnit4AnnotatedMethodInJUnit3TestCaseInspection());
+ myRelativePath = "junit/convert_to_junit4";
+
+ myFixture.addClass("package org.junit;" +
+ "public class Assert {" +
+ " public static void assertTrue(boolean condition) {}" +
+ " public static void assertFalse(boolean condition) {}" +
+ "}");
+ myFixture.addClass("package org.junit;" +
+ "@Retention(RetentionPolicy.RUNTIME)" +
+ "@Target({ElementType.METHOD})" +
+ "public @interface Test {}");
+ myFixture.addClass("package junit.framework;" +
+ "public class Assert {" +
+ " public static void assertTrue(boolean condition) {}" +
+ " public static void assertFalse(String message, boolean condition) {}" +
+ "}");
+ myFixture.addClass("package junit.framework;" +
+ "public abstract class TestCase extends Assert {}");
+ }
+}
/*
- * Copyright 2009-2012 Bas Leijdekkers
+ * Copyright 2009-2016 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
package com.siyeh.ipp.junit;
-import com.intellij.openapi.project.Project;
-import com.intellij.psi.*;
-import com.intellij.psi.codeStyle.JavaCodeStyleManager;
+import com.intellij.psi.PsiClass;
+import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
+import com.siyeh.ig.junit.JUnit4AnnotatedMethodInJUnit3TestCaseInspection;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
-import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class ConvertJUnit3TestCaseToJUnit4Intention extends Intention {
return;
}
final PsiClass aClass = (PsiClass)parent;
- final PsiReferenceList extendsList = aClass.getExtendsList();
- if (extendsList == null) {
- return;
- }
- final PsiMethod[] methods = aClass.getMethods();
- for (PsiMethod method : methods) {
- @NonNls final String name = method.getName();
- if (method.hasModifierProperty(PsiModifier.STATIC)) {
- continue;
- }
- final PsiType returnType = method.getReturnType();
- if (!PsiType.VOID.equals(returnType)) {
- continue;
- }
- final PsiModifierList modifierList = method.getModifierList();
- if (name.startsWith("test")) {
- addAnnotationIfNotPresent(modifierList, "org.junit.Test");
- }
- else if (name.equals("setUp")) {
- transformSetUpOrTearDownMethod(method);
- addAnnotationIfNotPresent(modifierList, "org.junit.Before");
- }
- else if (name.equals("tearDown")) {
- transformSetUpOrTearDownMethod(method);
- addAnnotationIfNotPresent(modifierList, "org.junit.After");
- }
- method.accept(new MethodCallModifier());
- }
- final PsiJavaCodeReferenceElement[] referenceElements = extendsList.getReferenceElements();
- for (PsiJavaCodeReferenceElement referenceElement : referenceElements) {
- referenceElement.delete();
- }
- }
-
- private static void addAnnotationIfNotPresent(PsiModifierList modifierList, String qualifiedAnnotationName) {
- if (modifierList.findAnnotation(qualifiedAnnotationName) != null) {
- return;
- }
- final PsiAnnotation annotation = modifierList.addAnnotation(qualifiedAnnotationName);
- final Project project = modifierList.getProject();
- final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
- codeStyleManager.shortenClassReferences(annotation);
- }
-
- private static void transformSetUpOrTearDownMethod(PsiMethod method) {
- final PsiModifierList modifierList = method.getModifierList();
- if (modifierList.hasModifierProperty(PsiModifier.PROTECTED)) {
- modifierList.setModifierProperty(PsiModifier.PROTECTED, false);
- }
- if (!modifierList.hasModifierProperty(PsiModifier.PUBLIC)) {
- modifierList.setModifierProperty(PsiModifier.PUBLIC, true);
- }
- final PsiAnnotation overrideAnnotation = modifierList.findAnnotation("java.lang.Override");
- if (overrideAnnotation != null) {
- overrideAnnotation.delete();
- }
- method.accept(new SuperLifeCycleCallRemover(method.getName()));
- }
-
- private static class SuperLifeCycleCallRemover extends JavaRecursiveElementVisitor {
-
- @NotNull private final String myLifeCycleMethodName;
-
- private SuperLifeCycleCallRemover(@NotNull String lifeCycleMethodName) {
- myLifeCycleMethodName = lifeCycleMethodName;
- }
-
- @Override
- public void visitMethodCallExpression(PsiMethodCallExpression expression) {
- super.visitMethodCallExpression(expression);
- final PsiReferenceExpression methodExpression = expression.getMethodExpression();
- final String methodName = methodExpression.getReferenceName();
- if (!myLifeCycleMethodName.equals(methodName)) {
- return;
- }
- final PsiExpression target = methodExpression.getQualifierExpression();
- if (!(target instanceof PsiSuperExpression)) {
- return;
- }
- expression.delete();
- }
- }
-
- private static class MethodCallModifier extends JavaRecursiveElementVisitor {
-
- @Override
- public void visitMethodCallExpression(PsiMethodCallExpression expression) {
- super.visitMethodCallExpression(expression);
- final PsiReferenceExpression methodExpression = expression.getMethodExpression();
- if (methodExpression.getQualifierExpression() != null) {
- return;
- }
- final PsiMethod method = expression.resolveMethod();
- if (method == null) {
- return;
- }
- final PsiClass aClass = method.getContainingClass();
- if (aClass == null) {
- return;
- }
- final String name = aClass.getQualifiedName();
- if (!"junit.framework.Assert".equals(name)) {
- return;
- }
- @NonNls final String newExpressionText = "org.junit.Assert." + expression.getText();
- final Project project = expression.getProject();
- final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
- final PsiExpression newExpression = factory.createExpressionFromText(newExpressionText, expression);
- final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
- final PsiElement replacedExpression = expression.replace(newExpression);
- codeStyleManager.shortenClassReferences(replacedExpression);
- }
+ JUnit4AnnotatedMethodInJUnit3TestCaseInspection.convertJUnit3ClassToJUnit4(aClass);
}
}