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() { | |
7b3eb8c0 | 105 | try (TestAnalysis module = setUpAnalysis();) { |
c068a752 | 106 | |
7b3eb8c0 AM |
107 | IStatus status = module.schedule(); |
108 | assertEquals(IStatus.ERROR, status.getSeverity()); | |
c068a752 | 109 | |
7b3eb8c0 AM |
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 | } | |
c068a752 | 116 | |
7b3eb8c0 AM |
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(); | |
c068a752 | 122 | |
7b3eb8c0 AM |
123 | assertTrue(completed); |
124 | assertEquals(1, module.getAnalysisOutput()); | |
125 | } | |
c068a752 GB |
126 | } |
127 | ||
128 | /** | |
7d6122fc | 129 | * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion} with cancellation |
c068a752 GB |
130 | */ |
131 | @Test | |
132 | public void testWaitForCompletionCancelled() { | |
7b3eb8c0 | 133 | try (TestAnalysis module = setUpAnalysis();) { |
c068a752 | 134 | |
7b3eb8c0 AM |
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 | } | |
c068a752 | 142 | |
7b3eb8c0 AM |
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 | } | |
c068a752 GB |
151 | } |
152 | ||
153 | /** | |
154 | * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method with wrong trace | |
155 | */ | |
156 | @Test | |
157 | public void testSetWrongTrace() { | |
bd64ee73 AM |
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()); | |
c068a752 | 173 | } |
c068a752 GB |
174 | } |
175 | ||
176 | /** | |
177 | * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method | |
178 | */ | |
179 | @Test | |
180 | public void testCancel() { | |
7b3eb8c0 | 181 | try (TestAnalysis module = setUpAnalysis();) { |
c068a752 | 182 | |
7b3eb8c0 AM |
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 | } | |
c068a752 | 189 | |
7b3eb8c0 | 190 | assertEquals(Status.OK_STATUS, module.schedule()); |
c068a752 | 191 | |
7b3eb8c0 AM |
192 | /* Give the job a chance to start */ |
193 | try { | |
194 | Thread.sleep(1000); | |
195 | } catch (InterruptedException e) { | |
196 | fail(e.getMessage()); | |
197 | } | |
c068a752 | 198 | |
7b3eb8c0 AM |
199 | module.cancel(); |
200 | assertFalse(module.waitForCompletion()); | |
201 | assertEquals(-1, module.getAnalysisOutput()); | |
202 | } | |
c068a752 GB |
203 | } |
204 | ||
205 | /** | |
206 | * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)} | |
207 | * method | |
208 | */ | |
209 | @Test | |
210 | public void testParameterChanged() { | |
7b3eb8c0 | 211 | try (TestAnalysis module = setUpAnalysis();) { |
c068a752 | 212 | |
7b3eb8c0 AM |
213 | try { |
214 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); | |
215 | } catch (TmfAnalysisException e) { | |
216 | fail(e.getMessage()); | |
217 | } | |
c068a752 | 218 | |
7b3eb8c0 AM |
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); | |
c068a752 | 227 | |
7b3eb8c0 AM |
228 | /* |
229 | * Cannot test anymore of this method, need a parameter provider to | |
230 | * do this | |
231 | */ | |
232 | } | |
c068a752 | 233 | } |
0e3d9d4a GB |
234 | |
235 | /** | |
236 | * Test the {@link TmfTestHelper#executeAnalysis(IAnalysisModule)} method | |
237 | */ | |
238 | @Test | |
239 | public void testHelper() { | |
7b3eb8c0 | 240 | try (TestAnalysis module = setUpAnalysis();) { |
0e3d9d4a | 241 | |
7b3eb8c0 AM |
242 | try { |
243 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); | |
244 | } catch (TmfAnalysisException e) { | |
245 | fail(e.getMessage()); | |
246 | } | |
0e3d9d4a | 247 | |
7b3eb8c0 AM |
248 | module.setParameter(TestAnalysis.PARAM_TEST, 1); |
249 | boolean res = TmfTestHelper.executeAnalysis(module); | |
250 | assertTrue(res); | |
0e3d9d4a | 251 | |
7b3eb8c0 AM |
252 | module.setParameter(TestAnalysis.PARAM_TEST, 0); |
253 | res = TmfTestHelper.executeAnalysis(module); | |
254 | assertFalse(res); | |
255 | } | |
0e3d9d4a | 256 | } |
c068a752 | 257 | } |