terminate all running descriptors
[idea/community.git] / platform / lang-impl / testSources / com / intellij / execution / impl / ExecutionManagerTest.java
1 /*
2  * Copyright 2000-2016 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.execution.impl;
17
18 import com.intellij.execution.ExecutionManager;
19 import com.intellij.execution.RunManagerConfig;
20 import com.intellij.execution.RunnerAndConfigurationSettings;
21 import com.intellij.execution.executors.DefaultRunExecutor;
22 import com.intellij.execution.process.ProcessHandler;
23 import com.intellij.execution.runners.ExecutionEnvironment;
24 import com.intellij.execution.runners.ExecutionEnvironmentBuilder;
25 import com.intellij.execution.ui.RunContentDescriptor;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.testFramework.LightPlatformTestCase;
28 import com.intellij.util.ui.UIUtil;
29 import org.jetbrains.annotations.NotNull;
30
31 public class ExecutionManagerTest extends LightPlatformTestCase {
32
33   private boolean myRestartRequiresConfirmation;
34
35   @Override
36   public void setUp() throws Exception {
37     super.setUp();
38     RunManagerConfig config = RunManagerImpl.getInstanceImpl(getProject()).getConfig();
39     myRestartRequiresConfirmation = config.isRestartRequiresConfirmation();
40     config.setRestartRequiresConfirmation(false);
41   }
42
43   @Override
44   public void tearDown() throws Exception {
45     try {
46       ExecutionTestUtil.terminateAllRunningDescriptors(ExecutionManager.getInstance(getProject()));
47       RunManagerConfig config = RunManagerImpl.getInstanceImpl(getProject()).getConfig();
48       config.setRestartRequiresConfirmation(myRestartRequiresConfirmation);
49     }
50     finally {
51       super.tearDown();
52     }
53   }
54
55   public void testRerunSingleton() throws Exception {
56     Project project = getProject();
57     ExecutionManagerImpl executionManager = ExecutionManagerImpl.getInstance(project);
58
59     FakeRunConfiguration rc = new FakeRunConfiguration(project, true);
60     RunnerAndConfigurationSettingsImpl settings = new RunnerAndConfigurationSettingsImpl(
61       RunManagerImpl.getInstanceImpl(project), rc, false
62     );
63     settings.setSingleton(true);
64
65     ExecutionEnvironment env1 = createEnv(project, settings);
66     executionManager.restartRunProfile(env1);
67     UIUtil.dispatchAllInvocationEvents();
68     ProcessHandler processHandler1 = getProcessHandler(executionManager);
69
70     ExecutionEnvironment env2 = createEnv(project, settings);
71     executionManager.restartRunProfile(env2);
72     UIUtil.dispatchInvocationEvent();
73
74     ProcessHandler processHandler2 = getProcessHandler(executionManager);
75     assertTrue(processHandler1 == processHandler2);
76     assertTrue(processHandler1.isProcessTerminating());
77
78     ExecutionEnvironment env3 = createEnv(project, settings);
79     executionManager.restartRunProfile(env3);
80     UIUtil.dispatchAllInvocationEvents();
81
82     FakeProcessHandler processHandler3 = getProcessHandler(executionManager);
83     assertTrue(processHandler1 != processHandler3);
84
85     assertTrue(!processHandler3.isProcessTerminating() && !processHandler3.isProcessTerminated());
86     processHandler3.killProcess();
87   }
88
89   @NotNull
90   private static FakeProcessHandler getProcessHandler(@NotNull ExecutionManagerImpl executionManager) {
91     RunContentDescriptor descriptor = ExecutionTestUtil.getSingleRunContentDescriptor(executionManager);
92     ProcessHandler processHandler = descriptor.getProcessHandler();
93     assertNotNull(processHandler);
94     return (FakeProcessHandler)processHandler;
95   }
96
97   @NotNull
98   private static ExecutionEnvironment createEnv(@NotNull Project project, @NotNull RunnerAndConfigurationSettings settings) {
99     return new ExecutionEnvironmentBuilder(project, DefaultRunExecutor.getRunExecutorInstance())
100       .runnerAndSettings(FakeProgramRunner.INSTANCE, settings)
101       .build();
102   }
103 }