tmf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.tests / src / org / eclipse / tracecompass / tmf / ui / tests / statistics / TmfStatisticsTreeNodeTest.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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 * Alexandre Montplaisir - Port to JUnit4
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.ui.tests.statistics;
16
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.Vector;
27
28 import org.eclipse.tracecompass.tmf.ui.viewers.statistics.model.Messages;
29 import org.eclipse.tracecompass.tmf.ui.viewers.statistics.model.TmfStatisticsTree;
30 import org.eclipse.tracecompass.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode;
31 import org.junit.Test;
32
33 /**
34 * TmfStatisticsTreeNode Test Cases.
35 */
36 public class TmfStatisticsTreeNodeTest {
37
38 // ------------------------------------------------------------------------
39 // Fields
40 // ------------------------------------------------------------------------
41
42 private final String fTypeId1 = "Some type1";
43 private final String fTypeId2 = "Some type2";
44 private final String fTypeId3 = "Some type3";
45
46 private final TmfStatisticsTree fStatsTree;
47
48 private static final String fTestName = "StatisticsTreeNodeTest";
49
50 // ------------------------------------------------------------------------
51 // Housekeeping
52 // ------------------------------------------------------------------------
53
54 /**
55 * Constructor
56 */
57 public TmfStatisticsTreeNodeTest() {
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);
71 }
72
73 /**
74 * Test checking for child.
75 */
76 @Test
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
86 assertTrue(traceNode.containsChild(catNode.getName()));
87 assertFalse(traceNode.containsChild(fTypeId1));
88
89 assertTrue(catNode.containsChild(fTypeId1));
90 assertTrue(catNode.containsChild(fTypeId2));
91 }
92
93 /**
94 * Test getting of children.
95 */
96 @Test
97 public void testGetChildren() {
98 // Getting children of the ROOT
99 Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsTree.getRootNode().getChildren();
100 assertEquals(1, childrenTreeNode.size());
101 TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next();
102 assertEquals(fTestName, treeNode.getName());
103
104 // Getting children of the trace
105 childrenTreeNode = fStatsTree.getNode(fTestName).getChildren();
106 assertEquals(1, childrenTreeNode.size());
107 treeNode = childrenTreeNode.iterator().next();
108 assertEquals(Messages.TmfStatisticsData_EventTypes, treeNode.getName());
109
110 Vector<String> keyExpected = new Vector<>();
111 keyExpected.add(fTypeId1);
112 keyExpected.add(fTypeId2);
113 keyExpected.add(fTypeId3);
114 // Getting children of a category
115 childrenTreeNode = treeNode.getChildren();
116 assertEquals(3, childrenTreeNode.size());
117
118 Iterator<TmfStatisticsTreeNode> iterChild = childrenTreeNode.iterator();
119 TmfStatisticsTreeNode temp;
120 while (iterChild.hasNext()) {
121 temp = iterChild.next();
122 if (keyExpected.contains(temp.getName())) {
123 keyExpected.removeElement(temp.getName());
124 } else {
125 fail();
126 }
127 }
128
129 // Get children of a specific event type
130 childrenTreeNode = fStatsTree.getNode(childrenTreeNode.iterator().next().getPath()).getChildren();
131 assertEquals(0, childrenTreeNode.size());
132 }
133
134 /**
135 * Test getting of number of children.
136 */
137 @Test
138 public void testGetNbChildren() {
139 TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode();
140 TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName);
141 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
142 TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
143
144 assertEquals(1, rootNode.getNbChildren());
145 assertEquals(1, traceNode.getNbChildren());
146 assertEquals(3, catNode.getNbChildren());
147 assertEquals(0, elementNode.getNbChildren());
148 }
149
150 /**
151 * Test checking for children.
152 */
153 @Test
154 public void testHasChildren() {
155 TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode();
156 TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName);
157 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
158 TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
159
160 assertTrue(rootNode.hasChildren());
161 assertTrue(traceNode.hasChildren());
162 assertTrue(catNode.hasChildren());
163 assertFalse(elementNode.hasChildren());
164 }
165
166 /**
167 * Test getting of parent.
168 */
169 @Test
170 public void testGetParent() {
171 final TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode();
172 TmfStatisticsTreeNode parentNode = rootNode.getParent();
173 assertNull(parentNode);
174
175 TmfStatisticsTreeNode newTraceNode = new TmfStatisticsTreeNode(fStatsTree, rootNode, "newly created trace node");
176 parentNode = newTraceNode.getParent();
177 assertNotNull(parentNode);
178 assertTrue(fStatsTree.getRootNode() == parentNode);
179
180 TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName);
181 parentNode = traceNode.getParent();
182 assertNotNull(parentNode);
183 assertTrue(rootNode == parentNode);
184
185 TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
186 parentNode = elementNode.getParent();
187 assertTrue(parentNode == fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes));
188
189 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
190 parentNode = catNode.getParent();
191 assertNotNull(parentNode);
192 assertTrue(parentNode == fStatsTree.getNode(fTestName));
193
194 parentNode = elementNode.getParent();
195 assertNotNull(parentNode);
196 assertTrue(arraysEqual(parentNode.getPath(), fTestName, Messages.TmfStatisticsData_EventTypes));
197 }
198
199 /**
200 * Test getting of key.
201 */
202 @Test
203 public void testgetName() {
204 TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode();
205 TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName);
206 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
207 TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
208
209 assertEquals(0, rootNode.getName().compareTo("root"));
210 assertEquals(0, traceNode.getName().compareTo(fTestName));
211 assertEquals(0, catNode.getName().compareTo(Messages.TmfStatisticsData_EventTypes));
212 assertEquals(0, elementNode.getName().compareTo(fTypeId1));
213 }
214
215 /**
216 * Test getting of path to node.
217 */
218 @Test
219 public void testGetPath() {
220 TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode();
221 TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName);
222 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
223 TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1);
224
225 assertEquals(0, rootNode.getPath().length); /* Root node has an empty path */
226 assertTrue(arraysEqual(traceNode.getPath(), fTestName));
227 assertTrue(arraysEqual(catNode.getPath(),
228 fTestName, Messages.TmfStatisticsData_EventTypes));
229 assertTrue(arraysEqual(elementNode.getPath(),
230 fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1));
231 }
232
233 /**
234 * Test getting statistic value.
235 */
236 @Test
237 public void testGetValue() {
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());
259 }
260
261 /**
262 * Test reset of tree.
263 */
264 @Test
265 public void testReset() {
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);
271
272 elementNode.reset();
273 assertEquals(0, elementNode.getValues().getTotal());
274 assertEquals(0, elementNode.getValues().getPartial());
275
276 catNode.reset();
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));
281
282 traceNode.reset();
283 assertEquals(0, traceNode.getValues().getTotal());
284 assertEquals(0, traceNode.getValues().getPartial());
285 assertEquals(0, traceNode.getNbChildren());
286
287 rootNode.reset();
288 assertEquals(0, rootNode.getValues().getTotal());
289 assertEquals(0, rootNode.getValues().getPartial());
290 assertEquals(0, rootNode.getNbChildren());
291 }
292
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 */
297 @Test
298 public void testResetGlobalValue() {
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);
306
307 rootNode.resetGlobalValue();
308
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());
314 assertEquals(0, eventTypeNode3.getValues().getTotal());
315
316 // Checks the state of the statistics tree
317 Collection<TmfStatisticsTreeNode> rootChildren = rootNode.getChildren();
318 assertEquals(1, rootChildren.size());
319 assertTrue(rootChildren.contains(traceNode));
320
321 Collection<TmfStatisticsTreeNode> traceChildren = traceNode.getChildren();
322 assertEquals(1, traceChildren.size());
323 assertTrue(traceChildren.contains(catNode));
324
325 Collection<TmfStatisticsTreeNode> catChildren = catNode.getChildren();
326 assertEquals(3, catChildren.size());
327 assertTrue(catChildren.contains(eventTypeNode1));
328 assertTrue(catChildren.contains(eventTypeNode2));
329 assertTrue(catChildren.contains(eventTypeNode3));
330 }
331
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 */
336 @Test
337 public void testResetTimeRangeValue() {
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);
345
346 rootNode.resetTimeRangeValue();
347
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());
353
354 // Checks the state of the statistics tree
355 Collection<TmfStatisticsTreeNode> rootChildren = rootNode.getChildren();
356 assertEquals(1, rootChildren.size());
357 assertTrue(rootChildren.contains(traceNode));
358
359 Collection<TmfStatisticsTreeNode> traceChildren = traceNode.getChildren();
360 assertEquals(1, traceChildren.size());
361 assertTrue(traceChildren.contains(catNode));
362
363 Collection<TmfStatisticsTreeNode> catChildren = catNode.getChildren();
364 assertEquals(3, catChildren.size());
365 assertTrue(catChildren.contains(eventTypeNode1));
366 assertTrue(catChildren.contains(eventTypeNode2));
367 assertTrue(catChildren.contains(eventTypeNode3));
368 }
369
370 /**
371 * Check if two String arrays are equals, by comparing their contents.
372 * Unlike Arrays.equals(), we can use varargs for the second argument.
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 }
385 }
This page took 0.044216 seconds and 5 git commands to generate.