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 { |
9ac63b5b | 61 | CTFTrace trace = testTrace.getTrace(); |
866e5b51 FC |
62 | |
63 | CTFTraceReader result = new CTFTraceReader(trace); | |
64 | assertNotNull(result); | |
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 { | |
4a9c1f07 | 75 | CTFTrace trace = new CTFTrace("badfile.bad"); |
866e5b51 FC |
76 | |
77 | CTFTraceReader result = new CTFTraceReader(trace); | |
78 | assertNotNull(result); | |
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 { | |
4a9c1f07 | 89 | CTFTrace trace = new CTFTrace(""); |
866e5b51 FC |
90 | |
91 | CTFTraceReader result = new CTFTraceReader(trace); | |
92 | assertNotNull(result); | |
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 { |
866e5b51 FC |
137 | CTFTraceReader result = fixture.copyFrom(); |
138 | assertNotNull(result); | |
139 | } | |
140 | ||
141 | /** | |
142 | * Test the hashCode method. | |
143 | */ | |
144 | @Test | |
145 | public void testHash() { | |
146 | int result = fixture.hashCode(); | |
147 | assertTrue(0 != result); | |
148 | } | |
149 | ||
150 | /** | |
151 | * Test the equals method. Uses the class-wide 'fixture' and another | |
152 | * method-local 'fixture2', which both point to the same trace. | |
153 | * | |
154 | * Both trace reader are different objects, so they shouldn't "equals" each | |
155 | * other. | |
bfe038ff | 156 | * |
ce2388e0 | 157 | * @throws CTFReaderException |
866e5b51 FC |
158 | */ |
159 | @Test | |
13be1a8f | 160 | public void testEquals() throws CTFReaderException { |
9ac63b5b | 161 | CTFTraceReader fixture2 = new CTFTraceReader(testTrace.getTrace()); |
bfe038ff | 162 | assertEquals(fixture, fixture2); |
866e5b51 FC |
163 | } |
164 | ||
165 | /** | |
166 | * Run the getCurrentEventDef() method test. Get the first event's | |
167 | * definition. | |
168 | */ | |
169 | @Test | |
170 | public void testGetCurrentEventDef_first() { | |
171 | EventDefinition result = fixture.getCurrentEventDef(); | |
172 | assertNotNull(result); | |
173 | } | |
174 | ||
175 | /** | |
176 | * Run the getCurrentEventDef() method test. Get the last event's | |
177 | * definition. | |
db8e8f7d | 178 | * @throws CTFReaderException error |
866e5b51 FC |
179 | */ |
180 | @Test | |
db8e8f7d | 181 | public void testGetCurrentEventDef_last() throws CTFReaderException { |
866e5b51 FC |
182 | fixture.goToLastEvent(); |
183 | EventDefinition result = fixture.getCurrentEventDef(); | |
184 | assertNotNull(result); | |
185 | } | |
186 | ||
187 | /** | |
188 | * Run the long getEndTime() method test. | |
189 | */ | |
190 | @Test | |
191 | public void testGetEndTime() { | |
192 | long result = fixture.getEndTime(); | |
193 | assertTrue(0L < result); | |
194 | } | |
195 | ||
196 | /** | |
197 | * Run the long getStartTime() method test. | |
198 | */ | |
199 | @Test | |
200 | public void testGetStartTime() { | |
201 | long result = fixture.getStartTime(); | |
202 | assertTrue(0L < result); | |
203 | } | |
204 | ||
205 | /** | |
206 | * Run the void goToLastEvent() method test. | |
db8e8f7d | 207 | * @throws CTFReaderException error |
866e5b51 FC |
208 | */ |
209 | @Test | |
db8e8f7d | 210 | public void testGoToLastEvent() throws CTFReaderException { |
866e5b51 | 211 | fixture.goToLastEvent(); |
ce2388e0 | 212 | long ts1 = getTimestamp(); |
866e5b51 | 213 | long ts2 = fixture.getEndTime(); |
bfe038ff | 214 | assertEquals(ts1, ts2); |
866e5b51 FC |
215 | } |
216 | ||
217 | /** | |
218 | * Run the boolean hasMoreEvents() method test. | |
219 | * | |
220 | * @throws CTFReaderException | |
221 | */ | |
222 | @Test | |
223 | public void testHasMoreEvents() { | |
224 | boolean result = fixture.hasMoreEvents(); | |
225 | assertTrue(result); | |
226 | } | |
227 | ||
228 | /** | |
229 | * Run the void printStats() method test with no 'width' parameter. | |
db8e8f7d | 230 | * @throws CTFReaderException error |
866e5b51 FC |
231 | */ |
232 | @Test | |
db8e8f7d | 233 | public void testPrintStats_noparam() throws CTFReaderException { |
866e5b51 FC |
234 | fixture.advance(); |
235 | fixture.printStats(); | |
236 | } | |
237 | ||
238 | /** | |
239 | * Run the void printStats(int) method test with width = 0. | |
db8e8f7d | 240 | * @throws CTFReaderException error |
866e5b51 FC |
241 | */ |
242 | @Test | |
db8e8f7d | 243 | public void testPrintStats_width0() throws CTFReaderException { |
866e5b51 FC |
244 | fixture.advance(); |
245 | fixture.printStats(0); | |
246 | } | |
247 | ||
248 | /** | |
249 | * Run the void printStats(int) method test with width = 1. | |
db8e8f7d | 250 | * @throws CTFReaderException error |
866e5b51 FC |
251 | */ |
252 | @Test | |
db8e8f7d | 253 | public void testPrintStats_width1() throws CTFReaderException { |
866e5b51 FC |
254 | fixture.advance(); |
255 | fixture.printStats(1); | |
256 | } | |
257 | ||
258 | /** | |
259 | * Run the void printStats(int) method test with width = 2. | |
db8e8f7d | 260 | * @throws CTFReaderException error |
866e5b51 FC |
261 | */ |
262 | @Test | |
db8e8f7d | 263 | public void testPrintStats_width2() throws CTFReaderException { |
866e5b51 FC |
264 | fixture.advance(); |
265 | fixture.printStats(2); | |
266 | } | |
267 | ||
268 | /** | |
269 | * Run the void printStats(int) method test with width = 10. | |
db8e8f7d | 270 | * @throws CTFReaderException error |
866e5b51 FC |
271 | */ |
272 | @Test | |
db8e8f7d | 273 | public void testPrintStats_width10() throws CTFReaderException { |
866e5b51 FC |
274 | fixture.advance(); |
275 | fixture.printStats(10); | |
276 | } | |
277 | ||
278 | /** | |
279 | * Run the void printStats(int) method test with width = 100. | |
db8e8f7d | 280 | * @throws CTFReaderException error |
866e5b51 FC |
281 | */ |
282 | @Test | |
db8e8f7d | 283 | public void testPrintStats_100() throws CTFReaderException { |
866e5b51 FC |
284 | for (int i = 0; i < 1000; i++) { |
285 | fixture.advance(); | |
286 | } | |
287 | fixture.printStats(100); | |
288 | } | |
289 | ||
290 | /** | |
291 | * Run the boolean seek(long) method test. | |
db8e8f7d | 292 | * @throws CTFReaderException error |
866e5b51 FC |
293 | */ |
294 | @Test | |
db8e8f7d | 295 | public void testSeek() throws CTFReaderException { |
866e5b51 FC |
296 | long timestamp = 1L; |
297 | boolean result = fixture.seek(timestamp); | |
298 | assertTrue(result); | |
299 | } | |
ce2388e0 FC |
300 | |
301 | ||
ce2388e0 FC |
302 | |
303 | /** | |
304 | * @return | |
305 | */ | |
306 | private long getTimestamp() { | |
bfe038ff | 307 | if (fixture.getCurrentEventDef() != null) { |
1d7277f3 | 308 | return fixture.getTrace().timestampCyclesToNanos(fixture.getCurrentEventDef().getTimestamp()); |
bfe038ff MK |
309 | } |
310 | return -1; | |
ce2388e0 | 311 | } |
866e5b51 | 312 | } |