1 /*******************************************************************************
2 * Copyright (c) 2014, 2016 École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.statesystem
.core
.tests
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
17 import static org
.junit
.Assert
.assertNull
;
18 import static org
.junit
.Assert
.fail
;
20 import org
.eclipse
.jdt
.annotation
.NonNull
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemFactory
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.IStateHistoryBackend
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.StateHistoryBackendFactory
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
32 import org
.junit
.After
;
33 import org
.junit
.Before
;
34 import org
.junit
.Test
;
37 * Test the {@link StateSystemUtils} class
39 * @author Geneviève Bastien
41 public class StateSystemUtilsTest
{
43 private static final long START_TIME
= 1000L;
44 private static final @NonNull String DUMMY_STRING
= "test";
46 private ITmfStateSystemBuilder fStateSystem
;
49 * Build a small test state system in memory
52 public void setupStateSystem() {
54 IStateHistoryBackend backend
= StateHistoryBackendFactory
.createInMemoryBackend(DUMMY_STRING
, START_TIME
);
55 fStateSystem
= StateSystemFactory
.newStateSystem(backend
);
56 int quark
= fStateSystem
.getQuarkAbsoluteAndAdd(DUMMY_STRING
);
58 fStateSystem
.modifyAttribute(1200L, TmfStateValue
.newValueInt(10), quark
);
59 fStateSystem
.modifyAttribute(1500L, TmfStateValue
.newValueInt(20), quark
);
60 fStateSystem
.closeHistory(2000L);
61 } catch (StateValueTypeException e
) {
70 public void tearDown() {
71 fStateSystem
.dispose();
75 * Test the {@link StateSystemUtils#queryUntilNonNullValue} method.
78 public void testQueryUntilNonNullValue() {
79 ITmfStateSystem ss
= fStateSystem
;
84 quark
= ss
.getQuarkAbsolute(DUMMY_STRING
);
86 /* Should return null if requested range is not within range */
87 assertNull(StateSystemUtils
.queryUntilNonNullValue(ss
, quark
, 0, 999L));
88 assertNull(StateSystemUtils
.queryUntilNonNullValue(ss
, quark
, 2001L, 5000L));
91 * Should return null if request within range, but condition is
94 assertNull(StateSystemUtils
.queryUntilNonNullValue(ss
, quark
, 1000L, 1199L));
97 * Should return the right interval if an interval is within range,
98 * even if the range starts or ends outside state system range
100 ITmfStateInterval interval
= StateSystemUtils
.queryUntilNonNullValue(ss
, quark
, 1000L, 1300L);
101 assertNotNull(interval
);
102 assertEquals(ITmfStateValue
.Type
.INTEGER
, interval
.getStateValue().getType());
103 assertEquals(10, interval
.getStateValue().unboxInt());
105 interval
= StateSystemUtils
.queryUntilNonNullValue(ss
, quark
, 800L, 2500L);
106 assertNotNull(interval
);
107 assertEquals(ITmfStateValue
.Type
.INTEGER
, interval
.getStateValue().getType());
108 assertEquals(10, interval
.getStateValue().unboxInt());
110 interval
= StateSystemUtils
.queryUntilNonNullValue(ss
, quark
, 1300L, 1800L);
111 assertNotNull(interval
);
112 assertEquals(ITmfStateValue
.Type
.INTEGER
, interval
.getStateValue().getType());
113 assertEquals(10, interval
.getStateValue().unboxInt());
115 interval
= StateSystemUtils
.queryUntilNonNullValue(ss
, quark
, 1500L, 1800L);
116 assertNotNull(interval
);
117 assertEquals(ITmfStateValue
.Type
.INTEGER
, interval
.getStateValue().getType());
118 assertEquals(20, interval
.getStateValue().unboxInt());
120 interval
= StateSystemUtils
.queryUntilNonNullValue(ss
, quark
, 1800L, 2500L);
121 assertNotNull(interval
);
122 assertEquals(ITmfStateValue
.Type
.INTEGER
, interval
.getStateValue().getType());
123 assertEquals(20, interval
.getStateValue().unboxInt());
125 } catch (AttributeNotFoundException e
) {
126 fail(e
.getMessage());