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 | |
4311ac8b | 10 | * Marc-Andre Laperle - Test in traces directory recursively |
890f9136 | 11 | * Simon Delisle - Add test for getCallsite(eventName, ip) |
4bd7f2db AM |
12 | *******************************************************************************/ |
13 | ||
866e5b51 FC |
14 | package org.eclipse.linuxtools.ctf.core.tests.trace; |
15 | ||
16 | import static org.junit.Assert.assertEquals; | |
17 | import static org.junit.Assert.assertFalse; | |
18 | import static org.junit.Assert.assertNotNull; | |
e291b8c8 | 19 | import static org.junit.Assert.assertNull; |
866e5b51 | 20 | import static org.junit.Assert.assertTrue; |
30753cb3 | 21 | import static org.junit.Assert.fail; |
e5acb357 | 22 | import static org.junit.Assume.assumeTrue; |
866e5b51 FC |
23 | |
24 | import java.io.File; | |
25 | import java.nio.ByteOrder; | |
26 | import java.util.Map; | |
27 | import java.util.UUID; | |
28 | ||
e291b8c8 | 29 | import org.eclipse.linuxtools.ctf.core.event.CTFClock; |
866e5b51 FC |
30 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
31 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; | |
9ac63b5b | 32 | import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace; |
866e5b51 FC |
33 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
34 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; | |
486efb2e | 35 | import org.eclipse.linuxtools.ctf.core.trace.Stream; |
ce2388e0 | 36 | import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException; |
866e5b51 FC |
37 | import org.junit.Before; |
38 | import org.junit.Test; | |
39 | ||
40 | /** | |
41 | * The class <code>CTFTraceTest</code> contains tests for the class | |
42 | * <code>{@link CTFTrace}</code>. | |
e291b8c8 | 43 | * |
866e5b51 | 44 | * @author ematkho |
866e5b51 FC |
45 | */ |
46 | public class CTFTraceTest { | |
47 | ||
9ac63b5b | 48 | private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL; |
32bf80d2 | 49 | |
866e5b51 FC |
50 | private CTFTrace fixture; |
51 | ||
866e5b51 FC |
52 | /** |
53 | * Perform pre-test initialization. | |
54 | */ | |
55 | @Before | |
56 | public void setUp() { | |
9ac63b5b AM |
57 | assumeTrue(testTrace.exists()); |
58 | try { | |
59 | fixture = testTrace.getTraceFromFile(); | |
60 | } catch (CTFReaderException e) { | |
61 | /* If the assumeTrue() call passed, this should not happen. */ | |
62 | fail(); | |
63 | } | |
866e5b51 FC |
64 | fixture.setMinor(1L); |
65 | fixture.setUUID(UUID.randomUUID()); | |
66 | fixture.setPacketHeader(new StructDeclaration(1L)); | |
67 | fixture.setMajor(1L); | |
68 | fixture.setByteOrder(ByteOrder.BIG_ENDIAN); | |
69 | } | |
70 | ||
866e5b51 FC |
71 | /** |
72 | * Run the CTFTrace(File) constructor test with a known existing trace. | |
73 | */ | |
74 | @Test | |
75 | public void testOpen_existing() { | |
9ac63b5b AM |
76 | try { |
77 | CTFTrace result = testTrace.getTraceFromFile(); | |
78 | assertNotNull(result.getUUID()); | |
79 | } catch (CTFReaderException e) { | |
80 | fail(); | |
81 | } | |
866e5b51 FC |
82 | } |
83 | ||
84 | /** | |
85 | * Run the CTFTrace(File) constructor test with an invalid path. | |
e291b8c8 | 86 | * |
866e5b51 | 87 | * @throws CTFReaderException |
30753cb3 | 88 | * is expected |
866e5b51 FC |
89 | */ |
90 | @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class) | |
91 | public void testOpen_invalid() throws CTFReaderException { | |
30753cb3 | 92 | File path = new File(""); |
866e5b51 FC |
93 | CTFTrace result = new CTFTrace(path); |
94 | assertNotNull(result); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Run the boolean UUIDIsSet() method test. | |
99 | */ | |
100 | @Test | |
101 | public void testUUIDIsSet() { | |
0594c61c | 102 | boolean result = fixture.uuidIsSet(); |
866e5b51 FC |
103 | assertTrue(result); |
104 | } | |
105 | ||
106 | /** | |
107 | * Run the void addStream(Stream) method test. | |
866e5b51 FC |
108 | */ |
109 | @Test | |
30753cb3 | 110 | public void testAddStream() { |
b26a2c52 BH |
111 | // test number of streams |
112 | int nbStreams = fixture.nbStreams(); | |
113 | assertEquals(1, nbStreams); | |
30753cb3 | 114 | |
b26a2c52 | 115 | // Add a stream |
30753cb3 | 116 | try { |
9ac63b5b | 117 | Stream stream = new Stream(testTrace.getTrace()); |
30753cb3 AM |
118 | stream.setId(1234); |
119 | fixture.addStream(stream); | |
120 | } catch (CTFReaderException e) { | |
121 | fail(); | |
122 | } catch (ParseException e) { | |
123 | fail(); | |
124 | } | |
125 | ||
b26a2c52 BH |
126 | // test number of streams |
127 | nbStreams = fixture.nbStreams(); | |
128 | assertEquals(2, nbStreams); | |
866e5b51 FC |
129 | } |
130 | ||
131 | /** | |
132 | * Run the boolean byteOrderIsSet() method test. | |
133 | */ | |
134 | @Test | |
135 | public void testByteOrderIsSet() { | |
136 | boolean result = fixture.byteOrderIsSet(); | |
137 | assertTrue(result); | |
138 | } | |
139 | ||
140 | /** | |
141 | * Run the ByteOrder getByteOrder() method test. | |
142 | */ | |
143 | @Test | |
144 | public void testGetByteOrder_1() { | |
145 | ByteOrder result = fixture.getByteOrder(); | |
146 | assertNotNull(result); | |
147 | } | |
148 | ||
149 | /** | |
150 | * Run the long getMajor() method test. | |
151 | */ | |
152 | @Test | |
153 | public void testGetMajor() { | |
154 | long result = fixture.getMajor(); | |
155 | assertEquals(1L, result); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Run the long getMinor() method test. | |
160 | */ | |
161 | @Test | |
162 | public void testGetMinor() { | |
163 | long result = fixture.getMinor(); | |
164 | assertEquals(1L, result); | |
165 | } | |
166 | ||
167 | /** | |
168 | * Run the StructDeclaration getPacketHeader() method test. | |
169 | */ | |
170 | @Test | |
171 | public void testGetPacketHeader() { | |
172 | StructDeclaration result = fixture.getPacketHeader(); | |
173 | assertNotNull(result); | |
174 | } | |
175 | ||
176 | /** | |
177 | * Run the String getPath() method test. | |
178 | */ | |
179 | @Test | |
180 | public void testGetPath() { | |
181 | String result = fixture.getPath(); | |
182 | assertNotNull(result); | |
183 | } | |
184 | ||
185 | /** | |
186 | * Run the Stream getStream(Long) method test. | |
187 | */ | |
188 | @Test | |
189 | public void testGetStream() { | |
190 | Long id = new Long(0L); | |
191 | Stream result = fixture.getStream(id); | |
192 | assertNotNull(result); | |
193 | } | |
194 | ||
195 | /** | |
196 | * Run the Map<Long, Stream> getStreams() method test. | |
197 | */ | |
198 | @Test | |
199 | public void testGetStreams() { | |
200 | Map<Long, Stream> result = fixture.getStreams(); | |
201 | assertNotNull(result); | |
202 | } | |
203 | ||
204 | /** | |
205 | * Run the File getTraceDirectory() method test. | |
206 | */ | |
207 | @Test | |
208 | public void testGetTraceDirectory() { | |
209 | File result = fixture.getTraceDirectory(); | |
210 | assertNotNull(result); | |
211 | } | |
212 | ||
213 | /** | |
214 | * Run the UUID getUUID() method test. | |
215 | */ | |
216 | @Test | |
217 | public void testGetUUID() { | |
218 | UUID result = fixture.getUUID(); | |
219 | assertNotNull(result); | |
220 | } | |
221 | ||
222 | /** | |
223 | * Run the Definition lookupDefinition(String) method test. | |
224 | */ | |
225 | @Test | |
226 | public void testLookupDefinition() { | |
30753cb3 | 227 | String lookupPath = "trace.packet.header"; |
866e5b51 FC |
228 | Definition result = fixture.lookupDefinition(lookupPath); |
229 | assertNotNull(result); | |
230 | } | |
231 | ||
232 | /** | |
07804639 | 233 | * Run the boolean majorIsSet() method test. |
866e5b51 FC |
234 | */ |
235 | @Test | |
07804639 EB |
236 | public void testMajorIsSet() { |
237 | boolean result = fixture.majorIsSet(); | |
866e5b51 FC |
238 | assertTrue(result); |
239 | } | |
240 | ||
241 | /** | |
242 | * Run the boolean minorIsSet() method test. | |
243 | */ | |
244 | @Test | |
245 | public void testMinorIsSet() { | |
246 | boolean result = fixture.minorIsSet(); | |
247 | assertTrue(result); | |
248 | } | |
249 | ||
866e5b51 FC |
250 | /** |
251 | * Run the boolean packetHeaderIsSet() method test with a valid header set. | |
252 | */ | |
253 | @Test | |
254 | public void testPacketHeaderIsSet_valid() { | |
255 | boolean result = fixture.packetHeaderIsSet(); | |
256 | assertTrue(result); | |
257 | } | |
258 | ||
259 | /** | |
260 | * Run the boolean packetHeaderIsSet() method test, without having a valid | |
261 | * header set. | |
262 | */ | |
263 | @Test | |
264 | public void testPacketHeaderIsSet_invalid() { | |
9ac63b5b AM |
265 | try { |
266 | CTFTrace fixture2 = testTrace.getTraceFromFile(); | |
267 | fixture2.setMinor(1L); | |
268 | fixture2.setUUID(UUID.randomUUID()); | |
269 | fixture2.setPacketHeader((StructDeclaration) null); /* it's null here! */ | |
270 | fixture2.setMajor(1L); | |
271 | fixture2.setByteOrder(ByteOrder.BIG_ENDIAN); | |
272 | ||
273 | boolean result = fixture2.packetHeaderIsSet(); | |
274 | assertFalse(result); | |
275 | } catch (CTFReaderException e) { | |
276 | fail(); | |
277 | } | |
866e5b51 FC |
278 | } |
279 | ||
280 | /** | |
281 | * Run the void setByteOrder(ByteOrder) method test. | |
282 | */ | |
283 | @Test | |
284 | public void testSetByteOrder() { | |
285 | ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; | |
286 | fixture.setByteOrder(byteOrder); | |
287 | } | |
288 | ||
289 | /** | |
290 | * Run the void setMajor(long) method test. | |
291 | */ | |
292 | @Test | |
293 | public void testSetMajor() { | |
294 | long major = 1L; | |
295 | fixture.setMajor(major); | |
296 | } | |
297 | ||
298 | /** | |
299 | * Run the void setMinor(long) method test. | |
300 | */ | |
301 | @Test | |
302 | public void testSetMinor() { | |
303 | long minor = 1L; | |
304 | fixture.setMinor(minor); | |
305 | } | |
306 | ||
307 | /** | |
308 | * Run the void setPacketHeader(StructDeclaration) method test. | |
309 | */ | |
310 | @Test | |
311 | public void testSetPacketHeader() { | |
312 | StructDeclaration packetHeader = new StructDeclaration(1L); | |
313 | fixture.setPacketHeader(packetHeader); | |
314 | } | |
315 | ||
316 | /** | |
317 | * Run the void setUUID(UUID) method test. | |
318 | */ | |
319 | @Test | |
320 | public void testSetUUID() { | |
321 | UUID uuid = UUID.randomUUID(); | |
322 | fixture.setUUID(uuid); | |
323 | } | |
e291b8c8 MK |
324 | |
325 | /** | |
30753cb3 | 326 | * Run the CTFClock getClock/setClock method test. |
e291b8c8 MK |
327 | */ |
328 | @Test | |
30753cb3 AM |
329 | public void testGetSetClock_1() { |
330 | String name = "clockyClock"; | |
e291b8c8 MK |
331 | fixture.addClock(name, new CTFClock()); |
332 | CTFClock result = fixture.getClock(name); | |
333 | ||
334 | assertNotNull(result); | |
335 | } | |
336 | ||
337 | /** | |
30753cb3 | 338 | * Run the CTFClock getClock/setClock method test. |
e291b8c8 MK |
339 | */ |
340 | @Test | |
30753cb3 AM |
341 | public void testGetSetClock_2() { |
342 | String name = ""; | |
e291b8c8 | 343 | CTFClock ctfClock = new CTFClock(); |
30753cb3 AM |
344 | ctfClock.addAttribute("name", "Bob"); |
345 | ctfClock.addAttribute("pi", new Double(java.lang.Math.PI)); | |
e291b8c8 MK |
346 | fixture.addClock(name, ctfClock); |
347 | CTFClock result = fixture.getClock(name); | |
348 | ||
349 | assertNotNull(result); | |
30753cb3 AM |
350 | assertTrue((Double) ctfClock.getProperty("pi") > 3.0); |
351 | assertTrue(ctfClock.getName().equals("Bob")); | |
e291b8c8 MK |
352 | } |
353 | ||
354 | /** | |
355 | * Run the String lookupEnvironment(String) method test. | |
356 | */ | |
357 | @Test | |
358 | public void testLookupEnvironment_1() { | |
30753cb3 | 359 | String key = ""; |
e291b8c8 MK |
360 | String result = fixture.lookupEnvironment(key); |
361 | assertNull(result); | |
362 | } | |
363 | ||
364 | /** | |
365 | * Run the String lookupEnvironment(String) method test. | |
366 | */ | |
367 | @Test | |
368 | public void testLookupEnvironment_2() { | |
30753cb3 | 369 | String key = "otherTest"; |
e291b8c8 MK |
370 | String result = fixture.lookupEnvironment(key); |
371 | assertNull(result); | |
372 | } | |
373 | ||
374 | /** | |
375 | * Run the String lookupEnvironment(String) method test. | |
376 | */ | |
377 | @Test | |
378 | public void testLookupEnvironment_3() { | |
30753cb3 | 379 | String key = "test"; |
e291b8c8 MK |
380 | fixture.addEnvironmentVar(key, key); |
381 | String result = fixture.lookupEnvironment(key); | |
382 | assertTrue(result.equals(key)); | |
383 | } | |
384 | ||
385 | /** | |
386 | * Run the String lookupEnvironment(String) method test. | |
387 | */ | |
388 | @Test | |
389 | public void testLookupEnvironment_4() { | |
30753cb3 AM |
390 | String key = "test"; |
391 | fixture.addEnvironmentVar(key, "bozo"); | |
392 | fixture.addEnvironmentVar(key, "the clown"); | |
e291b8c8 MK |
393 | String result = fixture.lookupEnvironment(key); |
394 | assertNotNull(result); | |
395 | } | |
396 | ||
890f9136 SD |
397 | /** |
398 | * Test for getCallsite(eventName, ip) | |
6cd87900 | 399 | * @throws CTFReaderException not expected |
890f9136 SD |
400 | */ |
401 | @Test | |
6cd87900 | 402 | public void callsitePosition() throws CTFReaderException{ |
890f9136 SD |
403 | long ip1 = 2; |
404 | long ip2 = 5; | |
405 | long ip3 = 7; | |
6cd87900 | 406 | CTFTrace callsiteTest = testTrace.getTraceFromFile(); |
890f9136 SD |
407 | callsiteTest.addCallsite("testEvent", null, ip1, null, 23); |
408 | callsiteTest.addCallsite("testEvent", null, ip2, null, 50); | |
409 | callsiteTest.addCallsite("testEvent", null, ip3, null, 15); | |
410 | ||
411 | assertEquals(2, (callsiteTest.getCallsite("testEvent", 1)).getIp()); | |
412 | assertEquals(2, (callsiteTest.getCallsite("testEvent", 2)).getIp()); | |
413 | assertEquals(5, (callsiteTest.getCallsite("testEvent", 3)).getIp()); | |
414 | assertEquals(5, (callsiteTest.getCallsite("testEvent", 5)).getIp()); | |
415 | assertEquals(7, (callsiteTest.getCallsite("testEvent", 6)).getIp()); | |
416 | assertEquals(7, (callsiteTest.getCallsite("testEvent", 7)).getIp()); | |
417 | assertEquals(7, (callsiteTest.getCallsite("testEvent", 8)).getIp()); | |
418 | } | |
419 | ||
866e5b51 | 420 | } |