execution:api: run test method of an abstract class in inheritors
authoranna <Anna.Kozlova@jetbrains.com>
Tue, 17 Jan 2012 13:25:10 +0000 (14:25 +0100)
committeranna <Anna.Kozlova@jetbrains.com>
Tue, 17 Jan 2012 15:41:51 +0000 (16:41 +0100)
java/execution/impl/src/com/intellij/execution/junit/InheritorChooser.java [new file with mode: 0644]
plugins/junit/src/com/intellij/execution/junit/TestMethodConfigurationProducer.java

diff --git a/java/execution/impl/src/com/intellij/execution/junit/InheritorChooser.java b/java/execution/impl/src/com/intellij/execution/junit/InheritorChooser.java
new file mode 100644 (file)
index 0000000..e68e064
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2000-2012 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;
+
+import com.intellij.execution.actions.ConfigurationContext;
+import com.intellij.ide.util.PsiClassListCellRenderer;
+import com.intellij.openapi.progress.ProgressManager;
+import com.intellij.openapi.ui.popup.JBPopupFactory;
+import com.intellij.psi.PsiClass;
+import com.intellij.psi.PsiMethod;
+import com.intellij.psi.PsiModifier;
+import com.intellij.psi.search.searches.ClassInheritorsSearch;
+import com.intellij.ui.ColoredListCellRenderer;
+import com.intellij.ui.components.JBList;
+import com.intellij.util.ArrayUtil;
+import com.intellij.util.Processor;
+
+import javax.swing.*;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * User: anna
+ * Date: 1/17/12
+ */
+public class InheritorChooser {
+
+  protected void runForClasses(final List<PsiClass> classes, final PsiMethod method, final ConfigurationContext context, final Runnable performRunnable) {
+    performRunnable.run();
+  }
+
+  protected void runForClass(final PsiClass aClass, final PsiMethod psiMethod, final ConfigurationContext context, final Runnable performRunnable) {
+    performRunnable.run();
+  }
+
+  public boolean runMethodInAbstractClass(final ConfigurationContext context,
+                                          final Runnable performRunnable,
+                                          final PsiMethod psiMethod,
+                                          final PsiClass containingClass) {
+    if (containingClass != null && containingClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
+      final List<PsiClass> classes = new ArrayList<PsiClass>();
+      if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
+        @Override
+        public void run() {
+          ClassInheritorsSearch.search(containingClass).forEach(new Processor<PsiClass>() {
+            @Override
+            public boolean process(PsiClass aClass) {
+              if (!aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
+                classes.add(aClass);
+              }
+              return true;
+            }
+          });
+        }
+      }, "Search for " + containingClass.getQualifiedName() + " inheritors", true, containingClass.getProject())) {
+        return true;
+      }
+
+      if (classes.size() == 1) {
+        runForClass(classes.get(0), psiMethod, context, performRunnable);
+        return true;
+      }
+      //suggest to run all inherited tests 
+      classes.add(0, null);
+      final JBList list = new JBList(classes);
+      list.setCellRenderer(new PsiClassListCellRenderer() {
+        @Override
+        protected boolean customizeNonPsiElementLeftRenderer(ColoredListCellRenderer renderer,
+                                                             JList list,
+                                                             Object value,
+                                                             int index,
+                                                             boolean selected,
+                                                             boolean hasFocus) {
+          if (value == null) {
+            renderer.append("All");
+            return true;
+          }
+          return super.customizeNonPsiElementLeftRenderer(renderer, list, value, index, selected, hasFocus);
+        }
+      });
+      JBPopupFactory.getInstance().createListPopupBuilder(list)
+        .setTitle("Choose executable classes to run " + psiMethod.getName())
+        .setMovable(false)
+        .setResizable(false)
+        .setRequestFocus(true)
+        .setItemChoosenCallback(new Runnable() {
+          public void run() {
+            final Object[] values = list.getSelectedValues();
+            if (values == null) return;
+            chooseAndPerform(values, psiMethod, context, performRunnable, classes);
+          }
+        }).createPopup().showInBestPositionFor(context.getDataContext());
+      return true;
+    }
+    return false;
+  }
+
+  private void chooseAndPerform(Object[] values,
+                                PsiMethod psiMethod,
+                                ConfigurationContext context,
+                                Runnable performRunnable,
+                                List<PsiClass> classes) {
+    classes.remove(null);
+    if (values.length == 1) {
+      final Object value = values[0];
+      if (value instanceof PsiClass) {
+        runForClass((PsiClass)value, psiMethod, context, performRunnable);
+      }
+      else {
+        runForClasses(classes, psiMethod, context, performRunnable);
+      }
+      return;
+    }
+    if (ArrayUtil.contains(null, values)) {
+      runForClasses(classes, psiMethod, context, performRunnable);
+    }
+    else {
+      final List<PsiClass> selectedClasses = new ArrayList<PsiClass>();
+      for (Object value : values) {
+        if (value instanceof PsiClass) {
+          selectedClasses.add((PsiClass)value);
+        }
+      }
+      runForClasses(selectedClasses, psiMethod, context, performRunnable);
+    }
+  }
+}
index 30b2837ac00719b536f12961770fc9ec9edd1ae0..ba311ff099e46f871fc01539ca22cff6ad7919d1 100644 (file)
@@ -22,23 +22,12 @@ import com.intellij.execution.PsiLocation;
 import com.intellij.execution.RunnerAndConfigurationSettings;
 import com.intellij.execution.actions.ConfigurationContext;
 import com.intellij.execution.junit2.info.MethodLocation;
-import com.intellij.ide.util.PsiClassListCellRenderer;
 import com.intellij.openapi.module.Module;
