Commit | Line | Data |
---|---|---|
c068a752 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 École Polytechnique de Montréal | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.tests.analysis; | |
14 | ||
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; | |
21 | ||
22 | import org.eclipse.core.runtime.IProgressMonitor; | |
23 | import org.eclipse.core.runtime.IStatus; | |
24 | import org.eclipse.core.runtime.NullProgressMonitor; | |
25 | import org.eclipse.core.runtime.Status; | |
26 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule; | |
27 | import org.eclipse.linuxtools.tmf.core.analysis.Messages; | |
28 | import org.eclipse.linuxtools.tmf.core.analysis.TmfAbstractAnalysisModule; | |
29 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException; | |
30 | import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace; | |
31 | import org.eclipse.linuxtools.tmf.core.tests.shared.TmfTestTrace; | |
32 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
33 | import org.eclipse.linuxtools.tmf.tests.stubs.analysis.TestAnalysis; | |
34 | import org.eclipse.linuxtools.tmf.tests.stubs.analysis.TestCtfAnalysis; | |
35 | import org.eclipse.osgi.util.NLS; | |
36 | import org.junit.After; | |
37 | import org.junit.Test; | |
38 | ||
39 | /** | |
40 | * Test suite for the {@link TmfAbstractAnalysisModule} class | |
41 | */ | |
42 | public class AnalysisModuleTest { | |
43 | ||
44 | private static String MODULE_GENERIC_ID = "test.id"; | |
45 | private static String MODULE_GENERIC_NAME = "Test analysis"; | |
46 | ||
47 | /** | |
48 | * Some tests use traces, let's clean them here | |
49 | */ | |
50 | @After | |
51 | public void cleanupTraces() { | |
52 | TmfTestTrace.A_TEST_10K.dispose(); | |
53 | CtfTmfTestTrace.KERNEL.dispose(); | |
54 | } | |
55 | ||
56 | /** | |
57 | * Test suite for analysis module getters and setters | |
58 | */ | |
59 | @Test | |
60 | public void testGettersSetters() { | |
61 | IAnalysisModule module = new TestAnalysis(); | |
62 | ||
63 | module.setName(MODULE_GENERIC_NAME); | |
64 | module.setId(MODULE_GENERIC_ID); | |
65 | assertEquals(MODULE_GENERIC_ID, module.getId()); | |
66 | assertEquals(MODULE_GENERIC_NAME, module.getName()); | |
67 | ||
68 | module.setAutomatic(false); | |
69 | assertFalse(module.isAutomatic()); | |
70 | module.setAutomatic(true); | |
71 | assertTrue(module.isAutomatic()); | |
72 | module.addParameter(TestAnalysis.PARAM_TEST); | |
73 | assertNull(module.getParameter(TestAnalysis.PARAM_TEST)); | |
74 | module.setParameter(TestAnalysis.PARAM_TEST, 1); | |
75 | assertEquals(1, module.getParameter(TestAnalysis.PARAM_TEST)); | |
76 | ||
77 | /* Try to set and get wrong parameter */ | |
78 | String wrongParam = "abc"; | |
79 | Exception exception = null; | |
80 | try { | |
81 | module.setParameter(wrongParam, 1); | |
82 | } catch (RuntimeException e) { | |
83 | exception = e; | |
84 | assertEquals(NLS.bind(Messages.TmfAbstractAnalysisModule_InvalidParameter, wrongParam, module.getName()), e.getMessage()); | |
85 | } | |
86 | assertNotNull(exception); | |
87 | assertNull(module.getParameter(wrongParam)); | |
88 | } | |
89 | ||
90 | private static TestAnalysis setUpAnalysis() { | |
91 | TestAnalysis module = new TestAnalysis(); | |
92 | ||
93 | module.setName(MODULE_GENERIC_NAME); | |
94 | module.setId(MODULE_GENERIC_ID); | |
95 | module.addParameter(TestAnalysis.PARAM_TEST); | |
96 | ||
97 | return module; | |
98 | ||
99 | } | |
100 | ||
101 | /** | |
102 | * Test suite for analysis module | |
103 | * {@link TmfAbstractAnalysisModule#waitForCompletion(IProgressMonitor)} with | |
104 | * successful execution | |
105 | */ | |
106 | @Test | |
107 | public void testWaitForCompletionSuccess() { | |
108 | TestAnalysis module = setUpAnalysis(); | |
109 | ||
110 | IStatus status = module.schedule(); | |
111 | assertEquals(IStatus.ERROR, status.getSeverity()); | |
112 | ||
113 | /* Set a stub trace for analysis */ | |
114 | try { | |
115 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); | |
116 | } catch (TmfAnalysisException e) { | |
117 | fail(e.getMessage()); | |
118 | } | |
119 | ||
120 | /* Default execution, with output 1 */ | |
121 | module.setParameter(TestAnalysis.PARAM_TEST, 1); | |
122 | status = module.schedule(); | |
123 | assertEquals(Status.OK_STATUS, status); | |
124 | boolean completed = module.waitForCompletion(new NullProgressMonitor()); | |
125 | ||
126 | assertTrue(completed); | |
127 | assertEquals(1, module.getAnalysisOutput()); | |
128 | } | |
129 | ||
130 | /** | |
131 | * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion(IProgressMonitor)} with cancellation | |
132 | */ | |
133 | @Test | |
134 | public void testWaitForCompletionCancelled() { | |
135 | TestAnalysis module = setUpAnalysis(); | |
136 | ||
137 | /* Set a stub trace for analysis */ | |
138 | ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace(); | |
139 | try { | |
140 | module.setTrace(trace); | |
141 | } catch (TmfAnalysisException e) { | |
142 | fail(e.getMessage()); | |
143 | } | |
144 | ||
145 | module.setParameter(TestAnalysis.PARAM_TEST, 0); | |
146 | IStatus status = module.schedule(); | |
147 | assertEquals(Status.OK_STATUS, status); | |
148 | boolean completed = module.waitForCompletion(new NullProgressMonitor()); | |
149 | ||
150 | assertFalse(completed); | |
151 | assertEquals(0, module.getAnalysisOutput()); | |
152 | } | |
153 | ||
154 | /** | |
155 | * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method with wrong trace | |
156 | */ | |
157 | @Test | |
158 | public void testSetWrongTrace() { | |
159 | IAnalysisModule module = new TestCtfAnalysis(); | |
160 | ||
161 | module.setName(MODULE_GENERIC_NAME); | |
162 | module.setId(MODULE_GENERIC_ID); | |
163 | assertEquals(MODULE_GENERIC_ID, module.getId()); | |
164 | assertEquals(MODULE_GENERIC_NAME, module.getName()); | |
165 | ||
166 | Exception exception = null; | |
167 | try { | |
168 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); | |
169 | } catch (TmfAnalysisException e) { | |
170 | exception = e; | |
171 | } | |
172 | assertNotNull(exception); | |
173 | assertEquals(NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisCannotExecute, module.getName()), exception.getMessage()); | |
174 | ||
175 | } | |
176 | ||
177 | /** | |
178 | * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method | |
179 | */ | |
180 | @Test | |
181 | public void testCancel() { | |
182 | TestAnalysis module = setUpAnalysis(); | |
183 | ||
184 | module.setParameter(TestAnalysis.PARAM_TEST, 999); | |
185 | try { | |
186 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); | |
187 | } catch (TmfAnalysisException e) { | |
188 | fail(e.getMessage()); | |
189 | } | |
190 | ||
191 | assertEquals(Status.OK_STATUS, module.schedule()); | |
192 | ||
193 | /* Give the job a chance to start */ | |
194 | try { | |
195 | Thread.sleep(1000); | |
196 | } catch (InterruptedException e) { | |
197 | fail(e.getMessage()); | |
198 | } | |
199 | ||
200 | module.cancel(); | |
201 | assertFalse(module.waitForCompletion(new NullProgressMonitor())); | |
202 | assertEquals(-1, module.getAnalysisOutput()); | |
203 | } | |
204 | ||
205 | /** | |
206 | * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)} | |
207 | * method | |
208 | */ | |
209 | @Test | |
210 | public void testParameterChanged() { | |
211 | TestAnalysis module = setUpAnalysis(); | |
212 | ||
213 | try { | |
214 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); | |
215 | } catch (TmfAnalysisException e) { | |
216 | fail(e.getMessage()); | |
217 | } | |
218 | ||
219 | /* Check exception if no wrong parameter name */ | |
220 | Exception exception = null; | |
221 | try { | |
222 | module.notifyParameterChanged("aaa"); | |
223 | } catch (RuntimeException e) { | |
224 | exception = e; | |
225 | } | |
226 | assertNotNull(exception); | |
227 | ||
228 | /* | |
229 | * Cannot test anymore of this method, need a parameter provider to do | |
230 | * this | |
231 | */ | |
232 | } | |
233 | } |