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