From 1f2dfd6c74e45e2770399d51a844d8a2031da846 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 10 Apr 2015 12:09:12 +0200 Subject: [PATCH] platform: Java test location provider extracted (for reuse) --- .../JavaTestLocationProvider.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 java/execution/impl/src/com/intellij/execution/testframework/JavaTestLocationProvider.java diff --git a/java/execution/impl/src/com/intellij/execution/testframework/JavaTestLocationProvider.java b/java/execution/impl/src/com/intellij/execution/testframework/JavaTestLocationProvider.java new file mode 100644 index 000000000000..885f3eaff3d4 --- /dev/null +++ b/java/execution/impl/src/com/intellij/execution/testframework/JavaTestLocationProvider.java @@ -0,0 +1,76 @@ +/* + * 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.testframework; + +import com.intellij.execution.Location; +import com.intellij.execution.PsiLocation; +import com.intellij.openapi.project.Project; +import com.intellij.psi.JavaPsiFacade; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.testIntegration.TestLocationProvider; +import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.Collections; +import java.util.List; + +public class JavaTestLocationProvider implements TestLocationProvider { + public static final String SUITE_PROTOCOL = "java:suite"; + public static final String TEST_PROTOCOL = "java:test"; + + private final GlobalSearchScope myScope; + + public JavaTestLocationProvider(@NotNull GlobalSearchScope scope) { + myScope = scope; + } + + @NotNull + @Override + public List getLocation(@NotNull String protocolId, @NotNull String locationData, Project project) { + List results = Collections.emptyList(); + + if (SUITE_PROTOCOL.equals(protocolId)) { + PsiClass[] classes = JavaPsiFacade.getInstance(project).findClasses(locationData, myScope); + if (classes.length > 0) { + results = ContainerUtil.newSmartList(); + for (PsiClass aClass : classes) { + results.add(new PsiLocation(project, aClass)); + } + } + } + else if (TEST_PROTOCOL.equals(protocolId)) { + int p = locationData.lastIndexOf('.'); + if (p > 0 && p < locationData.length() - 1) { + String className = locationData.substring(0, p); + PsiClass[] classes = JavaPsiFacade.getInstance(project).findClasses(className, myScope); + if (classes.length > 0) { + results = ContainerUtil.newSmartList(); + String methodName = locationData.substring(p + 1); + for (PsiClass aClass : classes) { + PsiMethod[] methods = aClass.findMethodsByName(methodName, true); + for (PsiMethod method : methods) { + results.add(new PsiLocation(project, method)); + } + } + } + } + } + + return results; + } +}