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 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());
128 * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion} with cancellation
131 public void testWaitForCompletionCancelled() {
132 TestAnalysis module
= setUpAnalysis();
134 /* Set a stub trace for analysis */
135 ITmfTrace trace
= TmfTestTrace
.A_TEST_10K
.getTrace();
137 module
.setTrace(trace
);
138 } catch (TmfAnalysisException e
) {
139 fail(e
.getMessage());
142 module
.setParameter(TestAnalysis
.PARAM_TEST
, 0);
143 IStatus status
= module
.schedule();
144 assertEquals(Status
.OK_STATUS
, status
);
145 boolean completed
= module
.waitForCompletion();
147 assertFalse(completed
);
148 assertEquals(0, module
.getAnalysisOutput());
152 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method with wrong trace
155 public void testSetWrongTrace() {
156 try (IAnalysisModule module
= new TestAnalysis2();) {
158 module
.setName(MODULE_GENERIC_NAME
);
159 module
.setId(MODULE_GENERIC_ID
);
160 assertEquals(MODULE_GENERIC_ID
, module
.getId());
161 assertEquals(MODULE_GENERIC_NAME
, module
.getName());
163 Exception exception
= null;
165 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
166 } catch (TmfAnalysisException e
) {
169 assertNotNull(exception
);
170 assertEquals(NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisCannotExecute
, module
.getName()), exception
.getMessage());
175 * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method
178 public void testCancel() {
179 TestAnalysis module
= setUpAnalysis();
181 module
.setParameter(TestAnalysis
.PARAM_TEST
, 999);
183 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
184 } catch (TmfAnalysisException e
) {
185 fail(e
.getMessage());
188 assertEquals(Status
.OK_STATUS
, module
.schedule());
190 /* Give the job a chance to start */
193 } catch (InterruptedException e
) {
194 fail(e
.getMessage());
198 assertFalse(module
.waitForCompletion());
199 assertEquals(-1, module
.getAnalysisOutput());
203 * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)}
207 public void testParameterChanged() {
208 TestAnalysis module
= setUpAnalysis();
211 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
212 } catch (TmfAnalysisException e
) {
213 fail(e
.getMessage());
216 /* Check exception if no wrong parameter name */
217 Exception exception
= null;
219 module
.notifyParameterChanged("aaa");
220 } catch (RuntimeException e
) {
223 assertNotNull(exception
);
226 * Cannot test anymore of this method, need a parameter provider to do
232 * Test the {@link TmfTestHelper#executeAnalysis(IAnalysisModule)} method
235 public void testHelper() {
236 TestAnalysis module
= setUpAnalysis();
239 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
240 } catch (TmfAnalysisException e
) {
241 fail(e
.getMessage());
244 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
245 boolean res
= TmfTestHelper
.executeAnalysis(module
);
248 module
.setParameter(TestAnalysis
.PARAM_TEST
, 0);
249 res
= TmfTestHelper
.executeAnalysis(module
);