BTree index on disk
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / indexer / checkpoint / AbstractIndexTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 Ericsson
3 *
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
8 *
9 * Contributors:
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 *******************************************************************************/
15
16 package org.eclipse.linuxtools.tmf.core.tests.trace.indexer.checkpoint;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.net.URISyntaxException;
25 import java.net.URL;
26
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;
45
46 /**
47 * Common code for index testing
48 *
49 * @author Marc-Andre Laperle
50 */
51 public abstract class AbstractIndexTest {
52
53 // ------------------------------------------------------------------------
54 // Variables
55 // ------------------------------------------------------------------------
56
57 /**
58 *
59 */
60 protected static final int BLOCK_SIZE = 100;
61 private static final int NB_EVENTS = 10000;
62 /**
63 * The trace being tested
64 */
65 protected static TestTrace fTrace = null;
66 private static EmptyTestTrace fEmptyTrace = null;
67
68 // ------------------------------------------------------------------------
69 // Housekeeping
70 // ------------------------------------------------------------------------
71
72 /**
73 * Setup the test
74 */
75 @Before
76 public void setUp() {
77 setupTrace(getTracePath());
78 }
79
80 /**
81 * Get the trace path
82 *
83 * @return the trace path
84 */
85 protected String getTracePath() {
86 return TmfTestTrace.A_TEST_10K.getFullPath();
87 }
88
89 /**
90 * Tear down the test
91 */
92 @After
93 public void tearDown() {
94 fTrace.dispose();
95 fTrace = null;
96 fEmptyTrace.dispose();
97 fEmptyTrace = null;
98 }
99
100 interface TestIndexerInterface extends ITmfTraceIndexer {
101 ITmfCheckpointIndex getCheckpoints();
102 }
103
104 // ------------------------------------------------------------------------
105 // Helper classes
106 // ------------------------------------------------------------------------
107
108 /**
109 * A test indexer
110 */
111 protected static class TestIndexer extends TmfCheckpointIndexer implements TestIndexerInterface {
112 /**
113 * Constructs the test indexer for a normal test trace
114 *
115 * @param testTrace
116 * the test trace
117 */
118 public TestIndexer(ITmfTrace testTrace) {
119 super(testTrace, BLOCK_SIZE);
120 }
121
122 @Override
123 public ITmfCheckpointIndex getCheckpoints() {
124 return getTraceIndex();
125 }
126 }
127
128 /**
129 * Create the indexer for testing
130 *
131 * @param trace
132 * the trace
133 * @return the indexer for testing
134 */
135 protected TestIndexerInterface createTestIndexer(TestTrace trace) {
136 return new TestIndexer(trace);
137 }
138
139 /**
140 * A test trace
141 */
142 protected class TestTrace extends TmfTraceStub {
143 /**
144 *
145 * @param path
146 * the path
147 * @param blockSize
148 * the block size
149 * @throws TmfTraceException
150 * when error occurs
151 */
152 public TestTrace(String path, int blockSize) throws TmfTraceException {
153 super(path, blockSize, false, null, null);
154 setIndexer(createTestIndexer(this));
155 }
156
157 @Override
158 public TestIndexerInterface getIndexer() {
159 return (TestIndexerInterface) super.getIndexer();
160 }
161 }
162
163 private class EmptyTestTrace extends TmfEmptyTraceStub {
164 public EmptyTestTrace() {
165 super();
166 setIndexer(new TestIndexer(this));
167 }
168
169 @Override
170 public TestIndexer getIndexer() {
171 return (TestIndexer) super.getIndexer();
172 }
173 }
174
175 // ------------------------------------------------------------------------
176 // Helper functions
177 // ------------------------------------------------------------------------
178
179 /**
180 * Creates the trace for the specified path
181 *
182 * @param path
183 * the path
184 * @return the created trace
185 * @throws URISyntaxException
186 * when error occurs
187 * @throws IOException
188 * when error occurs
189 * @throws TmfTraceException
190 * when error occurs
191 */
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);
197 return trace;
198 }
199
200 private synchronized void setupTrace(final String path) {
201 if (fTrace == null) {
202 try {
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());
210 }
211 }
212
213 if (fEmptyTrace == null) {
214 fEmptyTrace = new EmptyTestTrace();
215 fEmptyTrace.indexTrace(true);
216 }
217 }
218
219 // ------------------------------------------------------------------------
220 // Verify checkpoints
221 // ------------------------------------------------------------------------
222
223 /**
224 * Test the content of the index after building the full index
225 */
226 @Test
227 public void testTmfTraceIndexing() {
228 verifyIndexContent();
229 }
230
231 /**
232 * Verify the content of the index
233 */
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());
241
242 ITmfCheckpointIndex checkpoints = fTrace.getIndexer().getCheckpoints();
243 int pageSize = fTrace.getCacheSize();
244 assertTrue(checkpoints != null);
245 assertEquals(NB_EVENTS / BLOCK_SIZE, checkpoints.size());
246
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));
254 }
255 }
256
257 /**
258 * Test that a empty trace has the correct content
259 */
260 @Test
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());
268
269 ITmfCheckpointIndex checkpoints = fEmptyTrace.getIndexer().getCheckpoints();
270 assertTrue(checkpoints != null);
271 assertEquals(0, checkpoints.size());
272 }
273 }
This page took 0.036445 seconds and 5 git commands to generate.