Search in sources :

Example 1 with MethodLocation

use of com.intellij.execution.junit2.info.MethodLocation in project intellij-community by JetBrains.

the class TestMethods method getTestPresentation.

@Nullable
public static String getTestPresentation(AbstractTestProxy testInfo, Project project, GlobalSearchScope searchScope) {
    final Location location = testInfo.getLocation(project, searchScope);
    final PsiElement element = location != null ? location.getPsiElement() : null;
    if (element instanceof PsiMethod) {
        final PsiClass containingClass = location instanceof MethodLocation ? ((MethodLocation) location).getContainingClass() : location instanceof PsiMemberParameterizedLocation ? ((PsiMemberParameterizedLocation) location).getContainingClass() : ((PsiMethod) element).getContainingClass();
        if (containingClass != null) {
            final String proxyName = testInfo.getName();
            final String methodName = ((PsiMethod) element).getName();
            return JavaExecutionUtil.getRuntimeQualifiedName(containingClass) + "," + (proxyName.contains(methodName) ? proxyName.substring(proxyName.indexOf(methodName)) : methodName);
        }
    }
    return null;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) PsiElement(com.intellij.psi.PsiElement) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with MethodLocation

use of com.intellij.execution.junit2.info.MethodLocation in project intellij-community by JetBrains.

the class InheritorChooser method runMethodInAbstractClass.

public boolean runMethodInAbstractClass(final ConfigurationContext context, final Runnable performRunnable, final PsiMethod psiMethod, final PsiClass containingClass, final Condition<PsiClass> acceptAbstractCondition) {
    if (containingClass != null && acceptAbstractCondition.value(containingClass)) {
        final Location location = context.getLocation();
        if (location instanceof MethodLocation) {
            final PsiClass aClass = ((MethodLocation) location).getContainingClass();
            if (aClass != null && !aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
                return false;
            }
        } else if (location instanceof PsiMemberParameterizedLocation) {
            return false;
        }
        final List<PsiClass> classes = new ArrayList<>();
        if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
            final boolean isJUnit5 = ReadAction.compute(() -> JUnitUtil.isJUnit5(containingClass));
            ClassInheritorsSearch.search(containingClass).forEach(aClass -> {
                if (isJUnit5 && JUnitUtil.isJUnit5TestClass(aClass, true) || PsiClassUtil.isRunnableClass(aClass, true, true)) {
                    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;
        }
        if (classes.isEmpty())
            return false;
        final FileEditor fileEditor = PlatformDataKeys.FILE_EDITOR.getData(context.getDataContext());
        if (fileEditor instanceof TextEditor) {
            final Document document = ((TextEditor) fileEditor).getEditor().getDocument();
            final PsiFile containingFile = PsiDocumentManager.getInstance(context.getProject()).getPsiFile(document);
            if (containingFile instanceof PsiClassOwner) {
                final List<PsiClass> psiClasses = new ArrayList<>(Arrays.asList(((PsiClassOwner) containingFile).getClasses()));
                psiClasses.retainAll(classes);
                if (psiClasses.size() == 1) {
                    runForClass(psiClasses.get(0), psiMethod, context, performRunnable);
                    return true;
                }
            }
        }
        final int numberOfInheritors = classes.size();
        final PsiClassListCellRenderer renderer = new PsiClassListCellRenderer() {

            @Override
            protected boolean customizeNonPsiElementLeftRenderer(ColoredListCellRenderer renderer, JList list, Object value, int index, boolean selected, boolean hasFocus) {
                if (value == null) {
                    renderer.append("All (" + numberOfInheritors + ")");
                    return true;
                }
                return super.customizeNonPsiElementLeftRenderer(renderer, list, value, index, selected, hasFocus);
            }
        };
        Collections.sort(classes, renderer.getComparator());
        //suggest to run all inherited tests 
        classes.add(0, null);
        final JBList list = new JBList(classes);
        list.setCellRenderer(renderer);
        JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle("Choose executable classes to run " + (psiMethod != null ? psiMethod.getName() : containingClass.getName())).setMovable(false).setResizable(false).setRequestFocus(true).setItemChoosenCallback(() -> {
            final Object[] values = list.getSelectedValues();
            if (values == null)
                return;
            chooseAndPerform(values, psiMethod, context, performRunnable, classes);
        }).createPopup().showInBestPositionFor(context.getDataContext());
        return true;
    }
    return false;
}
Also used : ProgressManager(com.intellij.openapi.progress.ProgressManager) JBList(com.intellij.ui.components.JBList) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) Arrays(java.util.Arrays) ArrayUtil(com.intellij.util.ArrayUtil) ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) Document(com.intellij.openapi.editor.Document) FileEditor(com.intellij.openapi.fileEditor.FileEditor) ReadAction(com.intellij.openapi.application.ReadAction) ArrayList(java.util.ArrayList) List(java.util.List) PlatformDataKeys(com.intellij.openapi.actionSystem.PlatformDataKeys) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) PsiClassUtil(com.intellij.psi.util.PsiClassUtil) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) com.intellij.psi(com.intellij.psi) ClassInheritorsSearch(com.intellij.psi.search.searches.ClassInheritorsSearch) Location(com.intellij.execution.Location) Collections(java.util.Collections) TextEditor(com.intellij.openapi.fileEditor.TextEditor) Condition(com.intellij.openapi.util.Condition) javax.swing(javax.swing) FileEditor(com.intellij.openapi.fileEditor.FileEditor) ArrayList(java.util.ArrayList) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) Document(com.intellij.openapi.editor.Document) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) TextEditor(com.intellij.openapi.fileEditor.TextEditor) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) JBList(com.intellij.ui.components.JBList) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location)

