execution: extract check for alternative jre; check for working directory (IDEA-77754)
authorAnna Kozlova <anna.kozlova@jetbrains.com>
Mon, 30 Jan 2012 11:37:29 +0000 (15:37 +0400)
committerAnna Kozlova <anna.kozlova@jetbrains.com>
Mon, 30 Jan 2012 14:09:17 +0000 (18:09 +0400)
java/execution/impl/src/com/intellij/execution/application/ApplicationConfiguration.java
java/execution/impl/src/com/intellij/execution/util/JavaParametersUtil.java
platform/lang-impl/src/com/intellij/execution/util/ProgramParametersUtil.java
plugins/junit/src/com/intellij/execution/junit/TestDirectory.java
plugins/junit/src/com/intellij/execution/junit/TestObject.java
plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGConfiguration.java

index 1f9444abfad166526cbdc92db508691190ca9a48..69d02d7782563bbbedcf8f706f509a0b178e7870 100644 (file)
@@ -24,6 +24,7 @@ import com.intellij.execution.junit.RefactoringListeners;
 import com.intellij.execution.process.OSProcessHandler;
 import com.intellij.execution.runners.ExecutionEnvironment;
 import com.intellij.execution.util.JavaParametersUtil;
+import com.intellij.execution.util.ProgramParametersUtil;
 import com.intellij.openapi.components.PathMacroManager;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.extensions.Extensions;
@@ -31,7 +32,6 @@ import com.intellij.openapi.module.Module;
 import com.intellij.openapi.options.SettingsEditor;
 import com.intellij.openapi.options.SettingsEditorGroup;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
 import com.intellij.openapi.util.Comparing;
 import com.intellij.openapi.util.DefaultJDOMExternalizer;
 import com.intellij.openapi.util.InvalidDataException;
@@ -138,19 +138,13 @@ public class ApplicationConfiguration extends ModuleBasedConfiguration<JavaRunCo
   }
 
   public void checkConfiguration() throws RuntimeConfigurationException {
-    if (ALTERNATIVE_JRE_PATH_ENABLED){
-      if (ALTERNATIVE_JRE_PATH == null ||
-          ALTERNATIVE_JRE_PATH.length() == 0 ||
-          !JavaSdkImpl.checkForJre(ALTERNATIVE_JRE_PATH)){
-        throw new RuntimeConfigurationWarning(ExecutionBundle.message("jre.path.is.not.valid.jre.home.error.mesage", ALTERNATIVE_JRE_PATH));
-      }
-    }
+    JavaParametersUtil.checkAlternativeJRE(this);
     final JavaRunConfigurationModule configurationModule = getConfigurationModule();
     final PsiClass psiClass = configurationModule.checkModuleAndClassName(MAIN_CLASS_NAME, ExecutionBundle.message("no.main.class.specified.error.text"));
     if (!PsiMethodUtil.hasMainMethod(psiClass)) {
       throw new RuntimeConfigurationWarning(ExecutionBundle.message("main.method.not.found.in.class.error.message", MAIN_CLASS_NAME));
     }
-
+    ProgramParametersUtil.checkWorkingDirectoryExist(this, getProject(), configurationModule.getModule());
     JavaRunConfigurationExtensionManager.checkConfigurationIsValid(this);
   }
 
index bb8f62966e2164cf53a8fd07fd9f2bf2299b28d0..e19d2f9d9e6c50deb77a61c58f72414cb96c0d67 100644 (file)
@@ -17,9 +17,11 @@ package com.intellij.execution.util;
 
 import com.intellij.execution.CantRunException;
 import com.intellij.execution.CommonJavaRunConfigurationParameters;
+import com.intellij.execution.ExecutionBundle;
 import com.intellij.execution.JavaExecutionUtil;
 import com.intellij.execution.configurations.JavaParameters;
 import com.intellij.execution.configurations.RunConfigurationModule;
+import com.intellij.execution.configurations.RuntimeConfigurationWarning;
 import com.intellij.execution.configurations.SimpleJavaParameters;
 import com.intellij.execution.junit.JUnitUtil;
 import com.intellij.openapi.module.Module;
@@ -122,4 +124,15 @@ public class JavaParametersUtil {
     if (jdk == null) throw CantRunException.noJdkConfigured();
     return jdk;
   }
