Merge branch 'master' into lttng-luna
[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);
154 }
155
156 @Override
157 protected ITmfTraceIndexer createIndexer(int interval) {
158 return createTestIndexer(this);
159 }
160
161 @Override
162 public TestIndexerInterface getIndexer() {
163 return (TestIndexerInterface) super.getIndexer();
164 }
165 }
166
167 private class EmptyTestTrace extends TmfEmptyTraceStub {
168 public EmptyTestTrace() {
169 super();
170 }
171
172 @Override
173 protected ITmfTraceIndexer createIndexer(int interval) {
174 return new TestIndexer(this);
175 }
176
177 @Override
178 public TestIndexer getIndexer() {
179 return (TestIndexer) super.getIndexer();
180 }
181 }
182
183 // ------------------------------------------------------------------------
184 // Helper functions
185 // ------------------------------------------------------------------------
186
187 /**
188 * Creates the trace for the specified path
189 *
190 * @param path
191 * the path
192 * @return the created trace
193 * @throws URISyntaxException
194 * when error occurs
195 * @throws IOException
196 * when error occurs
197 * @throws TmfTraceException
198 * when error occurs
199 */
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);
205 return trace;
206 }
207
208 private synchronized void setupTrace(final String path) {
209 if (fTrace == null) {
210 try {
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());
218 }
219 }
220
221 if (fEmptyTrace == null) {
222 fEmptyTrace = new EmptyTestTrace();
223 fEmptyTrace.indexTrace(true);
224 }
225 }
226
227 // ------------------------------------------------------------------------
228 // Verify checkpoints
229 // ------------------------------------------------------------------------
230
231 /**
232 * Test the content of the index after building the full index
233 */
234 @Test
235 public void testTmfTraceIndexing() {
236 verifyIndexContent();
237 }
238
239 /**
240 * Verify the content of the index
241 */
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());
249
250 ITmfCheckpointIndex checkpoints = fTrace.getIndexer().getCheckpoints();
251 int pageSize = fTrace.getCacheSize();
252 assertTrue(checkpoints != null);
253 assertEquals(NB_EVENTS / BLOCK_SIZE, checkpoints.size());
254
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));
262 }
263 }
264
265 /**
266 * Test that a empty trace has the correct content
267 */
268 @Test
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());
276
277 ITmfCheckpointIndex checkpoints = fEmptyTrace.getIndexer().getCheckpoints();
278 assertTrue(checkpoints != null);
279 assertEquals(0, checkpoints.size());
280 }
281 }
This page took 0.039329 seconds and 6 git commands to generate.