Merge branch 'master' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / trace / CTFTraceTest.java
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
7 *
8 * Contributors:
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 *******************************************************************************/
13
14 package org.eclipse.linuxtools.ctf.core.tests.trace;
15
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;
23
24 import java.io.File;
25 import java.nio.ByteOrder;
26 import java.util.Map;
27 import java.util.UUID;
28
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;
39
40 /**
41 * The class <code>CTFTraceTest</code> contains tests for the class
42 * <code>{@link CTFTrace}</code>.
43 *
44 * @author ematkho
45 */
46 public class CTFTraceTest {
47
48 private static final String TRACES_DIRECTORY = "../org.eclipse.linuxtools.ctf.core.tests/traces";
49
50 private static final String METADATA_FILENAME = "metadata";
51
52 private static final int TRACE_INDEX = 0;
53
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;
56
57 private CTFTrace fixture;
58
59 /**
60 * Perform pre-test initialization.
61 */
62 @Before
63 public void setUp() {
64 assumeTrue(CtfTestTraces.tracesExist());
65 fixture = CtfTestTraces.getTestTraceFromFile(TRACE_INDEX);
66 fixture.setMinor(1L);
67 fixture.setUUID(UUID.randomUUID());
68 fixture.setPacketHeader(new StructDeclaration(1L));
69 fixture.setMajor(1L);
70 fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
71 }
72
73 /**
74 * Run the CTFTrace(File) constructor test with a known existing trace.
75 */
76 @Test
77 public void testOpen_existing() {
78 CTFTrace result = CtfTestTraces.getTestTraceFromFile(TRACE_INDEX);
79 assertNotNull(result.getUUID());
80 }
81
82 /**
83 * Run the CTFTrace(File) constructor test with an invalid path.
84 *
85 * @throws CTFReaderException
86 * is expected
87 */
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);
93 }
94
95 /**
96 * Run the boolean UUIDIsSet() method test.
97 */
98 @Test
99 public void testUUIDIsSet() {
100 boolean result = fixture.uuidIsSet();
101 assertTrue(result);
102 }
103
104 /**
105 * Run the void addStream(Stream) method test.
106 */
107 @Test
108 public void testAddStream() {
109 // test number of streams
110 int nbStreams = fixture.nbStreams();
111 assertEquals(1, nbStreams);
112
113 // Add a stream
114 try {
115 Stream stream = new Stream(CtfTestTraces.getTestTrace(TRACE_INDEX));
116 stream.setId(1234);
117 fixture.addStream(stream);
118 } catch (CTFReaderException e) {
119 fail();
120 } catch (ParseException e) {
121 fail();
122 }
123
124 // test number of streams
125 nbStreams = fixture.nbStreams();
126 assertEquals(2, nbStreams);
127 }
128
129 /**
130 * Run the boolean byteOrderIsSet() method test.
131 */
132 @Test
133 public void testByteOrderIsSet() {
134 boolean result = fixture.byteOrderIsSet();
135 assertTrue(result);
136 }
137
138 /**
139 * Run the ByteOrder getByteOrder() method test.
140 */
141 @Test
142 public void testGetByteOrder_1() {
143 ByteOrder result = fixture.getByteOrder();
144 assertNotNull(result);
145 }
146
147 /**
148 * Run the long getMajor() method test.
149 */
150 @Test
151 public void testGetMajor() {
152 long result = fixture.getMajor();
153 assertEquals(1L, result);
154 }
155
156 /**
157 * Run the long getMinor() method test.
158 */
159 @Test
160 public void testGetMinor() {
161 long result = fixture.getMinor();
162 assertEquals(1L, result);
163 }
164
165 /**
166 * Run the StructDeclaration getPacketHeader() method test.
167 */
168 @Test
169 public void testGetPacketHeader() {
170 StructDeclaration result = fixture.getPacketHeader();
171 assertNotNull(result);
172 }
173
174 /**
175 * Run the String getPath() method test.
176 */
177 @Test
178 public void testGetPath() {
179 String result = fixture.getPath();
180 assertNotNull(result);
181 }
182
183 /**
184 * Run the Stream getStream(Long) method test.
185 */
186 @Test
187 public void testGetStream() {
188 Long id = new Long(0L);
189 Stream result = fixture.getStream(id);
190 assertNotNull(result);
191 }
192
193 /**
194 * Run the Map<Long, Stream> getStreams() method test.
195 */
196 @Test
197 public void testGetStreams() {
198 Map<Long, Stream> result = fixture.getStreams();
199 assertNotNull(result);
200 }
201
202 /**
203 * Run the File getTraceDirectory() method test.
204 */
205 @Test
206 public void testGetTraceDirectory() {
207 File result = fixture.getTraceDirectory();
208 assertNotNull(result);
209 }
210
211 /**
212 * Run the UUID getUUID() method test.
213 */
214 @Test
215 public void testGetUUID() {
216 UUID result = fixture.getUUID();
217 assertNotNull(result);
218 }
219
220 /**
221 * Run the Definition lookupDefinition(String) method test.
222 */
223 @Test
224 public void testLookupDefinition() {
225 String lookupPath = "trace.packet.header";
226 Definition result = fixture.lookupDefinition(lookupPath);
227 assertNotNull(result);
228 }
229
230 /**
231 * Run the boolean majortIsSet() method test.
232 */
233 @Test
234 public void testMajortIsSet() {
235 boolean result = fixture.majortIsSet();
236 assertTrue(result);
237 }
238
239 /**
240 * Run the boolean minorIsSet() method test.
241 */
242 @Test
243 public void testMinorIsSet() {
244 boolean result = fixture.minorIsSet();
245 assertTrue(result);
246 }
247
248 /**
249 * Run the boolean packetHeaderIsSet() method test with a valid header set.
250 */
251 @Test
252 public void testPacketHeaderIsSet_valid() {
253 boolean result = fixture.packetHeaderIsSet();
254 assertTrue(result);
255 }
256
257 /**
258 * Run the boolean packetHeaderIsSet() method test, without having a valid
259 * header set.
260 */
261 @Test
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);
269
270 boolean result = fixture2.packetHeaderIsSet();
271 assertFalse(result);
272 }
273
274 /**
275 * Run the void setByteOrder(ByteOrder) method test.
276 */
277 @Test
278 public void testSetByteOrder() {
279 ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
280 fixture.setByteOrder(byteOrder);
281 }
282
283 /**
284 * Run the void setMajor(long) method test.
285 */
286 @Test
287 public void testSetMajor() {
288 long major = 1L;
289 fixture.setMajor(major);
290 }
291
292 /**
293 * Run the void setMinor(long) method test.
294 */
295 @Test
296 public void testSetMinor() {
297 long minor = 1L;
298 fixture.setMinor(minor);
299 }
300
301 /**
302 * Run the void setPacketHeader(StructDeclaration) method test.
303 */
304 @Test
305 public void testSetPacketHeader() {
306 StructDeclaration packetHeader = new StructDeclaration(1L);
307 fixture.setPacketHeader(packetHeader);
308 }
309
310 /**
311 * Run the void setUUID(UUID) method test.
312 */
313 @Test
314 public void testSetUUID() {
315 UUID uuid = UUID.randomUUID();
316 fixture.setUUID(uuid);
317 }
318
319 /**
320 * Run the CTFClock getClock/setClock method test.
321 */
322 @Test
323 public void testGetSetClock_1() {
324 String name = "clockyClock";
325 fixture.addClock(name, new CTFClock());
326 CTFClock result = fixture.getClock(name);
327
328 assertNotNull(result);
329 }
330
331 /**
332 * Run the CTFClock getClock/setClock method test.
333 */
334 @Test
335 public void testGetSetClock_2() {
336 String name = "";
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);
342
343 assertNotNull(result);
344 assertTrue((Double) ctfClock.getProperty("pi") > 3.0);
345 assertTrue(ctfClock.getName().equals("Bob"));
346 }
347
348 /**
349 * Run the String lookupEnvironment(String) method test.
350 */
351 @Test
352 public void testLookupEnvironment_1() {
353 String key = "";
354 String result = fixture.lookupEnvironment(key);
355 assertNull(result);
356 }
357
358 /**
359 * Run the String lookupEnvironment(String) method test.
360 */
361 @Test
362 public void testLookupEnvironment_2() {
363 String key = "otherTest";
364 String result = fixture.lookupEnvironment(key);
365 assertNull(result);
366 }
367
368 /**
369 * Run the String lookupEnvironment(String) method test.
370 */
371 @Test
372 public void testLookupEnvironment_3() {
373 String key = "test";
374 fixture.addEnvironmentVar(key, key);
375 String result = fixture.lookupEnvironment(key);
376 assertTrue(result.equals(key));
377 }
378
379 /**
380 * Run the String lookupEnvironment(String) method test.
381 */
382 @Test
383 public void testLookupEnvironment_4() {
384 String key = "test";
385 fixture.addEnvironmentVar(key, "bozo");
386 fixture.addEnvironmentVar(key, "the clown");
387 String result = fixture.lookupEnvironment(key);
388 assertNotNull(result);
389 }
390
391 /**
392 * Open traces in specified directories and expect them to fail
393 *
394 * @throws CTFReaderException not expected
395 */
396 @Test
397 public void testFailedParse() throws CTFReaderException {
398 parseTracesInDirectory(getTestTracesSubDirectory(CTF_SUITE_TEST_DIRECTORY + "/fail"), true);
399 }
400
401 /**
402 * Open traces in specified directories and expect them to succeed
403 *
404 * @throws CTFReaderException not expected
405 */
406 @Test
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);
411 }
412
413 /**
414 * Get the File object for the subDir in the traces directory. If the sub directory doesn't exist, the test is skipped.
415 */
416 private static File getTestTracesSubDirectory(String subDir) {
417 File file = new File(TRACES_DIRECTORY + "/" + subDir);
418 assumeTrue(file.isDirectory());
419 return file;
420 }
421
422 /**
423 * Parse the traces in given directory recursively
424 *
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
428 */
429 void parseTracesInDirectory(File directory, boolean expectException) throws CTFReaderException {
430 for (File file : directory.listFiles()) {
431 if (file.getName().equals(METADATA_FILENAME)) {
432 try {
433 new CTFTrace(directory);
434 if (expectException) {
435 fail("Trace was expected to fail parsing: " + directory);
436 }
437 } catch (RuntimeException e) {
438 if (!expectException) {
439 throw new CTFReaderException("Failed parsing " + directory, e);
440 }
441 } catch (CTFReaderException e) {
442 if (!expectException) {
443 throw new CTFReaderException("Failed parsing " + directory, e);
444 }
445 }
446 return;
447 }
448
449 if (file.isDirectory()) {
450 parseTracesInDirectory(file, expectException);
451 }
452 }
453 }
454
455 /**
456 * Test for getCallsite(eventName, ip)
457 */
458 @Test
459 public void callsitePosition(){
460 long ip1 = 2;
461 long ip2 = 5;
462 long ip3 = 7;
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);
467
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());
475 }
476
477 }
This page took 0.044981 seconds and 6 git commands to generate.