Search in sources :

Example 1 with PsiMemberParameterizedLocation

use of com.intellij.execution.junit2.PsiMemberParameterizedLocation 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 PsiMemberParameterizedLocation

use of com.intellij.execution.junit2.PsiMemberParameterizedLocation 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 PsiMemberParameterizedLocation

use of com.intellij.execution.junit2.PsiMemberParameterizedLocation in project android by JetBrains.

the class TestPatternConfigurationProducer method setupConfigurationFromContext.

@Override
protected boolean setupConfigurationFromContext(AndroidJUnitConfiguration configuration, ConfigurationContext context, Ref<PsiElement> sourceElement) {
    LinkedHashSet<String> classes = new LinkedHashSet<String>();
    PsiElement element = checkPatterns(context, classes);
    if (element == null) {
        return false;
    }
    sourceElement.set(element);
    JUnitConfiguration.Data data = configuration.getPersistentData();
    data.setPatterns(classes);
    data.TEST_OBJECT = JUnitConfiguration.TEST_PATTERN;
    data.setScope(setupPackageConfiguration(context, configuration, data.getScope()));
    configuration.setGeneratedName();
    Location contextLocation = context.getLocation();
    if (contextLocation instanceof PsiMemberParameterizedLocation) {
        String paramSetName = ((PsiMemberParameterizedLocation) contextLocation).getParamSetName();
        if (paramSetName != null) {
            configuration.setProgramParameters(paramSetName);
        }
    }
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PsiElement(com.intellij.psi.PsiElement) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) Location(com.intellij.execution.Location) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation)

Example 4 with PsiMemberParameterizedLocation

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

the class JavaTestLocator method collectMethodNavigatables.

private static List<Location> collectMethodNavigatables(@NotNull String path, @NotNull Project project, @NotNull GlobalSearchScope scope, String paramName) {
    List<Location> results = Collections.emptyList();
    String className = StringUtil.getPackageName(path);
    if (!StringUtil.isEmpty(className)) {
        String methodName = StringUtil.getShortName(path);
        PsiClass aClass = ClassUtil.findPsiClass(PsiManager.getInstance(project), className, null, true, scope);
        if (aClass != null) {
            results = ContainerUtil.newSmartList();
            if (methodName.trim().equals(aClass.getName())) {
                results.add(createClassNavigatable(paramName, aClass));
            } else {
                PsiMethod[] methods = aClass.findMethodsByName(methodName.trim(), true);
                if (methods.length > 0) {
                    for (PsiMethod method : methods) {
                        results.add(paramName != null ? new PsiMemberParameterizedLocation(project, method, aClass, paramName) : MethodLocation.elementInClass(method, aClass));
                    }
                }
            }
        }
    }
    return results;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) PsiLocation(com.intellij.execution.PsiLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation)

Example 5 with PsiMemberParameterizedLocation

use of com.intellij.execution.junit2.PsiMemberParameterizedLocation in project android by JetBrains.

the class AndroidJUnitConfigurations method isFromContext.

// Copy of JUnitConfigurationProducer#isConfigurationFromContext using AndroidJUnitConfigurationType instead of JUnitConfigurationType
public static boolean isFromContext(@NotNull JUnitConfiguration unitConfiguration, @NotNull ConfigurationContext context, @NotNull ConfigurationFactory configurationFactory) {
    if (RunConfigurationProducer.getInstance(PatternConfigurationProducer.class).isMultipleElementsSelected(context)) {
        return false;
    }
    RunConfiguration predefinedConfiguration = context.getOriginalConfiguration(AndroidJUnitConfigurationType.getInstance());
    Location contextLocation = context.getLocation();
    String paramSetName = contextLocation instanceof PsiMemberParameterizedLocation ? ((PsiMemberParameterizedLocation) contextLocation).getParamSetName() : null;
    assert contextLocation != null;
    Location location = JavaExecutionUtil.stepIntoSingleClass(contextLocation);
    if (location == null) {
        return false;
    }
    PsiElement element = location.getPsiElement();
    PsiClass testClass = getTestClass(element);
    PsiMethod testMethod = getTestMethod(element, false);
    PsiPackage testPackage;
    if (element instanceof PsiPackage) {
        testPackage = (PsiPackage) element;
    } else {
        if (element instanceof PsiDirectory) {
            testPackage = JavaDirectoryService.getInstance().getPackage(((PsiDirectory) element));
        } else {
            testPackage = null;
        }
    }
    PsiDirectory testDir = element instanceof PsiDirectory ? (PsiDirectory) element : null;
    RunnerAndConfigurationSettings template = RunManager.getInstance(location.getProject()).getConfigurationTemplate(configurationFactory);
    Module predefinedModule = ((JUnitConfiguration) template.getConfiguration()).getConfigurationModule().getModule();
    String vmParameters = predefinedConfiguration instanceof JUnitConfiguration ? ((JUnitConfiguration) predefinedConfiguration).getVMParameters() : null;
    if (vmParameters != null && !Comparing.strEqual(vmParameters, unitConfiguration.getVMParameters())) {
        return false;
    }
    if (paramSetName != null && !Comparing.strEqual(paramSetName, unitConfiguration.getProgramParameters())) {
        return false;
    }
    TestObject testobject = unitConfiguration.getTestObject();
    if (testobject != null) {
        if (testobject.isConfiguredByElement(unitConfiguration, testClass, testMethod, testPackage, testDir)) {
            Module configurationModule = unitConfiguration.getConfigurationModule().getModule();
            if (Comparing.equal(location.getModule(), configurationModule)) {
                return true;
            }
            if (Comparing.equal(predefinedModule, configurationModule)) {
                return true;
            }
        }
    }
    return false;
}
Also used : PatternConfigurationProducer(com.intellij.execution.junit.PatternConfigurationProducer) JUnitConfiguration(com.intellij.execution.junit.JUnitConfiguration) TestObject(com.intellij.execution.junit.TestObject) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) RunConfiguration(com.intellij.execution.configurations.RunConfiguration) RunnerAndConfigurationSettings(com.intellij.execution.RunnerAndConfigurationSettings) Module(com.intellij.openapi.module.Module) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) Location(com.intellij.execution.Location)

Aggregations

PsiMemberParameterizedLocation (com.intellij.execution.junit2.PsiMemberParameterizedLocation)12 Location (com.intellij.execution.Location)8 MethodLocation (com.intellij.execution.junit2.info.MethodLocation)6 PsiClass (com.intellij.psi.PsiClass)6 PsiMethod (com.intellij.psi.PsiMethod)6 PsiElement (com.intellij.psi.PsiElement)4 Module (com.intellij.openapi.module.Module)3 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)2 RunConfiguration (com.intellij.execution.configurations.RunConfiguration)2 PsiFile (com.intellij.psi.PsiFile)2 LinkedHashSet (java.util.LinkedHashSet)2 Test (org.junit.Test)2 PsiLocation (com.intellij.execution.PsiLocation)1 RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)1 ConfigurationContext (com.intellij.execution.actions.ConfigurationContext)1 JUnitConfiguration (com.intellij.execution.junit.JUnitConfiguration)1 PatternConfigurationProducer (com.intellij.execution.junit.PatternConfigurationProducer)1 TestObject (com.intellij.execution.junit.TestObject)1 JavaTestLocator (com.intellij.execution.testframework.JavaTestLocator)1 SMTestProxy (com.intellij.execution.testframework.sm.runner.SMTestProxy)1