fbeac78c638b3eda49f25e0b10d7a43ba67cb00a
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / 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.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.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;
36
37 /**
38 * Test suite for the {@link TmfAbstractAnalysisModule} class
39 */
40 public class AnalysisModuleTest {
41
42 private static String MODULE_GENERIC_ID = "test.id";
43 private static String MODULE_GENERIC_NAME = "Test analysis";
44
45 /**
46 * Some tests use traces, let's clean them here
47 */
48 @After
49 public void cleanupTraces() {
50 TmfTestTrace.A_TEST_10K.dispose();
51 }
52
53 /**
54 * Test suite for analysis module getters and setters
55 */
56 @Test
57 public void testGettersSetters() {
58 try (IAnalysisModule module = new TestAnalysis();) {
59
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());
64
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));
73
74 /* Try to set and get wrong parameter */
75 String wrongParam = "abc";
76 Exception exception = null;
77 try {
78 module.setParameter(wrongParam, 1);
79 } catch (RuntimeException e) {
80 exception = e;
81 assertEquals(NLS.bind(Messages.TmfAbstractAnalysisModule_InvalidParameter, wrongParam, module.getName()), e.getMessage());
82 }
83 assertNotNull(exception);
84 assertNull(module.getParameter(wrongParam));
85 }
86 }
87
88 private static TestAnalysis setUpAnalysis() {
89 TestAnalysis module = new TestAnalysis();
90
91 module.setName(MODULE_GENERIC_NAME);
92 module.setId(MODULE_GENERIC_ID);
93 module.addParameter(TestAnalysis.PARAM_TEST);
94
95 return module;
96 }
97
98 /**
99 * Test suite for analysis module
100 * {@link TmfAbstractAnalysisModule#waitForCompletion} with successful
101 * execution
102 */
103 @Test
104 public void testWaitForCompletionSuccess() {
105 try (TestAnalysis module = setUpAnalysis();) {
106
107 IStatus status = module.schedule();
108 assertEquals(IStatus.ERROR, status.getSeverity());
109
110 /* Set a stub trace for analysis */
111 try {
112 module.setTrace(TmfTestTrace.A_TEST_10K.getTrace());
113 } catch (TmfAnalysisException e) {
114 fail(e.getMessage());
115 }
116
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();
122
123 assertTrue(completed);
124 assertEquals(1, module.getAnalysisOutput());
125 }
126 }
127
128 /**
129 * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion} with cancellation
130 */
131 @Test
132 public void testWaitForCompletionCancelled() {
133 try (TestAnalysis module = setUpAnalysis();) {
134
135 /* Set a stub trace for analysis */
136 ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace();
137 try {
138 module.setTrace(trace);
139 } catch (TmfAnalysisException e) {
140 fail(e.getMessage());
141 }
142
143 module.setParameter(TestAnalysis.PARAM_TEST, 0);
144 IStatus status = module.schedule();
145 assertEquals(Status.OK_STATUS, status);
146 boolean completed = module.waitForCompletion();
147
148 assertFalse(completed);
149 assertEquals(0, module.getAnalysisOutput());
150 }
151 }
152
153 /**
154 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method with wrong trace
155 */
156 @Test
157 public void testSetWrongTrace() {
158 try (IAnalysisModule module = new TestAnalysis2();) {
159
160 module.setName(MODULE_GENERIC_NAME);
161 module.setId(MODULE_GENERIC_ID);
162 assertEquals(MODULE_GENERIC_ID, module.getId());
163 assertEquals(MODULE_GENERIC_NAME, module.getName());
164
165 Exception exception = null;
166 try {
167 module.setTrace(TmfTestTrace.A_TEST_10K.getTrace());
168 } catch (TmfAnalysisException e) {
169 exception = e;
170 }
171 assertNotNull(exception);
172 assertEquals(NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisCannotExecute, module.getName()), exception.getMessage());
173 }
174 }
175
176 /**
177 * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method
178 */
179 @Test
180 public void testCancel() {
181 try (TestAnalysis module = setUpAnalysis();) {
182
183 module.setParameter(TestAnalysis.PARAM_TEST, 999);
184 try {
185 module.setTrace(TmfTestTrace.A_TEST_10K.getTrace());
186 } catch (TmfAnalysisException e) {
187 fail(e.getMessage());
188 }
189
190 assertEquals(Status.OK_STATUS, module.schedule());
191
192 /* Give the job a chance to start */
193 try {
194 Thread.sleep(1000);
195 } catch (InterruptedException e) {
196 fail(e.getMessage());
197 }
198
199 module.cancel();
200 assertFalse(module.waitForCompletion());
201 assertEquals(-1, module.getAnalysisOutput());
202 }
203 }
204
205 /**
206 * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)}
207 * method
208 */
209 @Test
210 public void testParameterChanged() {
211 try (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
230 * do this
231 */
232 }
233 }
234
235 /**
236 * Test the {@link TmfTestHelper#executeAnalysis(IAnalysisModule)} method
237 */
238 @Test
239 public void testHelper() {
240 try (TestAnalysis module = setUpAnalysis();) {
241
242 try {
243 module.setTrace(TmfTestTrace.A_TEST_10K.getTrace());
244 } catch (TmfAnalysisException e) {
245 fail(e.getMessage());
246 }
247
248 module.setParameter(TestAnalysis.PARAM_TEST, 1);
249 boolean res = TmfTestHelper.executeAnalysis(module);
250 assertTrue(res);
251
252 module.setParameter(TestAnalysis.PARAM_TEST, 0);
253 res = TmfTestHelper.executeAnalysis(module);
254 assertFalse(res);
255 }
256 }
257 }
This page took 0.037041 seconds and 4 git commands to generate.