1 /*******************************************************************************
2 * Copyright (c) 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 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.tests
.trace
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertFalse
;
17 import static org
.junit
.Assert
.assertTrue
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpointIndex
;
29 import org
.junit
.After
;
30 import org
.junit
.Before
;
31 import org
.junit
.Test
;
34 * Common test code for custom trace indexes
36 * @author Marc-Andre Laperle
38 public abstract class AbstractCustomTraceIndexTest
{
41 * Time format use for event creation
43 protected static final String TIMESTAMP_FORMAT
= "dd/MM/yyyy HH:mm:ss:SSS";
45 * Block size used for the indexer
47 protected static final int BLOCK_SIZE
= 100;
49 * The total number of events in the generated trace
51 protected static final int NB_EVENTS
= 10000;
52 private TestTrace fTrace
= null;
55 * A common test indexer for custom trace index tests
57 protected static class TestIndexer
extends TmfBTreeTraceIndexer
{
60 * Constructs a new test indexer
62 * @param trace the trace
63 * @param interval the checkpoint interval
65 public TestIndexer(ITmfTrace trace
, int interval
) {
66 super(trace
, interval
);
74 public ITmfCheckpointIndex
getCheckpoints() {
75 return getTraceIndex();
79 interface TestTrace
extends ITmfTrace
{
80 TestIndexer
getIndexer();
86 * @throws Exception when error occurs
89 public void setUp() throws Exception
{
93 private synchronized void setupTrace() throws Exception
{
94 File traceDirectory
= new File(getTraceDirectory());
95 if (traceDirectory
.exists()) {
96 traceDirectory
.delete();
98 traceDirectory
.mkdir();
100 fTrace
= createTrace();
101 fTrace
.indexTrace(true);
106 * Create a test trace, varies between tests
108 * @return the test trace
109 * @throws Exception when error occurs
111 abstract protected TestTrace
createTrace() throws Exception
;
113 * Return the trace directory for the generated trace
115 * @return the trace directory for the generated trace
117 abstract protected String
getTraceDirectory();
123 public void tearDown() {
124 String directory
= TmfTraceManager
.getSupplementaryFileDir(fTrace
);
129 File dir
= new File(directory
);
131 File
[] files
= dir
.listFiles();
132 for (File file
: files
) {
138 File trace
= new File(getTraceDirectory());
139 if (trace
.exists()) {
147 * Test the content of the index after building the full index
150 public void testTmfTraceIndexing() {
151 verifyIndexContent();
154 private void verifyIndexContent() {
155 assertEquals("getCacheSize", BLOCK_SIZE
, fTrace
.getCacheSize());
156 assertEquals("getTraceSize", NB_EVENTS
, fTrace
.getNbEvents());
157 assertEquals("getRange-start", 0, fTrace
.getTimeRange().getStartTime().getValue());
158 assertEquals("getRange-end", NB_EVENTS
- 1, fTrace
.getTimeRange().getEndTime().getValue());
159 assertEquals("getStartTime", 0, fTrace
.getStartTime().getValue());
160 assertEquals("getEndTime", NB_EVENTS
- 1, fTrace
.getEndTime().getValue());
162 ITmfCheckpointIndex checkpoints
= fTrace
.getIndexer().getCheckpoints();
163 int pageSize
= fTrace
.getCacheSize();
164 assertTrue("Checkpoints exist", checkpoints
!= null);
165 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
167 // Validate that each checkpoint points to the right event
168 for (int i
= 0; i
< checkpoints
.size(); i
++) {
169 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
170 TmfContext context
= new TmfContext(checkpoint
.getLocation(), i
* pageSize
);
171 ITmfEvent event
= ((ITmfEventParser
)fTrace
).parseEvent(context
);
172 assertTrue(context
.getRank() == i
* pageSize
);
173 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
178 * Test that a fully built index has the same content when reloaded from disk
180 * @throws Exception when error occurs
183 public void testReopenIndex() throws Exception
{
185 fTrace
= createTrace();
186 assertFalse(fTrace
.getIndexer().getCheckpoints().isCreatedFromScratch());
187 fTrace
.indexTrace(true);
189 verifyIndexContent();