/*******************************************************************************
- * Copyright (c) 2009, 2010, 2012 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
* Contributors:
* Francois Chouinard - Initial API and implementation
* Francois Chouinard - Adjusted for new Trace Model
+ * Alexandre Montplaisir - Port to JUnit4
+ * Patrick Tasse - Updated for rank in experiment location
*******************************************************************************/
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 java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Vector;
-import junit.framework.TestCase;
-
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext;
import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentLocation;
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.request.ITmfDataRequest.ExecutionType;
+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.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.TmfExperiment;
-import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation;
+import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
+import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation;
import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub;
import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
+import org.junit.Before;
+import org.junit.Test;
/**
* Test suite for the TmfExperiment class (single trace).
*/
-@SuppressWarnings({"nls","javadoc"})
-public class TmfExperimentTest extends TestCase {
+@SuppressWarnings("javadoc")
+public class TmfExperimentTest {
// ------------------------------------------------------------------------
// Attributes
private static int NB_EVENTS = 10000;
private static int BLOCK_SIZE = 1000;
+ private static final double DELTA = 1e-15;
+
private ITmfTrace[] fTestTraces;
private TmfExperimentStub fExperiment;
}
}
- /**
- * @param name the test name
- */
- public TmfExperimentTest(final String name) {
- super(name);
- }
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
+ @Before
+ public void setUp() {
setupTrace(DIRECTORY + File.separator + TEST_STREAM);
setupExperiment();
}
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
- }
-
// ------------------------------------------------------------------------
// Constructor
// ------------------------------------------------------------------------
+ @Test
public void testSimpleTmfExperimentConstructor() {
-
- TmfExperiment experiment = new TmfExperiment(TmfEvent.class, EXPERIMENT, fTestTraces);
+ TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, EXPERIMENT, fTestTraces);
assertEquals("GetId", EXPERIMENT, experiment.getName());
assertEquals("GetCacheSize", TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, experiment.getCacheSize());
experiment.dispose();
- experiment = new TmfExperiment(TmfEvent.class, EXPERIMENT, null);
+ experiment = new TmfExperiment(ITmfEvent.class, EXPERIMENT, null);
experiment.dispose();
}
+ @Test
public void testNormalTmfExperimentConstructor() {
-
assertEquals("GetId", EXPERIMENT, fExperiment.getName());
assertEquals("GetNbEvents", NB_EVENTS, fExperiment.getNbEvents());
assertEquals("getEndTime", NB_EVENTS, timeRange.getEndTime().getValue());
}
- @SuppressWarnings("static-access")
- public void testSetCurrentExperiment() {
-
- TmfExperiment experiment = new TmfExperiment(TmfEvent.class, EXPERIMENT, fTestTraces);
- experiment.setCurrentExperiment(experiment);
- assertEquals("getCurrentExperiment", experiment, experiment.getCurrentExperiment());
-
- TmfExperiment experiment2 = new TmfExperiment(TmfEvent.class, EXPERIMENT, null);
- experiment.setCurrentExperiment(experiment2);
- assertEquals("getCurrentExperiment", experiment2, experiment.getCurrentExperiment());
-
- experiment.dispose();
- experiment2.dispose();
- }
-
// ------------------------------------------------------------------------
// getTimestamp
// ------------------------------------------------------------------------
+ @Test
public void testGetTimestamp() {
assertEquals("getTimestamp", new TmfTimestamp( 1, (byte) -3), fExperiment.getTimestamp( 0));
assertEquals("getTimestamp", new TmfTimestamp( 2, (byte) -3), fExperiment.getTimestamp( 1));
// Bookmarks file handling
// ------------------------------------------------------------------------
+ @Test
public void testBookmarks() {
assertNull("GetBookmarksFile", fExperiment.getBookmarksFile());
IFile bookmarks = (IFile) fTestTraces[0].getResource();
assertEquals("GetBookmarksFile", bookmarks, fExperiment.getBookmarksFile());
}
+ // ------------------------------------------------------------------------
+ // State system and statistics methods
+ // ------------------------------------------------------------------------
+
+ @Test
+ public void testGetStatistics() {
+ /* There should not be any experiment-specific statistics */
+ ITmfStatistics stats = fExperiment.getStatistics();
+ assertNull(stats);
+ }
+
+ @Test
+ public void testGetStateSystem() {
+ /* There should not be any experiment-specific state system */
+ ITmfStateSystem ss = fExperiment.getStateSystems().get("something");
+ assertNull(ss);
+ }
+
// ------------------------------------------------------------------------
// seekEvent by location
// ------------------------------------------------------------------------
+ @Test
public void testSeekBadLocation() {
ITmfContext context = fExperiment.seekEvent(new TmfLongLocation(0L));
assertNull("seekEvent", context);
}
+ @Test
public void testSeekNoTrace() {
- TmfExperiment experiment = new TmfExperiment(TmfEvent.class, EXPERIMENT, null);
+ TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, EXPERIMENT, null);
ITmfContext context = experiment.seekEvent((TmfExperimentLocation) null);
assertNull("seekEvent", context);
experiment.dispose();
// seekEvent on ratio
// ------------------------------------------------------------------------
+ @Test
public void testSeekEventOnRatio() {
-
// First event
ITmfContext context = fExperiment.seekEvent(0.0);
assertEquals("Context rank", 0, context.getRank());
assertEquals("Context rank", 0, context.getRank());
}
+ @Test
public void testGetLocationRatio() {
-
// First event
ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
double ratio = fExperiment.getLocationRatio(context.getLocation());
- context = fExperiment.seekEvent(ratio);
- double ratio2 = fExperiment.getLocationRatio(context.getLocation());
- assertEquals("getLocationRatio", ratio, ratio2);
+ assertEquals("getLocationRatio", 0.0, ratio, DELTA);
// Middle event
context = fExperiment.seekEvent(NB_EVENTS / 2);
ratio = fExperiment.getLocationRatio(context.getLocation());
- context = fExperiment.seekEvent(ratio);
- ratio2 = fExperiment.getLocationRatio(context.getLocation());
- assertEquals("getLocationRatio", ratio, ratio2);
+ assertEquals("getLocationRatio", (double) (NB_EVENTS / 2) / NB_EVENTS, ratio, DELTA);
// Last event
context = fExperiment.seekEvent(NB_EVENTS - 1);
ratio = fExperiment.getLocationRatio(context.getLocation());
- context = fExperiment.seekEvent(ratio);
- ratio2 = fExperiment.getLocationRatio(context.getLocation());
- assertEquals("getLocationRatio", ratio, ratio2);
+ assertEquals("getLocationRatio", (double) (NB_EVENTS - 1) / NB_EVENTS, ratio, DELTA);
}
// @SuppressWarnings("rawtypes")
// seekEvent on rank
// ------------------------------------------------------------------------
+ @Test
public void testSeekRankOnCacheBoundary() {
-
long cacheSize = fExperiment.getCacheSize();
// On lower bound, returns the first event (TS = 1)
assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
}
+ @Test
public void testSeekRankNotOnCacheBoundary() {
-
long cacheSize = fExperiment.getCacheSize();
// Position trace at event rank 9
assertEquals("Context rank", 4501, context.getRank());
}
+ @Test
public void testSeekRankOutOfScope() {
-
// Position trace at beginning
ITmfContext context = fExperiment.seekEvent(-1);
assertEquals("Event rank", 0, context.getRank());
// seekEvent on timestamp
// ------------------------------------------------------------------------
+ @Test
public void testSeekTimestampOnCacheBoundary() {
-
long cacheSize = fExperiment.getCacheSize();
// Position trace at event rank 0
assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
}
+ @Test
public void testSeekTimestampNotOnCacheBoundary() {
-
// Position trace at event rank 1 (TS = 2)
ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(2, SCALE, 0));
assertEquals("Context rank", 1, context.getRank());
assertEquals("Context rank", 4501, context.getRank());
}
+ @Test
public void testSeekTimestampOutOfScope() {
-
// Position trace at beginning
ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(-1, SCALE, 0));
assertEquals("Event rank", 0, context.getRank());
// seekEvent by location (context rank is undefined)
// ------------------------------------------------------------------------
+ @Test
public void testSeekLocationOnCacheBoundary() {
-
long cacheSize = fExperiment.getCacheSize();
// Position trace at event rank 0
assertEquals("Event timestamp", 4 * cacheSize + 2, event.getTimestamp().getValue());
}
+ @Test
public void testSeekLocationNotOnCacheBoundary() {
-
long cacheSize = fExperiment.getCacheSize();
// Position trace at event 'cacheSize' - 1
assertEquals("Event timestamp", 4502, event.getTimestamp().getValue());
}
+ @Test
public void testSeekLocationOutOfScope() {
-
// Position trace at beginning
ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
assertEquals("Experiment context rank", expRank, ctx.getRank());
}
+ @Test
public void testGetNextAfteSeekingOnTS_1() {
final long INITIAL_TS = 1;
validateContextRanks(context);
}
+ @Test
public void testGetNextAfteSeekingOnTS_2() {
-
final long INITIAL_TS = 2;
final int NB_READS = 20;
validateContextRanks(context);
}
+ @Test
public void testGetNextAfteSeekingOnTS_3() {
final long INITIAL_TS = 500;
validateContextRanks(context);
}
+ @Test
public void testGetNextAfterSeekingOnRank_1() {
-
final long INITIAL_RANK = 0L;
final int NB_READS = 20;
validateContextRanks(context);
}
+ @Test
public void testGetNextAfterSeekingOnRank_2() {
-
final long INITIAL_RANK = 1L;
final int NB_READS = 20;
validateContextRanks(context);
}
+ @Test
public void testGetNextAfterSeekingOnRank_3() {
-
final long INITIAL_RANK = 500L;
final int NB_READS = 20;
validateContextRanks(context);
}
+ @Test
public void testGetNextAfterSeekingOnLocation_1() {
-
final ITmfLocation INITIAL_LOC = null;
final long INITIAL_TS = 1;
final int NB_READS = 20;
validateContextRanks(context);
}
+ @Test
public void testGetNextAfterSeekingOnLocation_2() {
-
final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(1L).getLocation();
final long INITIAL_TS = 2;
final int NB_READS = 20;
validateContextRanks(context);
}
+ @Test
public void testGetNextAfterSeekingOnLocation_3() {
-
final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(500L).getLocation();
final long INITIAL_TS = 501;
final int NB_READS = 20;
validateContextRanks(context);
}
+ @Test
public void testGetNextLocation() {
ITmfContext context1 = fExperiment.seekEvent(0);
fExperiment.getNext(context1);
- ITmfLocation location = context1.getLocation().clone();
+ ITmfLocation location = context1.getLocation();
ITmfEvent event1 = fExperiment.getNext(context1);
ITmfContext context2 = fExperiment.seekEvent(location);
ITmfEvent event2 = fExperiment.getNext(context2);
assertEquals("Event timestamp", event1.getTimestamp().getValue(), event2.getTimestamp().getValue());
}
+ @Test
public void testGetNextEndLocation() {
ITmfContext context1 = fExperiment.seekEvent(fExperiment.getNbEvents() - 1);
fExperiment.getNext(context1);
- ITmfLocation location = context1.getLocation().clone();
+ ITmfLocation location = context1.getLocation();
ITmfContext context2 = fExperiment.seekEvent(location);
ITmfEvent event = fExperiment.getNext(context2);
assertNull("Event", event);
// processRequest
// ------------------------------------------------------------------------
+ @Test
public void testProcessRequestForNbEvents() throws InterruptedException {
-
- final int blockSize = 100;
final int nbEvents = 1000;
final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
- final TmfEventRequest request = new TmfEventRequest(TmfEvent.class, range, nbEvents, blockSize) {
- @Override
- public void handleData(final ITmfEvent event) {
- super.handleData(event);
- requestedEvents.add(event);
- }
- };
- fExperiment.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", i+1, requestedEvents.get(i).getTimestamp().getValue());
- }
- }
-
- public void testProcessRequestForNbEvents2() throws InterruptedException {
-
- final int blockSize = 2 * NB_EVENTS;
- final int nbEvents = 1000;
- final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
-
- final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
- final TmfEventRequest request = new TmfEventRequest(TmfEvent.class, range, nbEvents, blockSize) {
+ final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
+ range, 0, nbEvents, ExecutionType.FOREGROUND) {
@Override
public void handleData(final ITmfEvent event) {
super.handleData(event);
}
}
+ @Test
public void testProcessRequestForAllEvents() throws InterruptedException {
-
final int nbEvents = TmfDataRequest.ALL_DATA;
- final int blockSize = 1;
final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
final long nbExpectedEvents = NB_EVENTS;
final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
- final TmfEventRequest request = new TmfEventRequest(TmfEvent.class, range, nbEvents, blockSize) {
+ final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
+ range, 0, nbEvents, ExecutionType.FOREGROUND) {
@Override
public void handleData(final ITmfEvent event) {
super.handleData(event);
// cancel
// ------------------------------------------------------------------------
+ @Test
public void testCancel() throws InterruptedException {
-
- final int nbEvents = NB_EVENTS;
- final int blockSize = BLOCK_SIZE;
+ final int nbEvents = NB_EVENTS;
+ final int limit = BLOCK_SIZE;
final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
- final TmfEventRequest request = new TmfEventRequest(TmfEvent.class, range, nbEvents, blockSize) {
+ final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
+ range, 0, nbEvents, ExecutionType.FOREGROUND) {
int nbRead = 0;
+
@Override
public void handleData(final ITmfEvent event) {
super.handleData(event);
requestedEvents.add(event);
- if (++nbRead == blockSize) {
+ if (++nbRead == limit) {
cancel();
}
}
+
@Override
public void handleCancel() {
- if (requestedEvents.size() < blockSize) {
+ if (requestedEvents.size() < limit) {
System.out.println("aie");
}
}
fExperiment.sendRequest(request);
request.waitForCompletion();
- assertEquals("nbEvents", blockSize, requestedEvents.size());
+ assertEquals("nbEvents", limit, requestedEvents.size());
assertTrue("isCompleted", request.isCompleted());
assertTrue("isCancelled", request.isCancelled());
}