TMF: allow multiple analysis helpers to have the same ID
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / analysis / AnalysisModuleHelperTest.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 * Mathieu Rail - Added tests for getting a module's requirements
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.core.tests.analysis;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertNotNull;
19 import static org.junit.Assert.assertNull;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.eclipse.core.runtime.Platform;
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.IAnalysisModuleHelper;
32 import org.eclipse.tracecompass.tmf.core.analysis.Messages;
33 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
34 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisModuleHelperConfigElement;
35 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisRequirement;
36 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
37 import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace;
38 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
39 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
40 import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestAnalysis;
41 import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestAnalysis2;
42 import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestRequirementAnalysis;
43 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub;
44 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub2;
45 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub3;
46 import org.junit.After;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.osgi.framework.Bundle;
50
51 import com.google.common.collect.ImmutableSet;
52 import com.google.common.collect.Multimap;
53
54 /**
55 * Test suite for the {@link TmfAnalysisModuleHelperConfigElement} class
56 *
57 * @author Geneviève Bastien
58 */
59 public class AnalysisModuleHelperTest {
60
61 private IAnalysisModuleHelper fModule;
62 private IAnalysisModuleHelper fModuleOther;
63 private IAnalysisModuleHelper fReqModule;
64 private ITmfTrace fTrace;
65
66 private static IAnalysisModuleHelper getModuleHelper(@NonNull String moduleId) {
67 Multimap<String, IAnalysisModuleHelper> helpers = TmfAnalysisManager.getAnalysisModules();
68 assertEquals(1, helpers.get(moduleId).size());
69 return helpers.get(moduleId).iterator().next();
70 }
71
72 /**
73 * Gets the module helpers for 2 test modules
74 */
75 @Before
76 public void getModules() {
77 fModule = getModuleHelper(AnalysisManagerTest.MODULE_PARAM);
78 assertNotNull(fModule);
79 assertTrue(fModule instanceof TmfAnalysisModuleHelperConfigElement);
80 fModuleOther = getModuleHelper(AnalysisManagerTest.MODULE_SECOND);
81 assertNotNull(fModuleOther);
82 assertTrue(fModuleOther instanceof TmfAnalysisModuleHelperConfigElement);
83 fReqModule = getModuleHelper(AnalysisManagerTest.MODULE_REQ);
84 assertNotNull(fReqModule);
85 assertTrue(fReqModule instanceof TmfAnalysisModuleHelperConfigElement);
86 fTrace = TmfTestTrace.A_TEST_10K2.getTraceAsStub2();
87 }
88
89 /**
90 * Some tests use traces, let's clean them here
91 */
92 @After
93 public void cleanupTraces() {
94 TmfTestTrace.A_TEST_10K.dispose();
95 fTrace.dispose();
96 }
97
98 /**
99 * Test the helper's getters and setters
100 */
101 @Test
102 public void testHelperGetters() {
103 /* With first module */
104 assertEquals(AnalysisManagerTest.MODULE_PARAM, fModule.getId());
105 assertEquals("Test analysis", fModule.getName());
106 assertFalse(fModule.isAutomatic());
107
108 Bundle helperbundle = fModule.getBundle();
109 Bundle thisbundle = Platform.getBundle("org.eclipse.tracecompass.tmf.core.tests");
110 assertNotNull(helperbundle);
111 assertEquals(thisbundle, helperbundle);
112
113 /* With other module */
114 assertEquals(AnalysisManagerTest.MODULE_SECOND, fModuleOther.getId());
115 assertEquals("Test other analysis", fModuleOther.getName());
116 assertTrue(fModuleOther.isAutomatic());
117 }
118
119 /**
120 * Test the
121 * {@link TmfAnalysisModuleHelperConfigElement#appliesToTraceType(Class)}
122 * method for the 2 modules
123 */
124 @Test
125 public void testAppliesToTrace() {
126 /* stub module */
127 assertFalse(fModule.appliesToTraceType(TmfTrace.class));
128 assertTrue(fModule.appliesToTraceType(TmfTraceStub.class));
129 assertTrue(fModule.appliesToTraceType(TmfTraceStub2.class));
130 assertFalse(fModule.appliesToTraceType(TmfTraceStub3.class));
131
132 /* stub module 2 */
133 assertFalse(fModuleOther.appliesToTraceType(TmfTrace.class));
134 assertFalse(fModuleOther.appliesToTraceType(TmfTraceStub.class));
135 assertTrue(fModuleOther.appliesToTraceType(TmfTraceStub2.class));
136 assertTrue(fModuleOther.appliesToTraceType(TmfTraceStub3.class));
137 }
138
139 /**
140 * Test the
141 * {@link TmfAnalysisModuleHelperConfigElement#newModule(ITmfTrace)} method
142 * for the 2 modules
143 */
144 @Test
145 public void testNewModule() {
146 /* Test analysis module with traceStub */
147 Exception exception = null;
148 IAnalysisModule module = null;
149 try {
150 module = fModule.newModule(TmfTestTrace.A_TEST_10K.getTrace());
151 assertNotNull(module);
152 assertTrue(module instanceof TestAnalysis);
153 } catch (TmfAnalysisException e) {
154 exception = e;
155 } finally {
156 if (module != null) {
157 module.dispose();
158 }
159 }
160 assertNull(exception);
161
162 /* TestAnalysis2 module with trace, should return an exception */
163 try {
164 module = fModuleOther.newModule(TmfTestTrace.A_TEST_10K.getTrace());
165 } catch (TmfAnalysisException e) {
166 exception = e;
167 } finally {
168 if (module != null) {
169 module.dispose();
170 }
171 }
172 assertNotNull(exception);
173 assertEquals(NLS.bind(Messages.TmfAnalysisModuleHelper_AnalysisDoesNotApply, fModuleOther.getName()), exception.getMessage());
174
175 /* TestAnalysis2 module with a TraceStub2 */
176 exception = null;
177 ITmfTrace trace = fTrace;
178 assertNotNull(trace);
179 try {
180 module = fModuleOther.newModule(trace);
181 assertNotNull(module);
182 assertTrue(module instanceof TestAnalysis2);
183 } catch (TmfAnalysisException e) {
184 exception = e;
185 } finally {
186 if (module != null) {
187 module.dispose();
188 }
189 }
190 assertNull(exception);
191 }
192
193 /**
194 * Test for the initialization of parameters from the extension points
195 */
196 @Test
197 public void testParameters() {
198 ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace();
199
200 /*
201 * This analysis has a parameter, but no default value. we should be
202 * able to set the parameter
203 */
204 IAnalysisModuleHelper helper = getModuleHelper(AnalysisManagerTest.MODULE_PARAM);
205 assertNotNull(helper);
206 IAnalysisModule module = null;
207 try {
208 module = helper.newModule(trace);
209 assertNull(module.getParameter(TestAnalysis.PARAM_TEST));
210 module.setParameter(TestAnalysis.PARAM_TEST, 1);
211 assertEquals(1, module.getParameter(TestAnalysis.PARAM_TEST));
212
213 } catch (TmfAnalysisException e1) {
214 fail(e1.getMessage());
215 return;
216 } finally {
217 if (module != null) {
218 module.dispose();
219 }
220 }
221
222 /* This module has a parameter with default value */
223 helper = getModuleHelper(AnalysisManagerTest.MODULE_PARAM_DEFAULT);
224 assertNotNull(helper);
225 try {
226 module = helper.newModule(trace);
227 assertEquals(3, module.getParameter(TestAnalysis.PARAM_TEST));
228 module.setParameter(TestAnalysis.PARAM_TEST, 1);
229 assertEquals(1, module.getParameter(TestAnalysis.PARAM_TEST));
230
231 } catch (TmfAnalysisException e1) {
232 fail(e1.getMessage());
233 return;
234 } finally {
235 if (module != null) {
236 module.dispose();
237 }
238 }
239
240 /*
241 * This module does not have a parameter so setting it should throw an
242 * error
243 */
244 helper = getModuleHelper(AnalysisManagerTest.MODULE_SECOND);
245 assertNotNull(helper);
246 Exception exception = null;
247 trace = fTrace;
248 assertNotNull(trace);
249 try {
250 module = helper.newModule(trace);
251 assertNull(module.getParameter(TestAnalysis.PARAM_TEST));
252
253 try {
254 module.setParameter(TestAnalysis.PARAM_TEST, 1);
255 } catch (RuntimeException e) {
256 exception = e;
257 }
258 } catch (TmfAnalysisException e1) {
259 fail(e1.getMessage());
260 return;
261 } finally {
262 if (module != null) {
263 module.dispose();
264 }
265 }
266 assertNotNull(exception);
267 }
268
269 /**
270 * Test for the
271 * {@link TmfAnalysisModuleHelperConfigElement#getValidTraceTypes} method
272 */
273 @Test
274 public void testGetValidTraceTypes() {
275 Set<Class<? extends ITmfTrace>> expected = ImmutableSet.of((Class<? extends ITmfTrace>) TmfTraceStub.class, TmfTraceStub2.class, TmfTraceStub3.class);
276 Iterable<Class<? extends ITmfTrace>> traceTypes = fReqModule.getValidTraceTypes();
277 assertEquals(expected, traceTypes);
278 }
279
280 /**
281 * Test for the
282 * {@link TmfAnalysisModuleHelperConfigElement#getAnalysisRequirements}
283 * method
284 */
285 @Test
286 public void testGetRequirements() {
287 Iterable<TmfAnalysisRequirement> requirements = fReqModule.getAnalysisRequirements();
288 assertNotNull(requirements);
289
290 Map<String, TmfAnalysisRequirement> rMap = new HashMap<>();
291
292 for (TmfAnalysisRequirement req : requirements) {
293 assertFalse(rMap.containsKey(req.getType()));
294 rMap.put(req.getType(), req);
295 }
296 assertEquals(2, rMap.size());
297
298 /* Test if all types and values have been obtained */
299 TmfAnalysisRequirement req = rMap.get(TestRequirementAnalysis.EVENT_TYPE);
300 assertNotNull(req);
301
302 Set<String> values = req.getValues();
303 assertEquals(3, values.size());
304 assertTrue(values.contains(TestRequirementAnalysis.EXIT_SYSCALL));
305 assertTrue(values.contains(TestRequirementAnalysis.SCHED_SWITCH));
306 assertTrue(values.contains(TestRequirementAnalysis.SCHED_WAKEUP));
307
308 req = rMap.get(TestRequirementAnalysis.FIELD_TYPE);
309 assertNotNull(req);
310
311 values = req.getValues();
312 assertEquals(2, values.size());
313 assertTrue(values.contains(TestRequirementAnalysis.PID));
314 assertTrue(values.contains(TestRequirementAnalysis.TID));
315 }
316 }
This page took 0.041305 seconds and 5 git commands to generate.