@Override
 protected RunnerAndConfigurationSettings findExistingByElement(
     Location location,
     @NotNull RunnerAndConfigurationSettings[] existingConfigurations,
     ConfigurationContext context) {
   final PsiClass aClass = ApplicationConfigurationType.getMainClass(location.getPsiElement());
   if (aClass == null) {
     return null;
   }
   final Module predefinedModule =
       ((ApplicationConfiguration)
               ((RunManagerImpl) RunManagerEx.getInstanceEx(location.getProject()))
                   .getConfigurationTemplate(getConfigurationFactory())
                   .getConfiguration())
           .getConfigurationModule()
           .getModule();
   for (RunnerAndConfigurationSettings existingConfiguration : existingConfigurations) {
     final ApplicationConfiguration appConfiguration =
         (ApplicationConfiguration) existingConfiguration.getConfiguration();
     if (Comparing.equal(
         JavaExecutionUtil.getRuntimeQualifiedName(aClass), appConfiguration.MAIN_CLASS_NAME)) {
       if (Comparing.equal(
           location.getModule(), appConfiguration.getConfigurationModule().getModule())) {
         return existingConfiguration;
       }
       final Module configurationModule = appConfiguration.getConfigurationModule().getModule();
       if (Comparing.equal(location.getModule(), configurationModule))
         return existingConfiguration;
       if (Comparing.equal(predefinedModule, configurationModule)) {
         return existingConfiguration;
       }
     }
   }
   return null;
 }
  @Override
  public boolean isConfigurationFromContext(
      JUnitConfiguration unitConfiguration, ConfigurationContext context) {
    if (RunConfigurationProducer.getInstance(PatternConfigurationProducer.class)
        .isMultipleElementsSelected(context)) {
      return false;
    }
    final RunConfiguration predefinedConfiguration =
        context.getOriginalConfiguration(JUnitConfigurationType.getInstance());
    final 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;
    }
    final PsiElement element = location.getPsiElement();
    final PsiClass testClass = JUnitUtil.getTestClass(element);
    final PsiMethod testMethod = JUnitUtil.getTestMethod(element, false);
    final 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(getConfigurationFactory());
    final Module predefinedModule =
        ((JUnitConfiguration) template.getConfiguration()).getConfigurationModule().getModule();
    final 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;
    final TestObject testobject = unitConfiguration.getTestObject();
    if (testobject != null) {
      if (testobject.isConfiguredByElement(
          unitConfiguration, testClass, testMethod, testPackage, testDir)) {
        final Module configurationModule = unitConfiguration.getConfigurationModule().getModule();
        if (Comparing.equal(location.getModule(), configurationModule)) return true;
        if (Comparing.equal(predefinedModule, configurationModule)) {
          return true;
        }
      }
    }
    return false;
  }
 @Override
 public void update(AnActionEvent e) {
   final Presentation presentation = e.getPresentation();
   presentation.setVisible(false);
   final DataContext dataContext = e.getDataContext();
   final Project project = CommonDataKeys.PROJECT.getData(dataContext);
   if (project != null) {
     final RunConfiguration configuration = RunConfiguration.DATA_KEY.getData(dataContext);
     if (isPatternBasedConfiguration(configuration)) {
       final AbstractTestProxy testProxy = AbstractTestProxy.DATA_KEY.getData(dataContext);
       if (testProxy != null) {
         final Location location =
             testProxy.getLocation(
                 project, ((T) configuration).getConfigurationModule().getSearchScope());
         if (location != null) {
           final PsiElement psiElement = location.getPsiElement();
           if (psiElement instanceof PsiClass
               && getPattern((T) configuration)
                   .contains(((PsiClass) psiElement).getQualifiedName())) {
             presentation.setVisible(true);
           }
         }
       }
     }
   }
 }
 private void createConfiguration() {
   LOG.assertTrue(myConfiguration == null);
   final Location location = getLocation();
   myConfiguration =
       location != null && !DumbService.isDumb(location.getProject())
           ? PreferredProducerFind.createConfiguration(location, this)
           : null;
   myInitialized = true;
 }
  @Nullable
  public static List<String> findTestDataFiles(@NotNull DataContext context) {
    final PsiMethod method = findTargetMethod(context);
    if (method == null) {
      return null;
    }
    final String name = method.getName();

    if (name.startsWith("test")) {
      String testDataPath =
          TestDataLineMarkerProvider.getTestDataBasePath(method.getContainingClass());
      final TestDataReferenceCollector collector =
          new TestDataReferenceCollector(testDataPath, name.substring(4));
      return collector.collectTestDataReferences(method);
    }

    final Location<?> location = Location.DATA_KEY.getData(context);
    if (location instanceof PsiMemberParameterizedLocation) {
      PsiClass containingClass = ((PsiMemberParameterizedLocation) location).getContainingClass();
      if (containingClass == null) {
        containingClass =
            PsiTreeUtil.getParentOfType(location.getPsiElement(), PsiClass.class, false);
      }
      if (containingClass != null) {
        final PsiAnnotation annotation =
            AnnotationUtil.findAnnotationInHierarchy(
                containingClass, Collections.singleton(JUnitUtil.RUN_WITH));
        if (annotation != null) {
          final PsiAnnotationMemberValue memberValue = annotation.findAttributeValue("value");
          if (memberValue instanceof PsiClassObjectAccessExpression) {
            final PsiTypeElement operand =
                ((PsiClassObjectAccessExpression) memberValue).getOperand();
            if (operand.getType().equalsToText(Parameterized.class.getName())) {
              final String testDataPath =
                  TestDataLineMarkerProvider.getTestDataBasePath(containingClass);
              final String paramSetName =
                  ((PsiMemberParameterizedLocation) location).getParamSetName();
              final String baseFileName =
                  StringUtil.trimEnd(StringUtil.trimStart(paramSetName, "["), "]");
              final ProjectFileIndex fileIndex =
                  ProjectRootManager.getInstance(containingClass.getProject()).getFileIndex();
              return TestDataGuessByExistingFilesUtil.suggestTestDataFiles(
                  fileIndex, baseFileName, testDataPath, containingClass);
            }
          }
        }
      }
    }

    return null;
  }
  @Nullable
  private static List<String> getTasksTarget(Location location) {
    if (location instanceof GradleTaskLocation) {
      return ((GradleTaskLocation) location).getTasks();
    }

    PsiElement parent = location.getPsiElement();
    while (parent.getParent() != null && !(parent.getParent() instanceof PsiFile)) {
      parent = parent.getParent();
    }

    if (isCreateTaskMethod(parent)) {
      final GrExpression[] arguments = ((GrMethodCallExpression) parent).getExpressionArguments();
      if (arguments.length > 0
          && arguments[0] instanceof GrLiteral
          && ((GrLiteral) arguments[0]).getValue() instanceof String) {
        return Collections.singletonList((String) ((GrLiteral) arguments[0]).getValue());
      }
    } else if (parent instanceof GrApplicationStatement) {
      PsiElement shiftExpression = parent.getChildren()[1].getChildren()[0];
      if (shiftExpression instanceof GrShiftExpressionImpl) {
        PsiElement shiftiesChild = shiftExpression.getChildren()[0];
        if (shiftiesChild instanceof GrReferenceExpression) {
          return Collections.singletonList(shiftiesChild.getText());
        } else if (shiftiesChild instanceof GrMethodCallExpression) {
          return Collections.singletonList(shiftiesChild.getChildren()[0].getText());
        }
      } else if (shiftExpression instanceof GrMethodCallExpression) {
        return Collections.singletonList(shiftExpression.getChildren()[0].getText());
      }
    }

    return null;
  }
 @Override
 protected RunnerAndConfigurationSettings findExistingByElement(
     Location location,
     @NotNull List<RunnerAndConfigurationSettings> existingConfigurations,
     ConfigurationContext context) {
   for (RunnerAndConfigurationSettings existingConfiguration : existingConfigurations) {
     final RunConfiguration configuration = existingConfiguration.getConfiguration();
     final GroovyScriptRunConfiguration existing = (GroovyScriptRunConfiguration) configuration;
     final String path = existing.getScriptPath();
     if (path != null) {
       final PsiFile file = location.getPsiElement().getContainingFile();
       if (file instanceof GroovyFile) {
         final VirtualFile vfile = file.getVirtualFile();
         if (vfile != null && FileUtil.toSystemIndependentName(path).equals(vfile.getPath())) {
           if (!((GroovyFile) file).isScript()
               || GroovyScriptTypeDetector.getScriptType((GroovyFile) file)
                   .isConfigurationByLocation(existing, location)) {
             return existingConfiguration;
           }
         }
       }
     }
   }
   return null;
 }
