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