1 /*******************************************************************************
2 * Copyright (c) 2013 É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
.CtfTmfTestTrace
;
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
.TestCtfAnalysis
;
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();
51 CtfTmfTestTrace
.KERNEL
.dispose();
55 * Test suite for analysis module getters and setters
58 public void testGettersSetters() {
59 IAnalysisModule module
= new TestAnalysis();
61 module
.setName(MODULE_GENERIC_NAME
);
62 module
.setId(MODULE_GENERIC_ID
);
63 assertEquals(MODULE_GENERIC_ID
, module
.getId());
64 assertEquals(MODULE_GENERIC_NAME
, module
.getName());
66 module
.setAutomatic(false);
67 assertFalse(module
.isAutomatic());
68 module
.setAutomatic(true);
69 assertTrue(module
.isAutomatic());
70 module
.addParameter(TestAnalysis
.PARAM_TEST
);
71 assertNull(module
.getParameter(TestAnalysis
.PARAM_TEST
));
72 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
73 assertEquals(1, module
.getParameter(TestAnalysis
.PARAM_TEST
));
75 /* Try to set and get wrong parameter */
76 String wrongParam
= "abc";
77 Exception exception
= null;
79 module
.setParameter(wrongParam
, 1);
80 } catch (RuntimeException e
) {
82 assertEquals(NLS
.bind(Messages
.TmfAbstractAnalysisModule_InvalidParameter
, wrongParam
, module
.getName()), e
.getMessage());
84 assertNotNull(exception
);
85 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
);
100 * Test suite for analysis module
101 * {@link TmfAbstractAnalysisModule#waitForCompletion} with successful
105 public void testWaitForCompletionSuccess() {
106 TestAnalysis module
= setUpAnalysis();
108 IStatus status
= module
.schedule();
109 assertEquals(IStatus
.ERROR
, status
.getSeverity());
111 /* Set a stub trace for analysis */
113 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
114 } catch (TmfAnalysisException e
) {
115 fail(e
.getMessage());
118 /* Default execution, with output 1 */
119 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
120 status
= module
.schedule();
121 assertEquals(Status
.OK_STATUS
, status
);
122 boolean completed
= module
.waitForCompletion();
124 assertTrue(completed
);
125 assertEquals(1, module
.getAnalysisOutput());
129 * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion} with cancellation
132 public void testWaitForCompletionCancelled() {
133 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());
153 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method with wrong trace
156 public void testSetWrongTrace() {
157 IAnalysisModule module
= new TestCtfAnalysis();
159 module
.setName(MODULE_GENERIC_NAME
);
160 module
.setId(MODULE_GENERIC_ID
);
161 assertEquals(MODULE_GENERIC_ID
, module
.getId());
162 assertEquals(MODULE_GENERIC_NAME
, module
.getName());
164 Exception exception
= null;
166 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
167 } catch (TmfAnalysisException e
) {
170 assertNotNull(exception
);
171 assertEquals(NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisCannotExecute
, module
.getName()), exception
.getMessage());
176 * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method
179 public void testCancel() {
180 TestAnalysis module
= setUpAnalysis();
182 module
.setParameter(TestAnalysis
.PARAM_TEST
, 999);
184 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
185 } catch (TmfAnalysisException e
) {
186 fail(e
.getMessage());
189 assertEquals(Status
.OK_STATUS
, module
.schedule());
191 /* Give the job a chance to start */
194 } catch (InterruptedException e
) {
195 fail(e
.getMessage());
199 assertFalse(module
.waitForCompletion());
200 assertEquals(-1, module
.getAnalysisOutput());
204 * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)}
208 public void testParameterChanged() {
209 TestAnalysis module
= setUpAnalysis();
212 module
.setTrace(TmfTestTrace
.A_TEST_10K
.getTrace());
213 } catch (TmfAnalysisException e
) {
214 fail(e
.getMessage());
217 /* Check exception if no wrong parameter name */
218 Exception exception
= null;
220 module
.notifyParameterChanged("aaa");
221 } catch (RuntimeException e
) {
224 assertNotNull(exception
);
227 * Cannot test anymore of this method, need a parameter provider to do