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 / TmfCheckpointIndexTest2.java
CommitLineData
3eade83c 1/*******************************************************************************
6e1886bc 2 * Copyright (c) 2012, 2013 Ericsson
3eade83c
BH
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 * Bernd Hufmann - Initial API and implementation
6e1886bc 11 * Alexandre Montplaisir - Port to JUnit4
3eade83c
BH
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.tmf.core.tests.trace;
15
6e1886bc
AM
16import static org.junit.Assert.assertEquals;
17import static org.junit.Assert.assertNull;
18import static org.junit.Assert.assertTrue;
19
3eade83c
BH
20import java.io.File;
21import java.io.IOException;
22import java.net.URISyntaxException;
23import java.net.URL;
24import java.util.List;
25
3eade83c
BH
26import org.eclipse.core.runtime.FileLocator;
27import org.eclipse.core.runtime.Path;
28import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
29import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
30import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
31import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
32import org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint;
33import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
34import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpointIndexer;
35import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfEmptyTraceStub;
36import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
6e1886bc
AM
37import org.junit.After;
38import org.junit.Before;
39import org.junit.Test;
3eade83c
BH
40
41/**
42 * Test suite for the TmfCheckpointIndexer class (events with same
43 * timestamp around checkpoint).
44 */
45@SuppressWarnings({"nls","javadoc"})
6e1886bc 46public class TmfCheckpointIndexTest2 {
3eade83c
BH
47
48 // ------------------------------------------------------------------------
49 // Variables
50 // ------------------------------------------------------------------------
51
52 private static final String DIRECTORY = "testfiles";
53 // Trace has 3 events at t=101 at rank 99, 100, 101
54 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
55 private static final String TEST_STREAM = "A-Test-10K-2";
56 private static final int BLOCK_SIZE = 100;
57 private static final int NB_EVENTS = 702;
58 private static TestTrace fTrace = null;
59 private static EmptyTestTrace fEmptyTrace = null;
60
61 // ------------------------------------------------------------------------
62 // Housekeeping
63 // ------------------------------------------------------------------------
64
6e1886bc
AM
65 @Before
66 public void setUp() {
3eade83c
BH
67 setupTrace(DIRECTORY + File.separator + TEST_STREAM);
68 }
69
6e1886bc
AM
70 @After
71 public void tearDown() {
3eade83c
BH
72 fTrace.dispose();
73 fTrace = null;
74 fEmptyTrace.dispose();
75 fEmptyTrace = null;
76 }
77
78 // ------------------------------------------------------------------------
79 // Helper classes
80 // ------------------------------------------------------------------------
81
82 private static class TestIndexer extends TmfCheckpointIndexer {
83 @SuppressWarnings({ })
84 public TestIndexer(TestTrace testTrace) {
85 super(testTrace, BLOCK_SIZE);
86 }
87 @SuppressWarnings({ })
88 public TestIndexer(EmptyTestTrace testTrace) {
89 super(testTrace, BLOCK_SIZE);
90 }
91 public List<ITmfCheckpoint> getCheckpoints() {
92 return getTraceIndex();
93 }
94 }
95
96 private class TestTrace extends TmfTraceStub {
97 public TestTrace(String path, int blockSize) throws TmfTraceException {
98 super(path, blockSize);
99 setIndexer(new TestIndexer(this));
100 }
101 @Override
102 public TestIndexer getIndexer() {
103 return (TestIndexer) super.getIndexer();
104 }
105 }
106
107 private class EmptyTestTrace extends TmfEmptyTraceStub {
108 public EmptyTestTrace() {
109 super();
110 setIndexer(new TestIndexer(this));
111 }
112 @Override
113 public TestIndexer getIndexer() {
114 return (TestIndexer) super.getIndexer();
115 }
116 }
117
118 // ------------------------------------------------------------------------
119 // Helper functions
120 // ------------------------------------------------------------------------
121
122 private synchronized void setupTrace(final String path) {
123 if (fTrace == null) {
124 try {
125 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null);
126 final File test = new File(FileLocator.toFileURL(location).toURI());
127 fTrace = new TestTrace(test.toURI().getPath(), BLOCK_SIZE);
128 fTrace.indexTrace();
129 } catch (final TmfTraceException e) {
130 e.printStackTrace();
131 } catch (final URISyntaxException e) {
132 e.printStackTrace();
133 } catch (final IOException e) {
134 e.printStackTrace();
135 }
136 }
137
138 if (fEmptyTrace == null) {
139 fEmptyTrace = new EmptyTestTrace();
140 fEmptyTrace.indexTrace();
141 }
142 }
143
144 // ------------------------------------------------------------------------
145 // Verify checkpoints
146 // ------------------------------------------------------------------------
147
6e1886bc 148 @Test
3eade83c
BH
149 @SuppressWarnings("null")
150 public void testTmfTraceMultiTimestamps() {
151 assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize());
152 assertEquals("getTraceSize", NB_EVENTS, fTrace.getNbEvents());
153 assertEquals("getRange-start", 1, fTrace.getTimeRange().getStartTime().getValue());
154 assertEquals("getRange-end", 102, fTrace.getTimeRange().getEndTime().getValue());
155 assertEquals("getStartTime", 1, fTrace.getStartTime().getValue());
156 assertEquals("getEndTime", 102, fTrace.getEndTime().getValue());
157
158 List<ITmfCheckpoint> checkpoints = fTrace.getIndexer().getCheckpoints();
159 assertTrue("Checkpoints exist", checkpoints != null);
160 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE + 1, checkpoints.size());
161
162 // Trace has 3 events with same timestamp (ts=101) at rank 99, 100, 101
163
164 // Verify that the event at rank=99 is returned when seeking to ts=101 (first event with this timestamp)
165 // and not the event at checkpoint boundary
166 TmfTimestamp seekTs = new TmfTimestamp(101, -3, 0);
167 ITmfContext ctx = fTrace.seekEvent(seekTs);
168 ITmfEvent event = fTrace.getNext(ctx);
169
170 assertEquals(99, ctx.getRank());
171 assertEquals(0, seekTs.compareTo(event.getTimestamp(), false));
172
173 event = fTrace.getNext(ctx);
174
175 assertEquals(100, ctx.getRank());
176 assertEquals(0, seekTs.compareTo(event.getTimestamp(), false));
177
178 event = fTrace.getNext(ctx);
179
180 assertEquals(101, ctx.getRank());
181 assertEquals(0, seekTs.compareTo(event.getTimestamp(), false));
182
183 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
184 // Verify that the event at rank=102 is returned when seeking to ts=102 (first event with this timestamp)
185 // and not the event at checkpoint boundary
186 seekTs = new TmfTimestamp(102, -3, 0);
187 ctx = fTrace.seekEvent(seekTs);
188 event = fTrace.getNext(ctx);
189
190 assertEquals(102, ctx.getRank());
191 assertEquals(0, seekTs.compareTo(event.getTimestamp(), false));
192
193 // Verify seek to first checkpoint
194 seekTs = new TmfTimestamp(1, -3, 0);
195 ctx = fTrace.seekEvent(seekTs);
196 event = fTrace.getNext(ctx);
197
198 assertEquals(1, ctx.getRank());
199 assertEquals(0, seekTs.compareTo(event.getTimestamp(), false));
200
201 // Verify seek to timestamp before first event
202 seekTs = new TmfTimestamp(0, -3, 0);
203 ctx = fTrace.seekEvent(seekTs);
204 event = fTrace.getNext(ctx);
205
206 assertEquals(1, ctx.getRank());
207 assertEquals(0, new TmfTimestamp(1, -3, 0).compareTo(event.getTimestamp(), false));
208
209 // Verify seek to timestamp between first and second checkpoint
210 seekTs = new TmfTimestamp(50, -3, 0);
211 ctx = fTrace.seekEvent(seekTs);
212 event = fTrace.getNext(ctx);
213
214 assertEquals(50, ctx.getRank());
215 assertEquals(0, seekTs.compareTo(event.getTimestamp(), false));
216
217 // Verify seek to timestamp after last event in trace
218 seekTs = new TmfTimestamp(103, -3, 0);
219 ctx = fTrace.seekEvent(seekTs);
220 event = fTrace.getNext(ctx);
221
222 assertEquals(-1, ctx.getRank());
223 assertNull(event);
224 }
225}
This page took 0.032393 seconds and 5 git commands to generate.