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
;
47 * Test suite for the {@link TmfAbstractAnalysisModule} class
49 public class AnalysisModuleTest
{
53 public TestRule timeoutRule
= new Timeout(30, TimeUnit
.SECONDS
);
55 private static final @NonNull String MODULE_GENERIC_ID
= "test.id";
56 private static final @NonNull String MODULE_GENERIC_NAME
= "Test analysis";
59 * Some tests use traces, let's clean them here
62 public void cleanupTraces() {
63 TmfTestTrace
.A_TEST_10K
.dispose();
67 * Test suite for analysis module getters and setters
70 public void testGettersSetters() {
71 IAnalysisModule module
= new TestAnalysis();
73 module
.setName(MODULE_GENERIC_NAME
);
74 module
.setId(MODULE_GENERIC_ID
);
75 assertEquals(MODULE_GENERIC_ID
, module
.getId());
76 assertEquals(MODULE_GENERIC_NAME
, module
.getName());
78 module
.setAutomatic(false);
79 assertFalse(module
.isAutomatic());
80 module
.setAutomatic(true);
81 assertTrue(module
.isAutomatic());
82 module
.addParameter(TestAnalysis
.PARAM_TEST
);
83 assertNull(module
.getParameter(TestAnalysis
.PARAM_TEST
));
84 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
85 assertEquals(1, module
.getParameter(TestAnalysis
.PARAM_TEST
));
87 /* Try to set and get wrong parameter */
88 String wrongParam
= "abc";
89 Exception exception
= null;
91 module
.setParameter(wrongParam
, 1);
92 } catch (RuntimeException e
) {
94 assertEquals(NLS
.bind(Messages
.TmfAbstractAnalysisModule_InvalidParameter
, wrongParam
, module
.getName()), e
.getMessage());
96 assertNotNull(exception
);
97 assertNull(module
.getParameter(wrongParam
));
102 private static TestAnalysis
setUpAnalysis() {
103 TestAnalysis module
= new TestAnalysis();
105 module
.setName(MODULE_GENERIC_NAME
);
106 module
.setId(MODULE_GENERIC_ID
);
107 module
.addParameter(TestAnalysis
.PARAM_TEST
);
113 * Test suite for analysis module
114 * {@link TmfAbstractAnalysisModule#waitForCompletion} with successful
118 public void testWaitForCompletionSuccess() {
119 TestAnalysis module
= setUpAnalysis();
121 IStatus status
= module
.schedule();
122 assertEquals(IStatus
.ERROR
, status
.getSeverity());
124 /* Set a stub trace for analysis */
126 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
127 } catch (TmfAnalysisException e
) {
128 fail(e
.getMessage());
131 /* Default execution, with output 1 */
132 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
133 status
= module
.schedule();
134 assertEquals(Status
.OK_STATUS
, status
);
135 boolean completed
= module
.waitForCompletion();
137 assertTrue(completed
);
138 assertEquals(1, module
.getAnalysisOutput());
144 * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion} with
148 public void testWaitForCompletionCancelled() {
149 TestAnalysis module
= setUpAnalysis();
151 /* Set a stub trace for analysis */
152 ITmfTrace trace
= TmfTestTrace
.A_TEST_10K
.getTrace();
154 assertTrue(module
.setTrace(trace
));
155 } catch (TmfAnalysisException e
) {
156 fail(e
.getMessage());
159 module
.setParameter(TestAnalysis
.PARAM_TEST
, 0);
160 IStatus status
= module
.schedule();
161 assertEquals(Status
.OK_STATUS
, status
);
162 boolean completed
= module
.waitForCompletion();
164 assertFalse(completed
);
165 assertEquals(0, module
.getAnalysisOutput());
171 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method
175 public void testSetWrongTrace() {
176 IAnalysisModule module
= new TestAnalysis2();
178 module
.setName(MODULE_GENERIC_NAME
);
179 module
.setId(MODULE_GENERIC_ID
);
180 assertEquals(MODULE_GENERIC_ID
, module
.getId());
181 assertEquals(MODULE_GENERIC_NAME
, module
.getName());
184 assertFalse(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
185 } catch (TmfAnalysisException e
) {
193 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method
197 public void testSetTraceTwice() {
198 IAnalysisModule module
= new TestAnalysis();
200 module
.setName(MODULE_GENERIC_NAME
);
201 module
.setId(MODULE_GENERIC_ID
);
204 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
205 } catch (TmfAnalysisException e
) {
208 TmfAnalysisException exception
= null;
210 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
211 } catch (TmfAnalysisException e
) {
214 assertNotNull(exception
);
220 * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method
223 public void testCancel() {
224 TestAnalysis module
= setUpAnalysis();
226 module
.setParameter(TestAnalysis
.PARAM_TEST
, 999);
228 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
229 } catch (TmfAnalysisException e
) {
230 fail(e
.getMessage());
233 assertEquals(Status
.OK_STATUS
, module
.schedule());
235 /* Give the job a chance to start */
238 } catch (InterruptedException e
) {
239 fail(e
.getMessage());
243 assertFalse(module
.waitForCompletion());
244 assertEquals(-1, module
.getAnalysisOutput());
250 * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)}
254 public void testParameterChanged() {
255 TestAnalysis module
= setUpAnalysis();
258 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
259 } catch (TmfAnalysisException e
) {
260 fail(e
.getMessage());
263 /* Check exception if no wrong parameter name */
264 Exception exception
= null;
266 module
.notifyParameterChanged("aaa");
267 } catch (RuntimeException e
) {
270 assertNotNull(exception
);
273 * Cannot test anymore of this method, need a parameter provider to do
280 * Test the {@link TmfTestHelper#executeAnalysis(IAnalysisModule)} method
283 public void testHelper() {
284 TestAnalysis module
= setUpAnalysis();
287 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
288 } catch (TmfAnalysisException e
) {
289 fail(e
.getMessage());
292 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
293 boolean res
= TmfTestHelper
.executeAnalysis(module
);
296 module
.setParameter(TestAnalysis
.PARAM_TEST
, 0);
297 res
= TmfTestHelper
.executeAnalysis(module
);
304 * Test the {@link TmfAbstractAnalysisModule} also executes the dependent
308 public void testDependentAnalyses() {
310 ITmfTrace trace
= TmfTestTrace
.A_TEST_10K
.getTrace();
311 int paramAndResult
= 5;
313 /* Setup the dependent module */
314 final String suffix
= " dep";
315 final TestAnalysis depModule
= new TestAnalysis() {
318 protected boolean executeAnalysis(IProgressMonitor monitor
) {
321 } catch (InterruptedException e
) {
324 return super.executeAnalysis(monitor
);
328 depModule
.setName(MODULE_GENERIC_NAME
+ suffix
);
329 depModule
.setId(MODULE_GENERIC_ID
+ suffix
);
330 depModule
.addParameter(TestAnalysis
.PARAM_TEST
);
331 depModule
.setParameter(TestAnalysis
.PARAM_TEST
, paramAndResult
);
333 /* Prepare the main analysis with a dependent analysis */
334 TestAnalysis module
= new TestAnalysis() {
337 protected Iterable
<IAnalysisModule
> getDependentAnalyses() {
338 Set
<IAnalysisModule
> modules
= new HashSet
<>();
339 modules
.add(depModule
);
345 module
.setName(MODULE_GENERIC_NAME
);
346 module
.setId(MODULE_GENERIC_ID
);
347 module
.addParameter(TestAnalysis
.PARAM_TEST
);
348 module
.setParameter(TestAnalysis
.PARAM_TEST
, paramAndResult
);
351 assertTrue(depModule
.setTrace(trace
));
352 assertTrue(module
.setTrace(trace
));
353 } catch (TmfAnalysisException e
) {
354 fail(e
.getMessage());
357 /* Verify none of the module has run */
358 assertEquals(0, module
.getAnalysisOutput());
359 assertEquals(0, depModule
.getAnalysisOutput());
362 assertTrue(module
.waitForCompletion());
363 assertEquals(paramAndResult
, module
.getAnalysisOutput());
365 /* Make sure the dependent analysis has run and completed */
366 assertEquals(paramAndResult
, depModule
.getAnalysisOutput());