Integrate Babeltrace CTF tests and fix parsing problems
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / trace / CTFTraceTest.java
index 26296657318b924d7c1cd30838cd50436779a19c..fcf44a48dbaab2553a48a7e218b66ca84eb11eeb 100644 (file)
@@ -1,3 +1,15 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Ericsson
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Matthew Khouzam - Initial API and implementation
+ *     Marc-Andre Laperle - Test in traces directory recursively
+ *******************************************************************************/
+
 package org.eclipse.linuxtools.ctf.core.tests.trace;
 
 import static org.junit.Assert.assertEquals;
@@ -5,6 +17,8 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
 
 import java.io.File;
 import java.nio.ByteOrder;
@@ -14,12 +28,11 @@ import java.util.UUID;
 import org.eclipse.linuxtools.ctf.core.event.CTFClock;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
-import org.eclipse.linuxtools.ctf.core.tests.TestParams;
+import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTraces;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
+import org.eclipse.linuxtools.ctf.core.trace.Stream;
 import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
-import org.eclipse.linuxtools.internal.ctf.core.trace.Stream;
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -28,29 +41,27 @@ import org.junit.Test;
  * <code>{@link CTFTrace}</code>.
  *
  * @author ematkho
- * @version $Revision: 1.0 $
  */
-@SuppressWarnings("javadoc")
 public class CTFTraceTest {
 
-    private CTFTrace fixture;
+    private static final String TRACES_DIRECTORY = "../org.eclipse.linuxtools.ctf.core.tests/traces";
 
-    /**
-     * Launch the test.
-     *
-     * @param args
-     *            the command line arguments
-     */
-    public static void main(String[] args) {
-        new org.junit.runner.JUnitCore().run(CTFTraceTest.class);
-    }
+    private static final String METADATA_FILENAME = "metadata";
+
+    private static final int TRACE_INDEX = 0;
+
+    private static final String CTF_VERSION_NUMBER = "1.8";
+    private static final String CTF_SUITE_TEST_DIRECTORY = "ctf-testsuite/tests/" + CTF_VERSION_NUMBER;
+
+    private CTFTrace fixture;
 
     /**
      * Perform pre-test initialization.
      */
     @Before
     public void setUp() {
-        fixture = TestParams.createTraceFromFile();
+        assumeTrue(CtfTestTraces.tracesExist());
+        fixture = CtfTestTraces.getTestTraceFromFile(TRACE_INDEX);
         fixture.setMinor(1L);
         fixture.setUUID(UUID.randomUUID());
         fixture.setPacketHeader(new StructDeclaration(1L));
@@ -58,20 +69,12 @@ public class CTFTraceTest {
         fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
     }
 
-    /**
-     * Perform post-test clean-up.
-     */
-    @After
-    public void tearDown() {
-        // Add additional tear down code here
-    }
-
     /**
      * Run the CTFTrace(File) constructor test with a known existing trace.
      */
     @Test
     public void testOpen_existing() {
-        CTFTrace result = TestParams.createTraceFromFile();
+        CTFTrace result = CtfTestTraces.getTestTraceFromFile(TRACE_INDEX);
         assertNotNull(result.getUUID());
     }
 
@@ -79,10 +82,11 @@ public class CTFTraceTest {
      * Run the CTFTrace(File) constructor test with an invalid path.
      *
      * @throws CTFReaderException
+     *             is expected
      */
     @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class)
     public void testOpen_invalid() throws CTFReaderException {
-        File path = new File(""); //$NON-NLS-1$
+        File path = new File("");
         CTFTrace result = new CTFTrace(path);
         assertNotNull(result);
     }
@@ -98,15 +102,27 @@ public class CTFTraceTest {
 
     /**
      * Run the void addStream(Stream) method test.
-     *
-     * @throws ParseException
-     * @throws CTFReaderException
      */
     @Test
-    public void testAddStream() throws ParseException, CTFReaderException {
-        Stream stream = new Stream(TestParams.createTrace());
-        stream.setId(1234);
-        fixture.addStream(stream);
+    public void testAddStream() {
+        // test number of streams
+        int nbStreams = fixture.nbStreams();
+        assertEquals(1, nbStreams);
+
+        // Add a stream
+        try {
+            Stream stream = new Stream(CtfTestTraces.getTestTrace(TRACE_INDEX));
+            stream.setId(1234);
+            fixture.addStream(stream);
+        } catch (CTFReaderException e) {
+            fail();
+        } catch (ParseException e) {
+            fail();
+        }
+
+        // test number of streams
+        nbStreams = fixture.nbStreams();
+        assertEquals(2, nbStreams);
     }
 
     /**
@@ -205,7 +221,7 @@ public class CTFTraceTest {
      */
     @Test
     public void testLookupDefinition() {
-        String lookupPath = "trace.packet.header"; //$NON-NLS-1$
+        String lookupPath = "trace.packet.header";
         Definition result = fixture.lookupDefinition(lookupPath);
         assertNotNull(result);
     }
@@ -228,15 +244,6 @@ public class CTFTraceTest {
         assertTrue(result);
     }
 
-    /**
-     * Run the int nbStreams() method test.
-     */
-    @Test
-    public void testNbStreams() {
-        int result = fixture.nbStreams();
-        assertEquals(2, result);
-    }
-
     /**
      * Run the boolean packetHeaderIsSet() method test with a valid header set.
      */
@@ -252,7 +259,7 @@ public class CTFTraceTest {
      */
     @Test
     public void testPacketHeaderIsSet_invalid() {
-        CTFTrace fixture2 = TestParams.createTraceFromFile();
+        CTFTrace fixture2 = CtfTestTraces.getTestTraceFromFile(TRACE_INDEX);
         fixture2.setMinor(1L);
         fixture2.setUUID(UUID.randomUUID());
         fixture2.setPacketHeader((StructDeclaration) null); /* it's null here! */
@@ -309,41 +316,11 @@ public class CTFTraceTest {
     }
 
     /**
-     * Run the CTFClock getClock() method test.
+     * Run the CTFClock getClock/setClock method test.
      */
     @Test
-    public void testGetClock_1() {
-        CTFClock result = fixture.getClock();
-        assertNotNull(result);
-    }
-
-    /**
-     * Run the CTFClock getClock() method test.
-     *
-     */
-    @Test
-    public void testGetClock_2() {
-        CTFClock result = fixture.getClock("Blabla"); //$NON-NLS-1$
-        assertNull(result);
-    }
-
-    /**
-     * Run the CTFClock getClock(String) method test.
-     */
-    @Test
-    public void testGetClock_3() {
-        String name = "invisibleClock"; //$NON-NLS-1$
-        CTFClock result = fixture.getClock(name);
-        assertNull(result);
-    }
-
-
-    /**
-     * Run the CTFClock getClock(String) method test.
-     */
-    @Test
-    public void testSetClock_1() {
-        String name = "clockyClock"; //$NON-NLS-1$
+    public void testGetSetClock_1() {
+        String name = "clockyClock";
         fixture.addClock(name, new CTFClock());
         CTFClock result = fixture.getClock(name);
 
@@ -351,20 +328,20 @@ public class CTFTraceTest {
     }
 
     /**
-     * Run the CTFClock getClock(String) method test.
+     * Run the CTFClock getClock/setClock method test.
      */
     @Test
-    public void testSetClock_2() {
-        String name = ""; //$NON-NLS-1$
+    public void testGetSetClock_2() {
+        String name = "";
         CTFClock ctfClock = new CTFClock();
-        ctfClock.addAttribute("name", "Bob"); //$NON-NLS-1$ //$NON-NLS-2$
-        ctfClock.addAttribute("pi", new Double(java.lang.Math.PI)); //$NON-NLS-1$
+        ctfClock.addAttribute("name", "Bob");
+        ctfClock.addAttribute("pi", new Double(java.lang.Math.PI));
         fixture.addClock(name, ctfClock);
         CTFClock result = fixture.getClock(name);
 
         assertNotNull(result);
-        assertTrue( (Double)ctfClock.getProperty("pi")> 3.0); //$NON-NLS-1$
-        assertTrue( ctfClock.getName().equals("Bob")); //$NON-NLS-1$
+        assertTrue((Double) ctfClock.getProperty("pi") > 3.0);
+        assertTrue(ctfClock.getName().equals("Bob"));
     }
 
     /**
@@ -372,7 +349,7 @@ public class CTFTraceTest {
      */
     @Test
     public void testLookupEnvironment_1() {
-        String key = ""; //$NON-NLS-1$
+        String key = "";
         String result = fixture.lookupEnvironment(key);
         assertNull(result);
     }
@@ -382,7 +359,7 @@ public class CTFTraceTest {
      */
     @Test
     public void testLookupEnvironment_2() {
-        String key = "otherTest"; //$NON-NLS-1$
+        String key = "otherTest";
         String result = fixture.lookupEnvironment(key);
         assertNull(result);
     }
@@ -392,7 +369,7 @@ public class CTFTraceTest {
      */
     @Test
     public void testLookupEnvironment_3() {
-        String key = "test"; //$NON-NLS-1$
+        String key = "test";
         fixture.addEnvironmentVar(key, key);
         String result = fixture.lookupEnvironment(key);
         assertTrue(result.equals(key));
@@ -403,11 +380,75 @@ public class CTFTraceTest {
      */
     @Test
     public void testLookupEnvironment_4() {
-        String key = "test"; //$NON-NLS-1$
-        fixture.addEnvironmentVar(key, "bozo"); //$NON-NLS-1$
-        fixture.addEnvironmentVar(key, "the clown"); //$NON-NLS-1$
+        String key = "test";
+        fixture.addEnvironmentVar(key, "bozo");
+        fixture.addEnvironmentVar(key, "the clown");
         String result = fixture.lookupEnvironment(key);
         assertNotNull(result);
     }
 
+    /**
+     * Open traces in specified directories and expect them to fail
+     *
+     * @throws CTFReaderException not expected
+     */
+    @Test
+    public void testFailedParse() throws CTFReaderException {
+        parseTracesInDirectory(getTestTracesSubDirectory(CTF_SUITE_TEST_DIRECTORY + "/fail"), true);
+    }
+
+    /**
+     * Open traces in specified directories and expect them to succeed
+     *
+     * @throws CTFReaderException not expected
+     */
+    @Test
+    public void testSuccessfulParse() throws CTFReaderException {
+        parseTracesInDirectory(getTestTracesSubDirectory("kernel"), false);
+        parseTracesInDirectory(getTestTracesSubDirectory("trace2"), false);
+        parseTracesInDirectory(getTestTracesSubDirectory(CTF_SUITE_TEST_DIRECTORY + "/pass"), false);
+    }
+
+    /**
+     * Get the File object for the subDir in the traces directory. If the sub directory doesn't exist, the test is skipped.
+     */
+    private static File getTestTracesSubDirectory(String subDir) {
+        File file = new File(TRACES_DIRECTORY + "/" + subDir);
+        assumeTrue(file.isDirectory());
+        return file;
+    }
+
+    /**
+     * Parse the traces in given directory recursively
+     *
+     * @param directory The directory to search in
+     * @param expectException Whether or not traces in this directory are expected to throw an exception when parsed
+     * @throws CTFReaderException
+     */
+    void parseTracesInDirectory(File directory, boolean expectException) throws CTFReaderException {
+        for (File file : directory.listFiles()) {
+            if (file.getName().equals(METADATA_FILENAME)) {
+                try {
+                    new CTFTrace(directory);
+                    if (expectException) {
+                        fail("Trace was expected to fail parsing: " + directory);
+                    }
+                } catch (RuntimeException e) {
+                    if (!expectException) {
+                        throw new CTFReaderException("Failed parsing " + directory, e);
+                    }
+                } catch (CTFReaderException e) {
+                    if (!expectException) {
+                        throw new CTFReaderException("Failed parsing " + directory, e);
+                    }
+                }
+                return;
+            }
+
+            if (file.isDirectory()) {
+                parseTracesInDirectory(file, expectException);
+            }
+        }
+    }
+
 }
This page took 0.028901 seconds and 5 git commands to generate.