rcp: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / trace / CTFTraceReaderTest.java
CommitLineData
4bd7f2db 1/*******************************************************************************
b562a24f 2 * Copyright (c) 2013, 2014 Ericsson
4bd7f2db
AM
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
f357bcd4 12package org.eclipse.tracecompass.ctf.core.tests.trace;
866e5b51 13
ce2388e0 14import static org.junit.Assert.assertEquals;
866e5b51
FC
15import static org.junit.Assert.assertFalse;
16import static org.junit.Assert.assertNotNull;
17import static org.junit.Assert.assertTrue;
e5acb357 18import static org.junit.Assume.assumeTrue;
866e5b51 19
680f9173 20import org.eclipse.tracecompass.ctf.core.CTFException;
f357bcd4
AM
21import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
22import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTrace;
f357bcd4
AM
23import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
24import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
866e5b51
FC
25import org.junit.Before;
26import org.junit.Test;
27
28/**
29 * The class <code>CTFTraceReaderTest</code> contains tests for the class
30 * <code>{@link CTFTraceReader}</code>.
31 *
32 * @author ematkho
33 * @version $Revision: 1.0 $
34 */
be6df2d8 35@SuppressWarnings("javadoc")
866e5b51
FC
36public class CTFTraceReaderTest {
37
9ac63b5b 38 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
32bf80d2
AM
39
40 private CTFTraceReader fixture;
866e5b51 41
866e5b51
FC
42 /**
43 * Perform pre-test initialization.
bfe038ff 44 *
680f9173 45 * @throws CTFException
866e5b51
FC
46 */
47 @Before
680f9173 48 public void setUp() throws CTFException {
9ac63b5b
AM
49 assumeTrue(testTrace.exists());
50 fixture = new CTFTraceReader(testTrace.getTrace());
866e5b51
FC
51 }
52
866e5b51
FC
53 /**
54 * Run the CTFTraceReader(CTFTrace) constructor test. Open a known good
55 * trace.
bfe038ff 56 *
680f9173 57 * @throws CTFException
866e5b51
FC
58 */
59 @Test
680f9173 60 public void testOpen_existing() throws CTFException {
b562a24f
MK
61 CTFTrace trace = testTrace.getTrace();
62 try (CTFTraceReader result = new CTFTraceReader(trace);) {
05ce5fef
AM
63 assertNotNull(result);
64 }
866e5b51
FC
65 }
66
67 /**
68 * Run the CTFTraceReader(CTFTrace) constructor test. Open a non-existing
69 * trace, expect the exception.
70 *
680f9173 71 * @throws CTFException
866e5b51 72 */
680f9173
MK
73 @Test(expected = org.eclipse.tracecompass.ctf.core.CTFException.class)
74 public void testOpen_nonexisting() throws CTFException {
b562a24f
MK
75 CTFTrace trace = new CTFTrace("badfile.bad");
76 try (CTFTraceReader result = new CTFTraceReader(trace);) {
05ce5fef
AM
77 assertNotNull(result);
78 }
866e5b51
FC
79 }
80
81 /**
82 * Run the CTFTraceReader(CTFTrace) constructor test. Try to pen an invalid
83 * path, expect exception.
84 *
680f9173 85 * @throws CTFException
866e5b51 86 */
680f9173
MK
87 @Test(expected = org.eclipse.tracecompass.ctf.core.CTFException.class)
88 public void testOpen_invalid() throws CTFException {
b562a24f
MK
89 CTFTrace trace = new CTFTrace("");
90 try (CTFTraceReader result = new CTFTraceReader(trace);) {
05ce5fef
AM
91 assertNotNull(result);
92 }
866e5b51
FC
93 }
94
95 /**
96 * Run the boolean advance() method test. Test advancing normally.
b562a24f 97 *
680f9173 98 * @throws CTFException
b562a24f 99 * error
866e5b51
FC
100 */
101 @Test
680f9173 102 public void testAdvance_normal() throws CTFException {
866e5b51
FC
103 boolean result = fixture.advance();
104 assertTrue(result);
105 }
106
107 /**
108 * Run the boolean advance() method test. Test advancing when we're at the
109 * end, so we expect that there is no more events.
b562a24f 110 *
680f9173 111 * @throws CTFException
b562a24f 112 * error
866e5b51
FC
113 */
114 @Test
680f9173 115 public void testAdvance_end() throws CTFException {
bfe038ff
MK
116 int i = 0;
117 boolean result = fixture.advance();
118 while (result) {
119 result = fixture.advance();
120 i++;
121 }
122 fixture.seek(0);
123 fixture.advance();
866e5b51 124 fixture.goToLastEvent();
bfe038ff
MK
125 i = 1;
126 result = fixture.advance();
127 while (result) {
128 result = fixture.advance();
129 i++;
866e5b51 130 }
866e5b51 131 assertFalse(result);
bfe038ff 132 assertEquals(i, 1);
866e5b51
FC
133 }
134
135 /**
136 * Run the CTFTraceReader copy constructor test.
b562a24f 137 *
680f9173 138 * @throws CTFException
b562a24f 139 * error
866e5b51
FC
140 */
141 @Test
680f9173 142 public void testCopyFrom() throws CTFException {
05ce5fef
AM
143 try (CTFTraceReader result = fixture.copyFrom();) {
144 assertNotNull(result);
145 }
866e5b51
FC
146 }
147
148 /**
149 * Test the hashCode method.
150 */
151 @Test
152 public void testHash() {
153 int result = fixture.hashCode();
154 assertTrue(0 != result);
155 }
156
157 /**
158 * Test the equals method. Uses the class-wide 'fixture' and another
159 * method-local 'fixture2', which both point to the same trace.
160 *
161 * Both trace reader are different objects, so they shouldn't "equals" each
162 * other.
bfe038ff 163 *
680f9173 164 * @throws CTFException
866e5b51
FC
165 */
166 @Test
680f9173 167 public void testEquals() throws CTFException {
05ce5fef
AM
168 try (CTFTraceReader fixture2 = new CTFTraceReader(testTrace.getTrace());) {
169 assertEquals(fixture, fixture2);
170 }
866e5b51
FC
171 }
172
173 /**
174 * Run the getCurrentEventDef() method test. Get the first event's
175 * definition.
176 */
177 @Test
178 public void testGetCurrentEventDef_first() {
179 EventDefinition result = fixture.getCurrentEventDef();
180 assertNotNull(result);
181 }
182
183 /**
184 * Run the getCurrentEventDef() method test. Get the last event's
185 * definition.
b562a24f 186 *
680f9173 187 * @throws CTFException
b562a24f 188 * error
866e5b51
FC
189 */
190 @Test
680f9173 191 public void testGetCurrentEventDef_last() throws CTFException {
866e5b51
FC
192 fixture.goToLastEvent();
193 EventDefinition result = fixture.getCurrentEventDef();
194 assertNotNull(result);
195 }
196
197 /**
198 * Run the long getEndTime() method test.
199 */
200 @Test
201 public void testGetEndTime() {
202 long result = fixture.getEndTime();
203 assertTrue(0L < result);
204 }
205
206 /**
207 * Run the long getStartTime() method test.
208 */
209 @Test
210 public void testGetStartTime() {
211 long result = fixture.getStartTime();
212 assertTrue(0L < result);
213 }
214
215 /**
216 * Run the void goToLastEvent() method test.
b562a24f 217 *
680f9173 218 * @throws CTFException
b562a24f 219 * error
866e5b51
FC
220 */
221 @Test
680f9173 222 public void testGoToLastEvent() throws CTFException {
866e5b51 223 fixture.goToLastEvent();
ce2388e0 224 long ts1 = getTimestamp();
866e5b51 225 long ts2 = fixture.getEndTime();
bfe038ff 226 assertEquals(ts1, ts2);
866e5b51
FC
227 }
228
229 /**
230 * Run the boolean hasMoreEvents() method test.
231 *
680f9173 232 * @throws CTFException
866e5b51
FC
233 */
234 @Test
235 public void testHasMoreEvents() {
236 boolean result = fixture.hasMoreEvents();
237 assertTrue(result);
238 }
239
240 /**
241 * Run the void printStats() method test with no 'width' parameter.
b562a24f 242 *
680f9173 243 * @throws CTFException
b562a24f 244 * error
866e5b51
FC
245 */
246 @Test
680f9173 247 public void testPrintStats_noparam() throws CTFException {
866e5b51
FC
248 fixture.advance();
249 fixture.printStats();
250 }
251
252 /**
253 * Run the void printStats(int) method test with width = 0.
b562a24f 254 *
680f9173 255 * @throws CTFException
b562a24f 256 * error
866e5b51
FC
257 */
258 @Test
680f9173 259 public void testPrintStats_width0() throws CTFException {
866e5b51
FC
260 fixture.advance();
261 fixture.printStats(0);
262 }
263
264 /**
265 * Run the void printStats(int) method test with width = 1.
b562a24f 266 *
680f9173 267 * @throws CTFException
b562a24f 268 * error
866e5b51
FC
269 */
270 @Test
680f9173 271 public void testPrintStats_width1() throws CTFException {
866e5b51
FC
272 fixture.advance();
273 fixture.printStats(1);
274 }
275
276 /**
277 * Run the void printStats(int) method test with width = 2.
b562a24f 278 *
680f9173 279 * @throws CTFException
b562a24f 280 * error
866e5b51
FC
281 */
282 @Test
680f9173 283 public void testPrintStats_width2() throws CTFException {
866e5b51
FC
284 fixture.advance();
285 fixture.printStats(2);
286 }
287
288 /**
289 * Run the void printStats(int) method test with width = 10.
b562a24f 290 *
680f9173 291 * @throws CTFException
b562a24f 292 * error
866e5b51
FC
293 */
294 @Test
680f9173 295 public void testPrintStats_width10() throws CTFException {
866e5b51
FC
296 fixture.advance();
297 fixture.printStats(10);
298 }
299
300 /**
301 * Run the void printStats(int) method test with width = 100.
b562a24f 302 *
680f9173 303 * @throws CTFException
b562a24f 304 * error
866e5b51
FC
305 */
306 @Test
680f9173 307 public void testPrintStats_100() throws CTFException {
866e5b51
FC
308 for (int i = 0; i < 1000; i++) {
309 fixture.advance();
310 }
311 fixture.printStats(100);
312 }
313
314 /**
315 * Run the boolean seek(long) method test.
b562a24f 316 *
680f9173 317 * @throws CTFException
b562a24f 318 * error
866e5b51
FC
319 */
320 @Test
680f9173 321 public void testSeek() throws CTFException {
866e5b51
FC
322 long timestamp = 1L;
323 boolean result = fixture.seek(timestamp);
324 assertTrue(result);
325 }
ce2388e0 326
ce2388e0
FC
327 /**
328 * @return
329 */
330 private long getTimestamp() {
bfe038ff 331 if (fixture.getCurrentEventDef() != null) {
1d7277f3 332 return fixture.getTrace().timestampCyclesToNanos(fixture.getCurrentEventDef().getTimestamp());
bfe038ff
MK
333 }
334 return -1;
ce2388e0 335 }
866e5b51 336}
This page took 0.09161 seconds and 5 git commands to generate.