tmf: Merge TmfEventProvider#getType() and ITmfTrace#getEventType()
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / trace / CtfTmfTraceTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
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 * Matthew Khouzam - Initial generation with CodePro tools
11 * Alexandre Montplaisir - Clean up, consolidate redundant tests
12 * Patrick Tasse - Fix location ratio
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.ctf.core.tests.trace;
16
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assume.assumeTrue;
23
24 import java.util.Arrays;
25 import java.util.HashSet;
26 import java.util.Set;
27
28 import org.eclipse.core.resources.IProject;
29 import org.eclipse.core.resources.IResource;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.tracecompass.tmf.core.event.ITmfEventType;
32 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
33 import org.eclipse.tracecompass.tmf.core.signal.TmfEndSynchSignal;
34 import org.eclipse.tracecompass.tmf.core.signal.TmfSignal;
35 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
36 import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
37 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
38 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
39 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
40 import org.eclipse.tracecompass.tmf.core.trace.TmfEventTypeCollectionHelper;
41 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocation;
42 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocationInfo;
43 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
44 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
45 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
46 import org.junit.After;
47 import org.junit.Before;
48 import org.junit.Test;
49
50 /**
51 * The class <code>CtfTmfTraceTest</code> contains tests for the class
52 * <code>{@link CtfTmfTrace}</code>.
53 *
54 * @author ematkho
55 * @version 1.0
56 */
57 public class CtfTmfTraceTest {
58
59 private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL;
60
61 private CtfTmfTrace fixture;
62
63 /**
64 * Perform pre-test initialization.
65 *
66 * @throws TmfTraceException
67 * If the test trace is not found
68 */
69 @Before
70 public void setUp() throws TmfTraceException {
71 assumeTrue(testTrace.exists());
72 fixture = new CtfTmfTrace();
73 fixture.initTrace((IResource) null, testTrace.getPath(), CtfTmfEvent.class);
74 }
75
76 /**
77 * Perform post-test clean-up.
78 */
79 @After
80 public void tearDown() {
81 if (fixture != null) {
82 fixture.dispose();
83 }
84 }
85
86 /**
87 * Run the CtfTmfTrace() constructor test.
88 */
89 @Test
90 public void testCtfTmfTrace() {
91 CtfTmfTrace result = new CtfTmfTrace();
92
93 assertNotNull(result);
94 assertEquals(1000, result.getCacheSize());
95 assertEquals(0L, result.getNbEvents());
96 assertEquals(0L, result.getStreamingInterval());
97 assertNull(result.getResource());
98 assertNull(result.getEventType());
99
100 result.dispose();
101 }
102
103 /**
104 * Test the parseEvent() method
105 */
106 @Test
107 public void testParseEvent() {
108 ITmfContext ctx = fixture.seekEvent(0);
109 fixture.getNext(ctx);
110 CtfTmfEvent event = fixture.parseEvent(ctx);
111 assertNotNull(event);
112 ctx.dispose();
113 }
114
115 /**
116 * Run the void broadcast(TmfSignal) method test.
117 */
118 @Test
119 public void testBroadcast() {
120 TmfSignal signal = new TmfEndSynchSignal(1);
121 fixture.broadcast(signal);
122 }
123
124 /**
125 * Run the void dispose() method test.
126 */
127 @Test
128 public void testDispose() {
129 CtfTmfTrace emptyFixture = new CtfTmfTrace();
130 emptyFixture.dispose();
131 }
132
133 /**
134 * Run the int getCacheSize() method test.
135 */
136 @Test
137 public void testGetCacheSize() {
138 CtfTmfTrace emptyFixture = new CtfTmfTrace();
139 int result = emptyFixture.getCacheSize();
140 assertEquals(1000, result);
141
142 emptyFixture.dispose();
143 }
144
145 /**
146 * Run the ITmfLocation<Comparable> getCurrentLocation() method test.
147 */
148 @Test
149 public void testGetCurrentLocation() {
150 CtfLocation result = (CtfLocation) fixture.getCurrentLocation();
151 assertNull(result);
152 }
153
154 /**
155 * Test the seekEvent() method with a null location.
156 */
157 @Test
158 public void testSeekEventLoc_null() {
159 CtfLocation loc = null;
160 fixture.seekEvent(loc);
161 assertNotNull(fixture);
162 }
163
164 /**
165 * Test the seekEvent() method with a location from a timestamp.
166 */
167 @Test
168 public void testSeekEventLoc_timetamp() {
169 CtfLocation loc = new CtfLocation(new TmfNanoTimestamp(0L));
170 fixture.seekEvent(loc);
171 assertNotNull(fixture);
172 }
173
174 /**
175 * Run the ITmfTimestamp getEndTime() method test.
176 */
177 @Test
178 public void testGetEndTime() {
179 ITmfTimestamp result = fixture.getEndTime();
180 assertNotNull(result);
181 }
182
183 /**
184 * Run the String getEnvironment method test.
185 */
186 @Test
187 public void testGetEnvValue() {
188 String key = "tracer_name";
189 String result = fixture.getTraceProperties().get(key);
190 assertEquals("\"lttng-modules\"", result);
191 }
192
193 /**
194 * Test the {@link CtfTmfTrace#getEventType()} method.
195 */
196 @Test
197 public void testGetEventType() {
198 Class<?> result = fixture.getEventType();
199 assertNotNull(result);
200 assertEquals(CtfTmfEvent.class, result);
201 }
202
203 /**
204 * Run the Class<CtfTmfEvent> getContainedEventTypes() method test.
205 */
206 @Test
207 public void testGetContainedEventTypes() {
208 Set<? extends ITmfEventType> result = fixture.getContainedEventTypes();
209 assertNotNull(result);
210 assertFalse(result.isEmpty());
211 }
212
213 /**
214 * Run the double getLocationRatio(ITmfLocation<?>) method test.
215 */
216 @Test
217 public void testGetLocationRatio() {
218 ITmfContext context = fixture.seekEvent(0);
219 long t1 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
220 fixture.getNext(context);
221 long t2 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
222 fixture.getNext(context);
223 long t3 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
224 fixture.getNext(context);
225 context.dispose();
226 double ratio1 = fixture.getLocationRatio(new CtfLocation(t1, 0));
227 assertEquals(0.0, ratio1, 0.01);
228 double ratio2 = fixture.getLocationRatio(new CtfLocation(t2, 0));
229 assertEquals((double) (t2 - t1) / (t3 - t1), ratio2, 0.01);
230 double ratio3 = fixture.getLocationRatio(new CtfLocation(t3, 0));
231 assertEquals(1.0, ratio3, 0.01);
232 }
233
234 /**
235 * Run the String getName() method test.
236 */
237 @Test
238 public void testGetName() {
239 String result = fixture.getName();
240 assertNotNull(result);
241 }
242
243 /**
244 * Run the getTraceProperties() method test.
245 */
246 @Test
247 public void testGetTraceProperties() {
248 int result = fixture.getTraceProperties().size();
249 assertEquals(9, result);
250 }
251
252 /**
253 * Run the long getNbEvents() method test.
254 */
255 @Test
256 public void testGetNbEvents() {
257 long result = fixture.getNbEvents();
258 assertEquals(1L, result);
259 }
260
261 /**
262 * Run the CtfTmfEvent getNext(ITmfContext) method test.
263 */
264 @Test
265 public void testGetNext() {
266 ITmfContext context = fixture.seekEvent(0);
267 CtfTmfEvent result = fixture.getNext(context);
268 assertNotNull(result);
269 context.dispose();
270 }
271
272 /**
273 * Run the String getPath() method test.
274 */
275 @Test
276 public void testGetPath() {
277 String result = fixture.getPath();
278 assertNotNull(result);
279 }
280
281 /**
282 * Run the IResource getResource() method test.
283 */
284 @Test
285 public void testGetResource() {
286 IResource result = fixture.getResource();
287 assertNull(result);
288 }
289
290 /**
291 * Run the ITmfTimestamp getStartTime() method test.
292 */
293 @Test
294 public void testGetStartTime() {
295 ITmfTimestamp result = fixture.getStartTime();
296 assertNotNull(result);
297 }
298
299 /**
300 * Run the long getStreamingInterval() method test.
301 */
302 @Test
303 public void testGetStreamingInterval() {
304 long result = fixture.getStreamingInterval();
305 assertEquals(0L, result);
306 }
307
308 /**
309 * Run the TmfTimeRange getTimeRange() method test.
310 */
311 @Test
312 public void testGetTimeRange() {
313 TmfTimeRange result = fixture.getTimeRange();
314 assertNotNull(result);
315 }
316
317 /**
318 * Run the CtfTmfEvent readNextEvent(ITmfContext) method test.
319 */
320 @Test
321 public void testReadNextEvent() {
322 ITmfContext context = fixture.seekEvent(0);
323 CtfTmfEvent result = fixture.getNext(context);
324 assertNotNull(result);
325 context.dispose();
326 }
327
328 /**
329 * Run the ITmfContext seekEvent(double) method test.
330 */
331 @Test
332 public void testSeekEvent_ratio() {
333 ITmfContext context = fixture.seekEvent(0);
334 long t1 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
335 fixture.getNext(context);
336 long t2 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
337 fixture.getNext(context);
338 long t3 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
339 fixture.getNext(context);
340 context.dispose();
341 context = fixture.seekEvent(0.0);
342 assertEquals(t1, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
343 context.dispose();
344 context = fixture.seekEvent(0.5);
345 assertEquals(t2, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
346 context.dispose();
347 context = fixture.seekEvent(1.0);
348 assertEquals(t3, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
349 context.dispose();
350 }
351
352 /**
353 * Run the ITmfContext seekEvent(long) method test.
354 */
355 @Test
356 public void testSeekEvent_rank() {
357 long rank = 1L;
358 ITmfContext result = fixture.seekEvent(rank);
359 assertNotNull(result);
360 result.dispose();
361 }
362
363 /**
364 * Run the ITmfContext seekEvent(ITmfTimestamp) method test.
365 */
366 @Test
367 public void testSeekEvent_timestamp() {
368 ITmfTimestamp timestamp = new TmfTimestamp();
369 ITmfContext result = fixture.seekEvent(timestamp);
370 assertNotNull(result);
371 result.dispose();
372 }
373
374 /**
375 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test.
376 */
377 @Test
378 public void testSeekEvent_location() {
379 final CtfLocationInfo location2 = new CtfLocationInfo(1L, 0L);
380 CtfLocation ctfLocation = new CtfLocation(location2);
381 ITmfContext result = fixture.seekEvent(ctfLocation);
382 assertNotNull(result);
383 result.dispose();
384 }
385
386 /**
387 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test with invalid location.
388 */
389 @Test
390 public void testSeekEventInvalidLocation() {
391 CtfLocation ctfLocation = new CtfLocation(CtfLocation.INVALID_LOCATION);
392 ITmfContext result = fixture.seekEvent(ctfLocation);
393 assertNull(fixture.getNext(result));
394 assertEquals(CtfLocation.INVALID_LOCATION, result.getLocation().getLocationInfo());
395 result.dispose();
396
397 // Not using CtfLocation.INVALID_LOCATION directly on purpose, to make sure CtfLocationInfo.equals is properly used
398 CtfLocationInfo invalidLocation = new CtfLocationInfo(CtfLocation.INVALID_LOCATION.getTimestamp(), CtfLocation.INVALID_LOCATION.getIndex());
399 ctfLocation = new CtfLocation(invalidLocation);
400 result = fixture.seekEvent(ctfLocation);
401 assertNull(fixture.getNext(result));
402 assertEquals(CtfLocation.INVALID_LOCATION, result.getLocation().getLocationInfo());
403 result.dispose();
404 }
405
406 /**
407 * Run the boolean validate(IProject,String) method test.
408 */
409 @Test
410 public void testValidate() {
411 IProject project = null;
412 IStatus result = fixture.validate(project, testTrace.getPath());
413 assertTrue(result.isOK());
414 }
415
416 /**
417 * Run the boolean hasEvent(final String) method test
418 */
419 @Test
420 public void testEventLookup() {
421 Set<? extends ITmfEventType> eventTypes = fixture.getContainedEventTypes();
422 Set<String> eventNames = TmfEventTypeCollectionHelper.getEventNames(eventTypes);
423 assertTrue(eventNames.contains("sched_switch"));
424 assertFalse(eventNames.contains("Sched_switch"));
425 String[] events = { "sched_switch", "sched_wakeup", "timer_init" };
426 assertTrue(eventNames.containsAll(Arrays.asList(events)));
427 Set<String> copy = new HashSet<>(eventNames);
428 copy.retainAll(Arrays.asList(events));
429 assertFalse(copy.isEmpty());
430 String[] names = { "inexistent", "sched_switch", "SomeThing" };
431 copy = new HashSet<>(eventNames);
432 copy.retainAll(Arrays.asList(names));
433 assertTrue(!copy.isEmpty());
434 assertFalse(eventNames.containsAll(Arrays.asList(names)));
435 }
436
437 /**
438 * Run the String getHostId() method test
439 */
440 @Test
441 public void testCtfHostId() {
442 String a = fixture.getHostId();
443 assertEquals("\"84db105b-b3f4-4821-b662-efc51455106a\"", a);
444 }
445
446 }
This page took 0.063408 seconds and 5 git commands to generate.