35001d5e87e2ca2b5935860a6fddd8c365148f0a
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / analysis / AnalysisModuleTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.tests.analysis;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.assertNull;
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
21
22 import java.util.HashSet;
23 import java.util.Set;
24 import java.util.concurrent.TimeUnit;
25
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.eclipse.osgi.util.NLS;
31 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
32 import org.eclipse.tracecompass.tmf.core.analysis.Messages;
33 import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
34 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
35 import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestHelper;
36 import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace;
37 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
38 import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestAnalysis;
39 import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestAnalysis2;
40 import org.junit.After;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.rules.TestRule;
44 import org.junit.rules.Timeout;
45
46 import com.google.common.collect.ImmutableSet;
47
48 /**
49 * Test suite for the {@link TmfAbstractAnalysisModule} class
50 */
51 public class AnalysisModuleTest {
52
53 /** Test timeout */
54 @Rule
55 public TestRule timeoutRule = new Timeout(30, TimeUnit.SECONDS);
56
57 private static final @NonNull String MODULE_GENERIC_ID = "test.id";
58 private static final @NonNull String MODULE_GENERIC_NAME = "Test analysis";
59
60 /**
61 * Some tests use traces, let's clean them here
62 */
63 @After
64 public void cleanupTraces() {
65 TmfTestTrace.A_TEST_10K.dispose();
66 }
67
68 /**
69 * Test suite for analysis module getters and setters
70 */
71 @Test
72 public void testGettersSetters() {
73 IAnalysisModule module = new TestAnalysis();
74
75 module.setName(MODULE_GENERIC_NAME);
76 module.setId(MODULE_GENERIC_ID);
77 assertEquals(MODULE_GENERIC_ID, module.getId());
78 assertEquals(MODULE_GENERIC_NAME, module.getName());
79
80 module.setAutomatic(false);
81 assertFalse(module.isAutomatic());
82 module.setAutomatic(true);
83 assertTrue(module.isAutomatic());
84 module.addParameter(TestAnalysis.PARAM_TEST);
85 assertNull(module.getParameter(TestAnalysis.PARAM_TEST));
86 module.setParameter(TestAnalysis.PARAM_TEST, 1);
87 assertEquals(1, module.getParameter(TestAnalysis.PARAM_TEST));
88
89 assertEquals(0, module.getDependencyLevel());
90
91 /* Try to set and get wrong parameter */
92 String wrongParam = "abc";
93 Exception exception = null;
94 try {
95 module.setParameter(wrongParam, 1);
96 } catch (RuntimeException e) {
97 exception = e;
98 assertEquals(NLS.bind(Messages.TmfAbstractAnalysisModule_InvalidParameter, wrongParam, module.getName()), e.getMessage());
99 }
100 assertNotNull(exception);
101 assertNull(module.getParameter(wrongParam));
102
103 module.dispose();
104 }
105
106 private static TestAnalysis setUpAnalysis() {
107 TestAnalysis module = new TestAnalysis();
108
109 module.setName(MODULE_GENERIC_NAME);
110 module.setId(MODULE_GENERIC_ID);
111 module.addParameter(TestAnalysis.PARAM_TEST);
112
113 return module;
114 }
115
116 /**
117 * Test suite for analysis module
118 * {@link TmfAbstractAnalysisModule#waitForCompletion} with successful
119 * execution
120 */
121 @Test
122 public void testWaitForCompletionSuccess() {
123 TestAnalysis module = setUpAnalysis();
124
125 IStatus status = module.schedule();
126 assertEquals(IStatus.ERROR, status.getSeverity());
127
128 /* Set a stub trace for analysis */
129 try {
130 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
131 } catch (TmfAnalysisException e) {
132 fail(e.getMessage());
133 }
134
135 /* Default execution, with output 1 */
136 module.setParameter(TestAnalysis.PARAM_TEST, 1);
137 status = module.schedule();
138 assertEquals(Status.OK_STATUS, status);
139 boolean completed = module.waitForCompletion();
140
141 assertTrue(completed);
142 assertEquals(1, module.getAnalysisOutput());
143
144 module.dispose();
145 }
146
147 /**
148 * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion} with
149 * cancellation
150 */
151 @Test
152 public void testWaitForCompletionCancelled() {
153 TestAnalysis module = setUpAnalysis();
154
155 /* Set a stub trace for analysis */
156 ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace();
157 try {
158 assertTrue(module.setTrace(trace));
159 } catch (TmfAnalysisException e) {
160 fail(e.getMessage());
161 }
162
163 module.setParameter(TestAnalysis.PARAM_TEST, 0);
164 IStatus status = module.schedule();
165 assertEquals(Status.OK_STATUS, status);
166 boolean completed = module.waitForCompletion();
167
168 assertFalse(completed);
169 assertEquals(0, module.getAnalysisOutput());
170
171 module.dispose();
172 }
173
174 /**
175 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method
176 * with wrong trace
177 */
178 @Test
179 public void testSetWrongTrace() {
180 IAnalysisModule module = new TestAnalysis2();
181
182 module.setName(MODULE_GENERIC_NAME);
183 module.setId(MODULE_GENERIC_ID);
184 assertEquals(MODULE_GENERIC_ID, module.getId());
185 assertEquals(MODULE_GENERIC_NAME, module.getName());
186
187 try {
188 assertFalse(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
189 } catch (TmfAnalysisException e) {
190 fail();
191 }
192
193 module.dispose();
194 }
195
196 /**
197 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method
198 * with wrong trace
199 */
200 @Test
201 public void testSetTraceTwice() {
202 IAnalysisModule module = new TestAnalysis();
203
204 module.setName(MODULE_GENERIC_NAME);
205 module.setId(MODULE_GENERIC_ID);
206
207 try {
208 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
209 } catch (TmfAnalysisException e) {
210 fail();
211 }
212 TmfAnalysisException exception = null;
213 try {
214 module.setTrace(TmfTestTrace.A_TEST_10K.getTrace());
215 } catch (TmfAnalysisException e) {
216 exception = e;
217 }
218 assertNotNull(exception);
219
220 module.dispose();
221 }
222
223 /**
224 * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method
225 */
226 @Test
227 public void testCancel() {
228 TestAnalysis module = setUpAnalysis();
229
230 module.setParameter(TestAnalysis.PARAM_TEST, 999);
231 try {
232 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
233 } catch (TmfAnalysisException e) {
234 fail(e.getMessage());
235 }
236
237 assertEquals(Status.OK_STATUS, module.schedule());
238
239 /* Give the job a chance to start */
240 try {
241 Thread.sleep(1000);
242 } catch (InterruptedException e) {
243 fail(e.getMessage());
244 }
245
246 module.cancel();
247 assertFalse(module.waitForCompletion());
248 assertEquals(-1, module.getAnalysisOutput());
249
250 module.dispose();
251 }
252
253 /**
254 * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)}
255 * method
256 */
257 @Test
258 public void testParameterChanged() {
259 TestAnalysis module = setUpAnalysis();
260
261 try {
262 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
263 } catch (TmfAnalysisException e) {
264 fail(e.getMessage());
265 }
266
267 /* Check exception if no wrong parameter name */
268 Exception exception = null;
269 try {
270 module.notifyParameterChanged("aaa");
271 } catch (RuntimeException e) {
272 exception = e;
273 }
274 assertNotNull(exception);
275
276 /*
277 * Cannot test anymore of this method, need a parameter provider to do
278 * this
279 */
280 module.dispose();
281 }
282
283 /**
284 * Test the {@link TmfTestHelper#executeAnalysis(IAnalysisModule)} method
285 */
286 @Test
287 public void testHelper() {
288 TestAnalysis module = setUpAnalysis();
289
290 try {
291 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
292 } catch (TmfAnalysisException e) {
293 fail(e.getMessage());
294 }
295
296 module.setParameter(TestAnalysis.PARAM_TEST, 1);
297 boolean res = TmfTestHelper.executeAnalysis(module);
298 assertTrue(res);
299
300 module.setParameter(TestAnalysis.PARAM_TEST, 0);
301 res = TmfTestHelper.executeAnalysis(module);
302 assertFalse(res);
303
304 module.dispose();
305 }
306
307 /**
308 * Test the {@link TmfAbstractAnalysisModule} also executes the dependent
309 * analyses
310 */
311 @Test
312 public void testDependentAnalyses() {
313
314 ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace();
315 int paramAndResult = 5;
316
317 /* Setup the dependent module */
318 final String suffix = " dep";
319 final TestAnalysis depModule = new TestAnalysis() {
320
321 @Override
322 protected boolean executeAnalysis(IProgressMonitor monitor) {
323 try {
324 Thread.sleep(1000);
325 } catch (InterruptedException e) {
326 return false;
327 }
328 return super.executeAnalysis(monitor);
329 }
330
331 };
332 depModule.setName(MODULE_GENERIC_NAME + suffix);
333 depModule.setId(MODULE_GENERIC_ID + suffix);
334 depModule.addParameter(TestAnalysis.PARAM_TEST);
335 depModule.setParameter(TestAnalysis.PARAM_TEST, paramAndResult);
336
337 /* Prepare the main analysis with a dependent analysis */
338 TestAnalysis module = new TestAnalysis() {
339
340 @Override
341 protected Iterable<IAnalysisModule> getDependentAnalyses() {
342 Set<IAnalysisModule> modules = new HashSet<>();
343 modules.add(depModule);
344 return modules;
345 }
346
347 };
348
349 module.setName(MODULE_GENERIC_NAME);
350 module.setId(MODULE_GENERIC_ID);
351 module.addParameter(TestAnalysis.PARAM_TEST);
352 module.setParameter(TestAnalysis.PARAM_TEST, paramAndResult);
353
354 try {
355 assertTrue(depModule.setTrace(trace));
356 assertTrue(module.setTrace(trace));
357 } catch (TmfAnalysisException e) {
358 fail(e.getMessage());
359 }
360
361 /* Verify none of the module has run */
362 assertEquals(0, module.getAnalysisOutput());
363 assertEquals(0, depModule.getAnalysisOutput());
364
365 module.schedule();
366 assertTrue(module.waitForCompletion());
367 assertEquals(paramAndResult, module.getAnalysisOutput());
368
369 /* Make sure the dependent analysis has run and completed */
370 assertEquals(paramAndResult, depModule.getAnalysisOutput());
371
372 /* Check the dependency level of both analyses */
373 assertEquals(0, depModule.getDependencyLevel());
374 assertEquals(1, module.getDependencyLevel());
375
376 module.dispose();
377 depModule.dispose();
378 trace.dispose();
379
380 }
381
382 /**
383 * Test that the dependency level is consistent with a case where
384 * B depends on A, and C depends on A and B
385 */
386 @Test
387 public void testMultipleDependencies() {
388
389 ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace();
390
391 /* Prepare module A with no dependency */
392 IAnalysisModule moduleA = new TestAnalysis();
393 moduleA.setName(MODULE_GENERIC_NAME);
394 moduleA.setId(MODULE_GENERIC_ID);
395 moduleA.addParameter(TestAnalysis.PARAM_TEST);
396 moduleA.setParameter(TestAnalysis.PARAM_TEST, 1);
397
398 /* Prepare module B depending on A */
399 String suffix = " B";
400 IAnalysisModule moduleB = new TestAnalysis() {
401
402 @Override
403 protected Iterable<IAnalysisModule> getDependentAnalyses() {
404 return ImmutableSet.of(moduleA);
405 }
406
407 };
408 moduleB.setName(MODULE_GENERIC_NAME + suffix);
409 moduleB.setId(MODULE_GENERIC_ID + suffix);
410 moduleB.addParameter(TestAnalysis.PARAM_TEST);
411 moduleB.setParameter(TestAnalysis.PARAM_TEST, 1);
412
413 /* Prepare module C depending on A and B */
414 suffix = " C";
415 IAnalysisModule moduleC = new TestAnalysis() {
416
417 @Override
418 protected Iterable<IAnalysisModule> getDependentAnalyses() {
419 return ImmutableSet.of(moduleA, moduleB);
420 }
421
422 };
423 moduleC.setName(MODULE_GENERIC_NAME + suffix);
424 moduleC.setId(MODULE_GENERIC_ID + suffix);
425 moduleC.addParameter(TestAnalysis.PARAM_TEST);
426 moduleC.setParameter(TestAnalysis.PARAM_TEST, 1);
427
428 try {
429 assertTrue(moduleA.setTrace(trace));
430 assertTrue(moduleB.setTrace(trace));
431 assertTrue(moduleC.setTrace(trace));
432 } catch (TmfAnalysisException e) {
433 fail(e.getMessage());
434 }
435
436 moduleC.schedule();
437 assertTrue(moduleC.waitForCompletion());
438
439 /* Check the dependency level of the analyses */
440 assertEquals(0, moduleA.getDependencyLevel());
441 assertEquals(1, moduleB.getDependencyLevel());
442 assertEquals(3, moduleC.getDependencyLevel());
443
444 moduleA.dispose();
445 moduleB.dispose();
446 moduleC.dispose();
447 trace.dispose();
448
449 }
450 }
This page took 0.039213 seconds and 4 git commands to generate.