Commit | Line | Data |
---|---|---|
a4e71249 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2012, 2014 Ericsson |
a4e71249 AM |
3 | * |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Alexandre Montplaisir - Initial API and implementation | |
6e1886bc | 11 | * Alexandre Montplaisir - Port to JUnit4 |
a4e71249 AM |
12 | ******************************************************************************/ |
13 | ||
14 | package org.eclipse.linuxtools.tmf.core.tests.statesystem; | |
15 | ||
6e1886bc AM |
16 | import static org.junit.Assert.assertEquals; |
17 | import static org.junit.Assert.assertTrue; | |
18 | import static org.junit.Assert.fail; | |
19 | ||
a4e71249 AM |
20 | import java.io.File; |
21 | import java.io.IOException; | |
22 | import java.util.List; | |
23 | ||
a4e71249 | 24 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.StateSystem; |
f9a76cac AM |
25 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.IStateHistoryBackend; |
26 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.historytree.HistoryTreeBackend; | |
a4e71249 | 27 | import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException; |
96345c5a | 28 | import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException; |
a4e71249 AM |
29 | import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException; |
30 | import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException; | |
31 | import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval; | |
f1f86dfb | 32 | import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystemBuilder; |
a4e71249 AM |
33 | import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue; |
34 | import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue; | |
6e1886bc AM |
35 | import org.junit.After; |
36 | import org.junit.Before; | |
37 | import org.junit.Test; | |
a4e71249 AM |
38 | |
39 | /** | |
40 | * Unit tests for stack-attributes in the Generic State System (using | |
41 | * pushAttribute() and popAttribute()) | |
42 | * | |
43 | * @author Alexandre Montplaisir | |
44 | */ | |
6e1886bc | 45 | public class StateSystemPushPopTest { |
a4e71249 | 46 | |
f1f86dfb | 47 | private ITmfStateSystemBuilder ss; |
a4e71249 AM |
48 | |
49 | private ITmfStateInterval interval; | |
50 | private int attribute; | |
51 | ||
947504fa | 52 | private File testHtFile; |
a4e71249 | 53 | |
cad06250 | 54 | private final static String errMsg = "Caught exception: "; |
a4e71249 AM |
55 | |
56 | /* State values that will be used */ | |
57 | //private final static ITmfStateValue nullValue = TmfStateValue.nullValue(); | |
cad06250 | 58 | private final static ITmfStateValue value1 = TmfStateValue.newValueString("A"); |
a4e71249 AM |
59 | private final static ITmfStateValue value2 = TmfStateValue.newValueInt(10); |
60 | private final static ITmfStateValue value3 = TmfStateValue.nullValue(); | |
cad06250 | 61 | private final static ITmfStateValue value4 = TmfStateValue.newValueString("D"); |
1cbf1a19 | 62 | private final static ITmfStateValue value5 = TmfStateValue.newValueLong(Long.MAX_VALUE); |
a4e71249 | 63 | |
a4e71249 AM |
64 | /** |
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. | |
68 | * | |
69 | * @throws IOException | |
70 | * If we can write the file to the temporary directory. | |
71 | * @throws TimeRangeException | |
72 | * Fails the test | |
73 | * @throws AttributeNotFoundException | |
74 | * Fails the test | |
75 | * @throws StateValueTypeException | |
76 | * Fails the test | |
77 | */ | |
6e1886bc | 78 | @Before |
a4e71249 AM |
79 | public void setUp() throws IOException, TimeRangeException, |
80 | AttributeNotFoundException, StateValueTypeException { | |
81 | ITmfStateValue value; | |
947504fa | 82 | testHtFile = File.createTempFile("test", ".ht"); |
a4e71249 | 83 | |
a96cc6be | 84 | IStateHistoryBackend backend = new HistoryTreeBackend(testHtFile, 0, 0L); |
84a9548a | 85 | ss = new StateSystem("push-pop-test", backend, true); |
a4e71249 AM |
86 | |
87 | /* Build the thing */ | |
cad06250 | 88 | final int attrib = ss.getQuarkAbsoluteAndAdd("Test", "stack"); |
a4e71249 AM |
89 | |
90 | ss.pushAttribute( 2, value1, attrib); | |
91 | ss.pushAttribute( 4, value2, attrib); | |
92 | ss.pushAttribute( 6, value3, attrib); | |
1cbf1a19 FR |
93 | ss.pushAttribute( 8, value4, attrib); |
94 | ss.pushAttribute(10, value5, attrib); | |
95 | ||
96 | value = ss.popAttribute(11, attrib); | |
97 | assertEquals(value5, value); | |
a4e71249 AM |
98 | |
99 | value = ss.popAttribute(12, attrib); | |
100 | assertEquals(value4, value); | |
101 | ||
102 | value = ss.popAttribute(14, attrib); | |
103 | assertEquals(value3, value); | |
104 | ||
105 | value = ss.popAttribute(16, attrib); | |
106 | assertEquals(value2, value); | |
107 | ||
108 | value = ss.popAttribute(17, attrib); | |
109 | assertEquals(value1, value); | |
110 | ||
111 | value = ss.popAttribute(20, attrib); | |
112 | assertEquals(null, value); // Stack should already be empty here. | |
113 | ||
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); | |
117 | ||
118 | value = ss.popAttribute(24, attrib); | |
119 | //assertEquals(value1, value); | |
120 | assertEquals(value2, value); | |
121 | ||
122 | value = ss.popAttribute(26, attrib); | |
123 | assertEquals(value1, value); | |
124 | ||
125 | value = ss.popAttribute(28, attrib); | |
126 | assertEquals(null, value); // Stack should already be empty here. | |
127 | ||
128 | ss.closeHistory(30); | |
cad06250 | 129 | attribute = ss.getQuarkAbsolute("Test", "stack"); |
a4e71249 AM |
130 | } |
131 | ||
132 | /** | |
133 | * Clean-up after running a test. Delete the .ht file we created. | |
134 | */ | |
6e1886bc | 135 | @After |
a4e71249 AM |
136 | public void tearDown() { |
137 | testHtFile.delete(); | |
138 | } | |
139 | ||
140 | /** | |
141 | * Test that the value of the stack-attribute at the start and end of the | |
142 | * history are correct. | |
143 | */ | |
6e1886bc | 144 | @Test |
a4e71249 AM |
145 | public void testBeginEnd() { |
146 | try { | |
147 | interval = ss.querySingleState(0, attribute); | |
148 | assertEquals(0, interval.getStartTime()); | |
149 | assertEquals(1, interval.getEndTime()); | |
150 | assertTrue(interval.getStateValue().isNull()); | |
151 | ||
152 | interval = ss.querySingleState(29, attribute); | |
153 | assertEquals(26, interval.getStartTime()); | |
154 | assertEquals(30, interval.getEndTime()); | |
155 | assertTrue(interval.getStateValue().isNull()); | |
156 | ||
157 | } catch (AttributeNotFoundException e) { | |
158 | fail(errMsg + e.toString()); | |
159 | } catch (TimeRangeException e) { | |
160 | fail(errMsg + e.toString()); | |
96345c5a AM |
161 | } catch (StateSystemDisposedException e) { |
162 | fail(errMsg + e.toString()); | |
a4e71249 AM |
163 | } |
164 | } | |
165 | ||
166 | /** | |
167 | * Run single queries on the attribute stacks (with .querySingleState()). | |
168 | */ | |
6e1886bc | 169 | @Test |
a4e71249 AM |
170 | public void testSingleQueries() { |
171 | try { | |
cad06250 AM |
172 | final int subAttribute1 = ss.getQuarkRelative(attribute, "1"); |
173 | final int subAttribute2 = ss.getQuarkRelative(attribute, "2"); | |
a4e71249 AM |
174 | |
175 | /* Test the stack attributes themselves */ | |
176 | interval = ss.querySingleState(11, attribute); | |
177 | assertEquals(4, interval.getStateValue().unboxInt()); | |
178 | ||
179 | interval = ss.querySingleState(24, attribute); | |
180 | assertEquals(1, interval.getStateValue().unboxInt()); | |
181 | ||
182 | /* Go retrieve the user values manually */ | |
183 | interval = ss.querySingleState(10, subAttribute1); | |
184 | assertEquals(value1, interval.getStateValue()); // | |
185 | ||
186 | interval = ss.querySingleState(22, subAttribute2); | |
187 | assertEquals(value2, interval.getStateValue()); | |
188 | ||
189 | interval = ss.querySingleState(25, subAttribute2); | |
190 | assertTrue(interval.getStateValue().isNull()); // Stack depth is 1 at that point. | |
191 | ||
192 | } catch (AttributeNotFoundException e) { | |
193 | fail(errMsg + e.toString()); | |
194 | } catch (StateValueTypeException e) { | |
195 | fail(errMsg + e.toString()); | |
196 | } catch (TimeRangeException e) { | |
197 | fail(errMsg + e.toString()); | |
96345c5a AM |
198 | } catch (StateSystemDisposedException e) { |
199 | fail(errMsg + e.toString()); | |
a4e71249 AM |
200 | } |
201 | } | |
202 | ||
203 | /** | |
204 | * Test the .querySingletStackTop() convenience method. | |
205 | */ | |
6e1886bc | 206 | @Test |
a4e71249 AM |
207 | public void testStackTop() { |
208 | try { | |
209 | interval = ss.querySingleStackTop(10, attribute); | |
1cbf1a19 FR |
210 | assertEquals(value5, interval.getStateValue()); |
211 | ||
212 | interval = ss.querySingleStackTop(9, attribute); | |
a4e71249 AM |
213 | assertEquals(value4, interval.getStateValue()); |
214 | ||
215 | interval = ss.querySingleStackTop(13, attribute); | |
216 | assertEquals(value3, interval.getStateValue()); | |
217 | ||
218 | interval = ss.querySingleStackTop(16, attribute); | |
219 | assertEquals(value1, interval.getStateValue()); | |
220 | ||
221 | interval = ss.querySingleStackTop(25, attribute); | |
222 | assertEquals(value1, interval.getStateValue()); | |
223 | ||
224 | } catch (AttributeNotFoundException e) { | |
225 | fail(errMsg + e.toString()); | |
226 | } catch (StateValueTypeException e) { | |
227 | fail(errMsg + e.toString()); | |
228 | } catch (TimeRangeException e) { | |
229 | fail(errMsg + e.toString()); | |
96345c5a AM |
230 | } catch (StateSystemDisposedException e) { |
231 | fail(errMsg + e.toString()); | |
a4e71249 AM |
232 | } |
233 | } | |
234 | ||
235 | /** | |
236 | * Test the places where the stack is empty. | |
237 | */ | |
6e1886bc | 238 | @Test |
a4e71249 AM |
239 | public void testEmptyStack() { |
240 | try { | |
241 | /* At the start */ | |
242 | interval = ss.querySingleState(1, attribute); | |
243 | assertTrue(interval.getStateValue().isNull()); | |
244 | interval = ss.querySingleStackTop(1, attribute); | |
245 | assertEquals(null, interval); | |
246 | ||
247 | /* Between the two "stacks" in the state history */ | |
248 | interval = ss.querySingleState(19, attribute); | |
249 | assertTrue(interval.getStateValue().isNull()); | |
250 | interval = ss.querySingleStackTop(19, attribute); | |
251 | assertEquals(null, interval); | |
252 | ||
253 | /* At the end */ | |
254 | interval = ss.querySingleState(27, attribute); | |
255 | assertTrue(interval.getStateValue().isNull()); | |
256 | interval = ss.querySingleStackTop(27, attribute); | |
257 | assertEquals(null, interval); | |
258 | ||
259 | } catch (AttributeNotFoundException e) { | |
260 | fail(errMsg + e.toString()); | |
261 | } catch (StateValueTypeException e) { | |
262 | fail(errMsg + e.toString()); | |
263 | } catch (TimeRangeException e) { | |
264 | fail(errMsg + e.toString()); | |
96345c5a AM |
265 | } catch (StateSystemDisposedException e) { |
266 | fail(errMsg + e.toString()); | |
a4e71249 AM |
267 | } |
268 | } | |
269 | ||
270 | /** | |
271 | * Test full-queries (.queryFullState()) on the attribute stacks. | |
272 | */ | |
6e1886bc | 273 | @Test |
a4e71249 AM |
274 | public void testFullQueries() { |
275 | List<ITmfStateInterval> state; | |
276 | try { | |
cad06250 AM |
277 | final int subAttrib1 = ss.getQuarkRelative(attribute, "1"); |
278 | final int subAttrib2 = ss.getQuarkRelative(attribute, "2"); | |
279 | final int subAttrib3 = ss.getQuarkRelative(attribute, "3"); | |
280 | final int subAttrib4 = ss.getQuarkRelative(attribute, "4"); | |
a4e71249 | 281 | |
1cbf1a19 | 282 | /* Stack depth = 5 */ |
a4e71249 | 283 | state = ss.queryFullState(10); |
1cbf1a19 | 284 | assertEquals(5, state.get(attribute).getStateValue().unboxInt()); |
a4e71249 AM |
285 | assertEquals(value1, state.get(subAttrib1).getStateValue()); |
286 | assertEquals(value2, state.get(subAttrib2).getStateValue()); | |
287 | assertEquals(value3, state.get(subAttrib3).getStateValue()); | |
288 | assertEquals(value4, state.get(subAttrib4).getStateValue()); | |
289 | ||
290 | /* Stack is empty */ | |
291 | state = ss.queryFullState(18); | |
292 | assertTrue(state.get(attribute).getStateValue().isNull()); | |
293 | assertTrue(state.get(subAttrib1).getStateValue().isNull()); | |
294 | assertTrue(state.get(subAttrib2).getStateValue().isNull()); | |
295 | assertTrue(state.get(subAttrib3).getStateValue().isNull()); | |
296 | assertTrue(state.get(subAttrib4).getStateValue().isNull()); | |
297 | ||
298 | /* Stack depth = 1 */ | |
299 | state = ss.queryFullState(21); | |
300 | assertEquals(1, state.get(attribute).getStateValue().unboxInt()); | |
301 | assertEquals(value1, state.get(subAttrib1).getStateValue()); | |
302 | assertTrue(state.get(subAttrib2).getStateValue().isNull()); | |
303 | assertTrue(state.get(subAttrib3).getStateValue().isNull()); | |
304 | assertTrue(state.get(subAttrib4).getStateValue().isNull()); | |
305 | ||
306 | } catch (AttributeNotFoundException e) { | |
307 | fail(errMsg + e.toString()); | |
308 | } catch (StateValueTypeException e) { | |
309 | fail(errMsg + e.toString()); | |
310 | } catch (TimeRangeException e) { | |
311 | fail(errMsg + e.toString()); | |
96345c5a AM |
312 | } catch (StateSystemDisposedException e) { |
313 | fail(errMsg + e.toString()); | |
a4e71249 AM |
314 | } |
315 | } | |
316 | } |