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 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 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());
175 Exception exception
= null;
177 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
178 } catch (TmfAnalysisException e
) {
181 assertNotNull(exception
);
182 assertEquals(NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisCannotExecute
, module
.getName()), exception
.getMessage());
188 * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method
191 public void testCancel() {
192 TestAnalysis module
= setUpAnalysis();
194 module
.setParameter(TestAnalysis
.PARAM_TEST
, 999);
196 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
197 } catch (TmfAnalysisException e
) {
198 fail(e
.getMessage());
201 assertEquals(Status
.OK_STATUS
, module
.schedule());
203 /* Give the job a chance to start */
206 } catch (InterruptedException e
) {
207 fail(e
.getMessage());
211 assertFalse(module
.waitForCompletion());
212 assertEquals(-1, module
.getAnalysisOutput());
218 * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)}
222 public void testParameterChanged() {
223 TestAnalysis module
= setUpAnalysis();
226 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
227 } catch (TmfAnalysisException e
) {
228 fail(e
.getMessage());
231 /* Check exception if no wrong parameter name */
232 Exception exception
= null;
234 module
.notifyParameterChanged("aaa");
235 } catch (RuntimeException e
) {
238 assertNotNull(exception
);
241 * Cannot test anymore of this method, need a parameter provider to do
248 * Test the {@link TmfTestHelper#executeAnalysis(IAnalysisModule)} method
251 public void testHelper() {
252 TestAnalysis module
= setUpAnalysis();
255 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
256 } catch (TmfAnalysisException e
) {
257 fail(e
.getMessage());
260 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
261 boolean res
= TmfTestHelper
.executeAnalysis(module
);
264 module
.setParameter(TestAnalysis
.PARAM_TEST
, 0);
265 res
= TmfTestHelper
.executeAnalysis(module
);
272 * Test the {@link TmfAbstractAnalysisModule} also executes the dependent
276 public void testDependentAnalyses() {
278 ITmfTrace trace
= TmfTestTrace
.A_TEST_10K
.getTrace();
279 int paramAndResult
= 5;
281 /* Setup the dependent module */
282 final String suffix
= " dep";
283 final TestAnalysis depModule
= new TestAnalysis() {
286 protected boolean executeAnalysis(IProgressMonitor monitor
) {
289 } catch (InterruptedException e
) {
292 return super.executeAnalysis(monitor
);
296 depModule
.setName(MODULE_GENERIC_NAME
+ suffix
);
297 depModule
.setId(MODULE_GENERIC_ID
+ suffix
);
298 depModule
.addParameter(TestAnalysis
.PARAM_TEST
);
299 depModule
.setParameter(TestAnalysis
.PARAM_TEST
, paramAndResult
);
301 /* Prepare the main analysis with a dependent analysis */
302 TestAnalysis module
= new TestAnalysis() {
305 protected Iterable
<IAnalysisModule
> getDependentAnalyses() {
306 Set
<IAnalysisModule
> modules
= new HashSet
<>();
307 modules
.add(depModule
);
313 module
.setName(MODULE_GENERIC_NAME
);
314 module
.setId(MODULE_GENERIC_ID
);
315 module
.addParameter(TestAnalysis
.PARAM_TEST
);
316 module
.setParameter(TestAnalysis
.PARAM_TEST
, paramAndResult
);
319 depModule
.setTrace(trace
);
320 module
.setTrace(trace
);
321 } catch (TmfAnalysisException e
) {
322 fail(e
.getMessage());
325 /* Verify none of the module has run */
326 assertEquals(0, module
.getAnalysisOutput());
327 assertEquals(0, depModule
.getAnalysisOutput());
330 assertTrue(module
.waitForCompletion());
331 assertEquals(paramAndResult
, module
.getAnalysisOutput());
333 /* Make sure the dependent analysis has run and completed */
334 assertEquals(paramAndResult
, depModule
.getAnalysisOutput());