+
+  public static void checkAlternativeJRE(CommonJavaRunConfigurationParameters configuration) throws RuntimeConfigurationWarning {
+    if (configuration.isAlternativeJrePathEnabled()) {
+      if (configuration.getAlternativeJrePath() == null ||
+          configuration.getAlternativeJrePath().length() == 0 ||
+          !JavaSdk.checkForJre(configuration.getAlternativeJrePath())) {
+        throw new RuntimeConfigurationWarning(
+          ExecutionBundle.message("jre.path.is.not.valid.jre.home.error.mesage", configuration.getAlternativeJrePath()));
+      }
+    }
+  }
 }
index d5057096f40fa1be43de363a2a78c1874213eb33..2f7adf63a796c6f45ab9802fde8b34e5aadd5596 100644 (file)
@@ -17,6 +17,7 @@ package com.intellij.execution.util;
 
 import com.intellij.execution.CommonProgramRunConfigurationParameters;
 import com.intellij.execution.configurations.ModuleBasedConfiguration;
+import com.intellij.execution.configurations.RuntimeConfigurationWarning;
 import com.intellij.execution.configurations.SimpleProgramParameters;
 import com.intellij.openapi.components.PathMacroManager;
 import com.intellij.openapi.module.Module;
@@ -26,6 +27,7 @@ import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.util.PathUtil;
 import org.jetbrains.annotations.Nullable;
 
