1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
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
10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Initial design and implementation
11 * Bernd Hufmann - Fixed warnings
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.tests
.statistics
;
16 import java
.util
.Collection
;
17 import java
.util
.Iterator
;
18 import java
.util
.Vector
;
20 import junit
.framework
.TestCase
;
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
;
27 * TmfStatisticsTreeNode Test Cases.
29 public class TmfStatisticsTreeNodeTest
extends TestCase
{
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
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$
39 private final TmfStatisticsTree fStatsTree
;
41 private String fTestName
= null;
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
51 public TmfStatisticsTreeNodeTest(final String name
) {
55 fStatsTree
= new TmfStatisticsTree();
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);
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);
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 * Test checking for child.
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();
83 assertTrue(rootNode
.containsChild(fTestName
));
84 assertFalse(rootNode
.containsChild(catNode
.getName()));
85 assertFalse(rootNode
.containsChild(null));
87 assertTrue(traceNode
.containsChild(catNode
.getName()));
88 assertFalse(traceNode
.containsChild(fTypeId1
));
89 assertFalse(traceNode
.containsChild(null));
91 assertTrue(catNode
.containsChild(fTypeId1
));
92 assertTrue(catNode
.containsChild(fTypeId2
));
93 assertFalse(catNode
.containsChild(null));
96 // ------------------------------------------------------------------------
98 // ------------------------------------------------------------------------
101 * Test getting of children.
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());
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());
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());
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());
135 // Get children of a specific event type
136 childrenTreeNode
= fStatsTree
.getNode(childrenTreeNode
.iterator().next().getPath()).getChildren();
137 assertEquals(0, childrenTreeNode
.size());
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
145 * Test getting of number of children.
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
);
153 assertEquals(1, rootNode
.getNbChildren());
154 assertEquals(1, traceNode
.getNbChildren());
155 assertEquals(3, catNode
.getNbChildren());
156 assertEquals(0, elementNode
.getNbChildren());
159 // ------------------------------------------------------------------------
161 // ------------------------------------------------------------------------
164 * Test checking for children.
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
);
172 assertTrue(rootNode
.hasChildren());
173 assertTrue(traceNode
.hasChildren());
174 assertTrue(catNode
.hasChildren());
175 assertFalse(elementNode
.hasChildren());
178 // ------------------------------------------------------------------------
180 // ------------------------------------------------------------------------
183 * Test getting of parent.
185 public void testGetParent() {
186 final TmfStatisticsTreeNode rootNode
= fStatsTree
.getRootNode();
187 TmfStatisticsTreeNode parentNode
= rootNode
.getParent();
188 assertNull(parentNode
);
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
);
195 TmfStatisticsTreeNode traceNode
= fStatsTree
.getNode(fTestName
);
196 parentNode
= traceNode
.getParent();
197 assertNotNull(parentNode
);
198 assertTrue(rootNode
== parentNode
);
200 TmfStatisticsTreeNode elementNode
= fStatsTree
.getNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fTypeId1
);
201 parentNode
= elementNode
.getParent();
202 assertTrue(parentNode
== fStatsTree
.getNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
));
204 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
205 parentNode
= catNode
.getParent();
206 assertNotNull(parentNode
);
207 assertTrue(parentNode
== fStatsTree
.getNode(fTestName
));
209 parentNode
= elementNode
.getParent();
210 assertNotNull(parentNode
);
211 assertTrue(arraysEqual(parentNode
.getPath(), fTestName
, Messages
.TmfStatisticsData_EventTypes
));
214 // ------------------------------------------------------------------------
216 // ------------------------------------------------------------------------
219 * Test getting of key.
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
);
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
));
233 // ------------------------------------------------------------------------
235 // ------------------------------------------------------------------------
238 * Test getting of path to node.
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
);
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
));
254 // ------------------------------------------------------------------------
256 // ------------------------------------------------------------------------
259 * Test getting statistic value.
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
);
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());
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());
285 // ------------------------------------------------------------------------
287 // ------------------------------------------------------------------------
290 * Test reset of tree.
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
);
300 assertEquals(0, elementNode
.getValues().getTotal());
301 assertEquals(0, elementNode
.getValues().getPartial());
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
));
310 assertEquals(0, traceNode
.getValues().getTotal());
311 assertEquals(0, traceNode
.getValues().getPartial());
312 assertEquals(0, traceNode
.getNbChildren());
315 assertEquals(0, rootNode
.getValues().getTotal());
316 assertEquals(0, rootNode
.getValues().getPartial());
317 assertEquals(0, rootNode
.getNbChildren());
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.
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
);
333 rootNode
.resetGlobalValue();
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());
342 // Checks the state of the statistics tree
343 Collection
<TmfStatisticsTreeNode
> rootChildren
= rootNode
.getChildren();
344 assertEquals(1, rootChildren
.size());
345 assertTrue(rootChildren
.contains(traceNode
));
347 Collection
<TmfStatisticsTreeNode
> traceChildren
= traceNode
.getChildren();
348 assertEquals(1, traceChildren
.size());
349 assertTrue(traceChildren
.contains(catNode
));
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
));
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.
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
);
371 rootNode
.resetTimeRangeValue();
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());
379 // Checks the state of the statistics tree
380 Collection
<TmfStatisticsTreeNode
> rootChildren
= rootNode
.getChildren();
381 assertEquals(1, rootChildren
.size());
382 assertTrue(rootChildren
.contains(traceNode
));
384 Collection
<TmfStatisticsTreeNode
> traceChildren
= traceNode
.getChildren();
385 assertEquals(1, traceChildren
.size());
386 assertTrue(traceChildren
.contains(catNode
));
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
));
396 * Check if two String arrays are equals, by comparing their contents.
397 * Unlike Arrays.equals(), we can use varargs for the second argument.
399 private static boolean arraysEqual(String
[] array1
, String
... array2
) {
400 if (array1
.length
!= array2
.length
) {
403 for (int i
= 0; i
< array1
.length
; i
++) {
404 if (!array1
[i
].equals(array2
[i
])) {