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;
}
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;
}
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);
}
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;
}
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());
}
Aggregations