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
.linuxtools
.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 org
.eclipse
.core
.runtime
.IStatus
;
23 import org
.eclipse
.core
.runtime
.Status
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.IAnalysisModule
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.Messages
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfAnalysisException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
.TmfTestHelper
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
.TmfTestTrace
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
31 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.analysis
.TestAnalysis
;
32 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.analysis
.TestAnalysis2
;
33 import org
.eclipse
.osgi
.util
.NLS
;
34 import org
.junit
.After
;
35 import org
.junit
.Test
;
38 * Test suite for the {@link TmfAbstractAnalysisModule} class
40 public class AnalysisModuleTest
{
42 private static String MODULE_GENERIC_ID
= "test.id";
43 private static String MODULE_GENERIC_NAME
= "Test analysis";
46 * Some tests use traces, let's clean them here
49 public void cleanupTraces() {
50 TmfTestTrace
.A_TEST_10K
.dispose();
54 * Test suite for analysis module getters and setters
57 public void testGettersSetters() {
58 try (IAnalysisModule module
= new TestAnalysis();) {
60 module
.setName(MODULE_GENERIC_NAME
);
61 module
.setId(MODULE_GENERIC_ID
);
62 assertEquals(MODULE_GENERIC_ID
, module
.getId());
63 assertEquals(MODULE_GENERIC_NAME
, module
.getName());
65 module
.setAutomatic(false);
66 assertFalse(module
.isAutomatic());
67 module
.setAutomatic(true);
68 assertTrue(module
.isAutomatic());
69 module
.addParameter(TestAnalysis
.PARAM_TEST
);
70 assertNull(module
.getParameter(TestAnalysis
.PARAM_TEST
));
71 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
72 assertEquals(1, module
.getParameter(TestAnalysis
.PARAM_TEST
));
74 /* Try to set and get wrong parameter */
75 String wrongParam
= "abc";
76 Exception exception
= null;
78 module
.setParameter(wrongParam
, 1);
79 } catch (RuntimeException e
) {
81 assertEquals(NLS
.bind(Messages
.TmfAbstractAnalysisModule_InvalidParameter
, wrongParam
, module
.getName()), e
.getMessage());
83 assertNotNull(exception
);
84 assertNull(module
.getParameter(wrongParam
));
88 private static TestAnalysis
setUpAnalysis() {
89 TestAnalysis module
= new TestAnalysis();
91 module
.setName(MODULE_GENERIC_NAME
);
92 module
.setId(MODULE_GENERIC_ID
);
93 module
.addParameter(TestAnalysis
.PARAM_TEST
);
99 * Test suite for analysis module
100 * {@link TmfAbstractAnalysisModule#waitForCompletion} with successful
104 public void testWaitForCompletionSuccess() {
105 try (TestAnalysis module
= setUpAnalysis();) {
107 IStatus status
= module
.schedule();
108 assertEquals(IStatus
.ERROR
, status
.getSeverity());
110 /* Set a stub trace for analysis */
112 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
113 } catch (TmfAnalysisException e
) {
114 fail(e
.getMessage());
117 /* Default execution, with output 1 */
118 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
119 status
= module
.schedule();
120 assertEquals(Status
.OK_STATUS
, status
);
121 boolean completed
= module
.waitForCompletion();
123 assertTrue(completed
);
124 assertEquals(1, module
.getAnalysisOutput());
129 * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion} with cancellation
132 public void testWaitForCompletionCancelled() {
133 try (TestAnalysis module
= setUpAnalysis();) {
135 /* Set a stub trace for analysis */
136 ITmfTrace trace
= TmfTestTrace
.A_TEST_10K
.getTrace();
138 module
.setTrace(trace
);
139 } catch (TmfAnalysisException e
) {
140 fail(e
.getMessage());
143 module
.setParameter(TestAnalysis
.PARAM_TEST
, 0);
144 IStatus status
= module
.schedule();
145 assertEquals(Status
.OK_STATUS
, status
);
146 boolean completed
= module
.waitForCompletion();
148 assertFalse(completed
);
149 assertEquals(0, module
.getAnalysisOutput());
154 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method with wrong trace
157 public void testSetWrongTrace() {
158 try (IAnalysisModule module
= new TestAnalysis2();) {
160 module
.setName(MODULE_GENERIC_NAME
);
161 module
.setId(MODULE_GENERIC_ID
);
162 assertEquals(MODULE_GENERIC_ID
, module
.getId());
163 assertEquals(MODULE_GENERIC_NAME
, module
.getName());
165 Exception exception
= null;
167 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
168 } catch (TmfAnalysisException e
) {
171 assertNotNull(exception
);
172 assertEquals(NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisCannotExecute
, module
.getName()), exception
.getMessage());
177 * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method
180 public void testCancel() {
181 try (TestAnalysis module
= setUpAnalysis();) {
183 module
.setParameter(TestAnalysis
.PARAM_TEST
, 999);
185 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
186 } catch (TmfAnalysisException e
) {
187 fail(e
.getMessage());
190 assertEquals(Status
.OK_STATUS
, module
.schedule());
192 /* Give the job a chance to start */
195 } catch (InterruptedException e
) {
196 fail(e
.getMessage());
200 assertFalse(module
.waitForCompletion());
201 assertEquals(-1, module
.getAnalysisOutput());
206 * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)}
210 public void testParameterChanged() {
211 try (TestAnalysis module
= setUpAnalysis();) {
214 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
215 } catch (TmfAnalysisException e
) {
216 fail(e
.getMessage());
219 /* Check exception if no wrong parameter name */
220 Exception exception
= null;
222 module
.notifyParameterChanged("aaa");
223 } catch (RuntimeException e
) {
226 assertNotNull(exception
);
229 * Cannot test anymore of this method, need a parameter provider to
236 * Test the {@link TmfTestHelper#executeAnalysis(IAnalysisModule)} method
239 public void testHelper() {
240 try (TestAnalysis module
= setUpAnalysis();) {
243 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
244 } catch (TmfAnalysisException e
) {
245 fail(e
.getMessage());
248 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
249 boolean res
= TmfTestHelper
.executeAnalysis(module
);
252 module
.setParameter(TestAnalysis
.PARAM_TEST
, 0);
253 res
= TmfTestHelper
.executeAnalysis(module
);