ss: Fix some tests's copyright header
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core.tests / src / org / eclipse / tracecompass / statesystem / core / tests / backend / StateHistoryBackendTestBase.java
index 83c60c0807d4f20ca34e116870ef63c6f6e7bf0b..e8ec0d8c4f7a7f938e10869a74139ab28f395f42 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2016 Ericsson, EfficiOS Inc. and others
+ * Copyright (c) 2016 Ericsson 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
@@ -10,6 +10,7 @@
 package org.eclipse.tracecompass.statesystem.core.tests.backend;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -19,20 +20,36 @@ import java.util.List;
 
 import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
+import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
+import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
 import org.eclipse.tracecompass.statesystem.core.interval.TmfStateInterval;
+import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
+
 /**
- * Abstract class to test implementations of the {@link IStateHistoryBackend} interface.
+ * Abstract class to test implementations of the {@link IStateHistoryBackend}
+ * interface.
  *
  * @author Patrick Tasse
- * @author Alexandre Montplaisir
+ * @author Geneviève Bastien
  */
 public abstract class StateHistoryBackendTestBase {
 
+    /* Some state values of each type */
+    private static final ITmfStateValue INT_VAL1 = TmfStateValue.newValueInt(-42);
+    private static final ITmfStateValue INT_VAL2 = TmfStateValue.newValueInt(675893);
+    private static final ITmfStateValue LONG_VAL1 = TmfStateValue.newValueLong(-78L);
+    private static final ITmfStateValue LONG_VAL2 = TmfStateValue.newValueLong(2234L);
+    private static final ITmfStateValue DOUBLE_VAL1 = TmfStateValue.newValueDouble(-9.87);
+    private static final ITmfStateValue DOUBLE_VAL2 = TmfStateValue.newValueDouble(50324.131643);
+    private static final ITmfStateValue STR_VAL1 = TmfStateValue.newValueString("A string");
+    private static final ITmfStateValue STR_VAL2 = TmfStateValue.newValueString("Another éèstr");
+
     /**
      * Gets the backend to be used for building.
      *
@@ -75,7 +92,7 @@ public abstract class StateHistoryBackendTestBase {
      *            The intervals to insert in the history backend
      * @return The backend to be used for querying.
      */
-    protected final IStateHistoryBackend prepareBackend(long startTime, long endTime,
+    protected final @Nullable IStateHistoryBackend prepareBackend(long startTime, long endTime,
             List<ITmfStateInterval> intervals) {
 
         try {
@@ -103,6 +120,22 @@ public abstract class StateHistoryBackendTestBase {
         }
     }
 
+    /**
+     * Initializes a list for the number of attributes in the backend and
+     * associates a null value for each
+     *
+     * @param nbAttrib
+     *            The number of attributes in the backend
+     * @return A list of null values for each attribute
+     */
+    private static List<@Nullable ITmfStateInterval> prepareIntervalList(int nbAttrib) {
+        List<@Nullable ITmfStateInterval> intervals = new ArrayList<>(nbAttrib);
+        for (int i = 0; i < nbAttrib; i++) {
+            intervals.add(null);
+        }
+        return intervals;
+    }
+
     /**
      * Test the integrity of a backend by first building the backend with the
      * specified intervals, closing it, and then querying at every single
@@ -128,6 +161,7 @@ public abstract class StateHistoryBackendTestBase {
     protected final IStateHistoryBackend buildAndQueryFullRange(long startTime, long endTime, int nbAttr, List<ITmfStateInterval> intervals, boolean allowNull) {
 
         final IStateHistoryBackend backend = prepareBackend(startTime, endTime, intervals);
+        assertNotNull(backend);
 
         try {
             /*
@@ -135,10 +169,7 @@ public abstract class StateHistoryBackendTestBase {
              * intervals are returned.
              */
             for (long t = backend.getStartTime(); t <= backend.getEndTime(); t++) {
-                List<@Nullable ITmfStateInterval> stateInfo = new ArrayList<>(nbAttr);
-                for (int i = 0; i < nbAttr; i++) {
-                    stateInfo.add(null);
-                }
+                List<@Nullable ITmfStateInterval> stateInfo = prepareIntervalList(nbAttr);
                 backend.doQuery(stateInfo, t);
                 for (int attr = 0; attr < stateInfo.size(); attr++) {
                     ITmfStateInterval interval = stateInfo.get(attr);
@@ -155,7 +186,6 @@ public abstract class StateHistoryBackendTestBase {
             assertEquals(endTime, backend.getEndTime());
         } catch (StateSystemDisposedException e) {
             fail(e.getMessage());
-            return null;
         }
         return backend;
     }
@@ -226,4 +256,159 @@ public abstract class StateHistoryBackendTestBase {
 
         buildAndQueryFullRange(startTime, endTime, nbAttr, intervals, false);
     }
+
+    /**
+     * Test inserting values of different types and querying them right after
+     */
+    @Test
+    public void testInsertQueryStateValues() {
+        /* Test specific data initialization */
+        long startTime = 10;
+        long timeStep = 5;
+        int intQuark = 0;
+        int longQuark = 1;
+        int doubleQuark = 2;
+        int strQuark = 3;
+
+        try {
+            IStateHistoryBackend backend = getBackendForBuilding(startTime);
+            assertNotNull(backend);
+
+            /* Int interval */
+            backend.insertPastState(startTime, startTime + timeStep, intQuark, INT_VAL1);
+            ITmfStateInterval interval = backend.doSingularQuery(startTime, intQuark);
+
+            assertEquals("Int interval start time", startTime, interval.getStartTime());
+            assertEquals("Int interval end time", startTime + timeStep, interval.getEndTime());
+            assertEquals("Int interval value", INT_VAL1, interval.getStateValue());
+
+            /* Long interval */
+            backend.insertPastState(startTime, startTime + timeStep, longQuark, LONG_VAL1);
+            interval = backend.doSingularQuery(startTime, longQuark);
+
+            assertEquals("Long interval start time", startTime, interval.getStartTime());
+            assertEquals("Long interval end time", startTime + timeStep, interval.getEndTime());
+            assertEquals("Long interval value", LONG_VAL1, interval.getStateValue());
+
+            /* Double interval */
+            backend.insertPastState(startTime, startTime + timeStep, doubleQuark, DOUBLE_VAL1);
+            interval = backend.doSingularQuery(startTime, doubleQuark);
+
+            assertEquals("Double interval start time", startTime, interval.getStartTime());
+            assertEquals("Double interval end time", startTime + timeStep, interval.getEndTime());
+            assertEquals("Double interval value", DOUBLE_VAL1, interval.getStateValue());
+
+            /* String interval */
+            backend.insertPastState(startTime, startTime + timeStep, strQuark, STR_VAL1);
+            interval = backend.doSingularQuery(startTime, strQuark);
+
+            assertEquals("String interval start time", startTime, interval.getStartTime());
+            assertEquals("String interval end time", startTime + timeStep, interval.getEndTime());
+            assertEquals("String interval value", STR_VAL1, interval.getStateValue());
+
+            /*
+             * Add other intervals for the int quark and query at different
+             * times
+             */
+            backend.insertPastState(startTime + timeStep + 1, startTime + (2 * timeStep), intQuark, INT_VAL2);
+            backend.insertPastState(startTime + (2 * timeStep) + 1, startTime + (3 * timeStep), intQuark, INT_VAL1);
+
+            interval = backend.doSingularQuery(startTime + timeStep, intQuark);
+            assertEquals("Int interval value", INT_VAL1, interval.getStateValue());
+
+            interval = backend.doSingularQuery(startTime + timeStep + 1, intQuark);
+            assertEquals("Int interval value", INT_VAL2, interval.getStateValue());
+
+            interval = backend.doSingularQuery(startTime + (2 * timeStep), intQuark);
+            assertEquals("Int interval value", INT_VAL2, interval.getStateValue());
+
+            interval = backend.doSingularQuery(startTime + (2 * timeStep) + 1, intQuark);
+            assertEquals("Int interval value", INT_VAL1, interval.getStateValue());
+
+        } catch (TimeRangeException | StateSystemDisposedException | IOException | AttributeNotFoundException e) {
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Test querying various state value types after the state system has been
+     * built and finished
+     */
+    @Test
+    public void testBuildNowQueryLaterStateValues() {
+        /* Test specific data initialization */
+        long startTime = 10;
+        long timeStep = 5;
+        int intQuark = 0;
+        int longQuark = 1;
+        int doubleQuark = 2;
+        int strQuark = 3;
+        int nbAttribs = 4;
+
+        try {
+            IStateHistoryBackend backend = getBackendForBuilding(startTime);
+            assertNotNull(backend);
+
+            long firstEnd = startTime + timeStep;
+            long nextStart = firstEnd + 1;
+            long endTime = nextStart + timeStep;
+
+            insertIntervals(backend, ImmutableList.of(new TmfStateInterval(startTime, startTime + timeStep, intQuark, INT_VAL1),
+                    new TmfStateInterval(startTime, startTime + timeStep, longQuark, LONG_VAL1),
+                    new TmfStateInterval(startTime, startTime + timeStep, doubleQuark, DOUBLE_VAL1),
+                    new TmfStateInterval(startTime, startTime + timeStep, strQuark, STR_VAL1),
+                    new TmfStateInterval(nextStart, endTime, intQuark, INT_VAL2),
+                    new TmfStateInterval(nextStart, endTime, longQuark, LONG_VAL2),
+                    new TmfStateInterval(nextStart, endTime, doubleQuark, DOUBLE_VAL2),
+                    new TmfStateInterval(nextStart, endTime, strQuark, STR_VAL2)));
+
+            backend.finishedBuilding(endTime);
+
+            /* Make sure the end time corresponds to the backend end time */
+            assertEquals(endTime, backend.getEndTime());
+
+            IStateHistoryBackend backendQuery = getBackendForQuerying(backend);
+
+            /* Verify start and end times */
+            assertEquals("Backend start time", startTime, backendQuery.getStartTime());
+            assertEquals("Backend end time", endTime, backendQuery.getEndTime());
+
+            List<@Nullable ITmfStateInterval> intervals = prepareIntervalList(nbAttribs);
+
+            /* Do a full query at start and verify the values */
+            backendQuery.doQuery(intervals, startTime);
+
+            ITmfStateInterval interval = intervals.get(intQuark);
+            assertNotNull(interval);
+            assertEquals("Int value after read", INT_VAL1, interval.getStateValue());
+            interval = intervals.get(longQuark);
+            assertNotNull(interval);
+            assertEquals("Long value after read", LONG_VAL1, interval.getStateValue());
+            interval = intervals.get(doubleQuark);
+            assertNotNull(interval);
+            assertEquals("Double value after read", DOUBLE_VAL1, interval.getStateValue());
+            interval = intervals.get(strQuark);
+            assertNotNull(interval);
+            assertEquals("String value after read", STR_VAL1, interval.getStateValue());
+
+            /* Do a full query at the end and verify the values */
+            backendQuery.doQuery(intervals, endTime);
+
+            interval = intervals.get(intQuark);
+            assertNotNull(interval);
+            assertEquals("Int value after read", INT_VAL2, interval.getStateValue());
+            interval = intervals.get(longQuark);
+            assertNotNull(interval);
+            assertEquals("Long value after read", LONG_VAL2, interval.getStateValue());
+            interval = intervals.get(doubleQuark);
+            assertNotNull(interval);
+            assertEquals("Double value after read", DOUBLE_VAL2, interval.getStateValue());
+            interval = intervals.get(strQuark);
+            assertNotNull(interval);
+            assertEquals("String value after read", STR_VAL2, interval.getStateValue());
+
+        } catch (TimeRangeException | IOException | StateSystemDisposedException e) {
+            fail(e.getMessage());
+        }
+    }
 }
This page took 0.027748 seconds and 5 git commands to generate.