public void testRunningJUnit() throws ExecutionException {
   PsiClass testA = findTestA(getModule1());
   JUnitConfiguration configuration = createConfiguration(testA);
   JavaParameters parameters = checkCanRun(configuration);
   assertEmpty(parameters.getVMParametersList().getList());
   final SegmentedOutputStream notifications = new SegmentedOutputStream(System.out);
   assertTrue(
       JUnitStarter.checkVersion(
           parameters.getProgramParametersList().getArray(), new PrintStream(notifications)));
   assertTrue(parameters.getProgramParametersList().getList().contains(testA.getQualifiedName()));
   assertEquals(JUnitStarter.class.getName(), parameters.getMainClass());
   assertEquals(myJdk.getHomeDirectory().getPresentableUrl(), parameters.getJdkPath());
 }
  protected void initialize() throws ExecutionException {
    JavaParametersUtil.configureConfiguration(myJavaParameters, myConfiguration);
    myJavaParameters.setMainClass(JUnitConfiguration.JUNIT_START_CLASS);
    final Module module = myConfiguration.getConfigurationModule().getModule();
    if (myJavaParameters.getJdk() == null) {
      myJavaParameters.setJdk(
          module != null
              ? ModuleRootManager.getInstance(module).getSdk()
              : ProjectRootManager.getInstance(myProject).getProjectSdk());
    }

    myJavaParameters.getClassPath().add(JavaSdkUtil.getIdeaRtJarPath());
    myJavaParameters.getClassPath().add(PathUtil.getJarPathForClass(JUnitStarter.class));
    myJavaParameters
        .getProgramParametersList()
        .add(JUnitStarter.IDE_VERSION + JUnitStarter.VERSION);
    for (RunConfigurationExtension ext :
        Extensions.getExtensions(RunConfigurationExtension.EP_NAME)) {
      ext.updateJavaParameters(myConfiguration, myJavaParameters, getRunnerSettings());
    }

    final Object[] listeners = Extensions.getExtensions(IDEAJUnitListener.EP_NAME);
    final StringBuilder buf = new StringBuilder();
    for (final Object listener : listeners) {
      boolean enabled = true;
      for (RunConfigurationExtension ext :
          Extensions.getExtensions(RunConfigurationExtension.EP_NAME)) {
        if (ext.isListenerDisabled(myConfiguration, listener, getRunnerSettings())) {
          enabled = false;
          break;
        }
      }
      if (enabled) {
        final Class classListener = listener.getClass();
        buf.append(classListener.getName()).append("\n");
        myJavaParameters.getClassPath().add(PathUtil.getJarPathForClass(classListener));
      }
    }
    if (buf.length() > 0) {
      try {
        myListenersFile = FileUtil.createTempFile("junit_listeners_", "");
        myListenersFile.deleteOnExit();
        myJavaParameters.getProgramParametersList().add("@@" + myListenersFile.getPath());
        FileUtil.writeToFile(myListenersFile, buf.toString().getBytes());
      } catch (IOException e) {
        LOG.error(e);
      }
    }
  }
  private JavaParameters createJavaParameters() throws ExecutionException {
    JavaParameters parameters = new JavaParameters();
    Sdk sdk = JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk();
    if (sdk == null) {
      throw new ExecutionException("No Java SDK available.");
    }
    parameters.setJdk(sdk);
    parameters.setMainClass(AbstractPaxBasedFrameworkRunner.PaxRunnerMainClass);
    PathsList classpath = parameters.getClassPath();
    for (VirtualFile libraryFile : AbstractPaxBasedFrameworkRunner.getPaxLibraries()) {
      classpath.add(libraryFile);
    }

    ParametersList parametersList = parameters.getProgramParametersList();
    parametersList.add("--p=" + myFrameworkType);
    if (!StringUtil.isEmpty(myVersion)) {
      parametersList.add("--v=" + myVersion);
    }
    parametersList.add("--nologo=true");
    parametersList.add("--executor=noop");
    parametersList.add("--workingDirectory=" + myTargetFolder);
    if (myClearDownloadFolder) {
      parametersList.add("--clean");
    }
    if (!StringUtil.isEmpty(myProfiles)) {
      parametersList.add("--profiles=" + myProfiles);
    }
    return parameters;
  }
 protected void createServerSocket(JavaParameters javaParameters) {
   try {
     myServerSocket = new ServerSocket(0, 0, InetAddress.getByName("127.0.0.1"));
     javaParameters.getProgramParametersList().add("-socket" + myServerSocket.getLocalPort());
   } catch (IOException e) {
     LOG.error(e);
   }
 }
 protected void createTempFiles(JavaParameters javaParameters) {
   try {
     myTempFile = FileUtil.createTempFile("idea_" + getFrameworkId(), ".tmp");
     myTempFile.deleteOnExit();
     passTempFile(javaParameters.getProgramParametersList(), myTempFile.getAbsolutePath());
   } catch (IOException e) {
     LOG.error(e);
   }
 }
  protected static void addClasspathFromRootModel(
      @Nullable Module module, boolean isTests, JavaParameters params, boolean allowDuplication)
      throws CantRunException {
    PathsList nonCore = new PathsList();
    getClassPathFromRootModel(module, isTests, params, allowDuplication, nonCore);
    nonCore.add(".");

    final String cp = nonCore.getPathsString();
    if (!StringUtil.isEmptyOrSpaces(cp)) {
      params.getProgramParametersList().add("--classpath");
      params.getProgramParametersList().add(cp);
    }
  }
 private static List<String> extractAllInPackageTests(
     JavaParameters parameters, PsiPackage psiPackage) throws IOException {
   String filePath =
       ContainerUtil.find(
               parameters.getProgramParametersList().getArray(),
               value ->
                   StringUtil.startsWithChar(value, '@') && !StringUtil.startsWith(value, "@w@"))
           .substring(1);
   List<String> lines = readLinesFrom(new File(filePath));
   assertEquals(psiPackage.getQualifiedName(), lines.get(0));
   // lines.remove(0);
   lines.remove(0);
   return lines;
 }
  private void appendForkInfo(Executor executor) throws ExecutionException {
    final String forkMode = myConfiguration.getForkMode();
    if (Comparing.strEqual(forkMode, "none")) {
      return;
    }

    if (getRunnerSettings().getData() != null) {
      final String actionName = executor.getActionName();
      throw new CantRunException(
          actionName
              + " is disabled in fork mode.<br/>Please change fork mode to &lt;none&gt; to "
              + actionName.toLowerCase()
              + ".");
    }

    final JavaParameters javaParameters = getJavaParameters();
    final Sdk jdk = javaParameters.getJdk();
    if (jdk == null) {
      throw new ExecutionException(
          ExecutionBundle.message("run.configuration.error.no.jdk.specified"));
    }

    try {
      final File tempFile = FileUtil.createTempFile("command.line", "", true);
      final PrintWriter writer = new PrintWriter(tempFile, "UTF-8");
      try {
        writer.println(((JavaSdkType) jdk.getSdkType()).getVMExecutablePath(jdk));
        for (String vmParameter : javaParameters.getVMParametersList().getList()) {
          writer.println(vmParameter);
        }
        writer.println("-classpath");
        writer.println(javaParameters.getClassPath().getPathsString());
      } finally {
        writer.close();
      }

      myJavaParameters
          .getProgramParametersList()
          .add("@@@" + forkMode + ',' + tempFile.getAbsolutePath());
    } catch (Exception e) {
      LOG.error(e);
    }
  }
  @Override
  protected JavaParameters createJavaParameters() throws ExecutionException {
    final JavaParameters javaParameters = new JavaParameters();
    final Module module = getConfiguration().getConfigurationModule().getModule();

    Project project = getConfiguration().getProject();
    Sdk jdk =
        module == null
            ? ProjectRootManager.getInstance(project).getProjectSdk()
            : ModuleRootManager.getInstance(module).getSdk();
    javaParameters.setJdk(jdk);

    final Object[] patchers = Extensions.getExtensions(ExtensionPoints.JUNIT_PATCHER);
    for (Object patcher : patchers) {
      ((JUnitPatcher) patcher).patchJavaParameters(module, javaParameters);
    }

    // Append coverage parameters if appropriate
    for (RunConfigurationExtension ext :
        Extensions.getExtensions(RunConfigurationExtension.EP_NAME)) {
      ext.updateJavaParameters(getConfiguration(), javaParameters, getRunnerSettings());
    }

    final String parameters = getConfiguration().getProgramParameters();
    getConfiguration().setProgramParameters(null);
    try {
      JavaParametersUtil.configureConfiguration(javaParameters, getConfiguration());
    } finally {
      getConfiguration().setProgramParameters(parameters);
    }
    javaParameters.getClassPath().addFirst(JavaSdkUtil.getIdeaRtJarPath());
    configureClasspath(javaParameters);

    if (!StringUtil.isEmptyOrSpaces(parameters)) {
      javaParameters.getProgramParametersList().add("@name" + parameters);
    }

    return javaParameters;
  }
  protected <T> void addClassesListToJavaParameters(
      Collection<? extends T> elements,
      Function<T, String> nameFunction,
      String packageName,
      boolean createTempFile,
      boolean junit4) {
    try {
      if (createTempFile) {
        myTempFile = FileUtil.createTempFile("idea_junit", ".tmp");
        myTempFile.deleteOnExit();
        myJavaParameters.getProgramParametersList().add("@" + myTempFile.getAbsolutePath());
      }

      final PrintWriter writer = new PrintWriter(myTempFile, "UTF-8");
      try {
        writer.println(packageName);
        final List<String> testNames = new ArrayList<String>();
        for (final T element : elements) {
          final String name = nameFunction.fun(element);
          if (name == null) {
            LOG.error("invalid element " + element);
            return;
          }
          testNames.add(name);
        }
        Collections.sort(testNames); // sort tests in FQN order
        for (String testName : testNames) {
          writer.println(testName);
        }
      } finally {
        writer.close();
      }
    } catch (IOException e) {
      LOG.error(e);
    }
  }
  @Override
  public JavaParameters createJavaParameters(
      @NotNull Module module,
      boolean forCreation,
      boolean forTests,
      boolean classpathFromDependencies,
      @Nullable String jvmParams,
      @NotNull MvcCommand command)
      throws ExecutionException {
    JavaParameters params = new JavaParameters();

    Sdk sdk = ModuleRootManager.getInstance(module).getSdk();

    params.setJdk(sdk);
    final VirtualFile sdkRoot = getSdkRoot(module);
    if (sdkRoot == null) {
      return params;
    }

    Map<String, String> env = params.getEnv();
    if (env == null) {
      env = new HashMap<String, String>();
      params.setEnv(env);
    }
    env.put(getSdkHomePropertyName(), FileUtil.toSystemDependentName(sdkRoot.getPath()));

    final VirtualFile lib = sdkRoot.findChild("lib");
    if (lib != null) {
      for (final VirtualFile child : lib.getChildren()) {
        final String name = child.getName();
        if (name.startsWith("groovy-all-") && name.endsWith(".jar")) {
          params.getClassPath().add(child);
        }
      }
    }
    final VirtualFile dist = sdkRoot.findChild("dist");
    if (dist != null) {
      for (final VirtualFile child : dist.getChildren()) {
        final String name = child.getName();
        if (name.endsWith(".jar")) {
          if (name.startsWith("griffon-cli-")
              || name.startsWith("griffon-rt-")
              || name.startsWith("griffon-resources-")) {
            params.getClassPath().add(child);
          }
        }
      }
    }

    /////////////////////////////////////////////////////////////

    params.setMainClass("org.codehaus.griffon.cli.support.GriffonStarter");

    final VirtualFile rootFile;

    if (forCreation) {
      VirtualFile[] roots = ModuleRootManager.getInstance(module).getContentRoots();
      if (roots.length != 1) {
        throw new ExecutionException(
            "Failed to initialize griffon module: module "
                + module.getName()
                + " contains more than one root");
      }

      command.getArgs().add(0, roots[0].getName());

      rootFile = roots[0].getParent();
    } else {
      rootFile = findAppRoot(module);
      if (rootFile == null) {
        throw new ExecutionException(
            "Failed to run griffon command: module "
                + module.getName()
                + " is not a Griffon module");
      }
    }

    String workDir = VfsUtilCore.virtualToIoFile(rootFile).getAbsolutePath();

    if (jvmParams != null) {
      params.getVMParametersList().addParametersString(jvmParams);
    }

    if (!params.getVMParametersList().getParametersString().contains(XMX_JVM_PARAMETER)) {
      params.getVMParametersList().add("-Xmx256M");
    }

    final String griffonHomePath = FileUtil.toSystemDependentName(sdkRoot.getPath());
    params.getVMParametersList().add("-Dgriffon.home=" + griffonHomePath);
    params.getVMParametersList().add("-Dbase.dir=" + workDir);

    assert sdk != null;
    params
        .getVMParametersList()
        .add("-Dtools.jar=" + ((JavaSdkType) sdk.getSdkType()).getToolsPath(sdk));

    final String confpath = griffonHomePath + GROOVY_STARTER_CONF;
    params.getVMParametersList().add("-Dgroovy.starter.conf=" + confpath);

    params
        .getVMParametersList()
        .add(
            "-Dgroovy.sanitized.stacktraces=\"groovy., org.codehaus.groovy., java., javax., sun., gjdk.groovy., gant., org.codehaus.gant.\"");

    params.getProgramParametersList().add("--main");
    params.getProgramParametersList().add("org.codehaus.griffon.cli.GriffonScriptRunner");
    params.getProgramParametersList().add("--conf");
    params.getProgramParametersList().add(confpath);
    if (!forCreation && classpathFromDependencies) {
      final String path = getApplicationClassPath(module).getPathsString();
      if (StringUtil.isNotEmpty(path)) {
        params.getProgramParametersList().add("--classpath");
        params.getProgramParametersList().add(path);
      }
    }

    params.setWorkingDirectory(workDir);

    ParametersList paramList = new ParametersList();
    command.addToParametersList(paramList);
    params.getProgramParametersList().add(paramList.getParametersString());

    params.setDefaultCharset(module.getProject());

    return params;
  }
  /**
   * @param project
   * @param parameters
   * @param coreSettings
   * @param runnerSettings
   * @param runConfiguration used to creation fix if maven home not found
   * @return
   * @throws ExecutionException
   */
  public static JavaParameters createJavaParameters(
      @Nullable final Project project,
      @NotNull final MavenRunnerParameters parameters,
      @Nullable MavenGeneralSettings coreSettings,
      @Nullable MavenRunnerSettings runnerSettings,
      @Nullable MavenRunConfiguration runConfiguration)
      throws ExecutionException {
    final JavaParameters params = new JavaParameters();

    ApplicationManager.getApplication().assertReadAccessAllowed();

    if (coreSettings == null) {
      coreSettings =
          project == null
              ? new MavenGeneralSettings()
              : MavenProjectsManager.getInstance(project).getGeneralSettings();
    }
    if (runnerSettings == null) {
      runnerSettings =
          project == null ? new MavenRunnerSettings() : MavenRunner.getInstance(project).getState();
    }

    params.setWorkingDirectory(parameters.getWorkingDirFile());

    params.setJdk(
        getJdk(
            project,
            runnerSettings,
            project != null && MavenRunner.getInstance(project).getState() == runnerSettings));

    final String mavenHome = resolveMavenHome(coreSettings, project, runConfiguration);

    addVMParameters(params.getVMParametersList(), mavenHome, runnerSettings);

    File confFile = MavenUtil.getMavenConfFile(new File(mavenHome));
    if (!confFile.isFile()) {
      throw new ExecutionException(
          "Configuration file is not exists in maven home: " + confFile.getAbsolutePath());
    }

    if (project != null && parameters.isResolveToWorkspace()) {
      try {
        String resolverJar = getArtifactResolverJar(MavenUtil.isMaven3(mavenHome));
        confFile = patchConfFile(confFile, resolverJar);

        File modulesPathsFile = dumpModulesPaths(project);
        params
            .getVMParametersList()
            .addProperty(MavenModuleMap.PATHS_FILE_PROPERTY, modulesPathsFile.getAbsolutePath());
      } catch (IOException e) {
        LOG.error(e);
        throw new ExecutionException("Failed to run maven configuration", e);
      }
    }

    params.getVMParametersList().addProperty("classworlds.conf", confFile.getPath());

    for (String path : getMavenClasspathEntries(mavenHome)) {
      params.getClassPath().add(path);
    }

    params.setMainClass(MAVEN_LAUNCHER_CLASS);
    EncodingManager encodingManager =
        project == null
            ? EncodingProjectManager.getInstance()
            : EncodingProjectManager.getInstance(project);
    params.setCharset(encodingManager.getDefaultCharset());

    addMavenParameters(
        params.getProgramParametersList(), mavenHome, coreSettings, runnerSettings, parameters);

    return params;
  }
  protected JavaParameters createJavaParameters() throws ExecutionException {

    log.debug("createJavaParameters");

    SubstepsRunConfiguration runConfig =
        (SubstepsRunConfiguration) this.getEnvironment().getRunProfile();

    SubstepsRunnerConfigurationModel model = runConfig.getModel();

    JavaParameters params = model.getJavaParameters();

    params.setWorkingDirectory(model.getWorkingDir());

    if (params.getJdk() == null) {

      log.debug("params on the jdk is null");

      params.setJdk(
          JavaSdkImpl.getInstance().createJdk(model.getVersionString(), model.getHomePath()));

      params.getClassPath().add(model.getClassPathString());
    }

    params.getProgramParametersList().add("prog-args-env", "prg-localhost");

    ParametersList vmParametersList = params.getVMParametersList();

    vmParametersList.addParametersString("-Dfile.encoding=UTF-8");
    vmParametersList.addParametersString("-Dcom.sun.management.jmxremote.port=" + jmxPort);
    vmParametersList.addParametersString("-Dcom.sun.management.jmxremote.authenticate=false");
    vmParametersList.addParametersString("-Dcom.sun.management.jmxremote.ssl=false");
    vmParametersList.addParametersString("-Djava.rmi.server.hostname=localhost");

    //        String rmiClasspathString = "\"file://" +
    // model.getClassPathString().replaceAll(File.pathSeparator, " file://") + "\"";

    //        log.debug("rmi classpath: " + rmiClasspathString);

    //        vmParametersList.addParametersString("-Djava.rmi.server.codebase=" +
    // rmiClasspathString);

    vmParametersList.addParametersString("-Dsun.io.serialization.extendedDebugInfo=true");

    //        System.setProperty("java.rmi.server.codebase", rmiClasspathString);

    //        createServerSocket(params);
    //
    //
    //        if (myServerSocket != null) {
    //            params.getProgramParametersList().add(ForkedDebuggerHelper.DEBUG_SOCKET +
    // myServerSocket.getLocalPort());
    //        }
    //

    log.debug(
        "launching substeps runner with classpath: "
            + params.getClassPath().getPathsString()
            + "\njvm info: "
            + model.getHomePath()
            + " version: "
            + model.getVersionString());

    return params;
  }