0596bd9e932246c30d6a4f516dca974dfdf1c130
[deliverable/tracecompass.git] / tmf / 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 assertTrue(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 assertTrue(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 try {
176 assertFalse(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
177 } catch (TmfAnalysisException e) {
178 fail();
179 }
180
181 module.dispose();
182 }
183
184 /**
185 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method
186 * with wrong trace
187 */
188 @Test
189 public void testSetTraceTwice() {
190 IAnalysisModule module = new TestAnalysis();
191
192 module.setName(MODULE_GENERIC_NAME);
193 module.setId(MODULE_GENERIC_ID);
194
195 try {
196 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
197 } catch (TmfAnalysisException e) {
198 fail();
199 }
200 TmfAnalysisException exception = null;
201 try {
202 module.setTrace(TmfTestTrace.A_TEST_10K.getTrace());
203 } catch (TmfAnalysisException e) {
204 exception = e;
205 }
206 assertNotNull(exception);
207
208 module.dispose();
209 }
210
211 /**
212 * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method
213 */
214 @Test
215 public void testCancel() {
216 TestAnalysis module = setUpAnalysis();
217
218 module.setParameter(TestAnalysis.PARAM_TEST, 999);
219 try {
220 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
221 } catch (TmfAnalysisException e) {
222 fail(e.getMessage());
223 }
224
225 assertEquals(Status.OK_STATUS, module.schedule());
226
227 /* Give the job a chance to start */
228 try {
229 Thread.sleep(1000);
230 } catch (InterruptedException e) {
231 fail(e.getMessage());
232 }
233
234 module.cancel();
235 assertFalse(module.waitForCompletion());
236 assertEquals(-1, module.getAnalysisOutput());
237
238 module.dispose();
239 }
240
241 /**
242 * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)}
243 * method
244 */
245 @Test
246 public void testParameterChanged() {
247 TestAnalysis module = setUpAnalysis();
248
249 try {
250 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
251 } catch (TmfAnalysisException e) {
252 fail(e.getMessage());
253 }
254
255 /* Check exception if no wrong parameter name */
256 Exception exception = null;
257 try {
258 module.notifyParameterChanged("aaa");
259 } catch (RuntimeException e) {
260 exception = e;
261 }
262 assertNotNull(exception);
263
264 /*
265 * Cannot test anymore of this method, need a parameter provider to do
266 * this
267 */
268 module.dispose();
269 }
270
271 /**
272 * Test the {@link TmfTestHelper#executeAnalysis(IAnalysisModule)} method
273 */
274 @Test
275 public void testHelper() {
276 TestAnalysis module = setUpAnalysis();
277
278 try {
279 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
280 } catch (TmfAnalysisException e) {
281 fail(e.getMessage());
282 }
283
284 module.setParameter(TestAnalysis.PARAM_TEST, 1);
285 boolean res = TmfTestHelper.executeAnalysis(module);
286 assertTrue(res);
287
288 module.setParameter(TestAnalysis.PARAM_TEST, 0);
289 res = TmfTestHelper.executeAnalysis(module);
290 assertFalse(res);
291
292 module.dispose();
293 }
294
295 /**
296 * Test the {@link TmfAbstractAnalysisModule} also executes the dependent
297 * analyses
298 */
299 @Test
300 public void testDependentAnalyses() {
301
302 ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace();
303 int paramAndResult = 5;
304
305 /* Setup the dependent module */
306 final String suffix = " dep";
307 final TestAnalysis depModule = new TestAnalysis() {
308
309 @Override
310 protected boolean executeAnalysis(IProgressMonitor monitor) {
311 try {
312 Thread.sleep(1000);
313 } catch (InterruptedException e) {
314 return false;
315 }
316 return super.executeAnalysis(monitor);
317 }
318
319 };
320 depModule.setName(MODULE_GENERIC_NAME + suffix);
321 depModule.setId(MODULE_GENERIC_ID + suffix);
322 depModule.addParameter(TestAnalysis.PARAM_TEST);
323 depModule.setParameter(TestAnalysis.PARAM_TEST, paramAndResult);
324
325 /* Prepare the main analysis with a dependent analysis */
326 TestAnalysis module = new TestAnalysis() {
327
328 @Override
329 protected Iterable<IAnalysisModule> getDependentAnalyses() {
330 Set<IAnalysisModule> modules = new HashSet<>();
331 modules.add(depModule);
332 return modules;
333 }
334
335 };
336
337 module.setName(MODULE_GENERIC_NAME);
338 module.setId(MODULE_GENERIC_ID);
339 module.addParameter(TestAnalysis.PARAM_TEST);
340 module.setParameter(TestAnalysis.PARAM_TEST, paramAndResult);
341
342 try {
343 assertTrue(depModule.setTrace(trace));
344 assertTrue(module.setTrace(trace));
345 } catch (TmfAnalysisException e) {
346 fail(e.getMessage());
347 }
348
349 /* Verify none of the module has run */
350 assertEquals(0, module.getAnalysisOutput());
351 assertEquals(0, depModule.getAnalysisOutput());
352
353 module.schedule();
354 assertTrue(module.waitForCompletion());
355 assertEquals(paramAndResult, module.getAnalysisOutput());
356
357 /* Make sure the dependent analysis has run and completed */
358 assertEquals(paramAndResult, depModule.getAnalysisOutput());
359
360 module.dispose();
361 depModule.dispose();
362 trace.dispose();
363
364 }
365 }
This page took 0.041799 seconds and 4 git commands to generate.