Merge branch 'master' into lttng-kepler
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.tests / src / org / eclipse / linuxtools / tmf / ui / tests / statistics / TmfStatisticsTreeNodeTest.java
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
22 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.Messages;
23 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTree;
24 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode;
25
26 /**
27 * TmfStatisticsTreeNode Test Cases.
28 */
29 public class TmfStatisticsTreeNodeTest extends TestCase {
30
31 // ------------------------------------------------------------------------
32 // Fields
33 // ------------------------------------------------------------------------
34
35 private final String fTypeId1 = "Some type1"; //$NON-NLS-1$
36 private final String fTypeId2 = "Some type2"; //$NON-NLS-1$
37 private final String fTypeId3 = "Some type3"; //$NON-NLS-1$
38
39 private final TmfStatisticsTree fStatsTree;
40
41 private String fTestName = null;
42
43 // ------------------------------------------------------------------------
44 // Housekeeping
45 // ------------------------------------------------------------------------
46
47 /**
48 * @param name
49 * Test name
50 */
51 public TmfStatisticsTreeNodeTest(final String name) {
52 super(name);
53
54 fTestName = name;
55 fStatsTree = new TmfStatisticsTree();
56
57 /* Enter some global values */
58 fStatsTree.setTotal(fTestName, true, 18);
59 fStatsTree.setTypeCount(fTestName, fTypeId1, true, 5);
60 fStatsTree.setTypeCount(fTestName, fTypeId2, true, 6);
61 fStatsTree.setTypeCount(fTestName, fTypeId3, true, 7);
62
63 /* Enter some time range values */
64 fStatsTree.setTotal(fTestName, false, 9);
65 fStatsTree.setTypeCount(fTestName, fTypeId1, false, 2);
66 fStatsTree.setTypeCount(fTestName, fTypeId2, false, 3);
67 fStatsTree.setTypeCount(fTestName, fTypeId3, false, 4);
68 }
69
70 // ------------------------------------------------------------------------
71 // ContainsChild
72 // ------------------------------------------------------------------------
73
74 /**
75 * Test checking for child.
76 */
77 public void testContainsChild() {
78 TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode();
79 TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName);
80 // Creates a category from the key already created
81 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
82
83 assertTrue(rootNode.containsChild(fTestName));
84 assertFalse(rootNode.containsChild(catNode.getName()));
85 assertFalse(rootNode.containsChild(null));
86
87 assertTrue(traceNode.containsChild(catNode.getName()));
88 assertFalse(traceNode.containsChild(fTypeId1));
89 assertFalse(traceNode.containsChild(null));
90
91 assertTrue(catNode.containsChild(fTypeId1));
92 assertTrue(catNode.containsChild(fTypeId2));
93 assertFalse(catNode.containsChild(null));
94 }
95
96 // ------------------------------------------------------------------------
97 // GetChildren
98 // ------------------------------------------------------------------------
99
100 /**
101 * Test getting of children.
102 */
103 public void testGetChildren() {
104 // Getting children of the ROOT
105 Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsTree.getRootNode().getChildren();
106 assertEquals(1, childrenTreeNode.size());
107 TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next();
108 assertEquals(fTestName, treeNode.getName());
109
110 // Getting children of the trace
111 childrenTreeNode = fStatsTree.getNode(fTestName).getChildren();
112 assertEquals(1, childrenTreeNode.size());
113 treeNode = childrenTreeNode.iterator().next();
114 assertEquals(Messages.TmfStatisticsData_EventTypes, treeNode.getName());
115
116 Vector<String> keyExpected = new Vector<String>();
117 keyExpected.add(fTypeId1);
118 keyExpected.add(fTypeId2);
119 keyExpected.add(fTypeId3);
120 // Getting children of a category
121 childrenTreeNode = treeNode.getChildren();
122 assertEquals(3, childrenTreeNode.size());
123
124 Iterator<TmfStatisticsTreeNode> iterChild = childrenTreeNode.iterator();
125 TmfStatisticsTreeNode temp;
126 while (iterChild.hasNext()) {
127 temp = iterChild.next();
128 if (keyExpected.contains(temp.getName())) {
129 keyExpected.removeElement(temp.getName());
130 } else {
131 fail();
132 }
133 }
134
135 // Get children of a specific event type
136 childrenTreeNode = fStatsTree.getNode(childrenTreeNode.iterator().next().getPath()).getChildren();
137 assertEquals(0, childrenTreeNode.size());
138 }
139
140 // ------------------------------------------------------------------------
141 // GetNbChildren
142 // ------------------------------------------------------------------------
143
144 /**
145 * Test getting of number of children.
146 */
147 public void testGetNbChildren() {
148 TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode();
149 TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName);
150 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
151 TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
152
153 assertEquals(1, rootNode.getNbChildren());
154 assertEquals(1, traceNode.getNbChildren());
155 assertEquals(3, catNode.getNbChildren());
156 assertEquals(0, elementNode.getNbChildren());
157 }
158
159 // ------------------------------------------------------------------------
160 // HasChildren
161 // ------------------------------------------------------------------------
162
163 /**
164 * Test checking for children.
165 */
166 public void testHasChildren() {
167 TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode();
168 TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName);
169 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
170 TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
171
172 assertTrue(rootNode.hasChildren());
173 assertTrue(traceNode.hasChildren());
174 assertTrue(catNode.hasChildren());
175 assertFalse(elementNode.hasChildren());
176 }
177
178 // ------------------------------------------------------------------------
179 // GetParent
180 // ------------------------------------------------------------------------
181
182 /**
183 * Test getting of parent.
184 */
185 public void testGetParent() {
186 final TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode();
187 TmfStatisticsTreeNode parentNode = rootNode.getParent();
188 assertNull(parentNode);
189
190 TmfStatisticsTreeNode newTraceNode = new TmfStatisticsTreeNode(fStatsTree, rootNode, "newly created trace node"); //$NON-NLS-1$
191 parentNode = newTraceNode.getParent();
192 assertNotNull(parentNode);
193 assertTrue(fStatsTree.getRootNode() == parentNode);
194
195 TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName);
196 parentNode = traceNode.getParent();
197 assertNotNull(parentNode);
198 assertTrue(rootNode == parentNode);
199
200 TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
201 parentNode = elementNode.getParent();
202 assertTrue(parentNode == fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes));
203
204 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
205 parentNode = catNode.getParent();
206 assertNotNull(parentNode);
207 assertTrue(parentNode == fStatsTree.getNode(fTestName));
208
209 parentNode = elementNode.getParent();
210 assertNotNull(parentNode);
211 assertTrue(arraysEqual(parentNode.getPath(), fTestName, Messages.TmfStatisticsData_EventTypes));
212 }
213
214 // ------------------------------------------------------------------------
215 // GetKey
216 // ------------------------------------------------------------------------
217
218 /**
219 * Test getting of key.
220 */
221 public void testgetName() {
222 TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode();
223 TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName);
224 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
225 TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
226
227 assertEquals(0, rootNode.getName().compareTo("root")); //$NON-NLS-1$
228 assertEquals(0, traceNode.getName().compareTo(fTestName));
229 assertEquals(0, catNode.getName().compareTo(Messages.TmfStatisticsData_EventTypes));
230 assertEquals(0, elementNode.getName().compareTo(fTypeId1));
231 }
232
233 // ------------------------------------------------------------------------
234 // GetPath
235 // ------------------------------------------------------------------------
236
237 /**
238 * Test getting of path to node.
239 */
240 public void testGetPath() {
241 TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode();
242 TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName);
243 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
244 TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
245
246 assertEquals(0, rootNode.getPath().length); /* Root node has an empty path */
247 assertTrue(arraysEqual(traceNode.getPath(), fTestName));
248 assertTrue(arraysEqual(catNode.getPath(),
249 fTestName, Messages.TmfStatisticsData_EventTypes));
250 assertTrue(arraysEqual(elementNode.getPath(),
251 fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1));
252 }
253
254 // ------------------------------------------------------------------------
255 // GetValue
256 // ------------------------------------------------------------------------
257
258 /**
259 * Test getting statistic value.
260 */
261 public void testGetValue() {
262 TmfStatisticsTreeNode rootNode, traceNode, catNode, elementNode1, elementNode2, elementNode3;
263 rootNode = fStatsTree.getRootNode();
264 traceNode = fStatsTree.getNode(fTestName);
265 catNode = traceNode.getChildren().iterator().next();
266 elementNode1 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
267 elementNode2 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId2);
268 elementNode3 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId3);
269
270 assertEquals(0, rootNode.getValues().getTotal());
271 assertEquals(18, traceNode.getValues().getTotal());
272 assertEquals(0, catNode.getValues().getTotal());
273 assertEquals(5, elementNode1.getValues().getTotal());
274 assertEquals(6, elementNode2.getValues().getTotal());
275 assertEquals(7, elementNode3.getValues().getTotal());
276
277 assertEquals(0, rootNode.getValues().getPartial());
278 assertEquals(9, traceNode.getValues().getPartial());
279 assertEquals(0, catNode.getValues().getPartial());
280 assertEquals(2, elementNode1.getValues().getPartial());
281 assertEquals(3, elementNode2.getValues().getPartial());
282 assertEquals(4, elementNode3.getValues().getPartial());
283 }
284
285 // ------------------------------------------------------------------------
286 // Reset
287 // ------------------------------------------------------------------------
288
289 /**
290 * Test reset of tree.
291 */
292 public void testReset() {
293 TmfStatisticsTreeNode rootNode, traceNode, catNode, elementNode;
294 rootNode = fStatsTree.getRootNode();
295 traceNode = fStatsTree.getNode(fTestName);
296 catNode = traceNode.getChildren().iterator().next();
297 elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
298
299 elementNode.reset();
300 assertEquals(0, elementNode.getValues().getTotal());
301 assertEquals(0, elementNode.getValues().getPartial());
302
303 catNode.reset();
304 assertEquals(0, catNode.getValues().getTotal());
305 assertEquals(0, catNode.getValues().getPartial());
306 assertEquals(0, catNode.getNbChildren());
307 assertNull(fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1));
308
309 traceNode.reset();
310 assertEquals(0, traceNode.getValues().getTotal());
311 assertEquals(0, traceNode.getValues().getPartial());
312 assertEquals(0, traceNode.getNbChildren());
313
314 rootNode.reset();
315 assertEquals(0, rootNode.getValues().getTotal());
316 assertEquals(0, rootNode.getValues().getPartial());
317 assertEquals(0, rootNode.getNbChildren());
318 }
319
320 /**
321 * Test reset global value of the node in the tree. It should only clear
322 * the global value without removing any node from the tree.
323 */
324 public void testResetGlobalValue() {
325 TmfStatisticsTreeNode rootNode, traceNode, catNode, eventTypeNode1, eventTypeNode2, eventTypeNode3;
326 rootNode = fStatsTree.getRootNode();
327 traceNode = fStatsTree.getNode(fTestName);
328 catNode = traceNode.getChildren().iterator().next();
329 eventTypeNode1 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
330 eventTypeNode2 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId2);
331 eventTypeNode3 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId3);
332
333 rootNode.resetGlobalValue();
334
335 assertEquals(0, rootNode.getValues().getTotal());
336 assertEquals(0, traceNode.getValues().getTotal());
337 assertEquals(0, catNode.getValues().getTotal());
338 assertEquals(0, eventTypeNode1.getValues().getTotal());
339 assertEquals(0, eventTypeNode2.getValues().getTotal());
340 assertEquals(0, eventTypeNode3.getValues().getTotal());
341
342 // Checks the state of the statistics tree
343 Collection<TmfStatisticsTreeNode> rootChildren = rootNode.getChildren();
344 assertEquals(1, rootChildren.size());
345 assertTrue(rootChildren.contains(traceNode));
346
347 Collection<TmfStatisticsTreeNode> traceChildren = traceNode.getChildren();
348 assertEquals(1, traceChildren.size());
349 assertTrue(traceChildren.contains(catNode));
350
351 Collection<TmfStatisticsTreeNode> catChildren = catNode.getChildren();
352 assertEquals(3, catChildren.size());
353 assertTrue(catChildren.contains(eventTypeNode1));
354 assertTrue(catChildren.contains(eventTypeNode2));
355 assertTrue(catChildren.contains(eventTypeNode3));
356 }
357
358 /**
359 * Test reset time range value of the node in the tree. It should only clear
360 * the time range value without removing any node from the tree.
361 */
362 public void testResetTimeRangeValue() {
363 TmfStatisticsTreeNode rootNode, traceNode, catNode, eventTypeNode1, eventTypeNode2, eventTypeNode3;
364 rootNode = fStatsTree.getRootNode();
365 traceNode = fStatsTree.getNode(fTestName);
366 catNode = traceNode.getChildren().iterator().next();
367 eventTypeNode1 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
368 eventTypeNode2 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId2);
369 eventTypeNode3 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId3);
370
371 rootNode.resetTimeRangeValue();
372
373 assertEquals(0, rootNode.getValues().getPartial());
374 assertEquals(0, traceNode.getValues().getPartial());
375 assertEquals(0, catNode.getValues().getPartial());
376 assertEquals(0, eventTypeNode1.getValues().getPartial());
377 assertEquals(0, eventTypeNode2.getValues().getPartial());
378
379 // Checks the state of the statistics tree
380 Collection<TmfStatisticsTreeNode> rootChildren = rootNode.getChildren();
381 assertEquals(1, rootChildren.size());
382 assertTrue(rootChildren.contains(traceNode));
383
384 Collection<TmfStatisticsTreeNode> traceChildren = traceNode.getChildren();
385 assertEquals(1, traceChildren.size());
386 assertTrue(traceChildren.contains(catNode));
387
388 Collection<TmfStatisticsTreeNode> catChildren = catNode.getChildren();
389 assertEquals(3, catChildren.size());
390 assertTrue(catChildren.contains(eventTypeNode1));
391 assertTrue(catChildren.contains(eventTypeNode2));
392 assertTrue(catChildren.contains(eventTypeNode3));
393 }
394
395 /**
396 * Check if two String arrays are equals, by comparing their contents.
397 * Unlike Arrays.equals(), we can use varargs for the second argument.
398 */
399 private static boolean arraysEqual(String[] array1, String... array2) {
400 if (array1.length != array2.length) {
401 return false;
402 }
403 for (int i = 0; i < array1.length; i++) {
404 if (!array1[i].equals(array2[i])) {
405 return false;
406 }
407 }
408 return true;
409 }
410 }
This page took 0.047103 seconds and 6 git commands to generate.