Commit | Line | Data |
---|---|---|
c068a752 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 École Polytechnique de Montréal |
c068a752 GB |
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 | ||
c068a752 | 22 | import org.eclipse.core.runtime.IStatus; |
c068a752 GB |
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; | |
0e3d9d4a | 28 | import org.eclipse.linuxtools.tmf.core.tests.shared.TmfTestHelper; |
c068a752 GB |
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; | |
4d2857be | 32 | import org.eclipse.linuxtools.tmf.tests.stubs.analysis.TestAnalysis2; |
c068a752 GB |
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(); | |
c068a752 GB |
51 | } |
52 | ||
53 | /** | |
54 | * Test suite for analysis module getters and setters | |
55 | */ | |
56 | @Test | |
57 | public void testGettersSetters() { | |
bd64ee73 AM |
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)); | |
c068a752 | 85 | } |
c068a752 GB |
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; | |
c068a752 GB |
96 | } |
97 | ||
98 | /** | |
99 | * Test suite for analysis module | |
7d6122fc AM |
100 | * {@link TmfAbstractAnalysisModule#waitForCompletion} with successful |
101 | * execution | |
c068a752 GB |
102 | */ |
103 | @Test | |
104 | public void testWaitForCompletionSuccess() { | |
105 | 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); | |
7d6122fc | 121 | boolean completed = module.waitForCompletion(); |
c068a752 GB |
122 | |
123 | assertTrue(completed); | |
124 | assertEquals(1, module.getAnalysisOutput()); | |
125 | } | |
126 | ||
127 | /** | |
7d6122fc | 128 | * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion} with cancellation |
c068a752 GB |
129 | */ |
130 | @Test | |
131 | public void testWaitForCompletionCancelled() { | |
132 | TestAnalysis module = setUpAnalysis(); | |
133 | ||
134 | /* Set a stub trace for analysis */ | |
135 | ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace(); | |
136 | try { | |
137 | module.setTrace(trace); | |
138 | } catch (TmfAnalysisException e) { | |
139 | fail(e.getMessage()); | |
140 | } | |
141 | ||
142 | module.setParameter(TestAnalysis.PARAM_TEST, 0); | |
143 | IStatus status = module.schedule(); | |
144 | assertEquals(Status.OK_STATUS, status); | |
7d6122fc | 145 | boolean completed = module.waitForCompletion(); |
c068a752 GB |
146 | |
147 | assertFalse(completed); | |
148 | assertEquals(0, module.getAnalysisOutput()); | |
149 | } | |
150 | ||
151 | /** | |
152 | * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method with wrong trace | |
153 | */ | |
154 | @Test | |
155 | public void testSetWrongTrace() { | |
bd64ee73 AM |
156 | try (IAnalysisModule module = new TestAnalysis2();) { |
157 | ||
158 | module.setName(MODULE_GENERIC_NAME); | |
159 | module.setId(MODULE_GENERIC_ID); | |
160 | assertEquals(MODULE_GENERIC_ID, module.getId()); | |
161 | assertEquals(MODULE_GENERIC_NAME, module.getName()); | |
162 | ||
163 | Exception exception = null; | |
164 | try { | |
165 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); | |
166 | } catch (TmfAnalysisException e) { | |
167 | exception = e; | |
168 | } | |
169 | assertNotNull(exception); | |
170 | assertEquals(NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisCannotExecute, module.getName()), exception.getMessage()); | |
c068a752 | 171 | } |
c068a752 GB |
172 | } |
173 | ||
174 | /** | |
175 | * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method | |
176 | */ | |
177 | @Test | |
178 | public void testCancel() { | |
179 | TestAnalysis module = setUpAnalysis(); | |
180 | ||
181 | module.setParameter(TestAnalysis.PARAM_TEST, 999); | |
182 | try { | |
183 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); | |
184 | } catch (TmfAnalysisException e) { | |
185 | fail(e.getMessage()); | |
186 | } | |
187 | ||
188 | assertEquals(Status.OK_STATUS, module.schedule()); | |
189 | ||
190 | /* Give the job a chance to start */ | |
191 | try { | |
192 | Thread.sleep(1000); | |
193 | } catch (InterruptedException e) { | |
194 | fail(e.getMessage()); | |
195 | } | |
196 | ||
197 | module.cancel(); | |
7d6122fc | 198 | assertFalse(module.waitForCompletion()); |
c068a752 GB |
199 | assertEquals(-1, module.getAnalysisOutput()); |
200 | } | |
201 | ||
202 | /** | |
203 | * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)} | |
204 | * method | |
205 | */ | |
206 | @Test | |
207 | public void testParameterChanged() { | |
208 | TestAnalysis module = setUpAnalysis(); | |
209 | ||
210 | try { | |
211 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); | |
212 | } catch (TmfAnalysisException e) { | |
213 | fail(e.getMessage()); | |
214 | } | |
215 | ||
216 | /* Check exception if no wrong parameter name */ | |
217 | Exception exception = null; | |
218 | try { | |
219 | module.notifyParameterChanged("aaa"); | |
220 | } catch (RuntimeException e) { | |
221 | exception = e; | |
222 | } | |
223 | assertNotNull(exception); | |
224 | ||
225 | /* | |
226 | * Cannot test anymore of this method, need a parameter provider to do | |
227 | * this | |
228 | */ | |
229 | } | |
0e3d9d4a GB |
230 | |
231 | /** | |
232 | * Test the {@link TmfTestHelper#executeAnalysis(IAnalysisModule)} method | |
233 | */ | |
234 | @Test | |
235 | public void testHelper() { | |
236 | TestAnalysis module = setUpAnalysis(); | |
237 | ||
238 | try { | |
239 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); | |
240 | } catch (TmfAnalysisException e) { | |
241 | fail(e.getMessage()); | |
242 | } | |
243 | ||
244 | module.setParameter(TestAnalysis.PARAM_TEST, 1); | |
245 | boolean res = TmfTestHelper.executeAnalysis(module); | |
246 | assertTrue(res); | |
247 | ||
248 | module.setParameter(TestAnalysis.PARAM_TEST, 0); | |
249 | res = TmfTestHelper.executeAnalysis(module); | |
250 | assertFalse(res); | |
251 | } | |
c068a752 | 252 | } |