Example 3 with MethodLocation

use of com.intellij.execution.junit2.info.MethodLocation in project intellij-community by JetBrains.

the class AbstractInClassConfigurationProducer method onFirstRun.

public void onFirstRun(@NotNull final ConfigurationFromContext configuration, @NotNull final ConfigurationContext fromContext, @NotNull Runnable performRunnable) {
    final PsiElement psiElement = configuration.getSourceElement();
    if (psiElement instanceof PsiMethod || psiElement instanceof PsiClass) {
        final PsiMethod psiMethod;
        final PsiClass containingClass;
        if (psiElement instanceof PsiMethod) {
            psiMethod = (PsiMethod) psiElement;
            containingClass = psiMethod.getContainingClass();
        } else {
            psiMethod = null;
            containingClass = (PsiClass) psiElement;
        }
        final InheritorChooser inheritorChooser = new InheritorChooser() {

            @Override
            protected void runForClasses(List<PsiClass> classes, PsiMethod method, ConfigurationContext context, Runnable performRunnable) {
                ((T) configuration.getConfiguration()).bePatternConfiguration(classes, method);
                super.runForClasses(classes, method, context, performRunnable);
            }

            @Override
            protected void runForClass(PsiClass aClass, PsiMethod psiMethod, ConfigurationContext context, Runnable performRunnable) {
                if (psiElement instanceof PsiMethod) {
                    final Project project = psiMethod.getProject();
                    final MethodLocation methodLocation = new MethodLocation(project, psiMethod, PsiLocation.fromPsiElement(aClass));
                    ((T) configuration.getConfiguration()).beMethodConfiguration(methodLocation);
                } else {
                    ((T) configuration.getConfiguration()).beClassConfiguration(aClass);
                }
                super.runForClass(aClass, psiMethod, context, performRunnable);
            }
        };
        if (inheritorChooser.runMethodInAbstractClass(fromContext, performRunnable, psiMethod, containingClass, aClass -> aClass.hasModifierProperty(PsiModifier.ABSTRACT) && isTestClass(aClass))) {
            return;
        }
    }
    super.onFirstRun(configuration, fromContext, performRunnable);
}
Also used : ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) Project(com.intellij.openapi.project.Project) InheritorChooser(com.intellij.execution.junit.InheritorChooser) List(java.util.List) MethodLocation(com.intellij.execution.junit2.info.MethodLocation)

Example 4 with MethodLocation

use of com.intellij.execution.junit2.info.MethodLocation in project intellij-community by JetBrains.

the class AbstractInClassConfigurationProducer method setupConfigurationFromContext.

