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