1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial API and implementation
11 * Mathieu Rail - Added tests for getting a module's requirements
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.analysis
;
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
;
23 import java
.util
.HashMap
;
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
;
51 import com
.google
.common
.collect
.ImmutableSet
;
52 import com
.google
.common
.collect
.Multimap
;
55 * Test suite for the {@link TmfAnalysisModuleHelperConfigElement} class
57 * @author Geneviève Bastien
59 public class AnalysisModuleHelperTest
{
61 private IAnalysisModuleHelper fModule
;
62 private IAnalysisModuleHelper fModuleOther
;
63 private IAnalysisModuleHelper fReqModule
;
64 private ITmfTrace fTrace
;
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();
73 * Gets the module helpers for 2 test modules
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();
90 * Some tests use traces, let's clean them here
93 public void cleanupTraces() {
94 TmfTestTrace
.A_TEST_10K
.dispose();
99 * Test the helper's getters and setters
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());
108 Bundle helperbundle
= fModule
.getBundle();
109 Bundle thisbundle
= Platform
.getBundle("org.eclipse.tracecompass.tmf.core.tests");
110 assertNotNull(helperbundle
);
111 assertEquals(thisbundle
, helperbundle
);
113 /* With other module */
114 assertEquals(AnalysisManagerTest
.MODULE_SECOND
, fModuleOther
.getId());
115 assertEquals("Test other analysis", fModuleOther
.getName());
116 assertTrue(fModuleOther
.isAutomatic());
121 * {@link TmfAnalysisModuleHelperConfigElement#appliesToTraceType(Class)}
122 * method for the 2 modules
125 public void testAppliesToTrace() {
127 assertFalse(fModule
.appliesToTraceType(TmfTrace
.class));
128 assertTrue(fModule
.appliesToTraceType(TmfTraceStub
.class));
129 assertTrue(fModule
.appliesToTraceType(TmfTraceStub2
.class));
130 assertFalse(fModule
.appliesToTraceType(TmfTraceStub3
.class));
133 assertFalse(fModuleOther
.appliesToTraceType(TmfTrace
.class));
134 assertFalse(fModuleOther
.appliesToTraceType(TmfTraceStub
.class));
135 assertTrue(fModuleOther
.appliesToTraceType(TmfTraceStub2
.class));
136 assertTrue(fModuleOther
.appliesToTraceType(TmfTraceStub3
.class));
141 * {@link TmfAnalysisModuleHelperConfigElement#newModule(ITmfTrace)} method
145 public void testNewModule() {
146 /* Test analysis module with traceStub */
147 Exception exception
= null;
148 IAnalysisModule module
= null;
150 module
= fModule
.newModule(TmfTestTrace
.A_TEST_10K
.getTrace());
151 assertNotNull(module
);
152 assertTrue(module
instanceof TestAnalysis
);
153 } catch (TmfAnalysisException e
) {
156 if (module
!= null) {
160 assertNull(exception
);
162 /* TestAnalysis2 module with trace, should return an exception */
164 module
= fModuleOther
.newModule(TmfTestTrace
.A_TEST_10K
.getTrace());
165 } catch (TmfAnalysisException e
) {
168 if (module
!= null) {
172 assertNotNull(exception
);
173 assertEquals(NLS
.bind(Messages
.TmfAnalysisModuleHelper_AnalysisDoesNotApply
, fModuleOther
.getName()), exception
.getMessage());
175 /* TestAnalysis2 module with a TraceStub2 */
177 ITmfTrace trace
= fTrace
;
178 assertNotNull(trace
);
180 module
= fModuleOther
.newModule(trace
);
181 assertNotNull(module
);
182 assertTrue(module
instanceof TestAnalysis2
);
183 } catch (TmfAnalysisException e
) {
186 if (module
!= null) {
190 assertNull(exception
);
194 * Test for the initialization of parameters from the extension points
197 public void testParameters() {
198 ITmfTrace trace
= TmfTestTrace
.A_TEST_10K
.getTrace();
201 * This analysis has a parameter, but no default value. we should be
202 * able to set the parameter
204 IAnalysisModuleHelper helper
= getModuleHelper(AnalysisManagerTest
.MODULE_PARAM
);
205 assertNotNull(helper
);
206 IAnalysisModule module
= null;
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
));
213 } catch (TmfAnalysisException e1
) {
214 fail(e1
.getMessage());
217 if (module
!= null) {
222 /* This module has a parameter with default value */
223 helper
= getModuleHelper(AnalysisManagerTest
.MODULE_PARAM_DEFAULT
);
224 assertNotNull(helper
);
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
));
231 } catch (TmfAnalysisException e1
) {
232 fail(e1
.getMessage());
235 if (module
!= null) {
241 * This module does not have a parameter so setting it should throw an
244 helper
= getModuleHelper(AnalysisManagerTest
.MODULE_SECOND
);
245 assertNotNull(helper
);
246 Exception exception
= null;
248 assertNotNull(trace
);
250 module
= helper
.newModule(trace
);
251 assertNull(module
.getParameter(TestAnalysis
.PARAM_TEST
));
254 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
255 } catch (RuntimeException e
) {
258 } catch (TmfAnalysisException e1
) {
259 fail(e1
.getMessage());
262 if (module
!= null) {
266 assertNotNull(exception
);
271 * {@link TmfAnalysisModuleHelperConfigElement#getValidTraceTypes} method
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
);
282 * {@link TmfAnalysisModuleHelperConfigElement#getAnalysisRequirements}
286 public void testGetRequirements() {
287 Iterable
<TmfAnalysisRequirement
> requirements
= fReqModule
.getAnalysisRequirements();
288 assertNotNull(requirements
);
290 Map
<String
, TmfAnalysisRequirement
> rMap
= new HashMap
<>();
292 for (TmfAnalysisRequirement req
: requirements
) {
293 assertFalse(rMap
.containsKey(req
.getType()));
294 rMap
.put(req
.getType(), req
);
296 assertEquals(2, rMap
.size());
298 /* Test if all types and values have been obtained */
299 TmfAnalysisRequirement req
= rMap
.get(TestRequirementAnalysis
.EVENT_TYPE
);
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
));
308 req
= rMap
.get(TestRequirementAnalysis
.FIELD_TYPE
);
311 values
= req
.getValues();
312 assertEquals(2, values
.size());
313 assertTrue(values
.contains(TestRequirementAnalysis
.PID
));
314 assertTrue(values
.contains(TestRequirementAnalysis
.TID
));