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