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 CtfTestTrace testTrace
= CtfTestTrace
.KERNEL
;
50 private CTFTrace fixture
;
53 * Perform pre-test initialization.
57 assumeTrue(testTrace
.exists());
59 fixture
= testTrace
.getTraceFromFile();
60 } catch (CTFReaderException e
) {
61 /* If the assumeTrue() call passed, this should not happen. */
65 fixture
.setUUID(UUID
.randomUUID());
66 fixture
.setPacketHeader(new StructDeclaration(1L));
68 fixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
72 * Run the CTFTrace(File) constructor test with a known existing trace.
75 public void testOpen_existing() {
77 CTFTrace result
= testTrace
.getTraceFromFile();
78 assertNotNull(result
.getUUID());
79 } catch (CTFReaderException e
) {
85 * Run the CTFTrace(File) constructor test with an invalid path.
87 * @throws CTFReaderException
90 @Test(expected
= org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
.class)
91 public void testOpen_invalid() throws CTFReaderException
{
92 File path
= new File("");
93 CTFTrace result
= new CTFTrace(path
);
94 assertNotNull(result
);
98 * Run the boolean UUIDIsSet() method test.
101 public void testUUIDIsSet() {
102 boolean result
= fixture
.uuidIsSet();
107 * Run the void addStream(Stream) method test.
110 public void testAddStream() {
111 // test number of streams
112 int nbStreams
= fixture
.nbStreams();
113 assertEquals(1, nbStreams
);
117 Stream stream
= new Stream(testTrace
.getTrace());
119 fixture
.addStream(stream
);
120 } catch (CTFReaderException e
) {
122 } catch (ParseException e
) {
126 // test number of streams
127 nbStreams
= fixture
.nbStreams();
128 assertEquals(2, nbStreams
);
132 * Run the boolean byteOrderIsSet() method test.
135 public void testByteOrderIsSet() {
136 boolean result
= fixture
.byteOrderIsSet();
141 * Run the ByteOrder getByteOrder() method test.
144 public void testGetByteOrder_1() {
145 ByteOrder result
= fixture
.getByteOrder();
146 assertNotNull(result
);
150 * Run the long getMajor() method test.
153 public void testGetMajor() {
154 long result
= fixture
.getMajor();
155 assertEquals(1L, result
);
159 * Run the long getMinor() method test.
162 public void testGetMinor() {
163 long result
= fixture
.getMinor();
164 assertEquals(1L, result
);
168 * Run the StructDeclaration getPacketHeader() method test.
171 public void testGetPacketHeader() {
172 StructDeclaration result
= fixture
.getPacketHeader();
173 assertNotNull(result
);
177 * Run the String getPath() method test.
180 public void testGetPath() {
181 String result
= fixture
.getPath();
182 assertNotNull(result
);
186 * Run the Stream getStream(Long) method test.
189 public void testGetStream() {
190 Long id
= new Long(0L);
191 Stream result
= fixture
.getStream(id
);
192 assertNotNull(result
);
196 * Run the Map<Long, Stream> getStreams() method test.
199 public void testGetStreams() {
200 Map
<Long
, Stream
> result
= fixture
.getStreams();
201 assertNotNull(result
);
205 * Run the File getTraceDirectory() method test.
208 public void testGetTraceDirectory() {
209 File result
= fixture
.getTraceDirectory();
210 assertNotNull(result
);
214 * Run the UUID getUUID() method test.
217 public void testGetUUID() {
218 UUID result
= fixture
.getUUID();
219 assertNotNull(result
);
223 * Run the Definition lookupDefinition(String) method test.
226 public void testLookupDefinition() {
227 String lookupPath
= "trace.packet.header";
228 Definition result
= fixture
.lookupDefinition(lookupPath
);
229 assertNotNull(result
);
233 * Run the boolean majortIsSet() method test.
236 public void testMajortIsSet() {
237 boolean result
= fixture
.majortIsSet();
242 * Run the boolean minorIsSet() method test.
245 public void testMinorIsSet() {
246 boolean result
= fixture
.minorIsSet();
251 * Run the boolean packetHeaderIsSet() method test with a valid header set.
254 public void testPacketHeaderIsSet_valid() {
255 boolean result
= fixture
.packetHeaderIsSet();
260 * Run the boolean packetHeaderIsSet() method test, without having a valid
264 public void testPacketHeaderIsSet_invalid() {
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
);
273 boolean result
= fixture2
.packetHeaderIsSet();
275 } catch (CTFReaderException e
) {
281 * Run the void setByteOrder(ByteOrder) method test.
284 public void testSetByteOrder() {
285 ByteOrder byteOrder
= ByteOrder
.BIG_ENDIAN
;
286 fixture
.setByteOrder(byteOrder
);
290 * Run the void setMajor(long) method test.
293 public void testSetMajor() {
295 fixture
.setMajor(major
);
299 * Run the void setMinor(long) method test.
302 public void testSetMinor() {
304 fixture
.setMinor(minor
);
308 * Run the void setPacketHeader(StructDeclaration) method test.
311 public void testSetPacketHeader() {
312 StructDeclaration packetHeader
= new StructDeclaration(1L);
313 fixture
.setPacketHeader(packetHeader
);
317 * Run the void setUUID(UUID) method test.
320 public void testSetUUID() {
321 UUID uuid
= UUID
.randomUUID();
322 fixture
.setUUID(uuid
);
326 * Run the CTFClock getClock/setClock method test.
329 public void testGetSetClock_1() {
330 String name
= "clockyClock";
331 fixture
.addClock(name
, new CTFClock());
332 CTFClock result
= fixture
.getClock(name
);
334 assertNotNull(result
);
338 * Run the CTFClock getClock/setClock method test.
341 public void testGetSetClock_2() {
343 CTFClock ctfClock
= new CTFClock();
344 ctfClock
.addAttribute("name", "Bob");
345 ctfClock
.addAttribute("pi", new Double(java
.lang
.Math
.PI
));
346 fixture
.addClock(name
, ctfClock
);
347 CTFClock result
= fixture
.getClock(name
);
349 assertNotNull(result
);
350 assertTrue((Double
) ctfClock
.getProperty("pi") > 3.0);
351 assertTrue(ctfClock
.getName().equals("Bob"));
355 * Run the String lookupEnvironment(String) method test.
358 public void testLookupEnvironment_1() {
360 String result
= fixture
.lookupEnvironment(key
);
365 * Run the String lookupEnvironment(String) method test.
368 public void testLookupEnvironment_2() {
369 String key
= "otherTest";
370 String result
= fixture
.lookupEnvironment(key
);
375 * Run the String lookupEnvironment(String) method test.
378 public void testLookupEnvironment_3() {
380 fixture
.addEnvironmentVar(key
, key
);
381 String result
= fixture
.lookupEnvironment(key
);
382 assertTrue(result
.equals(key
));
386 * Run the String lookupEnvironment(String) method test.
389 public void testLookupEnvironment_4() {
391 fixture
.addEnvironmentVar(key
, "bozo");
392 fixture
.addEnvironmentVar(key
, "the clown");
393 String result
= fixture
.lookupEnvironment(key
);
394 assertNotNull(result
);
398 * Test for getCallsite(eventName, ip)
399 * @throws CTFReaderException not expected
402 public void callsitePosition() throws CTFReaderException
{
406 CTFTrace callsiteTest
= testTrace
.getTraceFromFile();
407 callsiteTest
.addCallsite("testEvent", null, ip1
, null, 23);
408 callsiteTest
.addCallsite("testEvent", null, ip2
, null, 50);
409 callsiteTest
.addCallsite("testEvent", null, ip3
, null, 15);
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());