TMF: Add some XML basic test cases and fix a bug
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Wed, 12 Nov 2014 16:13:56 +0000 (11:13 -0500)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Fri, 21 Nov 2014 20:37:53 +0000 (15:37 -0500)
A bug where if the previous value was not existent, it was unboxed at -1 while
it should start at 0.

Change-Id: I58fda5925010b675bcab15cf49b69b303e6e9a9e
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/36344
Tested-by: Hudson CI
Reviewed-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Tested-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
org.eclipse.tracecompass.tmf.analysis.xml.core.tests/META-INF/MANIFEST.MF
org.eclipse.tracecompass.tmf.analysis.xml.core.tests/common/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/common/TmfXmlTestFiles.java
org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/AllTests.java
org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderModelTest.java [new file with mode: 0644]
org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_traces/testTrace1.xml [new file with mode: 0644]
org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_xml_files/test_valid/test_attributes.xml [new file with mode: 0644]
org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/tmf/analysis/xml/core/model/readwrite/TmfXmlReadWriteStateValue.java
org.eclipse.tracecompass.tmf.core.tests/META-INF/MANIFEST.MF

index cac78c5ac4d610aa9ce3dbfc4ed7153fda6287d8..230ec2d6e83c78a3cb66e04146986f80d13bf894 100644 (file)
@@ -12,7 +12,8 @@ Require-Bundle: org.junit,
  org.eclipse.tracecompass.tmf.core,
  org.eclipse.tracecompass.tmf.core.tests,
  org.eclipse.tracecompass.tmf.ctf.core,
- org.eclipse.tracecompass.tmf.ctf.core.tests
+ org.eclipse.tracecompass.tmf.ctf.core.tests,
+ org.eclipse.tracecompass.statesystem.core
 Bundle-RequiredExecutionEnvironment: JavaSE-1.7
 Bundle-ActivationPolicy: lazy
 Import-Package: org.junit.runners
