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