Пример #8
0
 @Override
 protected RunnerAndConfigurationSettings createConfigurationByElement(
     Location location, ConfigurationContext context) {
   myContext = context;
   if (!((location instanceof MPSLocation))) {
     return null;
   }
   MPSLocation mpsLocation = (MPSLocation) location;
   final MPSPsiElement psiElement = mpsLocation.getPsiElement();
   RunConfiguration config =
       ModelAccess.instance()
           .runReadAction(
               new Computable<RunConfiguration>() {
                 @Override
                 public RunConfiguration compute() {
                   Object mpsItem = psiElement.getMPSItem();
                   if (mpsItem == null) {
                     return null;
                   }
                   if (!(isApplicable(mpsItem))) {
                     return null;
                   }
                   return doCreateConfiguration((T) psiElement.getMPSItem());
                 }
               });
   if (config == null) {
     return null;
   }
   return new RunnerAndConfigurationSettingsImpl(
       RunManagerImpl.getInstanceImpl(location.getProject()), config, false);
 }
Пример #9
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);
 }
  private MavenRunnerParameters createBuildParameters(Location l) {
    final PsiElement element = l.getPsiElement();
    final Project project = l.getProject();

    final Module module = ModuleUtil.findModuleForPsiElement(element);
    if (module == null) return null;

    final MavenProjectsManager mavenProjectsManager = MavenProjectsManager.getInstance(project);
    final MavenProject mavenProject = mavenProjectsManager.findProject(module);

    if (mavenProject == null) return null;

    // todo: check this code
    final List<MavenArtifact> dependencies = mavenProject.getDependencies();
    MavenArtifact artifact = null;
    for (MavenArtifact dependence : dependencies) {
      if (dependence.getArtifactId().equals(GROUP_ID_LIFT)) {
        artifact = dependence;
        break;
      } else if (dependence.getArtifactId().equals(ARTIFACT_ID_LIFT)) {
        artifact = dependence;
        break;
      }
    }
    // final MavenArtifact artifact = mavenProjectModel.findDependency(GROUP_ID_LIFT,
    // ARTIFACT_ID_LIFT);

    if (artifact == null) return null;

    mySourceElement = element;

    MavenExplicitProfiles profiles =
        MavenProjectsManager.getInstance(project).getExplicitProfiles();
    List<String> goals = new ArrayList<String>();

    goals.add(JETTY_RUN);

    final VirtualFile file = module.getModuleFile();
    if (file == null) return null;

    final VirtualFile parent = file.getParent();
    if (parent == null) return null;

    return new MavenRunnerParameters(true, parent.getPath(), goals, profiles);
  }
  protected RunnerAndConfigurationSettings createConfigurationByElement(
      final Location location, final ConfigurationContext context) {
    final Module module = context.getModule();
    if (module == null || !ScalaUtils.isSuitableModule(module)) return null;

    final MavenRunnerParameters params = createBuildParameters(location);
    if (params == null) return null;
    return createRunnerAndConfigurationSettings(null, null, params, location.getProject());
  }
  @Nullable
  private static PsiMethod findTargetMethod(@NotNull DataContext context) {
    final Editor editor = CommonDataKeys.EDITOR.getData(context);
    final PsiFile file = CommonDataKeys.PSI_FILE.getData(context);
    if (file != null && editor != null) {
      PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
      return PsiTreeUtil.getParentOfType(element, PsiMethod.class);
    }

    final Location<?> location = Location.DATA_KEY.getData(context);
    if (location != null) {
      final PsiElement element = location.getPsiElement();
      if (element instanceof PsiMethod) {
        return (PsiMethod) element;
      }
    }
    return null;
  }
