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