btf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / trace / CTFTraceTest.java
index fa9c087e1d104547ec48161f607d13faedd88050..aac1d6b445671de0ee13ccdf5fe10782363a2429 100644 (file)
@@ -1,3 +1,16 @@
+/*******************************************************************************
+ * Copyright (c) 2013, 2014 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
+ *     Simon Delisle - Add test for getCallsite(eventName, ip)
+ *******************************************************************************/
+
 package org.eclipse.linuxtools.ctf.core.tests.trace;
 
 import static org.junit.Assert.assertEquals;
@@ -5,21 +18,21 @@ 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;
-import java.util.Map;
 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.IDefinition;
 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.CtfTestTrace;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
+import org.eclipse.linuxtools.ctf.core.trace.CTFStream;
 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,25 @@ 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 CtfTestTrace testTrace = CtfTestTrace.KERNEL;
 
-    /**
-     * Launch the test.
-     *
-     * @param args
-     *            the command line arguments
-     */
-    public static void main(String[] args) {
-        new org.junit.runner.JUnitCore().run(CTFTraceTest.class);
-    }
+    private CTFTrace fixture;
 
     /**
      * Perform pre-test initialization.
      */
     @Before
     public void setUp() {
-        fixture = TestParams.createTraceFromFile();
+        assumeTrue(testTrace.exists());
+        try {
+            fixture = testTrace.getTraceFromFile();
+        } catch (CTFReaderException e) {
+            /* If the assumeTrue() call passed, this should not happen. */
+            fail();
+        }
         fixture.setMinor(1L);
         fixture.setUUID(UUID.randomUUID());
         fixture.setPacketHeader(new StructDeclaration(1L));
@@ -58,33 +67,30 @@ 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();
-        assertNotNull(result.getUUID());
+        try (CTFTrace result = testTrace.getTraceFromFile();) {
+            assertNotNull(result.getUUID());
+        } catch (CTFReaderException e) {
+            fail();
+        }
     }
 
     /**
      * 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$
-        CTFTrace result = new CTFTrace(path);
-        assertNotNull(result);
+        File path = new File("");
+        try (CTFTrace result = new CTFTrace(path);) {
+            assertNotNull(result);
+        }
     }
 
     /**
@@ -92,25 +98,30 @@ public class CTFTraceTest {
      */
     @Test
     public void testUUIDIsSet() {
-        boolean result = fixture.UUIDIsSet();
+        boolean result = fixture.uuidIsSet();
         assertTrue(result);
     }
 
     /**
      * Run the void addStream(Stream) method test.
-     *
-     * @throws ParseException
-     * @throws CTFReaderException
      */
     @Test
