Merge branch 'master' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / trace / CTFTraceTest.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 * Marc-Andre Laperle - Test in traces directory recursively
11 * Simon Delisle - Add test for getCallsite(eventName, ip)
12 *******************************************************************************/
13
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;
19 import static org.junit.Assert.assertNull;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 import static org.junit.Assume.assumeTrue;
23
24 import java.io.File;
25 import java.nio.ByteOrder;
26 import java.util.Map;
27 import java.util.UUID;
28
29 import org.eclipse.linuxtools.ctf.core.event.CTFClock;
30 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
31 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
32 import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace;
33 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
34 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
35 import org.eclipse.linuxtools.ctf.core.trace.Stream;
36 import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
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>.
43 *
44 * @author ematkho
45 */
46 public class CTFTraceTest {
47
48 private static final String TRACES_DIRECTORY = "../org.eclipse.linuxtools.ctf.core.tests/traces";
49
50 private static final String METADATA_FILENAME = "metadata";
51
52 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
53
54 private static final String CTF_VERSION_NUMBER = "1.8";
55 private static final String CTF_SUITE_TEST_DIRECTORY = "ctf-testsuite/tests/" + CTF_VERSION_NUMBER;
56
57 private CTFTrace fixture;
58
59 /**
60 * Perform pre-test initialization.
61 */
62 @Before
63 public void setUp() {
64 assumeTrue(testTrace.exists());
65 try {
66 fixture = testTrace.getTraceFromFile();
67 } catch (CTFReaderException e) {
68 /* If the assumeTrue() call passed, this should not happen. */
69 fail();
70 }
71 fixture.setMinor(1L);
72 fixture.setUUID(UUID.randomUUID());
73 fixture.setPacketHeader(new StructDeclaration(1L));
74 fixture.setMajor(1L);
75 fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
76 }
77
78 /**
79 * Run the CTFTrace(File) constructor test with a known existing trace.
80 */
81 @Test
82 public void testOpen_existing() {
83 try {
84 CTFTrace result = testTrace.getTraceFromFile();
85 assertNotNull(result.getUUID());
86 } catch (CTFReaderException e) {
87 fail();
88 }
89 }
90
91 /**
92 * Run the CTFTrace(File) constructor test with an invalid path.
93 *
94 * @throws CTFReaderException
95 * is expected
96 */
97 @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class)
98 public void testOpen_invalid() throws CTFReaderException {
99 File path = new File("");
100 CTFTrace result = new CTFTrace(path);
101 assertNotNull(result);
102 }
103
104 /**
105 * Run the boolean UUIDIsSet() method test.
106 */
107 @Test
108 public void testUUIDIsSet() {
109 boolean result = fixture.uuidIsSet();
110 assertTrue(result);
111 }
112
113 /**
114 * Run the void addStream(Stream) method test.
115 */
116 @Test
117 public void testAddStream() {
118 // test number of streams
119 int nbStreams = fixture.nbStreams();
120 assertEquals(1, nbStreams);
121
122 // Add a stream
123 try {
124 Stream stream = new Stream(testTrace.getTrace());
125 stream.setId(1234);
126 fixture.addStream(stream);
127 } catch (CTFReaderException e) {
128 fail();
129 } catch (ParseException e) {
130 fail();
131 }
132
133 // test number of streams
134 nbStreams = fixture.nbStreams();
135 assertEquals(2, nbStreams);
136 }
137
138 /**
139 * Run the boolean byteOrderIsSet() method test.
140 */
141 @Test
142 public void testByteOrderIsSet() {
143 boolean result = fixture.byteOrderIsSet();
144 assertTrue(result);
145 }
146
147 /**
148 * Run the ByteOrder getByteOrder() method test.
149 */
150 @Test
151 public void testGetByteOrder_1() {
152 ByteOrder result = fixture.getByteOrder();
153 assertNotNull(result);
154 }
155
156 /**
157 * Run the long getMajor() method test.
158 */
159 @Test
160 public void testGetMajor() {
161 long result = fixture.getMajor();
162 assertEquals(1L, result);
163 }
164
165 /**
166 * Run the long getMinor() method test.
167 */
168 @Test
169 public void testGetMinor() {
170 long result = fixture.getMinor();
171 assertEquals(1L, result);
172 }
173
174 /**
175 * Run the StructDeclaration getPacketHeader() method test.
176 */
177 @Test
178 public void testGetPacketHeader() {
179 StructDeclaration result = fixture.getPacketHeader();
180 assertNotNull(result);
181 }
182
183 /**
184 * Run the String getPath() method test.
185 */
186 @Test
187 public void testGetPath() {
188 String result = fixture.getPath();
189 assertNotNull(result);
190 }
191
192 /**
193 * Run the Stream getStream(Long) method test.
194 */
195 @Test
196 public void testGetStream() {
197 Long id = new Long(0L);
198 Stream result = fixture.getStream(id);
199 assertNotNull(result);
200 }
201
202 /**
203 * Run the Map<Long, Stream> getStreams() method test.
204 */
205 @Test
206 public void testGetStreams() {
207 Map<Long, Stream> result = fixture.getStreams();
208 assertNotNull(result);
209 }
210
211 /**
212 * Run the File getTraceDirectory() method test.
213 */
214 @Test
215 public void testGetTraceDirectory() {
216 File result = fixture.getTraceDirectory();
217 assertNotNull(result);
218 }
219
220 /**
221 * Run the UUID getUUID() method test.
222 */
223 @Test
224 public void testGetUUID() {
225 UUID result = fixture.getUUID();
226 assertNotNull(result);
227 }
228
229 /**
230 * Run the Definition lookupDefinition(String) method test.
231 */
232 @Test
233 public void testLookupDefinition() {
234 String lookupPath = "trace.packet.header";
235 Definition result = fixture.lookupDefinition(lookupPath);
236 assertNotNull(result);
237 }
238
239 /**
240 * Run the boolean majortIsSet() method test.
241 */
242 @Test
243 public void testMajortIsSet() {
244 boolean result = fixture.majortIsSet();
245 assertTrue(result);
246 }
247
248 /**
249 * Run the boolean minorIsSet() method test.
250 */
251 @Test
252 public void testMinorIsSet() {
253 boolean result = fixture.minorIsSet();
254 assertTrue(result);
255 }
256
257 /**
258 * Run the boolean packetHeaderIsSet() method test with a valid header set.
259 */
260 @Test
261 public void testPacketHeaderIsSet_valid() {
262 boolean result = fixture.packetHeaderIsSet();
263 assertTrue(result);
264 }
265
266 /**
267 * Run the boolean packetHeaderIsSet() method test, without having a valid
268 * header set.
269 */
270 @Test
271 public void testPacketHeaderIsSet_invalid() {
272 try {
273 CTFTrace fixture2 = testTrace.getTraceFromFile();
274 fixture2.setMinor(1L);
275 fixture2.setUUID(UUID.randomUUID());
276 fixture2.setPacketHeader((StructDeclaration) null); /* it's null here! */
277 fixture2.setMajor(1L);
278 fixture2.setByteOrder(ByteOrder.BIG_ENDIAN);
279
280 boolean result = fixture2.packetHeaderIsSet();
281 assertFalse(result);
282 } catch (CTFReaderException e) {
283 fail();
284 }
285 }
286
287 /**
288 * Run the void setByteOrder(ByteOrder) method test.
289 */
290 @Test
291 public void testSetByteOrder() {
292 ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
293 fixture.setByteOrder(byteOrder);
294 }
295
296 /**
297 * Run the void setMajor(long) method test.
298 */
299 @Test
300 public void testSetMajor() {
301 long major = 1L;
302 fixture.setMajor(major);
303 }
304
305 /**
306 * Run the void setMinor(long) method test.
307 */
308 @Test
309 public void testSetMinor() {
310 long minor = 1L;
311 fixture.setMinor(minor);
312 }
313
314 /**
315 * Run the void setPacketHeader(StructDeclaration) method test.
316 */
317 @Test
318 public void testSetPacketHeader() {
319 StructDeclaration packetHeader = new StructDeclaration(1L);
320 fixture.setPacketHeader(packetHeader);
321 }
322
323 /**
324 * Run the void setUUID(UUID) method test.
325 */
326 @Test
327 public void testSetUUID() {
328 UUID uuid = UUID.randomUUID();
329 fixture.setUUID(uuid);
330 }
331
332 /**
333 * Run the CTFClock getClock/setClock method test.
334 */
335 @Test
336 public void testGetSetClock_1() {
337 String name = "clockyClock";
338 fixture.addClock(name, new CTFClock());
339 CTFClock result = fixture.getClock(name);
340
341 assertNotNull(result);
342 }
343
344 /**
345 * Run the CTFClock getClock/setClock method test.
346 */
347 @Test
348 public void testGetSetClock_2() {
349 String name = "";
350 CTFClock ctfClock = new CTFClock();
351 ctfClock.addAttribute("name", "Bob");
352 ctfClock.addAttribute("pi", new Double(java.lang.Math.PI));
353 fixture.addClock(name, ctfClock);
354 CTFClock result = fixture.getClock(name);
355
356 assertNotNull(result);
357 assertTrue((Double) ctfClock.getProperty("pi") > 3.0);
358 assertTrue(ctfClock.getName().equals("Bob"));
359 }
360
361 /**
362 * Run the String lookupEnvironment(String) method test.
363 */
364 @Test
365 public void testLookupEnvironment_1() {
366 String key = "";
367 String result = fixture.lookupEnvironment(key);
368 assertNull(result);
369 }
370
371 /**
372 * Run the String lookupEnvironment(String) method test.
373 */
374 @Test
375 public void testLookupEnvironment_2() {
376 String key = "otherTest";
377 String result = fixture.lookupEnvironment(key);
378 assertNull(result);
379 }
380
381 /**
382 * Run the String lookupEnvironment(String) method test.
383 */
384 @Test
385 public void testLookupEnvironment_3() {
386 String key = "test";
387 fixture.addEnvironmentVar(key, key);
388 String result = fixture.lookupEnvironment(key);
389 assertTrue(result.equals(key));
390 }
391
392 /**
393 * Run the String lookupEnvironment(String) method test.
394 */
395 @Test
396 public void testLookupEnvironment_4() {
397 String key = "test";
398 fixture.addEnvironmentVar(key, "bozo");
399 fixture.addEnvironmentVar(key, "the clown");
400 String result = fixture.lookupEnvironment(key);
401 assertNotNull(result);
402 }
403
404 /**
405 * Open traces in specified directories and expect them to fail
406 *
407 * @throws CTFReaderException not expected
408 */
409 @Test
410 public void testFailedParse() throws CTFReaderException {
411 parseTracesInDirectory(getTestTracesSubDirectory(CTF_SUITE_TEST_DIRECTORY + "/fail"), true);
412 }
413
414 /**
415 * Open traces in specified directories and expect them to succeed
416 *
417 * @throws CTFReaderException not expected
418 */
419 @Test
420 public void testSuccessfulParse() throws CTFReaderException {
421 parseTracesInDirectory(getTestTracesSubDirectory("kernel"), false);
422 parseTracesInDirectory(getTestTracesSubDirectory("trace2"), false);
423 parseTracesInDirectory(getTestTracesSubDirectory(CTF_SUITE_TEST_DIRECTORY + "/pass"), false);
424 }
425
426 /**
427 * Get the File object for the subDir in the traces directory. If the sub directory doesn't exist, the test is skipped.
428 */
429 private static File getTestTracesSubDirectory(String subDir) {
430 File file = new File(TRACES_DIRECTORY + "/" + subDir);
431 assumeTrue(file.isDirectory());
432 return file;
433 }
434
435 /**
436 * Parse the traces in given directory recursively
437 *
438 * @param directory The directory to search in
439 * @param expectException Whether or not traces in this directory are expected to throw an exception when parsed
440 * @throws CTFReaderException
441 */
442 void parseTracesInDirectory(File directory, boolean expectException) throws CTFReaderException {
443 for (File file : directory.listFiles()) {
444 if (file.getName().equals(METADATA_FILENAME)) {
445 try {
446 new CTFTrace(directory);
447 if (expectException) {
448 fail("Trace was expected to fail parsing: " + directory);
449 }
450 } catch (RuntimeException e) {
451 if (!expectException) {
452 throw new CTFReaderException("Failed parsing " + directory, e);
453 }
454 } catch (CTFReaderException e) {
455 if (!expectException) {
456 throw new CTFReaderException("Failed parsing " + directory, e);
457 }
458 }
459 return;
460 }
461
462 if (file.isDirectory()) {
463 parseTracesInDirectory(file, expectException);
464 }
465 }
466 }
467
468 /**
469 * Test for getCallsite(eventName, ip)
470 * @throws CTFReaderException not expected
471 */
472 @Test
473 public void callsitePosition() throws CTFReaderException{
474 long ip1 = 2;
475 long ip2 = 5;
476 long ip3 = 7;
477 CTFTrace callsiteTest = testTrace.getTraceFromFile();
478 callsiteTest.addCallsite("testEvent", null, ip1, null, 23);
479 callsiteTest.addCallsite("testEvent", null, ip2, null, 50);
480 callsiteTest.addCallsite("testEvent", null, ip3, null, 15);
481
482 assertEquals(2, (callsiteTest.getCallsite("testEvent", 1)).getIp());
483 assertEquals(2, (callsiteTest.getCallsite("testEvent", 2)).getIp());
484 assertEquals(5, (callsiteTest.getCallsite("testEvent", 3)).getIp());
485 assertEquals(5, (callsiteTest.getCallsite("testEvent", 5)).getIp());
486 assertEquals(7, (callsiteTest.getCallsite("testEvent", 6)).getIp());
487 assertEquals(7, (callsiteTest.getCallsite("testEvent", 7)).getIp());
488 assertEquals(7, (callsiteTest.getCallsite("testEvent", 8)).getIp());
489 }
490
491 }
This page took 0.042153 seconds and 6 git commands to generate.