Change type of event source from TmfEventSource to String
[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.core.event.TmfEvent;
23 import org.eclipse.linuxtools.tmf.core.event.TmfEventContent;
24 import org.eclipse.linuxtools.tmf.core.event.TmfEventReference;
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.views.statistics.ITmfExtraEventInfo;
29 import org.eclipse.linuxtools.tmf.ui.views.statistics.model.AbsTmfStatisticsTree;
30 import org.eclipse.linuxtools.tmf.ui.views.statistics.model.Messages;
31 import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseStatisticsTree;
32 import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsTreeNode;
33
34 @SuppressWarnings("nls")
35 public class TmfStatisticsTreeNodeTest extends TestCase {
36
37 // ------------------------------------------------------------------------
38 // Fields
39 // ------------------------------------------------------------------------
40 private String fTestName = null;
41
42
43 private final String fTypeId1 = "Some type1";
44 private final String fTypeId2 = "Some type2";
45
46 private final String fLabel0 = "label1";
47 private final String fLabel1 = "label2";
48 private final String fLabel2 = "label3";
49 private final String[] fLabels = new String[] { fLabel0, fLabel1, fLabel2 };
50
51 private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5);
52 private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5);
53 private final TmfTimestamp fTimestamp3 = new TmfTimestamp(12355, (byte) 2, 5);
54
55 private final String fSource = "Source";
56
57 private final TmfEventType fType1 = new TmfEventType(fTypeId1, fLabels);
58 private final TmfEventType fType2 = new TmfEventType(fTypeId1, fLabels);
59 private final TmfEventType fType3 = new TmfEventType(fTypeId2, fLabels);
60
61 private final TmfEventReference fReference = new TmfEventReference("Some reference");
62
63 private final TmfEvent fEvent1;
64 private final TmfEvent fEvent2;
65 private final TmfEvent fEvent3;
66
67 private final TmfEventContent fContent1;
68 private final TmfEventContent fContent2;
69 private final TmfEventContent fContent3;
70
71 private final TmfBaseStatisticsTree fStatsData;
72
73 private final ITmfExtraEventInfo fExtraInfo;
74
75 // ------------------------------------------------------------------------
76 // Housekeeping
77 // ------------------------------------------------------------------------
78
79 /**
80 * @param name of the test
81 */
82 public TmfStatisticsTreeNodeTest(final String name) {
83 super(name);
84
85 fTestName = name;
86
87 fEvent1 = new TmfEvent(fTimestamp1, fSource, fType1, fReference);
88 fContent1 = new TmfEventContent(fEvent1, "Some content");
89 fEvent1.setContent(fContent1);
90
91 fEvent2 = new TmfEvent(fTimestamp2, fSource, fType2, fReference);
92 fContent2 = new TmfEventContent(fEvent2, "Some other content");
93 fEvent2.setContent(fContent2);
94
95 fEvent3 = new TmfEvent(fTimestamp3, fSource, fType3, fReference);
96 fContent3 = new TmfEventContent(fEvent3, "Some other different content");
97 fEvent3.setContent(fContent3);
98
99 fStatsData = new TmfBaseStatisticsTree();
100 fExtraInfo = new ITmfExtraEventInfo() {
101 @Override
102 public String getTraceName() {
103 return name;
104 }
105 };
106 fStatsData.registerEvent(fEvent1, fExtraInfo);
107 fStatsData.registerEvent(fEvent2, fExtraInfo);
108 fStatsData.registerEvent(fEvent3, fExtraInfo);
109 }
110
111 // ------------------------------------------------------------------------
112 // ContainsChild
113 // ------------------------------------------------------------------------
114
115 public void testContainsChild() {
116 TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT);
117 TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName));
118 // Creates a category from the key already created
119 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
120
121 assertTrue("containsChild", rootNode.containsChild(fTestName));
122 assertFalse("containsChild", rootNode.containsChild(catNode.getKey()));
123 assertFalse("containsChild", rootNode.containsChild(null));
124
125 assertTrue("containsChild", traceNode.containsChild(catNode.getKey()));
126 assertFalse("containsChild", traceNode.containsChild(fEvent1.getType().toString()));
127 assertFalse("containsChild", traceNode.containsChild(null));
128
129 assertTrue("containsChild", catNode.containsChild(fEvent1.getType().toString()));
130 assertTrue("containsChild", catNode.containsChild(fEvent3.getType().toString()));
131 assertFalse("containsChild", catNode.containsChild(null));
132 }
133
134 // ------------------------------------------------------------------------
135 // GetChildren
136 // ------------------------------------------------------------------------
137
138 public void testGetChildren() {
139 // Getting children of the ROOT
140 Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsData.get(AbsTmfStatisticsTree.ROOT).getChildren();
141 assertEquals("getChildren", 1, childrenTreeNode.size());
142 TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next();
143 assertEquals("getChildren", fTestName, treeNode.getKey());
144
145 // Getting children of the trace
146 childrenTreeNode = fStatsData.get(new TmfFixedArray<String>(fTestName)).getChildren();
147 assertEquals("getChildren", 1, childrenTreeNode.size());
148 treeNode = childrenTreeNode.iterator().next();
149 assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey());
150
151 Vector<String> keyExpected = new Vector<String>();
152 keyExpected.add(fEvent1.getType().toString());
153 keyExpected.add(fEvent3.getType().toString());
154 // Getting children of a category
155 childrenTreeNode = treeNode.getChildren();
156 assertEquals("getChildren", 2, childrenTreeNode.size());
157
158 Iterator<TmfStatisticsTreeNode> iterChild = childrenTreeNode.iterator();
159 TmfStatisticsTreeNode temp;
160 while (iterChild.hasNext()) {
161 temp = iterChild.next();
162 if (keyExpected.contains(temp.getKey())) {
163 keyExpected.removeElement(temp.getKey());
164 }
165 else {
166 fail();
167 }
168 }
169
170 // Get children of a specific event type
171 childrenTreeNode = fStatsData.get(childrenTreeNode.iterator().next().getPath()).getChildren();
172 assertEquals("getChildren", 0, childrenTreeNode.size());
173 }
174
175 // ------------------------------------------------------------------------
176 // GetAllChildren
177 // ------------------------------------------------------------------------
178
179 public void testGetAllChildren() {
180 // Getting children of the ROOT
181 Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsData.get(AbsTmfStatisticsTree.ROOT).getAllChildren();
182 assertEquals("getChildren", 1, childrenTreeNode.size());
183 TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next();
184 assertEquals("getChildren", fTestName, treeNode.getKey());
185
186 // Getting children of the trace
187 childrenTreeNode = fStatsData.get(new TmfFixedArray<String>(fTestName)).getAllChildren();
188 assertEquals("getChildren", 1, childrenTreeNode.size());
189 treeNode = childrenTreeNode.iterator().next();
190 assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey());
191
192 Vector<String> keyExpected = new Vector<String>();
193 keyExpected.add(fEvent1.getType().toString());
194 keyExpected.add(fEvent3.getType().toString());
195 // It should return the eventType even though the number of events equals 0
196 fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())).reset();
197 // Getting children of a category
198 childrenTreeNode = treeNode.getAllChildren();
199 assertEquals("getChildren", 2, childrenTreeNode.size());
200
201 Iterator<TmfStatisticsTreeNode> iterChild = childrenTreeNode.iterator();
202 TmfStatisticsTreeNode temp;
203 while (iterChild.hasNext()) {
204 temp = iterChild.next();
205 if (keyExpected.contains(temp.getKey())) {
206 keyExpected.removeElement(temp.getKey());
207 }
208 else {
209 fail();
210 }
211 }
212
213 // Get children of a specific event type
214 childrenTreeNode = fStatsData.get(childrenTreeNode.iterator().next().getPath()).getAllChildren();
215 assertEquals("getChildren", 0, childrenTreeNode.size());
216 }
217
218 // ------------------------------------------------------------------------
219 // GetNbChildren
220 // ------------------------------------------------------------------------
221
222 public void testGetNbChildren() {
223 TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT);
224 TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName));
225 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
226 TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString()));
227
228 assertEquals("getNbChildren", 1, rootNode.getNbChildren());
229 assertEquals("getNbChildren", 1, traceNode.getNbChildren());
230 assertEquals("getNbChildren", 2, catNode.getNbChildren());
231 assertEquals("getNbChildren", 0, elementNode.getNbChildren());
232 }
233
234 // ------------------------------------------------------------------------
235 // HasChildren
236 // ------------------------------------------------------------------------
237
238 public void testHasChildren() {
239 TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT);
240 TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName));
241 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
242 TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString()));
243
244 assertTrue ("hasChildren", rootNode.hasChildren());
245 assertTrue ("hasChildren", traceNode.hasChildren());
246 assertTrue ("hasChildren", catNode.hasChildren());
247 assertFalse("hasChildren", elementNode.hasChildren());
248 }
249
250 // ------------------------------------------------------------------------
251 // GetParent
252 // ------------------------------------------------------------------------
253
254 public void testGetParent() {
255 TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT);
256 TmfStatisticsTreeNode parentNode = rootNode.getParent();
257 assertNull("getParent", parentNode);
258
259 TmfStatisticsTreeNode newTraceNode = new TmfStatisticsTreeNode(new TmfFixedArray<String>("newly created trace node"), fStatsData);
260 parentNode = newTraceNode.getParent();
261 assertNotNull("getParent", parentNode);
262 assertEquals("getParent", 0, parentNode.getKey().compareTo(fStatsData.get(AbsTmfStatisticsTree.ROOT).getKey().toString()));
263
264 TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName));
265 parentNode = traceNode.getParent();
266 assertNotNull("getParent", parentNode);
267 assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(AbsTmfStatisticsTree.ROOT.toString()));
268
269 TmfStatisticsTreeNode newNode = new TmfStatisticsTreeNode(new TmfFixedArray<String>("TreeNode", Messages.TmfStatisticsData_EventTypes, "TreeNode that should not exist"), fStatsData);
270 parentNode = newNode.getParent();
271 assertNull("getParent", parentNode);
272
273 TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString()));
274 parentNode = elementNode.getParent();
275 assertNull("getParent", parentNode);
276
277 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
278 parentNode = catNode.getParent();
279 assertNotNull("getParent", parentNode);
280 assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(fStatsData.get(new TmfFixedArray<String>(fTestName)).getPath().toString()));
281
282 parentNode = elementNode.getParent();
283 assertNotNull("getParent", parentNode);
284 assertTrue("getParent", parentNode.getPath().equals(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes)));
285 }
286
287 // ------------------------------------------------------------------------
288 // GetKey
289 // ------------------------------------------------------------------------
290
291 public void testGetKey() {
292 TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT);
293 TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName));
294 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
295 TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString()));
296
297 assertEquals("getKey", 0, rootNode.getKey().compareTo(AbsTmfStatisticsTree.ROOT.get(0)));
298 assertEquals("getKey", 0, traceNode.getKey().compareTo(fTestName));
299 assertEquals("getKey", 0, catNode.getKey().compareTo(Messages.TmfStatisticsData_EventTypes));
300 assertEquals("getKey", 0, elementNode.getKey().compareTo(fEvent1.getType().toString()));
301 }
302
303 // ------------------------------------------------------------------------
304 // GetPath
305 // ------------------------------------------------------------------------
306
307 public void testGetPath() {
308 TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT);
309 TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName));
310 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
311 TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString()));
312
313 assertTrue("getPath", rootNode.getPath().equals(AbsTmfStatisticsTree.ROOT));
314 assertTrue("getPath", traceNode.getPath().equals(new TmfFixedArray<String>(fTestName)));
315 assertTrue("getPath", catNode.getPath().equals(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes)));
316 assertTrue("getPath", elementNode.getPath().equals(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())));
317 }
318
319 // ------------------------------------------------------------------------
320 // GetValue
321 // ------------------------------------------------------------------------
322
323 public void testGetValue() {
324 TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT);
325 TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName));
326 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
327 TmfStatisticsTreeNode elementNode1 = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString()));
328 TmfStatisticsTreeNode elementNode2 = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent3.getType().toString()));
329
330 assertEquals("getValue", 0, rootNode.getValue().nbEvents);
331 assertEquals("getValue", 3, traceNode.getValue().nbEvents);
332 assertEquals("getValue", 0, catNode.getValue().nbEvents);
333 assertEquals("getValue", 2, elementNode1.getValue().nbEvents);
334 assertEquals("getValue", 1, elementNode2.getValue().nbEvents);
335 }
336
337 // ------------------------------------------------------------------------
338 // Reset
339 // ------------------------------------------------------------------------
340
341 public void testReset() {
342 TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT);
343 TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray<String>(fTestName));
344 TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next();
345 TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString()));
346
347 elementNode.reset();
348 assertEquals("reset", 0, elementNode.getValue().nbEvents);
349
350 catNode.reset();
351 assertEquals("reset", 0, catNode.getValue().nbEvents);
352 assertEquals("reset", 0, catNode.getNbChildren());
353 assertNull("reset", fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())));
354
355 traceNode.reset();
356 assertEquals("reset", 0, traceNode.getValue().nbEvents);
357 // A trace always have at least one child that is eventType
358 assertEquals("reset", 1, traceNode.getNbChildren());
359
360 rootNode.reset();
361 assertEquals("reset", 0, rootNode.getValue().nbEvents);
362 assertEquals("reset", 1, rootNode.getNbChildren());
363 }
364 }
This page took 0.049707 seconds and 6 git commands to generate.