Fix for bug 381411: Incorrect experiment location after getNext.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / TmfTraceTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 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 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adapted for TMF Trace Model 1.0
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.tests.trace;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.net.URISyntaxException;
19 import java.net.URL;
20 import java.util.Vector;
21
22 import junit.framework.TestCase;
23
24 import org.eclipse.core.runtime.FileLocator;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.linuxtools.internal.tmf.core.component.TmfProviderManager;
27 import org.eclipse.linuxtools.tmf.core.component.ITmfDataProvider;
28 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
29 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
30 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
31 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
32 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
33 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
34 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
35 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
36 import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
37 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
38 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
39 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
40 import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpointIndexer;
41 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
42 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
43
44 /**
45 * Test suite for the TmfTrace class.
46 */
47 @SuppressWarnings({ "nls" })
48 public class TmfTraceTest extends TestCase {
49
50 // ------------------------------------------------------------------------
51 // Variables
52 // ------------------------------------------------------------------------
53
54 private static final String DIRECTORY = "testfiles";
55 private static final String TEST_STREAM = "A-Test-10K";
56 private static final int BLOCK_SIZE = 500;
57 private static final int NB_EVENTS = 10000;
58 private static TmfTraceStub fTrace = null;
59
60 private static int SCALE = -3;
61
62 // ------------------------------------------------------------------------
63 // Housekeeping
64 // ------------------------------------------------------------------------
65
66 public TmfTraceTest(final String name) throws Exception {
67 super(name);
68 }
69
70 @Override
71 protected void setUp() throws Exception {
72 super.setUp();
73 fTrace = setupTrace(DIRECTORY + File.separator + TEST_STREAM);
74 }
75
76 @Override
77 protected void tearDown() throws Exception {
78 super.tearDown();
79 fTrace.dispose();
80 fTrace = null;
81 }
82
83 // ------------------------------------------------------------------------
84 // Helper functions
85 // ------------------------------------------------------------------------
86
87 private TmfTraceStub setupTrace(final String path) {
88 if (fTrace == null) {
89 try {
90 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null);
91 final File test = new File(FileLocator.toFileURL(location).toURI());
92 fTrace = new TmfTraceStub(test.toURI().getPath(), BLOCK_SIZE);
93 fTrace.indexTrace();
94 } catch (final TmfTraceException e) {
95 e.printStackTrace();
96 } catch (final URISyntaxException e) {
97 e.printStackTrace();
98 } catch (final IOException e) {
99 e.printStackTrace();
100 }
101 }
102 return fTrace;
103 }
104
105 // ------------------------------------------------------------------------
106 // Constructors
107 // ------------------------------------------------------------------------
108
109 public void testStandardConstructor() throws Exception {
110 TmfTraceStub trace = null;
111 File testfile = null;
112 try {
113 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM), null);
114 testfile = new File(FileLocator.toFileURL(location).toURI());
115 trace = new TmfTraceStub(testfile.toURI().getPath());
116 trace.indexTrace();
117 } catch (final URISyntaxException e) {
118 fail("URISyntaxException");
119 } catch (final IOException e) {
120 fail("IOException");
121 }
122
123 assertFalse ("Open trace", trace == null);
124 assertEquals("getType", TmfEvent.class, trace.getType());
125 assertNull ("getResource", trace.getResource());
126 assertEquals("getPath", testfile.toURI().getPath(), trace.getPath());
127 assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize());
128 assertEquals("getStreamingInterval", 0, trace.getStreamingInterval());
129 assertEquals("getName", TEST_STREAM, trace.getName());
130
131 assertEquals("getNbEvents", NB_EVENTS, trace.getNbEvents());
132 assertEquals("getRange-start", 1, trace.getTimeRange().getStartTime().getValue());
133 assertEquals("getRange-end", NB_EVENTS, trace.getTimeRange().getEndTime().getValue());
134 assertEquals("getStartTime", 1, trace.getStartTime().getValue());
135 assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue());
136 }
137
138 public void testStandardConstructorCacheSize() throws Exception {
139 TmfTraceStub trace = null;
140 File testfile = null;
141 try {
142 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM), null);
143 testfile = new File(FileLocator.toFileURL(location).toURI());
144 trace = new TmfTraceStub(testfile.toURI().getPath(), 0);
145 trace.indexTrace();
146 } catch (final URISyntaxException e) {
147 fail("URISyntaxException");
148 } catch (final IOException e) {
149 fail("IOException");
150 }
151
152 assertFalse ("Open trace", trace == null);
153 assertEquals("getType", TmfEvent.class, trace.getType());
154 assertNull ("getResource", trace.getResource());
155 assertEquals("getPath", testfile.toURI().getPath(), trace.getPath());
156 assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize());
157 assertEquals("getStreamingInterval", 0, trace.getStreamingInterval());
158 assertEquals("getName", TEST_STREAM, trace.getName());
159
160 assertEquals("getNbEvents", NB_EVENTS, trace.getNbEvents());
161 assertEquals("getRange-start", 1, trace.getTimeRange().getStartTime().getValue());
162 assertEquals("getRange-end", NB_EVENTS, trace.getTimeRange().getEndTime().getValue());
163 assertEquals("getStartTime", 1, trace.getStartTime().getValue());
164 assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue());
165
166 try {
167 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM), null);
168 testfile = new File(FileLocator.toFileURL(location).toURI());
169 trace = new TmfTraceStub(testfile.toURI().getPath(), BLOCK_SIZE);
170 trace.indexTrace();
171 } catch (final URISyntaxException e) {
172 fail("URISyntaxException");
173 } catch (final IOException e) {
174 fail("IOException");
175 }
176
177 assertFalse ("Open trace", trace == null);
178 assertEquals("getType", TmfEvent.class, trace.getType());
179 assertNull ("getResource", trace.getResource());
180 assertEquals("getPath", testfile.toURI().getPath(), trace.getPath());
181 assertEquals("getCacheSize", BLOCK_SIZE, trace.getCacheSize());
182 assertEquals("getStreamingInterval", 0, trace.getStreamingInterval());
183 assertEquals("getName", TEST_STREAM, trace.getName());
184
185 assertEquals("getNbEvents", NB_EVENTS, trace.getNbEvents());
186 assertEquals("getRange-start", 1, trace.getTimeRange().getStartTime().getValue());
187 assertEquals("getRange-end", NB_EVENTS, trace.getTimeRange().getEndTime().getValue());
188 assertEquals("getStartTime", 1, trace.getStartTime().getValue());
189 assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue());
190 }
191
192 public void testFullConstructor() throws Exception {
193 TmfTraceStub trace = null;
194 File testfile = null;
195 try {
196 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM), null);
197 testfile = new File(FileLocator.toFileURL(location).toURI());
198 trace = new TmfTraceStub(testfile.toURI().getPath(), BLOCK_SIZE, null);
199 trace.indexTrace();
200 } catch (final URISyntaxException e) {
201 fail("URISyntaxException");
202 } catch (final IOException e) {
203 fail("IOException");
204 }
205
206 assertFalse ("Open trace", trace == null);
207 assertEquals("getType", TmfEvent.class, trace.getType());
208 assertNull ("getResource", trace.getResource());
209 assertEquals("getPath", testfile.toURI().getPath(), trace.getPath());
210 assertEquals("getCacheSize", BLOCK_SIZE, trace.getCacheSize());
211 assertEquals("getStreamingInterval", 0, trace.getStreamingInterval());
212 assertEquals("getName", TEST_STREAM, trace.getName());
213
214 assertEquals("getNbEvents", NB_EVENTS, trace.getNbEvents());
215 assertEquals("getRange-start", 1, trace.getTimeRange().getStartTime().getValue());
216 assertEquals("getRange-end", NB_EVENTS, trace.getTimeRange().getEndTime().getValue());
217 assertEquals("getStartTime", 1, trace.getStartTime().getValue());
218 assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue());
219 }
220
221 public void testLiveTraceConstructor() throws Exception {
222 TmfTraceStub trace = null;
223 File testfile = null;
224 final long interval = 100;
225 try {
226 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM), null);
227 testfile = new File(FileLocator.toFileURL(location).toURI());
228 trace = new TmfTraceStub(testfile.toURI().getPath(), BLOCK_SIZE, interval);
229 trace.indexTrace();
230 } catch (final URISyntaxException e) {
231 fail("URISyntaxException");
232 } catch (final IOException e) {
233 fail("IOException");
234 }
235
236 assertFalse ("Open trace", trace == null);
237 assertEquals("getType", TmfEvent.class, trace.getType());
238 assertNull ("getResource", trace.getResource());
239 assertEquals("getPath", testfile.toURI().getPath(), trace.getPath());
240 assertEquals("getCacheSize", BLOCK_SIZE, trace.getCacheSize());
241 assertEquals("getStreamingInterval", interval, trace.getStreamingInterval());
242 assertEquals("getName", TEST_STREAM, trace.getName());
243
244 assertEquals("getNbEvents", NB_EVENTS, trace.getNbEvents());
245 assertEquals("getRange-start", 1, trace.getTimeRange().getStartTime().getValue());
246 assertEquals("getRange-end", NB_EVENTS, trace.getTimeRange().getEndTime().getValue());
247 assertEquals("getStartTime", 1, trace.getStartTime().getValue());
248 assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue());
249 }
250
251 @SuppressWarnings({ "rawtypes", "unchecked" })
252 public void testCopyConstructor() throws Exception {
253 TmfTraceStub original = null;
254 TmfTraceStub trace = null;
255 File testfile = null;
256 try {
257 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM), null);
258 testfile = new File(FileLocator.toFileURL(location).toURI());
259 original = new TmfTraceStub(testfile.toURI().getPath(), BLOCK_SIZE, new TmfCheckpointIndexer(null));
260 trace = new TmfTraceStub(original);
261 trace.indexTrace();
262 } catch (final URISyntaxException e) {
263 fail("URISyntaxException");
264 } catch (final IOException e) {
265 fail("IOException");
266 }
267
268 assertFalse ("Open trace", trace == null);
269 assertEquals("getType", TmfEvent.class, trace.getType());
270 assertNull ("getResource", trace.getResource());
271 assertEquals("getPath", testfile.toURI().getPath(), trace.getPath());
272 assertEquals("getCacheSize", BLOCK_SIZE, trace.getCacheSize());
273 assertEquals("getStreamingInterval", 0, trace.getStreamingInterval());
274 assertEquals("getName", TEST_STREAM, trace.getName());
275
276 assertEquals("getNbEvents", NB_EVENTS, trace.getNbEvents());
277 assertEquals("getRange-start", 1, trace.getTimeRange().getStartTime().getValue());
278 assertEquals("getRange-end", NB_EVENTS, trace.getTimeRange().getEndTime().getValue());
279 assertEquals("getStartTime", 1, trace.getStartTime().getValue());
280 assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue());
281
282 // Test the copy of a null trace
283 try {
284 new TmfTraceStub((TmfTraceStub) null);
285 fail("Missing exception");
286 } catch (final IllegalArgumentException e) {
287 // test passed
288 } catch (final Exception e) {
289 fail("Unexpected exception");
290 }
291 }
292
293 // ------------------------------------------------------------------------
294 // Trace initialization
295 // ------------------------------------------------------------------------
296
297 public void testInitializeNullPath() throws Exception {
298
299 // Instantiate an "empty" trace
300 final TmfTraceStub trace = new TmfTraceStub();
301
302 try {
303 trace.initialize(null, null, TmfEvent.class);
304 fail("TmfTrace.initialize() - no exception thrown");
305 } catch (TmfTraceException e) {
306 // Success
307 } catch (Exception e) {
308 fail("TmfTrace.initialize() - wrong exception thrown");
309 }
310 }
311
312 public void testInitializeSimplePath() throws Exception {
313
314 // Instantiate an "empty" trace
315 final TmfTraceStub trace = new TmfTraceStub();
316
317 // Path == trace name
318 String path = "TraceName";
319 try {
320 trace.initialize(null, path, TmfEvent.class);
321 } catch (Exception e) {
322 fail("TmfTrace.initialize() - Exception thrown");
323 }
324
325 assertFalse ("Open trace", trace == null);
326 assertEquals("getType", TmfEvent.class, trace.getType());
327 assertNull ("getResource", trace.getResource());
328 assertEquals("getPath", path, trace.getPath());
329 assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize());
330 assertEquals("getStreamingInterval", 0, trace.getStreamingInterval());
331 assertEquals("getName", path, trace.getName());
332
333 assertEquals("getNbEvents", 0, trace.getNbEvents());
334 assertEquals("getRange-start", Long.MAX_VALUE, trace.getTimeRange().getStartTime().getValue());
335 assertEquals("getRange-end", Long.MIN_VALUE, trace.getTimeRange().getEndTime().getValue());
336 assertEquals("getStartTime", Long.MAX_VALUE, trace.getStartTime().getValue());
337 assertEquals("getEndTime", Long.MIN_VALUE, trace.getEndTime().getValue());
338 }
339
340 public void testInitializeNormalPath() throws Exception {
341
342 // Instantiate an "empty" trace
343 final TmfTraceStub trace = new TmfTraceStub();
344
345 // Path == trace name
346 String name = "TraceName";
347 String path = "/my/trace/path/" + name;
348 try {
349 trace.initialize(null, path, TmfEvent.class);
350 } catch (Exception e) {
351 fail("TmfTrace.initialize() - Exception thrown");
352 }
353
354 assertFalse ("Open trace", trace == null);
355 assertEquals("getType", TmfEvent.class, trace.getType());
356 assertNull ("getResource", trace.getResource());
357 assertEquals("getPath", path, trace.getPath());
358 assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize());
359 assertEquals("getStreamingInterval", 0, trace.getStreamingInterval());
360 assertEquals("getName", name, trace.getName());
361
362 assertEquals("getNbEvents", 0, trace.getNbEvents());
363 assertEquals("getRange-start", Long.MAX_VALUE, trace.getTimeRange().getStartTime().getValue());
364 assertEquals("getRange-end", Long.MIN_VALUE, trace.getTimeRange().getEndTime().getValue());
365 assertEquals("getStartTime", Long.MAX_VALUE, trace.getStartTime().getValue());
366 assertEquals("getEndTime", Long.MIN_VALUE, trace.getEndTime().getValue());
367 }
368
369 public void testInitTrace() throws Exception {
370
371 // Instantiate an "empty" trace
372 final TmfTraceStub trace = new TmfTraceStub();
373
374 assertFalse ("Open trace", trace == null);
375 assertNull ("getType", trace.getType());
376 assertNull ("getResource", trace.getResource());
377 assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize());
378 assertEquals("getStreamingInterval", 0, trace.getStreamingInterval());
379 assertEquals("getName", "", trace.getName());
380
381 assertEquals("getNbEvents", 0, trace.getNbEvents());
382 assertEquals("getRange-start", Long.MAX_VALUE, trace.getTimeRange().getStartTime().getValue());
383 assertEquals("getRange-end", Long.MIN_VALUE, trace.getTimeRange().getEndTime().getValue());
384 assertEquals("getStartTime", Long.MAX_VALUE, trace.getStartTime().getValue());
385 assertEquals("getEndTime", Long.MIN_VALUE, trace.getEndTime().getValue());
386
387 // Validate
388 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM), null);
389 final File testfile = new File(FileLocator.toFileURL(location).toURI());
390 assertTrue("validate", trace.validate(null, testfile.getPath()));
391
392 // InitTrace and wait for indexing completion...
393 trace.initTrace(null, testfile.toURI().getPath(), TmfEvent.class);
394 trace.indexTrace();
395 int nbSecs = 0;
396 while (trace.getNbEvents() < NB_EVENTS && nbSecs < 10) {
397 Thread.sleep(1000);
398 nbSecs++;
399 }
400 if (trace.getNbEvents() < NB_EVENTS) {
401 fail("indexing");
402 }
403
404 assertEquals("getType", TmfEvent.class, trace.getType());
405 assertNull ("getResource", trace.getResource());
406 assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize());
407 assertEquals("getStreamingInterval", 0, trace.getStreamingInterval());
408 assertEquals("getName", TEST_STREAM, trace.getName());
409
410 assertEquals("getNbEvents", NB_EVENTS, trace.getNbEvents());
411 assertEquals("getRange-start", 1, trace.getTimeRange().getStartTime().getValue());
412 assertEquals("getRange-end", NB_EVENTS, trace.getTimeRange().getEndTime().getValue());
413 assertEquals("getStartTime", 1, trace.getStartTime().getValue());
414 assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue());
415 }
416
417 // ------------------------------------------------------------------------
418 // Set/Get streaming interval
419 // ------------------------------------------------------------------------
420
421 public void testSetStreamingInterval() throws Exception {
422 final TmfTraceStub trace = new TmfTraceStub(fTrace);
423
424 long interval = 0;
425 assertEquals("getStreamingInterval", interval, trace.getStreamingInterval());
426
427 interval = 100;
428 trace.setStreamingInterval(interval);
429 assertEquals("getStreamingInterval", interval, trace.getStreamingInterval());
430
431 interval = -1;
432 trace.setStreamingInterval(interval);
433 assertEquals("getStreamingInterval", 0, trace.getStreamingInterval());
434
435 interval = 0;
436 trace.setStreamingInterval(interval);
437 assertEquals("getStreamingInterval", interval, trace.getStreamingInterval());
438
439 trace.dispose();
440 }
441
442 // ------------------------------------------------------------------------
443 // Set/Get time range
444 // ------------------------------------------------------------------------
445
446 public void testSetTimeRange() throws Exception {
447 final TmfTraceStub trace = new TmfTraceStub(fTrace);
448 trace.indexTrace();
449
450 assertEquals("getRange-start", 1, trace.getTimeRange().getStartTime().getValue());
451 assertEquals("getRange-end", NB_EVENTS, trace.getTimeRange().getEndTime().getValue());
452 assertEquals("getStartTime", 1, trace.getStartTime().getValue());
453 assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue());
454
455 trace.setTimeRange(new TmfTimeRange(new TmfTimestamp(100), new TmfTimestamp(200)));
456 assertEquals("setTimeRange", 100, trace.getTimeRange().getStartTime().getValue());
457 assertEquals("setTimeRange", 200, trace.getTimeRange().getEndTime().getValue());
458 assertEquals("setTimeRange", 100, trace.getStartTime().getValue());
459 assertEquals("setTimeRange", 200, trace.getEndTime().getValue());
460
461 trace.dispose();
462 }
463
464 public void testSetStartTime() throws Exception {
465 final TmfTraceStub trace = new TmfTraceStub(fTrace);
466 trace.indexTrace();
467
468 assertEquals("getRange-start", 1, trace.getTimeRange().getStartTime().getValue());
469 assertEquals("getRange-end", NB_EVENTS, trace.getTimeRange().getEndTime().getValue());
470 assertEquals("getStartTime", 1, trace.getStartTime().getValue());
471 assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue());
472
473 trace.setStartTime(new TmfTimestamp(100));
474 assertEquals("setStartTime", 100, trace.getTimeRange().getStartTime().getValue());
475 assertEquals("setStartTime", NB_EVENTS, trace.getTimeRange().getEndTime().getValue());
476 assertEquals("setStartTime", 100, trace.getStartTime().getValue());
477 assertEquals("setStartTime", NB_EVENTS, trace.getEndTime().getValue());
478
479 trace.dispose();
480 }
481
482 public void testSetEndTime() throws Exception {
483 final TmfTraceStub trace = new TmfTraceStub(fTrace);
484 trace.indexTrace();
485
486 assertEquals("getRange-start", 1, trace.getTimeRange().getStartTime().getValue());
487 assertEquals("getRange-end", NB_EVENTS, trace.getTimeRange().getEndTime().getValue());
488 assertEquals("getStartTime", 1, trace.getStartTime().getValue());
489 assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue());
490
491 trace.setEndTime(new TmfTimestamp(100));
492 assertEquals("setEndTime", 1, trace.getTimeRange().getStartTime().getValue());
493 assertEquals("setEndTime", 100, trace.getTimeRange().getEndTime().getValue());
494 assertEquals("setEndTime", 1, trace.getStartTime().getValue());
495 assertEquals("setEndTime", 100, trace.getEndTime().getValue());
496
497 trace.dispose();
498 }
499
500 public void testSetNbEvents() throws Exception {
501 final TmfTraceStub trace = new TmfTraceStub(fTrace);
502 trace.indexTrace();
503
504 assertEquals("getNbEvents", NB_EVENTS, trace.getNbEvents());
505
506 trace.setNbEvents(0);
507 assertEquals("getNbEvents", 0, trace.getNbEvents());
508
509 trace.setNbEvents(-1);
510 assertEquals("getNbEvents", 0, trace.getNbEvents());
511
512 trace.setNbEvents(NB_EVENTS + 1);
513 assertEquals("getNbEvents", NB_EVENTS + 1, trace.getNbEvents());
514
515 trace.setNbEvents(NB_EVENTS);
516 assertEquals("getNbEvents", NB_EVENTS, trace.getNbEvents());
517
518 trace.dispose();
519 }
520
521 // ------------------------------------------------------------------------
522 // seekEvent on location (note: does not reliably set the rank)
523 // ------------------------------------------------------------------------
524
525 public void testSeekEventOnCacheBoundary() throws Exception {
526
527 // Position trace at event rank 0
528 ITmfContext context = fTrace.seekEvent(0);
529 ITmfEvent event = fTrace.parseEvent(context);
530 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
531 assertEquals("Event rank", 0, context.getRank());
532
533 context = fTrace.seekEvent(context.getLocation());
534 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
535
536 event = fTrace.parseEvent(context);
537 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
538 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
539
540 event = fTrace.getNext(context);
541 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
542 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
543
544 // Position trace at event rank 1000
545 ITmfContext tmpContext = fTrace.seekEvent(new TmfTimestamp(1001, SCALE, 0));
546 context = fTrace.seekEvent(tmpContext.getLocation());
547 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
548
549 event = fTrace.parseEvent(context);
550 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
551 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
552
553 event = fTrace.getNext(context);
554 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
555 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
556
557 // Position trace at event rank 4000
558 tmpContext = fTrace.seekEvent(new TmfTimestamp(4001, SCALE, 0));
559 context = fTrace.seekEvent(tmpContext.getLocation());
560 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
561
562 event = fTrace.parseEvent(context);
563 assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
564 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
565
566 event = fTrace.getNext(context);
567 assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
568 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
569 }
570
571 public void testSeekEventNotOnCacheBoundary() throws Exception {
572
573 // Position trace at event rank 9
574 ITmfContext tmpContext = fTrace.seekEvent(new TmfTimestamp(10, SCALE, 0));
575 TmfContext context = fTrace.seekEvent(tmpContext.getLocation());
576 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
577
578 ITmfEvent event = fTrace.parseEvent(context);
579 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
580 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
581
582 event = fTrace.getNext(context);
583 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
584 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
585
586 // Position trace at event rank 999
587 tmpContext = fTrace.seekEvent(new TmfTimestamp(1000, SCALE, 0));
588 context = fTrace.seekEvent(tmpContext.getLocation());
589 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
590
591 event = fTrace.parseEvent(context);
592 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
593 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
594
595 event = fTrace.getNext(context);
596 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
597 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
598
599 // Position trace at event rank 1001
600 tmpContext = fTrace.seekEvent(new TmfTimestamp(1002, SCALE, 0));
601 context = fTrace.seekEvent(tmpContext.getLocation());
602 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
603
604 event = fTrace.parseEvent(context);
605 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
606 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
607
608 event = fTrace.getNext(context);
609 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
610 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
611
612 // Position trace at event rank 4500
613 tmpContext = fTrace.seekEvent(new TmfTimestamp(4501, SCALE, 0));
614 context = fTrace.seekEvent(tmpContext.getLocation());
615 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
616
617 event = fTrace.parseEvent(context);
618 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
619 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
620
621 event = fTrace.getNext(context);
622 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
623 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
624 }
625
626 public void testSeekEventOutOfScope() throws Exception {
627
628 // Position trace at beginning
629 ITmfContext tmpContext = fTrace.seekEvent(0);
630 ITmfContext context = fTrace.seekEvent(tmpContext.getLocation());
631 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
632
633 ITmfEvent event = fTrace.parseEvent(context);
634 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
635 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
636
637 event = fTrace.getNext(context);
638 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
639 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
640
641 // Position trace at event passed the end
642 context = fTrace.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0));
643 assertNull("Event timestamp", context.getLocation());
644 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
645
646 event = fTrace.getNext(context);
647 assertNull("Event", event);
648 }
649
650 // ------------------------------------------------------------------------
651 // seekEvent on timestamp (note: does not reliably set the rank)
652 // ------------------------------------------------------------------------
653
654 public void testSeekEventOnNullTimestamp() throws Exception {
655
656 // Position trace at event rank 0
657 ITmfContext context = fTrace.seekEvent((ITmfTimestamp) null);
658 assertEquals("Event rank", 0, context.getRank());
659
660 ITmfEvent event = fTrace.parseEvent(context);
661 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
662 assertEquals("Event rank", 0, context.getRank());
663 }
664
665 public void testSeekEventOnTimestampOnCacheBoundary() throws Exception {
666
667 // Position trace at event rank 0
668 ITmfContext context = fTrace.seekEvent(new TmfTimestamp(1, SCALE, 0));
669 assertEquals("Event rank", 0, context.getRank());
670
671 ITmfEvent event = fTrace.parseEvent(context);
672 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
673 assertEquals("Event rank", 0, context.getRank());
674
675 event = fTrace.getNext(context);
676 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
677 assertEquals("Event rank", 1, context.getRank());
678
679 // Position trace at event rank 1000
680 context = fTrace.seekEvent(new TmfTimestamp(1001, SCALE, 0));
681 assertEquals("Event rank", 1000, context.getRank());
682
683 event = fTrace.parseEvent(context);
684 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
685 assertEquals("Event rank", 1000, context.getRank());
686
687 event = fTrace.getNext(context);
688 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
689 assertEquals("Event rank", 1001, context.getRank());
690
691 // Position trace at event rank 4000
692 context = fTrace.seekEvent(new TmfTimestamp(4001, SCALE, 0));
693 assertEquals("Event rank", 4000, context.getRank());
694
695 event = fTrace.parseEvent(context);
696 assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
697 assertEquals("Event rank", 4000, context.getRank());
698
699 event = fTrace.getNext(context);
700 assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
701 assertEquals("Event rank", 4001, context.getRank());
702 }
703
704 public void testSeekEventOnTimestampNotOnCacheBoundary() throws Exception {
705
706 // Position trace at event rank 1
707 ITmfContext context = fTrace.seekEvent(new TmfTimestamp(2, SCALE, 0));
708 assertEquals("Event rank", 1, context.getRank());
709
710 ITmfEvent event = fTrace.parseEvent(context);
711 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
712 assertEquals("Event rank", 1, context.getRank());
713
714 event = fTrace.getNext(context);
715 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
716 assertEquals("Event rank", 2, context.getRank());
717
718 // Position trace at event rank 9
719 context = fTrace.seekEvent(new TmfTimestamp(10, SCALE, 0));
720 assertEquals("Event rank", 9, context.getRank());
721
722 event = fTrace.parseEvent(context);
723 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
724 assertEquals("Event rank", 9, context.getRank());
725
726 event = fTrace.getNext(context);
727 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
728 assertEquals("Event rank", 10, context.getRank());
729
730 // Position trace at event rank 999
731 context = fTrace.seekEvent(new TmfTimestamp(1000, SCALE, 0));
732 assertEquals("Event rank", 999, context.getRank());
733
734 event = fTrace.parseEvent(context);
735 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
736 assertEquals("Event rank", 999, context.getRank());
737
738 event = fTrace.getNext(context);
739 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
740 assertEquals("Event rank", 1000, context.getRank());
741
742 // Position trace at event rank 1001
743 context = fTrace.seekEvent(new TmfTimestamp(1002, SCALE, 0));
744 assertEquals("Event rank", 1001, context.getRank());
745
746 event = fTrace.parseEvent(context);
747 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
748 assertEquals("Event rank", 1001, context.getRank());
749
750 event = fTrace.getNext(context);
751 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
752 assertEquals("Event rank", 1002, context.getRank());
753
754 // Position trace at event rank 4500
755 context = fTrace.seekEvent(new TmfTimestamp(4501, SCALE, 0));
756 assertEquals("Event rank", 4500, context.getRank());
757
758 event = fTrace.parseEvent(context);
759 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
760 assertEquals("Event rank", 4500, context.getRank());
761
762 event = fTrace.getNext(context);
763 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
764 assertEquals("Event rank", 4501, context.getRank());
765 }
766
767 public void testSeekEventOnTimestampOutOfScope() throws Exception {
768
769 // Position trace at beginning
770 ITmfContext context = fTrace.seekEvent(new TmfTimestamp(-1, SCALE, 0));
771 assertEquals("Event rank", 0, context.getRank());
772
773 ITmfEvent event = fTrace.parseEvent(context);
774 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
775 assertEquals("Event rank", 0, context.getRank());
776
777 event = fTrace.getNext(context);
778 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
779 assertEquals("Event rank", 1, context.getRank());
780
781 // Position trace at event passed the end
782 context = fTrace.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0));
783 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
784
785 event = fTrace.parseEvent(context);
786 assertEquals("Event timestamp", null, event);
787 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
788
789 event = fTrace.getNext(context);
790 assertEquals("Event timestamp", null, event);
791 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
792 }
793
794 // ------------------------------------------------------------------------
795 // seekEvent on rank
796 // ------------------------------------------------------------------------
797
798 public void testSeekEventOnNegativeRank() throws Exception {
799
800 // Position trace at event rank 0
801 ITmfContext context = fTrace.seekEvent(-1);
802 assertEquals("Event rank", 0, context.getRank());
803
804 ITmfEvent event = fTrace.parseEvent(context);
805 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
806 assertEquals("Event rank", 0, context.getRank());
807 }
808
809 public void testSeekOnRankOnCacheBoundary() throws Exception {
810
811 // On lower bound, returns the first event (ts = 1)
812 ITmfContext context = fTrace.seekEvent(0);
813 assertEquals("Event rank", 0, context.getRank());
814
815 ITmfEvent event = fTrace.parseEvent(context);
816 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
817 assertEquals("Event rank", 0, context.getRank());
818
819 event = fTrace.getNext(context);
820 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
821 assertEquals("Event rank", 1, context.getRank());
822
823 // Position trace at event rank 1000
824 context = fTrace.seekEvent(1000);
825 assertEquals("Event rank", 1000, context.getRank());
826
827 event = fTrace.parseEvent(context);
828 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
829 assertEquals("Event rank", 1000, context.getRank());
830
831 event = fTrace.getNext(context);
832 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
833 assertEquals("Event rank", 1001, context.getRank());
834
835 // Position trace at event rank 4000
836 context = fTrace.seekEvent(4000);
837 assertEquals("Event rank", 4000, context.getRank());
838
839 event = fTrace.parseEvent(context);
840 assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
841 assertEquals("Event rank", 4000, context.getRank());
842
843 event = fTrace.getNext(context);
844 assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
845 assertEquals("Event rank", 4001, context.getRank());
846 }
847
848 public void testSeekOnRankNotOnCacheBoundary() throws Exception {
849
850 // Position trace at event rank 9
851 ITmfContext context = fTrace.seekEvent(9);
852 assertEquals("Event rank", 9, context.getRank());
853
854 ITmfEvent event = fTrace.parseEvent(context);
855 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
856 assertEquals("Event rank", 9, context.getRank());
857
858 event = fTrace.getNext(context);
859 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
860 assertEquals("Event rank", 10, context.getRank());
861
862 // Position trace at event rank 999
863 context = fTrace.seekEvent(999);
864 assertEquals("Event rank", 999, context.getRank());
865
866 event = fTrace.parseEvent(context);
867 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
868 assertEquals("Event rank", 999, context.getRank());
869
870 event = fTrace.getNext(context);
871 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
872 assertEquals("Event rank", 1000, context.getRank());
873
874 // Position trace at event rank 1001
875 context = fTrace.seekEvent(1001);
876 assertEquals("Event rank", 1001, context.getRank());
877
878 event = fTrace.parseEvent(context);
879 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
880 assertEquals("Event rank", 1001, context.getRank());
881
882 event = fTrace.getNext(context);
883 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
884 assertEquals("Event rank", 1002, context.getRank());
885
886 // Position trace at event rank 4500
887 context = fTrace.seekEvent(4500);
888 assertEquals("Event rank", 4500, context.getRank());
889
890 event = fTrace.parseEvent(context);
891 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
892 assertEquals("Event rank", 4500, context.getRank());
893
894 event = fTrace.getNext(context);
895 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
896 assertEquals("Event rank", 4501, context.getRank());
897 }
898
899 public void testSeekEventOnRankOutOfScope() throws Exception {
900
901 // Position trace at beginning
902 ITmfContext context = fTrace.seekEvent(-1);
903 assertEquals("Event rank", 0, context.getRank());
904
905 ITmfEvent event = fTrace.parseEvent(context);
906 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
907 assertEquals("Event rank", 0, context.getRank());
908
909 event = fTrace.getNext(context);
910 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
911 assertEquals("Event rank", 1, context.getRank());
912
913 // Position trace at event passed the end
914 context = fTrace.seekEvent(NB_EVENTS);
915 assertEquals("Event rank", NB_EVENTS, context.getRank());
916
917 event = fTrace.parseEvent(context);
918 assertNull("Event", event);
919 assertEquals("Event rank", NB_EVENTS, context.getRank());
920
921 event = fTrace.getNext(context);
922 assertNull("Event", event);
923 assertEquals("Event rank", NB_EVENTS, context.getRank());
924 }
925
926 // ------------------------------------------------------------------------
927 // parseEvent - make sure parseEvent doesn't update the context
928 // ------------------------------------------------------------------------
929
930 public void testParseEvent() throws Exception {
931
932 final int NB_READS = 20;
933
934 // On lower bound, returns the first event (ts = 0)
935 final TmfContext context = (TmfContext) fTrace.seekEvent(new TmfTimestamp(0, SCALE, 0));
936 TmfContext svContext = new TmfContext(context);
937
938 ITmfEvent event = fTrace.parseEvent(context);
939 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
940 assertEquals("Event rank", 0, context.getRank());
941 assertTrue("parseEvent", context.equals(svContext));
942
943 event = fTrace.parseEvent(context);
944 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
945 assertEquals("Event rank", 0, context.getRank());
946 assertTrue("parseEvent", context.equals(svContext));
947
948 event = fTrace.parseEvent(context);
949 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
950 assertEquals("Event rank", 0, context.getRank());
951 assertTrue("parseEvent", context.equals(svContext));
952
953 // Position the trace at event NB_READS
954 for (int i = 1; i < NB_READS; i++) {
955 event = fTrace.getNext(context);
956 assertEquals("Event timestamp", i, event.getTimestamp().getValue());
957 }
958
959 svContext = new TmfContext(context);
960 event = fTrace.parseEvent(context);
961 assertEquals("Event timestamp", NB_READS, event.getTimestamp().getValue());
962 assertEquals("Event rank", NB_READS -1 , context.getRank());
963 assertTrue("parseEvent", context.equals(svContext));
964
965 event = fTrace.parseEvent(context);
966 assertEquals("Event timestamp", NB_READS, event.getTimestamp().getValue());
967 assertEquals("Event rank", NB_READS - 1, context.getRank());
968 assertTrue("parseEvent", context.equals(svContext));
969 }
970
971 // ------------------------------------------------------------------------
972 // getNext - updates the context
973 // ------------------------------------------------------------------------
974
975 public void testGetNext() throws Exception {
976
977 final int NB_READS = 20;
978
979 // On lower bound, returns the first event (ts = 1)
980 final ITmfContext context = fTrace.seekEvent(new TmfTimestamp(0, SCALE, 0));
981
982 // Read NB_EVENTS
983 ITmfEvent event;
984 for (int i = 0; i < NB_READS; i++) {
985 event = fTrace.getNext(context);
986 assertEquals("Event timestamp", i + 1, event.getTimestamp().getValue());
987 assertEquals("Event rank", i + 1, context.getRank());
988 }
989
990 // Make sure we stay positioned
991 event = fTrace.parseEvent(context);
992 assertEquals("Event timestamp", NB_READS + 1, event.getTimestamp().getValue());
993 assertEquals("Event rank", NB_READS, context.getRank());
994 }
995
996 public void testGetNextLocation() throws Exception {
997
998 ITmfContext context1 = fTrace.seekEvent(0);
999 fTrace.getNext(context1);
1000 ITmfLocation<?> location = context1.getLocation().clone();
1001 ITmfEvent event1 = fTrace.getNext(context1);
1002 ITmfContext context2 = fTrace.seekEvent(location);
1003 ITmfEvent event2 = fTrace.getNext(context2);
1004 assertEquals("Event timestamp", event1.getTimestamp().getValue(), event2.getTimestamp().getValue());
1005 }
1006
1007 public void testGetNextEndLocation() throws Exception {
1008 ITmfContext context1 = fTrace.seekEvent(fTrace.getNbEvents() - 1);
1009 fTrace.getNext(context1);
1010 ITmfLocation<?> location = context1.getLocation().clone();
1011 ITmfContext context2 = fTrace.seekEvent(location);
1012 ITmfEvent event = fTrace.getNext(context2);
1013 assertNull("Event", event);
1014 }
1015
1016 // ------------------------------------------------------------------------
1017 // processRequest
1018 // ------------------------------------------------------------------------
1019
1020 @SuppressWarnings("unchecked")
1021 public void testProcessEventRequestForAllEvents() throws Exception {
1022 final int BLOCK_SIZE = 1;
1023 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
1024
1025 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
1026 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) {
1027 @Override
1028 public void handleData(final TmfEvent event) {
1029 super.handleData(event);
1030 requestedEvents.add(event);
1031 }
1032 };
1033 final ITmfDataProvider<TmfEvent>[] providers = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class);
1034 providers[0].sendRequest(request);
1035 request.waitForCompletion();
1036
1037 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
1038 assertTrue("isCompleted", request.isCompleted());
1039 assertFalse("isCancelled", request.isCancelled());
1040
1041 // Ensure that we have distinct events.
1042 // Don't go overboard: we are not validating the stub!
1043 for (int i = 0; i < NB_EVENTS; i++) {
1044 assertEquals("Distinct events", i + 1, requestedEvents.get(i).getTimestamp().getValue());
1045 }
1046 }
1047
1048 @SuppressWarnings("unchecked")
1049 public void testProcessEventRequestForNbEvents() throws Exception {
1050 final int BLOCK_SIZE = 100;
1051 final int NB_EVENTS = 1000;
1052 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
1053
1054 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
1055 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) {
1056 @Override
1057 public void handleData(final TmfEvent event) {
1058 super.handleData(event);
1059 requestedEvents.add(event);
1060 }
1061 };
1062 final ITmfDataProvider<TmfEvent>[] providers = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class);
1063 providers[0].sendRequest(request);
1064 request.waitForCompletion();
1065
1066 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
1067 assertTrue("isCompleted", request.isCompleted());
1068 assertFalse("isCancelled", request.isCancelled());
1069
1070 // Ensure that we have distinct events.
1071 // Don't go overboard: we are not validating the stub!
1072 for (int i = 0; i < NB_EVENTS; i++) {
1073 assertEquals("Distinct events", i + 1, requestedEvents.get(i).getTimestamp().getValue());
1074 }
1075 }
1076
1077 @SuppressWarnings("unchecked")
1078 public void testProcessEventRequestForSomeEvents() throws Exception {
1079 final int BLOCK_SIZE = 1;
1080 final long startTime = 100;
1081 final int NB_EVENTS = 1000;
1082 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
1083
1084 final TmfTimeRange range = new TmfTimeRange(new TmfTimestamp(startTime, SCALE), TmfTimestamp.BIG_CRUNCH);
1085 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) {
1086 @Override
1087 public void handleData(final TmfEvent event) {
1088 super.handleData(event);
1089 requestedEvents.add(event);
1090 }
1091 };
1092 final ITmfDataProvider<TmfEvent>[] providers = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class);
1093 providers[0].sendRequest(request);
1094 request.waitForCompletion();
1095
1096 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
1097 assertTrue("isCompleted", request.isCompleted());
1098 assertFalse("isCancelled", request.isCancelled());
1099
1100 // Ensure that we have distinct events.
1101 // Don't go overboard: we are not validating the stub!
1102 for (int i = 0; i < NB_EVENTS; i++) {
1103 assertEquals("Distinct events", startTime + i, requestedEvents.get(i).getTimestamp().getValue());
1104 }
1105 }
1106
1107 @SuppressWarnings("unchecked")
1108 public void testProcessEventRequestForOtherEvents() throws Exception {
1109 final int BLOCK_SIZE = 1;
1110 final int startIndex = 99;
1111 final long startTime = 100;
1112 final int NB_EVENTS = 1000;
1113 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
1114
1115 final TmfTimeRange range = new TmfTimeRange(new TmfTimestamp(startTime, SCALE), TmfTimestamp.BIG_CRUNCH);
1116 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, startIndex, NB_EVENTS, BLOCK_SIZE) {
1117 @Override
1118 public void handleData(final TmfEvent event) {
1119 super.handleData(event);
1120 requestedEvents.add(event);
1121 }
1122 };
1123 final ITmfDataProvider<TmfEvent>[] providers = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class);
1124 providers[0].sendRequest(request);
1125 request.waitForCompletion();
1126
1127 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
1128 assertTrue("isCompleted", request.isCompleted());
1129 assertFalse("isCancelled", request.isCancelled());
1130
1131 // Ensure that we have distinct events.
1132 // Don't go overboard: we are not validating the stub!
1133 for (int i = 0; i < NB_EVENTS; i++) {
1134 assertEquals("Distinct events", startTime + i, requestedEvents.get(i).getTimestamp().getValue());
1135 }
1136 }
1137
1138 @SuppressWarnings("unchecked")
1139 public void testProcessDataRequestForSomeEvents() throws Exception {
1140 final int startIndex = 100;
1141 final int NB_EVENTS = 1000;
1142 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
1143
1144 final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(TmfEvent.class, startIndex, NB_EVENTS) {
1145 @Override
1146 public void handleData(final TmfEvent event) {
1147 super.handleData(event);
1148 requestedEvents.add(event);
1149 }
1150 };
1151 final ITmfDataProvider<TmfEvent>[] providers = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class);
1152 providers[0].sendRequest(request);
1153 request.waitForCompletion();
1154
1155 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
1156 assertTrue("isCompleted", request.isCompleted());
1157 assertFalse("isCancelled", request.isCancelled());
1158
1159 // Ensure that we have distinct events.
1160 // Don't go overboard: we are not validating the stub!
1161 for (int i = 0; i < NB_EVENTS; i++) {
1162 assertEquals("Distinct events", startIndex + 1 + i, requestedEvents.get(i).getTimestamp().getValue());
1163 }
1164 }
1165
1166 // ------------------------------------------------------------------------
1167 // cancel
1168 // ------------------------------------------------------------------------
1169
1170 @SuppressWarnings("unchecked")
1171 public void testCancel() throws Exception {
1172 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
1173
1174 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
1175 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) {
1176 int nbRead = 0;
1177 @Override
1178 public void handleData(final TmfEvent event) {
1179 super.handleData(event);
1180 requestedEvents.add(event);
1181 if (++nbRead == BLOCK_SIZE) {
1182 cancel();
1183 }
1184 }
1185 };
1186 final ITmfDataProvider<TmfEvent>[] providers = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class);
1187 providers[0].sendRequest(request);
1188 request.waitForCompletion();
1189
1190 assertEquals("nbEvents", BLOCK_SIZE, requestedEvents.size());
1191 assertTrue("isCompleted", request.isCompleted());
1192 assertTrue("isCancelled", request.isCancelled());
1193 }
1194
1195 // ------------------------------------------------------------------------
1196 // toString
1197 // ------------------------------------------------------------------------
1198
1199 public void testDefaultTmfTraceStub() throws Exception {
1200 assertFalse ("Open trace", fTrace == null);
1201 assertEquals("getType", TmfEvent.class, fTrace.getType());
1202 assertNull ("getResource", fTrace.getResource());
1203 assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize());
1204 assertEquals("getStreamingInterval", 0, fTrace.getStreamingInterval());
1205 assertEquals("getName", TEST_STREAM, fTrace.getName());
1206
1207 assertEquals("getNbEvents", NB_EVENTS, fTrace.getNbEvents());
1208 assertEquals("getRange-start", 1, fTrace.getTimeRange().getStartTime().getValue());
1209 assertEquals("getRange-end", NB_EVENTS, fTrace.getTimeRange().getEndTime().getValue());
1210 assertEquals("getStartTime", 1, fTrace.getStartTime().getValue());
1211 assertEquals("getEndTime", NB_EVENTS, fTrace.getEndTime().getValue());
1212
1213 String expected = "TmfTrace [fPath=" + fTrace.getPath() + ", fCacheSize=" + fTrace.getCacheSize() +
1214 ", fNbEvents=" + fTrace.getNbEvents() + ", fStartTime=" + fTrace.getStartTime() +
1215 ", fEndTime=" + fTrace.getEndTime() + ", fStreamingInterval=" + fTrace.getStreamingInterval() +
1216 "]";
1217 assertEquals("toString", expected, fTrace.toString());
1218 }
1219
1220 }
This page took 0.074673 seconds and 6 git commands to generate.