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
.core
.event
.ITmfEventField
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
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
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.AbsTmfStatisticsTree
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.Messages
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.TmfBaseStatisticsTree
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.TmfStatisticsTreeNode
;
34 * TmfStatisticsTreeNode Test Cases.
36 @SuppressWarnings("nls")
37 public class TmfStatisticsTreeNodeTest
extends TestCase
{
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
42 private String fTestName
= null;
45 private final String fContext
= "UnitTest";
46 private final String fTypeId1
= "Some type1";
47 private final String fTypeId2
= "Some type2";
49 private final String fLabel0
= "label1";
50 private final String fLabel1
= "label2";
51 private final String fLabel2
= "label3";
52 private final String
[] fLabels
= new String
[] { fLabel0
, fLabel1
, fLabel2
};
54 private final TmfTimestamp fTimestamp1
= new TmfTimestamp(12345, (byte) 2, 5);
55 private final TmfTimestamp fTimestamp2
= new TmfTimestamp(12350, (byte) 2, 5);
56 private final TmfTimestamp fTimestamp3
= new TmfTimestamp(12355, (byte) 2, 5);
58 private final String fSource
= "Source";
60 private final TmfEventType fType1
= new TmfEventType(fContext
, fTypeId1
, TmfEventField
.makeRoot(fLabels
));
61 private final TmfEventType fType2
= new TmfEventType(fContext
, fTypeId2
, TmfEventField
.makeRoot(fLabels
));
63 private final String fReference
= "Some reference";
65 private final TmfEvent fEvent1
;
66 private final TmfEvent fEvent2
;
67 private final TmfEvent fEvent3
;
69 private final TmfEventField fContent1
;
70 private final TmfEventField fContent2
;
71 private final TmfEventField fContent3
;
73 private final TmfBaseStatisticsTree fStatsData
;
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
83 public TmfStatisticsTreeNodeTest(final String name
) {
88 fContent1
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some content");
89 fEvent1
= new TmfEvent(null, fTimestamp1
, fSource
, fType1
, fContent1
, fReference
);
91 fContent2
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some other content");
92 fEvent2
= new TmfEvent(null, fTimestamp2
, fSource
, fType1
, fContent2
, fReference
);
94 fContent3
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some other different content");
95 fEvent3
= new TmfEvent(null, fTimestamp3
, fSource
, fType2
, fContent3
, fReference
);
97 fStatsData
= new TmfBaseStatisticsTree();
99 fStatsData
.setTotal(fTestName
, true, 9);
100 fStatsData
.setTypeCount(fTestName
, fEvent1
.getType().getName(), true, 2);
101 fStatsData
.setTypeCount(fTestName
, fEvent2
.getType().getName(), true, 3);
102 fStatsData
.setTypeCount(fTestName
, fEvent3
.getType().getName(), true, 4);
104 // Registers some events in time range
105 fStatsData
.setTotal(fTestName
, false, 9);
106 fStatsData
.setTypeCount(fTestName
, fEvent1
.getType().getName(), false, 2);
107 fStatsData
.setTypeCount(fTestName
, fEvent2
.getType().getName(), false, 3);
108 fStatsData
.setTypeCount(fTestName
, fEvent3
.getType().getName(), false, 4);
111 // ------------------------------------------------------------------------
113 // ------------------------------------------------------------------------
116 * Test checking for child.
118 public void testContainsChild() {
119 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
120 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
121 // Creates a category from the key already created
122 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
124 assertTrue("containsChild", rootNode
.containsChild(fTestName
));
125 assertFalse("containsChild", rootNode
.containsChild(catNode
.getKey()));
126 assertFalse("containsChild", rootNode
.containsChild(null));
128 assertTrue("containsChild", traceNode
.containsChild(catNode
.getKey()));
129 assertFalse("containsChild", traceNode
.containsChild(fType1
.getName()));
130 assertFalse("containsChild", traceNode
.containsChild(null));
132 assertTrue("containsChild", catNode
.containsChild(fType1
.getName()));
133 assertTrue("containsChild", catNode
.containsChild(fType2
.getName()));
134 assertFalse("containsChild", catNode
.containsChild(null));
137 // ------------------------------------------------------------------------
139 // ------------------------------------------------------------------------
142 * Test getting of children.
144 public void testGetChildren() {
145 // Getting children of the ROOT
146 Collection
<TmfStatisticsTreeNode
> childrenTreeNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
).getChildren();
147 assertEquals("getChildren", 1, childrenTreeNode
.size());
148 TmfStatisticsTreeNode treeNode
= childrenTreeNode
.iterator().next();
149 assertEquals("getChildren", fTestName
, treeNode
.getKey());
151 // Getting children of the trace
152 childrenTreeNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
)).getChildren();
153 assertEquals("getChildren", 1, childrenTreeNode
.size());
154 treeNode
= childrenTreeNode
.iterator().next();
155 assertEquals("getChildren", Messages
.TmfStatisticsData_EventTypes
, treeNode
.getKey());
157 Vector
<String
> keyExpected
= new Vector
<String
>();
158 keyExpected
.add(fType1
.getName());
159 keyExpected
.add(fType2
.getName());
160 // Getting children of a category
161 childrenTreeNode
= treeNode
.getChildren();
162 assertEquals("getChildren", 2, childrenTreeNode
.size());
164 Iterator
<TmfStatisticsTreeNode
> iterChild
= childrenTreeNode
.iterator();
165 TmfStatisticsTreeNode temp
;
166 while (iterChild
.hasNext()) {
167 temp
= iterChild
.next();
168 if (keyExpected
.contains(temp
.getKey())) {
169 keyExpected
.removeElement(temp
.getKey());
175 // Get children of a specific event type
176 childrenTreeNode
= fStatsData
.get(childrenTreeNode
.iterator().next().getPath()).getChildren();
177 assertEquals("getChildren", 0, childrenTreeNode
.size());
180 // ------------------------------------------------------------------------
182 // ------------------------------------------------------------------------
185 * Test getting of all children.
187 public void testGetAllChildren() {
188 // Getting children of the ROOT
189 Collection
<TmfStatisticsTreeNode
> childrenTreeNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
).getAllChildren();
190 assertEquals("getChildren", 1, childrenTreeNode
.size());
191 TmfStatisticsTreeNode treeNode
= childrenTreeNode
.iterator().next();
192 assertEquals("getChildren", fTestName
, treeNode
.getKey());
194 // Getting children of the trace
195 childrenTreeNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
)).getAllChildren();
196 assertEquals("getChildren", 1, childrenTreeNode
.size());
197 treeNode
= childrenTreeNode
.iterator().next();
198 assertEquals("getChildren", Messages
.TmfStatisticsData_EventTypes
, treeNode
.getKey());
200 Vector
<String
> keyExpected
= new Vector
<String
>();
201 keyExpected
.add(fType1
.getName());
202 keyExpected
.add(fType2
.getName());
204 * It should return the eventType even though the number of events
207 fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName())).reset();
208 // Getting children of a category
209 childrenTreeNode
= treeNode
.getAllChildren();
210 assertEquals("getChildren", 2, childrenTreeNode
.size());
212 Iterator
<TmfStatisticsTreeNode
> iterChild
= childrenTreeNode
.iterator();
213 TmfStatisticsTreeNode temp
;
214 while (iterChild
.hasNext()) {
215 temp
= iterChild
.next();
216 if (keyExpected
.contains(temp
.getKey())) {
217 keyExpected
.removeElement(temp
.getKey());
223 // Get children of a specific event type
224 childrenTreeNode
= fStatsData
.get(childrenTreeNode
.iterator().next().getPath()).getAllChildren();
225 assertEquals("getChildren", 0, childrenTreeNode
.size());
228 // ------------------------------------------------------------------------
230 // ------------------------------------------------------------------------
233 * Test getting of number of children.
235 public void testGetNbChildren() {
236 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
237 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
238 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
239 TmfStatisticsTreeNode elementNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName()));
241 assertEquals("getNbChildren", 1, rootNode
.getNbChildren());
242 assertEquals("getNbChildren", 1, traceNode
.getNbChildren());
243 assertEquals("getNbChildren", 2, catNode
.getNbChildren());
244 assertEquals("getNbChildren", 0, elementNode
.getNbChildren());
247 // ------------------------------------------------------------------------
249 // ------------------------------------------------------------------------
252 * Test checking for children.
254 public void testHasChildren() {
255 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
256 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
257 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
258 TmfStatisticsTreeNode elementNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName()));
260 assertTrue("hasChildren", rootNode
.hasChildren());
261 assertTrue("hasChildren", traceNode
.hasChildren());
262 assertTrue("hasChildren", catNode
.hasChildren());
263 assertFalse("hasChildren", elementNode
.hasChildren());
266 // ------------------------------------------------------------------------
268 // ------------------------------------------------------------------------
271 * Test getting of parent.
273 public void testGetParent() {
274 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
275 TmfStatisticsTreeNode parentNode
= rootNode
.getParent();
276 assertNull("getParent", parentNode
);
278 TmfStatisticsTreeNode newTraceNode
= new TmfStatisticsTreeNode(new TmfFixedArray
<String
>("newly created trace node"), fStatsData
);
279 parentNode
= newTraceNode
.getParent();
280 assertNotNull("getParent", parentNode
);
281 assertEquals("getParent", 0, parentNode
.getKey().compareTo(fStatsData
.get(AbsTmfStatisticsTree
.ROOT
).getKey().toString()));
283 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
284 parentNode
= traceNode
.getParent();
285 assertNotNull("getParent", parentNode
);
286 assertEquals("getParent", 0, parentNode
.getPath().toString().compareTo(AbsTmfStatisticsTree
.ROOT
.toString()));
288 TmfStatisticsTreeNode newNode
= new TmfStatisticsTreeNode(new TmfFixedArray
<String
>("TreeNode", Messages
.TmfStatisticsData_EventTypes
, "TreeNode that should not exist"), fStatsData
);
289 parentNode
= newNode
.getParent();
290 assertNull("getParent", parentNode
);
292 TmfStatisticsTreeNode elementNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName()));
293 parentNode
= elementNode
.getParent();
294 assertNull("getParent", parentNode
);
296 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
297 parentNode
= catNode
.getParent();
298 assertNotNull("getParent", parentNode
);
299 assertEquals("getParent", 0, parentNode
.getPath().toString().compareTo(fStatsData
.get(new TmfFixedArray
<String
>(fTestName
)).getPath().toString()));
301 parentNode
= elementNode
.getParent();
302 assertNotNull("getParent", parentNode
);
303 assertTrue("getParent", parentNode
.getPath().equals(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
)));
306 // ------------------------------------------------------------------------
308 // ------------------------------------------------------------------------
311 * Test getting of key.
313 public void testGetKey() {
314 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
315 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
316 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
317 TmfStatisticsTreeNode elementNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName()));
319 assertEquals("getKey", 0, rootNode
.getKey().compareTo(AbsTmfStatisticsTree
.ROOT
.get(0)));
320 assertEquals("getKey", 0, traceNode
.getKey().compareTo(fTestName
));
321 assertEquals("getKey", 0, catNode
.getKey().compareTo(Messages
.TmfStatisticsData_EventTypes
));
322 assertEquals("getKey", 0, elementNode
.getKey().compareTo(fType1
.getName()));
325 // ------------------------------------------------------------------------
327 // ------------------------------------------------------------------------
330 * Test getting of path to node.
332 public void testGetPath() {
333 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
334 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
335 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
336 TmfStatisticsTreeNode elementNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName()));
338 assertTrue("getPath", rootNode
.getPath().equals(AbsTmfStatisticsTree
.ROOT
));
339 assertTrue("getPath", traceNode
.getPath().equals(new TmfFixedArray
<String
>(fTestName
)));
340 assertTrue("getPath", catNode
.getPath().equals(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
)));
341 assertTrue("getPath", elementNode
.getPath().equals(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName())));
344 // ------------------------------------------------------------------------
346 // ------------------------------------------------------------------------
349 * Test getting statistic value.
351 public void testGetValue() {
352 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
353 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
354 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
355 TmfStatisticsTreeNode elementNode1
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName()));
356 TmfStatisticsTreeNode elementNode3
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType2
.getName()));
358 assertEquals("getValue", 0, rootNode
.getValues().getTotal());
359 assertEquals("getValue", 9, traceNode
.getValues().getTotal());
360 assertEquals("getValue", 0, catNode
.getValues().getTotal());
361 assertEquals("getValue", 3, elementNode1
.getValues().getTotal());
362 assertEquals("getValue", 4, elementNode3
.getValues().getTotal());
364 assertEquals("getValue", 0, rootNode
.getValues().getPartial());
365 assertEquals("getValue", 9, traceNode
.getValues().getPartial());
366 assertEquals("getValue", 0, catNode
.getValues().getPartial());
367 assertEquals("getValue", 3, elementNode1
.getValues().getPartial());
368 assertEquals("getValue", 4, elementNode3
.getValues().getPartial());
371 // ------------------------------------------------------------------------
373 // ------------------------------------------------------------------------
376 * Test reset of tree.
378 public void testReset() {
379 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
380 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
381 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
382 TmfStatisticsTreeNode elementNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName()));
385 assertEquals("reset", 0, elementNode
.getValues().getTotal());
386 assertEquals("reset", 0, elementNode
.getValues().getPartial());
389 assertEquals("reset", 0, catNode
.getValues().getTotal());
390 assertEquals("reset", 0, catNode
.getValues().getPartial());
391 assertEquals("reset", 0, catNode
.getNbChildren());
392 assertNull("reset", fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName())));
395 assertEquals("reset", 0, traceNode
.getValues().getTotal());
396 assertEquals("reset", 0, traceNode
.getValues().getPartial());
397 // A trace always have at least one child that is eventType
398 assertEquals("reset", 1, traceNode
.getNbChildren());
401 assertEquals("reset", 0, rootNode
.getValues().getTotal());
402 assertEquals("reset", 0, rootNode
.getValues().getPartial());
403 assertEquals("reset", 1, rootNode
.getNbChildren());
407 * Test reset global value of the node in the tree. It should only clear
408 * the global value without removing any node from the tree.
410 public void testResetGlobalValue() {
411 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
412 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
413 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
414 TmfStatisticsTreeNode eventTypeNode1
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName()));
415 TmfStatisticsTreeNode eventTypeNode2
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType2
.getName()));
417 rootNode
.resetGlobalValue();
419 assertEquals(0, rootNode
.getValues().getTotal());
420 assertEquals(0, traceNode
.getValues().getTotal());
421 assertEquals(0, catNode
.getValues().getTotal());
422 assertEquals(0, eventTypeNode1
.getValues().getTotal());
423 assertEquals(0, eventTypeNode2
.getValues().getTotal());
425 // Checks the state of the statistics tree
426 Collection
<TmfStatisticsTreeNode
> rootChildren
= rootNode
.getAllChildren();
427 assertEquals(1, rootChildren
.size());
428 assertTrue(rootChildren
.contains(traceNode
));
430 Collection
<TmfStatisticsTreeNode
> traceChildren
= traceNode
.getAllChildren();
431 assertEquals(1, traceChildren
.size());
432 assertTrue(traceChildren
.contains(catNode
));
434 Collection
<TmfStatisticsTreeNode
> catChildren
= catNode
.getAllChildren();
435 assertEquals(2, catChildren
.size());
436 assertTrue(catChildren
.contains(eventTypeNode1
));
437 assertTrue(catChildren
.contains(eventTypeNode2
));
441 * Test reset time range value of the node in the tree. It should only clear
442 * the time range value without removing any node from the tree.
444 public void testResetTimeRangeValue() {
445 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
446 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
447 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
448 TmfStatisticsTreeNode eventTypeNode1
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName()));
449 TmfStatisticsTreeNode eventTypeNode2
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType2
.getName()));
451 rootNode
.resetTimeRangeValue();
453 assertEquals(0, rootNode
.getValues().getPartial());
454 assertEquals(0, traceNode
.getValues().getPartial());
455 assertEquals(0, catNode
.getValues().getPartial());
456 assertEquals(0, eventTypeNode1
.getValues().getPartial());
457 assertEquals(0, eventTypeNode2
.getValues().getPartial());
459 // Checks the state of the statistics tree
460 Collection
<TmfStatisticsTreeNode
> rootChildren
= rootNode
.getAllChildren();
461 assertEquals(1, rootChildren
.size());
462 assertTrue(rootChildren
.contains(traceNode
));
464 Collection
<TmfStatisticsTreeNode
> traceChildren
= traceNode
.getAllChildren();
465 assertEquals(1, traceChildren
.size());
466 assertTrue(traceChildren
.contains(catNode
));
468 Collection
<TmfStatisticsTreeNode
> catChildren
= catNode
.getAllChildren();
469 assertEquals(2, catChildren
.size());
470 assertTrue(catChildren
.contains(eventTypeNode1
));
471 assertTrue(catChildren
.contains(eventTypeNode2
));