1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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
.tracecompass
.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
;
26 import java
.util
.UUID
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.CTFClock
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
30 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.tests
.shared
.CtfTestTrace
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFReaderException
;
33 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFStream
;
34 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
35 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.exceptions
.ParseException
;
36 import org
.junit
.Before
;
37 import org
.junit
.Test
;
40 * The class <code>CTFTraceTest</code> contains tests for the class
41 * <code>{@link CTFTrace}</code>.
45 public class CTFTraceTest
{
47 private static final CtfTestTrace testTrace
= CtfTestTrace
.KERNEL
;
49 private CTFTrace fixture
;
52 * Perform pre-test initialization.
56 assumeTrue(testTrace
.exists());
58 fixture
= testTrace
.getTraceFromFile();
59 } catch (CTFReaderException e
) {
60 /* If the assumeTrue() call passed, this should not happen. */
64 fixture
.setUUID(UUID
.randomUUID());
65 fixture
.setPacketHeader(new StructDeclaration(1L));
67 fixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
71 * Run the CTFTrace(File) constructor test with a known existing trace.
74 public void testOpen_existing() {
75 try (CTFTrace result
= testTrace
.getTraceFromFile();) {
76 assertNotNull(result
.getUUID());
77 } catch (CTFReaderException e
) {
83 * Run the CTFTrace(File) constructor test with an invalid path.
85 * @throws CTFReaderException
88 @Test(expected
= org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFReaderException
.class)
89 public void testOpen_invalid() throws CTFReaderException
{
90 File path
= new File("");
91 try (CTFTrace result
= new CTFTrace(path
);) {
92 assertNotNull(result
);
97 * Run the boolean UUIDIsSet() method test.
100 public void testUUIDIsSet() {
101 boolean result
= fixture
.uuidIsSet();
106 * Run the void addStream(Stream) method test.
109 public void testAddStream() {
110 // test number of streams
111 int nbStreams
= fixture
.nbStreams();
112 assertEquals(1, nbStreams
);
116 CTFStream stream
= new CTFStream(testTrace
.getTrace());
118 fixture
.addStream(stream
);
119 } catch (CTFReaderException e
) {
121 } catch (ParseException e
) {
125 // test number of streams
126 nbStreams
= fixture
.nbStreams();
127 assertEquals(2, nbStreams
);
131 * Run the boolean byteOrderIsSet() method test.
134 public void testByteOrderIsSet() {
135 boolean result
= fixture
.byteOrderIsSet();
140 * Run the ByteOrder getByteOrder() method test.
143 public void testGetByteOrder_1() {
144 ByteOrder result
= fixture
.getByteOrder();
145 assertNotNull(result
);
149 * Run the long getMajor() method test.
152 public void testGetMajor() {
153 long result
= fixture
.getMajor();
154 assertEquals(1L, result
);
158 * Run the long getMinor() method test.
161 public void testGetMinor() {
162 long result
= fixture
.getMinor();
163 assertEquals(1L, result
);
167 * Run the StructDeclaration getPacketHeader() method test.
170 public void testGetPacketHeader() {
171 StructDeclaration result
= fixture
.getPacketHeader();
172 assertNotNull(result
);
176 * Run the String getPath() method test.
179 public void testGetPath() {
180 String result
= fixture
.getPath();
181 assertNotNull(result
);
185 * Run the Stream getStream(Long) method test.
188 public void testGetStream() {
189 Long id
= new Long(0L);
190 CTFStream result
= fixture
.getStream(id
);
191 assertNotNull(result
);
195 * Run the File getTraceDirectory() method test.
198 public void testGetTraceDirectory() {
199 File result
= fixture
.getTraceDirectory();
200 assertNotNull(result
);
204 * Run the UUID getUUID() method test.
207 public void testGetUUID() {
208 UUID result
= fixture
.getUUID();
209 assertNotNull(result
);
213 * Run the Definition lookupDefinition(String) method test.
216 public void testLookupDefinition() {
217 String lookupPath
= "trace.packet.header";
218 IDefinition result
= fixture
.lookupDefinition(lookupPath
);
219 assertNotNull(result
);
223 * Run the boolean majorIsSet() method test.
226 public void testMajorIsSet() {
227 boolean result
= fixture
.majorIsSet();
232 * Run the boolean minorIsSet() method test.
235 public void testMinorIsSet() {
236 boolean result
= fixture
.minorIsSet();
241 * Run the boolean packetHeaderIsSet() method test with a valid header set.
244 public void testPacketHeaderIsSet_valid() {
245 boolean result
= fixture
.packetHeaderIsSet();
250 * Run the boolean packetHeaderIsSet() method test, without having a valid
254 public void testPacketHeaderIsSet_invalid() {
255 try (CTFTrace fixture2
= testTrace
.getTraceFromFile();){
256 fixture2
.setMinor(1L);
257 fixture2
.setUUID(UUID
.randomUUID());
258 fixture2
.setPacketHeader((StructDeclaration
) null); /* it's null here! */
259 fixture2
.setMajor(1L);
260 fixture2
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
262 boolean result
= fixture2
.packetHeaderIsSet();
264 } catch (CTFReaderException e
) {
270 * Run the void setByteOrder(ByteOrder) method test.
273 public void testSetByteOrder() {
274 ByteOrder byteOrder
= ByteOrder
.BIG_ENDIAN
;
275 fixture
.setByteOrder(byteOrder
);
279 * Run the void setMajor(long) method test.
282 public void testSetMajor() {
284 fixture
.setMajor(major
);
288 * Run the void setMinor(long) method test.
291 public void testSetMinor() {
293 fixture
.setMinor(minor
);
297 * Run the void setPacketHeader(StructDeclaration) method test.
300 public void testSetPacketHeader() {
301 StructDeclaration packetHeader
= new StructDeclaration(1L);
302 fixture
.setPacketHeader(packetHeader
);
306 * Run the void setUUID(UUID) method test.
309 public void testSetUUID() {
310 UUID uuid
= UUID
.randomUUID();
311 fixture
.setUUID(uuid
);
315 * Run the CTFClock getClock/setClock method test.
318 public void testGetSetClock_1() {
319 String name
= "clockyClock";
320 fixture
.addClock(name
, new CTFClock());
321 CTFClock result
= fixture
.getClock(name
);
323 assertNotNull(result
);
327 * Run the CTFClock getClock/setClock method test.
330 public void testGetSetClock_2() {
332 CTFClock ctfClock
= new CTFClock();
333 ctfClock
.addAttribute("name", "Bob");
334 ctfClock
.addAttribute("pi", new Double(java
.lang
.Math
.PI
));
335 fixture
.addClock(name
, ctfClock
);
336 CTFClock result
= fixture
.getClock(name
);
338 assertNotNull(result
);
339 assertTrue((Double
) ctfClock
.getProperty("pi") > 3.0);
340 assertTrue(ctfClock
.getName().equals("Bob"));
344 * Run the String lookupEnvironment(String) method test.
347 public void testLookupEnvironment_1() {
349 String result
= fixture
.getEnvironment().get(key
);
354 * Run the String lookupEnvironment(String) method test.
357 public void testLookupEnvironment_2() {
358 String key
= "otherTest";
359 String result
= fixture
.getEnvironment().get(key
);
364 * Run the String lookupEnvironment(String) method test.
367 public void testLookupEnvironment_3() {
369 fixture
.addEnvironmentVar(key
, key
);
370 String result
= fixture
.getEnvironment().get(key
);
371 assertTrue(result
.equals(key
));
375 * Run the String lookupEnvironment(String) method test.
378 public void testLookupEnvironment_4() {
380 fixture
.addEnvironmentVar(key
, "bozo");
381 fixture
.addEnvironmentVar(key
, "the clown");
382 String result
= fixture
.getEnvironment().get(key
);
383 assertNotNull(result
);
387 * Test for getCallsite(eventName, ip)
388 * @throws CTFReaderException not expected
391 public void callsitePosition() throws CTFReaderException
{
395 try (CTFTrace callsiteTest
= testTrace
.getTraceFromFile()) {
396 callsiteTest
.addCallsite("testEvent", null, ip1
, null, 23);
397 callsiteTest
.addCallsite("testEvent", null, ip2
, null, 50);
398 callsiteTest
.addCallsite("testEvent", null, ip3
, null, 15);
400 assertEquals(2, (callsiteTest
.getCallsite("testEvent", 1)).getIp());
401 assertEquals(2, (callsiteTest
.getCallsite("testEvent", 2)).getIp());
402 assertEquals(5, (callsiteTest
.getCallsite("testEvent", 3)).getIp());
403 assertEquals(5, (callsiteTest
.getCallsite("testEvent", 5)).getIp());
404 assertEquals(7, (callsiteTest
.getCallsite("testEvent", 6)).getIp());
405 assertEquals(7, (callsiteTest
.getCallsite("testEvent", 7)).getIp());
406 assertEquals(7, (callsiteTest
.getCallsite("testEvent", 8)).getIp());