Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ce498d5bf2bab278bb1d9e158c3e92d2170493d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*******************************************************************************
 * Copyright (c) 2011 Anton Gorenkov 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Anton Gorenkov - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.testsrunner.internal.launcher;

import java.io.InputStream;
import java.util.Map;

import org.eclipse.cdt.dsf.gdb.IGdbDebugConstants;
import org.eclipse.cdt.dsf.gdb.launching.GDBProcess;
import org.eclipse.cdt.dsf.gdb.launching.InferiorRuntimeProcess;
import org.eclipse.cdt.testsrunner.internal.TestsRunnerPlugin;
import org.eclipse.cdt.testsrunner.internal.model.TestingSession;
import org.eclipse.cdt.testsrunner.launcher.ITestsRunnerProviderInfo;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.IProcessFactory;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.core.model.RuntimeProcess;

/**
 * Custom testing process factory allows to handle the output stream of the
 * testing process and prevent it from output to Console.
 */
public class TestingProcessFactory implements IProcessFactory {

	/**
	 * Runs data processing for the testing process and close IO stream when it
	 * is done.
	 */
	private class TestingSessionRunner implements Runnable {

		private TestingSession testingSession;
		private InputStream iStream;
		private ProcessWrapper processWrapper;
		
		TestingSessionRunner(TestingSession testingSession, InputStream iStream, ProcessWrapper processWrapper) {
			this.testingSession = testingSession;
			this.iStream = iStream;
			this.processWrapper = processWrapper;
		}
		
		@Override
		public void run() {
			try {
				testingSession.run(iStream);
			}
			finally {
				// Streams should be closed anyway to avoid testing process hang up
				processWrapper.allowStreamsClosing();
			}
		}
	}
	
	/**
	 * Creates a wrapper for the specified process to handle its input or error
	 * stream.
	 * 
	 * @param launch launch
	 * @param process process to wrap
	 * @return wrapped process
	 * @throws CoreException
	 */
	private Process wrapProcess(ILaunch launch, Process process) throws CoreException {
		TestingSession testingSession = TestsRunnerPlugin.getDefault().getTestingSessionsManager().newSession(launch);
		ITestsRunnerProviderInfo testsRunnerProvider = testingSession.getTestsRunnerProviderInfo();
		InputStream iStream = 
				testsRunnerProvider.isOutputStreamRequired() ? process.getInputStream() :
				testsRunnerProvider.isErrorStreamRequired() ? process.getErrorStream() : null;
		ProcessWrapper processWrapper = new ProcessWrapper(process, testsRunnerProvider.isOutputStreamRequired(), testsRunnerProvider.isErrorStreamRequired());
		Thread t = new Thread(new TestingSessionRunner(testingSession, iStream, processWrapper));
		t.start();
		return processWrapper;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public IProcess newProcess(ILaunch launch, Process process, String label, Map attributes) {
		
		try {
			// Mimic the behavior of DSF GDBProcessFactory.
			if (attributes != null) {
				Object processTypeCreationAttrValue = attributes.get(IGdbDebugConstants.PROCESS_TYPE_CREATION_ATTR);
				if (IGdbDebugConstants.GDB_PROCESS_CREATION_VALUE.equals(processTypeCreationAttrValue)) {
					return new GDBProcess(launch, process, label, attributes);
				}
	
				if (IGdbDebugConstants.INFERIOR_PROCESS_CREATION_VALUE.equals(processTypeCreationAttrValue)) {
					return new InferiorRuntimeProcess(launch, wrapProcess(launch, process), label, attributes);
				}

			// Probably, it is CDI creating a new inferior process
			} else {
				return new RuntimeProcess(launch, wrapProcess(launch, process), label, attributes);
			}

		} catch (CoreException e) {
			TestsRunnerPlugin.log(e);
		}

		return null;
	}

}

Back to the top