Convert Windows line delimiters to Unix.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.tests / src / org / eclipse / linuxtools / tmf / ui / tests / statistics / TmfBaseStatisticsDataTest.java
CommitLineData
79e08fd0
BH
1/*******************************************************************************
2 * Copyright (c) 2011 Ericsson
64636df8 3 *
79e08fd0
BH
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
64636df8 8 *
79e08fd0 9 * Contributors:
09667aa4 10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Initial design and implementation
79e08fd0
BH
11 * Bernd Hufmann - Fixed warnings
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.tmf.ui.tests.statistics;
15
16import java.util.Collection;
17import java.util.Iterator;
18import java.util.Vector;
19
20import junit.framework.TestCase;
21
4c564a2d 22import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
6c13869b 23import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
4c564a2d 24import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
6c13869b
FC
25import org.eclipse.linuxtools.tmf.core.event.TmfEventType;
26import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
27import org.eclipse.linuxtools.tmf.core.util.TmfFixedArray;
cfd22ad0
MD
28import org.eclipse.linuxtools.tmf.ui.viewers.statistics.ITmfExtraEventInfo;
29import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.AbsTmfStatisticsTree;
30import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.Messages;
31import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfBaseStatisticsTree;
32import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode;
79e08fd0 33
64636df8
BH
34/**
35 * TmfBaseStatistics Test Cases.
36 */
79e08fd0
BH
37@SuppressWarnings("nls")
38public class TmfBaseStatisticsDataTest extends TestCase {
64636df8 39
79e08fd0
BH
40 // ------------------------------------------------------------------------
41 // Fields
42 // ------------------------------------------------------------------------
09667aa4 43 private String fTestName = null;
64636df8 44
cbbcc354 45 private final String fContext = "UnitTest";
46 private final String fTypeId1 = "Some type1";
47 private final String fTypeId2 = "Some type2";
64636df8 48
79e08fd0
BH
49 private final String fLabel0 = "label1";
50 private final String fLabel1 = "label2";
51 private final String fLabel2 = "label3";
52 private final String[] fLabels = new String[] { fLabel0, fLabel1, fLabel2 };
53
4641c2f7
FC
54 private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5);
55 private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5);
56 private final TmfTimestamp fTimestamp3 = new TmfTimestamp(12355, (byte) 2, 5);
64636df8 57
4641c2f7 58 private final String fSource = "Source";
64636df8 59
4c564a2d
FC
60 private final TmfEventType fType1 = new TmfEventType(fContext, fTypeId1, TmfEventField.makeRoot(fLabels));
61 private final TmfEventType fType2 = new TmfEventType(fContext, fTypeId1, TmfEventField.makeRoot(fLabels));
62 private final TmfEventType fType3 = new TmfEventType(fContext, fTypeId2, TmfEventField.makeRoot(fLabels));
64636df8 63
4641c2f7 64 private final String fReference = "Some reference";
79e08fd0
BH
65
66 private final TmfEvent fEvent1;
67 private final TmfEvent fEvent2;
68 private final TmfEvent fEvent3;
69
4c564a2d
FC
70 private final TmfEventField fContent1;
71 private final TmfEventField fContent2;
72 private final TmfEventField fContent3;
64636df8 73
79e08fd0 74 private final TmfBaseStatisticsTree fStatsData;
64636df8 75
79e08fd0 76 private final ITmfExtraEventInfo fExtraInfo;
64636df8 77
79e08fd0
BH
78 // ------------------------------------------------------------------------
79 // Housekeeping
80 // ------------------------------------------------------------------------
64636df8 81
79e08fd0 82 /**
09667aa4
MD
83 * @param name
84 * Test name
79e08fd0
BH
85 */
86 public TmfBaseStatisticsDataTest(final String name) {
87 super(name);
64636df8 88
79e08fd0 89 fTestName = name;
64636df8 90
a4115405 91 fContent1 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some content");
7b477cc3 92 fEvent1 = new TmfEvent(null, fTimestamp1, fSource, fType1, fContent1, fReference);
79e08fd0 93
a4115405 94 fContent2 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other content");
7b477cc3 95 fEvent2 = new TmfEvent(null, fTimestamp2, fSource, fType2, fContent2, fReference);
64636df8 96
a4115405 97 fContent3 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other different content");
7b477cc3 98 fEvent3 = new TmfEvent(null, fTimestamp3, fSource, fType3, fContent3, fReference);
64636df8 99
79e08fd0
BH
100 fStatsData = new TmfBaseStatisticsTree();
101 fExtraInfo = new ITmfExtraEventInfo() {
102 @Override
103 public String getTraceName() {
104 return name;
105 }
106 };
107 fStatsData.registerEvent(fEvent1, fExtraInfo);
108 fStatsData.registerEvent(fEvent2, fExtraInfo);
109 fStatsData.registerEvent(fEvent3, fExtraInfo);
110 }
64636df8 111
79e08fd0
BH
112 // ------------------------------------------------------------------------
113 // GetChildren
114 // ------------------------------------------------------------------------
115
64636df8
BH
116 /**
117 * Test getting of children.
118 */
79e08fd0
BH
119 public void testGetChildren() {
120 // Getting children of the ROOT
121 Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsData.getChildren(AbsTmfStatisticsTree.ROOT);
122 assertEquals("getChildren", 1, childrenTreeNode.size());
123 TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next();
124 assertEquals("getChildren", fTestName, treeNode.getKey());
64636df8 125
79e08fd0
BH
126 // Getting children of the trace
127 childrenTreeNode = fStatsData.getChildren(new TmfFixedArray<String>(fTestName));
128 assertEquals("getChildren", 1, childrenTreeNode.size());
129 treeNode = childrenTreeNode.iterator().next();
130 assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey());
64636df8
BH
131
132 Vector<String> keyExpected = new Vector<String>();
59b50985
MD
133 keyExpected.add(fEvent1.getType().getName());
134 keyExpected.add(fEvent3.getType().getName());
79e08fd0
BH
135 // Getting children of a category
136 childrenTreeNode = fStatsData.getChildren(treeNode.getPath());
137 assertEquals("getChildren", 2, childrenTreeNode.size());
64636df8 138
79e08fd0
BH
139 Iterator<TmfStatisticsTreeNode> iterChild = childrenTreeNode.iterator();
140 TmfStatisticsTreeNode temp;
141 while (iterChild.hasNext()) {
142 temp = iterChild.next();
255224d9 143 assertEquals(0, fStatsData.getChildren(temp.getPath()).size());
79e08fd0
BH
144 if (keyExpected.contains(temp.getKey())) {
145 keyExpected.removeElement(temp.getKey());
09667aa4 146 } else {
79e08fd0
BH
147 fail();
148 }
149 }
64636df8 150
79e08fd0
BH
151 // Get children of a specific event type
152 childrenTreeNode = fStatsData.getChildren(childrenTreeNode.iterator().next().getPath());
153 assertEquals("getChildren", 0, childrenTreeNode.size());
154 }
64636df8 155
79e08fd0
BH
156 // ------------------------------------------------------------------------
157 // GetAllChildren
158 // ------------------------------------------------------------------------
159
64636df8
BH
160 /**
161 * Test getting of all children.
162 */
79e08fd0
BH
163 public void testGetAllChildren() {
164 // Getting children of the ROOT
165 Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsData.getAllChildren(AbsTmfStatisticsTree.ROOT);
166 assertEquals("getChildren", 1, childrenTreeNode.size());
167 TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next();
168 assertEquals("getChildren", fTestName, treeNode.getKey());
64636df8 169
79e08fd0
BH
170 // Getting children of the trace
171 childrenTreeNode = fStatsData.getAllChildren(new TmfFixedArray<String>(fTestName));
172 assertEquals("getChildren", 1, childrenTreeNode.size());
173 treeNode = childrenTreeNode.iterator().next();
174 assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey());
64636df8
BH
175
176 Vector<String> keyExpected = new Vector<String>();
59b50985
MD
177 keyExpected.add(fEvent1.getType().getName());
178 keyExpected.add(fEvent3.getType().getName());
09667aa4
MD
179 /*
180 * It should return the eventType even though the number of events
181 * equals 0
182 */
59b50985 183 fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName())).reset();
79e08fd0
BH
184 // Getting children of a category
185 childrenTreeNode = fStatsData.get(treeNode.getPath()).getAllChildren();
186 assertEquals("getChildren", 2, childrenTreeNode.size());
64636df8 187
79e08fd0
BH
188 Iterator<TmfStatisticsTreeNode> iterChild = childrenTreeNode.iterator();
189 TmfStatisticsTreeNode temp;
190 while (iterChild.hasNext()) {
191 temp = iterChild.next();
255224d9 192 assertEquals(0, fStatsData.getAllChildren(temp.getPath()).size());
79e08fd0
BH
193 if (keyExpected.contains(temp.getKey())) {
194 keyExpected.removeElement(temp.getKey());
09667aa4 195 } else {
79e08fd0
BH
196 fail();
197 }
198 }
64636df8 199
79e08fd0
BH
200 // Get children of a specific event type
201 childrenTreeNode = fStatsData.getAllChildren(childrenTreeNode.iterator().next().getPath());
202 assertEquals("getChildren", 0, childrenTreeNode.size());
203 }
204
205 // ------------------------------------------------------------------------
206 // RegisterEvent
207 // ------------------------------------------------------------------------
64636df8
BH
208
209 /**
210 * Test registering of events.
211 */
79e08fd0
BH
212 public void testRegisterEvent() {
213 TmfStatisticsTreeNode trace = fStatsData.get(new TmfFixedArray<String>(fTestName));
25a042b3 214 assertEquals("registerEvent", 3, trace.getValue().getTotal());
64636df8 215
79e08fd0
BH
216 Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsData.getChildren(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes));
217 for (TmfStatisticsTreeNode child : childrenTreeNode) {
59b50985 218 if (child.getKey().compareTo(fEvent1.getType().getName()) == 0) {
25a042b3 219 assertEquals("registerEvent", 2, child.getValue().getTotal());
59b50985 220 } else if (child.getKey().compareTo(fEvent3.getType().getName()) == 0) {
25a042b3 221 assertEquals("registerEvent", 1, child.getValue().getTotal());
79e08fd0
BH
222 }
223 }
224 }
64636df8 225
79e08fd0
BH
226 // ------------------------------------------------------------------------
227 // Get a node
228 // ------------------------------------------------------------------------
64636df8
BH
229
230 /**
231 * Test getter.
232 */
79e08fd0
BH
233 public void testGet() {
234 TmfStatisticsTreeNode traceRoot = fStatsData.get(new TmfFixedArray<String>(fTestName));
235 assertNotNull("get", traceRoot);
236 assertEquals("get", 0, traceRoot.getPath().toString().compareTo("[" + fTestName + "]"));
25a042b3 237 assertEquals("get", 3, traceRoot.getValue().getTotal());
79e08fd0
BH
238 assertEquals("get", 1, traceRoot.getNbChildren());
239 }
64636df8 240
79e08fd0
BH
241 // ------------------------------------------------------------------------
242 // GetOrCreate
243 // ------------------------------------------------------------------------
64636df8
BH
244
245 /**
246 * Test getting or creating of node entries.
247 */
79e08fd0
BH
248 public void testGetOrCreate() {
249 TmfFixedArray<String> newEventType = new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, "Fancy Type");
250 TmfStatisticsTreeNode newEventTypeNode;
64636df8 251
79e08fd0
BH
252 // newEventType is not in the tree
253 newEventTypeNode = fStatsData.get(newEventType);
254 assertNull("getOrCreate", newEventTypeNode);
64636df8 255
79e08fd0
BH
256 newEventTypeNode = fStatsData.getOrCreate(newEventType);
257 assertNotNull("getOrCreate", newEventTypeNode);
258 assertTrue("getOrCreate", newEventTypeNode.getPath().equals(newEventType));
64636df8 259
79e08fd0
BH
260 // newEventType is in the tree
261 newEventTypeNode.reset();
262 newEventTypeNode = fStatsData.get(newEventType);
263 assertNotNull("getOrCreate", newEventTypeNode);
64636df8 264
79e08fd0
BH
265 newEventTypeNode = fStatsData.getOrCreate(newEventType);
266 assertNotNull("getOrCreate", newEventTypeNode);
267 assertTrue("getOrCreate", newEventTypeNode.getPath().equals(newEventType));
268 }
64636df8 269
79e08fd0
BH
270 // ------------------------------------------------------------------------
271 // GetParent
272 // ------------------------------------------------------------------------
64636df8
BH
273
274 /**
275 * Test getting of parent node.
276 */
79e08fd0
BH
277 public void testGetParent() {
278 TmfStatisticsTreeNode parentNode = fStatsData.getParent(AbsTmfStatisticsTree.ROOT);
279 assertNull("getParent", parentNode);
64636df8 280
79e08fd0
BH
281 parentNode = fStatsData.getParent(new TmfFixedArray<String>("TreeRootNode that should not exist"));
282 assertNotNull("getParent", parentNode);
283 assertEquals("getParent", 0, parentNode.getKey().compareTo(fStatsData.get(AbsTmfStatisticsTree.ROOT).getKey().toString()));
64636df8 284
79e08fd0
BH
285 parentNode = fStatsData.getParent(new TmfFixedArray<String>("TreeNode", Messages.TmfStatisticsData_EventTypes, "TreeNode that should not exist"));
286 assertNull("getParent", parentNode);
59b50985 287 parentNode = fStatsData.getParent(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName()));
79e08fd0 288 assertNull("getParent", parentNode);
64636df8 289
79e08fd0
BH
290 parentNode = fStatsData.getParent(new TmfFixedArray<String>(fTestName));
291 assertNotNull("getParent", parentNode);
292 assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(AbsTmfStatisticsTree.ROOT.toString()));
64636df8 293
79e08fd0
BH
294 parentNode = fStatsData.getParent(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes));
295 assertNotNull("getParent", parentNode);
296 assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(fStatsData.get(new TmfFixedArray<String>(fTestName)).getPath().toString()));
297 }
298
299 // ------------------------------------------------------------------------
300 // Reset
301 // ------------------------------------------------------------------------
302
64636df8
BH
303 /**
304 * Test reset method
305 */
79e08fd0
BH
306 public void testReset() {
307 fStatsData.reset(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes));
64636df8 308
79e08fd0 309 assertEquals("reset", 0, fStatsData.getChildren(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes)).size());
4c564a2d
FC
310 assertNull("reset", fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName())));
311 assertNull("reset", fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fType3.getName())));
64636df8 312
79e08fd0 313 fStatsData.reset(new TmfFixedArray<String>(fTestName));
64636df8 314
79e08fd0
BH
315 // A rootz should always have at least one child that is eventType
316 assertEquals("reset", 1, fStatsData.getChildren(new TmfFixedArray<String>(fTestName)).size());
317 }
318}
This page took 0.051282 seconds and 5 git commands to generate.