-    public void testAddStream() throws ParseException, CTFReaderException {
+    public void testAddStream() {
         // test number of streams
         int nbStreams = fixture.nbStreams();
         assertEquals(1, nbStreams);
+
         // Add a stream
-        Stream stream = new Stream(TestParams.createTrace());
-        stream.setId(1234);
-        fixture.addStream(stream);
+        try {
+            CTFStream stream = new CTFStream(testTrace.getTrace());
+            stream.setId(1234);
+            fixture.addStream(stream);
+        } catch (CTFReaderException e) {
+            fail();
+        } catch (ParseException e) {
+            fail();
+        }
+
         // test number of streams
         nbStreams = fixture.nbStreams();
         assertEquals(2, nbStreams);
@@ -176,16 +187,7 @@ public class CTFTraceTest {
     @Test
     public void testGetStream() {
         Long id = new Long(0L);
-        Stream result = fixture.getStream(id);
-        assertNotNull(result);
-    }
-
-    /**
-     * Run the Map<Long, Stream> getStreams() method test.
-     */
-    @Test
-    public void testGetStreams() {
-        Map<Long, Stream> result = fixture.getStreams();
+        CTFStream result = fixture.getStream(id);
         assertNotNull(result);
     }
 
@@ -212,17 +214,17 @@ public class CTFTraceTest {
      */
     @Test
     public void testLookupDefinition() {
-        String lookupPath = "trace.packet.header"; //$NON-NLS-1$
-        Definition result = fixture.lookupDefinition(lookupPath);
+        String lookupPath = "trace.packet.header";
+        IDefinition result = fixture.lookupDefinition(lookupPath);
         assertNotNull(result);
     }
 
     /**
-     * Run the boolean majortIsSet() method test.
+     * Run the boolean majorIsSet() method test.
      */
     @Test
-    public void testMajortIsSet() {
-        boolean result = fixture.majortIsSet();
+    public void testMajorIsSet() {
+        boolean result = fixture.majorIsSet();
         assertTrue(result);
     }
 
@@ -250,15 +252,18 @@ public class CTFTraceTest {
      */
     @Test
     public void testPacketHeaderIsSet_invalid() {
-        CTFTrace fixture2 = TestParams.createTraceFromFile();
-        fixture2.setMinor(1L);
-        fixture2.setUUID(UUID.randomUUID());
-        fixture2.setPacketHeader((StructDeclaration) null); /* it's null here! */
-        fixture2.setMajor(1L);
-        fixture2.setByteOrder(ByteOrder.BIG_ENDIAN);
-
-        boolean result = fixture2.packetHeaderIsSet();
-        assertFalse(result);
+        try (CTFTrace fixture2 = testTrace.getTraceFromFile();){
+            fixture2.setMinor(1L);
+            fixture2.setUUID(UUID.randomUUID());
+            fixture2.setPacketHeader((StructDeclaration) null); /* it's null here! */
+            fixture2.setMajor(1L);
+            fixture2.setByteOrder(ByteOrder.BIG_ENDIAN);
+
+            boolean result = fixture2.packetHeaderIsSet();
+            assertFalse(result);
+        } catch (CTFReaderException e) {
+            fail();
+        }
     }
 
     /**
@@ -307,41 +312,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);
 
@@ -349,20 +324,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"));
     }
 
     /**
@@ -370,8 +345,8 @@ public class CTFTraceTest {
      */
     @Test
     public void testLookupEnvironment_1() {
-        String key = ""; //$NON-NLS-1$
-        String result = fixture.lookupEnvironment(key);
+        String key = "";
+        String result = fixture.getEnvironment().get(key);
         assertNull(result);
     }
 
@@ -380,8 +355,8 @@ public class CTFTraceTest {
      */
     @Test
     public void testLookupEnvironment_2() {
-        String key = "otherTest"; //$NON-NLS-1$
-        String result = fixture.lookupEnvironment(key);
+        String key = "otherTest";
+        String result = fixture.getEnvironment().get(key);
         assertNull(result);
     }
 
@@ -390,9 +365,9 @@ 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);
+        String result = fixture.getEnvironment().get(key);
         assertTrue(result.equals(key));
     }
 
@@ -401,11 +376,35 @@ 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 result = fixture.lookupEnvironment(key);
+        String key = "test";
+        fixture.addEnvironmentVar(key, "bozo");
+        fixture.addEnvironmentVar(key, "the clown");
+        String result = fixture.getEnvironment().get(key);
         assertNotNull(result);
     }
 
+    /**
+     * Test for getCallsite(eventName, ip)
+     * @throws CTFReaderException not expected
+     */
+    @Test
+    public void callsitePosition() throws CTFReaderException {
+        long ip1 = 2;
+        long ip2 = 5;
+        long ip3 = 7;
+        try (CTFTrace callsiteTest = testTrace.getTraceFromFile()) {
+            callsiteTest.addCallsite("testEvent", null, ip1, null, 23);
+            callsiteTest.addCallsite("testEvent", null, ip2, null, 50);
+            callsiteTest.addCallsite("testEvent", null, ip3, null, 15);
+
+            assertEquals(2, (callsiteTest.getCallsite("testEvent", 1)).getIp());
+            assertEquals(2, (callsiteTest.getCallsite("testEvent", 2)).getIp());
+            assertEquals(5, (callsiteTest.getCallsite("testEvent", 3)).getIp());
+            assertEquals(5, (callsiteTest.getCallsite("testEvent", 5)).getIp());
+            assertEquals(7, (callsiteTest.getCallsite("testEvent", 6)).getIp());
+            assertEquals(7, (callsiteTest.getCallsite("testEvent", 7)).getIp());
+            assertEquals(7, (callsiteTest.getCallsite("testEvent", 8)).getIp());
+        }
+    }
+
 }
This page took 0.029399 seconds and 5 git commands to generate.