Пример #13
0
 public Module setTestMethod(final Location<PsiMethod> methodLocation) {
   final PsiMethod method = methodLocation.getPsiElement();
   METHOD_NAME = method.getName();
   TEST_OBJECT = TEST_METHOD;
   return setMainClass(
       methodLocation instanceof MethodLocation
           ? ((MethodLocation) methodLocation).getContainingClass()
           : method.getContainingClass());
 }
  private GoFile locationToFile(Location location) {
    final PsiElement element = location.getPsiElement();
    final PsiFile file = element.getContainingFile();

    if (!(file instanceof GoFile)) {
      return null;
    }

    return (GoFile) file;
  }
  protected RunnerAndConfigurationSettings createConfigurationByElement(
      final Location location, final ConfigurationContext context) {
    final PsiElement element = location.getPsiElement();
    final PsiFile file = element.getContainingFile();
    if (!(file instanceof GroovyFile)) {
      return null;
    }

    GroovyFile groovyFile = (GroovyFile) file;
    final PsiClass aClass = GroovyRunnerUtil.getRunningClass(location.getPsiElement());
    if (aClass instanceof GroovyScriptClass || GroovyRunnerUtil.isRunnable(aClass)) {
      final RunnerAndConfigurationSettings settings = createConfiguration(aClass);
      if (settings != null) {
        mySourceElement = element;
        final GroovyScriptRunConfiguration configuration =
            (GroovyScriptRunConfiguration) settings.getConfiguration();
        GroovyScriptTypeDetector.getScriptType(groovyFile)
            .tuneConfiguration(groovyFile, configuration, location);
        return settings;
      }
    }

    if (file.getText().contains("@Grab")) {
      ApplicationConfigurationProducer producer = new ApplicationConfigurationProducer();
      ConfigurationFromContext settings = producer.createConfigurationFromContext(context);
      if (settings != null) {
        PsiElement src = settings.getSourceElement();
        mySourceElement = src;
        return createConfiguration(
            src instanceof PsiMethod ? ((PsiMethod) src).getContainingClass() : (PsiClass) src);
      }

      return null;
    } else {
      return null;
    }
  }
  /**
   * Finds an existing run configuration matching the context.
   *
   * @return an existing configuration, or null if none was found.
   */
  @Nullable
  public RunnerAndConfigurationSettings findExisting() {
    if (myExistingConfiguration != null) return myExistingConfiguration.get();
    myExistingConfiguration = new Ref<>();
    if (myLocation == null) {
      return null;
    }

    final PsiElement psiElement = myLocation.getPsiElement();
    if (!psiElement.isValid()) {
      return null;
    }

    final List<RuntimeConfigurationProducer> producers = findPreferredProducers();
    if (myRuntimeConfiguration != null) {
      if (producers != null) {
        for (RuntimeConfigurationProducer producer : producers) {
          final RunnerAndConfigurationSettings configuration =
              producer.findExistingConfiguration(myLocation, this);
          if (configuration != null && configuration.getConfiguration() == myRuntimeConfiguration) {
            myExistingConfiguration.set(configuration);
          }
        }
      }
      for (RunConfigurationProducer producer :
          RunConfigurationProducer.getProducers(getProject())) {
        RunnerAndConfigurationSettings configuration = producer.findExistingConfiguration(this);
        if (configuration != null && configuration.getConfiguration() == myRuntimeConfiguration) {
          myExistingConfiguration.set(configuration);
        }
      }
    }
    if (producers != null) {
      for (RuntimeConfigurationProducer producer : producers) {
        final RunnerAndConfigurationSettings configuration =
            producer.findExistingConfiguration(myLocation, this);
        if (configuration != null) {
          myExistingConfiguration.set(configuration);
        }
      }
    }
    for (RunConfigurationProducer producer : RunConfigurationProducer.getProducers(getProject())) {
      RunnerAndConfigurationSettings configuration = producer.findExistingConfiguration(this);
      if (configuration != null) {
        myExistingConfiguration.set(configuration);
      }
    }
    return myExistingConfiguration.get();
  }
 public RunnerAndConfigurationSettings createConfigurationByElement(
     Location location, final ConfigurationContext context) {
   location = JavaExecutionUtil.stepIntoSingleClass(location);
   if (location == null) return null;
   final PsiElement element = location.getPsiElement();
   if (!element.isPhysical()) return null;
   PsiElement currentElement = element;
   PsiMethod method;
   while ((method = findMain(currentElement)) != null) {
     final PsiClass aClass = method.getContainingClass();
     if (ConfigurationUtil.MAIN_CLASS.value(aClass)) {
       myPsiElement = method;
       return createConfiguration(aClass, context, location);
     }
     currentElement = method.getParent();
   }
   final PsiClass aClass = ApplicationConfigurationType.getMainClass(element);
   if (aClass == null) return null;
   myPsiElement = aClass;
   return createConfiguration(aClass, context, location);
 }
 public Project getProject() {
   return myLocation.getProject();
 }
 /**
  * Returns the PSI element at caret for this context.
  *
  * @return the PSI element, or null if no source code fragment is currently selected.
  */
 @Nullable
 public PsiElement getPsiLocation() {
   return myLocation != null ? myLocation.getPsiElement() : null;
 }
  @Override
  protected boolean setupConfigurationFromContext(
      TestNGConfiguration configuration,
      ConfigurationContext context,
      Ref<PsiElement> sourceElement) {
    // TODO: check TestNG Pattern running first, before method/class (see
    // TestNGInClassConfigurationProducer for logic)
    // TODO: and PsiClassOwner not handled, which is in TestNGInClassConfigurationProducer

    Location location = context.getLocation();
    if (location == null) {
      return false;
    }

    Project project = context.getProject();
    PsiElement leaf = location.getPsiElement();

    if (!ProjectRootsUtil.isInProjectOrLibSource(leaf)) {
      return false;
    }

    if (!(leaf.getContainingFile() instanceof KtFile)) {
      return false;
    }

    KtFile jetFile = (KtFile) leaf.getContainingFile();

    if (ProjectStructureUtil.isJsKotlinModule(jetFile)) {
      return false;
    }

    KtNamedDeclaration declarationToRun = getDeclarationToRun(leaf);

    if (declarationToRun instanceof KtNamedFunction) {
      KtNamedFunction function = (KtNamedFunction) declarationToRun;

      @SuppressWarnings("unchecked")
      KtElement owner = PsiTreeUtil.getParentOfType(function, KtFunction.class, KtClass.class);

      if (owner instanceof KtClass) {
        PsiClass delegate = LightClassUtil.INSTANCE.getPsiClass((KtClass) owner);
        if (delegate != null) {
          for (PsiMethod method : delegate.getMethods()) {
            if (method.getNavigationElement() == function) {
              if (TestNGUtil.hasTest(method)) {
                return configure(configuration, location, context, project, delegate, method);
              }
              break;
            }
          }
        }
      }
    }

    if (declarationToRun instanceof KtClass) {
      PsiClass delegate = LightClassUtil.INSTANCE.getPsiClass((KtClassOrObject) declarationToRun);
      if (!isTestNGClass(delegate)) {
        return false;
      }

      return configure(configuration, location, context, project, delegate, null);
    }

    return false;
  }