'Add junit to classpath' quick-fix moved to junit plugin
authornik <Nikolay.Chashnikov@jetbrains.com>
Wed, 15 Jul 2015 14:12:45 +0000 (17:12 +0300)
committernik <Nikolay.Chashnikov@jetbrains.com>
Wed, 15 Jul 2015 14:13:09 +0000 (17:13 +0300)
java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/OrderEntryFix.java
plugins/junit/src/META-INF/plugin.xml
plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnitUnresolvedReferenceQuickFixProvider.java [new file with mode: 0644]

index ffaa21f23010f5f988cab9f9aafb5142b7e564b3..76389e1debdd77b35c66984694ee1e694aab5070 100644 (file)
@@ -64,7 +64,7 @@ import java.util.Set;
 public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
   private static final String JUNIT4_LIBRARY_NAME = "JUnit4";
 
-  OrderEntryFix() {
+  protected OrderEntryFix() {
   }
 
   @Override
@@ -99,49 +99,6 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
     final Module currentModule = fileIndex.getModuleForFile(classVFile);
     if (currentModule == null) return null;
 
-    if ("TestCase".equals(referenceName) || isAnnotation(psiElement) && isJunitAnnotationName(referenceName, psiElement)) {
-      final boolean isJunit4 = !referenceName.equals("TestCase");
-      @NonNls final String className = isJunit4 ? "org.junit." + referenceName : "junit.framework.TestCase";
-      PsiClass found =
-        JavaPsiFacade.getInstance(project).findClass(className, currentModule.getModuleWithDependenciesAndLibrariesScope(true));
-      if (found != null) return null; //no need to add junit to classpath
-      final OrderEntryFix fix = new OrderEntryFix() {
-        @Override
-        @NotNull
-        public String getText() {
-          return QuickFixBundle.message("orderEntry.fix.add.junit.jar.to.classpath");
-        }
-
-        @Override
-        @NotNull
-        public String getFamilyName() {
-          return getText();
-        }
-
-        @Override
-        public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
-          return !project.isDisposed() && !currentModule.isDisposed();
-        }
-
-        @Override
-        public void invoke(@NotNull Project project, @Nullable Editor editor, PsiFile file) {
-          List<String> jarPaths;
-          String libraryName;
-          if (isJunit4) {
-            jarPaths = JavaSdkUtil.getJUnit4JarPaths();
-            libraryName = JUNIT4_LIBRARY_NAME;
-          }
-          else {
-            jarPaths = Collections.singletonList(JavaSdkUtil.getJunit3JarPath());
-            libraryName = null;
-          }
-          addJarsToRootsAndImportClass(jarPaths, libraryName, currentModule, editor, reference, className);
-        }
-      };
-      registrar.register(fix);
-      return Collections.singletonList((LocalQuickFix)fix);
-    }
-
     if (isAnnotation(psiElement) && AnnotationUtil.isJetbrainsAnnotation(referenceName)) {
       @NonNls final String className = "org.jetbrains.annotations." + referenceName;
       PsiClass found =
@@ -287,26 +244,6 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
     return PsiTreeUtil.getParentOfType(psiElement, PsiAnnotation.class) != null && PsiUtil.isLanguageLevel5OrHigher(psiElement);
   }
 
-  private static boolean isJunitAnnotationName(@NonNls final String referenceName, @NotNull final PsiElement psiElement) {
-    if ("Test".equals(referenceName) || "Ignore".equals(referenceName) || "RunWith".equals(referenceName) ||
-        "Before".equals(referenceName) || "BeforeClass".equals(referenceName) ||
-        "After".equals(referenceName) || "AfterClass".equals(referenceName)) {
-      return true;
-    }
-    final PsiElement parent = psiElement.getParent();
-    if (parent != null && !(parent instanceof PsiAnnotation)) {
-      final PsiReference reference = parent.getReference();
-      if (reference != null) {
-        final String referenceText = parent.getText();
-        if (isJunitAnnotationName(reference.getRangeInElement().substring(referenceText), parent)) {
-          final int lastDot = referenceText.lastIndexOf('.');
-          return lastDot > -1 && referenceText.substring(0, lastDot).equals("org.junit");
-        }
-      }
-    }
-    return false;
-  }
-
   /**
    * @deprecated use {@link #addJarsToRootsAndImportClass} instead
    */
index c8fc9906c8b6765e58f856185d15576ee346f95a..8d4fd1602b24eb96f9b391f4735b814cee0b6ab7 100644 (file)
@@ -60,6 +60,7 @@
     <stacktrace.fold substring="at junit.framework.TestResult"/>
 
     <programRunner implementation="com.intellij.execution.junit.JUnitDebuggerRunner"/>
+    <codeInsight.unresolvedReferenceQuickFixProvider implementation="com.intellij.execution.junit.codeInsight.JUnitUnresolvedReferenceQuickFixProvider"/>
   </extensions>
 
   <extensionPoints>
