1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 Ericsson
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 * Matthew Khouzam - Initial API and implementation
11 ******************************************************************************/
13 package org
.eclipse
.tracecompass
.statesystem
.core
.tests
.backend
;
15 import static org
.junit
.Assert
.assertArrayEquals
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertNotNull
;
18 import static org
.junit
.Assert
.assertNull
;
19 import static org
.junit
.Assert
.fail
;
21 import java
.util
.ArrayList
;
22 import java
.util
.List
;
24 import org
.eclipse
.jdt
.annotation
.NonNull
;
25 import org
.eclipse
.jdt
.annotation
.Nullable
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.IStateHistoryBackend
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.StateHistoryBackendFactory
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.TmfStateInterval
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
35 import org
.junit
.BeforeClass
;
36 import org
.junit
.Test
;
39 * Test cases for the in-memory backend
41 * @author Matthew Khouzam
43 public class InMemoryBackendTest
extends StateHistoryBackendTestBase
{
45 private static final @NonNull String SSID
= "test-ss";
46 private static final int NUMBER_OF_ATTRIBUTES
= 10;
47 private static IStateHistoryBackend fixture
;
50 * Test setup. make a state system that is moderately large
53 public static void init() {
54 fixture
= StateHistoryBackendFactory
.createInMemoryBackend(SSID
, 0);
55 for (int attribute
= 0; attribute
< NUMBER_OF_ATTRIBUTES
; attribute
++) {
56 for (int timeStart
= 0; timeStart
< 1000; timeStart
++) {
58 final int stateEndTime
= (timeStart
* 100) + 90 + attribute
;
59 final int stateStartTime
= timeStart
* 100 + attribute
;
60 fixture
.insertPastState(stateStartTime
, stateEndTime
, attribute
, TmfStateValue
.newValueInt(timeStart
% 100));
61 if (timeStart
!= 999) {
62 fixture
.insertPastState(stateEndTime
+ 1, stateEndTime
+ 9, attribute
, TmfStateValue
.nullValue());
64 } catch (TimeRangeException e
) {
65 /* Should not happen here */
66 throw new IllegalStateException();
72 private static void testInterval(ITmfStateInterval interval
, int startTime
,
73 int endTime
, int value
) {
74 assertNotNull(interval
);
75 assertEquals(startTime
, interval
.getStartTime());
76 assertEquals(endTime
, interval
.getEndTime());
78 assertEquals(value
, interval
.getStateValue().unboxInt());
79 } catch (StateValueTypeException e
) {
85 protected IStateHistoryBackend
getBackendForBuilding(long startTime
) {
86 return StateHistoryBackendFactory
.createInMemoryBackend(SSID
, startTime
);
93 public void testStartTime() {
94 assertEquals(0, fixture
.getStartTime());
101 public void testEndTime() {
102 assertEquals(99999, fixture
.getEndTime());
106 * Query the state system
109 public void testDoQuery() {
110 List
<@Nullable ITmfStateInterval
> interval
= new ArrayList
<>(NUMBER_OF_ATTRIBUTES
);
111 for (int i
= 0; i
< NUMBER_OF_ATTRIBUTES
; i
++) {
115 fixture
.doQuery(interval
, 950);
116 } catch (TimeRangeException
| StateSystemDisposedException e
) {
117 fail(e
.getMessage());
120 assertEquals(NUMBER_OF_ATTRIBUTES
, interval
.size());
121 testInterval(interval
.get(0), 900, 990, 9);
122 testInterval(interval
.get(1), 901, 991, 9);
123 testInterval(interval
.get(2), 902, 992, 9);
124 testInterval(interval
.get(3), 903, 993, 9);
125 testInterval(interval
.get(4), 904, 994, 9);
126 testInterval(interval
.get(5), 905, 995, 9);
127 testInterval(interval
.get(6), 906, 996, 9);
128 testInterval(interval
.get(7), 907, 997, 9);
129 testInterval(interval
.get(8), 908, 998, 9);
130 testInterval(interval
.get(9), 909, 999, 9);
135 * Test single attribute then compare it to a full query
138 public void testQueryAttribute() {
140 ITmfStateInterval interval
[] = new TmfStateInterval
[10];
141 for (int i
= 0; i
< 10; i
++) {
142 interval
[i
] = fixture
.doSingularQuery(950, i
);
145 testInterval(interval
[0], 900, 990, 9);
146 testInterval(interval
[1], 901, 991, 9);
147 testInterval(interval
[2], 902, 992, 9);
148 testInterval(interval
[3], 903, 993, 9);
149 testInterval(interval
[4], 904, 994, 9);
150 testInterval(interval
[5], 905, 995, 9);
151 testInterval(interval
[6], 906, 996, 9);
152 testInterval(interval
[7], 907, 997, 9);
153 testInterval(interval
[8], 908, 998, 9);
154 testInterval(interval
[9], 909, 999, 9);
156 List
<@Nullable ITmfStateInterval
> intervalQuery
= new ArrayList
<>(NUMBER_OF_ATTRIBUTES
);
157 for (int i
= 0; i
< NUMBER_OF_ATTRIBUTES
; i
++) {
158 intervalQuery
.add(null);
161 fixture
.doQuery(intervalQuery
, 950);
162 ITmfStateInterval ref
[] = intervalQuery
.toArray(new ITmfStateInterval
[0]);
163 assertArrayEquals(ref
, interval
);
165 } catch (TimeRangeException
| AttributeNotFoundException
| StateSystemDisposedException e
) {
166 fail(e
.getMessage());
171 * Test single attribute that should not exist
174 public void testQueryAttributeEmpty() {
176 ITmfStateInterval interval
= fixture
.doSingularQuery(999, 0);
177 assertEquals(TmfStateValue
.nullValue(), interval
.getStateValue());
179 } catch (TimeRangeException
| AttributeNotFoundException
| StateSystemDisposedException e
) {
180 fail(e
.getMessage());
185 * Test first element in ss
188 public void testBegin() {
190 ITmfStateInterval interval
= fixture
.doSingularQuery(0, 0);
191 assertEquals(0, interval
.getStartTime());
192 assertEquals(90, interval
.getEndTime());
193 assertEquals(0, interval
.getStateValue().unboxInt());
195 } catch (TimeRangeException
| AttributeNotFoundException
| StateSystemDisposedException e
) {
196 fail(e
.getMessage());
201 * Test last element in ss
204 public void testEnd() {
206 ITmfStateInterval interval
= fixture
.doSingularQuery(99998, 9);
207 testInterval(interval
, 99909, 99999, 99);
209 } catch (TimeRangeException
| AttributeNotFoundException
| StateSystemDisposedException e
) {
210 fail(e
.getMessage());
215 * Test out of range query
217 * @throws TimeRangeException
220 @Test(expected
= TimeRangeException
.class)
221 public void testOutOfRange_1() throws TimeRangeException
{
223 ITmfStateInterval interval
= fixture
.doSingularQuery(-1, 0);
224 assertNull(interval
);
226 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
227 fail(e
.getMessage());
232 * Test out of range query
234 * @throws TimeRangeException
237 @Test(expected
= TimeRangeException
.class)
238 public void testOutOfRange_2() throws TimeRangeException
{
240 ITmfStateInterval interval
= fixture
.doSingularQuery(100000, 0);
241 assertNull(interval
);
243 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
244 fail(e
.getMessage());