X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=org.eclipse.linuxtools.tmf.core.tests%2Fsrc%2Forg%2Feclipse%2Flinuxtools%2Ftmf%2Fcore%2Ftests%2Ftrace%2FTmfTraceTest.java;h=496a6d0b1dbef488af2b1b9b48edc1d31e6ea58c;hb=a94410d92f16c8ce3870bb2e1538b93e038e4f78;hp=d79f02d21446c721f33c0795177b74357e7749d6;hpb=2065894741f9e2ad30265d0ffe7ccf00fe40e67d;p=deliverable%2Ftracecompass.git diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfTraceTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfTraceTest.java index d79f02d214..496a6d0b1d 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfTraceTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfTraceTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2010, 20112 Ericsson + * Copyright (c) 2009, 2013 Ericsson * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License v1.0 which @@ -9,46 +9,59 @@ * Contributors: * Francois Chouinard - Initial API and implementation * Francois Chouinard - Adapted for TMF Trace Model 1.0 + * Alexandre Montplaisir - Port to JUnit4 *******************************************************************************/ package org.eclipse.linuxtools.tmf.core.tests.trace; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; +import java.util.Map; import java.util.Vector; -import junit.framework.TestCase; - import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.Path; +import org.eclipse.linuxtools.internal.tmf.core.component.TmfProviderManager; import org.eclipse.linuxtools.tmf.core.component.ITmfDataProvider; -import org.eclipse.linuxtools.tmf.core.component.TmfProviderManager; import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; -import org.eclipse.linuxtools.tmf.core.event.TmfEvent; -import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; -import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; +import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; +import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest; import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; +import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem; +import org.eclipse.linuxtools.tmf.core.statistics.ITmfStatistics; import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin; +import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; +import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; +import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; +import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; +import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; +import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpointIndexer; import org.eclipse.linuxtools.tmf.core.trace.TmfContext; -import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; -import org.eclipse.linuxtools.tmf.core.trace.TmfTraceIndexer; import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; /** - * TmfTraceTest - *

* Test suite for the TmfTrace class. */ -@SuppressWarnings("nls") -public class TmfTraceTest extends TestCase { +@SuppressWarnings("javadoc") +public class TmfTraceTest { // ------------------------------------------------------------------------ // Variables // ------------------------------------------------------------------------ + private static final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec private static final String DIRECTORY = "testfiles"; private static final String TEST_STREAM = "A-Test-10K"; private static final int BLOCK_SIZE = 500; @@ -61,19 +74,13 @@ public class TmfTraceTest extends TestCase { // Housekeeping // ------------------------------------------------------------------------ - public TmfTraceTest(final String name) throws Exception { - super(name); - } - - @Override - protected void setUp() throws Exception { - super.setUp(); + @Before + public void setUp() { fTrace = setupTrace(DIRECTORY + File.separator + TEST_STREAM); } - @Override - protected void tearDown() throws Exception { - super.tearDown(); + @After + public void tearDown() { fTrace.dispose(); fTrace = null; } @@ -82,13 +89,15 @@ public class TmfTraceTest extends TestCase { // Helper functions // ------------------------------------------------------------------------ - private TmfTraceStub setupTrace(final String path) { + private static TmfTraceStub setupTrace(final String path) { if (fTrace == null) { try { final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null); final File test = new File(FileLocator.toFileURL(location).toURI()); fTrace = new TmfTraceStub(test.toURI().getPath(), BLOCK_SIZE); fTrace.indexTrace(); + } catch (final TmfTraceException e) { + e.printStackTrace(); } catch (final URISyntaxException e) { e.printStackTrace(); } catch (final IOException e) { @@ -102,7 +111,9 @@ public class TmfTraceTest extends TestCase { // Constructors // ------------------------------------------------------------------------ - public void testStandardConstructor() throws Exception { + @Test + @SuppressWarnings("null") + public void testStandardConstructor() throws TmfTraceException { TmfTraceStub trace = null; File testfile = null; try { @@ -117,10 +128,10 @@ public class TmfTraceTest extends TestCase { } assertFalse ("Open trace", trace == null); - assertEquals("getType", TmfEvent.class, trace.getType()); + assertEquals("getType", ITmfEvent.class, trace.getType()); assertNull ("getResource", trace.getResource()); assertEquals("getPath", testfile.toURI().getPath(), trace.getPath()); - assertEquals("getCacheSize", TmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize()); + assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize()); assertEquals("getStreamingInterval", 0, trace.getStreamingInterval()); assertEquals("getName", TEST_STREAM, trace.getName()); @@ -131,7 +142,9 @@ public class TmfTraceTest extends TestCase { assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue()); } - public void testStandardConstructorCacheSize() throws Exception { + @Test + @SuppressWarnings("null") + public void testStandardConstructorCacheSize() throws TmfTraceException { TmfTraceStub trace = null; File testfile = null; try { @@ -146,10 +159,10 @@ public class TmfTraceTest extends TestCase { } assertFalse ("Open trace", trace == null); - assertEquals("getType", TmfEvent.class, trace.getType()); + assertEquals("getType", ITmfEvent.class, trace.getType()); assertNull ("getResource", trace.getResource()); assertEquals("getPath", testfile.toURI().getPath(), trace.getPath()); - assertEquals("getCacheSize", TmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize()); + assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize()); assertEquals("getStreamingInterval", 0, trace.getStreamingInterval()); assertEquals("getName", TEST_STREAM, trace.getName()); @@ -170,8 +183,7 @@ public class TmfTraceTest extends TestCase { fail("IOException"); } - assertFalse ("Open trace", trace == null); - assertEquals("getType", TmfEvent.class, trace.getType()); + assertEquals("getType", ITmfEvent.class, trace.getType()); assertNull ("getResource", trace.getResource()); assertEquals("getPath", testfile.toURI().getPath(), trace.getPath()); assertEquals("getCacheSize", BLOCK_SIZE, trace.getCacheSize()); @@ -185,7 +197,9 @@ public class TmfTraceTest extends TestCase { assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue()); } - public void testFullConstructor() throws Exception { + @Test + @SuppressWarnings("null") + public void testFullConstructor() throws TmfTraceException { TmfTraceStub trace = null; File testfile = null; try { @@ -200,7 +214,7 @@ public class TmfTraceTest extends TestCase { } assertFalse ("Open trace", trace == null); - assertEquals("getType", TmfEvent.class, trace.getType()); + assertEquals("getType", ITmfEvent.class, trace.getType()); assertNull ("getResource", trace.getResource()); assertEquals("getPath", testfile.toURI().getPath(), trace.getPath()); assertEquals("getCacheSize", BLOCK_SIZE, trace.getCacheSize()); @@ -214,15 +228,48 @@ public class TmfTraceTest extends TestCase { assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue()); } - @SuppressWarnings({ "rawtypes", "unchecked" }) - public void testCopyConstructor() throws Exception { + @Test + @SuppressWarnings("null") + public void testLiveTraceConstructor() throws TmfTraceException { + TmfTraceStub trace = null; + File testfile = null; + final long interval = 100; + try { + final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM), null); + testfile = new File(FileLocator.toFileURL(location).toURI()); + trace = new TmfTraceStub(testfile.toURI().getPath(), BLOCK_SIZE, interval); + trace.indexTrace(); + } catch (final URISyntaxException e) { + fail("URISyntaxException"); + } catch (final IOException e) { + fail("IOException"); + } + + assertFalse ("Open trace", trace == null); + assertEquals("getType", ITmfEvent.class, trace.getType()); + assertNull ("getResource", trace.getResource()); + assertEquals("getPath", testfile.toURI().getPath(), trace.getPath()); + assertEquals("getCacheSize", BLOCK_SIZE, trace.getCacheSize()); + assertEquals("getStreamingInterval", interval, trace.getStreamingInterval()); + assertEquals("getName", TEST_STREAM, trace.getName()); + + assertEquals("getNbEvents", NB_EVENTS, trace.getNbEvents()); + assertEquals("getRange-start", 1, trace.getTimeRange().getStartTime().getValue()); + assertEquals("getRange-end", NB_EVENTS, trace.getTimeRange().getEndTime().getValue()); + assertEquals("getStartTime", 1, trace.getStartTime().getValue()); + assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue()); + } + + @Test + @SuppressWarnings("null") + public void testCopyConstructor() throws TmfTraceException { TmfTraceStub original = null; TmfTraceStub trace = null; File testfile = null; try { final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM), null); testfile = new File(FileLocator.toFileURL(location).toURI()); - original = new TmfTraceStub(testfile.toURI().getPath(), BLOCK_SIZE, new TmfTraceIndexer(null)); + original = new TmfTraceStub(testfile.toURI().getPath(), BLOCK_SIZE, new TmfCheckpointIndexer(null)); trace = new TmfTraceStub(original); trace.indexTrace(); } catch (final URISyntaxException e) { @@ -232,7 +279,7 @@ public class TmfTraceTest extends TestCase { } assertFalse ("Open trace", trace == null); - assertEquals("getType", TmfEvent.class, trace.getType()); + assertEquals("getType", ITmfEvent.class, trace.getType()); assertNull ("getResource", trace.getResource()); assertEquals("getPath", testfile.toURI().getPath(), trace.getPath()); assertEquals("getCacheSize", BLOCK_SIZE, trace.getCacheSize()); @@ -257,59 +304,104 @@ public class TmfTraceTest extends TestCase { } // ------------------------------------------------------------------------ - // toString + // Trace initialization // ------------------------------------------------------------------------ - public void testDefaultTmfTraceStub() throws Exception { - assertFalse ("Open trace", fTrace == null); - assertEquals("getType", TmfEvent.class, fTrace.getType()); - assertNull ("getResource", fTrace.getResource()); - assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize()); - assertEquals("getStreamingInterval", 0, fTrace.getStreamingInterval()); - assertEquals("getName", TEST_STREAM, fTrace.getName()); + @Test + public void testInitializeNullPath() { + // Instantiate an "empty" trace + final TmfTraceStub trace = new TmfTraceStub(); - assertEquals("getNbEvents", NB_EVENTS, fTrace.getNbEvents()); - assertEquals("getRange-start", 1, fTrace.getTimeRange().getStartTime().getValue()); - assertEquals("getRange-end", NB_EVENTS, fTrace.getTimeRange().getEndTime().getValue()); - assertEquals("getStartTime", 1, fTrace.getStartTime().getValue()); - assertEquals("getEndTime", NB_EVENTS, fTrace.getEndTime().getValue()); + try { + trace.initialize(null, null, ITmfEvent.class); + fail("TmfTrace.initialize() - no exception thrown"); + } catch (TmfTraceException e) { + // Success + } catch (Exception e) { + fail("TmfTrace.initialize() - wrong exception thrown"); + } + } - String expected = "TmfTrace [fPath=" + fTrace.getPath() + ", fCacheSize=" + fTrace.getCacheSize() + - ", fNbEvents=" + fTrace.getNbEvents() + ", fStartTime=" + fTrace.getStartTime() + - ", fEndTime=" + fTrace.getEndTime() + ", fStreamingInterval=" + fTrace.getStreamingInterval() + - "]"; - assertEquals("toString", expected, fTrace.toString()); + @Test + public void testInitializeSimplePath() { + // Instantiate an "empty" trace + final TmfTraceStub trace = new TmfTraceStub(); + + // Path == trace name + String path = "TraceName"; + try { + trace.initialize(null, path, ITmfEvent.class); + } catch (Exception e) { + fail("TmfTrace.initialize() - Exception thrown"); + } + + assertEquals("getType", ITmfEvent.class, trace.getType()); + assertNull ("getResource", trace.getResource()); + assertEquals("getPath", path, trace.getPath()); + assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize()); + assertEquals("getStreamingInterval", 0, trace.getStreamingInterval()); + assertEquals("getName", path, trace.getName()); + + assertEquals("getNbEvents", 0, trace.getNbEvents()); + assertEquals("getRange-start", Long.MIN_VALUE, trace.getTimeRange().getStartTime().getValue()); + assertEquals("getRange-end", Long.MIN_VALUE, trace.getTimeRange().getEndTime().getValue()); + assertEquals("getStartTime", Long.MIN_VALUE, trace.getStartTime().getValue()); + assertEquals("getEndTime", Long.MIN_VALUE, trace.getEndTime().getValue()); } - // ------------------------------------------------------------------------ - // Trace initialization - // ------------------------------------------------------------------------ + @Test + public void testInitializeNormalPath() { + // Instantiate an "empty" trace + final TmfTraceStub trace = new TmfTraceStub(); + + // Path == trace name + String name = "TraceName"; + String path = "/my/trace/path/" + name; + try { + trace.initialize(null, path, ITmfEvent.class); + } catch (Exception e) { + fail("TmfTrace.initialize() - Exception thrown"); + } - public void testInitTrace() throws Exception { + assertEquals("getType", ITmfEvent.class, trace.getType()); + assertNull ("getResource", trace.getResource()); + assertEquals("getPath", path, trace.getPath()); + assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize()); + assertEquals("getStreamingInterval", 0, trace.getStreamingInterval()); + assertEquals("getName", name, trace.getName()); + assertEquals("getNbEvents", 0, trace.getNbEvents()); + assertEquals("getRange-start", Long.MIN_VALUE, trace.getTimeRange().getStartTime().getValue()); + assertEquals("getRange-end", Long.MIN_VALUE, trace.getTimeRange().getEndTime().getValue()); + assertEquals("getStartTime", Long.MIN_VALUE, trace.getStartTime().getValue()); + assertEquals("getEndTime", Long.MIN_VALUE, trace.getEndTime().getValue()); + } + + @Test + public void testInitTrace() throws URISyntaxException, IOException, TmfTraceException, InterruptedException { // Instantiate an "empty" trace final TmfTraceStub trace = new TmfTraceStub(); - assertFalse ("Open trace", trace == null); assertNull ("getType", trace.getType()); assertNull ("getResource", trace.getResource()); - assertEquals("getCacheSize", TmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize()); + assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize()); assertEquals("getStreamingInterval", 0, trace.getStreamingInterval()); assertEquals("getName", "", trace.getName()); assertEquals("getNbEvents", 0, trace.getNbEvents()); - assertEquals("getRange-start", Long.MAX_VALUE, trace.getTimeRange().getStartTime().getValue()); + assertEquals("getRange-start", Long.MIN_VALUE, trace.getTimeRange().getStartTime().getValue()); assertEquals("getRange-end", Long.MIN_VALUE, trace.getTimeRange().getEndTime().getValue()); - assertEquals("getStartTime", Long.MAX_VALUE, trace.getStartTime().getValue()); + assertEquals("getStartTime", Long.MIN_VALUE, trace.getStartTime().getValue()); assertEquals("getEndTime", Long.MIN_VALUE, trace.getEndTime().getValue()); // Validate final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM), null); final File testfile = new File(FileLocator.toFileURL(location).toURI()); - assertTrue("validate", trace.validate(null, testfile.getPath())); + assertTrue("validate", trace.validate(null, testfile.getPath()).isOK()); // InitTrace and wait for indexing completion... - trace.initTrace(null, testfile.getPath(), TmfEvent.class); + trace.initTrace(null, testfile.toURI().getPath(), ITmfEvent.class); + trace.indexTrace(); int nbSecs = 0; while (trace.getNbEvents() < NB_EVENTS && nbSecs < 10) { Thread.sleep(1000); @@ -319,10 +411,9 @@ public class TmfTraceTest extends TestCase { fail("indexing"); } - assertFalse ("Open trace", trace == null); - assertEquals("getType", TmfEvent.class, trace.getType()); + assertEquals("getType", ITmfEvent.class, trace.getType()); assertNull ("getResource", trace.getResource()); - assertEquals("getCacheSize", TmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize()); + assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize()); assertEquals("getStreamingInterval", 0, trace.getStreamingInterval()); assertEquals("getName", TEST_STREAM, trace.getName()); @@ -334,10 +425,37 @@ public class TmfTraceTest extends TestCase { } // ------------------------------------------------------------------------ - // Get/Set time range + // Set/Get streaming interval + // ------------------------------------------------------------------------ + + @Test + public void testSetStreamingInterval() throws TmfTraceException { + final TmfTraceStub trace = new TmfTraceStub(fTrace); + + long interval = 0; + assertEquals("getStreamingInterval", interval, trace.getStreamingInterval()); + + interval = 100; + trace.setStreamingInterval(interval); + assertEquals("getStreamingInterval", interval, trace.getStreamingInterval()); + + interval = -1; + trace.setStreamingInterval(interval); + assertEquals("getStreamingInterval", 0, trace.getStreamingInterval()); + + interval = 0; + trace.setStreamingInterval(interval); + assertEquals("getStreamingInterval", interval, trace.getStreamingInterval()); + + trace.dispose(); + } + + // ------------------------------------------------------------------------ + // Set/Get time range // ------------------------------------------------------------------------ - public void testSetTimeRange() throws Exception { + @Test + public void testSetTimeRange() throws TmfTraceException { final TmfTraceStub trace = new TmfTraceStub(fTrace); trace.indexTrace(); @@ -355,7 +473,8 @@ public class TmfTraceTest extends TestCase { trace.dispose(); } - public void testSetStartTime() throws Exception { + @Test + public void testSetStartTime() throws TmfTraceException { final TmfTraceStub trace = new TmfTraceStub(fTrace); trace.indexTrace(); @@ -373,7 +492,8 @@ public class TmfTraceTest extends TestCase { trace.dispose(); } - public void testSetEndTime() throws Exception { + @Test + public void testSetEndTime() throws TmfTraceException { final TmfTraceStub trace = new TmfTraceStub(fTrace); trace.indexTrace(); @@ -391,14 +511,192 @@ public class TmfTraceTest extends TestCase { trace.dispose(); } + @Test + public void testSetNbEvents() throws TmfTraceException { + final TmfTraceStub trace = new TmfTraceStub(fTrace); + trace.indexTrace(); + + assertEquals("getNbEvents", NB_EVENTS, trace.getNbEvents()); + + trace.setNbEvents(0); + assertEquals("getNbEvents", 0, trace.getNbEvents()); + + trace.setNbEvents(-1); + assertEquals("getNbEvents", 0, trace.getNbEvents()); + + trace.setNbEvents(NB_EVENTS + 1); + assertEquals("getNbEvents", NB_EVENTS + 1, trace.getNbEvents()); + + trace.setNbEvents(NB_EVENTS); + assertEquals("getNbEvents", NB_EVENTS, trace.getNbEvents()); + + trace.dispose(); + } + // ------------------------------------------------------------------------ - // seekEvent on timestamp + // State system and statistics methods // ------------------------------------------------------------------------ - public void testSeekEventOnNullTimestamp() throws Exception { + @Test + public void testGetStatistics() { + /* Should be null in unit tests */ + ITmfStatistics stats = fTrace.getStatistics(); + assertNull(stats); + } + + @Test + public void testGetStateSystem() { + /* There should be no state system registered so far */ + ITmfStateSystem ss = fTrace.getStateSystems().get("something"); + assertNull(ss); + } + + /** + * Make sure the returned map is unmodifiable. + */ + @Test(expected=UnsupportedOperationException.class) + public void testGetStateSystem_readOnly() { + Map sss = fTrace.getStateSystems(); + sss.put("something", null); + } + + // ------------------------------------------------------------------------ + // seekEvent on location (note: does not reliably set the rank) + // ------------------------------------------------------------------------ + + @Test + public void testSeekEventOnCacheBoundary() { + // Position trace at event rank 0 + ITmfContext context = fTrace.seekEvent(0); + ITmfEvent event = fTrace.parseEvent(context); + assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); + assertEquals("Event rank", 0, context.getRank()); + + context = fTrace.seekEvent(context.getLocation()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.getNext(context); + assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + // Position trace at event rank 1000 + ITmfContext tmpContext = fTrace.seekEvent(new TmfTimestamp(1001, SCALE, 0)); + context = fTrace.seekEvent(tmpContext.getLocation()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", 1001, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.getNext(context); + assertEquals("Event timestamp", 1001, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + // Position trace at event rank 4000 + tmpContext = fTrace.seekEvent(new TmfTimestamp(4001, SCALE, 0)); + context = fTrace.seekEvent(tmpContext.getLocation()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", 4001, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.getNext(context); + assertEquals("Event timestamp", 4001, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + } + + @Test + public void testSeekEventNotOnCacheBoundary() { + // Position trace at event rank 9 + ITmfContext tmpContext = fTrace.seekEvent(new TmfTimestamp(10, SCALE, 0)); + TmfContext context = fTrace.seekEvent(tmpContext.getLocation()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + ITmfEvent event = fTrace.parseEvent(context); + assertEquals("Event timestamp", 10, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.getNext(context); + assertEquals("Event timestamp", 10, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + // Position trace at event rank 999 + tmpContext = fTrace.seekEvent(new TmfTimestamp(1000, SCALE, 0)); + context = fTrace.seekEvent(tmpContext.getLocation()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", 1000, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.getNext(context); + assertEquals("Event timestamp", 1000, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + // Position trace at event rank 1001 + tmpContext = fTrace.seekEvent(new TmfTimestamp(1002, SCALE, 0)); + context = fTrace.seekEvent(tmpContext.getLocation()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", 1002, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.getNext(context); + assertEquals("Event timestamp", 1002, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + // Position trace at event rank 4500 + tmpContext = fTrace.seekEvent(new TmfTimestamp(4501, SCALE, 0)); + context = fTrace.seekEvent(tmpContext.getLocation()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.getNext(context); + assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + } + + @Test + public void testSeekEventOutOfScope() { + // Position trace at beginning + ITmfContext tmpContext = fTrace.seekEvent(0); + ITmfContext context = fTrace.seekEvent(tmpContext.getLocation()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + ITmfEvent event = fTrace.parseEvent(context); + assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.getNext(context); + assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + // Position trace at event passed the end + context = fTrace.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0)); + assertNull("Event timestamp", context.getLocation()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); + + event = fTrace.getNext(context); + assertNull("Event", event); + } + + // ------------------------------------------------------------------------ + // seekEvent on timestamp (note: does not reliably set the rank) + // ------------------------------------------------------------------------ + + @Test + public void testSeekEventOnNullTimestamp() { // Position trace at event rank 0 - ITmfContext context = fTrace.seekEvent(null); + ITmfContext context = fTrace.seekEvent((ITmfTimestamp) null); assertEquals("Event rank", 0, context.getRank()); ITmfEvent event = fTrace.parseEvent(context); @@ -406,8 +704,8 @@ public class TmfTraceTest extends TestCase { assertEquals("Event rank", 0, context.getRank()); } - public void testSeekEventOnTimestampOnCacheBoundary() throws Exception { - + @Test + public void testSeekEventOnTimestampOnCacheBoundary() { // Position trace at event rank 0 ITmfContext context = fTrace.seekEvent(new TmfTimestamp(1, SCALE, 0)); assertEquals("Event rank", 0, context.getRank()); @@ -416,7 +714,7 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); assertEquals("Event rank", 0, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); assertEquals("Event rank", 1, context.getRank()); @@ -428,7 +726,7 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 1001, event.getTimestamp().getValue()); assertEquals("Event rank", 1000, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 1001, event.getTimestamp().getValue()); assertEquals("Event rank", 1001, context.getRank()); @@ -440,13 +738,13 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 4001, event.getTimestamp().getValue()); assertEquals("Event rank", 4000, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 4001, event.getTimestamp().getValue()); assertEquals("Event rank", 4001, context.getRank()); } - public void testSeekEventOnTimestampNotOnCacheBoundary() throws Exception { - + @Test + public void testSeekEventOnTimestampNotOnCacheBoundary() { // Position trace at event rank 1 ITmfContext context = fTrace.seekEvent(new TmfTimestamp(2, SCALE, 0)); assertEquals("Event rank", 1, context.getRank()); @@ -455,7 +753,7 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 2, event.getTimestamp().getValue()); assertEquals("Event rank", 1, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 2, event.getTimestamp().getValue()); assertEquals("Event rank", 2, context.getRank()); @@ -467,7 +765,7 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 10, event.getTimestamp().getValue()); assertEquals("Event rank", 9, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 10, event.getTimestamp().getValue()); assertEquals("Event rank", 10, context.getRank()); @@ -479,7 +777,7 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 1000, event.getTimestamp().getValue()); assertEquals("Event rank", 999, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 1000, event.getTimestamp().getValue()); assertEquals("Event rank", 1000, context.getRank()); @@ -491,7 +789,7 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 1002, event.getTimestamp().getValue()); assertEquals("Event rank", 1001, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 1002, event.getTimestamp().getValue()); assertEquals("Event rank", 1002, context.getRank()); @@ -503,13 +801,13 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); assertEquals("Event rank", 4500, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); assertEquals("Event rank", 4501, context.getRank()); } - public void testSeekEventOnTimestampOutOfScope() throws Exception { - + @Test + public void testSeekEventOnTimestampOutOfScope() { // Position trace at beginning ITmfContext context = fTrace.seekEvent(new TmfTimestamp(-1, SCALE, 0)); assertEquals("Event rank", 0, context.getRank()); @@ -518,29 +816,29 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); assertEquals("Event rank", 0, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); assertEquals("Event rank", 1, context.getRank()); // Position trace at event passed the end context = fTrace.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0)); - assertEquals("Event rank", NB_EVENTS, context.getRank()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); event = fTrace.parseEvent(context); assertEquals("Event timestamp", null, event); - assertEquals("Event rank", NB_EVENTS, context.getRank()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", null, event); - assertEquals("Event rank", NB_EVENTS, context.getRank()); + assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); } // ------------------------------------------------------------------------ // seekEvent on rank // ------------------------------------------------------------------------ - public void testSeekEventOnNegativeRank() throws Exception { - + @Test + public void testSeekEventOnNegativeRank() { // Position trace at event rank 0 ITmfContext context = fTrace.seekEvent(-1); assertEquals("Event rank", 0, context.getRank()); @@ -550,8 +848,8 @@ public class TmfTraceTest extends TestCase { assertEquals("Event rank", 0, context.getRank()); } - public void testSeekOnRankOnCacheBoundary() throws Exception { - + @Test + public void testSeekOnRankOnCacheBoundary() { // On lower bound, returns the first event (ts = 1) ITmfContext context = fTrace.seekEvent(0); assertEquals("Event rank", 0, context.getRank()); @@ -560,7 +858,7 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); assertEquals("Event rank", 0, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); assertEquals("Event rank", 1, context.getRank()); @@ -572,7 +870,7 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 1001, event.getTimestamp().getValue()); assertEquals("Event rank", 1000, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 1001, event.getTimestamp().getValue()); assertEquals("Event rank", 1001, context.getRank()); @@ -584,13 +882,13 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 4001, event.getTimestamp().getValue()); assertEquals("Event rank", 4000, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 4001, event.getTimestamp().getValue()); assertEquals("Event rank", 4001, context.getRank()); } - public void testSeekOnRankNotOnCacheBoundary() throws Exception { - + @Test + public void testSeekOnRankNotOnCacheBoundary() { // Position trace at event rank 9 ITmfContext context = fTrace.seekEvent(9); assertEquals("Event rank", 9, context.getRank()); @@ -599,7 +897,7 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 10, event.getTimestamp().getValue()); assertEquals("Event rank", 9, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 10, event.getTimestamp().getValue()); assertEquals("Event rank", 10, context.getRank()); @@ -611,7 +909,7 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 1000, event.getTimestamp().getValue()); assertEquals("Event rank", 999, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 1000, event.getTimestamp().getValue()); assertEquals("Event rank", 1000, context.getRank()); @@ -623,7 +921,7 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 1002, event.getTimestamp().getValue()); assertEquals("Event rank", 1001, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 1002, event.getTimestamp().getValue()); assertEquals("Event rank", 1002, context.getRank()); @@ -635,13 +933,13 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); assertEquals("Event rank", 4500, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); assertEquals("Event rank", 4501, context.getRank()); } - public void testSeekEventOnRankOfScope() throws Exception { - + @Test + public void testSeekEventOnRankOutOfScope() { // Position trace at beginning ITmfContext context = fTrace.seekEvent(-1); assertEquals("Event rank", 0, context.getRank()); @@ -650,7 +948,7 @@ public class TmfTraceTest extends TestCase { assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); assertEquals("Event rank", 0, context.getRank()); - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); assertEquals("Event rank", 1, context.getRank()); @@ -659,11 +957,11 @@ public class TmfTraceTest extends TestCase { assertEquals("Event rank", NB_EVENTS, context.getRank()); event = fTrace.parseEvent(context); - assertEquals("Event timestamp", null, event); + assertNull("Event", event); assertEquals("Event rank", NB_EVENTS, context.getRank()); - event = fTrace.getNextEvent(context); - assertEquals("Event timestamp", null, event); + event = fTrace.getNext(context); + assertNull("Event", event); assertEquals("Event rank", NB_EVENTS, context.getRank()); } @@ -671,8 +969,8 @@ public class TmfTraceTest extends TestCase { // parseEvent - make sure parseEvent doesn't update the context // ------------------------------------------------------------------------ - public void testParseEvent() throws Exception { - + @Test + public void testParseEvent() { final int NB_READS = 20; // On lower bound, returns the first event (ts = 0) @@ -696,7 +994,7 @@ public class TmfTraceTest extends TestCase { // Position the trace at event NB_READS for (int i = 1; i < NB_READS; i++) { - event = fTrace.getNextEvent(context); + event = fTrace.getNext(context); assertEquals("Event timestamp", i, event.getTimestamp().getValue()); } @@ -713,49 +1011,245 @@ public class TmfTraceTest extends TestCase { } // ------------------------------------------------------------------------ - // getNextEvent - updates the context + // getNext - updates the context // ------------------------------------------------------------------------ - public void testGetNextEvent() throws Exception { + @Test + public void testGetNextAfteSeekingOnTS_1() { + final long INITIAL_TS = 1; + final int NB_READS = 20; + + // On lower bound, returns the first event (ts = 1) + final ITmfContext context = fTrace.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0)); + + // Read NB_EVENTS + ITmfEvent event; + for (int i = 0; i < NB_READS; i++) { + event = fTrace.getNext(context); + assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_TS + i, context.getRank()); + } + + // Make sure we stay positioned + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank()); + } + + @Test + public void testGetNextAfteSeekingOnTS_2() { + final long INITIAL_TS = 2; + final int NB_READS = 20; + + // On lower bound, returns the first event (ts = 500) + final ITmfContext context = fTrace.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0)); + + // Read NB_EVENTS + ITmfEvent event; + for (int i = 0; i < NB_READS; i++) { + event = fTrace.getNext(context); + assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_TS + i, context.getRank()); + } + + // Make sure we stay positioned + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank()); + } + + @Test + public void testGetNextAfteSeekingOnTS_3() { + final long INITIAL_TS = 500; + final int NB_READS = 20; + + // On lower bound, returns the first event (ts = 500) + final ITmfContext context = fTrace.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0)); + + // Read NB_EVENTS + ITmfEvent event; + for (int i = 0; i < NB_READS; i++) { + event = fTrace.getNext(context); + assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_TS + i, context.getRank()); + } + + // Make sure we stay positioned + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank()); + } + + @Test + public void testGetNextAfterSeekingOnRank_1() { + final long INITIAL_RANK = 0L; + final int NB_READS = 20; + + // On lower bound, returns the first event (rank = 0) + final ITmfContext context = fTrace.seekEvent(INITIAL_RANK); + + // Read NB_EVENTS + ITmfEvent event; + for (int i = 0; i < NB_READS; i++) { + event = fTrace.getNext(context); + assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank()); + } + + // Make sure we stay positioned + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank()); + } + @Test + public void testGetNextAfterSeekingOnRank_2() { + final long INITIAL_RANK = 1L; + final int NB_READS = 20; + + // On lower bound, returns the first event (rank = 0) + final ITmfContext context = fTrace.seekEvent(INITIAL_RANK); + + // Read NB_EVENTS + ITmfEvent event; + for (int i = 0; i < NB_READS; i++) { + event = fTrace.getNext(context); + assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank()); + } + + // Make sure we stay positioned + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank()); + } + + @Test + public void testGetNextAfterSeekingOnRank_3() { + final long INITIAL_RANK = 500L; + final int NB_READS = 20; + + // On lower bound, returns the first event (rank = 0) + final ITmfContext context = fTrace.seekEvent(INITIAL_RANK); + + // Read NB_EVENTS + ITmfEvent event; + for (int i = 0; i < NB_READS; i++) { + event = fTrace.getNext(context); + assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank()); + } + + // Make sure we stay positioned + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank()); + } + + @Test + public void testGetNextAfterSeekingOnLocation_1() { + final ITmfLocation INITIAL_LOC = null; + final long INITIAL_TS = 1; final int NB_READS = 20; // On lower bound, returns the first event (ts = 1) - final ITmfContext context = fTrace.seekEvent(new TmfTimestamp(0, SCALE, 0)); + final ITmfContext context = fTrace.seekEvent(INITIAL_LOC); // Read NB_EVENTS ITmfEvent event; for (int i = 0; i < NB_READS; i++) { - event = fTrace.getNextEvent(context); - assertEquals("Event timestamp", i + 1, event.getTimestamp().getValue()); - assertEquals("Event rank", i + 1, context.getRank()); + event = fTrace.getNext(context); + assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_TS + i, context.getRank()); } // Make sure we stay positioned event = fTrace.parseEvent(context); - assertEquals("Event timestamp", NB_READS + 1, event.getTimestamp().getValue()); - assertEquals("Event rank", NB_READS, context.getRank()); + assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); + assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank()); + } + + @Test + public void testGetNextAfterSeekingOnLocation_2() { + final ITmfLocation INITIAL_LOC = fTrace.seekEvent(1L).getLocation(); + final long INITIAL_TS = 2; + final int NB_READS = 20; + + // On lower bound, returns the first event (ts = 501) + final ITmfContext context = fTrace.seekEvent(INITIAL_LOC); + + // Read NB_EVENTS + ITmfEvent event; + for (int i = 0; i < NB_READS; i++) { + event = fTrace.getNext(context); + assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); + } + + // Make sure we stay positioned + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); + } + + @Test + public void testGetNextAfterSeekingOnLocation_3() { + final ITmfLocation INITIAL_LOC = fTrace.seekEvent(500L).getLocation(); + final long INITIAL_TS = 501; + final int NB_READS = 20; + + // On lower bound, returns the first event (ts = 501) + final ITmfContext context = fTrace.seekEvent(INITIAL_LOC); + + // Read NB_EVENTS + ITmfEvent event; + for (int i = 0; i < NB_READS; i++) { + event = fTrace.getNext(context); + assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); + } + + // Make sure we stay positioned + event = fTrace.parseEvent(context); + assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); + } + + @Test + public void testGetNextLocation() { + ITmfContext context1 = fTrace.seekEvent(0); + fTrace.getNext(context1); + ITmfLocation location = context1.getLocation(); + ITmfEvent event1 = fTrace.getNext(context1); + ITmfContext context2 = fTrace.seekEvent(location); + ITmfEvent event2 = fTrace.getNext(context2); + assertEquals("Event timestamp", event1.getTimestamp().getValue(), event2.getTimestamp().getValue()); + } + + @Test + public void testGetNextEndLocation() { + ITmfContext context1 = fTrace.seekEvent(fTrace.getNbEvents() - 1); + fTrace.getNext(context1); + ITmfLocation location = context1.getLocation(); + ITmfContext context2 = fTrace.seekEvent(location); + ITmfEvent event = fTrace.getNext(context2); + assertNull("Event", event); } // ------------------------------------------------------------------------ // processRequest // ------------------------------------------------------------------------ - @SuppressWarnings("unchecked") - public void testProcessRequestForNbEvents() throws Exception { - final int BLOCK_SIZE = 100; - final int NB_EVENTS = 1000; - final Vector requestedEvents = new Vector(); + @Test + public void testProcessEventRequestForAllEvents() throws InterruptedException { + final int blockSize = 1; + final Vector requestedEvents = new Vector(); final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH); - final TmfEventRequest request = new TmfEventRequest(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) { + final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, NB_EVENTS, blockSize) { @Override - public void handleData(final TmfEvent event) { + public void handleData(final ITmfEvent event) { super.handleData(event); requestedEvents.add(event); } }; - final ITmfDataProvider[] providers = (ITmfDataProvider[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class); + final ITmfDataProvider[] providers = TmfProviderManager.getProviders(ITmfEvent.class, TmfTraceStub.class); providers[0].sendRequest(request); request.waitForCompletion(); @@ -766,35 +1260,125 @@ public class TmfTraceTest extends TestCase { // Ensure that we have distinct events. // Don't go overboard: we are not validating the stub! for (int i = 0; i < NB_EVENTS; i++) { - assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue()); + assertEquals("Distinct events", i + 1, requestedEvents.get(i).getTimestamp().getValue()); } } - @SuppressWarnings("unchecked") - public void testProcessRequestForAllEvents() throws Exception { - final int BLOCK_SIZE = 1; - final Vector requestedEvents = new Vector(); + @Test + public void testProcessEventRequestForNbEvents() throws InterruptedException { + final int blockSize = 100; + final int nbEvents = 1000; + final Vector requestedEvents = new Vector(); final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH); - final TmfEventRequest request = new TmfEventRequest(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) { + final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) { @Override - public void handleData(final TmfEvent event) { + public void handleData(final ITmfEvent event) { super.handleData(event); requestedEvents.add(event); } }; - final ITmfDataProvider[] providers = (ITmfDataProvider[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class); + final ITmfDataProvider[] providers = TmfProviderManager.getProviders(ITmfEvent.class, TmfTraceStub.class); providers[0].sendRequest(request); request.waitForCompletion(); - assertEquals("nbEvents", NB_EVENTS, requestedEvents.size()); + assertEquals("nbEvents", nbEvents, requestedEvents.size()); assertTrue("isCompleted", request.isCompleted()); assertFalse("isCancelled", request.isCancelled()); // Ensure that we have distinct events. // Don't go overboard: we are not validating the stub! - for (int i = 0; i < NB_EVENTS; i++) { - assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue()); + for (int i = 0; i < nbEvents; i++) { + assertEquals("Distinct events", i + 1, requestedEvents.get(i).getTimestamp().getValue()); + } + } + + @Test + public void testProcessEventRequestForSomeEvents() throws InterruptedException { + final int blockSize = 1; + final long startTime = 100; + final int nbEvents = 1000; + final Vector requestedEvents = new Vector(); + + final TmfTimeRange range = new TmfTimeRange(new TmfTimestamp(startTime, SCALE), TmfTimestamp.BIG_CRUNCH); + final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) { + @Override + public void handleData(final ITmfEvent event) { + super.handleData(event); + requestedEvents.add(event); + } + }; + final ITmfDataProvider[] providers = TmfProviderManager.getProviders(ITmfEvent.class, TmfTraceStub.class); + providers[0].sendRequest(request); + request.waitForCompletion(); + + assertEquals("nbEvents", nbEvents, requestedEvents.size()); + assertTrue("isCompleted", request.isCompleted()); + assertFalse("isCancelled", request.isCancelled()); + + // Ensure that we have distinct events. + // Don't go overboard: we are not validating the stub! + for (int i = 0; i < nbEvents; i++) { + assertEquals("Distinct events", startTime + i, requestedEvents.get(i).getTimestamp().getValue()); + } + } + + @Test + public void testProcessEventRequestForOtherEvents() throws InterruptedException { + final int blockSize = 1; + final int startIndex = 99; + final long startTime = 100; + final int nbEvents = 1000; + final Vector requestedEvents = new Vector(); + + final TmfTimeRange range = new TmfTimeRange(new TmfTimestamp(startTime, SCALE), TmfTimestamp.BIG_CRUNCH); + final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, startIndex, nbEvents, blockSize) { + @Override + public void handleData(final ITmfEvent event) { + super.handleData(event); + requestedEvents.add(event); + } + }; + final ITmfDataProvider[] providers = TmfProviderManager.getProviders(ITmfEvent.class, TmfTraceStub.class); + providers[0].sendRequest(request); + request.waitForCompletion(); + + assertEquals("nbEvents", nbEvents, requestedEvents.size()); + assertTrue("isCompleted", request.isCompleted()); + assertFalse("isCancelled", request.isCancelled()); + + // Ensure that we have distinct events. + // Don't go overboard: we are not validating the stub! + for (int i = 0; i < nbEvents; i++) { + assertEquals("Distinct events", startTime + i, requestedEvents.get(i).getTimestamp().getValue()); + } + } + + @Test + public void testProcessDataRequestForSomeEvents() throws InterruptedException { + final int startIndex = 100; + final int nbEvents = 1000; + final Vector requestedEvents = new Vector(); + + final TmfDataRequest request = new TmfDataRequest(ITmfEvent.class, startIndex, nbEvents) { + @Override + public void handleData(final ITmfEvent event) { + super.handleData(event); + requestedEvents.add(event); + } + }; + final ITmfDataProvider[] providers = TmfProviderManager.getProviders(ITmfEvent.class, TmfTraceStub.class); + providers[0].sendRequest(request); + request.waitForCompletion(); + + assertEquals("nbEvents", nbEvents, requestedEvents.size()); + assertTrue("isCompleted", request.isCompleted()); + assertFalse("isCancelled", request.isCancelled()); + + // Ensure that we have distinct events. + // Don't go overboard: we are not validating the stub! + for (int i = 0; i < nbEvents; i++) { + assertEquals("Distinct events", startIndex + 1 + i, requestedEvents.get(i).getTimestamp().getValue()); } } @@ -802,15 +1386,15 @@ public class TmfTraceTest extends TestCase { // cancel // ------------------------------------------------------------------------ - @SuppressWarnings("unchecked") - public void testCancel() throws Exception { - final Vector requestedEvents = new Vector(); + @Test + public void testCancel() throws InterruptedException { + final Vector requestedEvents = new Vector(); final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH); - final TmfEventRequest request = new TmfEventRequest(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) { + final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) { int nbRead = 0; @Override - public void handleData(final TmfEvent event) { + public void handleData(final ITmfEvent event) { super.handleData(event); requestedEvents.add(event); if (++nbRead == BLOCK_SIZE) { @@ -818,7 +1402,7 @@ public class TmfTraceTest extends TestCase { } } }; - final ITmfDataProvider[] providers = (ITmfDataProvider[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class); + final ITmfDataProvider[] providers = TmfProviderManager.getProviders(ITmfEvent.class, TmfTraceStub.class); providers[0].sendRequest(request); request.waitForCompletion(); @@ -827,166 +1411,67 @@ public class TmfTraceTest extends TestCase { assertTrue("isCancelled", request.isCancelled()); } + // ------------------------------------------------------------------------ + // toString + // ------------------------------------------------------------------------ -// // ------------------------------------------------------------------------ -// // seekLocation -// // Note: seekLocation() does not reliably set the rank -// // ------------------------------------------------------------------------ -// -// public void testSeekLocationOnCacheBoundary() throws Exception { -// -// // Position trace at event rank 0 -// TmfContext context = fTrace.seekLocation(null); -// ITmfEvent event = fTrace.parseEvent(context); -// assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); -// assertEquals("Event rank", 0, context.getRank()); -// -// context = fTrace.seekLocation(context.getLocation()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.parseEvent(context); -// assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.getNextEvent(context); -// assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// // Position trace at event rank 1000 -// ITmfContext tmpContext = fTrace.seekEvent(new TmfTimestamp(1001, SCALE, 0)); -// context = fTrace.seekLocation(tmpContext.getLocation()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.parseEvent(context); -// assertEquals("Event timestamp", 1001, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.getNextEvent(context); -// assertEquals("Event timestamp", 1001, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// // Position trace at event rank 4000 -// tmpContext = fTrace.seekEvent(new TmfTimestamp(4001, SCALE, 0)); -// context = fTrace.seekLocation(tmpContext.getLocation()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.parseEvent(context); -// assertEquals("Event timestamp", 4001, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.getNextEvent(context); -// assertEquals("Event timestamp", 4001, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// } -// -// public void testSeekLocationNotOnCacheBoundary() throws Exception { -// -// // Position trace at event rank 9 -// ITmfContext tmpContext = fTrace.seekEvent(new TmfTimestamp(10, SCALE, 0)); -// TmfContext context = fTrace.seekLocation(tmpContext.getLocation()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// ITmfEvent event = fTrace.parseEvent(context); -// assertEquals("Event timestamp", 10, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.getNextEvent(context); -// assertEquals("Event timestamp", 10, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// // Position trace at event rank 999 -// tmpContext = fTrace.seekEvent(new TmfTimestamp(1000, SCALE, 0)); -// context = fTrace.seekLocation(tmpContext.getLocation()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.parseEvent(context); -// assertEquals("Event timestamp", 1000, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.getNextEvent(context); -// assertEquals("Event timestamp", 1000, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// // Position trace at event rank 1001 -// tmpContext = fTrace.seekEvent(new TmfTimestamp(1002, SCALE, 0)); -// context = fTrace.seekLocation(tmpContext.getLocation()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.parseEvent(context); -// assertEquals("Event timestamp", 1002, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.getNextEvent(context); -// assertEquals("Event timestamp", 1002, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// // Position trace at event rank 4500 -// tmpContext = fTrace.seekEvent(new TmfTimestamp(4501, SCALE, 0)); -// context = fTrace.seekLocation(tmpContext.getLocation()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.parseEvent(context); -// assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.getNextEvent(context); -// assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// } -// -// public void testSeekLocationOutOfScope() throws Exception { -// -// // Position trace at beginning -// ITmfContext tmpContext = fTrace.seekLocation(null); -// ITmfContext context = fTrace.seekLocation(tmpContext.getLocation()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// ITmfEvent event = fTrace.parseEvent(context); -// assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.getNextEvent(context); -// assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// // Position trace at event passed the end -// tmpContext = fTrace.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0)); -// context = fTrace.seekLocation(tmpContext.getLocation()); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.parseEvent(context); -// assertEquals("Event timestamp", null, event); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// -// event = fTrace.getNextEvent(context); -// assertEquals("Event timestamp", null, event); -// assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); -// } - - // ------------------------------------------------------------------------ - // Verify checkpoints - // ------------------------------------------------------------------------ - - // public void testTmfTraceIndexing() throws Exception { - // assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getIndexPageSize()); - // assertEquals("getTraceSize", NB_EVENTS, fTrace.getNbEvents()); - // assertEquals("getRange-start", 1, fTrace.getTimeRange().getStartTime().getValue()); - // assertEquals("getRange-end", NB_EVENTS, fTrace.getTimeRange().getEndTime().getValue()); - // assertEquals("getStartTime", 1, fTrace.getStartTime().getValue()); - // assertEquals("getEndTime", NB_EVENTS, fTrace.getEndTime().getValue()); - // - // final Vector checkpoints = fTrace.getCheckpoints(); - // final int pageSize = fTrace.getIndexPageSize(); - // assertTrue("Checkpoints exist", checkpoints != null); - // - // // Validate that each checkpoint points to the right event - // for (int i = 0; i < checkpoints.size(); i++) { - // final TmfCheckpoint checkpoint = checkpoints.get(i); - // final TmfContext context = new TmfContext(checkpoint.getLocation(), i * pageSize); - // final ITmfEvent event = fTrace.parseEvent(context); - // assertTrue(context.getRank() == i * pageSize); - // assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0)); - // } - // } - -} \ No newline at end of file + @Test + public void testDefaultTmfTraceStub() { + assertFalse ("Open trace", fTrace == null); + assertEquals("getType", ITmfEvent.class, fTrace.getType()); + assertNull ("getResource", fTrace.getResource()); + assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize()); + assertEquals("getStreamingInterval", 0, fTrace.getStreamingInterval()); + assertEquals("getName", TEST_STREAM, fTrace.getName()); + + assertEquals("getNbEvents", NB_EVENTS, fTrace.getNbEvents()); + assertEquals("getRange-start", 1, fTrace.getTimeRange().getStartTime().getValue()); + assertEquals("getRange-end", NB_EVENTS, fTrace.getTimeRange().getEndTime().getValue()); + assertEquals("getStartTime", 1, fTrace.getStartTime().getValue()); + assertEquals("getEndTime", NB_EVENTS, fTrace.getEndTime().getValue()); + + String expected = "TmfTrace [fPath=" + fTrace.getPath() + ", fCacheSize=" + fTrace.getCacheSize() + + ", fNbEvents=" + fTrace.getNbEvents() + ", fStartTime=" + fTrace.getStartTime() + + ", fEndTime=" + fTrace.getEndTime() + ", fStreamingInterval=" + fTrace.getStreamingInterval() + + "]"; + assertEquals("toString", expected, fTrace.toString()); + } + + // ------------------------------------------------------------------------ + // getInitialRangeOffset, getCurrentRange, getCurrentTime + // ------------------------------------------------------------------------ + + @Test + public void testCurrentTimeValues() throws TmfTraceException { + + TmfTraceStub trace = null; + File testfile = null; + try { + final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM), null); + testfile = new File(FileLocator.toFileURL(location).toURI()); + trace = new TmfTraceStub(testfile.toURI().getPath()); + // verify initial values + TmfTimestamp defaultInitRange = new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE); + assertEquals("getInitialRangeOffset", defaultInitRange, trace.getInitialRangeOffset()); + assertEquals("getCurrentTime", TmfTimestamp.ZERO, trace.getCurrentTime()); + assertEquals("getCurrentRange", TmfTimeRange.NULL_RANGE, trace.getCurrentRange()); + trace.setInitialRangeOffset(new TmfTimestamp(5, ITmfTimestamp.MILLISECOND_SCALE)); + trace.indexTrace(); + } catch (final URISyntaxException e) { + fail("URISyntaxException"); + } catch (final IOException e) { + fail("IOException"); + } + assertFalse ("Open trace", trace == null); + + TmfTimestamp initRange = new TmfTimestamp(5, ITmfTimestamp.MILLISECOND_SCALE); + assertEquals("getInitialRangeOffset", initRange, trace.getInitialRangeOffset()); + assertEquals("getCurrentTime", trace.getTimeRange().getStartTime(), trace.getCurrentTime()); + + ITmfTimestamp startTimestamp = trace.getTimeRange().getStartTime(); + long endValue = startTimestamp.getValue() + initRange.normalize(0, startTimestamp.getScale()).getValue(); + ITmfTimestamp endTimestamp = new TmfTimestamp(endValue, startTimestamp.getScale()); + TmfTimeRange expectedRange = new TmfTimeRange(startTimestamp, endTimestamp); + assertEquals("getCurrentRange", expectedRange, trace.getCurrentRange()); + } +}