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
.*;
17 import java
.util
.ArrayList
;
18 import java
.util
.List
;
20 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.IStateHistoryBackend
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.StateHistoryBackendFactory
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.TmfStateInterval
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
29 import org
.junit
.BeforeClass
;
30 import org
.junit
.Test
;
33 * Test cases for the in-memory backend
35 * @author Matthew Khouzam
37 public class InMemoryBackendTest
{
39 private static final int NUMBER_OF_ATTRIBUTES
= 10;
40 private static IStateHistoryBackend fixture
;
43 * Test setup. make a state system that is moderately large
46 public static void init() {
47 fixture
= StateHistoryBackendFactory
.createInMemoryBackend("test-ss", 0);
48 for (int attribute
= 0; attribute
< NUMBER_OF_ATTRIBUTES
; attribute
++) {
49 for (int timeStart
= 0; timeStart
< 1000; timeStart
++) {
51 final int stateEndTime
= (timeStart
* 100) + 90 + attribute
;
52 final int stateStartTime
= timeStart
* 100 + attribute
;
53 fixture
.insertPastState(stateStartTime
, stateEndTime
, attribute
, TmfStateValue
.newValueInt(timeStart
% 100));
54 if (timeStart
!= 999) {
55 fixture
.insertPastState(stateEndTime
+ 1, stateEndTime
+ 9, attribute
, TmfStateValue
.nullValue());
57 } catch (TimeRangeException e
) {
58 /* Should not happen here */
59 throw new IllegalStateException();
65 private static void testInterval(ITmfStateInterval interval
, int startTime
,
66 int endTime
, int value
) {
67 assertNotNull(interval
);
68 assertEquals(startTime
, interval
.getStartTime());
69 assertEquals(endTime
, interval
.getEndTime());
71 assertEquals(value
, interval
.getStateValue().unboxInt());
72 } catch (StateValueTypeException e
) {
82 public void testStartTime() {
83 assertEquals(0, fixture
.getStartTime());
90 public void testEndTime() {
91 assertEquals(99999, fixture
.getEndTime());
95 * Query the state system
98 public void testDoQuery() {
99 List
<ITmfStateInterval
> interval
= new ArrayList
<>(NUMBER_OF_ATTRIBUTES
);
100 for (int i
= 0; i
< NUMBER_OF_ATTRIBUTES
; i
++) {
104 fixture
.doQuery(interval
, 950);
105 } catch (TimeRangeException
| StateSystemDisposedException e
) {
106 fail(e
.getMessage());
109 assertEquals(NUMBER_OF_ATTRIBUTES
, interval
.size());
110 testInterval(interval
.get(0), 900, 990, 9);
111 testInterval(interval
.get(1), 901, 991, 9);
112 testInterval(interval
.get(2), 902, 992, 9);
113 testInterval(interval
.get(3), 903, 993, 9);
114 testInterval(interval
.get(4), 904, 994, 9);
115 testInterval(interval
.get(5), 905, 995, 9);
116 testInterval(interval
.get(6), 906, 996, 9);
117 testInterval(interval
.get(7), 907, 997, 9);
118 testInterval(interval
.get(8), 908, 998, 9);
119 testInterval(interval
.get(9), 909, 999, 9);
124 * Test single attribute then compare it to a full query
127 public void testQueryAttribute() {
129 ITmfStateInterval interval
[] = new TmfStateInterval
[10];
130 for (int i
= 0; i
< 10; i
++) {
131 interval
[i
] = fixture
.doSingularQuery(950, i
);
134 testInterval(interval
[0], 900, 990, 9);
135 testInterval(interval
[1], 901, 991, 9);
136 testInterval(interval
[2], 902, 992, 9);
137 testInterval(interval
[3], 903, 993, 9);
138 testInterval(interval
[4], 904, 994, 9);
139 testInterval(interval
[5], 905, 995, 9);
140 testInterval(interval
[6], 906, 996, 9);
141 testInterval(interval
[7], 907, 997, 9);
142 testInterval(interval
[8], 908, 998, 9);
143 testInterval(interval
[9], 909, 999, 9);
145 List
<ITmfStateInterval
> intervalQuery
= new ArrayList
<>(NUMBER_OF_ATTRIBUTES
);
146 for (int i
= 0; i
< NUMBER_OF_ATTRIBUTES
; i
++) {
147 intervalQuery
.add(null);
150 fixture
.doQuery(intervalQuery
, 950);
151 ITmfStateInterval ref
[] = intervalQuery
.toArray(new ITmfStateInterval
[0]);
152 assertArrayEquals(ref
, interval
);
154 } catch (TimeRangeException
| AttributeNotFoundException
| StateSystemDisposedException e
) {
155 fail(e
.getMessage());
160 * Test single attribute that should not exist
163 public void testQueryAttributeEmpty() {
165 ITmfStateInterval interval
= fixture
.doSingularQuery(999, 0);
166 assertEquals(TmfStateValue
.nullValue(), interval
.getStateValue());
168 } catch (TimeRangeException
| AttributeNotFoundException
| StateSystemDisposedException e
) {
169 fail(e
.getMessage());
174 * Test first element in ss
177 public void testBegin() {
179 ITmfStateInterval interval
= fixture
.doSingularQuery(0, 0);
180 assertEquals(0, interval
.getStartTime());
181 assertEquals(90, interval
.getEndTime());
182 assertEquals(0, interval
.getStateValue().unboxInt());
184 } catch (TimeRangeException
| AttributeNotFoundException
| StateSystemDisposedException e
) {
185 fail(e
.getMessage());
190 * Test last element in ss
193 public void testEnd() {
195 ITmfStateInterval interval
= fixture
.doSingularQuery(99998, 9);
196 testInterval(interval
, 99909, 99999, 99);
198 } catch (TimeRangeException
| AttributeNotFoundException
| StateSystemDisposedException e
) {
199 fail(e
.getMessage());
204 * Test out of range query
206 * @throws TimeRangeException
209 @Test(expected
= TimeRangeException
.class)
210 public void testOutOfRange_1() throws TimeRangeException
{
212 ITmfStateInterval interval
= fixture
.doSingularQuery(-1, 0);
213 assertNull(interval
);
215 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
216 fail(e
.getMessage());
221 * Test out of range query
223 * @throws TimeRangeException
226 @Test(expected
= TimeRangeException
.class)
227 public void testOutOfRange_2() throws TimeRangeException
{
229 ITmfStateInterval interval
= fixture
.doSingularQuery(100000, 0);
230 assertNull(interval
);
232 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
233 fail(e
.getMessage());