+import java.io.File;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -36,6 +38,19 @@ public class ProgramParametersUtil {
 
     parameters.getProgramParametersList().addParametersString(configuration.getProgramParameters());
 
+    parameters.setWorkingDirectory(getWorkingDir(configuration, project, module));
+
+    parameters.setupEnvs(configuration.getEnvs(), configuration.isPassParentEnvs());
+    if (parameters.getEnv() != null) {
+      Map<String, String> expanded = new HashMap<String, String>();
+      for (Map.Entry<String, String> each : parameters.getEnv().entrySet()) {
+        expanded.put(each.getKey(), expandPath(each.getValue(), module, project));
+      }
+      parameters.setEnv(expanded);
+    }
+  }
+
+  public static String getWorkingDir(CommonProgramRunConfigurationParameters configuration, Project project, Module module) {
     String workingDirectory = configuration.getWorkingDirectory();
     VirtualFile baseDir = project.getBaseDir();
 
@@ -46,15 +61,14 @@ public class ProgramParametersUtil {
     if (!FileUtil.isAbsolute(workingDirectory) && baseDir != null) {
       workingDirectory = baseDir.getPath() + "/" + workingDirectory;
     }
-    parameters.setWorkingDirectory(workingDirectory);
+    return workingDirectory;
+  }
 
-    parameters.setupEnvs(configuration.getEnvs(), configuration.isPassParentEnvs());
-    if (parameters.getEnv() != null) {
-      Map<String, String> expanded = new HashMap<String, String>();
-      for (Map.Entry<String, String> each : parameters.getEnv().entrySet()) {
-        expanded.put(each.getKey(), expandPath(each.getValue(), module, project));
-      }
-      parameters.setEnv(expanded);
+  public static void checkWorkingDirectoryExist(CommonProgramRunConfigurationParameters configuration, Project project, Module module)
+    throws RuntimeConfigurationWarning {
+    final String workingDir = getWorkingDir(configuration, project, module);
+    if (!new File(workingDir).exists()) {
+      throw new RuntimeConfigurationWarning("Working directory '" + workingDir + "' doesn't exist");
     }
   }
 
index a6d4607f9cc1ae51ea02a528c749fb52595a414a..057ab85becf0663366daf5147b691790879c2ccd 100644 (file)
 package com.intellij.execution.junit;
 
 import com.intellij.execution.CantRunException;
-import com.intellij.execution.ExecutionBundle;
 import com.intellij.execution.configurations.*;
 import com.intellij.execution.testframework.SourceScope;
+import com.intellij.execution.util.JavaParametersUtil;
+import com.intellij.execution.util.ProgramParametersUtil;
 import com.intellij.openapi.module.Module;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.projectRoots.JavaSdk;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VirtualFile;
-import com.intellij.psi.*;
+import com.intellij.psi.JavaDirectoryService;
+import com.intellij.psi.PsiDirectory;
+import com.intellij.psi.PsiManager;
+import com.intellij.psi.PsiPackage;
 import com.intellij.psi.search.GlobalSearchScope;
 import com.intellij.psi.search.GlobalSearchScopes;
 
@@ -80,14 +83,8 @@ class TestDirectory extends TestPackage {
 
   @Override
   public void checkConfiguration() throws RuntimeConfigurationException {
-    if (myConfiguration.isAlternativeJrePathEnabled()) {
-      if (myConfiguration.getAlternativeJrePath() == null ||
-          myConfiguration.getAlternativeJrePath().length() == 0 ||
-          !JavaSdk.checkForJre(myConfiguration.getAlternativeJrePath())) {
-        throw new RuntimeConfigurationWarning(
-          ExecutionBundle.message("jre.path.is.not.valid.jre.home.error.mesage", myConfiguration.getAlternativeJrePath()));
-      }
-    }
+    JavaParametersUtil.checkAlternativeJRE(myConfiguration);
+    ProgramParametersUtil.checkWorkingDirectoryExist(myConfiguration, myConfiguration.getProject(), myConfiguration.getConfigurationModule().getModule());
     final String dirName = myConfiguration.getPersistentData().getDirName();
     if (dirName == null || dirName.isEmpty()) {
       throw new RuntimeConfigurationError("Directory is not specified");
index 7c063b6439584a7baa2870bce1bd76601f6bc322..b3a288099cb6655d9cc1cfea8d3a496fe1fe17c7 100644 (file)
@@ -36,13 +36,13 @@ import com.intellij.execution.runners.ProgramRunner;
 import com.intellij.execution.testframework.*;
 import com.intellij.execution.ui.ConsoleViewContentType;
 import com.intellij.execution.util.JavaParametersUtil;
+import com.intellij.execution.util.ProgramParametersUtil;
 import com.intellij.openapi.Disposable;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.extensions.Extensions;
 import com.intellij.openapi.module.Module;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.projectRoots.JavaSdk;
 import com.intellij.openapi.projectRoots.JavaSdkType;
 import com.intellij.openapi.projectRoots.Sdk;
 import com.intellij.openapi.projectRoots.ex.JavaSdkUtil;
@@ -179,14 +179,8 @@ public abstract class TestObject implements JavaCommandLine {
   };
 
   public void checkConfiguration() throws RuntimeConfigurationException{
-    if (myConfiguration.isAlternativeJrePathEnabled()){
-      if (myConfiguration.getAlternativeJrePath() == null ||
-          myConfiguration.getAlternativeJrePath().length() == 0 ||
-          !JavaSdk.checkForJre(myConfiguration.getAlternativeJrePath())){
-        throw new RuntimeConfigurationWarning(
-          ExecutionBundle.message("jre.path.is.not.valid.jre.home.error.mesage", myConfiguration.getAlternativeJrePath()));
-      }
-    }
+    JavaParametersUtil.checkAlternativeJRE(myConfiguration);
+    ProgramParametersUtil.checkWorkingDirectoryExist(myConfiguration, myConfiguration.getProject(), myConfiguration.getConfigurationModule().getModule());
   }
 
   public SourceScope getSourceScope() {
index 9021ecea90b2f8fce9be0769a628713428e94716..52bc39ee1adfb0e0e39a31577a15f044723199cd 100644 (file)
@@ -29,6 +29,8 @@ import com.intellij.execution.configurations.*;
 import com.intellij.execution.junit.RefactoringListeners;
 import com.intellij.execution.runners.ExecutionEnvironment;
 import com.intellij.execution.testframework.SourceScope;
+import com.intellij.execution.util.JavaParametersUtil;
+import com.intellij.execution.util.ProgramParametersUtil;
 import com.intellij.openapi.components.PathMacroManager;
 import com.intellij.openapi.module.Module;
 import com.intellij.openapi.options.SettingsEditor;
@@ -338,6 +340,8 @@ public class TestNGConfiguration extends ModuleBasedConfiguration<JavaRunConfigu
       }
     }
     JavaRunConfigurationExtensionManager.checkConfigurationIsValid(this);
+    ProgramParametersUtil.checkWorkingDirectoryExist(this, getProject(), getConfigurationModule().getModule());
+    JavaParametersUtil.checkAlternativeJRE(this);
     //TODO add various checks here
   }