Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ec2f37be2e187d08ab96413749cc649b519bc297 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*******************************************************************************
 * Copyright (c) 2011, 2012 Anton Gorenkov
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Anton Gorenkov - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.testsrunner.internal.model;

import java.io.InputStream;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.Date;
import java.util.EnumMap;
import java.util.Map;

import org.eclipse.cdt.testsrunner.internal.TestsRunnerPlugin;
import org.eclipse.cdt.testsrunner.internal.launcher.TestsRunnerProviderInfo;
import org.eclipse.cdt.testsrunner.launcher.ITestsRunnerProvider;
import org.eclipse.cdt.testsrunner.model.IModelVisitor;
import org.eclipse.cdt.testsrunner.model.ITestCase;
import org.eclipse.cdt.testsrunner.model.ITestItem;
import org.eclipse.cdt.testsrunner.model.ITestItem.Status;
import org.eclipse.cdt.testsrunner.model.ITestMessage;
import org.eclipse.cdt.testsrunner.model.ITestModelAccessor;
import org.eclipse.cdt.testsrunner.model.ITestSuite;
import org.eclipse.cdt.testsrunner.model.ITestingSession;
import org.eclipse.cdt.testsrunner.model.ITestingSessionListener;
import org.eclipse.cdt.testsrunner.model.TestingException;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.ILaunch;

/**
 * Stores the information about tests running.
 */
public class TestingSession implements ITestingSession {

	/** Launch object the is connected to the tests running. */
	private ILaunch launch;

	/** Information about used Tests Runner provider plug-in. */
	private TestsRunnerProviderInfo testsRunnerProviderInfo;

	/** Main interface to Tests Runner provider plug-in. */
	private ITestsRunnerProvider testsRunnerProvider;

	/**
	 * Test Model manager that is used to fill and update the model for the
	 * session.
	 */
	private TestModelManager modelManager;

	/**
	 * Total tests counter. It is -1 by default, that means that total tests
	 * count is not available.
	 *
	 * @see getTotalCounter()
	 */
	private int totalCounter = -1;

	/** Already finished tests counter. */
	private int currentCounter = 0;

	/**
	 * Test counters map by test status. They are used to quickly provide simple
	 * statistics without model scanning.
	 *
	 */
	private Map<ITestItem.Status, Integer> statusCounters = new EnumMap<ITestItem.Status, Integer>(
			ITestItem.Status.class);

	/**
	 * The flag stores whether the testing session contains errors at the
	 * moment.
	 *
	 * @see hasErrors()
	 */
	private boolean hasErrors = false;

	/**
	 * The flag stores whether the testing session was stopped by user.
	 *
	 * @see wasStopped()
	 */
	private boolean wasStopped = false;

	/**
	 * The flag stores whether the testing session has been finished (with or
	 * without errors).
	 */
	private boolean finished = false;

	/** Stores current status of the testing session. */
	private String statusMessage = ModelMessages.TestingSession_starting_status;

	/** Stores the time when the testing session was created. */
	private long startTime;

	/**
	 * Counts the number of the test cases in tests hierarchy.
	 */
	private class TestCasesCounter implements IModelVisitor {

		public int result = 0;

		@Override
		public void visit(ITestCase testCase) {
			++result;
		}

		@Override
		public void visit(ITestSuite testSuite) {
		}

		@Override
		public void visit(ITestMessage testMessage) {
		}

		@Override
		public void leave(ITestSuite testSuite) {
		}

		@Override
		public void leave(ITestCase testCase) {
		}

		@Override
		public void leave(ITestMessage testMessage) {
		}
	}

