Commit | Line | Data |
---|---|---|
79e08fd0 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011 Ericsson | |
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 | * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial design and implementation | |
11 | * Bernd Hufmann - Fixed warnings | |
12 | *******************************************************************************/ | |
13 | ||
14 | package org.eclipse.linuxtools.tmf.ui.tests.statistics; | |
15 | ||
16 | import java.util.Collection; | |
17 | import java.util.Iterator; | |
18 | import java.util.Vector; | |
19 | ||
20 | import junit.framework.TestCase; | |
21 | ||
4c564a2d | 22 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
6c13869b | 23 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
4c564a2d | 24 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
6c13869b FC |
25 | import org.eclipse.linuxtools.tmf.core.event.TmfEventType; |
26 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; | |
27 | import org.eclipse.linuxtools.tmf.core.util.TmfFixedArray; | |
79e08fd0 | 28 | import org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfExtraEventInfo; |
99005796 | 29 | import org.eclipse.linuxtools.tmf.ui.views.statistics.model.AbsTmfStatisticsTree; |
79e08fd0 BH |
30 | import org.eclipse.linuxtools.tmf.ui.views.statistics.model.Messages; |
31 | import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseStatisticsTree; | |
79e08fd0 | 32 | import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsTreeNode; |
79e08fd0 BH |
33 | |
34 | @SuppressWarnings("nls") | |
35 | public class TmfStatisticsTreeNodeTest extends TestCase { | |
36 | ||
37 | // ------------------------------------------------------------------------ | |
38 | // Fields | |
39 | // ------------------------------------------------------------------------ | |
40 | private String fTestName = null; | |
41 | ||
42 | ||
cbbcc354 | 43 | private final String fContext = "UnitTest"; |
44 | private final String fTypeId1 = "Some type1"; | |
45 | private final String fTypeId2 = "Some type2"; | |
79e08fd0 BH |
46 | |
47 | private final String fLabel0 = "label1"; | |
48 | private final String fLabel1 = "label2"; | |
49 | private final String fLabel2 = "label3"; | |
50 | private final String[] fLabels = new String[] { fLabel0, fLabel1, fLabel2 }; | |
51 | ||
4641c2f7 FC |
52 | private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5); |
53 | private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5); | |
54 | private final TmfTimestamp fTimestamp3 = new TmfTimestamp(12355, (byte) 2, 5); | |
79e08fd0 | 55 | |
4641c2f7 | 56 | private final String fSource = "Source"; |
79e08fd0 | 57 | |
4c564a2d FC |
58 | private final TmfEventType fType1 = new TmfEventType(fContext, fTypeId1, TmfEventField.makeRoot(fLabels)); |
59 | private final TmfEventType fType2 = new TmfEventType(fContext, fTypeId1, TmfEventField.makeRoot(fLabels)); | |
60 | private final TmfEventType fType3 = new TmfEventType(fContext, fTypeId2, TmfEventField.makeRoot(fLabels)); | |
79e08fd0 | 61 | |
4641c2f7 | 62 | private final String fReference = "Some reference"; |
79e08fd0 BH |
63 | |
64 | private final TmfEvent fEvent1; | |
65 | private final TmfEvent fEvent2; | |
66 | private final TmfEvent fEvent3; | |
67 | ||
4c564a2d FC |
68 | private final TmfEventField fContent1; |
69 | private final TmfEventField fContent2; | |
70 | private final TmfEventField fContent3; | |
79e08fd0 BH |
71 | |
72 | private final TmfBaseStatisticsTree fStatsData; | |
73 | ||
74 | private final ITmfExtraEventInfo fExtraInfo; | |
75 | ||
76 | // ------------------------------------------------------------------------ | |
77 | // Housekeeping | |
78 | // ------------------------------------------------------------------------ | |
79 | ||
80 | /** | |
81 | * @param name of the test | |
82 | */ | |
83 | public TmfStatisticsTreeNodeTest(final String name) { | |
84 | super(name); | |
85 | ||
86 | fTestName = name; | |
87 | ||
a4115405 | 88 | fContent1 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some content"); |
7b477cc3 | 89 | fEvent1 = new TmfEvent(null, fTimestamp1, fSource, fType1, fContent1, fReference); |
79e08fd0 | 90 | |
a4115405 | 91 | fContent2 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other content"); |
7b477cc3 | 92 | fEvent2 = new TmfEvent(null, fTimestamp2, fSource, fType2, fContent2, fReference); |
79e08fd0 | 93 | |
a4115405 | 94 | fContent3 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other different content"); |
7b477cc3 FC |
95 | fEvent3 = new TmfEvent(null, fTimestamp3, fSource, fType3, fContent3, fReference); |
96 | ||
79e08fd0 BH |
97 | fStatsData = new TmfBaseStatisticsTree(); |
98 | fExtraInfo = new ITmfExtraEventInfo() { | |
99 | @Override | |
100 | public String getTraceName() { | |
101 | return name; | |
102 | } | |
103 | }; | |
104 | fStatsData.registerEvent(fEvent1, fExtraInfo); | |
105 | fStatsData.registerEvent(fEvent2, fExtraInfo); | |
106 | fStatsData.registerEvent(fEvent3, fExtraInfo); | |
107 | } | |
108 | ||
109 | // ------------------------------------------------------------------------ | |
110 | // ContainsChild | |
111 | // ------------------------------------------------------------------------ | |
112 | ||
113 | public void testContainsChild() { | |
114 | TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); | |
115 | TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName)); | |
116 | // Creates a category from the key already created | |
117 | TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); | |
118 | ||
119 | assertTrue("containsChild", rootNode.containsChild(fTestName)); | |
120 | assertFalse("containsChild", rootNode.containsChild(catNode.getKey())); | |
121 | assertFalse("containsChild", rootNode.containsChild(null)); | |
122 | ||
123 | assertTrue("containsChild", traceNode.containsChild(catNode.getKey())); | |
124 | assertFalse("containsChild", traceNode.containsChild(fEvent1.getType().toString())); | |
125 | assertFalse("containsChild", traceNode.containsChild(null)); | |
126 | ||
127 | assertTrue("containsChild", catNode.containsChild(fEvent1.getType().toString())); | |
128 | assertTrue("containsChild", catNode.containsChild(fEvent3.getType().toString())); | |
129 | assertFalse("containsChild", catNode.containsChild(null)); | |
130 | } | |
131 | ||
132 | // ------------------------------------------------------------------------ | |
133 | // GetChildren | |
134 | // ------------------------------------------------------------------------ | |
135 | ||
136 | public void testGetChildren() { | |
137 | // Getting children of the ROOT | |
138 | Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsData.get(AbsTmfStatisticsTree.ROOT).getChildren(); | |
139 | assertEquals("getChildren", 1, childrenTreeNode.size()); | |
140 | TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next(); | |
141 | assertEquals("getChildren", fTestName, treeNode.getKey()); | |
142 | ||
143 | // Getting children of the trace | |
144 | childrenTreeNode = fStatsData.get(new TmfFixedArray<String>(fTestName)).getChildren(); | |
145 | assertEquals("getChildren", 1, childrenTreeNode.size()); | |
146 | treeNode = childrenTreeNode.iterator().next(); | |
147 | assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey()); | |
148 | ||
149 | Vector<String> keyExpected = new Vector<String>(); | |
150 | keyExpected.add(fEvent1.getType().toString()); | |
151 | keyExpected.add(fEvent3.getType().toString()); | |
152 | // Getting children of a category | |
153 | childrenTreeNode = treeNode.getChildren(); | |
154 | assertEquals("getChildren", 2, childrenTreeNode.size()); | |
155 | ||
156 | Iterator<TmfStatisticsTreeNode> iterChild = childrenTreeNode.iterator(); | |
157 | TmfStatisticsTreeNode temp; | |
158 | while (iterChild.hasNext()) { | |
159 | temp = iterChild.next(); | |
160 | if (keyExpected.contains(temp.getKey())) { | |
161 | keyExpected.removeElement(temp.getKey()); | |
162 | } | |
163 | else { | |
164 | fail(); | |
165 | } | |
166 | } | |
167 | ||
168 | // Get children of a specific event type | |
169 | childrenTreeNode = fStatsData.get(childrenTreeNode.iterator().next().getPath()).getChildren(); | |
170 | assertEquals("getChildren", 0, childrenTreeNode.size()); | |
171 | } | |
172 | ||
173 | // ------------------------------------------------------------------------ | |
174 | // GetAllChildren | |
175 | // ------------------------------------------------------------------------ | |
176 | ||
177 | public void testGetAllChildren() { | |
178 | // Getting children of the ROOT | |
179 | Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsData.get(AbsTmfStatisticsTree.ROOT).getAllChildren(); | |
180 | assertEquals("getChildren", 1, childrenTreeNode.size()); | |
181 | TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next(); | |
182 | assertEquals("getChildren", fTestName, treeNode.getKey()); | |
183 | ||
184 | // Getting children of the trace | |
185 | childrenTreeNode = fStatsData.get(new TmfFixedArray<String>(fTestName)).getAllChildren(); | |
186 | assertEquals("getChildren", 1, childrenTreeNode.size()); | |
187 | treeNode = childrenTreeNode.iterator().next(); | |
188 | assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey()); | |
189 | ||
190 | Vector<String> keyExpected = new Vector<String>(); | |
191 | keyExpected.add(fEvent1.getType().toString()); | |
192 | keyExpected.add(fEvent3.getType().toString()); | |
193 | // It should return the eventType even though the number of events equals 0 | |
194 | fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())).reset(); | |
195 | // Getting children of a category | |
196 | childrenTreeNode = treeNode.getAllChildren(); | |
197 | assertEquals("getChildren", 2, childrenTreeNode.size()); | |
198 | ||
199 | Iterator<TmfStatisticsTreeNode> iterChild = childrenTreeNode.iterator(); | |
200 | TmfStatisticsTreeNode temp; | |
201 | while (iterChild.hasNext()) { | |
202 | temp = iterChild.next(); | |
203 | if (keyExpected.contains(temp.getKey())) { | |
204 | keyExpected.removeElement(temp.getKey()); | |
205 | } | |
206 | else { | |
207 | fail(); | |
208 | } | |
209 | } | |
210 | ||
211 | // Get children of a specific event type | |
212 | childrenTreeNode = fStatsData.get(childrenTreeNode.iterator().next().getPath()).getAllChildren(); | |
213 | assertEquals("getChildren", 0, childrenTreeNode.size()); | |
214 | } | |
215 | ||
216 | // ------------------------------------------------------------------------ | |
217 | // GetNbChildren | |
218 | // ------------------------------------------------------------------------ | |
219 | ||
220 | public void testGetNbChildren() { | |
221 | TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); | |
222 | TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName)); | |
223 | TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); | |
224 | TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); | |
225 | ||
226 | assertEquals("getNbChildren", 1, rootNode.getNbChildren()); | |
227 | assertEquals("getNbChildren", 1, traceNode.getNbChildren()); | |
228 | assertEquals("getNbChildren", 2, catNode.getNbChildren()); | |
229 | assertEquals("getNbChildren", 0, elementNode.getNbChildren()); | |
230 | } | |
231 | ||
232 | // ------------------------------------------------------------------------ | |
233 | // HasChildren | |
234 | // ------------------------------------------------------------------------ | |
235 | ||
236 | public void testHasChildren() { | |
237 | TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); | |
238 | TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName)); | |
239 | TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); | |
240 | TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); | |
241 | ||
242 | assertTrue ("hasChildren", rootNode.hasChildren()); | |
243 | assertTrue ("hasChildren", traceNode.hasChildren()); | |
244 | assertTrue ("hasChildren", catNode.hasChildren()); | |
245 | assertFalse("hasChildren", elementNode.hasChildren()); | |
246 | } | |
247 | ||
248 | // ------------------------------------------------------------------------ | |
249 | // GetParent | |
250 | // ------------------------------------------------------------------------ | |
251 | ||
252 | public void testGetParent() { | |
253 | TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); | |
254 | TmfStatisticsTreeNode parentNode = rootNode.getParent(); | |
255 | assertNull("getParent", parentNode); | |
256 | ||
257 | TmfStatisticsTreeNode newTraceNode = new TmfStatisticsTreeNode(new TmfFixedArray<String>("newly created trace node"), fStatsData); | |
258 | parentNode = newTraceNode.getParent(); | |
259 | assertNotNull("getParent", parentNode); | |
260 | assertEquals("getParent", 0, parentNode.getKey().compareTo(fStatsData.get(AbsTmfStatisticsTree.ROOT).getKey().toString())); | |
261 | ||
262 | TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName)); | |
263 | parentNode = traceNode.getParent(); | |
264 | assertNotNull("getParent", parentNode); | |
265 | assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(AbsTmfStatisticsTree.ROOT.toString())); | |
266 | ||
267 | TmfStatisticsTreeNode newNode = new TmfStatisticsTreeNode(new TmfFixedArray<String>("TreeNode", Messages.TmfStatisticsData_EventTypes, "TreeNode that should not exist"), fStatsData); | |
268 | parentNode = newNode.getParent(); | |
269 | assertNull("getParent", parentNode); | |
270 | ||
271 | TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); | |
272 | parentNode = elementNode.getParent(); | |
273 | assertNull("getParent", parentNode); | |
274 | ||
275 | TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); | |
276 | parentNode = catNode.getParent(); | |
277 | assertNotNull("getParent", parentNode); | |
278 | assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(fStatsData.get(new TmfFixedArray<String>(fTestName)).getPath().toString())); | |
279 | ||
280 | parentNode = elementNode.getParent(); | |
281 | assertNotNull("getParent", parentNode); | |
282 | assertTrue("getParent", parentNode.getPath().equals(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes))); | |
283 | } | |
284 | ||
285 | // ------------------------------------------------------------------------ | |
286 | // GetKey | |
287 | // ------------------------------------------------------------------------ | |
288 | ||
289 | public void testGetKey() { | |
290 | TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); | |
291 | TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName)); | |
292 | TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); | |
293 | TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); | |
294 | ||
295 | assertEquals("getKey", 0, rootNode.getKey().compareTo(AbsTmfStatisticsTree.ROOT.get(0))); | |
296 | assertEquals("getKey", 0, traceNode.getKey().compareTo(fTestName)); | |
297 | assertEquals("getKey", 0, catNode.getKey().compareTo(Messages.TmfStatisticsData_EventTypes)); | |
298 | assertEquals("getKey", 0, elementNode.getKey().compareTo(fEvent1.getType().toString())); | |
299 | } | |
300 | ||
301 | // ------------------------------------------------------------------------ | |
302 | // GetPath | |
303 | // ------------------------------------------------------------------------ | |
304 | ||
305 | public void testGetPath() { | |
306 | TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); | |
307 | TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName)); | |
308 | TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); | |
309 | TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); | |
310 | ||
311 | assertTrue("getPath", rootNode.getPath().equals(AbsTmfStatisticsTree.ROOT)); | |
312 | assertTrue("getPath", traceNode.getPath().equals(new TmfFixedArray<String>(fTestName))); | |
313 | assertTrue("getPath", catNode.getPath().equals(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes))); | |
314 | assertTrue("getPath", elementNode.getPath().equals(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString()))); | |
315 | } | |
316 | ||
317 | // ------------------------------------------------------------------------ | |
318 | // GetValue | |
319 | // ------------------------------------------------------------------------ | |
320 | ||
321 | public void testGetValue() { | |
322 | TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); | |
323 | TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName)); | |
324 | TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); | |
325 | TmfStatisticsTreeNode elementNode1 = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); | |
326 | TmfStatisticsTreeNode elementNode2 = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent3.getType().toString())); | |
327 | ||
328 | assertEquals("getValue", 0, rootNode.getValue().nbEvents); | |
329 | assertEquals("getValue", 3, traceNode.getValue().nbEvents); | |
330 | assertEquals("getValue", 0, catNode.getValue().nbEvents); | |
331 | assertEquals("getValue", 2, elementNode1.getValue().nbEvents); | |
332 | assertEquals("getValue", 1, elementNode2.getValue().nbEvents); | |
333 | } | |
334 | ||
335 | // ------------------------------------------------------------------------ | |
336 | // Reset | |
337 | // ------------------------------------------------------------------------ | |
338 | ||
339 | public void testReset() { | |
340 | TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); | |
341 | TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName)); | |
342 | TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); | |
343 | TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); | |
344 | ||
345 | elementNode.reset(); | |
346 | assertEquals("reset", 0, elementNode.getValue().nbEvents); | |
347 | ||
348 | catNode.reset(); | |
349 | assertEquals("reset", 0, catNode.getValue().nbEvents); | |
350 | assertEquals("reset", 0, catNode.getNbChildren()); | |
351 | assertNull("reset", fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString()))); | |
352 | ||
353 | traceNode.reset(); | |
354 | assertEquals("reset", 0, traceNode.getValue().nbEvents); | |
355 | // A trace always have at least one child that is eventType | |
356 | assertEquals("reset", 1, traceNode.getNbChildren()); | |
357 | ||
358 | rootNode.reset(); | |
359 | assertEquals("reset", 0, rootNode.getValue().nbEvents); | |
360 | assertEquals("reset", 1, rootNode.getNbChildren()); | |
361 | } | |
362 | } |