1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 20112 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adapted for TMF Trace Model 1.0
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
;
17 import java
.io
.IOException
;
18 import java
.net
.URISyntaxException
;
20 import java
.util
.Vector
;
22 import junit
.framework
.TestCase
;
24 import org
.eclipse
.core
.runtime
.FileLocator
;
25 import org
.eclipse
.core
.runtime
.Path
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.component
.ITmfDataProvider
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfProviderManager
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceIndexer
;
38 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
41 * <b><u>TmfTraceTest</u></b>
43 * Test suite for the TmfTrace class.
45 @SuppressWarnings("nls")
46 public class TmfTraceTest
extends TestCase
{
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 private static final String DIRECTORY
= "testfiles";
53 private static final String TEST_STREAM
= "A-Test-10K";
54 private static final int BLOCK_SIZE
= 500;
55 private static final int NB_EVENTS
= 10000;
56 private static TmfTraceStub fTrace
= null;
58 private static int SCALE
= -3;
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
64 public TmfTraceTest(final String name
) throws Exception
{
69 protected void setUp() throws Exception
{
71 fTrace
= setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
75 protected void tearDown() throws Exception
{
81 // ------------------------------------------------------------------------
83 // ------------------------------------------------------------------------
85 private TmfTraceStub
setupTrace(final String path
) {
88 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
89 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
90 fTrace
= new TmfTraceStub(test
.toURI().getPath(), BLOCK_SIZE
);
92 } catch (final URISyntaxException e
) {
94 } catch (final IOException e
) {
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
105 public void testStandardConstructor() throws Exception
{
106 TmfTraceStub trace
= null;
107 File testfile
= null;
109 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM
), null);
110 testfile
= new File(FileLocator
.toFileURL(location
).toURI());
111 trace
= new TmfTraceStub(testfile
.toURI().getPath());
113 } catch (final URISyntaxException e
) {
114 fail("URISyntaxException");
115 } catch (final IOException e
) {
119 assertFalse ("Open trace", trace
== null);
120 assertEquals("getType", TmfEvent
.class, trace
.getType());
121 assertNull ("getResource", trace
.getResource());
122 assertEquals("getPath", testfile
.toURI().getPath(), trace
.getPath());
123 assertEquals("getCacheSize", TmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, trace
.getCacheSize());
124 assertEquals("getStreamingInterval", 0, trace
.getStreamingInterval());
125 assertEquals("getName", TEST_STREAM
, trace
.getName());
127 assertEquals("getNbEvents", NB_EVENTS
, trace
.getNbEvents());
128 assertEquals("getRange-start", 1, trace
.getTimeRange().getStartTime().getValue());
129 assertEquals("getRange-end", NB_EVENTS
, trace
.getTimeRange().getEndTime().getValue());
130 assertEquals("getStartTime", 1, trace
.getStartTime().getValue());
131 assertEquals("getEndTime", NB_EVENTS
, trace
.getEndTime().getValue());
134 public void testStandardConstructorCacheSize() throws Exception
{
135 TmfTraceStub trace
= null;
136 File testfile
= null;
138 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM
), null);
139 testfile
= new File(FileLocator
.toFileURL(location
).toURI());
140 trace
= new TmfTraceStub(testfile
.toURI().getPath(), 0);
142 } catch (final URISyntaxException e
) {
143 fail("URISyntaxException");
144 } catch (final IOException e
) {
148 assertFalse ("Open trace", trace
== null);
149 assertEquals("getType", TmfEvent
.class, trace
.getType());
150 assertNull ("getResource", trace
.getResource());
151 assertEquals("getPath", testfile
.toURI().getPath(), trace
.getPath());
152 assertEquals("getCacheSize", TmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, trace
.getCacheSize());
153 assertEquals("getStreamingInterval", 0, trace
.getStreamingInterval());
154 assertEquals("getName", TEST_STREAM
, trace
.getName());
156 assertEquals("getNbEvents", NB_EVENTS
, trace
.getNbEvents());
157 assertEquals("getRange-start", 1, trace
.getTimeRange().getStartTime().getValue());
158 assertEquals("getRange-end", NB_EVENTS
, trace
.getTimeRange().getEndTime().getValue());
159 assertEquals("getStartTime", 1, trace
.getStartTime().getValue());
160 assertEquals("getEndTime", NB_EVENTS
, trace
.getEndTime().getValue());
163 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM
), null);
164 testfile
= new File(FileLocator
.toFileURL(location
).toURI());
165 trace
= new TmfTraceStub(testfile
.toURI().getPath(), BLOCK_SIZE
);
167 } catch (final URISyntaxException e
) {
168 fail("URISyntaxException");
169 } catch (final IOException e
) {
173 assertFalse ("Open trace", trace
== null);
174 assertEquals("getType", TmfEvent
.class, trace
.getType());
175 assertNull ("getResource", trace
.getResource());
176 assertEquals("getPath", testfile
.toURI().getPath(), trace
.getPath());
177 assertEquals("getCacheSize", BLOCK_SIZE
, trace
.getCacheSize());
178 assertEquals("getStreamingInterval", 0, trace
.getStreamingInterval());
179 assertEquals("getName", TEST_STREAM
, trace
.getName());
181 assertEquals("getNbEvents", NB_EVENTS
, trace
.getNbEvents());
182 assertEquals("getRange-start", 1, trace
.getTimeRange().getStartTime().getValue());
183 assertEquals("getRange-end", NB_EVENTS
, trace
.getTimeRange().getEndTime().getValue());
184 assertEquals("getStartTime", 1, trace
.getStartTime().getValue());
185 assertEquals("getEndTime", NB_EVENTS
, trace
.getEndTime().getValue());
188 public void testFullConstructor() throws Exception
{
189 TmfTraceStub trace
= null;
190 File testfile
= null;
192 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM
), null);
193 testfile
= new File(FileLocator
.toFileURL(location
).toURI());
194 trace
= new TmfTraceStub(testfile
.toURI().getPath(), BLOCK_SIZE
, null);
196 } catch (final URISyntaxException e
) {
197 fail("URISyntaxException");
198 } catch (final IOException e
) {
202 assertFalse ("Open trace", trace
== null);
203 assertEquals("getType", TmfEvent
.class, trace
.getType());
204 assertNull ("getResource", trace
.getResource());
205 assertEquals("getPath", testfile
.toURI().getPath(), trace
.getPath());
206 assertEquals("getCacheSize", BLOCK_SIZE
, trace
.getCacheSize());
207 assertEquals("getStreamingInterval", 0, trace
.getStreamingInterval());
208 assertEquals("getName", TEST_STREAM
, trace
.getName());
210 assertEquals("getNbEvents", NB_EVENTS
, trace
.getNbEvents());
211 assertEquals("getRange-start", 1, trace
.getTimeRange().getStartTime().getValue());
212 assertEquals("getRange-end", NB_EVENTS
, trace
.getTimeRange().getEndTime().getValue());
213 assertEquals("getStartTime", 1, trace
.getStartTime().getValue());
214 assertEquals("getEndTime", NB_EVENTS
, trace
.getEndTime().getValue());
217 @SuppressWarnings({ "rawtypes", "unchecked" })
218 public void testCopyConstructor() throws Exception
{
219 TmfTraceStub original
= null;
220 TmfTraceStub trace
= null;
221 File testfile
= null;
223 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM
), null);
224 testfile
= new File(FileLocator
.toFileURL(location
).toURI());
225 original
= new TmfTraceStub(testfile
.toURI().getPath(), BLOCK_SIZE
, new TmfTraceIndexer(null));
226 trace
= new TmfTraceStub(original
);
228 } catch (final URISyntaxException e
) {
229 fail("URISyntaxException");
230 } catch (final IOException e
) {
234 assertFalse ("Open trace", trace
== null);
235 assertEquals("getType", TmfEvent
.class, trace
.getType());
236 assertNull ("getResource", trace
.getResource());
237 assertEquals("getPath", testfile
.toURI().getPath(), trace
.getPath());
238 assertEquals("getCacheSize", BLOCK_SIZE
, trace
.getCacheSize());
239 assertEquals("getStreamingInterval", 0, trace
.getStreamingInterval());
240 assertEquals("getName", TEST_STREAM
, trace
.getName());
242 assertEquals("getNbEvents", NB_EVENTS
, trace
.getNbEvents());
243 assertEquals("getRange-start", 1, trace
.getTimeRange().getStartTime().getValue());
244 assertEquals("getRange-end", NB_EVENTS
, trace
.getTimeRange().getEndTime().getValue());
245 assertEquals("getStartTime", 1, trace
.getStartTime().getValue());
246 assertEquals("getEndTime", NB_EVENTS
, trace
.getEndTime().getValue());
248 // Test the copy of a null trace
250 new TmfTraceStub((TmfTraceStub
) null);
251 fail("Missing exception");
252 } catch (final IllegalArgumentException e
) {
254 } catch (final Exception e
) {
255 fail("Unexpected exception");
259 // ------------------------------------------------------------------------
261 // ------------------------------------------------------------------------
263 public void testDefaultTmfTraceStub() throws Exception
{
264 assertFalse ("Open trace", fTrace
== null);
265 assertEquals("getType", TmfEvent
.class, fTrace
.getType());
266 assertNull ("getResource", fTrace
.getResource());
267 assertEquals("getCacheSize", BLOCK_SIZE
, fTrace
.getCacheSize());
268 assertEquals("getStreamingInterval", 0, fTrace
.getStreamingInterval());
269 assertEquals("getName", TEST_STREAM
, fTrace
.getName());
271 assertEquals("getNbEvents", NB_EVENTS
, fTrace
.getNbEvents());
272 assertEquals("getRange-start", 1, fTrace
.getTimeRange().getStartTime().getValue());
273 assertEquals("getRange-end", NB_EVENTS
, fTrace
.getTimeRange().getEndTime().getValue());
274 assertEquals("getStartTime", 1, fTrace
.getStartTime().getValue());
275 assertEquals("getEndTime", NB_EVENTS
, fTrace
.getEndTime().getValue());
277 String expected
= "TmfTrace [fPath=" + fTrace
.getPath() + ", fCacheSize=" + fTrace
.getCacheSize() +
278 ", fNbEvents=" + fTrace
.getNbEvents() + ", fStartTime=" + fTrace
.getStartTime() +
279 ", fEndTime=" + fTrace
.getEndTime() + ", fStreamingInterval=" + fTrace
.getStreamingInterval() +
281 assertEquals("toString", expected
, fTrace
.toString());
284 // ------------------------------------------------------------------------
285 // Trace initialization
286 // ------------------------------------------------------------------------
288 public void testInitTrace() throws Exception
{
290 // Instantiate an "empty" trace
291 final TmfTraceStub trace
= new TmfTraceStub();
293 assertFalse ("Open trace", trace
== null);
294 assertNull ("getType", trace
.getType());
295 assertNull ("getResource", trace
.getResource());
296 assertEquals("getCacheSize", TmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, trace
.getCacheSize());
297 assertEquals("getStreamingInterval", 0, trace
.getStreamingInterval());
298 assertEquals("getName", "", trace
.getName());
300 assertEquals("getNbEvents", 0, trace
.getNbEvents());
301 assertEquals("getRange-start", Long
.MAX_VALUE
, trace
.getTimeRange().getStartTime().getValue());
302 assertEquals("getRange-end", Long
.MIN_VALUE
, trace
.getTimeRange().getEndTime().getValue());
303 assertEquals("getStartTime", Long
.MAX_VALUE
, trace
.getStartTime().getValue());
304 assertEquals("getEndTime", Long
.MIN_VALUE
, trace
.getEndTime().getValue());
307 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM
), null);
308 final File testfile
= new File(FileLocator
.toFileURL(location
).toURI());
309 assertTrue("validate", trace
.validate(null, testfile
.getPath()));
311 // InitTrace and wait for indexing completion...
312 trace
.initTrace(null, testfile
.getPath(), TmfEvent
.class);
314 while (trace
.getNbEvents() < NB_EVENTS
&& nbSecs
< 10) {
318 if (trace
.getNbEvents() < NB_EVENTS
) {
322 assertFalse ("Open trace", trace
== null);
323 assertEquals("getType", TmfEvent
.class, trace
.getType());
324 assertNull ("getResource", trace
.getResource());
325 assertEquals("getCacheSize", TmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, trace
.getCacheSize());
326 assertEquals("getStreamingInterval", 0, trace
.getStreamingInterval());
327 assertEquals("getName", TEST_STREAM
, trace
.getName());
329 assertEquals("getNbEvents", NB_EVENTS
, trace
.getNbEvents());
330 assertEquals("getRange-start", 1, trace
.getTimeRange().getStartTime().getValue());
331 assertEquals("getRange-end", NB_EVENTS
, trace
.getTimeRange().getEndTime().getValue());
332 assertEquals("getStartTime", 1, trace
.getStartTime().getValue());
333 assertEquals("getEndTime", NB_EVENTS
, trace
.getEndTime().getValue());
336 // ------------------------------------------------------------------------
337 // Get/Set time range
338 // ------------------------------------------------------------------------
340 public void testSetTimeRange() throws Exception
{
341 final TmfTraceStub trace
= new TmfTraceStub(fTrace
);
344 assertEquals("getRange-start", 1, trace
.getTimeRange().getStartTime().getValue());
345 assertEquals("getRange-end", NB_EVENTS
, trace
.getTimeRange().getEndTime().getValue());
346 assertEquals("getStartTime", 1, trace
.getStartTime().getValue());
347 assertEquals("getEndTime", NB_EVENTS
, trace
.getEndTime().getValue());
349 trace
.setTimeRange(new TmfTimeRange(new TmfTimestamp(100), new TmfTimestamp(200)));
350 assertEquals("setTimeRange", 100, trace
.getTimeRange().getStartTime().getValue());
351 assertEquals("setTimeRange", 200, trace
.getTimeRange().getEndTime().getValue());
352 assertEquals("setTimeRange", 100, trace
.getStartTime().getValue());
353 assertEquals("setTimeRange", 200, trace
.getEndTime().getValue());
358 public void testSetStartTime() throws Exception
{
359 final TmfTraceStub trace
= new TmfTraceStub(fTrace
);
362 assertEquals("getRange-start", 1, trace
.getTimeRange().getStartTime().getValue());
363 assertEquals("getRange-end", NB_EVENTS
, trace
.getTimeRange().getEndTime().getValue());
364 assertEquals("getStartTime", 1, trace
.getStartTime().getValue());
365 assertEquals("getEndTime", NB_EVENTS
, trace
.getEndTime().getValue());
367 trace
.setStartTime(new TmfTimestamp(100));
368 assertEquals("setStartTime", 100, trace
.getTimeRange().getStartTime().getValue());
369 assertEquals("setStartTime", NB_EVENTS
, trace
.getTimeRange().getEndTime().getValue());
370 assertEquals("setStartTime", 100, trace
.getStartTime().getValue());
371 assertEquals("setStartTime", NB_EVENTS
, trace
.getEndTime().getValue());
376 public void testSetEndTime() throws Exception
{
377 final TmfTraceStub trace
= new TmfTraceStub(fTrace
);
380 assertEquals("getRange-start", 1, trace
.getTimeRange().getStartTime().getValue());
381 assertEquals("getRange-end", NB_EVENTS
, trace
.getTimeRange().getEndTime().getValue());
382 assertEquals("getStartTime", 1, trace
.getStartTime().getValue());
383 assertEquals("getEndTime", NB_EVENTS
, trace
.getEndTime().getValue());
385 trace
.setEndTime(new TmfTimestamp(100));
386 assertEquals("setEndTime", 1, trace
.getTimeRange().getStartTime().getValue());
387 assertEquals("setEndTime", 100, trace
.getTimeRange().getEndTime().getValue());
388 assertEquals("setEndTime", 1, trace
.getStartTime().getValue());
389 assertEquals("setEndTime", 100, trace
.getEndTime().getValue());
394 // ------------------------------------------------------------------------
395 // seekEvent on timestamp
396 // ------------------------------------------------------------------------
398 public void testSeekEventOnNullTimestamp() throws Exception
{
400 // Position trace at event rank 0
401 ITmfContext context
= fTrace
.seekEvent(null);
402 assertEquals("Event rank", 0, context
.getRank());
404 ITmfEvent event
= fTrace
.parseEvent(context
);
405 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
406 assertEquals("Event rank", 0, context
.getRank());
409 public void testSeekEventOnTimestampOnCacheBoundary() throws Exception
{
411 // Position trace at event rank 0
412 ITmfContext context
= fTrace
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
413 assertEquals("Event rank", 0, context
.getRank());
415 ITmfEvent event
= fTrace
.parseEvent(context
);
416 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
417 assertEquals("Event rank", 0, context
.getRank());
419 event
= fTrace
.getNextEvent(context
);
420 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
421 assertEquals("Event rank", 1, context
.getRank());
423 // Position trace at event rank 1000
424 context
= fTrace
.seekEvent(new TmfTimestamp(1001, SCALE
, 0));
425 assertEquals("Event rank", 1000, context
.getRank());
427 event
= fTrace
.parseEvent(context
);
428 assertEquals("Event timestamp", 1001, event
.getTimestamp().getValue());
429 assertEquals("Event rank", 1000, context
.getRank());
431 event
= fTrace
.getNextEvent(context
);
432 assertEquals("Event timestamp", 1001, event
.getTimestamp().getValue());
433 assertEquals("Event rank", 1001, context
.getRank());
435 // Position trace at event rank 4000
436 context
= fTrace
.seekEvent(new TmfTimestamp(4001, SCALE
, 0));
437 assertEquals("Event rank", 4000, context
.getRank());
439 event
= fTrace
.parseEvent(context
);
440 assertEquals("Event timestamp", 4001, event
.getTimestamp().getValue());
441 assertEquals("Event rank", 4000, context
.getRank());
443 event
= fTrace
.getNextEvent(context
);
444 assertEquals("Event timestamp", 4001, event
.getTimestamp().getValue());
445 assertEquals("Event rank", 4001, context
.getRank());
448 public void testSeekEventOnTimestampNotOnCacheBoundary() throws Exception
{
450 // Position trace at event rank 1
451 ITmfContext context
= fTrace
.seekEvent(new TmfTimestamp(2, SCALE
, 0));
452 assertEquals("Event rank", 1, context
.getRank());
454 ITmfEvent event
= fTrace
.parseEvent(context
);
455 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
456 assertEquals("Event rank", 1, context
.getRank());
458 event
= fTrace
.getNextEvent(context
);
459 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
460 assertEquals("Event rank", 2, context
.getRank());
462 // Position trace at event rank 9
463 context
= fTrace
.seekEvent(new TmfTimestamp(10, SCALE
, 0));
464 assertEquals("Event rank", 9, context
.getRank());
466 event
= fTrace
.parseEvent(context
);
467 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
468 assertEquals("Event rank", 9, context
.getRank());
470 event
= fTrace
.getNextEvent(context
);
471 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
472 assertEquals("Event rank", 10, context
.getRank());
474 // Position trace at event rank 999
475 context
= fTrace
.seekEvent(new TmfTimestamp(1000, SCALE
, 0));
476 assertEquals("Event rank", 999, context
.getRank());
478 event
= fTrace
.parseEvent(context
);
479 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
480 assertEquals("Event rank", 999, context
.getRank());
482 event
= fTrace
.getNextEvent(context
);
483 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
484 assertEquals("Event rank", 1000, context
.getRank());
486 // Position trace at event rank 1001
487 context
= fTrace
.seekEvent(new TmfTimestamp(1002, SCALE
, 0));
488 assertEquals("Event rank", 1001, context
.getRank());
490 event
= fTrace
.parseEvent(context
);
491 assertEquals("Event timestamp", 1002, event
.getTimestamp().getValue());
492 assertEquals("Event rank", 1001, context
.getRank());
494 event
= fTrace
.getNextEvent(context
);
495 assertEquals("Event timestamp", 1002, event
.getTimestamp().getValue());
496 assertEquals("Event rank", 1002, context
.getRank());
498 // Position trace at event rank 4500
499 context
= fTrace
.seekEvent(new TmfTimestamp(4501, SCALE
, 0));
500 assertEquals("Event rank", 4500, context
.getRank());
502 event
= fTrace
.parseEvent(context
);
503 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
504 assertEquals("Event rank", 4500, context
.getRank());
506 event
= fTrace
.getNextEvent(context
);
507 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
508 assertEquals("Event rank", 4501, context
.getRank());
511 public void testSeekEventOnTimestampOutOfScope() throws Exception
{
513 // Position trace at beginning
514 ITmfContext context
= fTrace
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
515 assertEquals("Event rank", 0, context
.getRank());
517 ITmfEvent event
= fTrace
.parseEvent(context
);
518 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
519 assertEquals("Event rank", 0, context
.getRank());
521 event
= fTrace
.getNextEvent(context
);
522 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
523 assertEquals("Event rank", 1, context
.getRank());
525 // Position trace at event passed the end
526 context
= fTrace
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
527 assertEquals("Event rank", NB_EVENTS
, context
.getRank());
529 event
= fTrace
.parseEvent(context
);
530 assertEquals("Event timestamp", null, event
);
531 assertEquals("Event rank", NB_EVENTS
, context
.getRank());
533 event
= fTrace
.getNextEvent(context
);
534 assertEquals("Event timestamp", null, event
);
535 assertEquals("Event rank", NB_EVENTS
, context
.getRank());
538 // ------------------------------------------------------------------------
540 // ------------------------------------------------------------------------
542 public void testSeekEventOnNegativeRank() throws Exception
{
544 // Position trace at event rank 0
545 ITmfContext context
= fTrace
.seekEvent(-1);
546 assertEquals("Event rank", 0, context
.getRank());
548 ITmfEvent event
= fTrace
.parseEvent(context
);
549 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
550 assertEquals("Event rank", 0, context
.getRank());
553 public void testSeekOnRankOnCacheBoundary() throws Exception
{
555 // On lower bound, returns the first event (ts = 1)
556 ITmfContext context
= fTrace
.seekEvent(0);
557 assertEquals("Event rank", 0, context
.getRank());
559 ITmfEvent event
= fTrace
.parseEvent(context
);
560 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
561 assertEquals("Event rank", 0, context
.getRank());
563 event
= fTrace
.getNextEvent(context
);
564 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
565 assertEquals("Event rank", 1, context
.getRank());
567 // Position trace at event rank 1000
568 context
= fTrace
.seekEvent(1000);
569 assertEquals("Event rank", 1000, context
.getRank());
571 event
= fTrace
.parseEvent(context
);
572 assertEquals("Event timestamp", 1001, event
.getTimestamp().getValue());
573 assertEquals("Event rank", 1000, context
.getRank());
575 event
= fTrace
.getNextEvent(context
);
576 assertEquals("Event timestamp", 1001, event
.getTimestamp().getValue());
577 assertEquals("Event rank", 1001, context
.getRank());
579 // Position trace at event rank 4000
580 context
= fTrace
.seekEvent(4000);
581 assertEquals("Event rank", 4000, context
.getRank());
583 event
= fTrace
.parseEvent(context
);
584 assertEquals("Event timestamp", 4001, event
.getTimestamp().getValue());
585 assertEquals("Event rank", 4000, context
.getRank());
587 event
= fTrace
.getNextEvent(context
);
588 assertEquals("Event timestamp", 4001, event
.getTimestamp().getValue());
589 assertEquals("Event rank", 4001, context
.getRank());
592 public void testSeekOnRankNotOnCacheBoundary() throws Exception
{
594 // Position trace at event rank 9
595 ITmfContext context
= fTrace
.seekEvent(9);
596 assertEquals("Event rank", 9, context
.getRank());
598 ITmfEvent event
= fTrace
.parseEvent(context
);
599 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
600 assertEquals("Event rank", 9, context
.getRank());
602 event
= fTrace
.getNextEvent(context
);
603 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
604 assertEquals("Event rank", 10, context
.getRank());
606 // Position trace at event rank 999
607 context
= fTrace
.seekEvent(999);
608 assertEquals("Event rank", 999, context
.getRank());
610 event
= fTrace
.parseEvent(context
);
611 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
612 assertEquals("Event rank", 999, context
.getRank());
614 event
= fTrace
.getNextEvent(context
);
615 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
616 assertEquals("Event rank", 1000, context
.getRank());
618 // Position trace at event rank 1001
619 context
= fTrace
.seekEvent(1001);
620 assertEquals("Event rank", 1001, context
.getRank());
622 event
= fTrace
.parseEvent(context
);
623 assertEquals("Event timestamp", 1002, event
.getTimestamp().getValue());
624 assertEquals("Event rank", 1001, context
.getRank());
626 event
= fTrace
.getNextEvent(context
);
627 assertEquals("Event timestamp", 1002, event
.getTimestamp().getValue());
628 assertEquals("Event rank", 1002, context
.getRank());
630 // Position trace at event rank 4500
631 context
= fTrace
.seekEvent(4500);
632 assertEquals("Event rank", 4500, context
.getRank());
634 event
= fTrace
.parseEvent(context
);
635 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
636 assertEquals("Event rank", 4500, context
.getRank());
638 event
= fTrace
.getNextEvent(context
);
639 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
640 assertEquals("Event rank", 4501, context
.getRank());
643 public void testSeekEventOnRankOfScope() throws Exception
{
645 // Position trace at beginning
646 ITmfContext context
= fTrace
.seekEvent(-1);
647 assertEquals("Event rank", 0, context
.getRank());
649 ITmfEvent event
= fTrace
.parseEvent(context
);
650 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
651 assertEquals("Event rank", 0, context
.getRank());
653 event
= fTrace
.getNextEvent(context
);
654 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
655 assertEquals("Event rank", 1, context
.getRank());
657 // Position trace at event passed the end
658 context
= fTrace
.seekEvent(NB_EVENTS
);
659 assertEquals("Event rank", NB_EVENTS
, context
.getRank());
661 event
= fTrace
.parseEvent(context
);
662 assertEquals("Event timestamp", null, event
);
663 assertEquals("Event rank", NB_EVENTS
, context
.getRank());
665 event
= fTrace
.getNextEvent(context
);
666 assertEquals("Event timestamp", null, event
);
667 assertEquals("Event rank", NB_EVENTS
, context
.getRank());
670 // ------------------------------------------------------------------------
671 // parseEvent - make sure parseEvent doesn't update the context
672 // ------------------------------------------------------------------------
674 public void testParseEvent() throws Exception
{
676 final int NB_READS
= 20;
678 // On lower bound, returns the first event (ts = 0)
679 final TmfContext context
= (TmfContext
) fTrace
.seekEvent(new TmfTimestamp(0, SCALE
, 0));
680 TmfContext svContext
= new TmfContext(context
);
682 ITmfEvent event
= fTrace
.parseEvent(context
);
683 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
684 assertEquals("Event rank", 0, context
.getRank());
685 assertTrue("parseEvent", context
.equals(svContext
));
687 event
= fTrace
.parseEvent(context
);
688 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
689 assertEquals("Event rank", 0, context
.getRank());
690 assertTrue("parseEvent", context
.equals(svContext
));
692 event
= fTrace
.parseEvent(context
);
693 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
694 assertEquals("Event rank", 0, context
.getRank());
695 assertTrue("parseEvent", context
.equals(svContext
));
697 // Position the trace at event NB_READS
698 for (int i
= 1; i
< NB_READS
; i
++) {
699 event
= fTrace
.getNextEvent(context
);
700 assertEquals("Event timestamp", i
, event
.getTimestamp().getValue());
703 svContext
= new TmfContext(context
);
704 event
= fTrace
.parseEvent(context
);
705 assertEquals("Event timestamp", NB_READS
, event
.getTimestamp().getValue());
706 assertEquals("Event rank", NB_READS
-1 , context
.getRank());
707 assertTrue("parseEvent", context
.equals(svContext
));
709 event
= fTrace
.parseEvent(context
);
710 assertEquals("Event timestamp", NB_READS
, event
.getTimestamp().getValue());
711 assertEquals("Event rank", NB_READS
- 1, context
.getRank());
712 assertTrue("parseEvent", context
.equals(svContext
));
715 // ------------------------------------------------------------------------
716 // getNextEvent - updates the context
717 // ------------------------------------------------------------------------
719 public void testGetNextEvent() throws Exception
{
721 final int NB_READS
= 20;
723 // On lower bound, returns the first event (ts = 1)
724 final ITmfContext context
= fTrace
.seekEvent(new TmfTimestamp(0, SCALE
, 0));
728 for (int i
= 0; i
< NB_READS
; i
++) {
729 event
= fTrace
.getNextEvent(context
);
730 assertEquals("Event timestamp", i
+ 1, event
.getTimestamp().getValue());
731 assertEquals("Event rank", i
+ 1, context
.getRank());
734 // Make sure we stay positioned
735 event
= fTrace
.parseEvent(context
);
736 assertEquals("Event timestamp", NB_READS
+ 1, event
.getTimestamp().getValue());
737 assertEquals("Event rank", NB_READS
, context
.getRank());
740 // ------------------------------------------------------------------------
742 // ------------------------------------------------------------------------
744 @SuppressWarnings("unchecked")
745 public void testProcessRequestForNbEvents() throws Exception
{
746 final int BLOCK_SIZE
= 100;
747 final int NB_EVENTS
= 1000;
748 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
750 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
751 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, NB_EVENTS
, BLOCK_SIZE
) {
753 public void handleData(final TmfEvent event
) {
754 super.handleData(event
);
755 requestedEvents
.add(event
);
758 final ITmfDataProvider
<TmfEvent
>[] providers
= (ITmfDataProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfTraceStub
.class);
759 providers
[0].sendRequest(request
);
760 request
.waitForCompletion();
762 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
763 assertTrue("isCompleted", request
.isCompleted());
764 assertFalse("isCancelled", request
.isCancelled());
766 // Ensure that we have distinct events.
767 // Don't go overboard: we are not validating the stub!
768 for (int i
= 0; i
< NB_EVENTS
; i
++) {
769 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
773 @SuppressWarnings("unchecked")
774 public void testProcessRequestForAllEvents() throws Exception
{
775 final int BLOCK_SIZE
= 1;
776 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
778 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
779 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, NB_EVENTS
, BLOCK_SIZE
) {
781 public void handleData(final TmfEvent event
) {
782 super.handleData(event
);
783 requestedEvents
.add(event
);
786 final ITmfDataProvider
<TmfEvent
>[] providers
= (ITmfDataProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfTraceStub
.class);
787 providers
[0].sendRequest(request
);
788 request
.waitForCompletion();
790 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
791 assertTrue("isCompleted", request
.isCompleted());
792 assertFalse("isCancelled", request
.isCancelled());
794 // Ensure that we have distinct events.
795 // Don't go overboard: we are not validating the stub!
796 for (int i
= 0; i
< NB_EVENTS
; i
++) {
797 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
801 // ------------------------------------------------------------------------
803 // ------------------------------------------------------------------------
805 @SuppressWarnings("unchecked")
806 public void testCancel() throws Exception
{
807 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
809 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
810 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, NB_EVENTS
, BLOCK_SIZE
) {
813 public void handleData(final TmfEvent event
) {
814 super.handleData(event
);
815 requestedEvents
.add(event
);
816 if (++nbRead
== BLOCK_SIZE
) {
821 final ITmfDataProvider
<TmfEvent
>[] providers
= (ITmfDataProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfTraceStub
.class);
822 providers
[0].sendRequest(request
);
823 request
.waitForCompletion();
825 assertEquals("nbEvents", BLOCK_SIZE
, requestedEvents
.size());
826 assertTrue("isCompleted", request
.isCompleted());
827 assertTrue("isCancelled", request
.isCancelled());
831 // // ------------------------------------------------------------------------
833 // // Note: seekLocation() does not reliably set the rank
834 // // ------------------------------------------------------------------------
836 // public void testSeekLocationOnCacheBoundary() throws Exception {
838 // // Position trace at event rank 0
839 // TmfContext context = fTrace.seekLocation(null);
840 // ITmfEvent event = fTrace.parseEvent(context);
841 // assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
842 // assertEquals("Event rank", 0, context.getRank());
844 // context = fTrace.seekLocation(context.getLocation());
845 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
847 // event = fTrace.parseEvent(context);
848 // assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
849 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
851 // event = fTrace.getNextEvent(context);
852 // assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
853 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
855 // // Position trace at event rank 1000
856 // ITmfContext tmpContext = fTrace.seekEvent(new TmfTimestamp(1001, SCALE, 0));
857 // context = fTrace.seekLocation(tmpContext.getLocation());
858 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
860 // event = fTrace.parseEvent(context);
861 // assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
862 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
864 // event = fTrace.getNextEvent(context);
865 // assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
866 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
868 // // Position trace at event rank 4000
869 // tmpContext = fTrace.seekEvent(new TmfTimestamp(4001, SCALE, 0));
870 // context = fTrace.seekLocation(tmpContext.getLocation());
871 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
873 // event = fTrace.parseEvent(context);
874 // assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
875 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
877 // event = fTrace.getNextEvent(context);
878 // assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
879 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
882 // public void testSeekLocationNotOnCacheBoundary() throws Exception {
884 // // Position trace at event rank 9
885 // ITmfContext tmpContext = fTrace.seekEvent(new TmfTimestamp(10, SCALE, 0));
886 // TmfContext context = fTrace.seekLocation(tmpContext.getLocation());
887 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
889 // ITmfEvent event = fTrace.parseEvent(context);
890 // assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
891 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
893 // event = fTrace.getNextEvent(context);
894 // assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
895 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
897 // // Position trace at event rank 999
898 // tmpContext = fTrace.seekEvent(new TmfTimestamp(1000, SCALE, 0));
899 // context = fTrace.seekLocation(tmpContext.getLocation());
900 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
902 // event = fTrace.parseEvent(context);
903 // assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
904 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
906 // event = fTrace.getNextEvent(context);
907 // assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
908 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
910 // // Position trace at event rank 1001
911 // tmpContext = fTrace.seekEvent(new TmfTimestamp(1002, SCALE, 0));
912 // context = fTrace.seekLocation(tmpContext.getLocation());
913 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
915 // event = fTrace.parseEvent(context);
916 // assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
917 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
919 // event = fTrace.getNextEvent(context);
920 // assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
921 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
923 // // Position trace at event rank 4500
924 // tmpContext = fTrace.seekEvent(new TmfTimestamp(4501, SCALE, 0));
925 // context = fTrace.seekLocation(tmpContext.getLocation());
926 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
928 // event = fTrace.parseEvent(context);
929 // assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
930 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
932 // event = fTrace.getNextEvent(context);
933 // assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
934 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
937 // public void testSeekLocationOutOfScope() throws Exception {
939 // // Position trace at beginning
940 // ITmfContext tmpContext = fTrace.seekLocation(null);
941 // ITmfContext context = fTrace.seekLocation(tmpContext.getLocation());
942 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
944 // ITmfEvent event = fTrace.parseEvent(context);
945 // assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
946 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
948 // event = fTrace.getNextEvent(context);
949 // assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
950 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
952 // // Position trace at event passed the end
953 // tmpContext = fTrace.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0));
954 // context = fTrace.seekLocation(tmpContext.getLocation());
955 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
957 // event = fTrace.parseEvent(context);
958 // assertEquals("Event timestamp", null, event);
959 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
961 // event = fTrace.getNextEvent(context);
962 // assertEquals("Event timestamp", null, event);
963 // assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
966 // ------------------------------------------------------------------------
967 // Verify checkpoints
968 // ------------------------------------------------------------------------
970 // public void testTmfTraceIndexing() throws Exception {
971 // assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getIndexPageSize());
972 // assertEquals("getTraceSize", NB_EVENTS, fTrace.getNbEvents());
973 // assertEquals("getRange-start", 1, fTrace.getTimeRange().getStartTime().getValue());
974 // assertEquals("getRange-end", NB_EVENTS, fTrace.getTimeRange().getEndTime().getValue());
975 // assertEquals("getStartTime", 1, fTrace.getStartTime().getValue());
976 // assertEquals("getEndTime", NB_EVENTS, fTrace.getEndTime().getValue());
978 // final Vector<TmfCheckpoint> checkpoints = fTrace.getCheckpoints();
979 // final int pageSize = fTrace.getIndexPageSize();
980 // assertTrue("Checkpoints exist", checkpoints != null);
982 // // Validate that each checkpoint points to the right event
983 // for (int i = 0; i < checkpoints.size(); i++) {
984 // final TmfCheckpoint checkpoint = checkpoints.get(i);
985 // final TmfContext context = new TmfContext(checkpoint.getLocation(), i * pageSize);
986 // final ITmfEvent event = fTrace.parseEvent(context);
987 // assertTrue(context.getRank() == i * pageSize);
988 // assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));