public ImportRunProfile(VirtualFile file, Project project) {
      myFile = file;
      myProject = project;
      try {
        final Document document = JDOMUtil.loadDocument(VfsUtilCore.virtualToIoFile(myFile));
        final Element config = document.getRootElement().getChild("config");
        if (config != null) {
          String configTypeId = config.getAttributeValue("configId");
          if (configTypeId != null) {
            final ConfigurationType configurationType =
                ConfigurationTypeUtil.findConfigurationType(configTypeId);
            if (configurationType != null) {
              myConfiguration =
                  configurationType.getConfigurationFactories()[0].createTemplateConfiguration(
                      project);
              myConfiguration.setName(config.getAttributeValue("name"));
              myConfiguration.readExternal(config);

              final Executor executor =
                  ExecutorRegistry.getInstance().getExecutorById(DefaultRunExecutor.EXECUTOR_ID);
              if (executor != null) {
                if (myConfiguration instanceof SMRunnerConsolePropertiesProvider) {
                  myProperties =
                      ((SMRunnerConsolePropertiesProvider) myConfiguration)
                          .createTestConsoleProperties(executor);
                }
              }
            }
          }
        }
      } catch (Exception ignore) {
      }
    }
 @Nullable
 @Override
 public RunProfileState getState(
     @NotNull Executor executor, @NotNull ExecutionEnvironment environment)
     throws ExecutionException {
   if (!myImported) {
     myImported = true;
     return new ImportedTestRunnableState(this, VfsUtilCore.virtualToIoFile(myFile));
   }
   if (myConfiguration != null) {
     try {
       return myConfiguration.getState(executor, environment);
     } catch (Throwable e) {
       LOG.info(e);
       throw new ExecutionException("Unable to run the configuration: settings are corrupted");
     }
   }
   throw new ExecutionException(
       "Unable to run the configuration: failed to detect test framework");
 }
Esempio n. 3
0
  private void runConfigurations(
      final Executor executor, final List<RunConfiguration> runConfigurations, final int index) {
    if (index >= runConfigurations.size()) {
      stopRunningMultirunConfiguration.doneStaringConfigurations();
      return;
    }
    if (!stopRunningMultirunConfiguration.canContinueStartingConfigurations()) {
      stopRunningMultirunConfiguration.doneStaringConfigurations();
      // don't start more configurations if user stopped the plugin work.
      return;
    }

    final RunConfiguration runConfiguration = runConfigurations.get(index);
    final Project project = runConfiguration.getProject();
    final RunnerAndConfigurationSettings configuration =
        new RunnerAndConfigurationSettingsImpl(
            RunManagerImpl.getInstanceImpl(project), runConfiguration, false);

    boolean started = false;
    try {
      ProgramRunner runner =
          RunnerRegistry.getInstance().getRunner(executor.getId(), runConfiguration);
      if (runner == null) return;
      if (!checkRunConfiguration(executor, project, configuration)) return;

      runTriggers(executor, configuration);
      RunContentDescriptor runContentDescriptor =
          getRunContentDescriptor(runConfiguration, project);
      ExecutionEnvironment executionEnvironment =
          new ExecutionEnvironment(
              runner,
              DefaultExecutionTarget.INSTANCE,
              configuration,
              runContentDescriptor,
              project);

      runner.execute(
          executor,
          executionEnvironment,
          new ProgramRunner.Callback() {
            @SuppressWarnings("ConstantConditions")
            @Override
            public void processStarted(final RunContentDescriptor descriptor) {
              if (descriptor == null) {
                if (startOneByOne) {
                  // start next configuration..
                  runConfigurations(executor, runConfigurations, index + 1);
                }
                return;
              }

              final ProcessHandler processHandler = descriptor.getProcessHandler();
              if (processHandler != null) {
                processHandler.addProcessListener(
                    new ProcessAdapter() {
                      @SuppressWarnings("ConstantConditions")
                      @Override
                      public void startNotified(ProcessEvent processEvent) {
                        Content content = descriptor.getAttachedContent();
                        if (content != null) {
                          content.setIcon(descriptor.getIcon());
                          if (!stopRunningMultirunConfiguration
                              .canContinueStartingConfigurations()) {
                            // Multirun was stopped - destroy processes that are still starting up
                            processHandler.destroyProcess();

                            if (!content.isPinned() && !startOneByOne) {
                              // checks if not pinned, to avoid destroying already existed tab
                              // checks if start one by one - no need to close the console tab, as
                              // it's won't be shown
                              // as other checks disallow starting it

                              // content.getManager() can be null, if content is removed already as
                              // part of destroy above
                              if (content.getManager() != null) {
                                content.getManager().removeContent(content, false);
                              }
                            }
                          } else {
                            // mark all current console tab as pinned
                            content.setPinned(true);

                            // mark running process tab with *
                            content.setDisplayName(descriptor.getDisplayName() + "*");
                          }
                        }
                      }

                      @Override
                      public void processTerminated(final ProcessEvent processEvent) {
                        onTermination(processEvent, true);
                      }

                      @Override
                      public void processWillTerminate(
                          ProcessEvent processEvent, boolean willBeDestroyed) {
                        onTermination(processEvent, false);
                      }

                      private void onTermination(
                          final ProcessEvent processEvent, final boolean terminated) {
                        if (descriptor.getAttachedContent() == null) {
                          return;
                        }

                        LaterInvocator.invokeLater(
                            new Runnable() {
                              @Override
                              public void run() {
                                final Content content = descriptor.getAttachedContent();
                                if (content == null) return;

                                // exit code is 0 if the process completed successfully
                                final boolean completedSuccessfully =
                                    (terminated && processEvent.getExitCode() == 0);

                                if (hideSuccessProcess && completedSuccessfully) {
                                  // close the tab for the success process and exit - nothing else
                                  // could be done
                                  if (content.getManager() != null) {
                                    content.getManager().removeContent(content, false);
                                    return;
                                  }
                                }

                                if (!separateTabs && completedSuccessfully) {
                                  // un-pin the console tab if re-use is allowed and process
                                  // completed successfully,
                                  // so the tab could be re-used for other processes
                                  content.setPinned(false);
                                }

                                // remove the * used to identify running process
                                content.setDisplayName(descriptor.getDisplayName());

                                // add the alert icon in case if process existed with non-0 status
                                if (markFailedProcess && processEvent.getExitCode() != 0) {
                                  LaterInvocator.invokeLater(
                                      new Runnable() {
                                        @Override
                                        public void run() {
                                          content.setIcon(
                                              LayeredIcon.create(
                                                  content.getIcon(), AllIcons.Nodes.TabAlert));
                                        }
                                      });
                                }
                              }
                            });
                      }
                    });
              }
              stopRunningMultirunConfiguration.addProcess(project, processHandler);

              if (startOneByOne) {
                // start next configuration..
                runConfigurations(executor, runConfigurations, index + 1);
              }
            }
          });
      started = true;
    } catch (ExecutionException e) {
      ExecutionUtil.handleExecutionError(
          project, executor.getToolWindowId(), configuration.getConfiguration(), e);
    } finally {
      // start the next one
      if (!startOneByOne) {
        runConfigurations(executor, runConfigurations, index + 1);
      } else if (!started) {
        // failed to start current, means the chain is broken
        runConfigurations(executor, runConfigurations, index + 1);
      }
    }
  }
 @Override
 public String getName() {
   return myImported && myConfiguration != null
       ? myConfiguration.getName()
       : myFile.getNameWithoutExtension();
 }