tmf: Update tmf.core unit tests to JUnit4
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / TmfExperimentCheckpointIndexTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 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 * Alexandre Montplaisir - Port to JUnit4
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.tests.trace;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertTrue;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.net.URISyntaxException;
22 import java.net.URL;
23 import java.util.List;
24
25 import org.eclipse.core.runtime.FileLocator;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext;
28 import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentLocation;
29 import org.eclipse.linuxtools.internal.tmf.core.trace.TmfLocationArray;
30 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
31 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
32 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
33 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
34 import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
35 import org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint;
36 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
37 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
38 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
39 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub;
40 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44
45 /**
46 * Test suite for the TmfCheckpointIndexTest class.
47 */
48 @SuppressWarnings({"nls","javadoc"})
49 public class TmfExperimentCheckpointIndexTest {
50
51 // ------------------------------------------------------------------------
52 // Attributes
53 // ------------------------------------------------------------------------
54
55 private static final String DIRECTORY = "testfiles";
56 private static final String TEST_STREAM1 = "O-Test-10K";
57 private static final String TEST_STREAM2 = "E-Test-10K";
58 private static final String EXPERIMENT = "MyExperiment";
59 private static int NB_EVENTS = 20000;
60 private static int BLOCK_SIZE = 1000;
61
62 private static ITmfTrace[] fTestTraces;
63 private static TmfExperimentStub fExperiment;
64
65 // ------------------------------------------------------------------------
66 // Housekeeping
67 // ------------------------------------------------------------------------
68
69 @Before
70 public void setUp() {
71 setupTraces();
72 fExperiment = new TmfExperimentStub(EXPERIMENT, fTestTraces, BLOCK_SIZE);
73 fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true);
74 }
75
76 @After
77 public void tearDown() {
78 fExperiment.dispose();
79 fExperiment = null;
80 for (ITmfTrace trace : fTestTraces) {
81 trace.dispose();
82 }
83 fTestTraces = null;
84 }
85
86 private static void setupTraces() {
87 final String path1 = DIRECTORY + File.separator + TEST_STREAM1;
88 final String path2 = DIRECTORY + File.separator + TEST_STREAM2;
89
90 fTestTraces = new ITmfTrace[2];
91 try {
92 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path1), null);
93 File test = new File(FileLocator.toFileURL(location).toURI());
94 final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, true);
95 fTestTraces[0] = trace1;
96 location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path2), null);
97 test = new File(FileLocator.toFileURL(location).toURI());
98 final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, true);
99 fTestTraces[1] = trace2;
100 } catch (final TmfTraceException e) {
101 e.printStackTrace();
102 } catch (final URISyntaxException e) {
103 e.printStackTrace();
104 } catch (final IOException e) {
105 e.printStackTrace();
106 }
107 }
108
109 // ------------------------------------------------------------------------
110 // Verify checkpoints
111 // ------------------------------------------------------------------------
112
113 @Test
114 @SuppressWarnings("null")
115 public void testTmfTraceIndexing() {
116 assertEquals("getCacheSize", BLOCK_SIZE, fExperiment.getCacheSize());
117 assertEquals("getTraceSize", NB_EVENTS, fExperiment.getNbEvents());
118 assertEquals("getRange-start", 1, fExperiment.getTimeRange().getStartTime().getValue());
119 assertEquals("getRange-end", NB_EVENTS, fExperiment.getTimeRange().getEndTime().getValue());
120 assertEquals("getStartTime", 1, fExperiment.getStartTime().getValue());
121 assertEquals("getEndTime", NB_EVENTS, fExperiment.getEndTime().getValue());
122
123 List<ITmfCheckpoint> checkpoints = fExperiment.getIndexer().getCheckpoints();
124 int pageSize = fExperiment.getCacheSize();
125 assertTrue("Checkpoints exist", checkpoints != null);
126 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size());
127
128 // Validate that each checkpoint points to the right event
129 for (int i = 0; i < checkpoints.size(); i++) {
130 ITmfCheckpoint checkpoint = checkpoints.get(i);
131 TmfExperimentLocation expLocation = (TmfExperimentLocation) checkpoint.getLocation();
132 TmfLocationArray locations = expLocation.getLocationInfo();
133 ITmfContext[] trcContexts = new ITmfContext[2];
134 trcContexts[0] = new TmfContext(locations.getLocation(0), (i * pageSize) / 2);
135 trcContexts[1] = new TmfContext(locations.getLocation(1), (i * pageSize) / 2);
136 TmfExperimentContext expContext = new TmfExperimentContext(trcContexts);
137 expContext.getEvents()[0] = fTestTraces[0].getNext(fTestTraces[0].seekEvent((i * pageSize) / 2));
138 expContext.getEvents()[1] = fTestTraces[1].getNext(fTestTraces[1].seekEvent((i * pageSize) / 2));
139 ITmfEvent event = fExperiment.parseEvent(expContext);
140 assertTrue(expContext.getRank() == i * pageSize);
141 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
142 }
143 }
144
145 // ------------------------------------------------------------------------
146 // Streaming
147 // ------------------------------------------------------------------------
148
149 @Test
150 @SuppressWarnings("null")
151 public void testGrowingIndex() {
152 ITmfTrace[] testTraces = new TmfTraceStub[2];
153 try {
154 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM1), null);
155 File test = new File(FileLocator.toFileURL(location).toURI());
156 final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, false);
157 testTraces[0] = trace1;
158 location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM2), null);
159 test = new File(FileLocator.toFileURL(location).toURI());
160 final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, false);
161 testTraces[1] = trace2;
162 } catch (final TmfTraceException e) {
163 e.printStackTrace();
164 } catch (final URISyntaxException e) {
165 e.printStackTrace();
166 } catch (final IOException e) {
167 e.printStackTrace();
168 }
169
170 TmfExperimentStub experiment = new TmfExperimentStub(EXPERIMENT, testTraces, BLOCK_SIZE);
171 int pageSize = experiment.getCacheSize();
172
173 // Build the first half of the index
174 TmfTimeRange range = new TmfTimeRange(new TmfTimestamp(1, -3), new TmfTimestamp(NB_EVENTS / 2 - 1, -3));
175 experiment.getIndexer().buildIndex(0, range, true);
176
177 // Validate that each checkpoint points to the right event
178 List<ITmfCheckpoint> checkpoints = experiment.getIndexer().getCheckpoints();
179 assertTrue("Checkpoints exist", checkpoints != null);
180 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE / 2, checkpoints.size());
181
182 // Build the second half of the index
183 experiment.getIndexer().buildIndex(NB_EVENTS / 2, TmfTimeRange.ETERNITY, true);
184
185 // Validate that each checkpoint points to the right event
186 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size());
187 for (int i = 0; i < checkpoints.size(); i++) {
188 ITmfCheckpoint checkpoint = checkpoints.get(i);
189 TmfExperimentLocation expLocation = (TmfExperimentLocation) checkpoint.getLocation();
190 TmfLocationArray locations = expLocation.getLocationInfo();
191 ITmfContext[] trcContexts = new ITmfContext[2];
192 trcContexts[0] = new TmfContext(locations.getLocation(0), (i * pageSize) / 2);
193 trcContexts[1] = new TmfContext(locations.getLocation(1), (i * pageSize) / 2);
194 TmfExperimentContext expContext = new TmfExperimentContext(trcContexts);
195 expContext.getEvents()[0] = testTraces[0].getNext(testTraces[0].seekEvent((i * pageSize) / 2));
196 expContext.getEvents()[1] = testTraces[1].getNext(testTraces[1].seekEvent((i * pageSize) / 2));
197 ITmfEvent event = experiment.parseEvent(expContext);
198 assertTrue(expContext.getRank() == i * pageSize);
199 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
200 assertEquals("Checkpoint value", i * pageSize + 1, checkpoint.getTimestamp().getValue());
201 }
202
203 /* Clean up (since we didn't use the class-specific fixtures) */
204 experiment.dispose();
205 for (ITmfTrace trace : testTraces) {
206 trace.dispose();
207 }
208 }
209
210 }
This page took 0.036509 seconds and 6 git commands to generate.