index 60594b2281f8e2bd580e938b669c8ed9012066d8..c5151111cf2e24420a15d21cb6c4e19d391ce1d0 100644 (file)
@@ -36,7 +36,9 @@ public enum TmfXmlTestFiles {
     /** A valid XML test file */
     VALID_FILE("test_xml_files/test_valid/test_valid.xml"),
     /** An invalid test file */
-    INVALID_FILE("test_xml_files/test_invalid/test_invalid.xml");
+    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");
 
     private final String fPath;
 
index 08f8938c99c856b609eb2ad92e42af689bc7c316..d918cc028eceedfbbd02afcddce9e48ca843f2a3 100644 (file)
@@ -21,6 +21,7 @@ import org.junit.runners.Suite;
  */
 @RunWith(Suite.class)
 @Suite.SuiteClasses({
+        StateProviderModelTest.class,
         StateProviderTest.class,
         StateProviderModuleTest.class })
 public class AllTests {
diff --git a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderModelTest.java b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderModelTest.java
new file mode 100644 (file)
index 0000000..85fdb88
--- /dev/null
@@ -0,0 +1,153 @@
+/*******************************************************************************
+ * Copyright (c) 2014 École Polytechnique de Montréal
+ *
+ * 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:
+ *   Geneviève Bastien - 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.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.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
+ *
+ * @author Geneviève Bastien
+ */
+public class StateProviderModelTest {
+
+    private static final @NonNull String testTrace1 = "test_traces/testTrace1.xml";
+    /* Factor to convert seconds to nanoseconds */
+    private static final long TO_NS = 1000000000L;
+
+    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] * TO_NS, expectedStarts[expectedCount] * TO_NS);
+        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] * TO_NS, interval.getStartTime());
+            long actualEnd = (i == expectedCount - 1) ? (expectedStarts[i + 1] * TO_NS) : (expectedStarts[i + 1] * TO_NS) - 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);
+        try {
+
+            module.setTrace(trace);
+
+            module.schedule();
+            module.waitForCompletion();
+
+            ITmfStateSystem ss = module.getStateSystem();
+            assertNotNull(ss);
+
+            List<Integer> quarks = ss.getQuarks("*");
+            assertEquals(2, quarks.size());
+
+            for (Integer quark : quarks) {
+                String name = ss.getAttributeName(quark);
+                switch (name) {
+                case "test":
+                {
+                    final int[] expectedStarts = { 1, 5, 7 };
+                    ITmfStateValue[] expectedValues = { TmfStateValue.newValueInt(1), TmfStateValue.newValueInt(2) };
+                    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);
+                }
+                    break;
+                default:
+                    fail("Wrong attribute name " + name);
+                    break;
+                }
+            }
+
+        } catch (TmfAnalysisException | AttributeNotFoundException | StateSystemDisposedException e) {
+            fail(e.getMessage());
+        } finally {
+            module.dispose();
+            trace.dispose();
+        }
+
+    }
+
+}
diff --git a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_traces/testTrace1.xml b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_traces/testTrace1.xml
new file mode 100644 (file)
index 0000000..6d7bd2c
--- /dev/null
@@ -0,0 +1,21 @@
+<!-- ***************************************************************************
+* Copyright (c) 2014 École Polytechnique de Montréal
+*
+* 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:
+       * Geneviève Bastien - Initial API and implementation
+*************************************************************************** -->
+<trace>
+<event timestamp="1" name="test" source="0">
+</event>
+<event timestamp="3" name="test1" source="0">
+</event>
+<event timestamp="5" name="test" source="0">
+</event>
+<event timestamp="7" name="test1" source="0">
+</event>
+</trace>
\ No newline at end of file
diff --git a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_xml_files/test_valid/test_attributes.xml b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_xml_files/test_valid/test_attributes.xml
new file mode 100644 (file)
index 0000000..3f01dbf
--- /dev/null
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- ***************************************************************************
+* Copyright (c) 2014 École Polytechnique de Montréal
+*
+* 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:
+       * Geneviève Bastien - Initial API and implementation
+*************************************************************************** -->
+<tmfxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:noNamespaceSchemaLocation="xmlDefinition.xsd">
+
+       <stateProvider id="test.xml.attributes" version="1">
+
+               <!-- Test a increment of one on an event name, like for statistics -->
+               <eventHandler eventName="*">
+                       <stateChange>
+                               <stateAttribute type="eventName" />
+                               <stateValue type="int" value="1" increment="true" />
+                       </stateChange>
+               </eventHandler>
+       </stateProvider>
+</tmfxml>
\ No newline at end of file
index 4d7994ad495f2fceb2fcbcd2fddb204fb86a44b8..8bb039f52d410ee6170875bd0ffc1ffb5fb1668f 100644 (file)
@@ -207,14 +207,16 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
             switch (fValue.getType()) {
             case LONG: {
                 long incrementLong = fValue.unboxLong();
-                long currentValue = ss.queryOngoingState(quark).unboxLong();
+                ITmfStateValue currentState = ss.queryOngoingState(quark);
+                long currentValue = (currentState.isNull() ? 0 : currentState.unboxLong());
                 ITmfStateValue value = TmfStateValue.newValueLong(incrementLong + currentValue);
                 processValue(quark, timestamp, value);
                 return;
             }
             case INTEGER: {
                 int increment = fValue.unboxInt();
-                int currentValue = ss.queryOngoingState(quark).unboxInt();
+                ITmfStateValue currentState = ss.queryOngoingState(quark);
+                int currentValue = (currentState.isNull() ? 0 : currentState.unboxInt());
                 ITmfStateValue value = TmfStateValue.newValueInt(increment + currentValue);
                 processValue(quark, timestamp, value);
                 break;
index 71926ab7d304e30e5d3e43c4515b58beb7cee9bc..4b03dbc5aa6d9478e5a0f205ca4df66f6955e172 100644 (file)
@@ -16,5 +16,6 @@ Require-Bundle: org.junit;bundle-version="4.0.0",
 Export-Package: org.eclipse.tracecompass.tmf.core.tests,
  org.eclipse.tracecompass.tmf.core.tests.perf,
  org.eclipse.tracecompass.tmf.core.tests.shared,
- org.eclipse.tracecompass.tmf.tests.stubs.trace
+ org.eclipse.tracecompass.tmf.tests.stubs.trace,
+ org.eclipse.tracecompass.tmf.tests.stubs.trace.xml
 Import-Package: com.google.common.collect
This page took 0.029069 seconds and 5 git commands to generate.