1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.analysis
;
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
;
22 import java
.util
.HashSet
;
24 import java
.util
.concurrent
.TimeUnit
;
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
;
46 import com
.google
.common
.collect
.ImmutableSet
;
49 * Test suite for the {@link TmfAbstractAnalysisModule} class
51 public class AnalysisModuleTest
{
55 public TestRule timeoutRule
= new Timeout(30, TimeUnit
.SECONDS
);
57 private static final @NonNull String MODULE_GENERIC_ID
= "test.id";
58 private static final @NonNull String MODULE_GENERIC_NAME
= "Test analysis";
61 * Some tests use traces, let's clean them here
64 public void cleanupTraces() {
65 TmfTestTrace
.A_TEST_10K
.dispose();
69 * Test suite for analysis module getters and setters
72 public void testGettersSetters() {
73 IAnalysisModule module
= new TestAnalysis();
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());
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
));
89 assertEquals(0, module
.getDependencyLevel());
91 /* Try to set and get wrong parameter */
92 String wrongParam
= "abc";
93 Exception exception
= null;
95 module
.setParameter(wrongParam
, 1);
96 } catch (RuntimeException e
) {
98 assertEquals(NLS
.bind(Messages
.TmfAbstractAnalysisModule_InvalidParameter
, wrongParam
, module
.getName()), e
.getMessage());
100 assertNotNull(exception
);
101 assertNull(module
.getParameter(wrongParam
));
106 private static TestAnalysis
setUpAnalysis() {
107 TestAnalysis module
= new TestAnalysis();
109 module
.setName(MODULE_GENERIC_NAME
);
110 module
.setId(MODULE_GENERIC_ID
);
111 module
.addParameter(TestAnalysis
.PARAM_TEST
);
117 * Test suite for analysis module
118 * {@link TmfAbstractAnalysisModule#waitForCompletion} with successful
122 public void testWaitForCompletionSuccess() {
123 TestAnalysis module
= setUpAnalysis();
125 IStatus status
= module
.schedule();
126 assertEquals(IStatus
.ERROR
, status
.getSeverity());
128 /* Set a stub trace for analysis */
130 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
131 } catch (TmfAnalysisException e
) {
132 fail(e
.getMessage());
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();
141 assertTrue(completed
);
142 assertEquals(1, module
.getAnalysisOutput());
148 * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion} with
152 public void testWaitForCompletionCancelled() {
153 TestAnalysis module
= setUpAnalysis();
155 /* Set a stub trace for analysis */
156 ITmfTrace trace
= TmfTestTrace
.A_TEST_10K
.getTrace();
158 assertTrue(module
.setTrace(trace
));
159 } catch (TmfAnalysisException e
) {
160 fail(e
.getMessage());
163 module
.setParameter(TestAnalysis
.PARAM_TEST
, 0);
164 IStatus status
= module
.schedule();
165 assertEquals(Status
.OK_STATUS
, status
);
166 boolean completed
= module
.waitForCompletion();
168 assertFalse(completed
);
169 assertEquals(0, module
.getAnalysisOutput());
175 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method
179 public void testSetWrongTrace() {
180 IAnalysisModule module
= new TestAnalysis2();
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());
188 assertFalse(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
189 } catch (TmfAnalysisException e
) {
197 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method
201 public void testSetTraceTwice() {
202 IAnalysisModule module
= new TestAnalysis();
204 module
.setName(MODULE_GENERIC_NAME
);
205 module
.setId(MODULE_GENERIC_ID
);
208 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
209 } catch (TmfAnalysisException e
) {
212 TmfAnalysisException exception
= null;
214 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
215 } catch (TmfAnalysisException e
) {
218 assertNotNull(exception
);
224 * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method
227 public void testCancel() {
228 TestAnalysis module
= setUpAnalysis();
230 module
.setParameter(TestAnalysis
.PARAM_TEST
, 999);
232 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
233 } catch (TmfAnalysisException e
) {
234 fail(e
.getMessage());
237 IStatus schedule
= module
.schedule();
238 assertEquals(Status
.OK_STATUS
, schedule
);
240 /* Give the job a chance to start */
243 } catch (InterruptedException e
) {
244 fail(e
.getMessage());
248 assertFalse(module
.waitForCompletion());
249 assertEquals(-1, module
.getAnalysisOutput());
255 * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)}
259 public void testParameterChanged() {
260 TestAnalysis module
= setUpAnalysis();
263 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
264 } catch (TmfAnalysisException e
) {
265 fail(e
.getMessage());
268 /* Check exception if no wrong parameter name */
269 Exception exception
= null;
271 module
.notifyParameterChanged("aaa");
272 } catch (RuntimeException e
) {
275 assertNotNull(exception
);
278 * Cannot test anymore of this method, need a parameter provider to do
285 * Test the {@link TmfTestHelper#executeAnalysis(IAnalysisModule)} method
288 public void testHelper() {
289 TestAnalysis module
= setUpAnalysis();
292 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
293 } catch (TmfAnalysisException e
) {
294 fail(e
.getMessage());
297 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
298 boolean res
= TmfTestHelper
.executeAnalysis(module
);
301 module
.setParameter(TestAnalysis
.PARAM_TEST
, 0);
302 res
= TmfTestHelper
.executeAnalysis(module
);
309 * Test the {@link TmfAbstractAnalysisModule} also executes the dependent
313 public void testDependentAnalyses() {
315 ITmfTrace trace
= TmfTestTrace
.A_TEST_10K
.getTrace();
316 int paramAndResult
= 5;
318 /* Setup the dependent module */
319 final String suffix
= " dep";
320 final TestAnalysis depModule
= new TestAnalysis() {
323 protected boolean executeAnalysis(IProgressMonitor monitor
) {
326 } catch (InterruptedException e
) {
329 return super.executeAnalysis(monitor
);
333 depModule
.setName(MODULE_GENERIC_NAME
+ suffix
);
334 depModule
.setId(MODULE_GENERIC_ID
+ suffix
);
335 depModule
.addParameter(TestAnalysis
.PARAM_TEST
);
336 depModule
.setParameter(TestAnalysis
.PARAM_TEST
, paramAndResult
);
338 /* Prepare the main analysis with a dependent analysis */
339 TestAnalysis module
= new TestAnalysis() {
342 protected Iterable
<IAnalysisModule
> getDependentAnalyses() {
343 Set
<IAnalysisModule
> modules
= new HashSet
<>();
344 modules
.add(depModule
);
350 module
.setName(MODULE_GENERIC_NAME
);
351 module
.setId(MODULE_GENERIC_ID
);
352 module
.addParameter(TestAnalysis
.PARAM_TEST
);
353 module
.setParameter(TestAnalysis
.PARAM_TEST
, paramAndResult
);
356 assertTrue(depModule
.setTrace(trace
));
357 assertTrue(module
.setTrace(trace
));
358 } catch (TmfAnalysisException e
) {
359 fail(e
.getMessage());
362 /* Verify none of the module has run */
363 assertEquals(0, module
.getAnalysisOutput());
364 assertEquals(0, depModule
.getAnalysisOutput());
367 assertTrue(module
.waitForCompletion());
368 assertEquals(paramAndResult
, module
.getAnalysisOutput());
370 /* Make sure the dependent analysis has run and completed */
371 assertEquals(paramAndResult
, depModule
.getAnalysisOutput());
373 /* Check the dependency level of both analyses */
374 assertEquals(0, depModule
.getDependencyLevel());
375 assertEquals(1, module
.getDependencyLevel());
384 * Test that the dependency level is consistent with a case where
385 * B depends on A, and C depends on A and B
388 public void testMultipleDependencies() {
390 ITmfTrace trace
= TmfTestTrace
.A_TEST_10K
.getTrace();
392 /* Prepare module A with no dependency */
393 IAnalysisModule moduleA
= new TestAnalysis();
394 moduleA
.setName(MODULE_GENERIC_NAME
);
395 moduleA
.setId(MODULE_GENERIC_ID
);
396 moduleA
.addParameter(TestAnalysis
.PARAM_TEST
);
397 moduleA
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
399 /* Prepare module B depending on A */
400 String suffix
= " B";
401 IAnalysisModule moduleB
= new TestAnalysis() {
404 protected Iterable
<IAnalysisModule
> getDependentAnalyses() {
405 return ImmutableSet
.of(moduleA
);
409 moduleB
.setName(MODULE_GENERIC_NAME
+ suffix
);
410 moduleB
.setId(MODULE_GENERIC_ID
+ suffix
);
411 moduleB
.addParameter(TestAnalysis
.PARAM_TEST
);
412 moduleB
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
414 /* Prepare module C depending on A and B */
416 IAnalysisModule moduleC
= new TestAnalysis() {
419 protected Iterable
<IAnalysisModule
> getDependentAnalyses() {
420 return ImmutableSet
.of(moduleA
, moduleB
);
424 moduleC
.setName(MODULE_GENERIC_NAME
+ suffix
);
425 moduleC
.setId(MODULE_GENERIC_ID
+ suffix
);
426 moduleC
.addParameter(TestAnalysis
.PARAM_TEST
);
427 moduleC
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
430 assertTrue(moduleA
.setTrace(trace
));
431 assertTrue(moduleB
.setTrace(trace
));
432 assertTrue(moduleC
.setTrace(trace
));
433 } catch (TmfAnalysisException e
) {
434 fail(e
.getMessage());
438 assertTrue(moduleC
.waitForCompletion());
440 /* Check the dependency level of the analyses */
441 assertEquals(0, moduleA
.getDependencyLevel());
442 assertEquals(1, moduleB
.getDependencyLevel());
443 assertEquals(3, moduleC
.getDependencyLevel());