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 TmfBaseStatisticsDataTest
extends TestCase
{
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
41 private String fTestName
= null;
43 private final String fTypeId1
= "Some type1";
44 private final String fTypeId2
= "Some type2";
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
};
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);
55 private final TmfEventSource fSource
= new TmfEventSource("Source");
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
);
61 private final TmfEventReference fReference
= new TmfEventReference("Some reference");
63 private final TmfEvent fEvent1
;
64 private final TmfEvent fEvent2
;
65 private final TmfEvent fEvent3
;
67 private final TmfEventContent fContent1
;
68 private final TmfEventContent fContent2
;
69 private final TmfEventContent fContent3
;
71 private final TmfBaseStatisticsTree fStatsData
;
73 private final ITmfExtraEventInfo fExtraInfo
;
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
80 * @param name of the test
82 public TmfBaseStatisticsDataTest(final String name
) {
87 fEvent1
= new TmfEvent(fTimestamp1
, fSource
, fType1
, fReference
);
88 fContent1
= new TmfEventContent(fEvent1
, "Some content");
89 fEvent1
.setContent(fContent1
);
91 fEvent2
= new TmfEvent(fTimestamp1
, fTimestamp2
, fSource
, fType2
, fReference
);
92 fContent2
= new TmfEventContent(fEvent2
, "Some other content");
93 fEvent2
.setContent(fContent2
);
95 fEvent3
= new TmfEvent(fTimestamp2
, fTimestamp3
, fSource
, fType3
, fReference
);
96 fContent3
= new TmfEventContent(fEvent3
, "Some other different content");
97 fEvent3
.setContent(fContent3
);
99 fStatsData
= new TmfBaseStatisticsTree();
100 fExtraInfo
= new ITmfExtraEventInfo() {
102 public String
getTraceName() {
106 fStatsData
.registerEvent(fEvent1
, fExtraInfo
);
107 fStatsData
.registerEvent(fEvent2
, fExtraInfo
);
108 fStatsData
.registerEvent(fEvent3
, fExtraInfo
);
111 // ------------------------------------------------------------------------
113 // ------------------------------------------------------------------------
115 public void testGetChildren() {
116 // Getting children of the ROOT
117 Collection
<TmfStatisticsTreeNode
> childrenTreeNode
= fStatsData
.getChildren(AbsTmfStatisticsTree
.ROOT
);
118 assertEquals("getChildren", 1, childrenTreeNode
.size());
119 TmfStatisticsTreeNode treeNode
= childrenTreeNode
.iterator().next();
120 assertEquals("getChildren", fTestName
, treeNode
.getKey());
122 // Getting children of the trace
123 childrenTreeNode
= fStatsData
.getChildren(new TmfFixedArray
<String
>(fTestName
));
124 assertEquals("getChildren", 1, childrenTreeNode
.size());
125 treeNode
= childrenTreeNode
.iterator().next();
126 assertEquals("getChildren", Messages
.TmfStatisticsData_EventTypes
, treeNode
.getKey());
128 Vector
<String
> keyExpected
= new Vector
<String
>();
129 keyExpected
.add(fEvent1
.getType().toString());
130 keyExpected
.add(fEvent3
.getType().toString());
131 // Getting children of a category
132 childrenTreeNode
= fStatsData
.getChildren(treeNode
.getPath());
133 assertEquals("getChildren", 2, childrenTreeNode
.size());
135 Iterator
<TmfStatisticsTreeNode
> iterChild
= childrenTreeNode
.iterator();
136 TmfStatisticsTreeNode temp
;
137 while (iterChild
.hasNext()) {
138 temp
= iterChild
.next();
139 if (keyExpected
.contains(temp
.getKey())) {
140 keyExpected
.removeElement(temp
.getKey());
147 // Get children of a specific event type
148 childrenTreeNode
= fStatsData
.getChildren(childrenTreeNode
.iterator().next().getPath());
149 assertEquals("getChildren", 0, childrenTreeNode
.size());
152 // ------------------------------------------------------------------------
154 // ------------------------------------------------------------------------
156 public void testGetAllChildren() {
157 // Getting children of the ROOT
158 Collection
<TmfStatisticsTreeNode
> childrenTreeNode
= fStatsData
.getAllChildren(AbsTmfStatisticsTree
.ROOT
);
159 assertEquals("getChildren", 1, childrenTreeNode
.size());
160 TmfStatisticsTreeNode treeNode
= childrenTreeNode
.iterator().next();
161 assertEquals("getChildren", fTestName
, treeNode
.getKey());
163 // Getting children of the trace
164 childrenTreeNode
= fStatsData
.getAllChildren(new TmfFixedArray
<String
>(fTestName
));
165 assertEquals("getChildren", 1, childrenTreeNode
.size());
166 treeNode
= childrenTreeNode
.iterator().next();
167 assertEquals("getChildren", Messages
.TmfStatisticsData_EventTypes
, treeNode
.getKey());
169 Vector
<String
> keyExpected
= new Vector
<String
>();
170 keyExpected
.add(fEvent1
.getType().toString());
171 keyExpected
.add(fEvent3
.getType().toString());
172 // It should return the eventType even though the number of events equals 0
173 fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString())).reset();
174 // Getting children of a category
175 childrenTreeNode
= fStatsData
.get(treeNode
.getPath()).getAllChildren();
176 assertEquals("getChildren", 2, childrenTreeNode
.size());
178 Iterator
<TmfStatisticsTreeNode
> iterChild
= childrenTreeNode
.iterator();
179 TmfStatisticsTreeNode temp
;
180 while (iterChild
.hasNext()) {
181 temp
= iterChild
.next();
182 if (keyExpected
.contains(temp
.getKey())) {
183 keyExpected
.removeElement(temp
.getKey());
190 // Get children of a specific event type
191 childrenTreeNode
= fStatsData
.getAllChildren(childrenTreeNode
.iterator().next().getPath());
192 assertEquals("getChildren", 0, childrenTreeNode
.size());
195 // ------------------------------------------------------------------------
197 // ------------------------------------------------------------------------
199 public void testRegisterEvent() {
200 TmfStatisticsTreeNode trace
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
201 assertEquals("registerEvent", 3, trace
.getValue().nbEvents
);
203 Collection
<TmfStatisticsTreeNode
> childrenTreeNode
= fStatsData
.getChildren(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
));
204 for (TmfStatisticsTreeNode child
: childrenTreeNode
) {
205 if (child
.getKey().compareTo(fEvent1
.getType().toString()) == 0) {
206 assertEquals("registerEvent", 2, child
.getValue().nbEvents
);
208 else if (child
.getKey().compareTo(fEvent3
.getType().toString()) == 0) {
209 assertEquals("registerEvent", 1, child
.getValue().nbEvents
);
214 // ------------------------------------------------------------------------
216 // ------------------------------------------------------------------------
218 public void testGet() {
219 TmfStatisticsTreeNode traceRoot
= fStatsData
.get(new TmfFixedArray
<String
>(fTestName
));
220 assertNotNull("get", traceRoot
);
221 assertEquals("get", 0, traceRoot
.getPath().toString().compareTo("[" + fTestName
+ "]"));
222 assertEquals("get", 3, traceRoot
.getValue().nbEvents
);
223 assertEquals("get", 1, traceRoot
.getNbChildren());
226 // ------------------------------------------------------------------------
228 // ------------------------------------------------------------------------
230 public void testGetOrCreate() {
231 TmfFixedArray
<String
> newEventType
= new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, "Fancy Type");
232 TmfStatisticsTreeNode newEventTypeNode
;
234 // newEventType is not in the tree
235 newEventTypeNode
= fStatsData
.get(newEventType
);
236 assertNull("getOrCreate", newEventTypeNode
);
238 newEventTypeNode
= fStatsData
.getOrCreate(newEventType
);
239 assertNotNull("getOrCreate", newEventTypeNode
);
240 assertTrue("getOrCreate", newEventTypeNode
.getPath().equals(newEventType
));
242 // newEventType is in the tree
243 newEventTypeNode
.reset();
244 newEventTypeNode
= fStatsData
.get(newEventType
);
245 assertNotNull("getOrCreate", newEventTypeNode
);
247 newEventTypeNode
= fStatsData
.getOrCreate(newEventType
);
248 assertNotNull("getOrCreate", newEventTypeNode
);
249 assertTrue("getOrCreate", newEventTypeNode
.getPath().equals(newEventType
));
252 // ------------------------------------------------------------------------
254 // ------------------------------------------------------------------------
256 public void testGetParent() {
257 TmfStatisticsTreeNode parentNode
= fStatsData
.getParent(AbsTmfStatisticsTree
.ROOT
);
258 assertNull("getParent", parentNode
);
260 parentNode
= fStatsData
.getParent(new TmfFixedArray
<String
>("TreeRootNode that should not exist"));
261 assertNotNull("getParent", parentNode
);
262 assertEquals("getParent", 0, parentNode
.getKey().compareTo(fStatsData
.get(AbsTmfStatisticsTree
.ROOT
).getKey().toString()));
264 parentNode
= fStatsData
.getParent(new TmfFixedArray
<String
>("TreeNode", Messages
.TmfStatisticsData_EventTypes
, "TreeNode that should not exist"));
265 assertNull("getParent", parentNode
);
266 parentNode
= fStatsData
.getParent(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString()));
267 assertNull("getParent", parentNode
);
269 parentNode
= fStatsData
.getParent(new TmfFixedArray
<String
>(fTestName
));
270 assertNotNull("getParent", parentNode
);
271 assertEquals("getParent", 0, parentNode
.getPath().toString().compareTo(AbsTmfStatisticsTree
.ROOT
.toString()));
273 parentNode
= fStatsData
.getParent(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
));
274 assertNotNull("getParent", parentNode
);
275 assertEquals("getParent", 0, parentNode
.getPath().toString().compareTo(fStatsData
.get(new TmfFixedArray
<String
>(fTestName
)).getPath().toString()));
278 // ------------------------------------------------------------------------
280 // ------------------------------------------------------------------------
282 public void testReset() {
283 fStatsData
.reset(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
));
285 assertEquals("reset", 0, fStatsData
.getChildren(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
)).size());
286 assertNull("reset", fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getTypeId())));
287 assertNull("reset", fStatsData
.get(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType3
.getTypeId())));
289 fStatsData
.reset(new TmfFixedArray
<String
>(fTestName
));
291 // A rootz should always have at least one child that is eventType
292 assertEquals("reset", 1, fStatsData
.getChildren(new TmfFixedArray
<String
>(fTestName
)).size());