Skip to content

Commit 26ac02f

Browse files
committed
Redirect console output to file in test
Reading the stream content seems to be not reliable enough and is prone to side effects. THis now redirects the console output to a file and read it from there to make the test more robust.
1 parent 92a6765 commit 26ac02f

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

org.eclipse.m2e.jdt.tests/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ Require-Bundle: org.eclipse.core.runtime,
1414
org.eclipse.jdt.launching,
1515
org.eclipse.debug.core,
1616
org.eclipse.ui.tests.harness,
17-
org.eclipse.ui
17+
org.eclipse.ui,
18+
org.eclipse.debug.ui;bundle-version="3.18.600"
1819
Bundle-RequiredExecutionEnvironment: JavaSE-21
1920
Bundle-Vendor: Eclipse.org - m2e
2021
Automatic-Module-Name: org.eclipse.m2e.jdt.tests

org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/RunTest.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import static org.junit.Assert.assertEquals;
1313
import static org.junit.Assert.assertTrue;
1414

15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
17+
1518
import org.eclipse.core.resources.IProject;
1619
import org.eclipse.core.resources.IncrementalProjectBuilder;
1720
import org.eclipse.core.runtime.FileLocator;
@@ -24,6 +27,7 @@
2427
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
2528
import org.eclipse.debug.core.ILaunchManager;
2629
import org.eclipse.debug.core.model.IProcess;
30+
import org.eclipse.debug.ui.IDebugUIConstants;
2731
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
2832
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
2933
import org.junit.Test;
@@ -32,26 +36,35 @@ public class RunTest extends AbstractMavenProjectTestCase {
3236

3337
@Test
3438
public void testRunTwice() throws Exception {
35-
IProject project = importProject(FileLocator.toFileURL(getClass().getResource("/projects/basicProjectWithDep/pom.xml")).getPath());
39+
IProject project = importProject(
40+
FileLocator.toFileURL(getClass().getResource("/projects/basicProjectWithDep/pom.xml")).getPath());
3641
waitForJobsToComplete(monitor);
3742
project.build(IncrementalProjectBuilder.FULL_BUILD, null);
3843
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
39-
ILaunchConfigurationWorkingCopy launchConfig = launchManager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION).newInstance(project, "launch");
44+
ILaunchConfigurationWorkingCopy launchConfig = launchManager
45+
.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION)
46+
.newInstance(project, "launch");
4047
launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, project.getName());
4148
launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "testMvn.TestClass");
4249
launchConfig.setAttribute(DebugPlugin.ATTR_WORKING_DIRECTORY, project.getLocation().toString());
4350
IJobFunction assertSuccessfulRun = monitor -> {
4451
try {
45-
ILaunch launch = launchConfig.launch(ILaunchManager.RUN_MODE, new NullProgressMonitor());
46-
while (!launch.isTerminated()) {
47-
Thread.sleep(300);
52+
Path file = Files.createTempFile("RunTest", ".log");
53+
try {
54+
launchConfig.setAttribute(IDebugUIConstants.ATTR_CAPTURE_IN_FILE, file.toAbsolutePath().toString());
55+
ILaunch launch = launchConfig.launch(ILaunchManager.RUN_MODE, new NullProgressMonitor());
56+
while (!launch.isTerminated()) {
57+
Thread.sleep(300);
58+
}
59+
IProcess process = launch.getProcesses()[0];
60+
String errorOutput = process.getStreamsProxy().getErrorStreamMonitor().getContents();
61+
assertEquals("", errorOutput);
62+
assertEquals(0, process.getExitValue());
63+
assertEquals("ok", Files.readString(file));
64+
return Status.OK_STATUS;
65+
} finally {
66+
Files.delete(file);
4867
}
49-
IProcess process = launch.getProcesses()[0];
50-
String errorOutput = process.getStreamsProxy().getErrorStreamMonitor().getContents();
51-
assertEquals("", errorOutput);
52-
assertEquals(0, process.getExitValue());
53-
assertEquals("ok", process.getStreamsProxy().getOutputStreamMonitor().getContents());
54-
return Status.OK_STATUS;
5568
} catch (Exception e) {
5669
return Status.error(e.getMessage(), e);
5770
}

0 commit comments

Comments
 (0)