Fix some null warnings
[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.getTraceProperties().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.getTraceProperties().size();
244 assertEquals(9, result);
245 }
246
247 /**
248 * Run the long getNbEvents() method test.
249 */
250 @Test
251 public void testGetNbEvents() {
252 long result = fixture.getNbEvents();
253 assertEquals(1L, result);
254 }
255
256 /**
257 * Run the CtfTmfEvent getNext(ITmfContext) method test.
258 */
259 @Test
260 public void testGetNext() {
261 ITmfContext context = fixture.seekEvent(0);
262 CtfTmfEvent result = fixture.getNext(context);
263 assertNotNull(result);
264 context.dispose();
265 }
266
267 /**
268 * Run the String getPath() method test.
269 */
270 @Test
271 public void testGetPath() {
272 String result = fixture.getPath();
273 assertNotNull(result);
274 }
275
276 /**
277 * Run the IResource getResource() method test.
278 */
279 @Test
280 public void testGetResource() {
281 IResource result = fixture.getResource();
282 assertNull(result);
283 }
284
285 /**
286 * Run the ITmfTimestamp getStartTime() method test.
287 */
288 @Test
289 public void testGetStartTime() {
290 ITmfTimestamp result = fixture.getStartTime();
291 assertNotNull(result);
292 }
293
294 /**
295 * Run the long getStreamingInterval() method test.
296 */
297 @Test
298 public void testGetStreamingInterval() {
299 long result = fixture.getStreamingInterval();
300 assertEquals(0L, result);
301 }
302
303 /**
304 * Run the TmfTimeRange getTimeRange() method test.
305 */
306 @Test
307 public void testGetTimeRange() {
308 TmfTimeRange result = fixture.getTimeRange();
309 assertNotNull(result);
310 }
311
312 /**
313 * Run the CtfTmfEvent readNextEvent(ITmfContext) method test.
314 */
315 @Test
316 public void testReadNextEvent() {
317 ITmfContext context = fixture.seekEvent(0);
318 CtfTmfEvent result = fixture.getNext(context);
319 assertNotNull(result);
320 context.dispose();
321 }
322
323 /**
324 * Run the ITmfContext seekEvent(double) method test.
325 */
326 @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);
335 context.dispose();
336 context = fixture.seekEvent(0.0);
337 assertEquals(t1, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
338 context.dispose();
339 context = fixture.seekEvent(0.5);
340 assertEquals(t2, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
341 context.dispose();
342 context = fixture.seekEvent(1.0);
343 assertEquals(t3, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
344 context.dispose();
345 }
346
347 /**
348 * Run the ITmfContext seekEvent(long) method test.
349 */
350 @Test
351 public void testSeekEvent_rank() {
352 long rank = 1L;
353 ITmfContext result = fixture.seekEvent(rank);
354 assertNotNull(result);
355 result.dispose();
356 }
357
358 /**
359 * Run the ITmfContext seekEvent(ITmfTimestamp) method test.
360 */
361 @Test
362 public void testSeekEvent_timestamp() {
363 ITmfTimestamp timestamp = new TmfTimestamp();
364 ITmfContext result = fixture.seekEvent(timestamp);
365 assertNotNull(result);
366 result.dispose();
367 }
368
369 /**
370 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test.
371 */
372 @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);
378 result.dispose();
379 }
380
381 /**
382 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test with invalid location.
383 */
384 @Test
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());
390 result.dispose();
391
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());
398 result.dispose();
399 }
400
401 /**
402 * Run the boolean validate(IProject,String) method test.
403 */
404 @Test
405 public void testValidate() {
406 IProject project = null;
407 IStatus result = fixture.validate(project, fixture.getPath());
408 assertTrue(result.isOK());
409 }
410
411 /**
412 * Run the boolean hasEvent(final String) method test
413 */
414 @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)));
430 }
431
432 /**
433 * Run the String getHostId() method test
434 */
435 @Test
436 public void testCtfHostId() {
437 String a = fixture.getHostId();
438 assertEquals("\"84db105b-b3f4-4821-b662-efc51455106a\"", a);
439 }
440
441 }
This page took 0.043607 seconds and 5 git commands to generate.