943535b5bbb2da17a74ef8b521a48f5e95a5500b
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / analysis / AnalysisModuleTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 É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.tracecompass.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 java.util.HashSet;
23 import java.util.Set;
24
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;
41
42 /**
43 * Test suite for the {@link TmfAbstractAnalysisModule} class
44 */
45 public class AnalysisModuleTest {
46
47 private static final @NonNull String MODULE_GENERIC_ID = "test.id";
48 private static final @NonNull String MODULE_GENERIC_NAME = "Test analysis";
49
50 /**
51 * Some tests use traces, let's clean them here
52 */
53 @After
54 public void cleanupTraces() {
55 TmfTestTrace.A_TEST_10K.dispose();
56 }
57
58 /**
59 * Test suite for analysis module getters and setters
60 */
61 @Test
62 public void testGettersSetters() {
63 IAnalysisModule module = new TestAnalysis();
64
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());
69
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));
78
79 /* Try to set and get wrong parameter */
80 String wrongParam = "abc";
81 Exception exception = null;
82 try {
83 module.setParameter(wrongParam, 1);
84 } catch (RuntimeException e) {
85 exception = e;
86 assertEquals(NLS.bind(Messages.TmfAbstractAnalysisModule_InvalidParameter, wrongParam, module.getName()), e.getMessage());
87 }
88 assertNotNull(exception);
89 assertNull(module.getParameter(wrongParam));
90
91 module.dispose();
92 }
93
94 private static TestAnalysis setUpAnalysis() {
95 TestAnalysis module = new TestAnalysis();
96
97 module.setName(MODULE_GENERIC_NAME);
98 module.setId(MODULE_GENERIC_ID);
99 module.addParameter(TestAnalysis.PARAM_TEST);
100
101 return module;
102 }
103
104 /**
105 * Test suite for analysis module
106 * {@link TmfAbstractAnalysisModule#waitForCompletion} with successful
107 * execution
108 */
109 @Test
110 public void testWaitForCompletionSuccess() {
111 TestAnalysis module = setUpAnalysis();
112
113 IStatus status = module.schedule();
114 assertEquals(IStatus.ERROR, status.getSeverity());
115
116 /* Set a stub trace for analysis */
117 try {
118 module.setTrace(TmfTestTrace.A_TEST_10K.getTrace());
119 } catch (TmfAnalysisException e) {
120 fail(e.getMessage());
121 }
122
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();
128
129 assertTrue(completed);
130 assertEquals(1, module.getAnalysisOutput());
131
132 module.dispose();
133 }
134
135 /**
136 * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion} with
137 * cancellation
138 */
139 @Test
140 public void testWaitForCompletionCancelled() {
141 TestAnalysis module = setUpAnalysis();
142
143 /* Set a stub trace for analysis */
144 ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace();
145 try {
146 module.setTrace(trace);
147 } catch (TmfAnalysisException e) {
148 fail(e.getMessage());
149 }
150
151 module.setParameter(TestAnalysis.PARAM_TEST, 0);
152 IStatus status = module.schedule();
153 assertEquals(Status.OK_STATUS, status);
154 boolean completed = module.waitForCompletion();
155
156 assertFalse(completed);
157 assertEquals(0, module.getAnalysisOutput());
158
159 module.dispose();
160 }
161
162 /**
163 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method
164 * with wrong trace
165 */
166 @Test
167 public void testSetWrongTrace() {
168 IAnalysisModule module = new TestAnalysis2();
169
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());
174
175 Exception exception = null;
176 try {
177 module.setTrace(TmfTestTrace.A_TEST_10K.getTrace());
178 } catch (TmfAnalysisException e) {
179 exception = e;
180 }
181 assertNotNull(exception);
182 assertEquals(NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisCannotExecute, module.getName()), exception.getMessage());
183
184 module.dispose();
185 }
186
187 /**
188 * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method
189 */
190 @Test
191 public void testCancel() {
192 TestAnalysis module = setUpAnalysis();
193
194 module.setParameter(TestAnalysis.PARAM_TEST, 999);
195 try {
196 module.setTrace(TmfTestTrace.A_TEST_10K.getTrace());
197 } catch (TmfAnalysisException e) {
198 fail(e.getMessage());
199 }
200
201 assertEquals(Status.OK_STATUS, module.schedule());
202
203 /* Give the job a chance to start */
204 try {
205 Thread.sleep(1000);
206 } catch (InterruptedException e) {
207 fail(e.getMessage());
208 }
209
210 module.cancel();
211 assertFalse(module.waitForCompletion());
212 assertEquals(-1, module.getAnalysisOutput());
213
214 module.dispose();
215 }
216
217 /**
218 * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)}
219 * method
220 */
221 @Test
222 public void testParameterChanged() {
223 TestAnalysis module = setUpAnalysis();
224
225 try {
226 module.setTrace(TmfTestTrace.A_TEST_10K.getTrace());
227 } catch (TmfAnalysisException e) {
228 fail(e.getMessage());
229 }
230
231 /* Check exception if no wrong parameter name */
232 Exception exception = null;
233 try {
234 module.notifyParameterChanged("aaa");
235 } catch (RuntimeException e) {
236 exception = e;
237 }
238 assertNotNull(exception);
239
240 /*
241 * Cannot test anymore of this method, need a parameter provider to do
242 * this
243 */
244 module.dispose();
245 }
246
247 /**
248 * Test the {@link TmfTestHelper#executeAnalysis(IAnalysisModule)} method
249 */
250 @Test
251 public void testHelper() {
252 TestAnalysis module = setUpAnalysis();
253
254 try {
255 module.setTrace(TmfTestTrace.A_TEST_10K.getTrace());
256 } catch (TmfAnalysisException e) {
257 fail(e.getMessage());
258 }
259
260 module.setParameter(TestAnalysis.PARAM_TEST, 1);
261 boolean res = TmfTestHelper.executeAnalysis(module);
262 assertTrue(res);
263
264 module.setParameter(TestAnalysis.PARAM_TEST, 0);
265 res = TmfTestHelper.executeAnalysis(module);
266 assertFalse(res);
267
268 module.dispose();
269 }
270
271 /**
272 * Test the {@link TmfAbstractAnalysisModule} also executes the dependent
273 * analyses
274 */
275 @Test
276 public void testDependentAnalyses() {
277
278 ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace();
279 int paramAndResult = 5;
280
281 /* Setup the dependent module */
282 final String suffix = " dep";
283 final TestAnalysis depModule = new TestAnalysis() {
284
285 @Override
286 protected boolean executeAnalysis(IProgressMonitor monitor) {
287 try {
288 Thread.sleep(1000);
289 } catch (InterruptedException e) {
290 return false;
291 }
292 return super.executeAnalysis(monitor);
293 }
294
295 };
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);
300
301 /* Prepare the main analysis with a dependent analysis */
302 TestAnalysis module = new TestAnalysis() {
303
304 @Override
305 protected Iterable<IAnalysisModule> getDependentAnalyses() {
306 Set<IAnalysisModule> modules = new HashSet<>();
307 modules.add(depModule);
308 return modules;
309 }
310
311 };
312
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);
317
318 try {
319 depModule.setTrace(trace);
320 module.setTrace(trace);
321 } catch (TmfAnalysisException e) {
322 fail(e.getMessage());
323 }
324
325 /* Verify none of the module has run */
326 assertEquals(0, module.getAnalysisOutput());
327 assertEquals(0, depModule.getAnalysisOutput());
328
329 module.schedule();
330 assertTrue(module.waitForCompletion());
331 assertEquals(paramAndResult, module.getAnalysisOutput());
332
333 /* Make sure the dependent analysis has run and completed */
334 assertEquals(paramAndResult, depModule.getAnalysisOutput());
335
336 module.dispose();
337 depModule.dispose();
338 trace.dispose();
339
340 }
341 }
This page took 0.039674 seconds and 4 git commands to generate.