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
.CtfTestTraces
;
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 int TRACE_INDEX
= 0;
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(CtfTestTraces
.tracesExist());
65 fixture
= CtfTestTraces
.getTestTraceFromFile(TRACE_INDEX
);
67 fixture
.setUUID(UUID
.randomUUID());
68 fixture
.setPacketHeader(new StructDeclaration(1L));
70 fixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
74 * Run the CTFTrace(File) constructor test with a known existing trace.
77 public void testOpen_existing() {
78 CTFTrace result
= CtfTestTraces
.getTestTraceFromFile(TRACE_INDEX
);
79 assertNotNull(result
.getUUID());
83 * Run the CTFTrace(File) constructor test with an invalid path.
85 * @throws CTFReaderException
88 @Test(expected
= org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
.class)
89 public void testOpen_invalid() throws CTFReaderException
{
90 File path
= new File("");
91 CTFTrace result
= new CTFTrace(path
);
92 assertNotNull(result
);
96 * Run the boolean UUIDIsSet() method test.
99 public void testUUIDIsSet() {
100 boolean result
= fixture
.uuidIsSet();
105 * Run the void addStream(Stream) method test.
108 public void testAddStream() {
109 // test number of streams
110 int nbStreams
= fixture
.nbStreams();
111 assertEquals(1, nbStreams
);
115 Stream stream
= new Stream(CtfTestTraces
.getTestTrace(TRACE_INDEX
));
117 fixture
.addStream(stream
);
118 } catch (CTFReaderException e
) {
120 } catch (ParseException e
) {
124 // test number of streams
125 nbStreams
= fixture
.nbStreams();
126 assertEquals(2, nbStreams
);
130 * Run the boolean byteOrderIsSet() method test.
133 public void testByteOrderIsSet() {
134 boolean result
= fixture
.byteOrderIsSet();
139 * Run the ByteOrder getByteOrder() method test.
142 public void testGetByteOrder_1() {
143 ByteOrder result
= fixture
.getByteOrder();
144 assertNotNull(result
);
148 * Run the long getMajor() method test.
151 public void testGetMajor() {
152 long result
= fixture
.getMajor();
153 assertEquals(1L, result
);
157 * Run the long getMinor() method test.
160 public void testGetMinor() {
161 long result
= fixture
.getMinor();
162 assertEquals(1L, result
);
166 * Run the StructDeclaration getPacketHeader() method test.
169 public void testGetPacketHeader() {
170 StructDeclaration result
= fixture
.getPacketHeader();
171 assertNotNull(result
);
175 * Run the String getPath() method test.
178 public void testGetPath() {
179 String result
= fixture
.getPath();
180 assertNotNull(result
);
184 * Run the Stream getStream(Long) method test.
187 public void testGetStream() {
188 Long id
= new Long(0L);
189 Stream result
= fixture
.getStream(id
);
190 assertNotNull(result
);
194 * Run the Map<Long, Stream> getStreams() method test.
197 public void testGetStreams() {
198 Map
<Long
, Stream
> result
= fixture
.getStreams();
199 assertNotNull(result
);
203 * Run the File getTraceDirectory() method test.
206 public void testGetTraceDirectory() {
207 File result
= fixture
.getTraceDirectory();
208 assertNotNull(result
);
212 * Run the UUID getUUID() method test.
215 public void testGetUUID() {
216 UUID result
= fixture
.getUUID();
217 assertNotNull(result
);
221 * Run the Definition lookupDefinition(String) method test.
224 public void testLookupDefinition() {
225 String lookupPath
= "trace.packet.header";
226 Definition result
= fixture
.lookupDefinition(lookupPath
);
227 assertNotNull(result
);
231 * Run the boolean majortIsSet() method test.
234 public void testMajortIsSet() {
235 boolean result
= fixture
.majortIsSet();
240 * Run the boolean minorIsSet() method test.
243 public void testMinorIsSet() {
244 boolean result
= fixture
.minorIsSet();
249 * Run the boolean packetHeaderIsSet() method test with a valid header set.
252 public void testPacketHeaderIsSet_valid() {
253 boolean result
= fixture
.packetHeaderIsSet();
258 * Run the boolean packetHeaderIsSet() method test, without having a valid
262 public void testPacketHeaderIsSet_invalid() {
263 CTFTrace fixture2
= CtfTestTraces
.getTestTraceFromFile(TRACE_INDEX
);
264 fixture2
.setMinor(1L);
265 fixture2
.setUUID(UUID
.randomUUID());
266 fixture2
.setPacketHeader((StructDeclaration
) null); /* it's null here! */
267 fixture2
.setMajor(1L);
268 fixture2
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
270 boolean result
= fixture2
.packetHeaderIsSet();
275 * Run the void setByteOrder(ByteOrder) method test.
278 public void testSetByteOrder() {
279 ByteOrder byteOrder
= ByteOrder
.BIG_ENDIAN
;
280 fixture
.setByteOrder(byteOrder
);
284 * Run the void setMajor(long) method test.
287 public void testSetMajor() {
289 fixture
.setMajor(major
);
293 * Run the void setMinor(long) method test.
296 public void testSetMinor() {
298 fixture
.setMinor(minor
);
302 * Run the void setPacketHeader(StructDeclaration) method test.
305 public void testSetPacketHeader() {
306 StructDeclaration packetHeader
= new StructDeclaration(1L);
307 fixture
.setPacketHeader(packetHeader
);
311 * Run the void setUUID(UUID) method test.
314 public void testSetUUID() {
315 UUID uuid
= UUID
.randomUUID();
316 fixture
.setUUID(uuid
);
320 * Run the CTFClock getClock/setClock method test.
323 public void testGetSetClock_1() {
324 String name
= "clockyClock";
325 fixture
.addClock(name
, new CTFClock());
326 CTFClock result
= fixture
.getClock(name
);
328 assertNotNull(result
);
332 * Run the CTFClock getClock/setClock method test.
335 public void testGetSetClock_2() {
337 CTFClock ctfClock
= new CTFClock();
338 ctfClock
.addAttribute("name", "Bob");
339 ctfClock
.addAttribute("pi", new Double(java
.lang
.Math
.PI
));
340 fixture
.addClock(name
, ctfClock
);
341 CTFClock result
= fixture
.getClock(name
);
343 assertNotNull(result
);
344 assertTrue((Double
) ctfClock
.getProperty("pi") > 3.0);
345 assertTrue(ctfClock
.getName().equals("Bob"));
349 * Run the String lookupEnvironment(String) method test.
352 public void testLookupEnvironment_1() {
354 String result
= fixture
.lookupEnvironment(key
);
359 * Run the String lookupEnvironment(String) method test.
362 public void testLookupEnvironment_2() {
363 String key
= "otherTest";
364 String result
= fixture
.lookupEnvironment(key
);
369 * Run the String lookupEnvironment(String) method test.
372 public void testLookupEnvironment_3() {
374 fixture
.addEnvironmentVar(key
, key
);
375 String result
= fixture
.lookupEnvironment(key
);
376 assertTrue(result
.equals(key
));
380 * Run the String lookupEnvironment(String) method test.
383 public void testLookupEnvironment_4() {
385 fixture
.addEnvironmentVar(key
, "bozo");
386 fixture
.addEnvironmentVar(key
, "the clown");
387 String result
= fixture
.lookupEnvironment(key
);
388 assertNotNull(result
);
392 * Open traces in specified directories and expect them to fail
394 * @throws CTFReaderException not expected
397 public void testFailedParse() throws CTFReaderException
{
398 parseTracesInDirectory(getTestTracesSubDirectory(CTF_SUITE_TEST_DIRECTORY
+ "/fail"), true);
402 * Open traces in specified directories and expect them to succeed
404 * @throws CTFReaderException not expected
407 public void testSuccessfulParse() throws CTFReaderException
{
408 parseTracesInDirectory(getTestTracesSubDirectory("kernel"), false);
409 parseTracesInDirectory(getTestTracesSubDirectory("trace2"), false);
410 parseTracesInDirectory(getTestTracesSubDirectory(CTF_SUITE_TEST_DIRECTORY
+ "/pass"), false);
414 * Get the File object for the subDir in the traces directory. If the sub directory doesn't exist, the test is skipped.
416 private static File
getTestTracesSubDirectory(String subDir
) {
417 File file
= new File(TRACES_DIRECTORY
+ "/" + subDir
);
418 assumeTrue(file
.isDirectory());
423 * Parse the traces in given directory recursively
425 * @param directory The directory to search in
426 * @param expectException Whether or not traces in this directory are expected to throw an exception when parsed
427 * @throws CTFReaderException
429 void parseTracesInDirectory(File directory
, boolean expectException
) throws CTFReaderException
{
430 for (File file
: directory
.listFiles()) {
431 if (file
.getName().equals(METADATA_FILENAME
)) {
433 new CTFTrace(directory
);
434 if (expectException
) {
435 fail("Trace was expected to fail parsing: " + directory
);
437 } catch (RuntimeException e
) {
438 if (!expectException
) {
439 throw new CTFReaderException("Failed parsing " + directory
, e
);
441 } catch (CTFReaderException e
) {
442 if (!expectException
) {
443 throw new CTFReaderException("Failed parsing " + directory
, e
);
449 if (file
.isDirectory()) {
450 parseTracesInDirectory(file
, expectException
);
456 * Test for getCallsite(eventName, ip)
459 public void callsitePosition(){
463 CTFTrace callsiteTest
= CtfTestTraces
.getTestTraceFromFile(TRACE_INDEX
);
464 callsiteTest
.addCallsite("testEvent", null, ip1
, null, 23);
465 callsiteTest
.addCallsite("testEvent", null, ip2
, null, 50);
466 callsiteTest
.addCallsite("testEvent", null, ip3
, null, 15);
468 assertEquals(2, (callsiteTest
.getCallsite("testEvent", 1)).getIp());
469 assertEquals(2, (callsiteTest
.getCallsite("testEvent", 2)).getIp());
470 assertEquals(5, (callsiteTest
.getCallsite("testEvent", 3)).getIp());
471 assertEquals(5, (callsiteTest
.getCallsite("testEvent", 5)).getIp());
472 assertEquals(7, (callsiteTest
.getCallsite("testEvent", 6)).getIp());
473 assertEquals(7, (callsiteTest
.getCallsite("testEvent", 7)).getIp());
474 assertEquals(7, (callsiteTest
.getCallsite("testEvent", 8)).getIp());