1 /*******************************************************************************
2 * Copyright (c) 2012 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 ******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.statesystem
;
16 import java
.io
.IOException
;
17 import java
.util
.List
;
19 import junit
.framework
.TestCase
;
21 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.IStateHistoryBackend
;
22 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.StateSystem
;
23 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.historytree
.HistoryTreeBackend
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateSystemBuilder
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.ITmfStateValue
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.TmfStateValue
;
33 * Unit tests for stack-attributes in the Generic State System (using
34 * pushAttribute() and popAttribute())
36 * @author Alexandre Montplaisir
38 public class StateSystemPushPopTest
extends TestCase
{
40 private IStateSystemBuilder ss
;
42 private ITmfStateInterval interval
;
43 private int attribute
;
45 private final File testHtFile
;
47 private final static String errMsg
= "Caught exception: "; //$NON-NLS-1$
49 /* State values that will be used */
50 //private final static ITmfStateValue nullValue = TmfStateValue.nullValue();
51 private final static ITmfStateValue value1
= TmfStateValue
.newValueString("A"); //$NON-NLS-1$
52 private final static ITmfStateValue value2
= TmfStateValue
.newValueInt(10);
53 private final static ITmfStateValue value3
= TmfStateValue
.nullValue();
54 private final static ITmfStateValue value4
= TmfStateValue
.newValueString("D"); //$NON-NLS-1$
57 * Test case constructor
62 * If we couldn't create the state history test file
64 public StateSystemPushPopTest(final String name
) throws IOException
{
66 testHtFile
= File
.createTempFile("test", ".ht"); //$NON-NLS-1$ //$NON-NLS-2$
67 testHtFile
.deleteOnExit();
71 * Initialization. We run the checks for the return values of
72 * .popAttribute() in here, since this is only available when we are
73 * building the state history.
76 * If we can write the file to the temporary directory.
77 * @throws TimeRangeException
79 * @throws AttributeNotFoundException
81 * @throws StateValueTypeException
85 public void setUp() throws IOException
, TimeRangeException
,
86 AttributeNotFoundException
, StateValueTypeException
{
89 IStateHistoryBackend backend
= new HistoryTreeBackend(testHtFile
, 0);
90 ss
= new StateSystem(backend
, true);
93 final int attrib
= ss
.getQuarkAbsoluteAndAdd("Test", "stack"); //$NON-NLS-1$ //$NON-NLS-2$
95 ss
.pushAttribute( 2, value1
, attrib
);
96 ss
.pushAttribute( 4, value2
, attrib
);
97 ss
.pushAttribute( 6, value3
, attrib
);
98 ss
.pushAttribute(10, value4
, attrib
);
100 value
= ss
.popAttribute(12, attrib
);
101 assertEquals(value4
, value
);
103 value
= ss
.popAttribute(14, attrib
);
104 assertEquals(value3
, value
);
106 value
= ss
.popAttribute(16, attrib
);
107 assertEquals(value2
, value
);
109 value
= ss
.popAttribute(17, attrib
);
110 assertEquals(value1
, value
);
112 value
= ss
.popAttribute(20, attrib
);
113 assertEquals(null, value
); // Stack should already be empty here.
115 ss
.pushAttribute(21, value1
, attrib
);
116 //ss.pushAttribute(22, value1, attrib); //FIXME pushing twice the same value bugs out atm
117 ss
.pushAttribute(22, value2
, attrib
);
119 value
= ss
.popAttribute(24, attrib
);
120 //assertEquals(value1, value);
121 assertEquals(value2
, value
);
123 value
= ss
.popAttribute(26, attrib
);
124 assertEquals(value1
, value
);
126 value
= ss
.popAttribute(28, attrib
);
127 assertEquals(null, value
); // Stack should already be empty here.
130 attribute
= ss
.getQuarkAbsolute("Test", "stack"); //$NON-NLS-1$ //$NON-NLS-2$
134 * Clean-up after running a test. Delete the .ht file we created.
137 public void tearDown() {
142 * Test that the value of the stack-attribute at the start and end of the
143 * 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 e
) {
158 fail(errMsg
+ e
.toString());
159 } catch (TimeRangeException e
) {
160 fail(errMsg
+ e
.toString());
165 * Run single queries on the attribute stacks (with .querySingleState()).
167 public void testSingleQueries() {
169 final int subAttribute1
= ss
.getQuarkRelative(attribute
, "1"); //$NON-NLS-1$
170 final int subAttribute2
= ss
.getQuarkRelative(attribute
, "2"); //$NON-NLS-1$
172 /* Test the stack attributes themselves */
173 interval
= ss
.querySingleState(11, attribute
);
174 assertEquals(4, interval
.getStateValue().unboxInt());
176 interval
= ss
.querySingleState(24, attribute
);
177 assertEquals(1, interval
.getStateValue().unboxInt());
179 /* Go retrieve the user values manually */
180 interval
= ss
.querySingleState(10, subAttribute1
);
181 assertEquals(value1
, interval
.getStateValue()); //
183 interval
= ss
.querySingleState(22, subAttribute2
);
184 assertEquals(value2
, interval
.getStateValue());
186 interval
= ss
.querySingleState(25, subAttribute2
);
187 assertTrue(interval
.getStateValue().isNull()); // Stack depth is 1 at that point.
189 } catch (AttributeNotFoundException e
) {
190 fail(errMsg
+ e
.toString());
191 } catch (StateValueTypeException e
) {
192 fail(errMsg
+ e
.toString());
193 } catch (TimeRangeException e
) {
194 fail(errMsg
+ e
.toString());
199 * Test the .querySingletStackTop() convenience method.
201 public void testStackTop() {
203 interval
= ss
.querySingleStackTop(10, attribute
);
204 assertEquals(value4
, interval
.getStateValue());
206 interval
= ss
.querySingleStackTop(13, attribute
);
207 assertEquals(value3
, interval
.getStateValue());
209 interval
= ss
.querySingleStackTop(16, attribute
);
210 assertEquals(value1
, interval
.getStateValue());
212 interval
= ss
.querySingleStackTop(25, attribute
);
213 assertEquals(value1
, interval
.getStateValue());
215 } catch (AttributeNotFoundException e
) {
216 fail(errMsg
+ e
.toString());
217 } catch (StateValueTypeException e
) {
218 fail(errMsg
+ e
.toString());
219 } catch (TimeRangeException e
) {
220 fail(errMsg
+ e
.toString());
225 * Test the places where the stack is empty.
227 public void testEmptyStack() {
230 interval
= ss
.querySingleState(1, attribute
);
231 assertTrue(interval
.getStateValue().isNull());
232 interval
= ss
.querySingleStackTop(1, attribute
);
233 assertEquals(null, interval
);
235 /* Between the two "stacks" in the state history */
236 interval
= ss
.querySingleState(19, attribute
);
237 assertTrue(interval
.getStateValue().isNull());
238 interval
= ss
.querySingleStackTop(19, attribute
);
239 assertEquals(null, interval
);
242 interval
= ss
.querySingleState(27, attribute
);
243 assertTrue(interval
.getStateValue().isNull());
244 interval
= ss
.querySingleStackTop(27, attribute
);
245 assertEquals(null, interval
);
247 } catch (AttributeNotFoundException e
) {
248 fail(errMsg
+ e
.toString());
249 } catch (StateValueTypeException e
) {
250 fail(errMsg
+ e
.toString());
251 } catch (TimeRangeException e
) {
252 fail(errMsg
+ e
.toString());
257 * Test full-queries (.queryFullState()) on the attribute stacks.
259 public void testFullQueries() {
260 List
<ITmfStateInterval
> state
;
262 final int subAttrib1
= ss
.getQuarkRelative(attribute
, "1"); //$NON-NLS-1$
263 final int subAttrib2
= ss
.getQuarkRelative(attribute
, "2"); //$NON-NLS-1$
264 final int subAttrib3
= ss
.getQuarkRelative(attribute
, "3"); //$NON-NLS-1$
265 final int subAttrib4
= ss
.getQuarkRelative(attribute
, "4"); //$NON-NLS-1$
267 /* Stack depth = 4 */
268 state
= ss
.queryFullState(10);
269 assertEquals(4, state
.get(attribute
).getStateValue().unboxInt());
270 assertEquals(value1
, state
.get(subAttrib1
).getStateValue());
271 assertEquals(value2
, state
.get(subAttrib2
).getStateValue());
272 assertEquals(value3
, state
.get(subAttrib3
).getStateValue());
273 assertEquals(value4
, state
.get(subAttrib4
).getStateValue());
276 state
= ss
.queryFullState(18);
277 assertTrue(state
.get(attribute
).getStateValue().isNull());
278 assertTrue(state
.get(subAttrib1
).getStateValue().isNull());
279 assertTrue(state
.get(subAttrib2
).getStateValue().isNull());
280 assertTrue(state
.get(subAttrib3
).getStateValue().isNull());
281 assertTrue(state
.get(subAttrib4
).getStateValue().isNull());
283 /* Stack depth = 1 */
284 state
= ss
.queryFullState(21);
285 assertEquals(1, state
.get(attribute
).getStateValue().unboxInt());
286 assertEquals(value1
, state
.get(subAttrib1
).getStateValue());
287 assertTrue(state
.get(subAttrib2
).getStateValue().isNull());
288 assertTrue(state
.get(subAttrib3
).getStateValue().isNull());
289 assertTrue(state
.get(subAttrib4
).getStateValue().isNull());
291 } catch (AttributeNotFoundException e
) {
292 fail(errMsg
+ e
.toString());
293 } catch (StateValueTypeException e
) {
294 fail(errMsg
+ e
.toString());
295 } catch (TimeRangeException e
) {
296 fail(errMsg
+ e
.toString());