terminate all running descriptors
authorSergey Simonchik <sergey.simonchik@jetbrains.com>
Sun, 8 May 2016 19:17:36 +0000 (22:17 +0300)
committerSergey Simonchik <sergey.simonchik@jetbrains.com>
Sun, 8 May 2016 19:17:36 +0000 (22:17 +0300)
platform/lang-impl/testSources/com/intellij/execution/impl/ExecutionManagerTest.java
platform/lang-impl/testSources/com/intellij/execution/impl/ExecutionTestUtil.java [new file with mode: 0644]
platform/lang-impl/testSources/com/intellij/execution/impl/ModuleRunConfigurationManagerTest.java

index b6032ee4363148041912bd940e26a274b068c593..eaf0592753b0f89aae3eab64d9bf0b1bc9edfbe0 100644 (file)
@@ -15,6 +15,7 @@
  */
 package com.intellij.execution.impl;
 
+import com.intellij.execution.ExecutionManager;
 import com.intellij.execution.RunManagerConfig;
 import com.intellij.execution.RunnerAndConfigurationSettings;
 import com.intellij.execution.executors.DefaultRunExecutor;
@@ -23,16 +24,10 @@ import com.intellij.execution.runners.ExecutionEnvironment;
 import com.intellij.execution.runners.ExecutionEnvironmentBuilder;
 import com.intellij.execution.ui.RunContentDescriptor;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.Conditions;
-import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.testFramework.LightPlatformTestCase;
-import com.intellij.util.Function;
-import com.intellij.util.containers.ContainerUtil;
 import com.intellij.util.ui.UIUtil;
 import org.jetbrains.annotations.NotNull;
 
