public void testEditJUnitConfiguration() throws ConfigurationException {
    if (PlatformTestUtil.COVERAGE_ENABLED_BUILD) return;

    PsiClass testA = findTestA(getModule2());
    JUnitConfiguration configuration = createConfiguration(testA);
    JUnitConfigurable editor = new JUnitConfigurable(myProject);
    try {
      Configurable configurable = new RunConfigurationConfigurableAdapter(editor, configuration);
      configurable.reset();
      final EditorTextFieldWithBrowseButton component =
          ((LabeledComponent<EditorTextFieldWithBrowseButton>)
                  editor.getTestLocation(JUnitConfigurationModel.CLASS))
              .getComponent();
      assertEquals(testA.getQualifiedName(), component.getText());
      PsiClass otherTest = findClass(getModule2(), "test2.Test2");
      component.setText(otherTest.getQualifiedName());
      configurable.apply();
      assertEquals(otherTest.getName(), configuration.getName());
      String specialName = "My name";
      configuration.setName(specialName);
      configuration.setNameChangedByUser(true);
      configurable.reset();
      component.setText(testA.getQualifiedName());
      configurable.apply();
      assertEquals(specialName, configuration.getName());
    } finally {
      Disposer.dispose(editor);
    }
  }
  public void testCreateConfiguration() throws IOException, ExecutionException {
    Module module1 = getModule1();
    PsiClass psiClass = findTestA(module1);
    JUnitConfiguration configuration = createConfiguration(psiClass);
    assertEquals(
        Collections.singleton(module1), ContainerUtilRt.newHashSet(configuration.getModules()));
    checkClassName(psiClass.getQualifiedName(), configuration);
    assertEquals(psiClass.getName(), configuration.getName());
    checkTestObject(JUnitConfiguration.TEST_CLASS, configuration);
    Module module2 = getModule2();
    Assertion.compareUnordered(new Module[] {module1, module2}, configuration.getValidModules());

    PsiClass innerTest = findClass(module1, INNER_TEST_NAME);
    configuration =
        createJUnitConfiguration(
            innerTest, TestClassConfigurationProducer.class, new MapDataContext());
    checkClassName(RT_INNER_TEST_NAME, configuration);
    checkCanRun(configuration);

    PsiMethod[] testMethod = innerTest.findMethodsByName("test", false);
    assertEquals(1, testMethod.length);
    configuration = createConfiguration(testMethod[0]);
    checkClassName(RT_INNER_TEST_NAME, configuration);
    checkMethodName("test", configuration);
    checkTestObject(JUnitConfiguration.TEST_METHOD, configuration);
    checkCanRun(configuration);

    PsiMethod mainMethod = innerTest.findMethodsByName("main", false)[0];
    ApplicationConfiguration appConfiguration = createConfiguration(mainMethod);
    assertEquals(RT_INNER_TEST_NAME, appConfiguration.MAIN_CLASS_NAME);
    checkCanRun(configuration);
  }
  public void testAllInPackageForProject() throws IOException, ExecutionException {
    // module1 -> module2 -> module3
    // module5
    addModule("module5");
    addDependency(getModule1(), getModule2());
    addDependency(getModule2(), getModule3());
    String[][] outputs = new String[4][];
    for (int i = 0; i < 4; i++) {
      outputs[i] = addOutputs(getModule(i), i + 1);
    }

    PsiPackage defaultPackage = JavaPsiFacade.getInstance(myProject).findPackage("");
    JUnitConfiguration configuration =
        createJUnitConfiguration(
            defaultPackage, AllInPackageConfigurationProducer.class, new MapDataContext());
    configuration.getPersistentData().setScope(TestSearchScope.WHOLE_PROJECT);
    JavaParameters javaParameters = checkCanRun(configuration);
    String classPath = javaParameters.getClassPath().getPathsString();
    assertEquals(-1, classPath.indexOf(JarFileSystem.PROTOCOL_PREFIX));
    assertEquals(-1, classPath.indexOf(LocalFileSystem.PROTOCOL_PREFIX));
    for (int i = 0; i < 4; i++) {
      checkContains(classPath, outputs[i][0]);
      checkContains(classPath, outputs[i][1]);
    }
  }
 public void checkConfiguration() throws RuntimeConfigurationException {
   JavaParametersUtil.checkAlternativeJRE(myConfiguration);
   ProgramParametersUtil.checkWorkingDirectoryExist(
       myConfiguration,
       myConfiguration.getProject(),
       myConfiguration.getConfigurationModule().getModule());
 }
  public void testTestClassPathWhenRunningConfigurations() throws IOException, ExecutionException {
    addModule("module4", false);
    Module module4 = getModule4();
    assignJdk(module4);
    addSourcePath(module4, "testSrc", true);
    addSourcePath(module4, "src", false);
    String output = setCompilerOutput(module4, "classes", false);
    String testOuput = setCompilerOutput(module4, "testClasses", true);

    ApplicationConfiguration applicationConfiguration =
        createConfiguration(findClass(module4, "Application"));
    JavaParameters parameters = checkCanRun(applicationConfiguration);
    String classPath = parameters.getClassPath().getPathsString();
    checkDoesNotContain(classPath, testOuput);
    checkContains(classPath, output);

    JUnitConfiguration junitConfiguration =
        createJUnitConfiguration(
            findClass(module4, "TestApplication"),
            TestClassConfigurationProducer.class,
            new MapDataContext());
    parameters = checkCanRun(junitConfiguration);
    classPath = parameters.getClassPath().getPathsString();
    checkContains(classPath, testOuput);
    checkContains(classPath, output);

    applicationConfiguration.MAIN_CLASS_NAME =
        junitConfiguration.getPersistentData().getMainClassName();
    classPath = checkCanRun(applicationConfiguration).getClassPath().getPathsString();
    checkContains(classPath, testOuput);
    checkContains(classPath, output);
  }
  public void testModulesSelector() throws ConfigurationException {
    if (PlatformTestUtil.COVERAGE_ENABLED_BUILD) return;

    Module module1 = getModule1();
    Module module2 = getModule2();
    JUnitConfigurable editor = new JUnitConfigurable(myProject);
    try {
      JUnitConfiguration configuration = createConfiguration(findTestA(module2));
      editor.getComponent(); // To get all the watchers installed.
      Configurable configurable = new RunConfigurationConfigurableAdapter(editor, configuration);
      JComboBox comboBox = editor.getModulesComponent();
      configurable.reset();
      assertFalse(configurable.isModified());
      assertEquals(module2.getName(), ((Module) comboBox.getSelectedItem()).getName());
      assertEquals(
          ModuleManager.getInstance(myProject).getModules().length + 1,
          comboBox.getModel().getSize()); // no module
      comboBox.setSelectedItem(module1);
      assertTrue(configurable.isModified());
      configurable.apply();
      assertFalse(configurable.isModified());
      assertEquals(
          Collections.singleton(module1), ContainerUtilRt.newHashSet(configuration.getModules()));
    } finally {
      Disposer.dispose(editor);
    }
  }
 private JUnitConfiguration createConfiguration(PsiPackage psiPackage, Module module) {
   JUnitConfiguration configuration =
       new JUnitConfiguration(
           "", myProject, JUnitConfigurationType.getInstance().getConfigurationFactories()[0]);
   configuration.getPersistentData().TEST_OBJECT = JUnitConfiguration.TEST_PACKAGE;
   configuration.getPersistentData().PACKAGE_NAME = psiPackage.getQualifiedName();
   configuration.getPersistentData().setScope(TestSearchScope.WHOLE_PROJECT);
   configuration.setModule(module);
   return configuration;
 }
 protected void configureModule(
     final JavaParameters parameters,
     final RunConfigurationModule configurationModule,
     final String mainClassName)
     throws CantRunException {
   int classPathType =
       JavaParametersUtil.getClasspathType(configurationModule, mainClassName, true);
   JavaParametersUtil.configureModule(
       configurationModule,
       parameters,
       classPathType,
       myConfiguration.isAlternativeJrePathEnabled()
           ? myConfiguration.getAlternativeJrePath()
           : null);
 }
 @Override
 public JavaParameters getJavaParameters() throws ExecutionException {
   if (myJavaParameters == null) {
     myJavaParameters = new JavaParameters();
     initialize();
     final Module module = myConfiguration.getConfigurationModule().getModule();
     final Object[] patchers = Extensions.getExtensions(ExtensionPoints.JUNIT_PATCHER);
     for (Object patcher : patchers) {
       ((JUnitPatcher) patcher).patchJavaParameters(module, myJavaParameters);
     }
   }
   return myJavaParameters;
 }
 public void testAllInPackageForCommonAncestorModule() throws IOException, ExecutionException {
   disposeModule(getModule2());
   addModule("module5", true);
   Module ancestor = getModule1();
   Module child1 = getModule2();
   Module child2 = getModule3();
   addDependency(ancestor, child1);
   addDependency(ancestor, child2);
   PsiPackage psiPackage = JavaPsiFacade.getInstance(myProject).findPackage("test1");
   JUnitConfiguration configuration =
       createJUnitConfiguration(
           psiPackage, AllInPackageConfigurationProducer.class, new MapDataContext());
   configuration.getPersistentData().setScope(TestSearchScope.WHOLE_PROJECT);
   assertNotNull(configuration);
   checkPackage(psiPackage.getQualifiedName(), configuration);
   assertEmpty(configuration.getModules());
   JavaParameters parameters = checkCanRun(configuration);
   List<String> tests = extractAllInPackageTests(parameters, psiPackage);
   String childTest1 = findClass(child1, "test1.TestB").getQualifiedName();
   String childTest2 = findClass(child2, "test1.Test5").getQualifiedName();
   String ancestorTest = findClass(ancestor, "test1.TestA").getQualifiedName();
   CHECK.containsAll(tests, new Object[] {ancestorTest, childTest1, childTest2});
 }
  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);
      }
    }
  }
  public void testExternalizeJUnitConfiguration()
      throws WriteExternalException, InvalidDataException {
    JUnitConfiguration configuration = createConfiguration(findTestA(getModule1()));
    Element element = new Element("cfg");
    configuration.writeExternal(element);
    JUnitConfiguration newCfg =
        new JUnitConfiguration(
            null, myProject, JUnitConfigurationType.getInstance().getConfigurationFactories()[0]);

    newCfg.readExternal(element);
    checkTestObject(configuration.getPersistentData().TEST_OBJECT, newCfg);
    assertEquals(
        Collections.singleton(getModule1()), ContainerUtilRt.newHashSet(newCfg.getModules()));
    checkClassName(configuration.getPersistentData().getMainClassName(), newCfg);
  }
  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);
    }
  }
  public void testRunAllInPackageWhenPackageIsEmptyInModule() throws ExecutionException {
    assignJdk(getModule2());
    JUnitConfiguration configuration =
        new JUnitConfiguration(
            "", myProject, JUnitConfigurationType.getInstance().getConfigurationFactories()[0]);
    configuration.getPersistentData().TEST_OBJECT = JUnitConfiguration.TEST_PACKAGE;
    configuration.getPersistentData().PACKAGE_NAME = "test2";
    configuration.getPersistentData().setScope(TestSearchScope.WHOLE_PROJECT);
    assertEmpty(configuration.getModules());
    checkCanRun(configuration);
    configuration.getPersistentData().PACKAGE_NAME = "noTests";
    //    checkCantRun(configuration, "No tests found in the package '");

    configuration.getPersistentData().PACKAGE_NAME = "com.abcent";
    checkCantRun(configuration, "Package 'com.abcent' not found");
  }
 public SourceScope getSourceScope() {
   return SourceScope.modulesWithDependencies(myConfiguration.getModules());
 }