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, null);
154 setIndexer(createTestIndexer(this));
158 public TestIndexerInterface
getIndexer() {
159 return (TestIndexerInterface
) super.getIndexer();
163 private class EmptyTestTrace
extends TmfEmptyTraceStub
{
164 public EmptyTestTrace() {
166 setIndexer(new TestIndexer(this));
170 public TestIndexer
getIndexer() {
171 return (TestIndexer
) super.getIndexer();
175 // ------------------------------------------------------------------------
177 // ------------------------------------------------------------------------
180 * Creates the trace for the specified path
184 * @return the created trace
185 * @throws URISyntaxException
187 * @throws IOException
189 * @throws TmfTraceException
192 protected TestTrace
createTrace(final String path
) throws URISyntaxException
, IOException
, TmfTraceException
{
193 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
194 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
195 TestTrace trace
= new TestTrace(test
.toURI().getPath(), BLOCK_SIZE
);
196 trace
.indexTrace(true);
200 private synchronized void setupTrace(final String path
) {
201 if (fTrace
== null) {
203 fTrace
= createTrace(path
);
204 } catch (final TmfTraceException e
) {
205 fail(e
.getMessage());
206 } catch (final URISyntaxException e
) {
207 fail(e
.getMessage());
208 } catch (final IOException e
) {
209 fail(e
.getMessage());
213 if (fEmptyTrace
== null) {
214 fEmptyTrace
= new EmptyTestTrace();
215 fEmptyTrace
.indexTrace(true);
219 // ------------------------------------------------------------------------
220 // Verify checkpoints
221 // ------------------------------------------------------------------------
224 * Test the content of the index after building the full index
227 public void testTmfTraceIndexing() {
228 verifyIndexContent();
232 * Verify the content of the index
234 protected static void verifyIndexContent() {
235 assertEquals(BLOCK_SIZE
, fTrace
.getCacheSize());
236 assertEquals(NB_EVENTS
, fTrace
.getNbEvents());
237 assertEquals(1, fTrace
.getTimeRange().getStartTime().getValue());
238 assertEquals(NB_EVENTS
, fTrace
.getTimeRange().getEndTime().getValue());
239 assertEquals(1, fTrace
.getStartTime().getValue());
240 assertEquals(NB_EVENTS
, fTrace
.getEndTime().getValue());
242 ITmfCheckpointIndex checkpoints
= fTrace
.getIndexer().getCheckpoints();
243 int pageSize
= fTrace
.getCacheSize();
244 assertTrue(checkpoints
!= null);
245 assertEquals(NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
247 // Validate that each checkpoint points to the right event
248 for (int i
= 0; i
< checkpoints
.size(); i
++) {
249 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
250 TmfContext context
= new TmfContext(checkpoint
.getLocation(), i
* pageSize
);
251 ITmfEvent event
= fTrace
.parseEvent(context
);
252 assertEquals(context
.getRank(), i
* pageSize
);
253 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
258 * Test that a empty trace has the correct content
261 public void testEmptyTmfTraceIndexing() {
262 assertEquals(ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, fEmptyTrace
.getCacheSize());
263 assertEquals(0, fEmptyTrace
.getNbEvents());
264 assertEquals(TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getTimeRange().getStartTime());
265 assertEquals(TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getTimeRange().getEndTime());
266 assertEquals(TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getStartTime());
267 assertEquals(TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getEndTime());
269 ITmfCheckpointIndex checkpoints
= fEmptyTrace
.getIndexer().getCheckpoints();
270 assertTrue(checkpoints
!= null);
271 assertEquals(0, checkpoints
.size());