1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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 * Alexandre Montplaisir - Initial API and implementation
11 * Alexandre Montplaisir - Port to JUnit4
12 ******************************************************************************/
14 package org
.eclipse
.linuxtools
.statesystem
.core
.tests
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertTrue
;
18 import static org
.junit
.Assert
.fail
;
21 import java
.io
.IOException
;
22 import java
.util
.List
;
24 import org
.eclipse
.linuxtools
.internal
.statesystem
.core
.StateSystem
;
25 import org
.eclipse
.linuxtools
.statesystem
.core
.ITmfStateSystemBuilder
;
26 import org
.eclipse
.linuxtools
.statesystem
.core
.backend
.IStateHistoryBackend
;
27 import org
.eclipse
.linuxtools
.statesystem
.core
.backend
.historytree
.HistoryTreeBackend
;
28 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
29 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
30 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.StateValueTypeException
;
31 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.TimeRangeException
;
32 import org
.eclipse
.linuxtools
.statesystem
.core
.interval
.ITmfStateInterval
;
33 import org
.eclipse
.linuxtools
.statesystem
.core
.statevalue
.ITmfStateValue
;
34 import org
.eclipse
.linuxtools
.statesystem
.core
.statevalue
.TmfStateValue
;
35 import org
.junit
.After
;
36 import org
.junit
.Before
;
37 import org
.junit
.Test
;
40 * Unit tests for stack-attributes in the Generic State System (using
41 * pushAttribute() and popAttribute())
43 * @author Alexandre Montplaisir
45 public class StateSystemPushPopTest
{
47 private ITmfStateSystemBuilder ss
;
49 private ITmfStateInterval interval
;
50 private int attribute
;
52 private File testHtFile
;
54 private final static String errMsg
= "Caught exception: ";
56 /* State values that will be used */
57 //private final static ITmfStateValue nullValue = TmfStateValue.nullValue();
58 private final static ITmfStateValue value1
= TmfStateValue
.newValueString("A");
59 private final static ITmfStateValue value2
= TmfStateValue
.newValueInt(10);
60 private final static ITmfStateValue value3
= TmfStateValue
.nullValue();
61 private final static ITmfStateValue value4
= TmfStateValue
.newValueString("D");
62 private final static ITmfStateValue value5
= TmfStateValue
.newValueLong(Long
.MAX_VALUE
);
65 * Initialization. We run the checks for the return values of
66 * .popAttribute() in here, since this is only available when we are
67 * building the state history.
70 * If we can write the file to the temporary directory.
71 * @throws TimeRangeException
73 * @throws AttributeNotFoundException
75 * @throws StateValueTypeException
79 public void setUp() throws IOException
, TimeRangeException
,
80 AttributeNotFoundException
, StateValueTypeException
{
82 testHtFile
= File
.createTempFile("test", ".ht");
84 IStateHistoryBackend backend
= new HistoryTreeBackend(testHtFile
, 0, 0L);
85 ss
= new StateSystem("push-pop-test", backend
, true);
88 final int attrib
= ss
.getQuarkAbsoluteAndAdd("Test", "stack");
90 ss
.pushAttribute( 2, value1
, attrib
);
91 ss
.pushAttribute( 4, value2
, attrib
);
92 ss
.pushAttribute( 6, value3
, attrib
);
93 ss
.pushAttribute( 8, value4
, attrib
);
94 ss
.pushAttribute(10, value5
, attrib
);
96 value
= ss
.popAttribute(11, attrib
);
97 assertEquals(value5
, value
);
99 value
= ss
.popAttribute(12, attrib
);
100 assertEquals(value4
, value
);
102 value
= ss
.popAttribute(14, attrib
);
103 assertEquals(value3
, value
);
105 value
= ss
.popAttribute(16, attrib
);
106 assertEquals(value2
, value
);
108 value
= ss
.popAttribute(17, attrib
);
109 assertEquals(value1
, value
);
111 value
= ss
.popAttribute(20, attrib
);
112 assertEquals(null, value
); // Stack should already be empty here.
114 ss
.pushAttribute(21, value1
, attrib
);
115 //ss.pushAttribute(22, value1, attrib); //FIXME pushing twice the same value bugs out atm
116 ss
.pushAttribute(22, value2
, attrib
);
118 value
= ss
.popAttribute(24, attrib
);
119 //assertEquals(value1, value);
120 assertEquals(value2
, value
);
122 value
= ss
.popAttribute(26, attrib
);
123 assertEquals(value1
, value
);
125 value
= ss
.popAttribute(28, attrib
);
126 assertEquals(null, value
); // Stack should already be empty here.
129 attribute
= ss
.getQuarkAbsolute("Test", "stack");
133 * Clean-up after running a test. Delete the .ht file we created.
136 public void tearDown() {
141 * Test that the value of the stack-attribute at the start and end of the
142 * history are correct.
145 public void testBeginEnd() {
147 interval
= ss
.querySingleState(0, attribute
);
148 assertEquals(0, interval
.getStartTime());
149 assertEquals(1, interval
.getEndTime());
150 assertTrue(interval
.getStateValue().isNull());
152 interval
= ss
.querySingleState(29, attribute
);
153 assertEquals(26, interval
.getStartTime());
154 assertEquals(30, interval
.getEndTime());
155 assertTrue(interval
.getStateValue().isNull());
157 } catch (AttributeNotFoundException
| TimeRangeException
| StateSystemDisposedException e
) {
158 fail(errMsg
+ e
.toString());
163 * Run single queries on the attribute stacks (with .querySingleState()).
166 public void testSingleQueries() {
168 final int subAttribute1
= ss
.getQuarkRelative(attribute
, "1");
169 final int subAttribute2
= ss
.getQuarkRelative(attribute
, "2");
171 /* Test the stack attributes themselves */
172 interval
= ss
.querySingleState(11, attribute
);
173 assertEquals(4, interval
.getStateValue().unboxInt());
175 interval
= ss
.querySingleState(24, attribute
);
176 assertEquals(1, interval
.getStateValue().unboxInt());
178 /* Go retrieve the user values manually */
179 interval
= ss
.querySingleState(10, subAttribute1
);
180 assertEquals(value1
, interval
.getStateValue()); //
182 interval
= ss
.querySingleState(22, subAttribute2
);
183 assertEquals(value2
, interval
.getStateValue());
185 interval
= ss
.querySingleState(25, subAttribute2
);
186 assertTrue(interval
.getStateValue().isNull()); // Stack depth is 1 at that point.
188 } catch (AttributeNotFoundException e
) {
189 fail(errMsg
+ e
.toString());
190 } catch (StateValueTypeException e
) {
191 fail(errMsg
+ e
.toString());
192 } catch (TimeRangeException e
) {
193 fail(errMsg
+ e
.toString());
194 } catch (StateSystemDisposedException e
) {
195 fail(errMsg
+ e
.toString());
200 * Test the .querySingletStackTop() convenience method.
203 public void testStackTop() {
205 interval
= ss
.querySingleStackTop(10, attribute
);
206 assertEquals(value5
, interval
.getStateValue());
208 interval
= ss
.querySingleStackTop(9, attribute
);
209 assertEquals(value4
, interval
.getStateValue());
211 interval
= ss
.querySingleStackTop(13, attribute
);
212 assertEquals(value3
, interval
.getStateValue());
214 interval
= ss
.querySingleStackTop(16, attribute
);
215 assertEquals(value1
, interval
.getStateValue());
217 interval
= ss
.querySingleStackTop(25, attribute
);
218 assertEquals(value1
, interval
.getStateValue());
220 } catch (AttributeNotFoundException e
) {
221 fail(errMsg
+ e
.toString());
222 } catch (StateValueTypeException e
) {
223 fail(errMsg
+ e
.toString());
224 } catch (TimeRangeException e
) {
225 fail(errMsg
+ e
.toString());
226 } catch (StateSystemDisposedException e
) {
227 fail(errMsg
+ e
.toString());
232 * Test the places where the stack is empty.
235 public void testEmptyStack() {
238 interval
= ss
.querySingleState(1, attribute
);
239 assertTrue(interval
.getStateValue().isNull());
240 interval
= ss
.querySingleStackTop(1, attribute
);
241 assertEquals(null, interval
);
243 /* Between the two "stacks" in the state history */
244 interval
= ss
.querySingleState(19, attribute
);
245 assertTrue(interval
.getStateValue().isNull());
246 interval
= ss
.querySingleStackTop(19, attribute
);
247 assertEquals(null, interval
);
250 interval
= ss
.querySingleState(27, attribute
);
251 assertTrue(interval
.getStateValue().isNull());
252 interval
= ss
.querySingleStackTop(27, attribute
);
253 assertEquals(null, interval
);
255 } catch (AttributeNotFoundException e
) {
256 fail(errMsg
+ e
.toString());
257 } catch (StateValueTypeException e
) {
258 fail(errMsg
+ e
.toString());
259 } catch (TimeRangeException e
) {
260 fail(errMsg
+ e
.toString());
261 } catch (StateSystemDisposedException e
) {
262 fail(errMsg
+ e
.toString());
267 * Test full-queries (.queryFullState()) on the attribute stacks.
270 public void testFullQueries() {
271 List
<ITmfStateInterval
> state
;
273 final int subAttrib1
= ss
.getQuarkRelative(attribute
, "1");
274 final int subAttrib2
= ss
.getQuarkRelative(attribute
, "2");
275 final int subAttrib3
= ss
.getQuarkRelative(attribute
, "3");
276 final int subAttrib4
= ss
.getQuarkRelative(attribute
, "4");
278 /* Stack depth = 5 */
279 state
= ss
.queryFullState(10);
280 assertEquals(5, state
.get(attribute
).getStateValue().unboxInt());
281 assertEquals(value1
, state
.get(subAttrib1
).getStateValue());
282 assertEquals(value2
, state
.get(subAttrib2
).getStateValue());
283 assertEquals(value3
, state
.get(subAttrib3
).getStateValue());
284 assertEquals(value4
, state
.get(subAttrib4
).getStateValue());
287 state
= ss
.queryFullState(18);
288 assertTrue(state
.get(attribute
).getStateValue().isNull());
289 assertTrue(state
.get(subAttrib1
).getStateValue().isNull());
290 assertTrue(state
.get(subAttrib2
).getStateValue().isNull());
291 assertTrue(state
.get(subAttrib3
).getStateValue().isNull());
292 assertTrue(state
.get(subAttrib4
).getStateValue().isNull());
294 /* Stack depth = 1 */
295 state
= ss
.queryFullState(21);
296 assertEquals(1, state
.get(attribute
).getStateValue().unboxInt());
297 assertEquals(value1
, state
.get(subAttrib1
).getStateValue());
298 assertTrue(state
.get(subAttrib2
).getStateValue().isNull());
299 assertTrue(state
.get(subAttrib3
).getStateValue().isNull());
300 assertTrue(state
.get(subAttrib4
).getStateValue().isNull());
302 } catch (AttributeNotFoundException e
) {
303 fail(errMsg
+ e
.toString());
304 } catch (StateValueTypeException e
) {
305 fail(errMsg
+ e
.toString());
306 } catch (TimeRangeException e
) {
307 fail(errMsg
+ e
.toString());
308 } catch (StateSystemDisposedException e
) {
309 fail(errMsg
+ e
.toString());