-import java.util.List;
-
 public class ExecutionManagerTest extends LightPlatformTestCase {
 
   private boolean myRestartRequiresConfirmation;
@@ -48,6 +43,7 @@ public class ExecutionManagerTest extends LightPlatformTestCase {
   @Override
   public void tearDown() throws Exception {
     try {
+      ExecutionTestUtil.terminateAllRunningDescriptors(ExecutionManager.getInstance(getProject()));
       RunManagerConfig config = RunManagerImpl.getInstanceImpl(getProject()).getConfig();
       config.setRestartRequiresConfirmation(myRestartRequiresConfirmation);
     }
@@ -92,33 +88,13 @@ public class ExecutionManagerTest extends LightPlatformTestCase {
 
   @NotNull
   private static FakeProcessHandler getProcessHandler(@NotNull ExecutionManagerImpl executionManager) {
-    List<RunContentDescriptor> descriptors = executionManager.getRunningDescriptors(Conditions.alwaysTrue());
-    String actualDescriptorsMsg = stringifyDescriptors(descriptors);
-    assertEquals(actualDescriptorsMsg, 1, descriptors.size());
-    RunContentDescriptor descriptor = ContainerUtil.getFirstItem(descriptors);
-    assertNotNull(actualDescriptorsMsg, descriptor);
+    RunContentDescriptor descriptor = ExecutionTestUtil.getSingleRunContentDescriptor(executionManager);
     ProcessHandler processHandler = descriptor.getProcessHandler();
-    assertNotNull(actualDescriptorsMsg, processHandler);
+    assertNotNull(processHandler);
     return (FakeProcessHandler)processHandler;
   }
 
   @NotNull
-  private static String stringifyDescriptors(@NotNull List<RunContentDescriptor> descriptors) {
-    return "Actual descriptors: " + StringUtil.join(descriptors, new Function<RunContentDescriptor, String>() {
-      @Override
-      public String fun(RunContentDescriptor descriptor) {
-        if (descriptor == null) {
-          return "null";
-        }
-        ProcessHandler processHandler = descriptor.getProcessHandler();
-        return String.format("[%s, %s]",
-                             descriptor.getDisplayName(),
-                             processHandler != null ? processHandler.getClass().getName() : null);
-      }
-    }, ", ");
-  }
-
-  @NotNull
   private static ExecutionEnvironment createEnv(@NotNull Project project, @NotNull RunnerAndConfigurationSettings settings) {
     return new ExecutionEnvironmentBuilder(project, DefaultRunExecutor.getRunExecutorInstance())
       .runnerAndSettings(FakeProgramRunner.INSTANCE, settings)
diff --git a/platform/lang-impl/testSources/com/intellij/execution/impl/ExecutionTestUtil.java b/platform/lang-impl/testSources/com/intellij/execution/impl/ExecutionTestUtil.java
new file mode 100644 (file)
index 0000000..4c4e877
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.execution.impl;
+
+import com.intellij.execution.ExecutionManager;
+import com.intellij.execution.process.ProcessHandler;
+import com.intellij.execution.process.ScriptRunnerUtil;
+import com.intellij.execution.ui.RunContentDescriptor;
+import com.intellij.openapi.util.Conditions;
+import com.intellij.openapi.util.text.StringUtil;
+import com.intellij.util.Function;
+import com.intellij.util.ObjectUtils;
+import com.intellij.util.containers.ContainerUtil;
+import org.jetbrains.annotations.NotNull;
+import org.junit.Assert;
+
+import java.util.List;
+
+public class ExecutionTestUtil {
+  private ExecutionTestUtil() {
+  }
+
+  @NotNull
+  public static RunContentDescriptor getSingleRunContentDescriptor(@NotNull ExecutionManager executionManager) {
+    List<RunContentDescriptor> descriptors = ((ExecutionManagerImpl)executionManager).getRunningDescriptors(Conditions.alwaysTrue());
+    String actualDescriptorsMsg = stringifyDescriptors(descriptors);
+    Assert.assertEquals(actualDescriptorsMsg, 1, descriptors.size());
+    RunContentDescriptor descriptor = ContainerUtil.getFirstItem(descriptors);
+    return ObjectUtils.notNull(descriptor);
+  }
+
+  public static void terminateAllRunningDescriptors(@NotNull ExecutionManager executionManager) {
+    List<RunContentDescriptor> descriptors = ((ExecutionManagerImpl)executionManager).getRunningDescriptors(Conditions.alwaysTrue());
+    for (RunContentDescriptor descriptor : descriptors) {
+      ProcessHandler processHandler = descriptor.getProcessHandler();
+      if (processHandler != null) {
+        ScriptRunnerUtil.terminateProcessHandler(processHandler, 0, null);
+      }
+    }
+  }
+
+  @NotNull
+  private static String stringifyDescriptors(@NotNull List<RunContentDescriptor> descriptors) {
+    return "Actual descriptors: " + StringUtil.join(descriptors, new Function<RunContentDescriptor, String>() {
+      @Override
+      public String fun(RunContentDescriptor descriptor) {
+        if (descriptor == null) {
+          return "null";
+        }
+        ProcessHandler processHandler = descriptor.getProcessHandler();
+        return String.format("[%s, %s]",
+                             descriptor.getDisplayName(),
+                             processHandler != null ? processHandler.getClass().getName() : null);
+      }
+    }, ", ");
+  }
+}
index b8105d9fea41b29dc17d2aabf1413688b01c59d3..4981a74f444a9916338d19b864ff9b76d60f4c08 100644 (file)
@@ -35,6 +35,7 @@ import com.intellij.testFramework.LightPlatformTestCase;
 import com.intellij.util.containers.ContainerUtil;
 import com.intellij.util.messages.MessageBus;
 import com.intellij.util.messages.MessageBusConnection;
+import com.intellij.util.ui.UIUtil;
 import org.jdom.Element;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -86,8 +87,13 @@ public class ModuleRunConfigurationManagerTest extends LightPlatformTestCase {
 
   @Override
   public void tearDown() throws Exception {
-    myManager = null;
-    super.tearDown();
+    try {
+      ExecutionTestUtil.terminateAllRunningDescriptors(ExecutionManager.getInstance(getProject()));
+      myManager = null;
+    }
+    finally {
+      super.tearDown();
+    }
   }
 
   public void testInitComponentSubscribesForModulesTopic() throws Exception {
@@ -134,9 +140,10 @@ public class ModuleRunConfigurationManagerTest extends LightPlatformTestCase {
     descriptorToReuse.setActivateToolWindowWhenAdded(false);
     descriptorToReuse.setReuseToolWindowActivation(true);
     env.setContentToReuse(descriptorToReuse);
-    env.getRunner().execute(env);
-    RunContentDescriptor lastDescriptor = programRunner.getLastDescriptor();
-    assertNotNull(lastDescriptor);
+    ExecutionManager executionManager = ExecutionManager.getInstance(getProject());
+    executionManager.restartRunProfile(env);
+    UIUtil.dispatchAllInvocationEvents();
+    RunContentDescriptor lastDescriptor = ExecutionTestUtil.getSingleRunContentDescriptor(executionManager);
     assertFalse(lastDescriptor.isActivateToolWindowWhenAdded());
     Disposer.dispose(descriptorToReuse);
   }