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
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
12 package org.eclipse.tracecompass.ctf.core.tests.trace;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assume.assumeTrue;
19
20 import org.eclipse.tracecompass.ctf.core.CTFException;
21 import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
22 import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTrace;
23 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
24 import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
25 import org.junit.Before;
26 import 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 */
35 @SuppressWarnings("javadoc")
36 public class CTFTraceReaderTest {
37
38 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
39
40 private CTFTraceReader fixture;
41
42 /**
43 * Perform pre-test initialization.
44 *
45 * @throws CTFException
46 */
47 @Before
48 public void setUp() throws CTFException {
49 assumeTrue(testTrace.exists());
50 fixture = new CTFTraceReader(testTrace.getTrace());
51 }
52
53 /**
54 * Run the CTFTraceReader(CTFTrace) constructor test. Open a known good
55 * trace.
56 *
57 * @throws CTFException
58 */
59 @Test
60 public void testOpen_existing() throws CTFException {
61 CTFTrace trace = testTrace.getTrace();
62 try (CTFTraceReader result = new CTFTraceReader(trace);) {
63 assertNotNull(result);
64 }
65 }
66
67 /**
68 * Run the CTFTraceReader(CTFTrace) constructor test. Open a non-existing
69 * trace, expect the exception.
70 *
71 * @throws CTFException
72 */
73 @Test(expected = org.eclipse.tracecompass.ctf.core.CTFException.class)
74 public void testOpen_nonexisting() throws CTFException {
75 CTFTrace trace = new CTFTrace("badfile.bad");
76 try (CTFTraceReader result = new CTFTraceReader(trace);) {
77 assertNotNull(result);
78 }
79 }
80
81 /**
82 * Run the CTFTraceReader(CTFTrace) constructor test. Try to pen an invalid
83 * path, expect exception.
84 *
85 * @throws CTFException
86 */
87 @Test(expected = org.eclipse.tracecompass.ctf.core.CTFException.class)
88 public void testOpen_invalid() throws CTFException {
89 CTFTrace trace = new CTFTrace("");
90 try (CTFTraceReader result = new CTFTraceReader(trace);) {
91 assertNotNull(result);
92 }
93 }
94
95 /**
96 * Run the boolean advance() method test. Test advancing normally.
97 *
98 * @throws CTFException
99 * error
100 */
101 @Test
102 public void testAdvance_normal() throws CTFException {
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.
110 *
111 * @throws CTFException
112 * error
113 */
114 @Test
115 public void testAdvance_end() throws CTFException {
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();
124 fixture.goToLastEvent();
125 i = 1;
126 result = fixture.advance();
127 while (result) {
128 result = fixture.advance();
129 i++;
130 }
131 assertFalse(result);
132 assertEquals(i, 1);
133 }
134
135 /**
136 * Run the CTFTraceReader copy constructor test.
137 *
138 * @throws CTFException
139 * error
140 */
141 @Test
142 public void testCopyFrom() throws CTFException {
143 try (CTFTraceReader result = fixture.copyFrom();) {
144 assertNotNull(result);
145 }
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.
163 *
164 * @throws CTFException
165 */
166 @Test
167 public void testEquals() throws CTFException {
168 try (CTFTraceReader fixture2 = new CTFTraceReader(testTrace.getTrace());) {
169 assertEquals(fixture, fixture2);
170 }
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.
186 *
187 * @throws CTFException
188 * error
189 */
190 @Test
191 public void testGetCurrentEventDef_last() throws CTFException {
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.
217 *
218 * @throws CTFException
219 * error
220 */
221 @Test
222 public void testGoToLastEvent() throws CTFException {
223 fixture.goToLastEvent();
224 long ts1 = getTimestamp();
225 long ts2 = fixture.getEndTime();
226 assertEquals(ts1, ts2);
227 }
228
229 /**
230 * Run the boolean hasMoreEvents() method test.
231 *
232 * @throws CTFException
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.
242 *
243 * @throws CTFException
244 * error
245 */
246 @Test
247 public void testPrintStats_noparam() throws CTFException {
248 fixture.advance();
249 fixture.printStats();
250 }
251
252 /**
253 * Run the void printStats(int) method test with width = 0.
254 *
255 * @throws CTFException
256 * error
257 */
258 @Test
259 public void testPrintStats_width0() throws CTFException {
260 fixture.advance();
261 fixture.printStats(0);
262 }
263
264 /**
265 * Run the void printStats(int) method test with width = 1.
266 *
267 * @throws CTFException
268 * error
269 */
270 @Test
271 public void testPrintStats_width1() throws CTFException {
272 fixture.advance();
273 fixture.printStats(1);
274 }
275
276 /**
277 * Run the void printStats(int) method test with width = 2.
278 *
279 * @throws CTFException
280 * error
281 */
282 @Test
283 public void testPrintStats_width2() throws CTFException {
284 fixture.advance();
285 fixture.printStats(2);
286 }
287
288 /**
289 * Run the void printStats(int) method test with width = 10.
290 *
291 * @throws CTFException
292 * error
293 */
294 @Test
295 public void testPrintStats_width10() throws CTFException {
296 fixture.advance();
297 fixture.printStats(10);
298 }
299
300 /**
301 * Run the void printStats(int) method test with width = 100.
302 *
303 * @throws CTFException
304 * error
305 */
306 @Test
307 public void testPrintStats_100() throws CTFException {
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.
316 *
317 * @throws CTFException
318 * error
319 */
320 @Test
321 public void testSeek() throws CTFException {
322 long timestamp = 1L;
323 boolean result = fixture.seek(timestamp);
324 assertTrue(result);
325 }
326
327 /**
328 * @return
329 */
330 private long getTimestamp() {
331 if (fixture.getCurrentEventDef() != null) {
332 return fixture.getTrace().timestampCyclesToNanos(fixture.getCurrentEventDef().getTimestamp());
333 }
334 return -1;
335 }
336 }
This page took 0.052607 seconds and 5 git commands to generate.