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
.event
.TmfEvent
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
.TmfTestTrace
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpointIndex
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
41 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfEmptyTraceStub
;
42 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
43 import org
.junit
.After
;
44 import org
.junit
.Before
;
45 import org
.junit
.Test
;
48 * Common code for index testing
50 * @author Marc-Andre Laperle
52 public abstract class AbstractIndexTest
{
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
61 protected static final int BLOCK_SIZE
= 100;
62 private static final int NB_EVENTS
= 10000;
64 * The trace being tested
66 protected static TestTrace fTrace
= null;
67 private static EmptyTestTrace fEmptyTrace
= null;
69 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
78 setupTrace(getTracePath());
84 * @return the trace path
86 protected String
getTracePath() {
87 return TmfTestTrace
.A_TEST_10K
.getFullPath();
94 public void tearDown() {
97 fEmptyTrace
.dispose();
101 interface TestIndexerInterface
extends ITmfTraceIndexer
{
102 ITmfCheckpointIndex
getCheckpoints();
105 // ------------------------------------------------------------------------
107 // ------------------------------------------------------------------------
112 protected static class TestIndexer
extends TmfCheckpointIndexer
implements TestIndexerInterface
{
114 * Constructs the test indexer for a normal test trace
119 public TestIndexer(ITmfTrace testTrace
) {
120 super(testTrace
, BLOCK_SIZE
);
124 public ITmfCheckpointIndex
getCheckpoints() {
125 return getTraceIndex();
130 * Create the indexer for testing
134 * @return the indexer for testing
136 protected TestIndexerInterface
createTestIndexer(TestTrace trace
) {
137 return new TestIndexer(trace
);
143 protected class TestTrace
extends TmfTraceStub
{
150 * @throws TmfTraceException
153 public TestTrace(String path
, int blockSize
) throws TmfTraceException
{
154 super(path
, blockSize
, false, null);
158 protected ITmfTraceIndexer
createIndexer(int interval
) {
159 return createTestIndexer(this);
163 public TestIndexerInterface
getIndexer() {
164 return (TestIndexerInterface
) super.getIndexer();
168 private class EmptyTestTrace
extends TmfEmptyTraceStub
{
169 public EmptyTestTrace() {
171 init(getClass().getSimpleName(), TmfEvent
.class);
175 protected ITmfTraceIndexer
createIndexer(int interval
) {
176 return new TestIndexer(this);
180 public TestIndexer
getIndexer() {
181 return (TestIndexer
) super.getIndexer();
185 // ------------------------------------------------------------------------
187 // ------------------------------------------------------------------------
190 * Creates the trace for the specified path
194 * @return the created trace
195 * @throws URISyntaxException
197 * @throws IOException
199 * @throws TmfTraceException
202 protected TestTrace
createTrace(final String path
) throws URISyntaxException
, IOException
, TmfTraceException
{
203 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
204 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
205 TestTrace trace
= new TestTrace(test
.toURI().getPath(), BLOCK_SIZE
);
206 trace
.indexTrace(true);
210 private synchronized void setupTrace(final String path
) {
211 if (fTrace
== null) {
213 fTrace
= createTrace(path
);
214 } catch (final TmfTraceException e
) {
215 fail(e
.getMessage());
216 } catch (final URISyntaxException e
) {
217 fail(e
.getMessage());
218 } catch (final IOException e
) {
219 fail(e
.getMessage());
223 if (fEmptyTrace
== null) {
224 fEmptyTrace
= new EmptyTestTrace();
225 fEmptyTrace
.indexTrace(true);
229 // ------------------------------------------------------------------------
230 // Verify checkpoints
231 // ------------------------------------------------------------------------
234 * Test the content of the index after building the full index
237 public void testTmfTraceIndexing() {
238 verifyIndexContent();
242 * Verify the content of the index
244 protected static void verifyIndexContent() {
245 assertEquals(BLOCK_SIZE
, fTrace
.getCacheSize());
246 assertEquals(NB_EVENTS
, fTrace
.getNbEvents());
247 assertEquals(1, fTrace
.getTimeRange().getStartTime().getValue());
248 assertEquals(NB_EVENTS
, fTrace
.getTimeRange().getEndTime().getValue());
249 assertEquals(1, fTrace
.getStartTime().getValue());
250 assertEquals(NB_EVENTS
, fTrace
.getEndTime().getValue());
252 ITmfCheckpointIndex checkpoints
= fTrace
.getIndexer().getCheckpoints();
253 int pageSize
= fTrace
.getCacheSize();
254 assertTrue(checkpoints
!= null);
255 assertEquals(NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
257 // Validate that each checkpoint points to the right event
258 for (int i
= 0; i
< checkpoints
.size(); i
++) {
259 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
260 TmfContext context
= new TmfContext(checkpoint
.getLocation(), i
* pageSize
);
261 ITmfEvent event
= fTrace
.parseEvent(context
);
262 assertEquals(context
.getRank(), i
* pageSize
);
263 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
268 * Test that a empty trace has the correct content
271 public void testEmptyTmfTraceIndexing() {
272 assertEquals(ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, fEmptyTrace
.getCacheSize());
273 assertEquals(0, fEmptyTrace
.getNbEvents());
274 assertEquals(TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getTimeRange().getStartTime());
275 assertEquals(TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getTimeRange().getEndTime());
276 assertEquals(TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getStartTime());
277 assertEquals(TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getEndTime());
279 ITmfCheckpointIndex checkpoints
= fEmptyTrace
.getIndexer().getCheckpoints();
280 assertTrue(checkpoints
!= null);
281 assertEquals(0, checkpoints
.size());