1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adapted for TMF Trace Model 1.0
12 * Alexandre Montplaisir - Port to JUnit4
13 * Marc-Andre Laperle - Extracted to a common class from TmfCheckpointIndexTest
14 *******************************************************************************/
16 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
.indexer
.checkpoint
;
18 import static org
.junit
.Assert
.assertEquals
;
19 import static org
.junit
.Assert
.assertTrue
;
20 import static org
.junit
.Assert
.fail
;
23 import java
.io
.IOException
;
24 import java
.net
.URISyntaxException
;
27 import org
.eclipse
.core
.runtime
.FileLocator
;
28 import org
.eclipse
.core
.runtime
.Path
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
.TmfTestTrace
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpointIndex
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
40 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfEmptyTraceStub
;
41 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
42 import org
.junit
.After
;
43 import org
.junit
.Before
;
44 import org
.junit
.Test
;
47 * Common code for index testing
49 * @author Marc-Andre Laperle
51 public abstract class AbstractIndexTest
{
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
60 protected static final int BLOCK_SIZE
= 100;
61 private static final int NB_EVENTS
= 10000;
63 * The trace being tested
65 protected static TestTrace fTrace
= null;
66 private static EmptyTestTrace fEmptyTrace
= null;
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
77 setupTrace(getTracePath());
83 * @return the trace path
85 protected String
getTracePath() {
86 return TmfTestTrace
.A_TEST_10K
.getFullPath();
93 public void tearDown() {
96 fEmptyTrace
.dispose();
100 interface TestIndexerInterface
extends ITmfTraceIndexer
{
101 ITmfCheckpointIndex
getCheckpoints();
104 // ------------------------------------------------------------------------
106 // ------------------------------------------------------------------------
111 protected static class TestIndexer
extends TmfCheckpointIndexer
implements TestIndexerInterface
{
113 * Constructs the test indexer for a normal test trace
118 public TestIndexer(ITmfTrace testTrace
) {
119 super(testTrace
, BLOCK_SIZE
);
123 public ITmfCheckpointIndex
getCheckpoints() {
124 return getTraceIndex();
129 * Create the indexer for testing
133 * @return the indexer for testing
135 protected TestIndexerInterface
createTestIndexer(TestTrace trace
) {
136 return new TestIndexer(trace
);
142 protected class TestTrace
extends TmfTraceStub
{
149 * @throws TmfTraceException
152 public TestTrace(String path
, int blockSize
) throws TmfTraceException
{
153 super(path
, blockSize
, false, null);
157 protected ITmfTraceIndexer
createIndexer(int interval
) {
158 return createTestIndexer(this);
162 public TestIndexerInterface
getIndexer() {
163 return (TestIndexerInterface
) super.getIndexer();
167 private class EmptyTestTrace
extends TmfEmptyTraceStub
{
168 public EmptyTestTrace() {
173 protected ITmfTraceIndexer
createIndexer(int interval
) {
174 return new TestIndexer(this);
178 public TestIndexer
getIndexer() {
179 return (TestIndexer
) super.getIndexer();
183 // ------------------------------------------------------------------------
185 // ------------------------------------------------------------------------
188 * Creates the trace for the specified path
192 * @return the created trace
193 * @throws URISyntaxException
195 * @throws IOException
197 * @throws TmfTraceException
200 protected TestTrace
createTrace(final String path
) throws URISyntaxException
, IOException
, TmfTraceException
{
201 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
202 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
203 TestTrace trace
= new TestTrace(test
.toURI().getPath(), BLOCK_SIZE
);
204 trace
.indexTrace(true);
208 private synchronized void setupTrace(final String path
) {
209 if (fTrace
== null) {
211 fTrace
= createTrace(path
);
212 } catch (final TmfTraceException e
) {
213 fail(e
.getMessage());
214 } catch (final URISyntaxException e
) {
215 fail(e
.getMessage());
216 } catch (final IOException e
) {
217 fail(e
.getMessage());
221 if (fEmptyTrace
== null) {
222 fEmptyTrace
= new EmptyTestTrace();
223 fEmptyTrace
.indexTrace(true);
227 // ------------------------------------------------------------------------
228 // Verify checkpoints
229 // ------------------------------------------------------------------------
232 * Test the content of the index after building the full index
235 public void testTmfTraceIndexing() {
236 verifyIndexContent();
240 * Verify the content of the index
242 protected static void verifyIndexContent() {
243 assertEquals(BLOCK_SIZE
, fTrace
.getCacheSize());
244 assertEquals(NB_EVENTS
, fTrace
.getNbEvents());
245 assertEquals(1, fTrace
.getTimeRange().getStartTime().getValue());
246 assertEquals(NB_EVENTS
, fTrace
.getTimeRange().getEndTime().getValue());
247 assertEquals(1, fTrace
.getStartTime().getValue());
248 assertEquals(NB_EVENTS
, fTrace
.getEndTime().getValue());
250 ITmfCheckpointIndex checkpoints
= fTrace
.getIndexer().getCheckpoints();
251 int pageSize
= fTrace
.getCacheSize();
252 assertTrue(checkpoints
!= null);
253 assertEquals(NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
255 // Validate that each checkpoint points to the right event
256 for (int i
= 0; i
< checkpoints
.size(); i
++) {
257 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
258 TmfContext context
= new TmfContext(checkpoint
.getLocation(), i
* pageSize
);
259 ITmfEvent event
= fTrace
.parseEvent(context
);
260 assertEquals(context
.getRank(), i
* pageSize
);
261 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
266 * Test that a empty trace has the correct content
269 public void testEmptyTmfTraceIndexing() {
270 assertEquals(ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, fEmptyTrace
.getCacheSize());
271 assertEquals(0, fEmptyTrace
.getNbEvents());
272 assertEquals(TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getTimeRange().getStartTime());
273 assertEquals(TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getTimeRange().getEndTime());
274 assertEquals(TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getStartTime());
275 assertEquals(TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getEndTime());
277 ITmfCheckpointIndex checkpoints
= fEmptyTrace
.getIndexer().getCheckpoints();
278 assertTrue(checkpoints
!= null);
279 assertEquals(0, checkpoints
.size());