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
.core
.event
.ITmfEventField
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventType
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.Messages
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.TmfStatisticsTree
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.TmfStatisticsTreeNode
;
32 * TmfBaseStatistics Test Cases.
34 @SuppressWarnings("nls")
35 public class TmfBaseStatisticsDataTest
extends TestCase
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
40 private String fTestName
= null;
42 private final String fContext
= "UnitTest";
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 String fSource
= "Source";
57 private final TmfEventType fType1
= new TmfEventType(fContext
, fTypeId1
, TmfEventField
.makeRoot(fLabels
));
58 private final TmfEventType fType2
= new TmfEventType(fContext
, fTypeId1
, TmfEventField
.makeRoot(fLabels
));
59 private final TmfEventType fType3
= new TmfEventType(fContext
, fTypeId2
, TmfEventField
.makeRoot(fLabels
));
61 private final String fReference
= "Some reference";
63 private final TmfEvent fEvent1
;
64 private final TmfEvent fEvent2
;
65 private final TmfEvent fEvent3
;
67 private final TmfEventField fContent1
;
68 private final TmfEventField fContent2
;
69 private final TmfEventField fContent3
;
71 private final TmfStatisticsTree fStatsData
;
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
81 public TmfBaseStatisticsDataTest(final String name
) {
86 fContent1
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some content");
87 fEvent1
= new TmfEvent(null, fTimestamp1
, fSource
, fType1
, fContent1
, fReference
);
89 fContent2
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some other content");
90 fEvent2
= new TmfEvent(null, fTimestamp2
, fSource
, fType2
, fContent2
, fReference
);
92 fContent3
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some other different content");
93 fEvent3
= new TmfEvent(null, fTimestamp3
, fSource
, fType3
, fContent3
, fReference
);
95 fStatsData
= new TmfStatisticsTree();
97 fStatsData
.setTotal(fTestName
, true, 3);
98 fStatsData
.setTypeCount(fTestName
, fEvent1
.getType().getName(), true, 1);
99 fStatsData
.setTypeCount(fTestName
, fEvent2
.getType().getName(), true, 1);
100 fStatsData
.setTypeCount(fTestName
, fEvent3
.getType().getName(), true, 1);
103 // ------------------------------------------------------------------------
105 // ------------------------------------------------------------------------
108 * Test getting of children.
110 public void testGetChildren() {
111 // Getting children of the ROOT
112 Collection
<TmfStatisticsTreeNode
> childrenTreeNode
= fStatsData
.getChildren(TmfStatisticsTree
.ROOT
);
113 assertEquals("getChildren", 1, childrenTreeNode
.size());
114 TmfStatisticsTreeNode treeNode
= childrenTreeNode
.iterator().next();
115 assertEquals("getChildren", fTestName
, treeNode
.getKey());
117 // Getting children of the trace
118 childrenTreeNode
= fStatsData
.getChildren(fTestName
);
119 assertEquals("getChildren", 1, childrenTreeNode
.size());
120 treeNode
= childrenTreeNode
.iterator().next();
121 assertEquals("getChildren", Messages
.TmfStatisticsData_EventTypes
, treeNode
.getKey());
123 Vector
<String
> keyExpected
= new Vector
<String
>();
124 keyExpected
.add(fEvent1
.getType().getName());
125 keyExpected
.add(fEvent3
.getType().getName());
126 // Getting children of a category
127 childrenTreeNode
= fStatsData
.getChildren(treeNode
.getPath());
128 assertEquals("getChildren", 2, childrenTreeNode
.size());
130 Iterator
<TmfStatisticsTreeNode
> iterChild
= childrenTreeNode
.iterator();
131 TmfStatisticsTreeNode temp
;
132 while (iterChild
.hasNext()) {
133 temp
= iterChild
.next();
134 assertEquals(0, fStatsData
.getChildren(temp
.getPath()).size());
135 if (keyExpected
.contains(temp
.getKey())) {
136 keyExpected
.removeElement(temp
.getKey());
142 // Get children of a specific event type
143 childrenTreeNode
= fStatsData
.getChildren(childrenTreeNode
.iterator().next().getPath());
144 assertEquals("getChildren", 0, childrenTreeNode
.size());
147 // ------------------------------------------------------------------------
149 // ------------------------------------------------------------------------
152 * Test getting of all children.
154 public void testGetAllChildren() {
155 // Getting children of the ROOT
156 Collection
<TmfStatisticsTreeNode
> childrenTreeNode
= fStatsData
.getAllChildren(TmfStatisticsTree
.ROOT
);
157 assertEquals("getChildren", 1, childrenTreeNode
.size());
158 TmfStatisticsTreeNode treeNode
= childrenTreeNode
.iterator().next();
159 assertEquals("getChildren", fTestName
, treeNode
.getKey());
161 // Getting children of the trace
162 childrenTreeNode
= fStatsData
.getAllChildren(fTestName
);
163 assertEquals("getChildren", 1, childrenTreeNode
.size());
164 treeNode
= childrenTreeNode
.iterator().next();
165 assertEquals("getChildren", Messages
.TmfStatisticsData_EventTypes
, treeNode
.getKey());
167 Vector
<String
> keyExpected
= new Vector
<String
>();
168 keyExpected
.add(fEvent1
.getType().getName());
169 keyExpected
.add(fEvent3
.getType().getName());
171 * It should return the eventType even though the number of events
174 fStatsData
.get(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().getName()).reset();
175 // Getting children of a category
176 childrenTreeNode
= fStatsData
.get(treeNode
.getPath()).getAllChildren();
177 assertEquals("getChildren", 2, childrenTreeNode
.size());
179 Iterator
<TmfStatisticsTreeNode
> iterChild
= childrenTreeNode
.iterator();
180 TmfStatisticsTreeNode temp
;
181 while (iterChild
.hasNext()) {
182 temp
= iterChild
.next();
183 assertEquals(0, fStatsData
.getAllChildren(temp
.getPath()).size());
184 if (keyExpected
.contains(temp
.getKey())) {
185 keyExpected
.removeElement(temp
.getKey());
191 // Get children of a specific event type
192 childrenTreeNode
= fStatsData
.getAllChildren(childrenTreeNode
.iterator().next().getPath());
193 assertEquals("getChildren", 0, childrenTreeNode
.size());
196 // ------------------------------------------------------------------------
198 // ------------------------------------------------------------------------
201 * Test registering of events.
203 public void testRegisterEvent() {
204 TmfStatisticsTreeNode trace
= fStatsData
.get(fTestName
);
205 assertEquals("registerEvent", 3, trace
.getValues().getTotal());
207 Collection
<TmfStatisticsTreeNode
> childrenTreeNode
= fStatsData
.getChildren(fTestName
, Messages
.TmfStatisticsData_EventTypes
);
208 for (TmfStatisticsTreeNode child
: childrenTreeNode
) {
209 if (child
.getKey().compareTo(fEvent1
.getType().getName()) == 0) {
210 assertEquals("registerEvent", 1, child
.getValues().getTotal());
211 } else if (child
.getKey().compareTo(fEvent3
.getType().getName()) == 0) {
212 assertEquals("registerEvent", 1, child
.getValues().getTotal());
217 // ------------------------------------------------------------------------
219 // ------------------------------------------------------------------------
224 public void testGet() {
225 TmfStatisticsTreeNode traceRoot
= fStatsData
.get(fTestName
);
226 assertNotNull("get", traceRoot
);
227 assertEquals("get", 0, traceRoot
.getPath()[0].compareTo(fTestName
));
228 assertEquals("get", 3, traceRoot
.getValues().getTotal());
229 assertEquals("get", 1, traceRoot
.getNbChildren());
232 // ------------------------------------------------------------------------
234 // ------------------------------------------------------------------------
237 * Test getting or creating of node entries.
239 public void testGetOrCreate() {
240 String
[] newEventType
= new String
[] { fTestName
, Messages
.TmfStatisticsData_EventTypes
, "Fancy Type" };
241 TmfStatisticsTreeNode newEventTypeNode
;
243 // newEventType is not in the tree
244 newEventTypeNode
= fStatsData
.get(newEventType
);
245 assertNull("getOrCreate", newEventTypeNode
);
247 newEventTypeNode
= fStatsData
.getOrCreate(newEventType
);
248 assertNotNull("getOrCreate", newEventTypeNode
);
249 assertTrue("getOrCreate", newEventTypeNode
.getPath().equals(newEventType
));
251 // newEventType is in the tree
252 newEventTypeNode
.reset();
253 newEventTypeNode
= fStatsData
.get(newEventType
);
254 assertNotNull("getOrCreate", newEventTypeNode
);
256 newEventTypeNode
= fStatsData
.getOrCreate(newEventType
);
257 assertNotNull("getOrCreate", newEventTypeNode
);
258 assertTrue("getOrCreate", newEventTypeNode
.getPath().equals(newEventType
));
261 // ------------------------------------------------------------------------
263 // ------------------------------------------------------------------------
266 * Test getting of parent node.
268 public void testGetParent() {
269 TmfStatisticsTreeNode parentNode
= fStatsData
.getParent(TmfStatisticsTree
.ROOT
);
270 assertNull("getParent", parentNode
);
272 parentNode
= fStatsData
.getParent("TreeRootNode that should not exist");
273 assertNotNull("getParent", parentNode
);
274 assertEquals("getParent", 0, parentNode
.getKey().compareTo(fStatsData
.get(TmfStatisticsTree
.ROOT
).getKey().toString()));
276 parentNode
= fStatsData
.getParent("TreeNode", Messages
.TmfStatisticsData_EventTypes
, "TreeNode that should not exist");
277 assertNull("getParent", parentNode
);
278 parentNode
= fStatsData
.getParent(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().getName());
279 assertNull("getParent", parentNode
);
281 parentNode
= fStatsData
.getParent(fTestName
);
282 assertNotNull("getParent", parentNode
);
283 assertEquals("getParent", 0, parentNode
.getPath().toString().compareTo(TmfStatisticsTree
.ROOT
.toString()));
285 parentNode
= fStatsData
.getParent(fTestName
, Messages
.TmfStatisticsData_EventTypes
);
286 assertNotNull("getParent", parentNode
);
287 assertEquals("getParent", 0, parentNode
.getPath().toString().compareTo(fStatsData
.get(fTestName
).getPath().toString()));
290 // ------------------------------------------------------------------------
292 // ------------------------------------------------------------------------
297 public void testReset() {
298 fStatsData
.reset(fTestName
, Messages
.TmfStatisticsData_EventTypes
);
300 assertEquals("reset", 0, fStatsData
.getChildren(fTestName
, Messages
.TmfStatisticsData_EventTypes
).size());
301 assertNull("reset", fStatsData
.get(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName()));
302 assertNull("reset", fStatsData
.get(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType3
.getName()));
304 fStatsData
.reset(fTestName
);
306 // A root should always have at least one child that is eventType
307 assertEquals("reset", 1, fStatsData
.getChildren(fTestName
).size());