rcp: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / 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 import static org.junit.Assume.assumeTrue;
23
24 import java.util.Arrays;
25 import java.util.HashSet;
26 import java.util.Set;
27
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;
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 CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL;
60
61 private CtfTmfTrace fixture;
62
63 /**
64 * Perform pre-test initialization.
65 *
66 * @throws TmfTraceException
67 * If the test trace is not found
68 */
69 @Before
70 public void setUp() throws TmfTraceException {
71 assumeTrue(testTrace.exists());
72 fixture = new CtfTmfTrace();
73 fixture.initTrace((IResource) null, testTrace.getPath(), CtfTmfEvent.class);
74 }
75
76 /**
77 * Perform post-test clean-up.
78 */
79 @After
80 public void tearDown() {
81 if (fixture != null) {
82 fixture.dispose();
83 }
84 }
85
86 /**
87 * Run the CtfTmfTrace() constructor test.
88 */
89 @Test
90 public void testCtfTmfTrace() {
91 try (CtfTmfTrace result = new CtfTmfTrace();) {
92 assertNotNull(result);
93 assertEquals(1000, result.getCacheSize());
94 assertEquals(0L, result.getNbEvents());
95 assertEquals(0L, result.getStreamingInterval());
96 assertNull(result.getResource());
97 assertNull(result.getType());
98 }
99 }
100
101 /**
102 * Test the parseEvent() method
103 */
104 @Test
105 public void testParseEvent() {
106 ITmfContext ctx = fixture.seekEvent(0);
107 fixture.getNext(ctx);
108 CtfTmfEvent event = fixture.parseEvent(ctx);
109 assertNotNull(event);
110 ctx.dispose();
111 }
112
113 /**
114 * Run the void broadcast(TmfSignal) method test.
115 */
116 @Test
117 public void testBroadcast() {
118 TmfSignal signal = new TmfEndSynchSignal(1);
119 fixture.broadcast(signal);
120 }
121
122 /**
123 * Run the void dispose() method test.
124 */
125 @Test
126 public void testClose() {
127 try (CtfTmfTrace emptyFixture = new CtfTmfTrace();) {
128 }
129 }
130
131 /**
132 * Run the int getCacheSize() method test.
133 */
134 @Test
135 public void testGetCacheSize() {
136 try (CtfTmfTrace emptyFixture = new CtfTmfTrace();) {
137 int result = emptyFixture.getCacheSize();
138 assertEquals(1000, result);
139 }
140 }
141
142 /**
143 * Run the ITmfLocation<Comparable> getCurrentLocation() method test.
144 */
145 @Test
146 public void testGetCurrentLocation() {
147 CtfLocation result = (CtfLocation) fixture.getCurrentLocation();
148 assertNull(result);
149 }
150
151 /**
152 * Test the seekEvent() method with a null location.
153 */
154 @Test
155 public void testSeekEventLoc_null() {
156 CtfLocation loc = null;
157 fixture.seekEvent(loc);
158 assertNotNull(fixture);
159 }
160
161 /**
162 * Test the seekEvent() method with a location from a timestamp.
163 */
164 @Test
165 public void testSeekEventLoc_timetamp() {
166 CtfLocation loc = new CtfLocation(new TmfNanoTimestamp(0L));
167 fixture.seekEvent(loc);
168 assertNotNull(fixture);
169 }
170
171 /**
172 * Run the ITmfTimestamp getEndTime() method test.
173 */
174 @Test
175 public void testGetEndTime() {
176 ITmfTimestamp result = fixture.getEndTime();
177 assertNotNull(result);
178 }
179
180 /**
181 * Run the String getEnvironment method test.
182 */
183 @Test
184 public void testGetEnvValue() {
185 String key = "tracer_name";
186 String result = fixture.getTraceProperties().get(key);
187 assertEquals("\"lttng-modules\"", result);
188 }
189
190 /**
191 * Test the {@link CtfTmfTrace#getEventType()} method.
192 */
193 @Test
194 public void testGetEventType() {
195 Class<?> result = fixture.getEventType();
196 assertNotNull(result);
197 assertEquals(CtfTmfEvent.class, result);
198 }
199
200 /**
201 * Run the Class<CtfTmfEvent> getContainedEventTypes() method test.
202 */
203 @Test
204 public void testGetContainedEventTypes() {
205 Set<? extends ITmfEventType> result = fixture.getContainedEventTypes();
206 assertNotNull(result);
207 assertFalse(result.isEmpty());
208 }
209
210 /**
211 * Run the double getLocationRatio(ITmfLocation<?>) method test.
212 */
213 @Test
214 public void testGetLocationRatio() {
215 ITmfContext context = fixture.seekEvent(0);
216 long t1 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
217 fixture.getNext(context);
218 long t2 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
219 fixture.getNext(context);
220 long t3 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
221 fixture.getNext(context);
222 context.dispose();
223 double ratio1 = fixture.getLocationRatio(new CtfLocation(t1, 0));
224 assertEquals(0.0, ratio1, 0.01);
225 double ratio2 = fixture.getLocationRatio(new CtfLocation(t2, 0));
226 assertEquals((double) (t2 - t1) / (t3 - t1), ratio2, 0.01);
227 double ratio3 = fixture.getLocationRatio(new CtfLocation(t3, 0));
228 assertEquals(1.0, ratio3, 0.01);
229 }
230
231 /**
232 * Run the String getName() method test.
233 */
234 @Test
235 public void testGetName() {
236 String result = fixture.getName();
237 assertNotNull(result);
238 }
239
240 /**
241 * Run the getTraceProperties() method test.
242 */
243 @Test
244 public void testGetTraceProperties() {
245 int result = fixture.getTraceProperties().size();
246 assertEquals(9, result);
247 }
248
249 /**
250 * Run the long getNbEvents() method test.
251 */
252 @Test
253 public void testGetNbEvents() {
254 long result = fixture.getNbEvents();
255 assertEquals(1L, result);
256 }
257
258 /**
259 * Run the CtfTmfEvent getNext(ITmfContext) method test.
260 */
261 @Test
262 public void testGetNext() {
263 ITmfContext context = fixture.seekEvent(0);
264 CtfTmfEvent result = fixture.getNext(context);
265 assertNotNull(result);
266 context.dispose();
267 }
268
269 /**
270 * Run the String getPath() method test.
271 */
272 @Test
273 public void testGetPath() {
274 String result = fixture.getPath();
275 assertNotNull(result);
276 }
277
278 /**
279 * Run the IResource getResource() method test.
280 */
281 @Test
282 public void testGetResource() {
283 IResource result = fixture.getResource();
284 assertNull(result);
285 }
286
287 /**
288 * Run the ITmfTimestamp getStartTime() method test.
289 */
290 @Test
291 public void testGetStartTime() {
292 ITmfTimestamp result = fixture.getStartTime();
293 assertNotNull(result);
294 }
295
296 /**
297 * Run the long getStreamingInterval() method test.
298 */
299 @Test
300 public void testGetStreamingInterval() {
301 long result = fixture.getStreamingInterval();
302 assertEquals(0L, result);
303 }
304
305 /**
306 * Run the TmfTimeRange getTimeRange() method test.
307 */
308 @Test
309 public void testGetTimeRange() {
310 TmfTimeRange result = fixture.getTimeRange();
311 assertNotNull(result);
312 }
313
314 /**
315 * Run the CtfTmfEvent readNextEvent(ITmfContext) method test.
316 */
317 @Test
318 public void testReadNextEvent() {
319 ITmfContext context = fixture.seekEvent(0);
320 CtfTmfEvent result = fixture.getNext(context);
321 assertNotNull(result);
322 context.dispose();
323 }
324
325 /**
326 * Run the ITmfContext seekEvent(double) method test.
327 */
328 @Test
329 public void testSeekEvent_ratio() {
330 ITmfContext context = fixture.seekEvent(0);
331 long t1 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
332 fixture.getNext(context);
333 long t2 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
334 fixture.getNext(context);
335 long t3 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
336 fixture.getNext(context);
337 context.dispose();
338 context = fixture.seekEvent(0.0);
339 assertEquals(t1, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
340 context.dispose();
341 context = fixture.seekEvent(0.5);
342 assertEquals(t2, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
343 context.dispose();
344 context = fixture.seekEvent(1.0);
345 assertEquals(t3, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
346 context.dispose();
347 }
348
349 /**
350 * Run the ITmfContext seekEvent(long) method test.
351 */
352 @Test
353 public void testSeekEvent_rank() {
354 long rank = 1L;
355 ITmfContext result = fixture.seekEvent(rank);
356 assertNotNull(result);
357 result.dispose();
358 }
359
360 /**
361 * Run the ITmfContext seekEvent(ITmfTimestamp) method test.
362 */
363 @Test
364 public void testSeekEvent_timestamp() {
365 ITmfTimestamp timestamp = new TmfTimestamp();
366 ITmfContext result = fixture.seekEvent(timestamp);
367 assertNotNull(result);
368 result.dispose();
369 }
370
371 /**
372 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test.
373 */
374 @Test
375 public void testSeekEvent_location() {
376 final CtfLocationInfo location2 = new CtfLocationInfo(1L, 0L);
377 CtfLocation ctfLocation = new CtfLocation(location2);
378 ITmfContext result = fixture.seekEvent(ctfLocation);
379 assertNotNull(result);
380 result.dispose();
381 }
382
383 /**
384 * Run the boolean validate(IProject,String) method test.
385 */
386 @Test
387 public void testValidate() {
388 IProject project = null;
389 IStatus result = fixture.validate(project, testTrace.getPath());
390 assertTrue(result.isOK());
391 }
392
393 /**
394 * Run the boolean hasEvent(final String) method test
395 */
396 @Test
397 public void testEventLookup() {
398 Set<? extends ITmfEventType> eventTypes = fixture.getContainedEventTypes();
399 Set<String> eventNames = TmfEventTypeCollectionHelper.getEventNames(eventTypes);
400 assertTrue(eventNames.contains("sched_switch"));
401 assertFalse(eventNames.contains("Sched_switch"));
402 String[] events = { "sched_switch", "sched_wakeup", "timer_init" };
403 assertTrue(eventNames.containsAll(Arrays.asList(events)));
404 Set<String> copy = new HashSet<>(eventNames);
405 copy.retainAll(Arrays.asList(events));
406 assertFalse(copy.isEmpty());
407 String[] names = { "inexistent", "sched_switch", "SomeThing" };
408 copy = new HashSet<>(eventNames);
409 copy.retainAll(Arrays.asList(names));
410 assertTrue(!copy.isEmpty());
411 assertFalse(eventNames.containsAll(Arrays.asList(names)));
412 }
413
414 /**
415 * Run the String getHostId() method test
416 */
417 @Test
418 public void testCtfHostId() {
419 String a = fixture.getHostId();
420 assertEquals("\"84db105b-b3f4-4821-b662-efc51455106a\"", a);
421 }
422
423 }
This page took 0.043004 seconds and 5 git commands to generate.