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
;
25 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.IStatus
;
27 import org
.eclipse
.core
.runtime
.Status
;
28 import org
.eclipse
.jdt
.annotation
.NonNull
;
29 import org
.eclipse
.osgi
.util
.NLS
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.Messages
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.shared
.TmfTestHelper
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.shared
.TmfTestTrace
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
37 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.analysis
.TestAnalysis
;
38 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.analysis
.TestAnalysis2
;
39 import org
.junit
.After
;
40 import org
.junit
.Test
;
43 * Test suite for the {@link TmfAbstractAnalysisModule} class
45 public class AnalysisModuleTest
{
47 private static final @NonNull String MODULE_GENERIC_ID
= "test.id";
48 private static final @NonNull String MODULE_GENERIC_NAME
= "Test analysis";
51 * Some tests use traces, let's clean them here
54 public void cleanupTraces() {
55 TmfTestTrace
.A_TEST_10K
.dispose();
59 * Test suite for analysis module getters and setters
62 public void testGettersSetters() {
63 IAnalysisModule module
= new TestAnalysis();
65 module
.setName(MODULE_GENERIC_NAME
);
66 module
.setId(MODULE_GENERIC_ID
);
67 assertEquals(MODULE_GENERIC_ID
, module
.getId());
68 assertEquals(MODULE_GENERIC_NAME
, module
.getName());
70 module
.setAutomatic(false);
71 assertFalse(module
.isAutomatic());
72 module
.setAutomatic(true);
73 assertTrue(module
.isAutomatic());
74 module
.addParameter(TestAnalysis
.PARAM_TEST
);
75 assertNull(module
.getParameter(TestAnalysis
.PARAM_TEST
));
76 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
77 assertEquals(1, module
.getParameter(TestAnalysis
.PARAM_TEST
));
79 /* Try to set and get wrong parameter */
80 String wrongParam
= "abc";
81 Exception exception
= null;
83 module
.setParameter(wrongParam
, 1);
84 } catch (RuntimeException e
) {
86 assertEquals(NLS
.bind(Messages
.TmfAbstractAnalysisModule_InvalidParameter
, wrongParam
, module
.getName()), e
.getMessage());
88 assertNotNull(exception
);
89 assertNull(module
.getParameter(wrongParam
));
94 private static TestAnalysis
setUpAnalysis() {
95 TestAnalysis module
= new TestAnalysis();
97 module
.setName(MODULE_GENERIC_NAME
);
98 module
.setId(MODULE_GENERIC_ID
);
99 module
.addParameter(TestAnalysis
.PARAM_TEST
);
105 * Test suite for analysis module
106 * {@link TmfAbstractAnalysisModule#waitForCompletion} with successful
110 public void testWaitForCompletionSuccess() {
111 TestAnalysis module
= setUpAnalysis();
113 IStatus status
= module
.schedule();
114 assertEquals(IStatus
.ERROR
, status
.getSeverity());
116 /* Set a stub trace for analysis */
118 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
119 } catch (TmfAnalysisException e
) {
120 fail(e
.getMessage());
123 /* Default execution, with output 1 */
124 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
125 status
= module
.schedule();
126 assertEquals(Status
.OK_STATUS
, status
);
127 boolean completed
= module
.waitForCompletion();
129 assertTrue(completed
);
130 assertEquals(1, module
.getAnalysisOutput());
136 * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion} with
140 public void testWaitForCompletionCancelled() {
141 TestAnalysis module
= setUpAnalysis();
143 /* Set a stub trace for analysis */
144 ITmfTrace trace
= TmfTestTrace
.A_TEST_10K
.getTrace();
146 assertTrue(module
.setTrace(trace
));
147 } catch (TmfAnalysisException e
) {
148 fail(e
.getMessage());
151 module
.setParameter(TestAnalysis
.PARAM_TEST
, 0);
152 IStatus status
= module
.schedule();
153 assertEquals(Status
.OK_STATUS
, status
);
154 boolean completed
= module
.waitForCompletion();
156 assertFalse(completed
);
157 assertEquals(0, module
.getAnalysisOutput());
163 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method
167 public void testSetWrongTrace() {
168 IAnalysisModule module
= new TestAnalysis2();
170 module
.setName(MODULE_GENERIC_NAME
);
171 module
.setId(MODULE_GENERIC_ID
);
172 assertEquals(MODULE_GENERIC_ID
, module
.getId());
173 assertEquals(MODULE_GENERIC_NAME
, module
.getName());
176 assertFalse(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
177 } catch (TmfAnalysisException e
) {
185 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method
189 public void testSetTraceTwice() {
190 IAnalysisModule module
= new TestAnalysis();
192 module
.setName(MODULE_GENERIC_NAME
);
193 module
.setId(MODULE_GENERIC_ID
);
196 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
197 } catch (TmfAnalysisException e
) {
200 TmfAnalysisException exception
= null;
202 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
203 } catch (TmfAnalysisException e
) {
206 assertNotNull(exception
);
212 * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method
215 public void testCancel() {
216 TestAnalysis module
= setUpAnalysis();
218 module
.setParameter(TestAnalysis
.PARAM_TEST
, 999);
220 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
221 } catch (TmfAnalysisException e
) {
222 fail(e
.getMessage());
225 assertEquals(Status
.OK_STATUS
, module
.schedule());
227 /* Give the job a chance to start */
230 } catch (InterruptedException e
) {
231 fail(e
.getMessage());
235 assertFalse(module
.waitForCompletion());
236 assertEquals(-1, module
.getAnalysisOutput());
242 * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)}
246 public void testParameterChanged() {
247 TestAnalysis module
= setUpAnalysis();
250 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
251 } catch (TmfAnalysisException e
) {
252 fail(e
.getMessage());
255 /* Check exception if no wrong parameter name */
256 Exception exception
= null;
258 module
.notifyParameterChanged("aaa");
259 } catch (RuntimeException e
) {
262 assertNotNull(exception
);
265 * Cannot test anymore of this method, need a parameter provider to do
272 * Test the {@link TmfTestHelper#executeAnalysis(IAnalysisModule)} method
275 public void testHelper() {
276 TestAnalysis module
= setUpAnalysis();
279 assertTrue(module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace()));
280 } catch (TmfAnalysisException e
) {
281 fail(e
.getMessage());
284 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
285 boolean res
= TmfTestHelper
.executeAnalysis(module
);
288 module
.setParameter(TestAnalysis
.PARAM_TEST
, 0);
289 res
= TmfTestHelper
.executeAnalysis(module
);
296 * Test the {@link TmfAbstractAnalysisModule} also executes the dependent
300 public void testDependentAnalyses() {
302 ITmfTrace trace
= TmfTestTrace
.A_TEST_10K
.getTrace();
303 int paramAndResult
= 5;
305 /* Setup the dependent module */
306 final String suffix
= " dep";
307 final TestAnalysis depModule
= new TestAnalysis() {
310 protected boolean executeAnalysis(IProgressMonitor monitor
) {
313 } catch (InterruptedException e
) {
316 return super.executeAnalysis(monitor
);
320 depModule
.setName(MODULE_GENERIC_NAME
+ suffix
);
321 depModule
.setId(MODULE_GENERIC_ID
+ suffix
);
322 depModule
.addParameter(TestAnalysis
.PARAM_TEST
);
323 depModule
.setParameter(TestAnalysis
.PARAM_TEST
, paramAndResult
);
325 /* Prepare the main analysis with a dependent analysis */
326 TestAnalysis module
= new TestAnalysis() {
329 protected Iterable
<IAnalysisModule
> getDependentAnalyses() {
330 Set
<IAnalysisModule
> modules
= new HashSet
<>();
331 modules
.add(depModule
);
337 module
.setName(MODULE_GENERIC_NAME
);
338 module
.setId(MODULE_GENERIC_ID
);
339 module
.addParameter(TestAnalysis
.PARAM_TEST
);
340 module
.setParameter(TestAnalysis
.PARAM_TEST
, paramAndResult
);
343 assertTrue(depModule
.setTrace(trace
));
344 assertTrue(module
.setTrace(trace
));
345 } catch (TmfAnalysisException e
) {
346 fail(e
.getMessage());
349 /* Verify none of the module has run */
350 assertEquals(0, module
.getAnalysisOutput());
351 assertEquals(0, depModule
.getAnalysisOutput());
354 assertTrue(module
.waitForCompletion());
355 assertEquals(paramAndResult
, module
.getAnalysisOutput());
357 /* Make sure the dependent analysis has run and completed */
358 assertEquals(paramAndResult
, depModule
.getAnalysisOutput());