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
.getTraceProperties().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
.getTraceProperties().size();
244 assertEquals(9, result
);
248 * Run the long getNbEvents() method test.
251 public void testGetNbEvents() {
252 long result
= fixture
.getNbEvents();
253 assertEquals(1L, result
);
257 * Run the CtfTmfEvent getNext(ITmfContext) method test.
260 public void testGetNext() {
261 ITmfContext context
= fixture
.seekEvent(0);
262 CtfTmfEvent result
= fixture
.getNext(context
);
263 assertNotNull(result
);
268 * Run the String getPath() method test.
271 public void testGetPath() {
272 String result
= fixture
.getPath();
273 assertNotNull(result
);
277 * Run the IResource getResource() method test.
280 public void testGetResource() {
281 IResource result
= fixture
.getResource();
286 * Run the ITmfTimestamp getStartTime() method test.
289 public void testGetStartTime() {
290 ITmfTimestamp result
= fixture
.getStartTime();
291 assertNotNull(result
);
295 * Run the long getStreamingInterval() method test.
298 public void testGetStreamingInterval() {
299 long result
= fixture
.getStreamingInterval();
300 assertEquals(0L, result
);
304 * Run the TmfTimeRange getTimeRange() method test.
307 public void testGetTimeRange() {
308 TmfTimeRange result
= fixture
.getTimeRange();
309 assertNotNull(result
);
313 * Run the CtfTmfEvent readNextEvent(ITmfContext) method test.
316 public void testReadNextEvent() {
317 ITmfContext context
= fixture
.seekEvent(0);
318 CtfTmfEvent result
= fixture
.getNext(context
);
319 assertNotNull(result
);
324 * Run the ITmfContext seekEvent(double) method test.
327 public void testSeekEvent_ratio() {
328 ITmfContext context
= fixture
.seekEvent(0);
329 long t1
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp();
330 fixture
.getNext(context
);
331 long t2
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp();
332 fixture
.getNext(context
);
333 long t3
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp();
334 fixture
.getNext(context
);
336 context
= fixture
.seekEvent(0.0);
337 assertEquals(t1
, ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp());
339 context
= fixture
.seekEvent(0.5);
340 assertEquals(t2
, ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp());
342 context
= fixture
.seekEvent(1.0);
343 assertEquals(t3
, ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp());
348 * Run the ITmfContext seekEvent(long) method test.
351 public void testSeekEvent_rank() {
353 ITmfContext result
= fixture
.seekEvent(rank
);
354 assertNotNull(result
);
359 * Run the ITmfContext seekEvent(ITmfTimestamp) method test.
362 public void testSeekEvent_timestamp() {
363 ITmfTimestamp timestamp
= new TmfTimestamp();
364 ITmfContext result
= fixture
.seekEvent(timestamp
);
365 assertNotNull(result
);
370 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test.
373 public void testSeekEvent_location() {
374 final CtfLocationInfo location2
= new CtfLocationInfo(1L, 0L);
375 CtfLocation ctfLocation
= new CtfLocation(location2
);
376 ITmfContext result
= fixture
.seekEvent(ctfLocation
);
377 assertNotNull(result
);
382 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test with invalid location.
385 public void testSeekEventInvalidLocation() {
386 CtfLocation ctfLocation
= new CtfLocation(CtfLocation
.INVALID_LOCATION
);
387 ITmfContext result
= fixture
.seekEvent(ctfLocation
);
388 assertNull(fixture
.getNext(result
));
389 assertEquals(CtfLocation
.INVALID_LOCATION
, result
.getLocation().getLocationInfo());
392 // Not using CtfLocation.INVALID_LOCATION directly on purpose, to make sure CtfLocationInfo.equals is properly used
393 CtfLocationInfo invalidLocation
= new CtfLocationInfo(CtfLocation
.INVALID_LOCATION
.getTimestamp(), CtfLocation
.INVALID_LOCATION
.getIndex());
394 ctfLocation
= new CtfLocation(invalidLocation
);
395 result
= fixture
.seekEvent(ctfLocation
);
396 assertNull(fixture
.getNext(result
));
397 assertEquals(CtfLocation
.INVALID_LOCATION
, result
.getLocation().getLocationInfo());
402 * Run the boolean validate(IProject,String) method test.
405 public void testValidate() {
406 IProject project
= null;
407 IStatus result
= fixture
.validate(project
, fixture
.getPath());
408 assertTrue(result
.isOK());
412 * Run the boolean hasEvent(final String) method test
415 public void testEventLookup() {
416 Set
<@NonNull ?
extends ITmfEventType
> eventTypes
= fixture
.getContainedEventTypes();
417 Set
<String
> eventNames
= TmfEventTypeCollectionHelper
.getEventNames(eventTypes
);
418 assertTrue(eventNames
.contains("sched_switch"));
419 assertFalse(eventNames
.contains("Sched_switch"));
420 String
[] events
= { "sched_switch", "sched_wakeup", "timer_init" };
421 assertTrue(eventNames
.containsAll(Arrays
.asList(events
)));
422 Set
<String
> copy
= new HashSet
<>(eventNames
);
423 copy
.retainAll(Arrays
.asList(events
));
424 assertFalse(copy
.isEmpty());
425 String
[] names
= { "inexistent", "sched_switch", "SomeThing" };
426 copy
= new HashSet
<>(eventNames
);
427 copy
.retainAll(Arrays
.asList(names
));
428 assertTrue(!copy
.isEmpty());
429 assertFalse(eventNames
.containsAll(Arrays
.asList(names
)));
433 * Run the String getHostId() method test
436 public void testCtfHostId() {
437 String a
= fixture
.getHostId();
438 assertEquals("\"84db105b-b3f4-4821-b662-efc51455106a\"", a
);