Commit | Line | Data |
---|---|---|
4bd7f2db AM |
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 | ||
866e5b51 FC |
12 | package org.eclipse.linuxtools.ctf.core.tests.trace; |
13 | ||
ce2388e0 | 14 | import static org.junit.Assert.assertEquals; |
866e5b51 FC |
15 | import static org.junit.Assert.assertFalse; |
16 | import static org.junit.Assert.assertNotNull; | |
17 | import static org.junit.Assert.assertTrue; | |
e5acb357 | 18 | import static org.junit.Assume.assumeTrue; |
866e5b51 FC |
19 | |
20 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; | |
9ac63b5b | 21 | import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace; |
866e5b51 FC |
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; | |
866e5b51 FC |
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 | */ | |
be6df2d8 | 35 | @SuppressWarnings("javadoc") |
866e5b51 FC |
36 | public 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 | * |
ce2388e0 | 45 | * @throws CTFReaderException |
866e5b51 FC |
46 | */ |
47 | @Before | |
13be1a8f | 48 | public void setUp() throws CTFReaderException { |
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 | * |
ce2388e0 | 57 | * @throws CTFReaderException |
866e5b51 FC |
58 | */ |
59 | @Test | |
13be1a8f | 60 | public void testOpen_existing() throws CTFReaderException { |
05ce5fef AM |
61 | try (CTFTrace trace = testTrace.getTrace(); |
62 | CTFTraceReader result = new CTFTraceReader(trace);) { | |
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 | * | |
71 | * @throws CTFReaderException | |
72 | */ | |
73 | @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class) | |
74 | public void testOpen_nonexisting() throws CTFReaderException { | |
05ce5fef AM |
75 | try (CTFTrace trace = new CTFTrace("badfile.bad"); |
76 | CTFTraceReader result = new CTFTraceReader(trace);) { | |
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 | * | |
85 | * @throws CTFReaderException | |
86 | */ | |
87 | @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class) | |
88 | public void testOpen_invalid() throws CTFReaderException { | |
05ce5fef AM |
89 | try (CTFTrace trace = new CTFTrace(""); |
90 | CTFTraceReader result = new CTFTraceReader(trace);) { | |
91 | assertNotNull(result); | |
92 | } | |
866e5b51 FC |
93 | } |
94 | ||
95 | /** | |
96 | * Run the boolean advance() method test. Test advancing normally. | |
db8e8f7d | 97 | * @throws CTFReaderException error |
866e5b51 FC |
98 | */ |
99 | @Test | |
db8e8f7d | 100 | public void testAdvance_normal() throws CTFReaderException { |
866e5b51 FC |
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. | |
db8e8f7d | 108 | * @throws CTFReaderException error |
866e5b51 FC |
109 | */ |
110 | @Test | |
db8e8f7d | 111 | public void testAdvance_end() throws CTFReaderException { |
bfe038ff MK |
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(); | |
866e5b51 | 120 | fixture.goToLastEvent(); |
bfe038ff MK |
121 | i = 1; |
122 | result = fixture.advance(); | |
123 | while (result) { | |
124 | result = fixture.advance(); | |
125 | i++; | |
866e5b51 | 126 | } |
866e5b51 | 127 | assertFalse(result); |
bfe038ff | 128 | assertEquals(i, 1); |
866e5b51 FC |
129 | } |
130 | ||
131 | /** | |
132 | * Run the CTFTraceReader copy constructor test. | |
db8e8f7d | 133 | * @throws CTFReaderException error |
866e5b51 FC |
134 | */ |
135 | @Test | |
db8e8f7d | 136 | public void testCopyFrom() throws CTFReaderException { |
05ce5fef AM |
137 | try (CTFTraceReader result = fixture.copyFrom();) { |
138 | assertNotNull(result); | |
139 | } | |
866e5b51 FC |
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. | |
bfe038ff | 157 | * |
ce2388e0 | 158 | * @throws CTFReaderException |
866e5b51 FC |
159 | */ |
160 | @Test | |
13be1a8f | 161 | public void testEquals() throws CTFReaderException { |
05ce5fef AM |
162 | try (CTFTraceReader fixture2 = new CTFTraceReader(testTrace.getTrace());) { |
163 | assertEquals(fixture, fixture2); | |
164 | } | |
866e5b51 FC |
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. | |
db8e8f7d | 180 | * @throws CTFReaderException error |
866e5b51 FC |
181 | */ |
182 | @Test | |
db8e8f7d | 183 | public void testGetCurrentEventDef_last() throws CTFReaderException { |
866e5b51 FC |
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. | |
db8e8f7d | 209 | * @throws CTFReaderException error |
866e5b51 FC |
210 | */ |
211 | @Test | |
db8e8f7d | 212 | public void testGoToLastEvent() throws CTFReaderException { |
866e5b51 | 213 | fixture.goToLastEvent(); |
ce2388e0 | 214 | long ts1 = getTimestamp(); |
866e5b51 | 215 | long ts2 = fixture.getEndTime(); |
bfe038ff | 216 | assertEquals(ts1, ts2); |
866e5b51 FC |
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. | |
db8e8f7d | 232 | * @throws CTFReaderException error |
866e5b51 FC |
233 | */ |
234 | @Test | |
db8e8f7d | 235 | public void testPrintStats_noparam() throws CTFReaderException { |
866e5b51 FC |
236 | fixture.advance(); |
237 | fixture.printStats(); | |
238 | } | |
239 | ||
240 | /** | |
241 | * Run the void printStats(int) method test with width = 0. | |
db8e8f7d | 242 | * @throws CTFReaderException error |
866e5b51 FC |
243 | */ |
244 | @Test | |
db8e8f7d | 245 | public void testPrintStats_width0() throws CTFReaderException { |
866e5b51 FC |
246 | fixture.advance(); |
247 | fixture.printStats(0); | |
248 | } | |
249 | ||
250 | /** | |
251 | * Run the void printStats(int) method test with width = 1. | |
db8e8f7d | 252 | * @throws CTFReaderException error |
866e5b51 FC |
253 | */ |
254 | @Test | |
db8e8f7d | 255 | public void testPrintStats_width1() throws CTFReaderException { |
866e5b51 FC |
256 | fixture.advance(); |
257 | fixture.printStats(1); | |
258 | } | |
259 | ||
260 | /** | |
261 | * Run the void printStats(int) method test with width = 2. | |
db8e8f7d | 262 | * @throws CTFReaderException error |
866e5b51 FC |
263 | */ |
264 | @Test | |
db8e8f7d | 265 | public void testPrintStats_width2() throws CTFReaderException { |
866e5b51 FC |
266 | fixture.advance(); |
267 | fixture.printStats(2); | |
268 | } | |
269 | ||
270 | /** | |
271 | * Run the void printStats(int) method test with width = 10. | |
db8e8f7d | 272 | * @throws CTFReaderException error |
866e5b51 FC |
273 | */ |
274 | @Test | |
db8e8f7d | 275 | public void testPrintStats_width10() throws CTFReaderException { |
866e5b51 FC |
276 | fixture.advance(); |
277 | fixture.printStats(10); | |
278 | } | |
279 | ||
280 | /** | |
281 | * Run the void printStats(int) method test with width = 100. | |
db8e8f7d | 282 | * @throws CTFReaderException error |
866e5b51 FC |
283 | */ |
284 | @Test | |
db8e8f7d | 285 | public void testPrintStats_100() throws CTFReaderException { |
866e5b51 FC |
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. | |
db8e8f7d | 294 | * @throws CTFReaderException error |
866e5b51 FC |
295 | */ |
296 | @Test | |
db8e8f7d | 297 | public void testSeek() throws CTFReaderException { |
866e5b51 FC |
298 | long timestamp = 1L; |
299 | boolean result = fixture.seek(timestamp); | |
300 | assertTrue(result); | |
301 | } | |
ce2388e0 FC |
302 | |
303 | ||
ce2388e0 FC |
304 | |
305 | /** | |
306 | * @return | |
307 | */ | |
308 | private long getTimestamp() { | |
bfe038ff | 309 | if (fixture.getCurrentEventDef() != null) { |
1d7277f3 | 310 | return fixture.getTrace().timestampCyclesToNanos(fixture.getCurrentEventDef().getTimestamp()); |
bfe038ff MK |
311 | } |
312 | return -1; | |
ce2388e0 | 313 | } |
866e5b51 | 314 | } |