Commit | Line | Data |
---|---|---|
032ecd45 MAL |
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 | ||
2bdf0193 | 16 | package org.eclipse.tracecompass.tmf.core.tests.trace.indexer.checkpoint; |
032ecd45 MAL |
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; | |
2bdf0193 AM |
29 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
30 | import org.eclipse.tracecompass.tmf.core.event.TmfEvent; | |
31 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; | |
32 | import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin; | |
33 | import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace; | |
34 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; | |
35 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
36 | import org.eclipse.tracecompass.tmf.core.trace.TmfContext; | |
37 | import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer; | |
38 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint; | |
39 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.ITmfCheckpointIndex; | |
40 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer; | |
41 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfEmptyTraceStub; | |
42 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub; | |
032ecd45 MAL |
43 | import org.junit.After; |
44 | import org.junit.Before; | |
45 | import org.junit.Test; | |
46 | ||
47 | /** | |
48 | * Common code for index testing | |
49 | * | |
50 | * @author Marc-Andre Laperle | |
51 | */ | |
52 | public abstract class AbstractIndexTest { | |
53 | ||
54 | // ------------------------------------------------------------------------ | |
55 | // Variables | |
56 | // ------------------------------------------------------------------------ | |
57 | ||
58 | /** | |
59 | * | |
60 | */ | |
61 | protected static final int BLOCK_SIZE = 100; | |
62 | private static final int NB_EVENTS = 10000; | |
63 | /** | |
64 | * The trace being tested | |
65 | */ | |
66 | protected static TestTrace fTrace = null; | |
67 | private static EmptyTestTrace fEmptyTrace = null; | |
68 | ||
69 | // ------------------------------------------------------------------------ | |
70 | // Housekeeping | |
71 | // ------------------------------------------------------------------------ | |
72 | ||
73 | /** | |
74 | * Setup the test | |
75 | */ | |
76 | @Before | |
77 | public void setUp() { | |
78 | setupTrace(getTracePath()); | |
79 | } | |
80 | ||
81 | /** | |
82 | * Get the trace path | |
83 | * | |
84 | * @return the trace path | |
85 | */ | |
86 | protected String getTracePath() { | |
87 | return TmfTestTrace.A_TEST_10K.getFullPath(); | |
88 | } | |
89 | ||
90 | /** | |
91 | * Tear down the test | |
92 | */ | |
93 | @After | |
94 | public void tearDown() { | |
95 | fTrace.dispose(); | |
96 | fTrace = null; | |
97 | fEmptyTrace.dispose(); | |
98 | fEmptyTrace = null; | |
99 | } | |
100 | ||
101 | interface TestIndexerInterface extends ITmfTraceIndexer { | |
102 | ITmfCheckpointIndex getCheckpoints(); | |
103 | } | |
104 | ||
105 | // ------------------------------------------------------------------------ | |
106 | // Helper classes | |
107 | // ------------------------------------------------------------------------ | |
108 | ||
109 | /** | |
110 | * A test indexer | |
111 | */ | |
112 | protected static class TestIndexer extends TmfCheckpointIndexer implements TestIndexerInterface { | |
113 | /** | |
114 | * Constructs the test indexer for a normal test trace | |
115 | * | |
116 | * @param testTrace | |
117 | * the test trace | |
118 | */ | |
119 | public TestIndexer(ITmfTrace testTrace) { | |
120 | super(testTrace, BLOCK_SIZE); | |
121 | } | |
122 | ||
123 | @Override | |
124 | public ITmfCheckpointIndex getCheckpoints() { | |
125 | return getTraceIndex(); | |
126 | } | |
127 | } | |
128 | ||
129 | /** | |
130 | * Create the indexer for testing | |
131 | * | |
132 | * @param trace | |
133 | * the trace | |
134 | * @return the indexer for testing | |
135 | */ | |
136 | protected TestIndexerInterface createTestIndexer(TestTrace trace) { | |
137 | return new TestIndexer(trace); | |
138 | } | |
139 | ||
140 | /** | |
141 | * A test trace | |
142 | */ | |
143 | protected class TestTrace extends TmfTraceStub { | |
144 | /** | |
145 | * | |
146 | * @param path | |
147 | * the path | |
148 | * @param blockSize | |
149 | * the block size | |
150 | * @throws TmfTraceException | |
151 | * when error occurs | |
152 | */ | |
153 | public TestTrace(String path, int blockSize) throws TmfTraceException { | |
ab186fbb MAL |
154 | super(path, blockSize, false, null); |
155 | } | |
156 | ||
157 | @Override | |
158 | protected ITmfTraceIndexer createIndexer(int interval) { | |
159 | return createTestIndexer(this); | |
032ecd45 MAL |
160 | } |
161 | ||
162 | @Override | |
163 | public TestIndexerInterface getIndexer() { | |
164 | return (TestIndexerInterface) super.getIndexer(); | |
165 | } | |
166 | } | |
167 | ||
168 | private class EmptyTestTrace extends TmfEmptyTraceStub { | |
169 | public EmptyTestTrace() { | |
170 | super(); | |
2669de0c | 171 | init(getClass().getSimpleName(), TmfEvent.class); |
ab186fbb MAL |
172 | } |
173 | ||
174 | @Override | |
175 | protected ITmfTraceIndexer createIndexer(int interval) { | |
176 | return new TestIndexer(this); | |
032ecd45 MAL |
177 | } |
178 | ||
179 | @Override | |
180 | public TestIndexer getIndexer() { | |
181 | return (TestIndexer) super.getIndexer(); | |
182 | } | |
183 | } | |
184 | ||
185 | // ------------------------------------------------------------------------ | |
186 | // Helper functions | |
187 | // ------------------------------------------------------------------------ | |
188 | ||
189 | /** | |
190 | * Creates the trace for the specified path | |
191 | * | |
192 | * @param path | |
193 | * the path | |
194 | * @return the created trace | |
195 | * @throws URISyntaxException | |
196 | * when error occurs | |
197 | * @throws IOException | |
198 | * when error occurs | |
199 | * @throws TmfTraceException | |
200 | * when error occurs | |
201 | */ | |
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); | |
207 | return trace; | |
208 | } | |
209 | ||
210 | private synchronized void setupTrace(final String path) { | |
211 | if (fTrace == null) { | |
212 | try { | |
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()); | |
220 | } | |
221 | } | |
222 | ||
223 | if (fEmptyTrace == null) { | |
224 | fEmptyTrace = new EmptyTestTrace(); | |
225 | fEmptyTrace.indexTrace(true); | |
226 | } | |
227 | } | |
228 | ||
229 | // ------------------------------------------------------------------------ | |
230 | // Verify checkpoints | |
231 | // ------------------------------------------------------------------------ | |
232 | ||
233 | /** | |
234 | * Test the content of the index after building the full index | |
235 | */ | |
236 | @Test | |
237 | public void testTmfTraceIndexing() { | |
238 | verifyIndexContent(); | |
239 | } | |
240 | ||
241 | /** | |
242 | * Verify the content of the index | |
243 | */ | |
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()); | |
251 | ||
252 | ITmfCheckpointIndex checkpoints = fTrace.getIndexer().getCheckpoints(); | |
253 | int pageSize = fTrace.getCacheSize(); | |
254 | assertTrue(checkpoints != null); | |
255 | assertEquals(NB_EVENTS / BLOCK_SIZE, checkpoints.size()); | |
256 | ||
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); | |
065cc19b | 263 | assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp()) == 0)); |
032ecd45 MAL |
264 | } |
265 | } | |
266 | ||
267 | /** | |
268 | * Test that a empty trace has the correct content | |
269 | */ | |
270 | @Test | |
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()); | |
278 | ||
279 | ITmfCheckpointIndex checkpoints = fEmptyTrace.getIndexer().getCheckpoints(); | |
280 | assertTrue(checkpoints != null); | |
281 | assertEquals(0, checkpoints.size()); | |
282 | } | |
283 | } |