tmf : Add test suite for the XML conditions
authorJean-Christian Kouame <jean-christian.kouame@ericsson.com>
Mon, 30 Nov 2015 23:35:47 +0000 (18:35 -0500)
committerBernd Hufmann <bernd.hufmann@ericsson.com>
Tue, 8 Dec 2015 19:50:53 +0000 (14:50 -0500)
Change-Id: I2a338f8b61cd920a7b3eb528be00aef696d6776b
Signed-off-by: Jean-Christian Kouame <jean-christian.kouame@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/61673
Reviewed-by: Hudson CI
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/common/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/common/TmfXmlTestFiles.java
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/module/XmlUtilsTest.java
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/AllTests.java
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderModelTest.java
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/TmfXmlConditionTest.java [new file with mode: 0644]
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_traces/testTrace2.xml [new file with mode: 0644]
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_xml_files/test_valid/test_conditions.xml [new file with mode: 0644]

index c5151111cf2e24420a15d21cb6c4e19d391ce1d0..0733d581f864452375b9bdac44e50d71cb327b8c 100644 (file)
@@ -38,7 +38,9 @@ public enum TmfXmlTestFiles {
     /** An invalid test file */
     INVALID_FILE("test_xml_files/test_invalid/test_invalid.xml"),
     /** A valid file for state attribute tests */
-    ATTRIBUTE_FILE("test_xml_files/test_valid/test_attributes.xml");
+    ATTRIBUTE_FILE("test_xml_files/test_valid/test_attributes.xml"),
+    /** A valid file for conditions tests */
+    CONDITION_FILE("test_xml_files/test_valid/test_conditions.xml");
 
     private final String fPath;
 
index b39da5fe20ee55b9713582d539d7f7275a039c86..dea9243c52c1659a99844facb2c56d499eb72c14 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2014 École Polytechnique de Montréal
+ * Copyright (c) 2014, 2015 École Polytechnique de Montréal and others
  *
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
@@ -27,13 +27,26 @@ import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
+import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
+import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
+import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
+import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
+import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
+import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.XmlStateSystemModule;
 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.Activator;
 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles;
+import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
+import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
 import org.junit.After;
 import org.junit.Test;
+import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
 
 /**
  * Tests for the {@link XmlUtils} class
@@ -208,4 +221,83 @@ public class XmlUtilsTest {
 
     }
 
+    /**
+     * Initialize a new trace based using the input file path
+     *
+     * @param traceFile
+     *            The trace file
+     * @return The trace
+     */
+    public static @NonNull ITmfTrace initializeTrace(String traceFile) {
+        /* Initialize the trace */
+        TmfXmlTraceStub trace = new TmfXmlTraceStub();
+        try {
+            trace.initTrace(null, Activator.getAbsolutePath(new Path(traceFile)).toOSString(), TmfEvent.class);
+        } catch (TmfTraceException e1) {
+            fail(e1.getMessage());
+        }
+        return trace;
+    }
+
+    /**
+     * Initialize a new module using the xml file
+     *
+     * @param xmlAnalysisFile
+     *            The xml file used to initialize the module
+     * @return The module
+     */
+    public static @NonNull XmlStateSystemModule initializeModule(TmfXmlTestFiles xmlAnalysisFile) {
+
+        /* Initialize the state provider module */
+        Document doc = xmlAnalysisFile.getXmlDocument();
+        assertNotNull(doc);
+
+        /* get State Providers modules */
+        NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER);
+        assertFalse(stateproviderNodes.getLength() == 0);
+
+        Element node = (Element) stateproviderNodes.item(0);
+        XmlStateSystemModule module = new XmlStateSystemModule();
+        String moduleId = node.getAttribute(TmfXmlStrings.ID);
+        assertNotNull(moduleId);
+        module.setId(moduleId);
+
+        module.setXmlFile(xmlAnalysisFile.getPath());
+
+        return module;
+    }
+
+    /**
+     * This function test the data provided by the state intervals queried
+     *
+     * @param testId
+     *            The id of the test
+     * @param ss
+     *            The state system associated to this test
+     * @param quark
+     *            The quark we want to query
+     * @param expectedStarts
+     *            The expected start timestamps for the intervals generated for
+     *            this quark
+     * @param expectedValues
+     *            The expected content values for this quark
+     * @throws AttributeNotFoundException
+     *             If the quark we want to query is invalid
+     * @throws StateSystemDisposedException
+     *             If the state system has been disposed before the end of the
+     *             queries
+     */
+    public static void verifyStateIntervals(String testId, @NonNull ITmfStateSystem ss, Integer quark, int[] expectedStarts, ITmfStateValue[] expectedValues) throws AttributeNotFoundException, StateSystemDisposedException {
+        int expectedCount = expectedStarts.length - 1;
+        List<ITmfStateInterval> intervals = StateSystemUtils.queryHistoryRange(ss, quark, expectedStarts[0], expectedStarts[expectedCount]);
+        assertEquals(testId + ": Interval count", expectedCount, intervals.size());
+        for (int i = 0; i < expectedCount; i++) {
+            ITmfStateInterval interval = intervals.get(i);
+            assertEquals(testId + ": Start time of interval " + i, expectedStarts[i], interval.getStartTime());
+            long actualEnd = (i == expectedCount - 1) ? (expectedStarts[i + 1]) : (expectedStarts[i + 1]) - 1;
+            assertEquals(testId + ": End time of interval " + i, actualEnd, interval.getEndTime());
+            assertEquals(testId + ": Expected value of interval " + i, expectedValues[i], interval.getStateValue());
+        }
+    }
+
 }
