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
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 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.ctf
.core
.tests
.trace
;
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
;
25 import java
.nio
.ByteOrder
;
27 import java
.util
.UUID
;
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
;
41 * The class <code>CTFTraceTest</code> contains tests for the class
42 * <code>{@link CTFTrace}</code>.
46 public class CTFTraceTest
{
48 private static final String TRACES_DIRECTORY
= "../org.eclipse.linuxtools.ctf.core.tests/traces";
50 private static final String METADATA_FILENAME
= "metadata";
52 private static final CtfTestTrace testTrace
= CtfTestTrace
.KERNEL
;
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
;
57 private CTFTrace fixture
;
60 * Perform pre-test initialization.
64 assumeTrue(testTrace
.exists());
66 fixture
= testTrace
.getTraceFromFile();
67 } catch (CTFReaderException e
) {
68 /* If the assumeTrue() call passed, this should not happen. */
72 fixture
.setUUID(UUID
.randomUUID());
73 fixture
.setPacketHeader(new StructDeclaration(1L));
75 fixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
79 * Run the CTFTrace(File) constructor test with a known existing trace.
82 public void testOpen_existing() {
84 CTFTrace result
= testTrace
.getTraceFromFile();
85 assertNotNull(result
.getUUID());
86 } catch (CTFReaderException e
) {
92 * Run the CTFTrace(File) constructor test with an invalid path.
94 * @throws CTFReaderException
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
);
105 * Run the boolean UUIDIsSet() method test.
108 public void testUUIDIsSet() {
109 boolean result
= fixture
.uuidIsSet();
114 * Run the void addStream(Stream) method test.
117 public void testAddStream() {
118 // test number of streams
119 int nbStreams
= fixture
.nbStreams();
120 assertEquals(1, nbStreams
);
124 Stream stream
= new Stream(testTrace
.getTrace());
126 fixture
.addStream(stream
);
127 } catch (CTFReaderException e
) {
129 } catch (ParseException e
) {
133 // test number of streams
134 nbStreams
= fixture
.nbStreams();
135 assertEquals(2, nbStreams
);
139 * Run the boolean byteOrderIsSet() method test.
142 public void testByteOrderIsSet() {
143 boolean result
= fixture
.byteOrderIsSet();
148 * Run the ByteOrder getByteOrder() method test.
151 public void testGetByteOrder_1() {
152 ByteOrder result
= fixture
.getByteOrder();
153 assertNotNull(result
);
157 * Run the long getMajor() method test.
160 public void testGetMajor() {
161 long result
= fixture
.getMajor();
162 assertEquals(1L, result
);
166 * Run the long getMinor() method test.
169 public void testGetMinor() {
170 long result
= fixture
.getMinor();
171 assertEquals(1L, result
);
175 * Run the StructDeclaration getPacketHeader() method test.
178 public void testGetPacketHeader() {
179 StructDeclaration result
= fixture
.getPacketHeader();
180 assertNotNull(result
);
184 * Run the String getPath() method test.
187 public void testGetPath() {
188 String result
= fixture
.getPath();
189 assertNotNull(result
);
193 * Run the Stream getStream(Long) method test.
196 public void testGetStream() {
197 Long id
= new Long(0L);
198 Stream result
= fixture
.getStream(id
);
199 assertNotNull(result
);
203 * Run the Map<Long, Stream> getStreams() method test.
206 public void testGetStreams() {
207 Map
<Long
, Stream
> result
= fixture
.getStreams();
208 assertNotNull(result
);
212 * Run the File getTraceDirectory() method test.
215 public void testGetTraceDirectory() {
216 File result
= fixture
.getTraceDirectory();
217 assertNotNull(result
);
221 * Run the UUID getUUID() method test.
224 public void testGetUUID() {
225 UUID result
= fixture
.getUUID();
226 assertNotNull(result
);
230 * Run the Definition lookupDefinition(String) method test.
233 public void testLookupDefinition() {
234 String lookupPath
= "trace.packet.header";
235 Definition result
= fixture
.lookupDefinition(lookupPath
);
236 assertNotNull(result
);
240 * Run the boolean majortIsSet() method test.
243 public void testMajortIsSet() {
244 boolean result
= fixture
.majortIsSet();
249 * Run the boolean minorIsSet() method test.
252 public void testMinorIsSet() {
253 boolean result
= fixture
.minorIsSet();
258 * Run the boolean packetHeaderIsSet() method test with a valid header set.
261 public void testPacketHeaderIsSet_valid() {
262 boolean result
= fixture
.packetHeaderIsSet();
267 * Run the boolean packetHeaderIsSet() method test, without having a valid
271 public void testPacketHeaderIsSet_invalid() {
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
);
280 boolean result
= fixture2
.packetHeaderIsSet();
282 } catch (CTFReaderException e
) {
288 * Run the void setByteOrder(ByteOrder) method test.
291 public void testSetByteOrder() {
292 ByteOrder byteOrder
= ByteOrder
.BIG_ENDIAN
;
293 fixture
.setByteOrder(byteOrder
);
297 * Run the void setMajor(long) method test.
300 public void testSetMajor() {
302 fixture
.setMajor(major
);
306 * Run the void setMinor(long) method test.
309 public void testSetMinor() {
311 fixture
.setMinor(minor
);
315 * Run the void setPacketHeader(StructDeclaration) method test.
318 public void testSetPacketHeader() {
319 StructDeclaration packetHeader
= new StructDeclaration(1L);
320 fixture
.setPacketHeader(packetHeader
);
324 * Run the void setUUID(UUID) method test.
327 public void testSetUUID() {
328 UUID uuid
= UUID
.randomUUID();
329 fixture
.setUUID(uuid
);
333 * Run the CTFClock getClock/setClock method test.
336 public void testGetSetClock_1() {
337 String name
= "clockyClock";
338 fixture
.addClock(name
, new CTFClock());
339 CTFClock result
= fixture
.getClock(name
);
341 assertNotNull(result
);
345 * Run the CTFClock getClock/setClock method test.
348 public void testGetSetClock_2() {
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
);
356 assertNotNull(result
);
357 assertTrue((Double
) ctfClock
.getProperty("pi") > 3.0);
358 assertTrue(ctfClock
.getName().equals("Bob"));
362 * Run the String lookupEnvironment(String) method test.
365 public void testLookupEnvironment_1() {
367 String result
= fixture
.lookupEnvironment(key
);
372 * Run the String lookupEnvironment(String) method test.
375 public void testLookupEnvironment_2() {
376 String key
= "otherTest";
377 String result
= fixture
.lookupEnvironment(key
);
382 * Run the String lookupEnvironment(String) method test.
385 public void testLookupEnvironment_3() {
387 fixture
.addEnvironmentVar(key
, key
);
388 String result
= fixture
.lookupEnvironment(key
);
389 assertTrue(result
.equals(key
));
393 * Run the String lookupEnvironment(String) method test.
396 public void testLookupEnvironment_4() {
398 fixture
.addEnvironmentVar(key
, "bozo");
399 fixture
.addEnvironmentVar(key
, "the clown");
400 String result
= fixture
.lookupEnvironment(key
);
401 assertNotNull(result
);
405 * Open traces in specified directories and expect them to fail
407 * @throws CTFReaderException not expected
410 public void testFailedParse() throws CTFReaderException
{
411 parseTracesInDirectory(getTestTracesSubDirectory(CTF_SUITE_TEST_DIRECTORY
+ "/fail"), true);
415 * Open traces in specified directories and expect them to succeed
417 * @throws CTFReaderException not expected
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);
427 * Get the File object for the subDir in the traces directory. If the sub directory doesn't exist, the test is skipped.
429 private static File
getTestTracesSubDirectory(String subDir
) {
430 File file
= new File(TRACES_DIRECTORY
+ "/" + subDir
);
431 assumeTrue(file
.isDirectory());
436 * Parse the traces in given directory recursively
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
442 void parseTracesInDirectory(File directory
, boolean expectException
) throws CTFReaderException
{
443 for (File file
: directory
.listFiles()) {
444 if (file
.getName().equals(METADATA_FILENAME
)) {
446 new CTFTrace(directory
);
447 if (expectException
) {
448 fail("Trace was expected to fail parsing: " + directory
);
450 } catch (RuntimeException e
) {
451 if (!expectException
) {
452 throw new CTFReaderException("Failed parsing " + directory
, e
);
454 } catch (CTFReaderException e
) {
455 if (!expectException
) {
456 throw new CTFReaderException("Failed parsing " + directory
, e
);
462 if (file
.isDirectory()) {
463 parseTracesInDirectory(file
, expectException
);
469 * Test for getCallsite(eventName, ip)
470 * @throws CTFReaderException not expected
473 public void callsitePosition() throws CTFReaderException
{
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);
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());