@Override
  public boolean canHandle(RunConfiguration configuration, Project project) {
    if (!(configuration instanceof JUnitConfiguration)) {
      return false;
    } else {
      JUnitConfiguration.Data data = ((JUnitConfiguration) configuration).getPersistentData();

      String mainClassName = data.getMainClassName();
      String methodName = data.getMethodName();

      JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
      PsiClass referencedClass =
          psiFacade.findClass(mainClassName, GlobalSearchScope.allScope(project));
      PsiFile containingFile = referencedClass.getContainingFile();

      FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
      if (methodName == null) {
        fileEditorManager.openFile(containingFile.getVirtualFile(), true);
      } else {
        PsiMethod[] methodsByName = referencedClass.findMethodsByName(methodName, false);

        fileEditorManager.openEditor(
            new OpenFileDescriptor(
                project, containingFile.getVirtualFile(), methodsByName[0].getTextOffset()),
            true);
      }
      return true;
    }
  }
 public String suggestActionName() {
   final String configurationName = myConfiguration.getName();
   if (!myConfiguration.isGeneratedName()) {
     return "'" + configurationName + "'";
   }
   final JUnitConfiguration.Data data = myConfiguration.getPersistentData();
   if (data.getPackageName().trim().length() > 0) {
     return ExecutionBundle.message("test.in.scope.presentable.text", data.getPackageName());
   }
   return ExecutionBundle.message("all.tests.scope.presentable.text");
 }
Esempio n. 3
0
 protected void initialize() throws ExecutionException {
   defaultInitialize();
   final JUnitConfiguration.Data data = myConfiguration.getPersistentData();
   RunConfigurationModule module = myConfiguration.getConfigurationModule();
   final Project project = module.getProject();
   addJUnit4Parameter(data, project);
   final ExecutionException[] exception = new ExecutionException[1];
   ApplicationManager.getApplication()
       .runReadAction(
           new Runnable() {
             public void run() {
               try {
                 myConfiguration.configureClasspath(myJavaParameters);
               } catch (CantRunException e) {
                 exception[0] = e;
               }
             }
           });
   if (exception[0] != null) throw exception[0];
   final LinkedHashMap<PsiMethod, TestInfo> methods = new LinkedHashMap<PsiMethod, TestInfo>();
   for (AbstractTestProxy failedTest : myFailedTests) {
     Location location = failedTest.getLocation(project);
     if (!(location instanceof MethodLocation)) continue;
     PsiElement psiElement = location.getPsiElement();
     LOG.assertTrue(psiElement instanceof PsiMethod);
     PsiMethod method = (PsiMethod) psiElement;
     methods.put(method, ((TestProxy) failedTest).getInfo());
   }
   addClassesListToJavaParameters(
       methods.keySet(),
       new Function<PsiElement, String>() {
         public String fun(PsiElement element) {
           if (element instanceof PsiMethod) {
             final PsiMethod method = (PsiMethod) element;
             final TestInfo testInfo = methods.get(method);
             return JavaExecutionUtil.getRuntimeQualifiedName(
                     ((MethodLocation) testInfo.getLocation(project)).getContainingClass())
                 + ","
                 + testInfo.getName();
           }
           return null;
         }
       },
       data.getPackageName(),
       true,
       false);
 }
 protected PsiPackage getPackage(JUnitConfiguration.Data data) throws CantRunException {
   final Project project = myConfiguration.getProject();
   final String packageName = data.getPackageName();
   final PsiManager psiManager = PsiManager.getInstance(project);
   final PsiPackage aPackage =
       JavaPsiFacade.getInstance(psiManager.getProject()).findPackage(packageName);
   if (aPackage == null) throw CantRunException.packageNotFound(packageName);
   return aPackage;
 }
 @Override
 protected boolean setupConfigurationFromContext(
     JUnitConfiguration configuration,
     ConfigurationContext context,
     Ref<PsiElement> sourceElement) {
   PsiPackage psiPackage =
       JavaRuntimeConfigurationProducerBase.checkPackage(context.getPsiLocation());
   if (psiPackage == null) return false;
   sourceElement.set(psiPackage);
   if (!LocationUtil.isJarAttached(
       context.getLocation(), psiPackage, JUnitUtil.TESTCASE_CLASS, JUnitUtil.TEST5_ANNOTATION))
     return false;
   final JUnitConfiguration.Data data = configuration.getPersistentData();
   data.PACKAGE_NAME = psiPackage.getQualifiedName();
   data.TEST_OBJECT = JUnitConfiguration.TEST_PACKAGE;
   data.setScope(setupPackageConfiguration(context, configuration, data.getScope()));
   configuration.setGeneratedName();
   return true;
 }
 @Override
 protected PsiPackage getPackage(JUnitConfiguration.Data data) throws CantRunException {
   final String dirName = data.getDirName();
   final VirtualFile file =
       LocalFileSystem.getInstance().findFileByPath(FileUtil.toSystemIndependentName(dirName));
   if (file == null) {
     throw new CantRunException("Directory \'" + dirName + "\' is not found");
   }
   final PsiDirectory directory = PsiManager.getInstance(myProject).findDirectory(file);
   if (directory == null) {
     throw new CantRunException("Directory \'" + dirName + "\' is not found");
   }
   final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(directory);
   if (aPackage == null) {
     throw new CantRunException("Package not found in directory");
   }
   return aPackage;
 }
 public SourceScope getSourceScope() {
   final JUnitConfiguration.Data data = myConfiguration.getPersistentData();
   return data.getScope().getSourceScope(myConfiguration);
 }