7557f77a13ed1e5f9f21dfa3d9290dfa0ce20cee
[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.RunManagerConfig;
19 import com.intellij.execution.RunnerAndConfigurationSettings;
20 import com.intellij.execution.executors.DefaultRunExecutor;
21 import com.intellij.execution.process.ProcessHandler;
22 import com.intellij.execution.runners.ExecutionEnvironment;
23 import com.intellij.execution.runners.ExecutionEnvironmentBuilder;
24 import com.intellij.execution.ui.RunContentDescriptor;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.util.Conditions;
27 import com.intellij.testFramework.LightPlatformTestCase;
28 import com.intellij.util.containers.ContainerUtil;
29 import com.intellij.util.ui.UIUtil;
30 import org.jetbrains.annotations.NotNull;
31
32 import java.util.List;
33
34 public class ExecutionManagerTest extends LightPlatformTestCase {
35
36   private boolean myRestartRequiresConfirmation;
37
38   @Override
39   public void setUp() throws Exception {
40     super.setUp();
41     RunManagerConfig config = RunManagerImpl.getInstanceImpl(getProject()).getConfig();
42     myRestartRequiresConfirmation = config.isRestartRequiresConfirmation();
43     config.setRestartRequiresConfirmation(false);
44   }
45
46   @Override
47   public void tearDown() throws Exception {
48     try {
49       RunManagerConfig config = RunManagerImpl.getInstanceImpl(getProject()).getConfig();
50       config.setRestartRequiresConfirmation(myRestartRequiresConfirmation);
51     }
52     finally {
53       super.tearDown();
54     }
55   }
56
57   public void testRerunSingleton() throws Exception {
58     Project project = getProject();
59     ExecutionManagerImpl executionManager = ExecutionManagerImpl.getInstance(project);
60
61     FakeRunConfiguration rc = new FakeRunConfiguration(project, true);
62     RunnerAndConfigurationSettingsImpl settings = new RunnerAndConfigurationSettingsImpl(
63       RunManagerImpl.getInstanceImpl(project), rc, false
64     );
65     settings.setSingleton(true);
66
67     ExecutionEnvironment env1 = createEnv(project, settings);
68     executionManager.restartRunProfile(env1);
69     UIUtil.dispatchAllInvocationEvents();
70     ProcessHandler processHandler1 = getProcessHandler(executionManager);
71
72     ExecutionEnvironment env2 = createEnv(project, settings);
73     executionManager.restartRunProfile(env2);
74     UIUtil.dispatchInvocationEvent();
75
76     ProcessHandler processHandler2 = getProcessHandler(executionManager);
77     assertTrue(processHandler1 == processHandler2);
78     assertTrue(processHandler1.isProcessTerminating());
79
80     ExecutionEnvironment env3 = createEnv(project, settings);
81     executionManager.restartRunProfile(env3);
82     UIUtil.dispatchAllInvocationEvents();
83
84     FakeProcessHandler processHandler3 = getProcessHandler(executionManager);
85     assertTrue(processHandler1 != processHandler3);
86
87     assertTrue(!processHandler3.isProcessTerminating() && !processHandler3.isProcessTerminated());
88     processHandler3.killProcess();
89   }
90
91   @NotNull
92   private static FakeProcessHandler getProcessHandler(@NotNull ExecutionManagerImpl executionManager) {
93     List<RunContentDescriptor> descriptors = executionManager.getRunningDescriptors(Conditions.alwaysTrue());
94     assertEquals(1, descriptors.size());
95     RunContentDescriptor descriptor = ContainerUtil.getFirstItem(descriptors);
96     assertNotNull(descriptor);
97     ProcessHandler processHandler = descriptor.getProcessHandler();
98     assertNotNull(processHandler);
99     return (FakeProcessHandler)processHandler;
100   }
101
102   @NotNull
103   private static ExecutionEnvironment createEnv(@NotNull Project project, @NotNull RunnerAndConfigurationSettings settings) {
104     return new ExecutionEnvironmentBuilder(project, DefaultRunExecutor.getRunExecutorInstance())
105       .runnerAndSettings(FakeProgramRunner.INSTANCE, settings)
106       .build();
107   }
108 }