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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.tests
.trace
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertFalse
;
17 import static org
.junit
.Assert
.assertNotNull
;
18 import static org
.junit
.Assert
.assertNull
;
19 import static org
.junit
.Assert
.assertTrue
;
20 import static org
.junit
.Assert
.fail
;
21 import static org
.junit
.Assume
.assumeTrue
;
24 import java
.nio
.ByteOrder
;
26 import java
.util
.UUID
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFClock
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.tests
.shared
.CtfTestTraces
;
32 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
33 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
34 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.Stream
;
35 import org
.eclipse
.linuxtools
.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 String TRACES_DIRECTORY
= "../org.eclipse.linuxtools.ctf.core.tests/traces";
49 private static final String METADATA_FILENAME
= "metadata";
51 private static final int TRACE_INDEX
= 0;
53 private static final String CTF_VERSION_NUMBER
= "1.8";
54 private static final String CTF_SUITE_TEST_DIRECTORY
= "ctf-testsuite/tests/" + CTF_VERSION_NUMBER
;
56 private CTFTrace fixture
;
59 * Perform pre-test initialization.
63 assumeTrue(CtfTestTraces
.tracesExist());
64 fixture
= CtfTestTraces
.getTestTraceFromFile(TRACE_INDEX
);
66 fixture
.setUUID(UUID
.randomUUID());
67 fixture
.setPacketHeader(new StructDeclaration(1L));
69 fixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
73 * Run the CTFTrace(File) constructor test with a known existing trace.
76 public void testOpen_existing() {
77 CTFTrace result
= CtfTestTraces
.getTestTraceFromFile(TRACE_INDEX
);
78 assertNotNull(result
.getUUID());
82 * Run the CTFTrace(File) constructor test with an invalid path.
84 * @throws CTFReaderException
87 @Test(expected
= org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
.class)
88 public void testOpen_invalid() throws CTFReaderException
{
89 File path
= new File("");
90 CTFTrace result
= new CTFTrace(path
);
91 assertNotNull(result
);
95 * Run the boolean UUIDIsSet() method test.
98 public void testUUIDIsSet() {
99 boolean result
= fixture
.uuidIsSet();
104 * Run the void addStream(Stream) method test.
107 public void testAddStream() {
108 // test number of streams
109 int nbStreams
= fixture
.nbStreams();
110 assertEquals(1, nbStreams
);
114 Stream stream
= new Stream(CtfTestTraces
.getTestTrace(TRACE_INDEX
));
116 fixture
.addStream(stream
);
117 } catch (CTFReaderException e
) {
119 } catch (ParseException e
) {
123 // test number of streams
124 nbStreams
= fixture
.nbStreams();
125 assertEquals(2, nbStreams
);
129 * Run the boolean byteOrderIsSet() method test.
132 public void testByteOrderIsSet() {
133 boolean result
= fixture
.byteOrderIsSet();
138 * Run the ByteOrder getByteOrder() method test.
141 public void testGetByteOrder_1() {
142 ByteOrder result
= fixture
.getByteOrder();
143 assertNotNull(result
);
147 * Run the long getMajor() method test.
150 public void testGetMajor() {
151 long result
= fixture
.getMajor();
152 assertEquals(1L, result
);
156 * Run the long getMinor() method test.
159 public void testGetMinor() {
160 long result
= fixture
.getMinor();
161 assertEquals(1L, result
);
165 * Run the StructDeclaration getPacketHeader() method test.
168 public void testGetPacketHeader() {
169 StructDeclaration result
= fixture
.getPacketHeader();
170 assertNotNull(result
);
174 * Run the String getPath() method test.
177 public void testGetPath() {
178 String result
= fixture
.getPath();
179 assertNotNull(result
);
183 * Run the Stream getStream(Long) method test.
186 public void testGetStream() {
187 Long id
= new Long(0L);
188 Stream result
= fixture
.getStream(id
);
189 assertNotNull(result
);
193 * Run the Map<Long, Stream> getStreams() method test.
196 public void testGetStreams() {
197 Map
<Long
, Stream
> result
= fixture
.getStreams();
198 assertNotNull(result
);
202 * Run the File getTraceDirectory() method test.
205 public void testGetTraceDirectory() {
206 File result
= fixture
.getTraceDirectory();
207 assertNotNull(result
);
211 * Run the UUID getUUID() method test.
214 public void testGetUUID() {
215 UUID result
= fixture
.getUUID();
216 assertNotNull(result
);
220 * Run the Definition lookupDefinition(String) method test.
223 public void testLookupDefinition() {
224 String lookupPath
= "trace.packet.header";
225 Definition result
= fixture
.lookupDefinition(lookupPath
);
226 assertNotNull(result
);
230 * Run the boolean majortIsSet() method test.
233 public void testMajortIsSet() {
234 boolean result
= fixture
.majortIsSet();
239 * Run the boolean minorIsSet() method test.
242 public void testMinorIsSet() {
243 boolean result
= fixture
.minorIsSet();
248 * Run the boolean packetHeaderIsSet() method test with a valid header set.
251 public void testPacketHeaderIsSet_valid() {
252 boolean result
= fixture
.packetHeaderIsSet();
257 * Run the boolean packetHeaderIsSet() method test, without having a valid
261 public void testPacketHeaderIsSet_invalid() {
262 CTFTrace fixture2
= CtfTestTraces
.getTestTraceFromFile(TRACE_INDEX
);
263 fixture2
.setMinor(1L);
264 fixture2
.setUUID(UUID
.randomUUID());
265 fixture2
.setPacketHeader((StructDeclaration
) null); /* it's null here! */
266 fixture2
.setMajor(1L);
267 fixture2
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
269 boolean result
= fixture2
.packetHeaderIsSet();
274 * Run the void setByteOrder(ByteOrder) method test.
277 public void testSetByteOrder() {
278 ByteOrder byteOrder
= ByteOrder
.BIG_ENDIAN
;
279 fixture
.setByteOrder(byteOrder
);
283 * Run the void setMajor(long) method test.
286 public void testSetMajor() {
288 fixture
.setMajor(major
);
292 * Run the void setMinor(long) method test.
295 public void testSetMinor() {
297 fixture
.setMinor(minor
);
301 * Run the void setPacketHeader(StructDeclaration) method test.
304 public void testSetPacketHeader() {
305 StructDeclaration packetHeader
= new StructDeclaration(1L);
306 fixture
.setPacketHeader(packetHeader
);
310 * Run the void setUUID(UUID) method test.
313 public void testSetUUID() {
314 UUID uuid
= UUID
.randomUUID();
315 fixture
.setUUID(uuid
);
319 * Run the CTFClock getClock/setClock method test.
322 public void testGetSetClock_1() {
323 String name
= "clockyClock";
324 fixture
.addClock(name
, new CTFClock());
325 CTFClock result
= fixture
.getClock(name
);
327 assertNotNull(result
);
331 * Run the CTFClock getClock/setClock method test.
334 public void testGetSetClock_2() {
336 CTFClock ctfClock
= new CTFClock();
337 ctfClock
.addAttribute("name", "Bob");
338 ctfClock
.addAttribute("pi", new Double(java
.lang
.Math
.PI
));
339 fixture
.addClock(name
, ctfClock
);
340 CTFClock result
= fixture
.getClock(name
);
342 assertNotNull(result
);
343 assertTrue((Double
) ctfClock
.getProperty("pi") > 3.0);
344 assertTrue(ctfClock
.getName().equals("Bob"));
348 * Run the String lookupEnvironment(String) method test.
351 public void testLookupEnvironment_1() {
353 String result
= fixture
.lookupEnvironment(key
);
358 * Run the String lookupEnvironment(String) method test.
361 public void testLookupEnvironment_2() {
362 String key
= "otherTest";
363 String result
= fixture
.lookupEnvironment(key
);
368 * Run the String lookupEnvironment(String) method test.
371 public void testLookupEnvironment_3() {
373 fixture
.addEnvironmentVar(key
, key
);
374 String result
= fixture
.lookupEnvironment(key
);
375 assertTrue(result
.equals(key
));
379 * Run the String lookupEnvironment(String) method test.
382 public void testLookupEnvironment_4() {
384 fixture
.addEnvironmentVar(key
, "bozo");
385 fixture
.addEnvironmentVar(key
, "the clown");
386 String result
= fixture
.lookupEnvironment(key
);
387 assertNotNull(result
);
391 * Open traces in specified directories and expect them to fail
393 * @throws CTFReaderException not expected
396 public void testFailedParse() throws CTFReaderException
{
397 parseTracesInDirectory(getTestTracesSubDirectory(CTF_SUITE_TEST_DIRECTORY
+ "/fail"), true);
401 * Open traces in specified directories and expect them to succeed
403 * @throws CTFReaderException not expected
406 public void testSuccessfulParse() throws CTFReaderException
{
407 parseTracesInDirectory(getTestTracesSubDirectory("kernel"), false);
408 parseTracesInDirectory(getTestTracesSubDirectory("trace2"), false);
409 parseTracesInDirectory(getTestTracesSubDirectory(CTF_SUITE_TEST_DIRECTORY
+ "/pass"), false);
413 * Get the File object for the subDir in the traces directory. If the sub directory doesn't exist, the test is skipped.
415 private static File
getTestTracesSubDirectory(String subDir
) {
416 File file
= new File(TRACES_DIRECTORY
+ "/" + subDir
);
417 assumeTrue(file
.isDirectory());
422 * Parse the traces in given directory recursively
424 * @param directory The directory to search in
425 * @param expectException Whether or not traces in this directory are expected to throw an exception when parsed
426 * @throws CTFReaderException
428 void parseTracesInDirectory(File directory
, boolean expectException
) throws CTFReaderException
{
429 for (File file
: directory
.listFiles()) {
430 if (file
.getName().equals(METADATA_FILENAME
)) {
432 new CTFTrace(directory
);
433 if (expectException
) {
434 fail("Trace was expected to fail parsing: " + directory
);
436 } catch (RuntimeException e
) {
437 if (!expectException
) {
438 throw new CTFReaderException("Failed parsing " + directory
, e
);
440 } catch (CTFReaderException e
) {
441 if (!expectException
) {
442 throw new CTFReaderException("Failed parsing " + directory
, e
);
448 if (file
.isDirectory()) {
449 parseTracesInDirectory(file
, expectException
);