diff --git a/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnitUnresolvedReferenceQuickFixProvider.java b/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnitUnresolvedReferenceQuickFixProvider.java
new file mode 100644 (file)
index 0000000..2d3a53e
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ * Copyright 2000-2015 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.intellij.execution.junit.codeInsight;
+
+import com.intellij.codeInsight.daemon.QuickFixActionRegistrar;
+import com.intellij.codeInsight.daemon.QuickFixBundle;
+import com.intellij.codeInsight.daemon.impl.quickfix.OrderEntryFix;
+import com.intellij.codeInsight.quickfix.UnresolvedReferenceQuickFixProvider;
+import com.intellij.execution.junit.JUnit3Framework;
+import com.intellij.execution.junit.JUnit4Framework;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.module.Module;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.roots.ProjectFileIndex;
+import com.intellij.openapi.roots.ProjectRootManager;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.psi.*;
+import com.intellij.psi.util.PsiTreeUtil;
+import com.intellij.testIntegration.JavaTestFramework;
+import com.intellij.testIntegration.TestFramework;
+import org.jetbrains.annotations.NonNls;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.List;
+
+/**
+ * @author nik
+ */
+public class JUnitUnresolvedReferenceQuickFixProvider extends UnresolvedReferenceQuickFixProvider<PsiJavaCodeReferenceElement> {
+  @Override
+  public void registerFixes(@NotNull final PsiJavaCodeReferenceElement reference, @NotNull QuickFixActionRegistrar registrar) {
+    final PsiElement psiElement = reference.getElement();
+    @NonNls final String referenceName = reference.getRangeInElement().substring(psiElement.getText());
+
+    Project project = psiElement.getProject();
+    PsiFile containingFile = psiElement.getContainingFile();
+    if (containingFile == null) return;
+
+    final VirtualFile classVFile = containingFile.getVirtualFile();
+    if (classVFile == null) return;
+
+    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
+    final Module currentModule = fileIndex.getModuleForFile(classVFile);
+    if (currentModule == null) return;
+
+    final JavaTestFramework framework;
+    @NonNls final String className;
+    if ("TestCase".equals(referenceName)) {
+      framework = TestFramework.EXTENSION_NAME.findExtension(JUnit3Framework.class);
+      className = "junit.framework.TestCase";
+    }
+    else if (PsiTreeUtil.getParentOfType(psiElement, PsiAnnotation.class) != null && isJunitAnnotationName(referenceName, psiElement)) {
+      framework = TestFramework.EXTENSION_NAME.findExtension(JUnit4Framework.class);
+      className = "org.junit." + referenceName;
+    }
+    else {
+      return;
+    }
+
+    PsiClass found = JavaPsiFacade.getInstance(project).findClass(className, currentModule.getModuleWithDependenciesAndLibrariesScope(true));
+    if (found != null) return;
+
+    final OrderEntryFix fix = new OrderEntryFix() {
+      @Override
+      @NotNull
+      public String getText() {
+        return QuickFixBundle.message("orderEntry.fix.add.junit.jar.to.classpath");
+      }
+
+      @Override
+      @NotNull
+      public String getFamilyName() {
+        return getText();
+      }
+
+      @Override
+      public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
+        return !project.isDisposed() && !currentModule.isDisposed();
+      }
+
+      @Override
+      public void invoke(@NotNull Project project, @Nullable Editor editor, PsiFile file) {
+        List<String> jarPaths = framework.getLibraryPaths();
+        String libraryName = jarPaths.size() == 1 ? null : framework.getName();
+        addJarsToRootsAndImportClass(jarPaths, libraryName, currentModule, editor, reference, className);
+      }
+    };
+    registrar.register(fix);
+  }
+
+  private static boolean isJunitAnnotationName(@NonNls final String referenceName, @NotNull final PsiElement psiElement) {
+    if ("Test".equals(referenceName) || "Ignore".equals(referenceName) || "RunWith".equals(referenceName) ||
+        "Before".equals(referenceName) || "BeforeClass".equals(referenceName) ||
+        "After".equals(referenceName) || "AfterClass".equals(referenceName)) {
+      return true;
+    }
+    final PsiElement parent = psiElement.getParent();
+    if (parent != null && !(parent instanceof PsiAnnotation)) {
+      final PsiReference reference = parent.getReference();
+      if (reference != null) {
+        final String referenceText = parent.getText();
+        if (isJunitAnnotationName(reference.getRangeInElement().substring(referenceText), parent)) {
+          final int lastDot = referenceText.lastIndexOf('.');
+          return lastDot > -1 && referenceText.substring(0, lastDot).equals("org.junit");
+        }
+      }
+    }
+    return false;
+  }
+
+  @NotNull
+  @Override
+  public Class<PsiJavaCodeReferenceElement> getReferenceClass() {
+    return PsiJavaCodeReferenceElement.class;
+  }
+}