log details to fight blinking test
[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.openapi.util.text.StringUtil;
28 import com.intellij.testFramework.LightPlatformTestCase;
29 import com.intellij.util.Function;
30 import com.intellij.util.containers.ContainerUtil;
31 import com.intellij.util.ui.UIUtil;
32 import org.jetbrains.annotations.NotNull;
33
34 import java.util.List;
35
36 public class ExecutionManagerTest extends LightPlatformTestCase {
37
38   private boolean myRestartRequiresConfirmation;
39
40   @Override
41   public void setUp() throws Exception {
42     super.setUp();
43     RunManagerConfig config = RunManagerImpl.getInstanceImpl(getProject()).getConfig();
44     myRestartRequiresConfirmation = config.isRestartRequiresConfirmation();
45     config.setRestartRequiresConfirmation(false);
46   }
47
48   @Override
49   public void tearDown() throws Exception {
50     try {
51       RunManagerConfig config = RunManagerImpl.getInstanceImpl(getProject()).getConfig();
52       config.setRestartRequiresConfirmation(myRestartRequiresConfirmation);
53     }
54     finally {
55       super.tearDown();
56     }
57   }
58
59   public void testRerunSingleton() throws Exception {
60     Project project = getProject();
61     ExecutionManagerImpl executionManager = ExecutionManagerImpl.getInstance(project);
62
63     FakeRunConfiguration rc = new FakeRunConfiguration(project, true);
64     RunnerAndConfigurationSettingsImpl settings = new RunnerAndConfigurationSettingsImpl(
65       RunManagerImpl.getInstanceImpl(project), rc, false
66     );
67     settings.setSingleton(true);
68
69     ExecutionEnvironment env1 = createEnv(project, settings);
70     executionManager.restartRunProfile(env1);
71     UIUtil.dispatchAllInvocationEvents();
72     ProcessHandler processHandler1 = getProcessHandler(executionManager);
73
74     ExecutionEnvironment env2 = createEnv(project, settings);
75     executionManager.restartRunProfile(env2);
76     UIUtil.dispatchInvocationEvent();
77
78     ProcessHandler processHandler2 = getProcessHandler(executionManager);
79     assertTrue(processHandler1 == processHandler2);
80     assertTrue(processHandler1.isProcessTerminating());
81
82     ExecutionEnvironment env3 = createEnv(project, settings);
83     executionManager.restartRunProfile(env3);
84     UIUtil.dispatchAllInvocationEvents();
85
86     FakeProcessHandler processHandler3 = getProcessHandler(executionManager);
87     assertTrue(processHandler1 != processHandler3);
88
89     assertTrue(!processHandler3.isProcessTerminating() && !processHandler3.isProcessTerminated());
90     processHandler3.killProcess();
91   }
92
93   @NotNull
94   private static FakeProcessHandler getProcessHandler(@NotNull ExecutionManagerImpl executionManager) {
95     List<RunContentDescriptor> descriptors = executionManager.getRunningDescriptors(Conditions.alwaysTrue());
96     String actualDescriptorsMsg = stringifyDescriptors(descriptors);
97     assertEquals(actualDescriptorsMsg, 1, descriptors.size());
98     RunContentDescriptor descriptor = ContainerUtil.getFirstItem(descriptors);
99     assertNotNull(actualDescriptorsMsg, descriptor);
100     ProcessHandler processHandler = descriptor.getProcessHandler();
101     assertNotNull(actualDescriptorsMsg, processHandler);
102     return (FakeProcessHandler)processHandler;
103   }
104
105   @NotNull
106   private static String stringifyDescriptors(@NotNull List<RunContentDescriptor> descriptors) {
107     return "Actual descriptors: " + StringUtil.join(descriptors, new Function<RunContentDescriptor, String>() {
108       @Override
109       public String fun(RunContentDescriptor descriptor) {
110         if (descriptor == null) {
111           return "null";
112         }
113         ProcessHandler processHandler = descriptor.getProcessHandler();
114         return String.format("[%s, %s]",
115                              descriptor.getDisplayName(),
116                              processHandler != null ? processHandler.getClass().getName() : null);
117       }
118     }, ", ");
119   }
120
121   @NotNull
122   private static ExecutionEnvironment createEnv(@NotNull Project project, @NotNull RunnerAndConfigurationSettings settings) {
123     return new ExecutionEnvironmentBuilder(project, DefaultRunExecutor.getRunExecutorInstance())
124       .runnerAndSettings(FakeProgramRunner.INSTANCE, settings)
125       .build();
126   }
127 }