From b75d6b654c2797b5505b4f4ea8c070ddac0c778a Mon Sep 17 00:00:00 2001 From: Francois Chouinard Date: Thu, 17 May 2012 13:40:55 -0400 Subject: [PATCH] Improve TmfTrace test coverage --- .../tests/trace/TmfCheckpointIndexTest.java | 63 +++++++++++++--- .../tmf/core/tests/trace/TmfTraceTest.java | 21 ++++++ .../tests/stubs/trace/TmfEmptyTraceStub.java | 72 +++++++++++++++++++ .../tmf/tests/stubs/trace/TmfTraceStub.java | 5 ++ 4 files changed, 153 insertions(+), 8 deletions(-) create mode 100644 org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfEmptyTraceStub.java diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointIndexTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointIndexTest.java index 3255dc29df..a888bf872f 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointIndexTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointIndexTest.java @@ -24,12 +24,14 @@ import junit.framework.TestCase; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.Path; import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; +import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin; import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpoint; import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpointIndexer; import org.eclipse.linuxtools.tmf.core.trace.TmfContext; +import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfEmptyTraceStub; import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; /** @@ -42,11 +44,12 @@ public class TmfCheckpointIndexTest extends TestCase { // Variables // ------------------------------------------------------------------------ - private static final String DIRECTORY = "testfiles"; - private static final String TEST_STREAM = "A-Test-10K"; - private static final int BLOCK_SIZE = 100; - private static final int NB_EVENTS = 10000; - private static TestTrace fTrace = null; + private static final String DIRECTORY = "testfiles"; + private static final String TEST_STREAM = "A-Test-10K"; + private static final int BLOCK_SIZE = 100; + private static final int NB_EVENTS = 10000; + private static TestTrace fTrace = null; + private static EmptyTestTrace fEmptyTrace = null; // ------------------------------------------------------------------------ // Housekeeping @@ -59,7 +62,7 @@ public class TmfCheckpointIndexTest extends TestCase { @Override protected void setUp() throws Exception { super.setUp(); - fTrace = setupTrace(DIRECTORY + File.separator + TEST_STREAM); + setupTrace(DIRECTORY + File.separator + TEST_STREAM); } @Override @@ -67,6 +70,8 @@ public class TmfCheckpointIndexTest extends TestCase { super.tearDown(); fTrace.dispose(); fTrace = null; + fEmptyTrace.dispose(); + fEmptyTrace = null; } // ------------------------------------------------------------------------ @@ -78,6 +83,10 @@ public class TmfCheckpointIndexTest extends TestCase { public TestIndexer(TestTrace testTrace) { super((ITmfTrace) testTrace, BLOCK_SIZE); } + @SuppressWarnings({ "unchecked", "rawtypes" }) + public TestIndexer(EmptyTestTrace testTrace) { + super((ITmfTrace) testTrace, BLOCK_SIZE); + } public List getCheckpoints() { return getTraceIndex(); } @@ -94,11 +103,22 @@ public class TmfCheckpointIndexTest extends TestCase { } } + private class EmptyTestTrace extends TmfEmptyTraceStub { + public EmptyTestTrace() { + super(); + setIndexer(new TestIndexer(this)); + } + @Override + public TestIndexer getIndexer() { + return (TestIndexer) super.getIndexer(); + } + } + // ------------------------------------------------------------------------ // Helper functions // ------------------------------------------------------------------------ - private TestTrace setupTrace(final String path) { + private synchronized void setupTrace(final String path) { if (fTrace == null) { try { final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null); @@ -113,7 +133,11 @@ public class TmfCheckpointIndexTest extends TestCase { e.printStackTrace(); } } - return fTrace; + + if (fEmptyTrace == null) { + fEmptyTrace = new EmptyTestTrace(); + fEmptyTrace.indexTrace(); + } } // ------------------------------------------------------------------------ @@ -143,4 +167,27 @@ public class TmfCheckpointIndexTest extends TestCase { } } + public void testEmptyTmfTraceIndexing() throws Exception { + assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, fEmptyTrace.getCacheSize()); + assertEquals("getTraceSize", 0, fEmptyTrace.getNbEvents()); + assertEquals("getRange-start", TmfTimestamp.BIG_CRUNCH, fEmptyTrace.getTimeRange().getStartTime()); + assertEquals("getRange-end", TmfTimestamp.BIG_BANG, fEmptyTrace.getTimeRange().getEndTime()); + assertEquals("getStartTime", TmfTimestamp.BIG_CRUNCH, fEmptyTrace.getStartTime()); + assertEquals("getEndTime", TmfTimestamp.BIG_BANG, fEmptyTrace.getEndTime()); + + List checkpoints = fEmptyTrace.getIndexer().getCheckpoints(); + int pageSize = fEmptyTrace.getCacheSize(); + assertTrue("Checkpoints exist", checkpoints != null); + assertEquals("Checkpoints size", 0, checkpoints.size()); + + // Validate that each checkpoint points to the right event + for (int i = 0; i < checkpoints.size(); i++) { + TmfCheckpoint checkpoint = checkpoints.get(i); + TmfContext context = new TmfContext(checkpoint.getLocation(), i * pageSize); + ITmfEvent event = fEmptyTrace.parseEvent(context); + assertTrue(context.getRank() == i * pageSize); + assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0)); + } + } + } \ No newline at end of file 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 40986df9b3..7716e6d0d4 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 @@ -497,6 +497,27 @@ public class TmfTraceTest extends TestCase { trace.dispose(); } + public void testSetNbEvents() throws Exception { + 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 location (note: does not reliably set the rank) // ------------------------------------------------------------------------ diff --git a/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfEmptyTraceStub.java b/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfEmptyTraceStub.java new file mode 100644 index 0000000000..ab8c584d27 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfEmptyTraceStub.java @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright (c) 2009, 2010 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Francois Chouinard - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.tests.stubs.trace; + +import java.io.FileNotFoundException; + +import org.eclipse.linuxtools.tmf.core.event.TmfEvent; +import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; +import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; +import org.eclipse.linuxtools.tmf.core.trace.TmfContext; +import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; + +/** + * TmfEmptyTraceStub + *

+ * Dummy test trace. Use in conjunction with TmfEventParserStub. + */ +public class TmfEmptyTraceStub extends TmfTraceStub { + + // ------------------------------------------------------------------------ + // Constructors + // ------------------------------------------------------------------------ + + /** + * @param path + * @throws FileNotFoundException + */ + public TmfEmptyTraceStub() { + super(); + setParser(new TmfEventParserStub(this)); + } + + // ------------------------------------------------------------------------ + // Operators + // ------------------------------------------------------------------------ + + @Override + public TmfContext seekEvent(final ITmfLocation location) { + return new TmfContext(); + } + + @Override + public TmfContext seekEvent(final double ratio) { + return new TmfContext(); + } + + @Override + public double getLocationRatio(ITmfLocation location) { + return 0; + } + + @Override + public TmfLocation getCurrentLocation() { + return null; + } + + @Override + public TmfEvent parseEvent(final ITmfContext context) { + return null; + } + +} \ No newline at end of file diff --git a/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfTraceStub.java b/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfTraceStub.java index e005b39855..0738c7ab9f 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfTraceStub.java +++ b/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfTraceStub.java @@ -313,6 +313,11 @@ public class TmfTraceStub extends TmfTrace implements ITmfEventParser< return null; } + @Override + public void setNbEvents(final long nbEvents) { + super.setNbEvents(nbEvents); + } + @Override public void setTimeRange(final TmfTimeRange range) { super.setTimeRange(range); -- 2.34.1