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
.linuxtools
.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
.linuxtools
.tmf
.core
.analysis
.IAnalysisModule
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.Messages
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.TmfAnalysisManager
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.TmfAnalysisModuleHelperConfigElement
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.TmfAnalysisRequirement
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfAnalysisException
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
.TmfTestTrace
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
38 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.analysis
.TestAnalysis
;
39 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.analysis
.TestAnalysis2
;
40 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.analysis
.TestRequirementAnalysis
;
41 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
42 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub2
;
43 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub3
;
44 import org
.eclipse
.osgi
.util
.NLS
;
45 import org
.junit
.After
;
46 import org
.junit
.Before
;
47 import org
.junit
.Test
;
48 import org
.osgi
.framework
.Bundle
;
50 import com
.google
.common
.collect
.ImmutableSet
;
53 * Test suite for the {@link TmfAnalysisModuleHelperConfigElement} class
55 * @author Geneviève Bastien
57 public class AnalysisModuleHelperTest
{
59 private IAnalysisModuleHelper fModule
;
60 private IAnalysisModuleHelper fModuleOther
;
61 private IAnalysisModuleHelper fReqModule
;
62 private ITmfTrace fTrace
;
65 * Gets the module helpers for 2 test modules
68 public void getModules() {
69 fModule
= TmfAnalysisManager
.getAnalysisModule(AnalysisManagerTest
.MODULE_PARAM
);
70 assertNotNull(fModule
);
71 assertTrue(fModule
instanceof TmfAnalysisModuleHelperConfigElement
);
72 fModuleOther
= TmfAnalysisManager
.getAnalysisModule(AnalysisManagerTest
.MODULE_SECOND
);
73 assertNotNull(fModuleOther
);
74 assertTrue(fModuleOther
instanceof TmfAnalysisModuleHelperConfigElement
);
75 fReqModule
= TmfAnalysisManager
.getAnalysisModule(AnalysisManagerTest
.MODULE_REQ
);
76 assertNotNull(fReqModule
);
77 assertTrue(fReqModule
instanceof TmfAnalysisModuleHelperConfigElement
);
78 fTrace
= TmfTestTrace
.A_TEST_10K2
.getTraceAsStub2();
82 * Some tests use traces, let's clean them here
85 public void cleanupTraces() {
86 TmfTestTrace
.A_TEST_10K
.dispose();
91 * Test the helper's getters and setters
94 public void testHelperGetters() {
95 /* With first module */
96 assertEquals(AnalysisManagerTest
.MODULE_PARAM
, fModule
.getId());
97 assertEquals("Test analysis", fModule
.getName());
98 assertFalse(fModule
.isAutomatic());
100 Bundle helperbundle
= fModule
.getBundle();
101 Bundle thisbundle
= Platform
.getBundle("org.eclipse.tracecompass.tmf.core.tests");
102 assertNotNull(helperbundle
);
103 assertEquals(thisbundle
, helperbundle
);
105 /* With other module */
106 assertEquals(AnalysisManagerTest
.MODULE_SECOND
, fModuleOther
.getId());
107 assertEquals("Test other analysis", fModuleOther
.getName());
108 assertTrue(fModuleOther
.isAutomatic());
113 * {@link TmfAnalysisModuleHelperConfigElement#appliesToTraceType(Class)}
114 * method for the 2 modules
117 public void testAppliesToTrace() {
119 assertFalse(fModule
.appliesToTraceType(TmfTrace
.class));
120 assertTrue(fModule
.appliesToTraceType(TmfTraceStub
.class));
121 assertTrue(fModule
.appliesToTraceType(TmfTraceStub2
.class));
122 assertFalse(fModule
.appliesToTraceType(TmfTraceStub3
.class));
125 assertFalse(fModuleOther
.appliesToTraceType(TmfTrace
.class));
126 assertFalse(fModuleOther
.appliesToTraceType(TmfTraceStub
.class));
127 assertTrue(fModuleOther
.appliesToTraceType(TmfTraceStub2
.class));
128 assertTrue(fModuleOther
.appliesToTraceType(TmfTraceStub3
.class));
133 * {@link TmfAnalysisModuleHelperConfigElement#newModule(ITmfTrace)} method
137 public void testNewModule() {
138 /* Test analysis module with traceStub */
139 Exception exception
= null;
140 try (IAnalysisModule module
= fModule
.newModule(TmfTestTrace
.A_TEST_10K
.getTrace());) {
141 assertNotNull(module
);
142 assertTrue(module
instanceof TestAnalysis
);
143 } catch (TmfAnalysisException e
) {
146 assertNull(exception
);
148 /* TestAnalysis2 module with trace, should return an exception */
149 try (IAnalysisModule module
= fModuleOther
.newModule(TmfTestTrace
.A_TEST_10K
.getTrace());) {
150 } catch (TmfAnalysisException e
) {
153 assertNotNull(exception
);
154 assertEquals(NLS
.bind(Messages
.TmfAnalysisModuleHelper_AnalysisDoesNotApply
, fModuleOther
.getName()), exception
.getMessage());
156 /* TestAnalysis2 module with a TraceStub2 */
158 try (IAnalysisModule module
= fModuleOther
.newModule(fTrace
);) {
159 assertNotNull(module
);
160 assertTrue(module
instanceof TestAnalysis2
);
161 } catch (TmfAnalysisException e
) {
164 assertNull(exception
);
168 * Test for the initialization of parameters from the extension points
171 public void testParameters() {
172 ITmfTrace trace
= TmfTestTrace
.A_TEST_10K
.getTrace();
175 * This analysis has a parameter, but no default value. we should be
176 * able to set the parameter
178 IAnalysisModuleHelper helper
= TmfAnalysisManager
.getAnalysisModule(AnalysisManagerTest
.MODULE_PARAM
);
179 try (IAnalysisModule module
= helper
.newModule(trace
);) {
180 assertNull(module
.getParameter(TestAnalysis
.PARAM_TEST
));
181 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
182 assertEquals(1, module
.getParameter(TestAnalysis
.PARAM_TEST
));
184 } catch (TmfAnalysisException e1
) {
185 fail(e1
.getMessage());
189 /* This module has a parameter with default value */
190 helper
= TmfAnalysisManager
.getAnalysisModule(AnalysisManagerTest
.MODULE_PARAM_DEFAULT
);
191 try (IAnalysisModule module
= helper
.newModule(trace
);) {
192 assertEquals(3, module
.getParameter(TestAnalysis
.PARAM_TEST
));
193 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
194 assertEquals(1, module
.getParameter(TestAnalysis
.PARAM_TEST
));
196 } catch (TmfAnalysisException e1
) {
197 fail(e1
.getMessage());
202 * This module does not have a parameter so setting it should throw an
205 helper
= TmfAnalysisManager
.getAnalysisModule(AnalysisManagerTest
.MODULE_SECOND
);
206 Exception exception
= null;
207 try (IAnalysisModule module
= helper
.newModule(fTrace
);) {
209 assertNull(module
.getParameter(TestAnalysis
.PARAM_TEST
));
212 module
.setParameter(TestAnalysis
.PARAM_TEST
, 1);
213 } catch (RuntimeException e
) {
216 } catch (TmfAnalysisException e1
) {
217 fail(e1
.getMessage());
220 assertNotNull(exception
);
225 * {@link TmfAnalysisModuleHelperConfigElement#getValidTraceTypes} method
228 public void testGetValidTraceTypes() {
229 Set
<Class
<?
extends ITmfTrace
>> expected
= ImmutableSet
.of((Class
<?
extends ITmfTrace
>) TmfTraceStub
.class, TmfTraceStub2
.class, TmfTraceStub3
.class);
230 Iterable
<Class
<?
extends ITmfTrace
>> traceTypes
= fReqModule
.getValidTraceTypes();
231 assertEquals(expected
, traceTypes
);
236 * {@link TmfAnalysisModuleHelperConfigElement#getAnalysisRequirements}
240 public void testGetRequirements() {
241 Iterable
<TmfAnalysisRequirement
> requirements
= fReqModule
.getAnalysisRequirements();
242 assertNotNull(requirements
);
244 Map
<String
, TmfAnalysisRequirement
> rMap
= new HashMap
<>();
246 for (TmfAnalysisRequirement req
: requirements
) {
247 assertFalse(rMap
.containsKey(req
.getType()));
248 rMap
.put(req
.getType(), req
);
250 assertEquals(2, rMap
.size());
252 /* Test if all types and values have been obtained */
253 TmfAnalysisRequirement req
= rMap
.get(TestRequirementAnalysis
.EVENT_TYPE
);
256 Set
<String
> values
= req
.getValues();
257 assertEquals(3, values
.size());
258 assertTrue(values
.contains(TestRequirementAnalysis
.EXIT_SYSCALL
));
259 assertTrue(values
.contains(TestRequirementAnalysis
.SCHED_SWITCH
));
260 assertTrue(values
.contains(TestRequirementAnalysis
.SCHED_WAKEUP
));
262 req
= rMap
.get(TestRequirementAnalysis
.FIELD_TYPE
);
265 values
= req
.getValues();
266 assertEquals(2, values
.size());
267 assertTrue(values
.contains(TestRequirementAnalysis
.PID
));
268 assertTrue(values
.contains(TestRequirementAnalysis
.TID
));