Tmf: Rework test trace stub for non-ctf traces
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / TmfCheckpointIndexTest.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 *******************************************************************************/
14
15 package org.eclipse.linuxtools.tmf.core.tests.trace;
16
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertTrue;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24 import java.util.List;
25
26 import org.eclipse.core.runtime.FileLocator;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
29 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
30 import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
31 import org.eclipse.linuxtools.tmf.core.tests.shared.TmfTestTrace;
32 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
33 import org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint;
34 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
35 import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpointIndexer;
36 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
37 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfEmptyTraceStub;
38 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42
43 /**
44 * Test suite for the TmfCheckpointIndexTest class.
45 */
46 @SuppressWarnings("javadoc")
47 public class TmfCheckpointIndexTest {
48
49 // ------------------------------------------------------------------------
50 // Variables
51 // ------------------------------------------------------------------------
52
53 private static final int BLOCK_SIZE = 100;
54 private static final int NB_EVENTS = 10000;
55 private static TestTrace fTrace = null;
56 private static EmptyTestTrace fEmptyTrace = null;
57
58 // ------------------------------------------------------------------------
59 // Housekeeping
60 // ------------------------------------------------------------------------
61
62 @Before
63 public void setUp() {
64 setupTrace(TmfTestTrace.A_TEST_10K.getFullPath());
65 }
66
67 @After
68 public void tearDown() {
69 fTrace.dispose();
70 fTrace = null;
71 fEmptyTrace.dispose();
72 fEmptyTrace = null;
73 }
74
75 // ------------------------------------------------------------------------
76 // Helper classes
77 // ------------------------------------------------------------------------
78
79 private static class TestIndexer extends TmfCheckpointIndexer {
80 @SuppressWarnings({ })
81 public TestIndexer(TestTrace testTrace) {
82 super(testTrace, BLOCK_SIZE);
83 }
84 @SuppressWarnings({ })
85 public TestIndexer(EmptyTestTrace testTrace) {
86 super(testTrace, BLOCK_SIZE);
87 }
88 public List<ITmfCheckpoint> getCheckpoints() {
89 return getTraceIndex();
90 }
91 }
92
93 private class TestTrace extends TmfTraceStub {
94 public TestTrace(String path, int blockSize) throws TmfTraceException {
95 super(path, blockSize);
96 setIndexer(new TestIndexer(this));
97 }
98 @Override
99 public TestIndexer getIndexer() {
100 return (TestIndexer) super.getIndexer();
101 }
102 }
103
104 private class EmptyTestTrace extends TmfEmptyTraceStub {
105 public EmptyTestTrace() {
106 super();
107 setIndexer(new TestIndexer(this));
108 }
109 @Override
110 public TestIndexer getIndexer() {
111 return (TestIndexer) super.getIndexer();
112 }
113 }
114
115 // ------------------------------------------------------------------------
116 // Helper functions
117 // ------------------------------------------------------------------------
118
119 private synchronized void setupTrace(final String path) {
120 if (fTrace == null) {
121 try {
122 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null);
123 final File test = new File(FileLocator.toFileURL(location).toURI());
124 fTrace = new TestTrace(test.toURI().getPath(), BLOCK_SIZE);
125 fTrace.indexTrace(true);
126 } catch (final TmfTraceException e) {
127 e.printStackTrace();
128 } catch (final URISyntaxException e) {
129 e.printStackTrace();
130 } catch (final IOException e) {
131 e.printStackTrace();
132 }
133 }
134
135 if (fEmptyTrace == null) {
136 fEmptyTrace = new EmptyTestTrace();
137 fEmptyTrace.indexTrace(true);
138 }
139 }
140
141 // ------------------------------------------------------------------------
142 // Verify checkpoints
143 // ------------------------------------------------------------------------
144
145 @Test
146 public void testTmfTraceIndexing() {
147 assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize());
148 assertEquals("getTraceSize", NB_EVENTS, fTrace.getNbEvents());
149 assertEquals("getRange-start", 1, fTrace.getTimeRange().getStartTime().getValue());
150 assertEquals("getRange-end", NB_EVENTS, fTrace.getTimeRange().getEndTime().getValue());
151 assertEquals("getStartTime", 1, fTrace.getStartTime().getValue());
152 assertEquals("getEndTime", NB_EVENTS, fTrace.getEndTime().getValue());
153
154 List<ITmfCheckpoint> checkpoints = fTrace.getIndexer().getCheckpoints();
155 int pageSize = fTrace.getCacheSize();
156 assertTrue("Checkpoints exist", checkpoints != null);
157 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size());
158
159 // Validate that each checkpoint points to the right event
160 for (int i = 0; i < checkpoints.size(); i++) {
161 ITmfCheckpoint checkpoint = checkpoints.get(i);
162 TmfContext context = new TmfContext(checkpoint.getLocation(), i * pageSize);
163 ITmfEvent event = fTrace.parseEvent(context);
164 assertTrue(context.getRank() == i * pageSize);
165 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
166 }
167 }
168
169 @Test
170 public void testEmptyTmfTraceIndexing() {
171 assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, fEmptyTrace.getCacheSize());
172 assertEquals("getTraceSize", 0, fEmptyTrace.getNbEvents());
173 assertEquals("getRange-start", TmfTimestamp.BIG_BANG, fEmptyTrace.getTimeRange().getStartTime());
174 assertEquals("getRange-end", TmfTimestamp.BIG_BANG, fEmptyTrace.getTimeRange().getEndTime());
175 assertEquals("getStartTime", TmfTimestamp.BIG_BANG, fEmptyTrace.getStartTime());
176 assertEquals("getEndTime", TmfTimestamp.BIG_BANG, fEmptyTrace.getEndTime());
177
178 List<ITmfCheckpoint> checkpoints = fEmptyTrace.getIndexer().getCheckpoints();
179 int pageSize = fEmptyTrace.getCacheSize();
180 assertTrue("Checkpoints exist", checkpoints != null);
181 assertEquals("Checkpoints size", 0, checkpoints.size());
182
183 // Validate that each checkpoint points to the right event
184 for (int i = 0; i < checkpoints.size(); i++) {
185 ITmfCheckpoint checkpoint = checkpoints.get(i);
186 TmfContext context = new TmfContext(checkpoint.getLocation(), i * pageSize);
187 ITmfEvent event = fEmptyTrace.parseEvent(context);
188 assertTrue(context.getRank() == i * pageSize);
189 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
190 }
191 }
192
193 }
This page took 0.050905 seconds and 5 git commands to generate.