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
;
22 import static org
.junit
.Assume
.assumeTrue
;
24 import java
.util
.Arrays
;
25 import java
.util
.HashSet
;
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
;
51 * The class <code>CtfTmfTraceTest</code> contains tests for the class
52 * <code>{@link CtfTmfTrace}</code>.
57 public class CtfTmfTraceTest
{
59 private static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.KERNEL
;
61 private CtfTmfTrace fixture
;
64 * Perform pre-test initialization.
66 * @throws TmfTraceException
67 * If the test trace is not found
70 public void setUp() throws TmfTraceException
{
71 assumeTrue(testTrace
.exists());
72 fixture
= new CtfTmfTrace();
73 fixture
.initTrace((IResource
) null, testTrace
.getPath(), CtfTmfEvent
.class);
77 * Perform post-test clean-up.
80 public void tearDown() {
81 if (fixture
!= null) {
87 * Run the CtfTmfTrace() constructor test.
90 public void testCtfTmfTrace() {
91 CtfTmfTrace result
= new CtfTmfTrace();
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
.getType());
104 * Test the parseEvent() method
107 public void testParseEvent() {
108 ITmfContext ctx
= fixture
.seekEvent(0);
109 fixture
.getNext(ctx
);
110 CtfTmfEvent event
= fixture
.parseEvent(ctx
);
111 assertNotNull(event
);
116 * Run the void broadcast(TmfSignal) method test.
119 public void testBroadcast() {
120 TmfSignal signal
= new TmfEndSynchSignal(1);
121 fixture
.broadcast(signal
);
125 * Run the void dispose() method test.
128 public void testDispose() {
129 CtfTmfTrace emptyFixture
= new CtfTmfTrace();
130 emptyFixture
.dispose();
134 * Run the int getCacheSize() method test.
137 public void testGetCacheSize() {
138 CtfTmfTrace emptyFixture
= new CtfTmfTrace();
139 int result
= emptyFixture
.getCacheSize();
140 assertEquals(1000, result
);
142 emptyFixture
.dispose();
146 * Run the ITmfLocation<Comparable> getCurrentLocation() method test.
149 public void testGetCurrentLocation() {
150 CtfLocation result
= (CtfLocation
) fixture
.getCurrentLocation();
155 * Test the seekEvent() method with a null location.
158 public void testSeekEventLoc_null() {
159 CtfLocation loc
= null;
160 fixture
.seekEvent(loc
);
161 assertNotNull(fixture
);
165 * Test the seekEvent() method with a location from a timestamp.
168 public void testSeekEventLoc_timetamp() {
169 CtfLocation loc
= new CtfLocation(new TmfNanoTimestamp(0L));
170 fixture
.seekEvent(loc
);
171 assertNotNull(fixture
);
175 * Run the ITmfTimestamp getEndTime() method test.
178 public void testGetEndTime() {
179 ITmfTimestamp result
= fixture
.getEndTime();
180 assertNotNull(result
);
184 * Run the String getEnvironment method test.
187 public void testGetEnvValue() {
188 String key
= "tracer_name";
189 String result
= fixture
.getTraceProperties().get(key
);
190 assertEquals("\"lttng-modules\"", result
);
194 * Test the {@link CtfTmfTrace#getEventType()} method.
197 public void testGetEventType() {
198 Class
<?
> result
= fixture
.getEventType();
199 assertNotNull(result
);
200 assertEquals(CtfTmfEvent
.class, result
);
204 * Run the Class<CtfTmfEvent> getContainedEventTypes() method test.
207 public void testGetContainedEventTypes() {
208 Set
<?
extends ITmfEventType
> result
= fixture
.getContainedEventTypes();
209 assertNotNull(result
);
210 assertFalse(result
.isEmpty());
214 * Run the double getLocationRatio(ITmfLocation<?>) method 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
);
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);
235 * Run the String getName() method test.
238 public void testGetName() {
239 String result
= fixture
.getName();
240 assertNotNull(result
);
244 * Run the getTraceProperties() method test.
247 public void testGetTraceProperties() {
248 int result
= fixture
.getTraceProperties().size();
249 assertEquals(9, result
);
253 * Run the long getNbEvents() method test.
256 public void testGetNbEvents() {
257 long result
= fixture
.getNbEvents();
258 assertEquals(1L, result
);
262 * Run the CtfTmfEvent getNext(ITmfContext) method test.
265 public void testGetNext() {
266 ITmfContext context
= fixture
.seekEvent(0);
267 CtfTmfEvent result
= fixture
.getNext(context
);
268 assertNotNull(result
);
273 * Run the String getPath() method test.
276 public void testGetPath() {
277 String result
= fixture
.getPath();
278 assertNotNull(result
);
282 * Run the IResource getResource() method test.
285 public void testGetResource() {
286 IResource result
= fixture
.getResource();
291 * Run the ITmfTimestamp getStartTime() method test.
294 public void testGetStartTime() {
295 ITmfTimestamp result
= fixture
.getStartTime();
296 assertNotNull(result
);
300 * Run the long getStreamingInterval() method test.
303 public void testGetStreamingInterval() {
304 long result
= fixture
.getStreamingInterval();
305 assertEquals(0L, result
);
309 * Run the TmfTimeRange getTimeRange() method test.
312 public void testGetTimeRange() {
313 TmfTimeRange result
= fixture
.getTimeRange();
314 assertNotNull(result
);
318 * Run the CtfTmfEvent readNextEvent(ITmfContext) method test.
321 public void testReadNextEvent() {
322 ITmfContext context
= fixture
.seekEvent(0);
323 CtfTmfEvent result
= fixture
.getNext(context
);
324 assertNotNull(result
);
329 * Run the ITmfContext seekEvent(double) method 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
);
341 context
= fixture
.seekEvent(0.0);
342 assertEquals(t1
, ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp());
344 context
= fixture
.seekEvent(0.5);
345 assertEquals(t2
, ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp());
347 context
= fixture
.seekEvent(1.0);
348 assertEquals(t3
, ((CtfLocationInfo
) context
.getLocation().getLocationInfo()).getTimestamp());
353 * Run the ITmfContext seekEvent(long) method test.
356 public void testSeekEvent_rank() {
358 ITmfContext result
= fixture
.seekEvent(rank
);
359 assertNotNull(result
);
364 * Run the ITmfContext seekEvent(ITmfTimestamp) method test.
367 public void testSeekEvent_timestamp() {
368 ITmfTimestamp timestamp
= new TmfTimestamp();
369 ITmfContext result
= fixture
.seekEvent(timestamp
);
370 assertNotNull(result
);
375 * Run the ITmfContext seekEvent(ITmfLocation<?>) method 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
);
387 * Run the boolean validate(IProject,String) method test.
390 public void testValidate() {
391 IProject project
= null;
392 IStatus result
= fixture
.validate(project
, testTrace
.getPath());
393 assertTrue(result
.isOK());
397 * Run the boolean hasEvent(final String) method test
400 public void testEventLookup() {
401 Set
<?
extends ITmfEventType
> eventTypes
= fixture
.getContainedEventTypes();
402 Set
<String
> eventNames
= TmfEventTypeCollectionHelper
.getEventNames(eventTypes
);
403 assertTrue(eventNames
.contains("sched_switch"));
404 assertFalse(eventNames
.contains("Sched_switch"));
405 String
[] events
= { "sched_switch", "sched_wakeup", "timer_init" };
406 assertTrue(eventNames
.containsAll(Arrays
.asList(events
)));
407 Set
<String
> copy
= new HashSet
<>(eventNames
);
408 copy
.retainAll(Arrays
.asList(events
));
409 assertFalse(copy
.isEmpty());
410 String
[] names
= { "inexistent", "sched_switch", "SomeThing" };
411 copy
= new HashSet
<>(eventNames
);
412 copy
.retainAll(Arrays
.asList(names
));
413 assertTrue(!copy
.isEmpty());
414 assertFalse(eventNames
.containsAll(Arrays
.asList(names
)));
418 * Run the String getHostId() method test
421 public void testCtfHostId() {
422 String a
= fixture
.getHostId();
423 assertEquals("\"84db105b-b3f4-4821-b662-efc51455106a\"", a
);