public static ILaunchConfigurationWorkingCopy setFailedTestsJvmArg(
     String value, ILaunchConfigurationWorkingCopy config) {
   try {
     String key = TestNGPlugin.getFailedTestsKey();
     String jvmargs = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "");
     String newarg = key + "=\"" + value + "\" ";
     if (!key.startsWith("-D")) newarg = "-D" + newarg;
     // if there is no value, then remove this jvm arg if there is one.
     if (value == "") newarg = " ";
     else newarg = " " + newarg;
     if (jvmargs.equals("")) {
       // simplest case: set the attribute
       config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, newarg);
     } else if (jvmargs.indexOf(key) == -1) {
       // nothing to replace; just add
       config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, jvmargs + newarg);
     } else {
       // find the new arg in the existing jvm args and replace it
       int start = jvmargs.indexOf(key);
       int next = jvmargs.indexOf("-D", start + 1);
       StringBuffer buf = new StringBuffer();
       buf.append(newarg).append(jvmargs.substring(0, start));
       if (next > start) {
         buf.append(jvmargs.substring(next));
       }
       config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, buf.toString());
     }
   } catch (CoreException ce) {
   }
   return config;
 }
  /**
   * Initialize a <code>ILaunchConfigurationWorkingCopy</code> either by using an existing one (if
   * found), or using a default configuration (for example, the one used for the most recent
   * launch), or by creating a new one based on the <code>project</code> name and the <code>confName
   * </code>.
   *
   * @throws CoreException if getting an working copy from an existing configuration fails
   */
  private static ILaunchConfigurationWorkingCopy createLaunchConfiguration(
      IProject project, String confName, RunInfo runInfo) {
    ILaunchManager launchManager = getLaunchManager();
    ILaunchConfiguration config =
        ConfigurationHelper.findConfiguration(launchManager, project, confName, runInfo);

    ILaunchConfigurationWorkingCopy configWC = null;
    if (null != config) {
      try {
        configWC = config.getWorkingCopy();
      } catch (CoreException cex) {
        TestNGPlugin.log(
            new Status(
                IStatus.ERROR,
                TestNGPlugin.PLUGIN_ID,
                TestNGPluginConstants.LAUNCH_ERROR,
                "Cannot create working copy of existing launcher " + config.getName(),
                cex));
      }
    }
    if (null == configWC) {
      if (confName == null && runInfo != null) {
        confName = runInfo.getClassName() + "." + runInfo.getMethodName();
      }
      configWC = ConfigurationHelper.createBasicConfiguration(launchManager, project, confName);
    }

    return configWC;
  }
  /**
   * Saves the working copy (may return <tt>null</tt> if the <code>launchWorkingCopy</code> is
   * <tt>null</tt> or the save fails).
   */
  private static ILaunchConfiguration save(ILaunchConfigurationWorkingCopy launchWorkingCopy) {
    if (null == launchWorkingCopy) return null;

    try {
      return launchWorkingCopy.doSave();
    } catch (CoreException cex) {
      TestNGPlugin.log(cex);
    }

    return null;
  }
 protected void saveSuiteContent(final File file, final XMLStringBuffer content) {
   FileOutputStream fileOutputStream = null;
   BufferedOutputStream bufferedOutputStream = null;
   OutputStreamWriter osw = null;
   try {
     fileOutputStream = new FileOutputStream(file);
     bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
     // use utf8 to solve special character problem
     osw = new OutputStreamWriter(bufferedOutputStream, Charset.forName("UTF-8"));
     osw.write(content.getStringBuffer().toString());
   } catch (IOException ioException) {
     TestNGPlugin.log(ioException);
   } finally {
     try {
       if (osw != null) osw.close();
       if (bufferedOutputStream != null) bufferedOutputStream.close();
       if (fileOutputStream != null) fileOutputStream.close();
     } catch (Exception e) {
       TestNGPlugin.log(e);
     }
   }
 }
  /** Suite file launcher. The file may reside outside the workbench. */
  public static void launchFailedSuiteConfiguration(
      IJavaProject javaProject,
      String runMode,
      ILaunchConfiguration prevConfig,
      Set failureDescriptions) {
    final String suiteConfName =
        javaProject.getElementName() + "-" + FailedReporter.TESTNG_FAILED_XML;
    final String suiteFilePath =
        TestNGPlugin.getPluginPreferenceStore().getOutputAbsolutePath(javaProject).toOSString()
            + "/"
            + FailedReporter.TESTNG_FAILED_XML;

    launchSuiteConfiguration(
        javaProject.getProject(),
        suiteConfName,
        suiteFilePath,
        runMode,
        prevConfig,
        failureDescriptions);
  }
  protected XMLStringBuffer createContentBuffer() {
    PreferenceStoreUtil storage =
        new PreferenceStoreUtil(TestNGPlugin.getDefault().getPreferenceStore());
    String xmlFile = storage.getXmlTemplateFile(m_projectName, false /* not only project */);
    boolean hasEclipseXmlFile = !Utils.isStringEmpty(xmlFile);
    XMLStringBuffer suiteBuffer = new XMLStringBuffer(); // $NON-NLS-1$
    suiteBuffer.setDocType("suite SYSTEM \"" + Parser.TESTNG_DTD_URL + "\"");

    if (hasEclipseXmlFile) {
      createXmlFileFromTemplate(suiteBuffer, xmlFile);
    } else {
      createXmlFileFromParameters(suiteBuffer);
    }

    // Done with the top of the XML file, now generate the <test> elements
    initContentBuffer(suiteBuffer);

    suiteBuffer.pop("suite");

    return suiteBuffer;
  }
  /** Launch a compilation unit (a source file) based test. */
  public static void launchCompilationUnitConfiguration(
      IJavaProject ijp, ICompilationUnit icu, String mode) {
    IType[] types = null;
    try {
      types = icu.getTypes();
    } catch (JavaModelException jme) {
      TestNGPlugin.log(
          new Status(
              IStatus.ERROR,
              TestNGPlugin.PLUGIN_ID,
              TestNGPluginConstants.LAUNCH_ERROR,
              "No types in compilation unit " + icu.getElementName(),
              jme));
    }

    if (null == types) return;

    IType mainType = icu.findPrimaryType();
    final String confName = mainType != null ? mainType.getElementName() : icu.getElementName();

    launchTypeBasedConfiguration(ijp, confName, types, mode);
  }
  public static void launchMapConfiguration(
      IProject project,
      String configName,
      Map launchAttributes,
      ICompilationUnit compilationUnit,
      String launchMode) {
    ILaunchConfigurationWorkingCopy workingCopy =
        createLaunchConfiguration(project, configName, null);

    try {
      launchAttributes.putAll(workingCopy.getAttributes());
    } catch (CoreException ce) {
      TestNGPlugin.log(ce);
    }

    if (null != compilationUnit) {
      Map params = solveParameters(compilationUnit);
      launchAttributes.put(TestNGLaunchConfigurationConstants.PARAMS, params);
    }

    workingCopy.setAttributes(launchAttributes);

    runConfig(workingCopy, launchMode);
  }
  private void createUi(Composite wizardParent) {
    Composite control = new Composite(wizardParent, SWT.NONE);
    SWTUtil.createGridLayout(control, 1);
    control.setLayout(new GridLayout());
    control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    //
    // "Generate testng.xml" box
    //
    m_generateBox = new Button(control, SWT.CHECK);
    m_generateBox.setText("Generate testng.xml");
    m_generateBox.setSelection(true);

    final Group group = new Group(control, SWT.NONE);
    {
      group.setLayout(new GridLayout());
      GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
      group.setLayoutData(gd);
      group.setEnabled(true);
    }

    m_generateBox.addSelectionListener(
        new SelectionListener() {

          @Override
          public void widgetSelected(SelectionEvent e) {
            group.setEnabled(((Button) e.getSource()).getSelection());
          }

          @Override
          public void widgetDefaultSelected(SelectionEvent e) {}
        });

    Composite parent = SWTUtil.createGridContainer(group, 3);
    parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    //
    // Location
    //
    m_xmlFile = SWTUtil.createPathBrowserText(parent, "Location:", null);
    List<JavaElement> elements = Utils.getSelectedJavaElements();
    if (elements.size() > 0) {
      m_xmlFile.setText(elements.get(0).getProject().getPath() + "/testng.xml");
    }

    //
    // Suite/test name
    //
    m_suiteText = addTextLabel(parent, "Suite name:");
    m_suiteText.setText(getDefaultSuiteName());
    m_testText = addTextLabel(parent, "Test name:");
    m_testText.setText(getDefaultTestName());

    Composite horizontal = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(6, true);
    horizontal.setLayout(layout);
    {
      GridData gd = new GridData();
      gd.horizontalSpan = 3;
      horizontal.setLayoutData(gd);
    }

    //
    // Selection combo
    //
    {
      Label l = new Label(horizontal, SWT.NONE);
      l.setText("Class selection:");
      m_selectionCombo = new Combo(horizontal, SWT.READ_ONLY);
      m_selectionCombo.add(Selection.CLASSES.toString());
      m_selectionCombo.add(Selection.PACKAGES.toString());
      m_selectionCombo.select(0);
    }

    //
    // Parallel mode
    //
    {
      Label l = new Label(horizontal, SWT.NONE);
      l.setText("Parallel mode:");
      m_parallelCombo = new Combo(horizontal, SWT.READ_ONLY);
      m_parallelCombo.add(XmlSuite.ParallelMode.NONE.toString());
      m_parallelCombo.add(XmlSuite.ParallelMode.METHODS.toString());
      m_parallelCombo.add(XmlSuite.ParallelMode.CLASSES.toString());
      m_parallelCombo.add(XmlSuite.ParallelMode.TESTS.toString());
      m_parallelCombo.select(0);
    }

    //
    // Thread count
    //
    {
      Label l = new Label(horizontal, SWT.NONE);
      l.setText("Thread count:");
      m_threadCountText = new Text(horizontal, SWT.BORDER);
    }

    //
    // Preview text
    //
    {
      Label previewLabelText = new Label(parent, SWT.NONE);
      previewLabelText.setText("Preview");
      GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
      gd.horizontalSpan = 3;
      previewLabelText.setLayoutData(gd);
    }

    {
      m_previewText = new Text(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
      GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
      gd.horizontalSpan = 3;
      m_previewText.setLayoutData(gd);
    }

    //
    // "Code generation" box
    //
    m_codeGenerationBox = new Label(control, SWT.CHECK);
    m_codeGenerationBox.setText("Code generation");

    final Group group2 = new Group(control, SWT.NONE);
    {
      RowLayout gl = new RowLayout();
      //      GridLayout gl = new GridLayout(2, true /* same size  columns */);
      group2.setLayout(gl);
      GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
      group2.setLayoutData(gd);
      group2.setEnabled(true);
    }

    {
      Label l = new Label(group2, SWT.NONE);
      l.setText("suite() methods:");

      m_suiteMethodCombo = new Combo(group2, SWT.READ_ONLY);
      m_suiteMethodCombo.add("Remove");
      m_suiteMethodCombo.add("Comment out");
      m_suiteMethodCombo.add("Don't touch");
      SuiteMethodTreatment lastValue =
          TestNGPlugin.getPluginPreferenceStore().getSuiteMethodTreatement();
      m_suiteMethodCombo.select(lastValue.ordinal());
      m_suiteMethodCombo.addSelectionListener(
          new SelectionListener() {

            @Override
            public void widgetSelected(SelectionEvent e) {
              TestNGPlugin.getPluginPreferenceStore()
                  .storeSuiteMethodTreatement(m_suiteMethodCombo.getSelectionIndex());
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e) {}
          });
    }

    setControl(control);
  }