tmf: Refactor TMF statistics
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.tests / src / org / eclipse / linuxtools / tmf / ui / tests / statistics / TmfBaseStatisticsDataTest.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.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;
30
31 /**
32 * TmfBaseStatistics Test Cases.
33 */
34 @SuppressWarnings("nls")
35 public class TmfBaseStatisticsDataTest extends TestCase {
36
37 // ------------------------------------------------------------------------
38 // Fields
39 // ------------------------------------------------------------------------
40 private String fTestName = null;
41
42 private final String fContext = "UnitTest";
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(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));
60
61 private final String fReference = "Some reference";
62
63 private final TmfEvent fEvent1;
64 private final TmfEvent fEvent2;
65 private final TmfEvent fEvent3;
66
67 private final TmfEventField fContent1;
68 private final TmfEventField fContent2;
69 private final TmfEventField fContent3;
70
71 private final TmfStatisticsTree fStatsData;
72
73 // ------------------------------------------------------------------------
74 // Housekeeping
75 // ------------------------------------------------------------------------
76
77 /**
78 * @param name
79 * Test name
80 */
81 public TmfBaseStatisticsDataTest(final String name) {
82 super(name);
83
84 fTestName = name;
85
86 fContent1 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some content");
87 fEvent1 = new TmfEvent(null, fTimestamp1, fSource, fType1, fContent1, fReference);
88
89 fContent2 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other content");
90 fEvent2 = new TmfEvent(null, fTimestamp2, fSource, fType2, fContent2, fReference);
91
92 fContent3 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other different content");
93 fEvent3 = new TmfEvent(null, fTimestamp3, fSource, fType3, fContent3, fReference);
94
95 fStatsData = new TmfStatisticsTree();
96
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);
101 }
102
103 // ------------------------------------------------------------------------
104 // GetChildren
105 // ------------------------------------------------------------------------
106
107 /**
108 * Test getting of children.
109 */
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());
116
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());
122
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());
129
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());
137 } else {
138 fail();
139 }
140 }
141
142 // Get children of a specific event type
143 childrenTreeNode = fStatsData.getChildren(childrenTreeNode.iterator().next().getPath());
144 assertEquals("getChildren", 0, childrenTreeNode.size());
145 }
146
147 // ------------------------------------------------------------------------
148 // GetAllChildren
149 // ------------------------------------------------------------------------
150
151 /**
152 * Test getting of all children.
153 */
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());
160
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());
166
167 Vector<String> keyExpected = new Vector<String>();
168 keyExpected.add(fEvent1.getType().getName());
169 keyExpected.add(fEvent3.getType().getName());
170 /*
171 * It should return the eventType even though the number of events
172 * equals 0
173 */
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());
178
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());
186 } else {
187 fail();
188 }
189 }
190
191 // Get children of a specific event type
192 childrenTreeNode = fStatsData.getAllChildren(childrenTreeNode.iterator().next().getPath());
193 assertEquals("getChildren", 0, childrenTreeNode.size());
194 }
195
196 // ------------------------------------------------------------------------
197 // RegisterEvent
198 // ------------------------------------------------------------------------
199
200 /**
201 * Test registering of events.
202 */
203 public void testRegisterEvent() {
204 TmfStatisticsTreeNode trace = fStatsData.get(fTestName);
205 assertEquals("registerEvent", 3, trace.getValues().getTotal());
206
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());
213 }
214 }
215 }
216
217 // ------------------------------------------------------------------------
218 // Get a node
219 // ------------------------------------------------------------------------
220
221 /**
222 * Test getter.
223 */
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());
230 }
231
232 // ------------------------------------------------------------------------
233 // GetOrCreate
234 // ------------------------------------------------------------------------
235
236 /**
237 * Test getting or creating of node entries.
238 */
239 public void testGetOrCreate() {
240 String[] newEventType = new String[] { fTestName, Messages.TmfStatisticsData_EventTypes, "Fancy Type" };
241 TmfStatisticsTreeNode newEventTypeNode;
242
243 // newEventType is not in the tree
244 newEventTypeNode = fStatsData.get(newEventType);
245 assertNull("getOrCreate", newEventTypeNode);
246
247 newEventTypeNode = fStatsData.getOrCreate(newEventType);
248 assertNotNull("getOrCreate", newEventTypeNode);
249 assertTrue("getOrCreate", newEventTypeNode.getPath().equals(newEventType));
250
251 // newEventType is in the tree
252 newEventTypeNode.reset();
253 newEventTypeNode = fStatsData.get(newEventType);
254 assertNotNull("getOrCreate", newEventTypeNode);
255
256 newEventTypeNode = fStatsData.getOrCreate(newEventType);
257 assertNotNull("getOrCreate", newEventTypeNode);
258 assertTrue("getOrCreate", newEventTypeNode.getPath().equals(newEventType));
259 }
260
261 // ------------------------------------------------------------------------
262 // GetParent
263 // ------------------------------------------------------------------------
264
265 /**
266 * Test getting of parent node.
267 */
268 public void testGetParent() {
269 TmfStatisticsTreeNode parentNode = fStatsData.getParent(TmfStatisticsTree.ROOT);
270 assertNull("getParent", parentNode);
271
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()));
275
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);
280
281 parentNode = fStatsData.getParent(fTestName);
282 assertNotNull("getParent", parentNode);
283 assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(TmfStatisticsTree.ROOT.toString()));
284
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()));
288 }
289
290 // ------------------------------------------------------------------------
291 // Reset
292 // ------------------------------------------------------------------------
293
294 /**
295 * Test reset method
296 */
297 public void testReset() {
298 fStatsData.reset(fTestName, Messages.TmfStatisticsData_EventTypes);
299
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()));
303
304 fStatsData.reset(fTestName);
305
306 // A root should always have at least one child that is eventType
307 assertEquals("reset", 1, fStatsData.getChildren(fTestName).size());
308 }
309 }
This page took 0.039009 seconds and 5 git commands to generate.