5f079dc688abed693061978ebd2b78280ed44665
[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
23 import java.util.Arrays;
24 import java.util.HashSet;
25 import java.util.Set;
26
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;
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 @NonNull CtfTestTrace testTrace = CtfTestTrace.KERNEL;
60
61 private CtfTmfTrace fixture;
62
63 /**
64 * Perform pre-test initialization.
65 */
66 @Before
67 public void setUp() {
68 fixture = CtfTmfTestTraceUtils.getTrace(testTrace);
69 }
70
71 /**
72 * Perform post-test clean-up.
73 */
74 @After
75 public void tearDown() {
76 if (fixture != null) {
77 fixture.dispose();
78 }
79 }
80
81 /**
82 * Run the CtfTmfTrace() constructor test.
83 */
84 @Test
85 public void testCtfTmfTrace() {
86 CtfTmfTrace result = new CtfTmfTrace();
87
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());
94
95 result.dispose();
96 }
97
98 /**
99 * Test the parseEvent() method
100 */
101 @Test
102 public void testParseEvent() {
103 ITmfContext ctx = fixture.seekEvent(0);
104 fixture.getNext(ctx);
105 CtfTmfEvent event = fixture.parseEvent(ctx);
106 assertNotNull(event);
107 ctx.dispose();
108 }
109
110 /**
111 * Run the void broadcast(TmfSignal) method test.
112 */
113 @Test
114 public void testBroadcast() {
115 TmfSignal signal = new TmfEndSynchSignal(1);
116 fixture.broadcast(signal);
117 }
118
119 /**
120 * Run the void dispose() method test.
121 */
122 @Test
123 public void testDispose() {
124 CtfTmfTrace emptyFixture = new CtfTmfTrace();
125 emptyFixture.dispose();
126 }
127
128 /**
129 * Run the int getCacheSize() method test.
130 */
131 @Test
132 public void testGetCacheSize() {
133 CtfTmfTrace emptyFixture = new CtfTmfTrace();
134 int result = emptyFixture.getCacheSize();
135 assertEquals(1000, result);
136
137 emptyFixture.dispose();
138 }
139
140 /**
141 * Run the ITmfLocation<Comparable> getCurrentLocation() method test.
142 */
143 @Test
144 public void testGetCurrentLocation() {
145 CtfLocation result = (CtfLocation) fixture.getCurrentLocation();
146 assertNull(result);
147 }
148
149 /**
150 * Test the seekEvent() method with a null location.
151 */
152 @Test
153 public void testSeekEventLoc_null() {
154 CtfLocation loc = null;
155 fixture.seekEvent(loc);
156 assertNotNull(fixture);
157 }
158
159 /**
160 * Test the seekEvent() method with a location from a timestamp.
161 */
162 @Test
163 public void testSeekEventLoc_timetamp() {
164 CtfLocation loc = new CtfLocation(new TmfNanoTimestamp(0L));
165 fixture.seekEvent(loc);
166 assertNotNull(fixture);
167 }
168
169 /**
170 * Run the ITmfTimestamp getEndTime() method test.
171 */
172 @Test
173 public void testGetEndTime() {
174 ITmfTimestamp result = fixture.getEndTime();
175 assertNotNull(result);
176 }
177
178 /**
179 * Run the String getEnvironment method test.
180 */
181 @Test
182 public void testGetEnvValue() {
183 String key = "tracer_name";
184 String result = fixture.getProperties().get(key);
185 assertEquals("\"lttng-modules\"", result);
186 }
187
188 /**
189 * Test the {@link CtfTmfTrace#getEventType()} method.
190 */
191 @Test
192 public void testGetEventType() {
193 Class<?> result = fixture.getEventType();
194 assertNotNull(result);
195 assertEquals(CtfTmfEvent.class, result);
196 }
197
198 /**
199 * Run the Class<CtfTmfEvent> getContainedEventTypes() method test.
200 */
201 @Test
202 public void testGetContainedEventTypes() {
203 Set<? extends ITmfEventType> result = fixture.getContainedEventTypes();
204 assertNotNull(result);
205 assertFalse(result.isEmpty());
206 }
207
208 /**
209 * Run the double getLocationRatio(ITmfLocation<?>) method test.
210 */
211 @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);
220 context.dispose();
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);
227 }
228
229 /**
230 * Run the String getName() method test.
231 */
232 @Test
233 public void testGetName() {
234 String result = fixture.getName();
235 assertNotNull(result);
236 }
237
238 /**
239 * Run the getTraceProperties() method test.
240 */
241 @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"));
246 }
247
248 /**
249 * Run the long getNbEvents() method test.
250 */
251 @Test
252 public void testGetNbEvents() {
253 long result = fixture.getNbEvents();
254 assertEquals(1L, result);
255 }
256
257 /**
258 * Run the CtfTmfEvent getNext(ITmfContext) method test.
259 */
260 @Test
261 public void testGetNext() {
262 ITmfContext context = fixture.seekEvent(0);
263 CtfTmfEvent result = fixture.getNext(context);
264 assertNotNull(result);
265 context.dispose();
266 }
267
268 /**
269 * Run the String getPath() method test.
270 */
271 @Test
272 public void testGetPath() {
273 String result = fixture.getPath();
274 assertNotNull(result);
275 }
276
277 /**
278 * Run the IResource getResource() method test.
279 */
280 @Test
281 public void testGetResource() {
282 IResource result = fixture.getResource();
283 assertNull(result);
284 }
285
286 /**
287 * Run the ITmfTimestamp getStartTime() method test.
288 */
289 @Test
290 public void testGetStartTime() {
291 ITmfTimestamp result = fixture.getStartTime();
292 assertNotNull(result);
293 }
294
295 /**
296 * Run the long getStreamingInterval() method test.
297 */
298 @Test
299 public void testGetStreamingInterval() {
300 long result = fixture.getStreamingInterval();
301 assertEquals(0L, result);
302 }
303
304 /**
305 * Run the TmfTimeRange getTimeRange() method test.
306 */
307 @Test
308 public void testGetTimeRange() {
309 TmfTimeRange result = fixture.getTimeRange();
310 assertNotNull(result);
311 }
312
313 /**
314 * Run the CtfTmfEvent readNextEvent(ITmfContext) method test.
315 */
316 @Test
317 public void testReadNextEvent() {
318 ITmfContext context = fixture.seekEvent(0);
319 CtfTmfEvent result = fixture.getNext(context);
320 assertNotNull(result);
321 context.dispose();
322 }
323
324 /**
325 * Run the ITmfContext seekEvent(double) method test.
326 */
327 @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);
336 context.dispose();
337 context = fixture.seekEvent(0.0);
338 assertEquals(t1, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
339 context.dispose();
340 context = fixture.seekEvent(0.5);
341 assertEquals(t2, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
342 context.dispose();
343 context = fixture.seekEvent(1.0);
344 assertEquals(t3, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
345 context.dispose();
346 }
347
348 /**
349 * Run the ITmfContext seekEvent(long) method test.
350 */
351 @Test
352 public void testSeekEvent_rank() {
353 long rank = 1L;
354 ITmfContext result = fixture.seekEvent(rank);
355 assertNotNull(result);
356 result.dispose();
357 }
358
359 /**
360 * Run the ITmfContext seekEvent(ITmfTimestamp) method test.
361 */
362 @Test
363 public void testSeekEvent_timestamp() {
364 ITmfTimestamp timestamp = new TmfTimestamp();
365 ITmfContext result = fixture.seekEvent(timestamp);
366 assertNotNull(result);
367 result.dispose();
368 }
369
370 /**
371 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test.
372 */
373 @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);
379 result.dispose();
380 }
381
382 /**
383 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test with invalid location.
384 */
385 @Test
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());
391 result.dispose();
392
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());
399 result.dispose();
400 }
401
402 /**
403 * Run the boolean validate(IProject,String) method test.
404 */
405 @Test
406 public void testValidate() {
407 IProject project = null;
408 IStatus result = fixture.validate(project, fixture.getPath());
409 assertTrue(result.isOK());
410 }
411
412 /**
413 * Run the boolean hasEvent(final String) method test
414 */
415 @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)));
431 }
432
433 /**
434 * Run the String getHostId() method test
435 */
436 @Test
437 public void testCtfHostId() {
438 String a = fixture.getHostId();
439 assertEquals("\"84db105b-b3f4-4821-b662-efc51455106a\"", a);
440 }
441
442 }
This page took 0.061452 seconds and 4 git commands to generate.