Commit | Line | Data |
---|---|---|
a4e71249 | 1 | /******************************************************************************* |
6e1886bc | 2 | * Copyright (c) 2012, 2013 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 | ||
52 | private final File testHtFile; | |
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 AM |
63 | |
64 | /** | |
65 | * Test case constructor | |
66 | * | |
a4e71249 AM |
67 | * @throws IOException |
68 | * If we couldn't create the state history test file | |
69 | */ | |
6e1886bc | 70 | public StateSystemPushPopTest() throws IOException { |
cad06250 | 71 | testHtFile = File.createTempFile("test", ".ht"); |
a4e71249 AM |
72 | testHtFile.deleteOnExit(); |
73 | } | |
74 | ||
75 | /** | |
76 | * Initialization. We run the checks for the return values of | |
77 | * .popAttribute() in here, since this is only available when we are | |
78 | * building the state history. | |
79 | * | |
80 | * @throws IOException | |
81 | * If we can write the file to the temporary directory. | |
82 | * @throws TimeRangeException | |
83 | * Fails the test | |
84 | * @throws AttributeNotFoundException | |
85 | * Fails the test | |
86 | * @throws StateValueTypeException | |
87 | * Fails the test | |
88 | */ | |
6e1886bc | 89 | @Before |
a4e71249 AM |
90 | public void setUp() throws IOException, TimeRangeException, |
91 | AttributeNotFoundException, StateValueTypeException { | |
92 | ITmfStateValue value; | |
93 | ||
a96cc6be | 94 | IStateHistoryBackend backend = new HistoryTreeBackend(testHtFile, 0, 0L); |
a4e71249 AM |
95 | ss = new StateSystem(backend, true); |
96 | ||
97 | /* Build the thing */ | |
cad06250 | 98 | final int attrib = ss.getQuarkAbsoluteAndAdd("Test", "stack"); |
a4e71249 AM |
99 | |
100 | ss.pushAttribute( 2, value1, attrib); | |
101 | ss.pushAttribute( 4, value2, attrib); | |
102 | ss.pushAttribute( 6, value3, attrib); | |
1cbf1a19 FR |
103 | ss.pushAttribute( 8, value4, attrib); |
104 | ss.pushAttribute(10, value5, attrib); | |
105 | ||
106 | value = ss.popAttribute(11, attrib); | |
107 | assertEquals(value5, value); | |
a4e71249 AM |
108 | |
109 | value = ss.popAttribute(12, attrib); | |
110 | assertEquals(value4, value); | |
111 | ||
112 | value = ss.popAttribute(14, attrib); | |
113 | assertEquals(value3, value); | |
114 | ||
115 | value = ss.popAttribute(16, attrib); | |
116 | assertEquals(value2, value); | |
117 | ||
118 | value = ss.popAttribute(17, attrib); | |
119 | assertEquals(value1, value); | |
120 | ||
121 | value = ss.popAttribute(20, attrib); | |
122 | assertEquals(null, value); // Stack should already be empty here. | |
123 | ||
124 | ss.pushAttribute(21, value1, attrib); | |
125 | //ss.pushAttribute(22, value1, attrib); //FIXME pushing twice the same value bugs out atm | |
126 | ss.pushAttribute(22, value2, attrib); | |
127 | ||
128 | value = ss.popAttribute(24, attrib); | |
129 | //assertEquals(value1, value); | |
130 | assertEquals(value2, value); | |
131 | ||
132 | value = ss.popAttribute(26, attrib); | |
133 | assertEquals(value1, value); | |
134 | ||
135 | value = ss.popAttribute(28, attrib); | |
136 | assertEquals(null, value); // Stack should already be empty here. | |
137 | ||
138 | ss.closeHistory(30); | |
cad06250 | 139 | attribute = ss.getQuarkAbsolute("Test", "stack"); |
a4e71249 AM |
140 | } |
141 | ||
142 | /** | |
143 | * Clean-up after running a test. Delete the .ht file we created. | |
144 | */ | |
6e1886bc | 145 | @After |
a4e71249 AM |
146 | public void tearDown() { |
147 | testHtFile.delete(); | |
148 | } | |
149 | ||
150 | /** | |
151 | * Test that the value of the stack-attribute at the start and end of the | |
152 | * history are correct. | |
153 | */ | |
6e1886bc | 154 | @Test |
a4e71249 AM |
155 | public void testBeginEnd() { |
156 | try { | |
157 | interval = ss.querySingleState(0, attribute); | |
158 | assertEquals(0, interval.getStartTime()); | |
159 | assertEquals(1, interval.getEndTime()); | |
160 | assertTrue(interval.getStateValue().isNull()); | |
161 | ||
162 | interval = ss.querySingleState(29, attribute); | |
163 | assertEquals(26, interval.getStartTime()); | |
164 | assertEquals(30, interval.getEndTime()); | |
165 | assertTrue(interval.getStateValue().isNull()); | |
166 | ||
167 | } catch (AttributeNotFoundException e) { | |
168 | fail(errMsg + e.toString()); | |
169 | } catch (TimeRangeException e) { | |
170 | fail(errMsg + e.toString()); | |
96345c5a AM |
171 | } catch (StateSystemDisposedException e) { |
172 | fail(errMsg + e.toString()); | |
a4e71249 AM |
173 | } |
174 | } | |
175 | ||
176 | /** | |
177 | * Run single queries on the attribute stacks (with .querySingleState()). | |
178 | */ | |
6e1886bc | 179 | @Test |
a4e71249 AM |
180 | public void testSingleQueries() { |
181 | try { | |
cad06250 AM |
182 | final int subAttribute1 = ss.getQuarkRelative(attribute, "1"); |
183 | final int subAttribute2 = ss.getQuarkRelative(attribute, "2"); | |
a4e71249 AM |
184 | |
185 | /* Test the stack attributes themselves */ | |
186 | interval = ss.querySingleState(11, attribute); | |
187 | assertEquals(4, interval.getStateValue().unboxInt()); | |
188 | ||
189 | interval = ss.querySingleState(24, attribute); | |
190 | assertEquals(1, interval.getStateValue().unboxInt()); | |
191 | ||
192 | /* Go retrieve the user values manually */ | |
193 | interval = ss.querySingleState(10, subAttribute1); | |
194 | assertEquals(value1, interval.getStateValue()); // | |
195 | ||
196 | interval = ss.querySingleState(22, subAttribute2); | |
197 | assertEquals(value2, interval.getStateValue()); | |
198 | ||
199 | interval = ss.querySingleState(25, subAttribute2); | |
200 | assertTrue(interval.getStateValue().isNull()); // Stack depth is 1 at that point. | |
201 | ||
202 | } catch (AttributeNotFoundException e) { | |
203 | fail(errMsg + e.toString()); | |
204 | } catch (StateValueTypeException e) { | |
205 | fail(errMsg + e.toString()); | |
206 | } catch (TimeRangeException e) { | |
207 | fail(errMsg + e.toString()); | |
96345c5a AM |
208 | } catch (StateSystemDisposedException e) { |
209 | fail(errMsg + e.toString()); | |
a4e71249 AM |
210 | } |
211 | } | |
212 | ||
213 | /** | |
214 | * Test the .querySingletStackTop() convenience method. | |
215 | */ | |
6e1886bc | 216 | @Test |
a4e71249 AM |
217 | public void testStackTop() { |
218 | try { | |
219 | interval = ss.querySingleStackTop(10, attribute); | |
1cbf1a19 FR |
220 | assertEquals(value5, interval.getStateValue()); |
221 | ||
222 | interval = ss.querySingleStackTop(9, attribute); | |
a4e71249 AM |
223 | assertEquals(value4, interval.getStateValue()); |
224 | ||
225 | interval = ss.querySingleStackTop(13, attribute); | |
226 | assertEquals(value3, interval.getStateValue()); | |
227 | ||
228 | interval = ss.querySingleStackTop(16, attribute); | |
229 | assertEquals(value1, interval.getStateValue()); | |
230 | ||
231 | interval = ss.querySingleStackTop(25, attribute); | |
232 | assertEquals(value1, interval.getStateValue()); | |
233 | ||
234 | } catch (AttributeNotFoundException e) { | |
235 | fail(errMsg + e.toString()); | |
236 | } catch (StateValueTypeException e) { | |
237 | fail(errMsg + e.toString()); | |
238 | } catch (TimeRangeException e) { | |
239 | fail(errMsg + e.toString()); | |
96345c5a AM |
240 | } catch (StateSystemDisposedException e) { |
241 | fail(errMsg + e.toString()); | |
a4e71249 AM |
242 | } |
243 | } | |
244 | ||
245 | /** | |
246 | * Test the places where the stack is empty. | |
247 | */ | |
6e1886bc | 248 | @Test |
a4e71249 AM |
249 | public void testEmptyStack() { |
250 | try { | |
251 | /* At the start */ | |
252 | interval = ss.querySingleState(1, attribute); | |
253 | assertTrue(interval.getStateValue().isNull()); | |
254 | interval = ss.querySingleStackTop(1, attribute); | |
255 | assertEquals(null, interval); | |
256 | ||
257 | /* Between the two "stacks" in the state history */ | |
258 | interval = ss.querySingleState(19, attribute); | |
259 | assertTrue(interval.getStateValue().isNull()); | |
260 | interval = ss.querySingleStackTop(19, attribute); | |
261 | assertEquals(null, interval); | |
262 | ||
263 | /* At the end */ | |
264 | interval = ss.querySingleState(27, attribute); | |
265 | assertTrue(interval.getStateValue().isNull()); | |
266 | interval = ss.querySingleStackTop(27, attribute); | |
267 | assertEquals(null, interval); | |
268 | ||
269 | } catch (AttributeNotFoundException e) { | |
270 | fail(errMsg + e.toString()); | |
271 | } catch (StateValueTypeException e) { | |
272 | fail(errMsg + e.toString()); | |
273 | } catch (TimeRangeException e) { | |
274 | fail(errMsg + e.toString()); | |
96345c5a AM |
275 | } catch (StateSystemDisposedException e) { |
276 | fail(errMsg + e.toString()); | |
a4e71249 AM |
277 | } |
278 | } | |
279 | ||
280 | /** | |
281 | * Test full-queries (.queryFullState()) on the attribute stacks. | |
282 | */ | |
6e1886bc | 283 | @Test |
a4e71249 AM |
284 | public void testFullQueries() { |
285 | List<ITmfStateInterval> state; | |
286 | try { | |
cad06250 AM |
287 | final int subAttrib1 = ss.getQuarkRelative(attribute, "1"); |
288 | final int subAttrib2 = ss.getQuarkRelative(attribute, "2"); | |
289 | final int subAttrib3 = ss.getQuarkRelative(attribute, "3"); | |
290 | final int subAttrib4 = ss.getQuarkRelative(attribute, "4"); | |
a4e71249 | 291 | |
1cbf1a19 | 292 | /* Stack depth = 5 */ |
a4e71249 | 293 | state = ss.queryFullState(10); |
1cbf1a19 | 294 | assertEquals(5, state.get(attribute).getStateValue().unboxInt()); |
a4e71249 AM |
295 | assertEquals(value1, state.get(subAttrib1).getStateValue()); |
296 | assertEquals(value2, state.get(subAttrib2).getStateValue()); | |
297 | assertEquals(value3, state.get(subAttrib3).getStateValue()); | |
298 | assertEquals(value4, state.get(subAttrib4).getStateValue()); | |
299 | ||
300 | /* Stack is empty */ | |
301 | state = ss.queryFullState(18); | |
302 | assertTrue(state.get(attribute).getStateValue().isNull()); | |
303 | assertTrue(state.get(subAttrib1).getStateValue().isNull()); | |
304 | assertTrue(state.get(subAttrib2).getStateValue().isNull()); | |
305 | assertTrue(state.get(subAttrib3).getStateValue().isNull()); | |
306 | assertTrue(state.get(subAttrib4).getStateValue().isNull()); | |
307 | ||
308 | /* Stack depth = 1 */ | |
309 | state = ss.queryFullState(21); | |
310 | assertEquals(1, state.get(attribute).getStateValue().unboxInt()); | |
311 | assertEquals(value1, state.get(subAttrib1).getStateValue()); | |
312 | assertTrue(state.get(subAttrib2).getStateValue().isNull()); | |
313 | assertTrue(state.get(subAttrib3).getStateValue().isNull()); | |
314 | assertTrue(state.get(subAttrib4).getStateValue().isNull()); | |
315 | ||
316 | } catch (AttributeNotFoundException e) { | |
317 | fail(errMsg + e.toString()); | |
318 | } catch (StateValueTypeException e) { | |
319 | fail(errMsg + e.toString()); | |
320 | } catch (TimeRangeException e) { | |
321 | fail(errMsg + e.toString()); | |
96345c5a AM |
322 | } catch (StateSystemDisposedException e) { |
323 | fail(errMsg + e.toString()); | |
a4e71249 AM |
324 | } |
325 | } | |
326 | } |