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 | import static org.junit.Assume.assumeTrue; | |
22 | ||
23 | import org.eclipse.core.runtime.Platform; | |
24 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule; | |
25 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper; | |
26 | import org.eclipse.linuxtools.tmf.core.analysis.Messages; | |
27 | import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager; | |
b3b03da0 | 28 | import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisModuleHelperConfigElement; |
c068a752 GB |
29 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace; |
30 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException; | |
31 | import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace; | |
32 | import org.eclipse.linuxtools.tmf.core.tests.shared.TmfTestTrace; | |
33 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
34 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; | |
35 | import org.eclipse.linuxtools.tmf.tests.stubs.analysis.TestAnalysis; | |
36 | import org.eclipse.linuxtools.tmf.tests.stubs.analysis.TestCtfAnalysis; | |
37 | import org.eclipse.linuxtools.tmf.tests.stubs.ctf.CtfTmfTraceStub; | |
38 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; | |
39 | import org.eclipse.osgi.util.NLS; | |
40 | import org.junit.After; | |
41 | import org.junit.Before; | |
42 | import org.junit.Test; | |
43 | import org.osgi.framework.Bundle; | |
44 | ||
45 | /** | |
b3b03da0 | 46 | * Test suite for the {@link TmfAnalysisModuleHelperConfigElement} class |
c068a752 GB |
47 | * |
48 | * @author Geneviève Bastien | |
49 | */ | |
50 | public class AnalysisModuleHelperTest { | |
51 | ||
52 | private IAnalysisModuleHelper fModule; | |
53 | private IAnalysisModuleHelper fCtfModule; | |
54 | ||
55 | /** | |
56 | * Gets the module helpers for 2 test modules | |
57 | */ | |
58 | @Before | |
59 | public void getModules() { | |
60 | fModule = TmfAnalysisManager.getAnalysisModule(AnalysisManagerTest.MODULE_PARAM); | |
61 | assertNotNull(fModule); | |
b3b03da0 | 62 | assertTrue(fModule instanceof TmfAnalysisModuleHelperConfigElement); |
c068a752 GB |
63 | fCtfModule = TmfAnalysisManager.getAnalysisModule(AnalysisManagerTest.MODULE_CTF); |
64 | assertNotNull(fCtfModule); | |
b3b03da0 | 65 | assertTrue(fCtfModule instanceof TmfAnalysisModuleHelperConfigElement); |
c068a752 GB |
66 | } |
67 | ||
68 | /** | |
69 | * Some tests use traces, let's clean them here | |
70 | */ | |
71 | @After | |
72 | public void cleanupTraces() { | |
73 | TmfTestTrace.A_TEST_10K.dispose(); | |
74 | CtfTmfTestTrace.KERNEL.dispose(); | |
75 | } | |
76 | ||
77 | /** | |
78 | * Test the helper's getters and setters | |
79 | */ | |
80 | @Test | |
81 | public void testHelperGetters() { | |
82 | /* With first module */ | |
83 | assertEquals(AnalysisManagerTest.MODULE_PARAM, fModule.getId()); | |
84 | assertEquals("Test analysis", fModule.getName()); | |
85 | assertFalse(fModule.isAutomatic()); | |
86 | ||
87 | Bundle helperbundle = fModule.getBundle(); | |
88 | Bundle thisbundle = Platform.getBundle("org.eclipse.linuxtools.tmf.core.tests"); | |
89 | assertNotNull(helperbundle); | |
90 | assertEquals(thisbundle, helperbundle); | |
91 | ||
92 | /* With ctf module */ | |
93 | assertEquals(AnalysisManagerTest.MODULE_CTF, fCtfModule.getId()); | |
94 | assertEquals("Test analysis ctf", fCtfModule.getName()); | |
95 | assertTrue(fCtfModule.isAutomatic()); | |
96 | } | |
97 | ||
98 | /** | |
b3b03da0 | 99 | * Test the {@link TmfAnalysisModuleHelperConfigElement#appliesToTraceType(Class)} |
c068a752 GB |
100 | * method for the 2 modules |
101 | */ | |
102 | @Test | |
103 | public void testAppliesToTrace() { | |
104 | /* non-ctf module */ | |
105 | assertFalse(fModule.appliesToTraceType(TmfTrace.class)); | |
106 | assertTrue(fModule.appliesToTraceType(TmfTraceStub.class)); | |
107 | assertFalse(fModule.appliesToTraceType(CtfTmfTraceStub.class)); | |
108 | ||
109 | /* ctf module */ | |
110 | assertFalse(fCtfModule.appliesToTraceType(TmfTrace.class)); | |
111 | assertFalse(fCtfModule.appliesToTraceType(TmfTraceStub.class)); | |
112 | assertTrue(fCtfModule.appliesToTraceType(CtfTmfTraceStub.class)); | |
113 | } | |
114 | ||
115 | /** | |
b3b03da0 | 116 | * Test the {@link TmfAnalysisModuleHelperConfigElement#newModule(ITmfTrace)} method |
c068a752 GB |
117 | * for the 2 modules |
118 | */ | |
119 | @Test | |
120 | public void testNewModule() { | |
121 | /* Test analysis module with traceStub */ | |
122 | Exception exception = null; | |
123 | IAnalysisModule module = null; | |
124 | try { | |
125 | module = fModule.newModule(TmfTestTrace.A_TEST_10K.getTrace()); | |
126 | } catch (TmfAnalysisException e) { | |
127 | exception = e; | |
128 | } | |
129 | assertNull(exception); | |
130 | assertNotNull(module); | |
131 | assertTrue(module instanceof TestAnalysis); | |
132 | ||
133 | /* Test Analysis module with ctf trace, should return an exception */ | |
134 | assumeTrue(CtfTmfTestTrace.KERNEL.exists()); | |
135 | CtfTmfTraceStub ctfTrace = (CtfTmfTraceStub) CtfTmfTestTrace.KERNEL.getTrace(); | |
136 | module = null; | |
137 | try { | |
138 | module = fModule.newModule(ctfTrace); | |
139 | } catch (TmfAnalysisException e) { | |
140 | exception = e; | |
141 | } | |
142 | assertNotNull(exception); | |
143 | assertEquals(NLS.bind(Messages.TmfAnalysisModuleHelper_AnalysisDoesNotApply, fModule.getName()), exception.getMessage()); | |
144 | ||
145 | /* Test analysis CTF module with ctf trace stub */ | |
146 | exception = null; | |
147 | module = null; | |
148 | try { | |
149 | module = fCtfModule.newModule(ctfTrace); | |
150 | } catch (TmfAnalysisException e) { | |
151 | exception = e; | |
152 | } | |
153 | assertNull(exception); | |
154 | assertNotNull(module); | |
155 | assertTrue(module instanceof TestCtfAnalysis); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Test for the initialization of parameters from the extension points | |
160 | */ | |
161 | @Test | |
162 | public void testParameters() { | |
163 | assumeTrue(CtfTmfTestTrace.KERNEL.exists()); | |
164 | CtfTmfTrace ctftrace = CtfTmfTestTrace.KERNEL.getTrace(); | |
165 | ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace(); | |
166 | ||
167 | /* | |
168 | * This analysis has a parameter, but no default value. we should be | |
169 | * able to set the parameter | |
170 | */ | |
171 | IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(AnalysisManagerTest.MODULE_PARAM); | |
172 | IAnalysisModule module; | |
173 | try { | |
174 | module = helper.newModule(trace); | |
175 | } catch (TmfAnalysisException e1) { | |
176 | fail(e1.getMessage()); | |
177 | return; | |
178 | } | |
179 | ||
180 | assertNull(module.getParameter(TestAnalysis.PARAM_TEST)); | |
181 | module.setParameter(TestAnalysis.PARAM_TEST, 1); | |
182 | assertEquals(1, module.getParameter(TestAnalysis.PARAM_TEST)); | |
183 | ||
184 | /* This module has a parameter with default value */ | |
185 | helper = TmfAnalysisManager.getAnalysisModule(AnalysisManagerTest.MODULE_PARAM_DEFAULT); | |
186 | try { | |
187 | module = helper.newModule(trace); | |
188 | } catch (TmfAnalysisException e1) { | |
189 | fail(e1.getMessage()); | |
190 | return; | |
191 | } | |
192 | assertEquals(3, module.getParameter(TestAnalysis.PARAM_TEST)); | |
193 | module.setParameter(TestAnalysis.PARAM_TEST, 1); | |
194 | assertEquals(1, module.getParameter(TestAnalysis.PARAM_TEST)); | |
195 | ||
196 | /* | |
197 | * This module does not have a parameter so setting it should throw an | |
198 | * error | |
199 | */ | |
200 | helper = TmfAnalysisManager.getAnalysisModule(AnalysisManagerTest.MODULE_CTF); | |
201 | try { | |
202 | module = helper.newModule(ctftrace); | |
203 | } catch (TmfAnalysisException e1) { | |
204 | fail(e1.getMessage()); | |
205 | return; | |
206 | } | |
207 | assertNull(module.getParameter(TestAnalysis.PARAM_TEST)); | |
208 | Exception exception = null; | |
209 | try { | |
210 | module.setParameter(TestAnalysis.PARAM_TEST, 1); | |
211 | } catch (RuntimeException e) { | |
212 | exception = e; | |
213 | } | |
214 | assertNotNull(exception); | |
215 | } | |
216 | } |