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
.event
.TmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventContent
;
24 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventReference
;
25 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventSource
;
26 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventType
;
27 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
.ITmfExtraEventInfo
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
.model
.Messages
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
.model
.TmfBaseStatisticsTree
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
.model
.AbsTmfStatisticsTree
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
.model
.TmfStatisticsTreeNode
;
33 import org
.eclipse
.linuxtools
.tmf
.util
.TmfFixedArray
;
35 @SuppressWarnings("nls")
36 public class TmfStatisticsTreeNodeTest
extends TestCase
{
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
41 private String fTestName
= null;
44 private final String fTypeId1
= "Some type1";
45 private final String fTypeId2
= "Some type2";
47 private final String fLabel0
= "label1";
48 private final String fLabel1
= "label2";
49 private final String fLabel2
= "label3";
50 private final String
[] fLabels
= new String
[] { fLabel0
, fLabel1
, fLabel2
};
52 private final TmfTimestamp fTimestamp1
= new TmfTimestamp(12345, (byte) 2, 5);
53 private final TmfTimestamp fTimestamp2
= new TmfTimestamp(12350, (byte) 2, 5);
54 private final TmfTimestamp fTimestamp3
= new TmfTimestamp(12355, (byte) 2, 5);
56 private final TmfEventSource fSource
= new TmfEventSource("Source");
58 private final TmfEventType fType1
= new TmfEventType(fTypeId1
, fLabels
);
59 private final TmfEventType fType2
= new TmfEventType(fTypeId1
, fLabels
);
60 private final TmfEventType fType3
= new TmfEventType(fTypeId2
, fLabels
);
62 private final TmfEventReference fReference
= new TmfEventReference("Some reference");
64 private final TmfEvent fEvent1
;
65 private final TmfEvent fEvent2
;
66 private final TmfEvent fEvent3
;
68 private final TmfEventContent fContent1
;
69 private final TmfEventContent fContent2
;
70 private final TmfEventContent fContent3
;
72 private final TmfBaseStatisticsTree fStatsData
;
74 private final ITmfExtraEventInfo fExtraInfo
;
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
81 * @param name of the test
83 public TmfStatisticsTreeNodeTest(final String name
) {
88 fEvent1
= new TmfEvent(fTimestamp1
, fSource
, fType1
, fReference
);
89 fContent1
= new TmfEventContent(fEvent1
, "Some content");
90 fEvent1
.setContent(fContent1
);
92 fEvent2
= new TmfEvent(fTimestamp1
, fTimestamp2
, fSource
, fType2
, fReference
);
93 fContent2
= new TmfEventContent(fEvent2
, "Some other content");
94 fEvent2
.setContent(fContent2
);
96 fEvent3
= new TmfEvent(fTimestamp2
, fTimestamp3
, fSource
, fType3
, fReference
);
97 fContent3
= new TmfEventContent(fEvent3
, "Some other different content");
98 fEvent3
.setContent(fContent3
);
100 fStatsData
= new TmfBaseStatisticsTree();
101 fExtraInfo
= new ITmfExtraEventInfo() {
103 public String
getTraceName() {
107 fStatsData
.registerEvent(fEvent1
, fExtraInfo
);
108 fStatsData
.registerEvent(fEvent2
, fExtraInfo
);
109 fStatsData
.registerEvent(fEvent3
, fExtraInfo
);
112 // ------------------------------------------------------------------------
114 // ------------------------------------------------------------------------
116 public void testContainsChild() {
117 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
118 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
119 // Creates a category from the key already created
120 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
122 assertTrue("containsChild", rootNode
.containsChild(fTestName
));
123 assertFalse("containsChild", rootNode
.containsChild(catNode
.getKey()));
124 assertFalse("containsChild", rootNode
.containsChild(null));
126 assertTrue("containsChild", traceNode
.containsChild(catNode
.getKey()));
127 assertFalse("containsChild", traceNode
.containsChild(fEvent1
.getType().toString()));
128 assertFalse("containsChild", traceNode
.containsChild(null));
130 assertTrue("containsChild", catNode
.containsChild(fEvent1
.getType().toString()));
131 assertTrue("containsChild", catNode
.containsChild(fEvent3
.getType().toString()));
132 assertFalse("containsChild", catNode
.containsChild(null));
135 // ------------------------------------------------------------------------
137 // ------------------------------------------------------------------------
139 public void testGetChildren() {
140 // Getting children of the ROOT
141 Collection
<TmfStatisticsTreeNode
> childrenTreeNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
).getChildren();
142 assertEquals("getChildren", 1, childrenTreeNode
.size());
143 TmfStatisticsTreeNode treeNode
= childrenTreeNode
.iterator().next();
144 assertEquals("getChildren", fTestName
, treeNode
.getKey());
146 // Getting children of the trace
147 childrenTreeNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
)).getChildren();
148 assertEquals("getChildren", 1, childrenTreeNode
.size());
149 treeNode
= childrenTreeNode
.iterator().next();
150 assertEquals("getChildren", Messages
.TmfStatisticsData_EventTypes
, treeNode
.getKey());
152 Vector
<String
> keyExpected
= new Vector
<String
>();
153 keyExpected
.add(fEvent1
.getType().toString());
154 keyExpected
.add(fEvent3
.getType().toString());
155 // Getting children of a category
156 childrenTreeNode
= treeNode
.getChildren();
157 assertEquals("getChildren", 2, childrenTreeNode
.size());
159 Iterator
<TmfStatisticsTreeNode
> iterChild
= childrenTreeNode
.iterator();
160 TmfStatisticsTreeNode temp
;
161 while (iterChild
.hasNext()) {
162 temp
= iterChild
.next();
163 if (keyExpected
.contains(temp
.getKey())) {
164 keyExpected
.removeElement(temp
.getKey());
171 // Get children of a specific event type
172 childrenTreeNode
= fStatsData
.get(childrenTreeNode
.iterator().next().getPath()).getChildren();
173 assertEquals("getChildren", 0, childrenTreeNode
.size());
176 // ------------------------------------------------------------------------
178 // ------------------------------------------------------------------------
180 public void testGetAllChildren() {
181 // Getting children of the ROOT
182 Collection
<TmfStatisticsTreeNode
> childrenTreeNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
).getAllChildren();
183 assertEquals("getChildren", 1, childrenTreeNode
.size());
184 TmfStatisticsTreeNode treeNode
= childrenTreeNode
.iterator().next();
185 assertEquals("getChildren", fTestName
, treeNode
.getKey());
187 // Getting children of the trace
188 childrenTreeNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
)).getAllChildren();
189 assertEquals("getChildren", 1, childrenTreeNode
.size());
190 treeNode
= childrenTreeNode
.iterator().next();
191 assertEquals("getChildren", Messages
.TmfStatisticsData_EventTypes
, treeNode
.getKey());
193 Vector
<String
> keyExpected
= new Vector
<String
>();
194 keyExpected
.add(fEvent1
.getType().toString());
195 keyExpected
.add(fEvent3
.getType().toString());
196 // It should return the eventType even though the number of events equals 0
197 fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString())).reset();
198 // Getting children of a category
199 childrenTreeNode
= treeNode
.getAllChildren();
200 assertEquals("getChildren", 2, childrenTreeNode
.size());
202 Iterator
<TmfStatisticsTreeNode
> iterChild
= childrenTreeNode
.iterator();
203 TmfStatisticsTreeNode temp
;
204 while (iterChild
.hasNext()) {
205 temp
= iterChild
.next();
206 if (keyExpected
.contains(temp
.getKey())) {
207 keyExpected
.removeElement(temp
.getKey());
214 // Get children of a specific event type
215 childrenTreeNode
= fStatsData
.get(childrenTreeNode
.iterator().next().getPath()).getAllChildren();
216 assertEquals("getChildren", 0, childrenTreeNode
.size());
219 // ------------------------------------------------------------------------
221 // ------------------------------------------------------------------------
223 public void testGetNbChildren() {
224 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
225 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
226 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
227 TmfStatisticsTreeNode elementNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString()));
229 assertEquals("getNbChildren", 1, rootNode
.getNbChildren());
230 assertEquals("getNbChildren", 1, traceNode
.getNbChildren());
231 assertEquals("getNbChildren", 2, catNode
.getNbChildren());
232 assertEquals("getNbChildren", 0, elementNode
.getNbChildren());
235 // ------------------------------------------------------------------------
237 // ------------------------------------------------------------------------
239 public void testHasChildren() {
240 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
241 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
242 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
243 TmfStatisticsTreeNode elementNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString()));
245 assertTrue ("hasChildren", rootNode
.hasChildren());
246 assertTrue ("hasChildren", traceNode
.hasChildren());
247 assertTrue ("hasChildren", catNode
.hasChildren());
248 assertFalse("hasChildren", elementNode
.hasChildren());
251 // ------------------------------------------------------------------------
253 // ------------------------------------------------------------------------
255 public void testGetParent() {
256 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
257 TmfStatisticsTreeNode parentNode
= rootNode
.getParent();
258 assertNull("getParent", parentNode
);
260 TmfStatisticsTreeNode newTraceNode
= new TmfStatisticsTreeNode(new TmfFixedArray
<String
>("newly created trace node"), fStatsData
);
261 parentNode
= newTraceNode
.getParent();
262 assertNotNull("getParent", parentNode
);
263 assertEquals("getParent", 0, parentNode
.getKey().compareTo(fStatsData
.get(AbsTmfStatisticsTree
.ROOT
).getKey().toString()));
265 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
266 parentNode
= traceNode
.getParent();
267 assertNotNull("getParent", parentNode
);
268 assertEquals("getParent", 0, parentNode
.getPath().toString().compareTo(AbsTmfStatisticsTree
.ROOT
.toString()));
270 TmfStatisticsTreeNode newNode
= new TmfStatisticsTreeNode(new TmfFixedArray
<String
>("TreeNode", Messages
.TmfStatisticsData_EventTypes
, "TreeNode that should not exist"), fStatsData
);
271 parentNode
= newNode
.getParent();
272 assertNull("getParent", parentNode
);
274 TmfStatisticsTreeNode elementNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString()));
275 parentNode
= elementNode
.getParent();
276 assertNull("getParent", parentNode
);
278 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
279 parentNode
= catNode
.getParent();
280 assertNotNull("getParent", parentNode
);
281 assertEquals("getParent", 0, parentNode
.getPath().toString().compareTo(fStatsData
.get(new TmfFixedArray
<String
>(fTestName
)).getPath().toString()));
283 parentNode
= elementNode
.getParent();
284 assertNotNull("getParent", parentNode
);
285 assertTrue("getParent", parentNode
.getPath().equals(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
)));
288 // ------------------------------------------------------------------------
290 // ------------------------------------------------------------------------
292 public void testGetKey() {
293 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
294 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
295 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
296 TmfStatisticsTreeNode elementNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString()));
298 assertEquals("getKey", 0, rootNode
.getKey().compareTo(AbsTmfStatisticsTree
.ROOT
.get(0)));
299 assertEquals("getKey", 0, traceNode
.getKey().compareTo(fTestName
));
300 assertEquals("getKey", 0, catNode
.getKey().compareTo(Messages
.TmfStatisticsData_EventTypes
));
301 assertEquals("getKey", 0, elementNode
.getKey().compareTo(fEvent1
.getType().toString()));
304 // ------------------------------------------------------------------------
306 // ------------------------------------------------------------------------
308 public void testGetPath() {
309 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
310 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
311 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
312 TmfStatisticsTreeNode elementNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString()));
314 assertTrue("getPath", rootNode
.getPath().equals(AbsTmfStatisticsTree
.ROOT
));
315 assertTrue("getPath", traceNode
.getPath().equals(new TmfFixedArray
<String
>(fTestName
)));
316 assertTrue("getPath", catNode
.getPath().equals(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
)));
317 assertTrue("getPath", elementNode
.getPath().equals(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString())));
320 // ------------------------------------------------------------------------
322 // ------------------------------------------------------------------------
324 public void testGetValue() {
325 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
326 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
327 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
328 TmfStatisticsTreeNode elementNode1
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString()));
329 TmfStatisticsTreeNode elementNode2
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent3
.getType().toString()));
331 assertEquals("getValue", 0, rootNode
.getValue().nbEvents
);
332 assertEquals("getValue", 3, traceNode
.getValue().nbEvents
);
333 assertEquals("getValue", 0, catNode
.getValue().nbEvents
);
334 assertEquals("getValue", 2, elementNode1
.getValue().nbEvents
);
335 assertEquals("getValue", 1, elementNode2
.getValue().nbEvents
);
338 // ------------------------------------------------------------------------
340 // ------------------------------------------------------------------------
342 public void testReset() {
343 TmfStatisticsTreeNode rootNode
= fStatsData
.get(AbsTmfStatisticsTree
.ROOT
);
344 TmfStatisticsTreeNode traceNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
345 TmfStatisticsTreeNode catNode
= traceNode
.getChildren().iterator().next();
346 TmfStatisticsTreeNode elementNode
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString()));
349 assertEquals("reset", 0, elementNode
.getValue().nbEvents
);
352 assertEquals("reset", 0, catNode
.getValue().nbEvents
);
353 assertEquals("reset", 0, catNode
.getNbChildren());
354 assertNull("reset", fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString())));
357 assertEquals("reset", 0, traceNode
.getValue().nbEvents
);
358 // A trace always have at least one child that is eventType
359 assertEquals("reset", 1, traceNode
.getNbChildren());
362 assertEquals("reset", 0, rootNode
.getValue().nbEvents
);
363 assertEquals("reset", 1, rootNode
.getNbChildren());