| 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 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.ctf.core.tests.trace; |
| 14 | |
| 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; |
| 22 | |
| 23 | import java.io.File; |
| 24 | import java.nio.ByteOrder; |
| 25 | import java.util.Map; |
| 26 | import java.util.UUID; |
| 27 | |
| 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; |
| 38 | |
| 39 | /** |
| 40 | * The class <code>CTFTraceTest</code> contains tests for the class |
| 41 | * <code>{@link CTFTrace}</code>. |
| 42 | * |
| 43 | * @author ematkho |
| 44 | */ |
| 45 | public class CTFTraceTest { |
| 46 | |
| 47 | private static final String TRACES_DIRECTORY = "../org.eclipse.linuxtools.ctf.core.tests/traces"; |
| 48 | |
| 49 | private static final String METADATA_FILENAME = "metadata"; |
| 50 | |
| 51 | private static final int TRACE_INDEX = 0; |
| 52 | |
| 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 | |
| 56 | private CTFTrace fixture; |
| 57 | |
| 58 | /** |
| 59 | * Perform pre-test initialization. |
| 60 | */ |
| 61 | @Before |
| 62 | public void setUp() { |
| 63 | assumeTrue(CtfTestTraces.tracesExist()); |
| 64 | fixture = CtfTestTraces.getTestTraceFromFile(TRACE_INDEX); |
| 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 | |
| 72 | /** |
| 73 | * Run the CTFTrace(File) constructor test with a known existing trace. |
| 74 | */ |
| 75 | @Test |
| 76 | public void testOpen_existing() { |
| 77 | CTFTrace result = CtfTestTraces.getTestTraceFromFile(TRACE_INDEX); |
| 78 | assertNotNull(result.getUUID()); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Run the CTFTrace(File) constructor test with an invalid path. |
| 83 | * |
| 84 | * @throws CTFReaderException |
| 85 | * is expected |
| 86 | */ |
| 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); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Run the boolean UUIDIsSet() method test. |
| 96 | */ |
| 97 | @Test |
| 98 | public void testUUIDIsSet() { |
| 99 | boolean result = fixture.UUIDIsSet(); |
| 100 | assertTrue(result); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Run the void addStream(Stream) method test. |
| 105 | */ |
| 106 | @Test |
| 107 | public void testAddStream() { |
| 108 | // test number of streams |
| 109 | int nbStreams = fixture.nbStreams(); |
| 110 | assertEquals(1, nbStreams); |
| 111 | |
| 112 | // Add a stream |
| 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 | |
| 123 | // test number of streams |
| 124 | nbStreams = fixture.nbStreams(); |
| 125 | assertEquals(2, nbStreams); |
| 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() { |
| 224 | String lookupPath = "trace.packet.header"; |
| 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 | |
| 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() { |
| 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); |
| 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 | } |
| 317 | |
| 318 | /** |
| 319 | * Run the CTFClock getClock/setClock method test. |
| 320 | */ |
| 321 | @Test |
| 322 | public void testGetSetClock_1() { |
| 323 | String name = "clockyClock"; |
| 324 | fixture.addClock(name, new CTFClock()); |
| 325 | CTFClock result = fixture.getClock(name); |
| 326 | |
| 327 | assertNotNull(result); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Run the CTFClock getClock/setClock method test. |
| 332 | */ |
| 333 | @Test |
| 334 | public void testGetSetClock_2() { |
| 335 | String name = ""; |
| 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); |
| 341 | |
| 342 | assertNotNull(result); |
| 343 | assertTrue((Double) ctfClock.getProperty("pi") > 3.0); |
| 344 | assertTrue(ctfClock.getName().equals("Bob")); |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Run the String lookupEnvironment(String) method test. |
| 349 | */ |
| 350 | @Test |
| 351 | public void testLookupEnvironment_1() { |
| 352 | String key = ""; |
| 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() { |
| 362 | String key = "otherTest"; |
| 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() { |
| 372 | String key = "test"; |
| 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() { |
| 383 | String key = "test"; |
| 384 | fixture.addEnvironmentVar(key, "bozo"); |
| 385 | fixture.addEnvironmentVar(key, "the clown"); |
| 386 | String result = fixture.lookupEnvironment(key); |
| 387 | assertNotNull(result); |
| 388 | } |
| 389 | |
| 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 | |
| 454 | } |