statesystem: Add utility methods to help unit test state systems
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Tue, 2 Feb 2016 20:21:42 +0000 (15:21 -0500)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Fri, 12 Feb 2016 22:23:32 +0000 (17:23 -0500)
These methods and classes can be used to easily unit test the intervals
for an attribute for the duration of the tree, or the values of many attributes
at a given timestamp.

Change-Id: I3eb9fc8bdc1d2ba8a388c9e0f979b506a60e3ae1
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/66403
Reviewed-by: Hudson CI
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Reviewed-by: Alexandre Montplaisir <alexmonthy@efficios.com>
statesystem/org.eclipse.tracecompass.statesystem.core.tests/.classpath
statesystem/org.eclipse.tracecompass.statesystem.core.tests/META-INF/MANIFEST.MF
statesystem/org.eclipse.tracecompass.statesystem.core.tests/build.properties
statesystem/org.eclipse.tracecompass.statesystem.core.tests/shared/org/eclipse/tracecompass/statesystem/core/tests/shared/utils/StateIntervalStub.java [new file with mode: 0644]
statesystem/org.eclipse.tracecompass.statesystem.core.tests/shared/org/eclipse/tracecompass/statesystem/core/tests/shared/utils/StateSystemTestUtils.java [new file with mode: 0644]
statesystem/org.eclipse.tracecompass.statesystem.core.tests/shared/org/eclipse/tracecompass/statesystem/core/tests/shared/utils/package-info.java [new file with mode: 0644]

index aed35557fedd7efa43bfe401b6545ee3cc4ac7fc..ee8a85abcd5ec66e38a639ae146a3106929ec167 100644 (file)
@@ -12,5 +12,6 @@
        </classpathentry>
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="src" path="stubs"/>
+       <classpathentry kind="src" path="shared"/>
        <classpathentry kind="output" path="bin"/>
 </classpath>
index 0c4d83dfa1e735374030333b356b9911b2698216..ebbd065addb0799d6e74b9da4b52c505a75427f2 100644 (file)
@@ -12,5 +12,7 @@ Require-Bundle: org.junit;bundle-version="4.0.0",
  org.eclipse.core.resources,
  org.eclipse.tracecompass.common.core,
  org.eclipse.tracecompass.statesystem.core
-Export-Package: org.eclipse.tracecompass.statesystem.core.tests
-Import-Package: com.google.common.collect
+Export-Package: org.eclipse.tracecompass.statesystem.core.tests,
+ org.eclipse.tracecompass.statesystem.core.tests.shared.utils
+Import-Package: com.google.common.base,
+ com.google.common.collect
index 3193c5d94c9c63f65f4d182ac9a063fba6436f99..a1ac2971c82483c240e8ebf416b5a0419ab48ee6 100644 (file)
@@ -11,7 +11,8 @@
 ###############################################################################
 
 source.. = src/,\
-           stubs/
+           stubs/,\
+           shared/
 output.. = bin/
 bin.includes = META-INF/,\
                .,\
diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core.tests/shared/org/eclipse/tracecompass/statesystem/core/tests/shared/utils/StateIntervalStub.java b/statesystem/org.eclipse.tracecompass.statesystem.core.tests/shared/org/eclipse/tracecompass/statesystem/core/tests/shared/utils/StateIntervalStub.java
new file mode 100644 (file)
index 0000000..4e655cf
--- /dev/null
@@ -0,0 +1,76 @@
+/*******************************************************************************
+ * Copyright (c) 2016 É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
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.statesystem.core.tests.shared.utils;
+
+import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
+import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
+
+/**
+ * State interval class to help with unit testing. This class should be used in
+ * unit test to store the expected values of an interval that can then be
+ * compared with an actual interval from the state system. It is not meant to be
+ * inserted in the state system, so the attribute field does not need to be set.
+ *
+ * @author Geneviève Bastien
+ */
+public class StateIntervalStub implements ITmfStateInterval {
+
+    /*
+     * For this kind of interval, the attribute does not matter, so just set a
+     * default value here
+     */
+    private static final int TEST_ATTRIBUTE = 1;
+
+    private final long fStart;
+    private final long fEnd;
+    private final ITmfStateValue fValue;
+
+    /**
+     * Constructor
+     *
+     * @param start
+     *            Start time of the interval
+     * @param end
+     *            End time of the interval
+     * @param value
+     *            Value of the interval
+     */
+    public StateIntervalStub(final int start, final int end, final ITmfStateValue value) {
+        fStart = start;
+        fEnd = end;
+        fValue = value;
+    }
+
+    @Override
+    public long getStartTime() {
+        return fStart;
+    }
+
+    @Override
+    public long getEndTime() {
+        return fEnd;
+    }
+
+    @Override
+    public int getAttribute() {
+        return TEST_ATTRIBUTE;
+    }
+
+    @Override
+    public ITmfStateValue getStateValue() {
+        return fValue;
+    }
+
+    @Override
+    public boolean intersects(long timestamp) {
+        return (fStart >= timestamp && fEnd <= timestamp);
+    }
+
+}
diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core.tests/shared/org/eclipse/tracecompass/statesystem/core/tests/shared/utils/StateSystemTestUtils.java b/statesystem/org.eclipse.tracecompass.statesystem.core.tests/shared/org/eclipse/tracecompass/statesystem/core/tests/shared/utils/StateSystemTestUtils.java
new file mode 100644 (file)
index 0000000..569e68e
--- /dev/null
@@ -0,0 +1,132 @@
+/*******************************************************************************
+ * Copyright (c) 2016 É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
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.statesystem.core.tests.shared.utils;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+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;
+
+/**
+ * Contains utilities to test the content of state systems
+ *
+ * @author Geneviève Bastien
+ */
+public final class StateSystemTestUtils {
+
+    private StateSystemTestUtils() {
+
+    }
+
+    /**
+     * Test that the intervals for a given attribute correspond to what is
+     * expected. If the attribute does not exist or if one of the interval does
+     * not correspond, it will fail the test.
+     *
+     * @param ss
+     *            The state system containing the attribute
+     * @param path
+     *            The path to the attribute
+     * @param expected
+     *            The list of intervals the attribute should have
+     */
+    public static void testIntervalForAttributes(ITmfStateSystem ss, List<ITmfStateInterval> expected, String... path) {
+        try {
+            int quark = ss.getQuarkAbsolute(path);
+            List<ITmfStateInterval> actual = StateSystemUtils.queryHistoryRange(ss, quark, ss.getStartTime(), ss.getCurrentEndTime());
+            /*
+             * This unit test must help debug why a test fail, that is why we do
+             * not test the number of intervals at this point. We make sure each
+             * of the expected interval exists and has the same start and end
+             * times and state value
+             */
+            for (int i = 0; i < expected.size(); i++) {
+                if (i >= actual.size()) {
+                    fail(Arrays.deepToString(path) + ": Missing interval " + i);
+                }
+                ITmfStateInterval act = actual.get(i);
+                ITmfStateInterval exp = expected.get(i);
+                if (!compareIntervalContent(exp, act)) {
+                    fail(Arrays.deepToString(path) + ":Interval at position " + i + ": expected: " + displayInterval(exp) + " actual: " + displayInterval(act));
+                }
+            }
+            /* Make sure there is no extra interval in the actual */
+            assertEquals(Arrays.deepToString(path) + ":Number of intervals", expected.size(), actual.size());
+        } catch (AttributeNotFoundException e) {
+            fail("Attribute " + Arrays.deepToString(path) + " does not exist");
+        } catch (StateSystemDisposedException e) {
+            fail("State system was disposed");
+        }
+    }
+
+    private static boolean compareIntervalContent(ITmfStateInterval expected, ITmfStateInterval actual) {
+        return (expected.getStartTime() == actual.getStartTime()) &&
+                (expected.getEndTime() == actual.getEndTime()) &&
+                (expected.getStateValue().equals(actual.getStateValue()));
+    }
+
+    private static String displayInterval(ITmfStateInterval interval) {
+        return "[" + interval.getStartTime() + "," + interval.getEndTime() + "]:" + interval.getStateValue();
+    }
+
+    /**
+     * Test that the values in the state system at time t correspond to what is
+     * expected
+     *
+     * @param ss
+     *            The state system containing the values
+     * @param t
+     *            The time at which to test the values
+     * @param expected
+     *            A mapping between the full path of an attribute and its
+     *            expected value
+     */
+    public static void testValuesAtTime(ITmfStateSystem ss, long t, Map<String[], ITmfStateValue> expected) {
+        try {
+            List<ITmfStateInterval> intervals = ss.queryFullState(t);
+            for (Entry<String[], ITmfStateValue> entry : expected.entrySet()) {
+                String[] path = entry.getKey();
+                try {
+                    int quark = ss.getQuarkAbsolute(entry.getKey());
+                    ITmfStateInterval interval = intervals.get(quark);
+                    assertEquals(Arrays.deepToString(path) + " at time " + t, entry.getValue(), interval.getStateValue());
+                } catch (AttributeNotFoundException e) {
+                    fail("Attribute " + Arrays.deepToString(path) + " does not exist at time " + t);
+                }
+
+            }
+        } catch (StateSystemDisposedException e) {
+            fail("State system was disposed");
+        }
+    }
+
+    /**
+     * Utility method to return an attribute path as an array using varargs
+     *
+     * @param path
+     *            The path of the attribute
+     * @return The path as an array of String
+     */
+    public static @NonNull String[] makeAttribute(@NonNull String... path) {
+        return path;
+    }
+
+}
diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core.tests/shared/org/eclipse/tracecompass/statesystem/core/tests/shared/utils/package-info.java b/statesystem/org.eclipse.tracecompass.statesystem.core.tests/shared/org/eclipse/tracecompass/statesystem/core/tests/shared/utils/package-info.java
new file mode 100644 (file)
index 0000000..9a9dd6b
--- /dev/null
@@ -0,0 +1,11 @@
+/*******************************************************************************
+ * Copyright (c) 2016 É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
+ *******************************************************************************/
+
+@org.eclipse.jdt.annotation.NonNullByDefault
+package org.eclipse.tracecompass.statesystem.core.tests.shared.utils;
This page took 0.0302 seconds and 5 git commands to generate.