protected boolean setupConfigurationFromContext(T configuration, ConfigurationContext context, Ref<PsiElement> sourceElement) {
    if (isMultipleElementsSelected(context)) {
        return false;
    }
    final Location contextLocation = context.getLocation();
    setupConfigurationParamName(configuration, contextLocation);
    PsiClass psiClass = null;
    PsiElement element = context.getPsiLocation();
    while (element != null) {
        if (element instanceof PsiClass && isTestClass((PsiClass) element)) {
            psiClass = (PsiClass) element;
            break;
        } else if (element instanceof PsiMember) {
            psiClass = contextLocation instanceof MethodLocation ? ((MethodLocation) contextLocation).getContainingClass() : ((PsiMember) element).getContainingClass();
            if (isTestClass(psiClass)) {
                break;
            }
        } else if (element instanceof PsiClassOwner) {
            final PsiClass[] classes = ((PsiClassOwner) element).getClasses();
            if (classes.length == 1) {
                psiClass = classes[0];
                break;
            }
        }
        element = element.getParent();
    }
    if (!isTestClass(psiClass))
        return false;
    PsiElement psiElement = psiClass;
    final Project project = context.getProject();
    RunnerAndConfigurationSettings settings = cloneTemplateConfiguration(context);
    setupConfigurationModule(context, configuration);
    final Module originalModule = configuration.getConfigurationModule().getModule();
    configuration.beClassConfiguration(psiClass);
    PsiMethod method = PsiTreeUtil.getParentOfType(context.getPsiLocation(), PsiMethod.class, false);
    while (method != null) {
        if (isTestMethod(false, method)) {
            configuration.beMethodConfiguration(MethodLocation.elementInClass(method, psiClass));
            psiElement = method;
        }
        method = PsiTreeUtil.getParentOfType(method, PsiMethod.class);
    }
    configuration.restoreOriginalModule(originalModule);
    settings.setName(configuration.getName());
    sourceElement.set(psiElement);
    return true;
}
Also used : Project(com.intellij.openapi.project.Project) RunnerAndConfigurationSettings(com.intellij.execution.RunnerAndConfigurationSettings) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Module(com.intellij.openapi.module.Module) PsiLocation(com.intellij.execution.PsiLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location)

Example 5 with MethodLocation

use of com.intellij.execution.junit2.info.MethodLocation in project intellij-community by JetBrains.

the class TestData method setTestMethod.

public Module setTestMethod(Location<PsiMethod> location) {
    final PsiMethod method = location.getPsiElement();
    METHOD_NAME = method.getName();
    TEST_OBJECT = TestType.METHOD.getType();
    return setMainClass(location instanceof MethodLocation ? ((MethodLocation) location).getContainingClass() : method.getContainingClass());
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) MethodLocation(com.intellij.execution.junit2.info.MethodLocation)

Aggregations

MethodLocation (com.intellij.execution.junit2.info.MethodLocation)7 Location (com.intellij.execution.Location)4 ConfigurationContext (com.intellij.execution.actions.ConfigurationContext)3 PsiMemberParameterizedLocation (com.intellij.execution.junit2.PsiMemberParameterizedLocation)3 Project (com.intellij.openapi.project.Project)3 PsiMethod (com.intellij.psi.PsiMethod)3 List (java.util.List)3 InheritorChooser (com.intellij.execution.junit.InheritorChooser)2 Module (com.intellij.openapi.module.Module)2 PsiClass (com.intellij.psi.PsiClass)2 PsiLocation (com.intellij.execution.PsiLocation)1 RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)1 PsiClassListCellRenderer (com.intellij.ide.util.PsiClassListCellRenderer)1 PlatformDataKeys (com.intellij.openapi.actionSystem.PlatformDataKeys)1 ReadAction (com.intellij.openapi.application.ReadAction)1 Document (com.intellij.openapi.editor.Document)1 FileEditor (com.intellij.openapi.fileEditor.FileEditor)1 TextEditor (com.intellij.openapi.fileEditor.TextEditor)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 JBPopupFactory (com.intellij.openapi.ui.popup.JBPopupFactory)1