index f0245c1ce72b72ef58b1de2936d97a7939b242fd..9d91c93bfc19e57766510fb91b2c85e099a44d93 100644 (file)
@@ -23,7 +23,8 @@ import org.junit.runners.Suite;
 @Suite.SuiteClasses({
         StateProviderModelTest.class,
         StateProviderTest.class,
-        StateProviderModuleTest.class })
+        StateProviderModuleTest.class,
+        TmfXmlConditionTest.class})
 public class AllTests {
 
 }
\ No newline at end of file
index 9fdbfcdd2f2ab2de7299a7d06cd5316ce9c1f3f5..14943a302d50f541a9503df8f4fa507d4f45e43c 100644 (file)
 package org.eclipse.tracecompass.tmf.analysis.xml.core.tests.stateprovider;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
 import java.util.List;
 
-import org.eclipse.core.runtime.Path;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
-import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
-import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
-import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.XmlStateSystemModule;
-import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.Activator;
 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles;
-import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
+import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.module.XmlUtilsTest;
 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
-import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
-import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
 import org.junit.Test;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
 
 /**
  * Test the XML model for state providers
@@ -51,58 +40,13 @@ public class StateProviderModelTest {
 
     private static final @NonNull String testTrace1 = "test_traces/testTrace1.xml";
 
-    private static @NonNull ITmfTrace initializeTrace(String traceFile) {
-        /* Initialize the trace */
-        TmfXmlTraceStub trace = new TmfXmlTraceStub();
-        try {
-            trace.initTrace(null, Activator.getAbsolutePath(new Path(traceFile)).toOSString(), TmfEvent.class);
-        } catch (TmfTraceException e1) {
-            fail(e1.getMessage());
-        }
-        return trace;
-    }
-
-    private static @NonNull XmlStateSystemModule initializeModule(TmfXmlTestFiles xmlAnalysisFile) {
-
-        /* Initialize the state provider module */
-        Document doc = xmlAnalysisFile.getXmlDocument();
-        assertNotNull(doc);
-
-        /* get State Providers modules */
-        NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER);
-        assertFalse(stateproviderNodes.getLength() == 0);
-
-        Element node = (Element) stateproviderNodes.item(0);
-        XmlStateSystemModule module = new XmlStateSystemModule();
-        String moduleId = node.getAttribute(TmfXmlStrings.ID);
-        assertNotNull(moduleId);
-        module.setId(moduleId);
-
-        module.setXmlFile(xmlAnalysisFile.getPath());
-
-        return module;
-    }
-
-    private static void verifyStateIntervals(String testId, @NonNull ITmfStateSystem ss, Integer quark, int[] expectedStarts, ITmfStateValue[] expectedValues) throws AttributeNotFoundException, StateSystemDisposedException {
-        int expectedCount = expectedStarts.length - 1;
-        List<ITmfStateInterval> intervals = StateSystemUtils.queryHistoryRange(ss, quark, expectedStarts[0], expectedStarts[expectedCount]);
-        assertEquals(testId + ": Interval count", expectedCount, intervals.size());
-        for (int i = 0; i < expectedCount; i++) {
-            ITmfStateInterval interval = intervals.get(i);
-            assertEquals(testId + ": Start time of interval " + i, expectedStarts[i], interval.getStartTime());
-            long actualEnd = (i == expectedCount - 1) ? (expectedStarts[i + 1]) : (expectedStarts[i + 1]) - 1;
-            assertEquals(testId + ": End time of interval " + i, actualEnd, interval.getEndTime());
-            assertEquals(testId + ": Expected value of interval " + i, expectedValues[i], interval.getStateValue());
-        }
-    }
-
     /**
      * Test an increment of one, for an event name attribute
      */
     @Test
     public void testEventName() {
-        ITmfTrace trace = initializeTrace(testTrace1);
-        XmlStateSystemModule module = initializeModule(TmfXmlTestFiles.ATTRIBUTE_FILE);
+        ITmfTrace trace = XmlUtilsTest.initializeTrace(testTrace1);
+        XmlStateSystemModule module = XmlUtilsTest.initializeModule(TmfXmlTestFiles.ATTRIBUTE_FILE);
         try {
 
             module.setTrace(trace);
@@ -123,14 +67,14 @@ public class StateProviderModelTest {
                 {
                     final int[] expectedStarts = { 1, 5, 7 };
                     ITmfStateValue[] expectedValues = { TmfStateValue.newValueInt(1), TmfStateValue.newValueInt(2) };
-                    verifyStateIntervals("test", ss, quark, expectedStarts, expectedValues);
+                    XmlUtilsTest.verifyStateIntervals("test", ss, quark, expectedStarts, expectedValues);
                 }
                     break;
                 case "test1":
                 {
                     final int[] expectedStarts = { 1, 3, 7, 7 };
                     ITmfStateValue[] expectedValues = { TmfStateValue.nullValue(), TmfStateValue.newValueInt(1), TmfStateValue.newValueInt(2) };
-                    verifyStateIntervals("test1", ss, quark, expectedStarts, expectedValues);
+                    XmlUtilsTest.verifyStateIntervals("test1", ss, quark, expectedStarts, expectedValues);
                 }
                     break;
                 default:
diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/TmfXmlConditionTest.java b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/TmfXmlConditionTest.java
new file mode 100644 (file)
index 0000000..33b1676
--- /dev/null
@@ -0,0 +1,93 @@
+/*******************************************************************************
+ * Copyright (c) 2015 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:
+ *   Jean-Christian Kouame - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.tracecompass.tmf.analysis.xml.core.tests.stateprovider;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import java.util.List;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
+import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
+import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
+import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
+import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
+import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.XmlStateSystemModule;
+import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles;
+import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.module.XmlUtilsTest;
+import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+import org.junit.Test;
+
+/**
+ * Test suite for the XML conditions
+ *
+ */
+public class TmfXmlConditionTest {
+
+    private static final @NonNull String testTrace2 = "test_traces/testTrace2.xml";
+
+    /**
+     *
+     */
+    @Test
+    public void testConditionsValidation() {
+        ITmfTrace trace = XmlUtilsTest.initializeTrace(testTrace2);
+        XmlStateSystemModule module = XmlUtilsTest.initializeModule(TmfXmlTestFiles.CONDITION_FILE);
+        try {
+            module.setTrace(trace);
+
+            module.schedule();
+            module.waitForCompletion();
+
+            ITmfStateSystem ss = module.getStateSystem();
+            assertNotNull(ss);
+
+            List<Integer> quarks = ss.getQuarks("*");
+            assertEquals(3, quarks.size());
+
+            for (Integer quark : quarks) {
+                String name = ss.getAttributeName(quark);
+                switch (name) {
+                case "test": {
+                    final int[] expectedStarts = { 1, 5, 7 };
+                    ITmfStateValue[] expectedValues = { TmfStateValue.newValueLong(1), TmfStateValue.newValueLong(0) };
+                    XmlUtilsTest.verifyStateIntervals("test", ss, quark, expectedStarts, expectedValues);
+                }
+                    break;
+                case "test1": {
+                    final int[] expectedStarts = { 1, 3, 7, 7 };
+                    ITmfStateValue[] expectedValues = { TmfStateValue.nullValue(), TmfStateValue.newValueLong(0), TmfStateValue.newValueLong(1) };
+                    XmlUtilsTest.verifyStateIntervals("test1", ss, quark, expectedStarts, expectedValues);
+                }
+                    break;
+                case "checkpoint": {
+                    final int[] expectedStarts = { 1, 5, 7, 7 };
+                    ITmfStateValue[] expectedValues = { TmfStateValue.newValueLong(0), TmfStateValue.newValueLong(1), TmfStateValue.newValueLong(0) };
+                    XmlUtilsTest.verifyStateIntervals("checkpoint", ss, quark, expectedStarts, expectedValues);
+                }
+                    break;
+                default:
+                    fail("Wrong attribute name " + name);
+                    break;
+                }
+            }
+        } catch (TmfAnalysisException | AttributeNotFoundException | StateSystemDisposedException e) {
+            fail(e.getMessage());
+        } finally {
+            module.dispose();
+            trace.dispose();
+        }
+    }
+}
diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_traces/testTrace2.xml b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_traces/testTrace2.xml
new file mode 100644 (file)
index 0000000..7d31e3b
--- /dev/null
@@ -0,0 +1,25 @@
+<!-- ***************************************************************************
+* Copyright (c) 2015 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:
+*   Jean-Christian Kouame - Initial API and implementation
+*************************************************************************** -->
+<trace>
+<event timestamp="1" name="test">
+<field name="testField" type="long" value="10" />
+</event>
+<event timestamp="3" name="test1">
+<field name="testField" type="long" value="100" />
+</event>
+<event timestamp="5" name="test">
+<field name="testField" type="long" value="20" />
+</event>
+<event timestamp="7" name="test1">
+<field name="testField" type="long" value="200" />
+</event>
+</trace>
\ No newline at end of file
diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_xml_files/test_valid/test_conditions.xml b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_xml_files/test_valid/test_conditions.xml
new file mode 100644 (file)
index 0000000..3f5a6f7
--- /dev/null
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- ***************************************************************************
+* Copyright (c) 2015 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:
+*   Jean-Christian Kouame - Initial API and implementation
+*************************************************************************** -->
+<tmfxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:noNamespaceSchemaLocation="xmlDefinition.xsd">
+
+       <stateProvider id="test.xml.conditions" version="1">
+               <eventHandler eventName="test">
+                       <stateChange>
+                               <if>
+                                       <condition>
+                                               <field name="testField" />
+                                               <stateValue type="long" value="10" />
+                                       </condition>
+                               </if>
+                               <then>
+                                       <stateAttribute type="eventName" />
+                                       <stateValue type="long" value="1" />
+                               </then>
+                               <else>
+                                       <stateAttribute type="eventName" />
+                                       <stateValue type="long" value="0" />
+                               </else>
+                       </stateChange>
+               </eventHandler>
+               <eventHandler eventName="test1">
+                       <stateChange>
+                               <if>
+                                       <condition>
+                                               <field name="testField" />
+                                               <stateValue type="long" value="200" />
+                                       </condition>
+                               </if>
+                               <then>
+                                       <stateAttribute type="eventName" />
+                                       <stateValue type="long" value="1" />
+                               </then>
+                               <else>
+                                       <stateAttribute type="eventName" />
+                                       <stateValue type="long" value="0" />
+                               </else>
+                       </stateChange>
+               </eventHandler>
+               <eventHandler eventName="*">
+                       <stateChange>
+                               <if>
+                                       <condition>
+                                               <stateValue type="query">
+                                                       <stateAttribute type="constant" value="test" />
+                                               </stateValue>
+                                               <stateValue type="query">
+                                                       <stateAttribute type="constant" value="test1" />
+                                               </stateValue>
+                                       </condition>
+                               </if>
+                               <then>
+                                       <stateAttribute type="constant" value="checkpoint" />
+                                       <stateValue type="long" value="1" />
+                               </then>
+                               <else>
+                                       <stateAttribute type="constant" value="checkpoint" />
+                                       <stateValue type="long" value="0" />
+                               </else>
+                       </stateChange>
+               </eventHandler>
+       </stateProvider>
+</tmfxml>
\ No newline at end of file
This page took 0.031506 seconds and 5 git commands to generate.