	/**
	 * The constructor.
	 *
	 * @param launch connected launch object
	 * @param testsRunnerProviderInfo the information about the tests runner
	 * @param previousSession is used to determine total tests count & for tests
	 * hierarchy reusing if it is considered as similar
	 */
	public TestingSession(ILaunch launch, TestsRunnerProviderInfo testsRunnerProviderInfo,
			TestingSession previousSession) {
		this.launch = launch;
		this.testsRunnerProviderInfo = testsRunnerProviderInfo;
		this.testsRunnerProvider = testsRunnerProviderInfo.instantiateTestsRunnerProvider();
		this.startTime = System.currentTimeMillis();
		// Calculate approximate tests count by the previous similar testing session (if available)
		if (previousSession != null) {
			TestCasesCounter testCasesCounter = new TestCasesCounter();
			previousSession.getModelAccessor().getRootSuite().visit(testCasesCounter);
			totalCounter = testCasesCounter.result;
		}
		ITestSuite rootTestSuite = previousSession != null ? previousSession.getModelAccessor().getRootSuite() : null;
		this.modelManager = new TestModelManager(rootTestSuite,
				testsRunnerProviderInfo.isAllowedTestingTimeMeasurement());
		this.modelManager.addChangesListener(new ITestingSessionListener() {

			@Override
			public void testingStarted() {
			}

			@Override
			public void testingFinished() {
				// This is necessary if totalCounter was -1 (tests count was unknown)
				// or if tests count was estimated not accurately
				totalCounter = currentCounter;
			}

			@Override
			public void exitTestSuite(ITestSuite testSuite) {
			}

			@Override
			public void exitTestCase(ITestCase testCase) {
				// Update testing session info (counters, flags)
				Status testStatus = testCase.getStatus();
				statusCounters.put(testStatus, getCount(testStatus) + 1);
				++currentCounter;
				if (testStatus.isError())
					hasErrors = true;
			}

			@Override
			public void enterTestSuite(ITestSuite testSuite) {
			}

			@Override
			public void enterTestCase(ITestCase testCase) {
			}

			@Override
			public void childrenUpdate(ITestSuite parent) {
			}
		});
	}

	/**
	 * Starts the processing of the test module output.
	 *
	 * @param inputStream test module output stream
	 */
	public void run(InputStream inputStream) {
		modelManager.testingStarted();
		try {
			testsRunnerProvider.run(modelManager, inputStream);
			// If testing session was stopped, the status is set in stop()
			if (!wasStopped()) {
				double testingTime = getModelAccessor().getRootSuite().getTestingTime();
				statusMessage = MessageFormat.format(ModelMessages.TestingSession_finished_status,
						testingTime / 1000.0);
			}
		} catch (TestingException e) {
			// If testing session was stopped, the status is set in stop()
			if (!wasStopped()) {
				statusMessage = e.getLocalizedMessage();
				hasErrors = true;
			}
		}
		finished = true;
		modelManager.testingFinished();
	}

	@Override
	public int getCurrentCounter() {
		return currentCounter;
	}

	@Override
	public int getTotalCounter() {
		return totalCounter;
	}

	@Override
	public int getCount(ITestItem.Status status) {
		Integer counterValue = statusCounters.get(status);
		return (counterValue == null) ? 0 : counterValue;
	}

	@Override
	public boolean hasErrors() {
		return hasErrors;
	}

	@Override
	public boolean wasStopped() {
		return wasStopped;
	}

	@Override
	public boolean isFinished() {
		return finished;
	}

	@Override
	public ITestModelAccessor getModelAccessor() {
		return modelManager;
	}

	@Override
	public ILaunch getLaunch() {
		return launch;
	}

	@Override
	public TestsRunnerProviderInfo getTestsRunnerProviderInfo() {
		return testsRunnerProviderInfo;
	}

	@Override
	public String getStatusMessage() {
		return statusMessage;
	}

	@Override
	public String getName() {
		String launchConfName = launch.getLaunchConfiguration().getName();
		String startTimeStr = DateFormat.getDateTimeInstance().format(new Date(startTime));
		return MessageFormat.format(ModelMessages.TestingSession_name_format, launchConfName, startTimeStr);
	}

	@Override
	public void stop() {
		if (!launch.isTerminated() && launch.canTerminate()) {
			try {
				launch.terminate();
				wasStopped = true;
				statusMessage = ModelMessages.TestingSession_stopped_status;
			} catch (DebugException e) {
				TestsRunnerPlugin.log(e);
			}
		}
	}

}

Back to the top