-import com.intellij.openapi.progress.ProgressManager;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.ui.popup.JBPopupFactory;
 import com.intellij.psi.PsiClass;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiMethod;
-import com.intellij.psi.PsiModifier;
-import com.intellij.psi.search.searches.ClassInheritorsSearch;
-import com.intellij.ui.ColoredListCellRenderer;
-import com.intellij.ui.components.JBList;
-import com.intellij.util.ArrayUtil;
-import com.intellij.util.Processor;
 
-import javax.swing.*;
-import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 
@@ -76,108 +65,26 @@ public class TestMethodConfigurationProducer extends JUnitConfigurationProducer
   public void perform(final ConfigurationContext context, final Runnable performRunnable) {
     final PsiMethod psiMethod = myMethodLocation.getPsiElement();
     final PsiClass containingClass = psiMethod.getContainingClass();
-    if (containingClass != null && containingClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
-      final List<PsiClass> classes = new ArrayList<PsiClass>();
-      if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
-        @Override
-        public void run() {
-          ClassInheritorsSearch.search(containingClass).forEach(new Processor<PsiClass>() {
-            @Override
-            public boolean process(PsiClass aClass) {
-              if (!aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
-                classes.add(aClass);
-              }
-              return true;
-            }
-          });
-        }
-      }, "Search for " + containingClass.getQualifiedName() + " inheritors", true, containingClass.getProject())) {
-        return;
+    final InheritorChooser inheritorChooser = new InheritorChooser() {
+      @Override
+      protected void runForClasses(List<PsiClass> classes, PsiMethod method, ConfigurationContext context, Runnable performRunnable) {
+        ((JUnitConfiguration)context.getConfiguration().getConfiguration()).bePatternConfiguration(classes, method);
+        super.runForClasses(classes, method, context, performRunnable);
       }
 
-      if (classes.size() == 1) {
-        runForClass(classes.get(0), psiMethod, context, performRunnable);
-        return;
+      @Override
+      protected void runForClass(PsiClass aClass,
+                                 PsiMethod psiMethod,
+                                 ConfigurationContext context,
+                                 Runnable performRunnable) {
+        final Project project = psiMethod.getProject();
+        final MethodLocation methodLocation = new MethodLocation(project, psiMethod, PsiLocation.fromPsiElement(aClass));
+        ((JUnitConfiguration)context.getConfiguration().getConfiguration()).beMethodConfiguration(methodLocation);
+        super.runForClass(aClass, psiMethod, context, performRunnable);
       }
-      //suggest to run all inherited tests 
-      classes.add(0, null);
-      final JBList list = new JBList(classes);
-      list.setCellRenderer(new PsiClassListCellRenderer() {
-        @Override
-        protected boolean customizeNonPsiElementLeftRenderer(ColoredListCellRenderer renderer,
-                                                             JList list,
-                                                             Object value,
-                                                             int index,
-                                                             boolean selected,
-                                                             boolean hasFocus) {
-          if (value == null) {
-            renderer.append("All");
-            return true;
-          }
-          return super.customizeNonPsiElementLeftRenderer(renderer, list, value, index, selected, hasFocus);
-        }
-      });
-      JBPopupFactory.getInstance().createListPopupBuilder(list)
-        .setTitle("Choose executable classes to run " + psiMethod.getName())
-        .setMovable(false)
-        .setResizable(false)
-        .setRequestFocus(true)
-        .setItemChoosenCallback(new Runnable() {
-          public void run() {
-            final Object[] values = list.getSelectedValues();
-            if (values == null) return;
-            runMethod(values, psiMethod, context, performRunnable, classes);
-          }
-        }).createPopup().showInBestPositionFor(context.getDataContext());
-      return;
-    }
+    };
+    if (inheritorChooser.runMethodInAbstractClass(context, performRunnable, psiMethod, containingClass)) return;
     super.perform(context, performRunnable);
   }
-
-  private static void runMethod(Object[] values,
-                                PsiMethod psiMethod,
-                                ConfigurationContext context,
-                                Runnable performRunnable,
-                                List<PsiClass> classes) {
-    if (values.length == 1) {
-      final Object value = values[0];
-      if (value instanceof PsiClass) {
-        runForClass((PsiClass)value, psiMethod, context, performRunnable);
-      }
-      else {
-        runForClasses(classes, psiMethod, context, performRunnable);
-      }
-      return;
-    }
-    if (ArrayUtil.contains(null, values)) {
-      runForClasses(classes, psiMethod, context, performRunnable);
-    }
-    else {
-      final List<PsiClass> selectedClasses = new ArrayList<PsiClass>();
-      for (Object value : values) {
-        if (value instanceof PsiClass) {
-          selectedClasses.add((PsiClass)value);
-        }
-      }
-      runForClasses(selectedClasses, psiMethod, context, performRunnable);
-    }
-  }
-
-  private static void runForClasses(List<PsiClass> classes, PsiMethod method, ConfigurationContext context, Runnable performRunnable) {
-    classes.remove(null);
-    ((JUnitConfiguration)context.getConfiguration().getConfiguration()).bePatternConfiguration(classes, method);
-    performRunnable.run();
-  }
-
-  private static void runForClass(final PsiClass aClass,
-                                  final PsiMethod psiMethod, 
-                                  final ConfigurationContext context,
-                                  final Runnable performRunnable) {
-    final Project project = psiMethod.getProject();
-    ((JUnitConfiguration)context.getConfiguration().getConfiguration()).beMethodConfiguration(
-      new MethodLocation(project, psiMethod,
-                         new PsiLocation<PsiClass>(project, aClass)));
-    performRunnable.run();
-  }
 }