1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
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 * Matthew Khouzam - Initial generation with CodePro tools
11 * Alexandre Montplaisir - Clean up, consolidate redundant tests
12 * Patrick Tasse - Fix location ratio
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.trace
;
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
;
23 import java
.util
.Arrays
;
24 import java
.util
.HashSet
;
27 import org
.eclipse
.core
.resources
.IProject
;
28 import org
.eclipse
.core
.resources
.IResource
;
29 import org
.eclipse
.core
.runtime
.IStatus
;
30 import org
.eclipse
.jdt
.annotation
.NonNull
;
31 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
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
.CtfTmfTestTraceUtils
;
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
;
51 * The class <code>CtfTmfTraceTest</code> contains tests for the class
52 * <code>{@link CtfTmfTrace}</code>.
57 public class CtfTmfTraceTest
{
59 private static final @NonNull CtfTestTrace testTrace
= CtfTestTrace
.KERNEL
;
61 private CtfTmfTrace fixture
;
64 * Perform pre-test initialization.
68 fixture
= CtfTmfTestTraceUtils
.getTrace(testTrace
);
72 * Perform post-test clean-up.
75 public void tearDown() {
76 if (fixture
!= null) {
82 * Run the CtfTmfTrace() constructor test.
85 public void testCtfTmfTrace() {
86 CtfTmfTrace result
= new CtfTmfTrace();
88 assertNotNull(result
);
89 assertEquals(1000, result
.getCacheSize());
90 assertEquals(0L, result
.getNbEvents());
91 assertEquals(0L, result
.getStreamingInterval());
92 assertNull(result
.getResource());
93 assertNull(result
.getEventType());
99 * Test the parseEvent() method
102 public void testParseEvent() {
103 ITmfContext ctx
= fixture
.seekEvent(0);
104 fixture
.getNext(ctx
);
105 CtfTmfEvent event
= fixture
.parseEvent(ctx
);
106 assertNotNull(event
);
111 * Run the void broadcast(TmfSignal) method test.
114 public void testBroadcast() {
115 TmfSignal signal
= new TmfEndSynchSignal(1);
116 fixture
.broadcast(signal
);
120 * Run the void dispose() method test.
123 public void testDispose() {
124 CtfTmfTrace emptyFixture
= new CtfTmfTrace();
125 emptyFixture
.dispose();
129 * Run the int getCacheSize() method test.
132 public void testGetCacheSize() {
133 CtfTmfTrace emptyFixture
= new CtfTmfTrace();
134 int result
= emptyFixture
.getCacheSize();
135 assertEquals(1000, result
);
137 emptyFixture
.dispose();
141 * Run the ITmfLocation<Comparable> getCurrentLocation() method test.
144 public void testGetCurrentLocation() {
145 CtfLocation result
= (CtfLocation
) fixture
.getCurrentLocation();
150 * Test the seekEvent() method with a null location.
153 public void testSeekEventLoc_null() {
154 CtfLocation loc
= null;
155 fixture
.seekEvent(loc
);
156 assertNotNull(fixture
);
160 * Test the seekEvent() method with a location from a timestamp.
163 public void testSeekEventLoc_timetamp() {
164 CtfLocation loc
= new CtfLocation(new TmfNanoTimestamp(0L));
165 fixture
.seekEvent(loc
);
166 assertNotNull(fixture
);
170 * Run the ITmfTimestamp getEndTime() method test.
173 public void testGetEndTime() {
174 ITmfTimestamp result
= fixture
.getEndTime();
175 assertNotNull(result
);
179 * Run the String getEnvironment method test.
182 public void testGetEnvValue() {
183 String key
= "tracer_name";
184 String result
= fixture
.getProperties().get(key
);
185 assertEquals("\"lttng-modules\"", result
);
189 * Test the {@link CtfTmfTrace#getEventType()} method.
192 public void testGetEventType() {
193 Class
<?
> result
= fixture
.getEventType();
194 assertNotNull(result
);
195 assertEquals(CtfTmfEvent
.class, result
);
199 * Run the Class<CtfTmfEvent> getContainedEventTypes() method test.
202 public void testGetContainedEventTypes() {
203 Set
<?
extends ITmfEventType
> result
= fixture
.getContainedEventTypes();
204 assertNotNull(result
);
205 assertFalse(result
.isEmpty());
209 * Run the double getLocationRatio(ITmfLocation<?>) method test.
212 public void testGetLocationRatio() {
213 ITmfContext context
= fixture
.seekEvent(0);
214 long t1
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp();
215 fixture
.getNext(context
);
216 long t2
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp();
217 fixture
.getNext(context
);
218 long t3
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp();
219 fixture
.getNext(context
);
221 double ratio1
= fixture
.getLocationRatio(new CtfLocation(t1
, 0));
222 assertEquals(0.0, ratio1
, 0.01);
223 double ratio2
= fixture
.getLocationRatio(new CtfLocation(t2
, 0));
224 assertEquals((double) (t2
- t1
) / (t3
- t1
), ratio2
, 0.01);
225 double ratio3
= fixture
.getLocationRatio(new CtfLocation(t3
, 0));
226 assertEquals(1.0, ratio3
, 0.01);
230 * Run the String getName() method test.
233 public void testGetName() {
234 String result
= fixture
.getName();
235 assertNotNull(result
);
239 * Run the getTraceProperties() method test.
242 public void testGetTraceProperties() {
243 int result
= fixture
.getProperties().size();
244 assertEquals(10, result
);
245 assertEquals(String
.valueOf(1332166405241713987L), fixture
.getProperties().get("clock_offset"));
249 * Run the long getNbEvents() method test.
252 public void testGetNbEvents() {
253 long result
= fixture
.getNbEvents();
254 assertEquals(1L, result
);
258 * Run the CtfTmfEvent getNext(ITmfContext) method test.
261 public void testGetNext() {
262 ITmfContext context
= fixture
.seekEvent(0);
263 CtfTmfEvent result
= fixture
.getNext(context
);
264 assertNotNull(result
);
269 * Run the String getPath() method test.
272 public void testGetPath() {
273 String result
= fixture
.getPath();
274 assertNotNull(result
);
278 * Run the IResource getResource() method test.
281 public void testGetResource() {
282 IResource result
= fixture
.getResource();
287 * Run the ITmfTimestamp getStartTime() method test.
290 public void testGetStartTime() {
291 ITmfTimestamp result
= fixture
.getStartTime();
292 assertNotNull(result
);
296 * Run the long getStreamingInterval() method test.
299 public void testGetStreamingInterval() {
300 long result
= fixture
.getStreamingInterval();
301 assertEquals(0L, result
);
305 * Run the TmfTimeRange getTimeRange() method test.
308 public void testGetTimeRange() {
309 TmfTimeRange result
= fixture
.getTimeRange();
310 assertNotNull(result
);
314 * Run the CtfTmfEvent readNextEvent(ITmfContext) method test.
317 public void testReadNextEvent() {
318 ITmfContext context
= fixture
.seekEvent(0);
319 CtfTmfEvent result
= fixture
.getNext(context
);
320 assertNotNull(result
);
325 * Run the ITmfContext seekEvent(double) method test.
328 public void testSeekEvent_ratio() {
329 ITmfContext context
= fixture
.seekEvent(0);
330 long t1
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp();
331 fixture
.getNext(context
);
332 long t2
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp();
333 fixture
.getNext(context
);
334 long t3
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp();
335 fixture
.getNext(context
);
337 context
= fixture
.seekEvent(0.0);
338 assertEquals(t1
, ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp());
340 context
= fixture
.seekEvent(0.5);
341 assertEquals(t2
, ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp());
343 context
= fixture
.seekEvent(1.0);
344 assertEquals(t3
, ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp());
349 * Run the ITmfContext seekEvent(long) method test.
352 public void testSeekEvent_rank() {
354 ITmfContext result
= fixture
.seekEvent(rank
);
355 assertNotNull(result
);
360 * Run the ITmfContext seekEvent(ITmfTimestamp) method test.
363 public void testSeekEvent_timestamp() {
364 ITmfTimestamp timestamp
= new TmfTimestamp();
365 ITmfContext result
= fixture
.seekEvent(timestamp
);
366 assertNotNull(result
);
371 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test.
374 public void testSeekEvent_location() {
375 final CtfLocationInfo location2
= new CtfLocationInfo(1L, 0L);
376 CtfLocation ctfLocation
= new CtfLocation(location2
);
377 ITmfContext result
= fixture
.seekEvent(ctfLocation
);
378 assertNotNull(result
);
383 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test with invalid location.
386 public void testSeekEventInvalidLocation() {
387 CtfLocation ctfLocation
= new CtfLocation(CtfLocation
.INVALID_LOCATION
);
388 ITmfContext result
= fixture
.seekEvent(ctfLocation
);
389 assertNull(fixture
.getNext(result
));
390 assertEquals(CtfLocation
.INVALID_LOCATION
, result
.getLocation().getLocationInfo());
393 // Not using CtfLocation.INVALID_LOCATION directly on purpose, to make sure CtfLocationInfo.equals is properly used
394 CtfLocationInfo invalidLocation
= new CtfLocationInfo(CtfLocation
.INVALID_LOCATION
.getTimestamp(), CtfLocation
.INVALID_LOCATION
.getIndex());
395 ctfLocation
= new CtfLocation(invalidLocation
);
396 result
= fixture
.seekEvent(ctfLocation
);
397 assertNull(fixture
.getNext(result
));
398 assertEquals(CtfLocation
.INVALID_LOCATION
, result
.getLocation().getLocationInfo());
403 * Run the boolean validate(IProject,String) method test.
406 public void testValidate() {
407 IProject project
= null;
408 IStatus result
= fixture
.validate(project
, fixture
.getPath());
409 assertTrue(result
.isOK());
413 * Run the boolean hasEvent(final String) method test
416 public void testEventLookup() {
417 Set
<@NonNull ?
extends ITmfEventType
> eventTypes
= fixture
.getContainedEventTypes();
418 Set
<String
> eventNames
= TmfEventTypeCollectionHelper
.getEventNames(eventTypes
);
419 assertTrue(eventNames
.contains("sched_switch"));
420 assertFalse(eventNames
.contains("Sched_switch"));
421 String
[] events
= { "sched_switch", "sched_wakeup", "timer_init" };
422 assertTrue(eventNames
.containsAll(Arrays
.asList(events
)));
423 Set
<String
> copy
= new HashSet
<>(eventNames
);
424 copy
.retainAll(Arrays
.asList(events
));
425 assertFalse(copy
.isEmpty());
426 String
[] names
= { "inexistent", "sched_switch", "SomeThing" };
427 copy
= new HashSet
<>(eventNames
);
428 copy
.retainAll(Arrays
.asList(names
));
429 assertTrue(!copy
.isEmpty());
430 assertFalse(eventNames
.containsAll(Arrays
.asList(names
)));
434 * Run the String getHostId() method test
437 public void testCtfHostId() {
438 String a
= fixture
.getHostId();
439 assertEquals("\"84db105b-b3f4-4821-b662-efc51455106a\"", a
);