1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertFalse
;
18 import static org
.junit
.Assert
.assertNotNull
;
19 import static org
.junit
.Assert
.assertNull
;
20 import static org
.junit
.Assert
.assertTrue
;
21 import static org
.junit
.Assume
.assumeTrue
;
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
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfEndSynchSignal
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignal
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfEventTypeCollectionHelper
;
39 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfLocation
;
40 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfLocationInfo
;
41 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfTmfEvent
;
42 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfTmfTimestamp
;
43 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfTmfTrace
;
44 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
45 import org
.junit
.After
;
46 import org
.junit
.Before
;
47 import org
.junit
.Test
;
50 * The class <code>CtfTmfTraceTest</code> contains tests for the class
51 * <code>{@link CtfTmfTrace}</code>.
56 public class CtfTmfTraceTest
{
58 private static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.KERNEL
;
60 private CtfTmfTrace fixture
;
63 * Perform pre-test initialization.
65 * @throws TmfTraceException
66 * If the test trace is not found
69 public void setUp() throws TmfTraceException
{
70 assumeTrue(testTrace
.exists());
71 fixture
= new CtfTmfTrace();
72 fixture
.initTrace((IResource
) null, testTrace
.getPath(), CtfTmfEvent
.class);
76 * Perform post-test clean-up.
79 public void tearDown() {
80 if (fixture
!= null) {
86 * Run the CtfTmfTrace() constructor test.
89 public void testCtfTmfTrace() {
90 try (CtfTmfTrace result
= new CtfTmfTrace();) {
91 assertNotNull(result
);
92 assertEquals(1000, result
.getCacheSize());
93 assertEquals(0L, result
.getNbEvents());
94 assertEquals(0L, result
.getStreamingInterval());
95 assertNull(result
.getResource());
96 assertNull(result
.getType());
101 * Test the parseEvent() method
104 public void testParseEvent() {
105 ITmfContext ctx
= fixture
.seekEvent(0);
106 fixture
.getNext(ctx
);
107 CtfTmfEvent event
= fixture
.parseEvent(ctx
);
108 assertNotNull(event
);
112 * Run the void broadcast(TmfSignal) method test.
115 public void testBroadcast() {
116 TmfSignal signal
= new TmfEndSynchSignal(1);
117 fixture
.broadcast(signal
);
121 * Run the void dispose() method test.
124 public void testClose() {
125 try (CtfTmfTrace emptyFixture
= new CtfTmfTrace();) {
130 * Run the int getCacheSize() method test.
133 public void testGetCacheSize() {
134 try (CtfTmfTrace emptyFixture
= new CtfTmfTrace();) {
135 int result
= emptyFixture
.getCacheSize();
136 assertEquals(1000, result
);
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 CtfTmfTimestamp(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 final CtfLocationInfo location2
= new CtfLocationInfo(1, 0);
214 CtfLocation location
= new CtfLocation(location2
);
215 double result
= fixture
.getLocationRatio(location
);
217 assertEquals(Double
.NEGATIVE_INFINITY
, result
, 0.1);
221 * Run the String getName() method test.
224 public void testGetName() {
225 String result
= fixture
.getName();
226 assertNotNull(result
);
230 * Run the getTraceProperties() method test.
233 public void testGetTraceProperties() {
234 int result
= fixture
.getTraceProperties().size();
235 assertEquals(9, result
);
239 * Run the long getNbEvents() method test.
242 public void testGetNbEvents() {
243 long result
= fixture
.getNbEvents();
244 assertEquals(1L, result
);
248 * Run the CtfTmfEvent getNext(ITmfContext) method test.
251 public void testGetNext() {
252 ITmfContext context
= fixture
.seekEvent(0);
253 CtfTmfEvent result
= fixture
.getNext(context
);
254 assertNotNull(result
);
258 * Run the String getPath() method test.
261 public void testGetPath() {
262 String result
= fixture
.getPath();
263 assertNotNull(result
);
267 * Run the IResource getResource() method test.
270 public void testGetResource() {
271 IResource result
= fixture
.getResource();
276 * Run the ITmfTimestamp getStartTime() method test.
279 public void testGetStartTime() {
280 ITmfTimestamp result
= fixture
.getStartTime();
281 assertNotNull(result
);
285 * Run the long getStreamingInterval() method test.
288 public void testGetStreamingInterval() {
289 long result
= fixture
.getStreamingInterval();
290 assertEquals(0L, result
);
294 * Run the TmfTimeRange getTimeRange() method test.
297 public void testGetTimeRange() {
298 TmfTimeRange result
= fixture
.getTimeRange();
299 assertNotNull(result
);
303 * Run the CtfTmfEvent readNextEvent(ITmfContext) method test.
306 public void testReadNextEvent() {
307 ITmfContext context
= fixture
.seekEvent(0);
308 CtfTmfEvent result
= fixture
.getNext(context
);
309 assertNotNull(result
);
313 * Run the ITmfContext seekEvent(double) method test.
316 public void testSeekEvent_ratio() {
318 ITmfContext result
= fixture
.seekEvent(ratio
);
319 assertNotNull(result
);
323 * Run the ITmfContext seekEvent(long) method test.
326 public void testSeekEvent_rank() {
328 ITmfContext result
= fixture
.seekEvent(rank
);
329 assertNotNull(result
);
333 * Run the ITmfContext seekEvent(ITmfTimestamp) method test.
336 public void testSeekEvent_timestamp() {
337 ITmfTimestamp timestamp
= new TmfTimestamp();
338 ITmfContext result
= fixture
.seekEvent(timestamp
);
339 assertNotNull(result
);
343 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test.
346 public void testSeekEvent_location() {
347 final CtfLocationInfo location2
= new CtfLocationInfo(1L, 0L);
348 CtfLocation ctfLocation
= new CtfLocation(location2
);
349 ITmfContext result
= fixture
.seekEvent(ctfLocation
);
350 assertNotNull(result
);
354 * Run the boolean validate(IProject,String) method test.
357 public void testValidate() {
358 IProject project
= null;
359 IStatus result
= fixture
.validate(project
, testTrace
.getPath());
360 assertTrue(result
.isOK());
364 * Run the boolean hasEvent(final String) method test
367 public void testEventLookup() {
368 Set
<?
extends ITmfEventType
> eventTypes
= fixture
.getContainedEventTypes();
369 Set
<String
> eventNames
= TmfEventTypeCollectionHelper
.getEventNames(eventTypes
);
370 assertTrue(eventNames
.contains("sched_switch"));
371 assertFalse(eventNames
.contains("Sched_switch"));
372 String
[] events
= { "sched_switch", "sched_wakeup", "timer_init" };
373 assertTrue(eventNames
.containsAll(Arrays
.asList(events
)));
374 Set
<String
> copy
= new HashSet
<>(eventNames
);
375 copy
.retainAll(Arrays
.asList(events
));
376 assertFalse(copy
.isEmpty());
377 String
[] names
= { "inexistent", "sched_switch", "SomeThing" };
378 copy
= new HashSet
<>(eventNames
);
379 copy
.retainAll(Arrays
.asList(names
));
380 assertTrue(!copy
.isEmpty());
381 assertFalse(eventNames
.containsAll(Arrays
.asList(names
)));
385 * Run the String getHostId() method test
388 public void testCtfHostId() {
389 String a
= fixture
.getHostId();
390 assertEquals("\"84db105b-b3f4-4821-b662-efc51455106a\"", a
);