ctf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / trace / CTFTraceReaderTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013 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.linuxtools.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.linuxtools.ctf.core.event.EventDefinition;
21 import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace;
22 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
23 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
24 import org.eclipse.linuxtools.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 CTFReaderException
46 */
47 @Before
48 public void setUp() throws CTFReaderException {
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 CTFReaderException
58 */
59 @Test
60 public void testOpen_existing() throws CTFReaderException {
61 try (CTFTrace trace = testTrace.getTrace();
62 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 CTFReaderException
72 */
73 @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class)
74 public void testOpen_nonexisting() throws CTFReaderException {
75 try (CTFTrace trace = new CTFTrace("badfile.bad");
76 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 CTFReaderException
86 */
87 @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class)
88 public void testOpen_invalid() throws CTFReaderException {
89 try (CTFTrace trace = new CTFTrace("");
90 CTFTraceReader result = new CTFTraceReader(trace);) {
91 assertNotNull(result);
92 }
93 }
94
95 /**
96 * Run the boolean advance() method test. Test advancing normally.
97 * @throws CTFReaderException error
98 */
99 @Test
100 public void testAdvance_normal() throws CTFReaderException {
101 boolean result = fixture.advance();
102 assertTrue(result);
103 }
104
105 /**
106 * Run the boolean advance() method test. Test advancing when we're at the
107 * end, so we expect that there is no more events.
108 * @throws CTFReaderException error
109 */
110 @Test
111 public void testAdvance_end() throws CTFReaderException {
112 int i = 0;
113 boolean result = fixture.advance();
114 while (result) {
115 result = fixture.advance();
116 i++;
117 }
118 fixture.seek(0);
119 fixture.advance();
120 fixture.goToLastEvent();
121 i = 1;
122 result = fixture.advance();
123 while (result) {
124 result = fixture.advance();
125 i++;
126 }
127 assertFalse(result);
128 assertEquals(i, 1);
129 }
130
131 /**
132 * Run the CTFTraceReader copy constructor test.
133 * @throws CTFReaderException error
134 */
135 @Test
136 public void testCopyFrom() throws CTFReaderException {
137 try (CTFTraceReader result = fixture.copyFrom();) {
138 assertNotNull(result);
139 }
140 }
141
142 /**
143 * Test the hashCode method.
144 */
145 @Test
146 public void testHash() {
147 int result = fixture.hashCode();
148 assertTrue(0 != result);
149 }
150
151 /**
152 * Test the equals method. Uses the class-wide 'fixture' and another
153 * method-local 'fixture2', which both point to the same trace.
154 *
155 * Both trace reader are different objects, so they shouldn't "equals" each
156 * other.
157 *
158 * @throws CTFReaderException
159 */
160 @Test
161 public void testEquals() throws CTFReaderException {
162 try (CTFTraceReader fixture2 = new CTFTraceReader(testTrace.getTrace());) {
163 assertEquals(fixture, fixture2);
164 }
165 }
166
167 /**
168 * Run the getCurrentEventDef() method test. Get the first event's
169 * definition.
170 */
171 @Test
172 public void testGetCurrentEventDef_first() {
173 EventDefinition result = fixture.getCurrentEventDef();
174 assertNotNull(result);
175 }
176
177 /**
178 * Run the getCurrentEventDef() method test. Get the last event's
179 * definition.
180 * @throws CTFReaderException error
181 */
182 @Test
183 public void testGetCurrentEventDef_last() throws CTFReaderException {
184 fixture.goToLastEvent();
185 EventDefinition result = fixture.getCurrentEventDef();
186 assertNotNull(result);
187 }
188
189 /**
190 * Run the long getEndTime() method test.
191 */
192 @Test
193 public void testGetEndTime() {
194 long result = fixture.getEndTime();
195 assertTrue(0L < result);
196 }
197
198 /**
199 * Run the long getStartTime() method test.
200 */
201 @Test
202 public void testGetStartTime() {
203 long result = fixture.getStartTime();
204 assertTrue(0L < result);
205 }
206
207 /**
208 * Run the void goToLastEvent() method test.
209 * @throws CTFReaderException error
210 */
211 @Test
212 public void testGoToLastEvent() throws CTFReaderException {
213 fixture.goToLastEvent();
214 long ts1 = getTimestamp();
215 long ts2 = fixture.getEndTime();
216 assertEquals(ts1, ts2);
217 }
218
219 /**
220 * Run the boolean hasMoreEvents() method test.
221 *
222 * @throws CTFReaderException
223 */
224 @Test
225 public void testHasMoreEvents() {
226 boolean result = fixture.hasMoreEvents();
227 assertTrue(result);
228 }
229
230 /**
231 * Run the void printStats() method test with no 'width' parameter.
232 * @throws CTFReaderException error
233 */
234 @Test
235 public void testPrintStats_noparam() throws CTFReaderException {
236 fixture.advance();
237 fixture.printStats();
238 }
239
240 /**
241 * Run the void printStats(int) method test with width = 0.
242 * @throws CTFReaderException error
243 */
244 @Test
245 public void testPrintStats_width0() throws CTFReaderException {
246 fixture.advance();
247 fixture.printStats(0);
248 }
249
250 /**
251 * Run the void printStats(int) method test with width = 1.
252 * @throws CTFReaderException error
253 */
254 @Test
255 public void testPrintStats_width1() throws CTFReaderException {
256 fixture.advance();
257 fixture.printStats(1);
258 }
259
260 /**
261 * Run the void printStats(int) method test with width = 2.
262 * @throws CTFReaderException error
263 */
264 @Test
265 public void testPrintStats_width2() throws CTFReaderException {
266 fixture.advance();
267 fixture.printStats(2);
268 }
269
270 /**
271 * Run the void printStats(int) method test with width = 10.
272 * @throws CTFReaderException error
273 */
274 @Test
275 public void testPrintStats_width10() throws CTFReaderException {
276 fixture.advance();
277 fixture.printStats(10);
278 }
279
280 /**
281 * Run the void printStats(int) method test with width = 100.
282 * @throws CTFReaderException error
283 */
284 @Test
285 public void testPrintStats_100() throws CTFReaderException {
286 for (int i = 0; i < 1000; i++) {
287 fixture.advance();
288 }
289 fixture.printStats(100);
290 }
291
292 /**
293 * Run the boolean seek(long) method test.
294 * @throws CTFReaderException error
295 */
296 @Test
297 public void testSeek() throws CTFReaderException {
298 long timestamp = 1L;
299 boolean result = fixture.seek(timestamp);
300 assertTrue(result);
301 }
302
303
304
305 /**
306 * @return
307 */
308 private long getTimestamp() {
309 if (fixture.getCurrentEventDef() != null) {
310 return fixture.getTrace().timestampCyclesToNanos(fixture.getCurrentEventDef().getTimestamp());
311 }
312 return -1;
313 }
314 }
This page took 0.044741 seconds and 5 git commands to generate.