Commit | Line | Data |
---|---|---|
79e08fd0 BH |
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 | ||
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; | |
79e08fd0 | 28 | import org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfExtraEventInfo; |
99005796 | 29 | import org.eclipse.linuxtools.tmf.ui.views.statistics.model.AbsTmfStatisticsTree; |
79e08fd0 BH |
30 | import org.eclipse.linuxtools.tmf.ui.views.statistics.model.Messages; |
31 | import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseStatisticsTree; | |
79e08fd0 | 32 | import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsTreeNode; |
79e08fd0 BH |
33 | |
34 | @SuppressWarnings("nls") | |
35 | public class TmfBaseStatisticsDataTest extends TestCase { | |
36 | ||
37 | // ------------------------------------------------------------------------ | |
38 | // Fields | |
39 | // ------------------------------------------------------------------------ | |
40 | private String fTestName = null; | |
41 | ||
cbbcc354 | 42 | private final String fContext = "UnitTest"; |
43 | private final String fTypeId1 = "Some type1"; | |
44 | private final String fTypeId2 = "Some type2"; | |
79e08fd0 BH |
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 | ||
4641c2f7 FC |
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); | |
79e08fd0 | 54 | |
4641c2f7 | 55 | private final String fSource = "Source"; |
79e08fd0 | 56 | |
4c564a2d FC |
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)); | |
79e08fd0 | 60 | |
4641c2f7 | 61 | private final String fReference = "Some reference"; |
79e08fd0 BH |
62 | |
63 | private final TmfEvent fEvent1; | |
64 | private final TmfEvent fEvent2; | |
65 | private final TmfEvent fEvent3; | |
66 | ||
4c564a2d FC |
67 | private final TmfEventField fContent1; |
68 | private final TmfEventField fContent2; | |
69 | private final TmfEventField fContent3; | |
79e08fd0 BH |
70 | |
71 | private final TmfBaseStatisticsTree fStatsData; | |
72 | ||
73 | private final ITmfExtraEventInfo fExtraInfo; | |
74 | ||
75 | // ------------------------------------------------------------------------ | |
76 | // Housekeeping | |
77 | // ------------------------------------------------------------------------ | |
78 | ||
79 | /** | |
80 | * @param name of the test | |
81 | */ | |
82 | public TmfBaseStatisticsDataTest(final String name) { | |
83 | super(name); | |
84 | ||
85 | fTestName = name; | |
86 | ||
87 | fEvent1 = new TmfEvent(fTimestamp1, fSource, fType1, fReference); | |
4c564a2d | 88 | fContent1 = new TmfEventField(ITmfEventField.ROOT_ID, "Some content"); |
79e08fd0 BH |
89 | fEvent1.setContent(fContent1); |
90 | ||
ce970a71 | 91 | fEvent2 = new TmfEvent(fTimestamp2, fSource, fType2, fReference); |
4c564a2d | 92 | fContent2 = new TmfEventField(ITmfEventField.ROOT_ID, "Some other content"); |
79e08fd0 BH |
93 | fEvent2.setContent(fContent2); |
94 | ||
ce970a71 | 95 | fEvent3 = new TmfEvent(fTimestamp3, fSource, fType3, fReference); |
4c564a2d | 96 | fContent3 = new TmfEventField(ITmfEventField.ROOT_ID, "Some other different content"); |
79e08fd0 BH |
97 | fEvent3.setContent(fContent3); |
98 | ||
99 | fStatsData = new TmfBaseStatisticsTree(); | |
100 | fExtraInfo = new ITmfExtraEventInfo() { | |
101 | @Override | |
102 | public String getTraceName() { | |
103 | return name; | |
104 | } | |
105 | }; | |
106 | fStatsData.registerEvent(fEvent1, fExtraInfo); | |
107 | fStatsData.registerEvent(fEvent2, fExtraInfo); | |
108 | fStatsData.registerEvent(fEvent3, fExtraInfo); | |
109 | } | |
110 | ||
111 | // ------------------------------------------------------------------------ | |
112 | // GetChildren | |
113 | // ------------------------------------------------------------------------ | |
114 | ||
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()); | |
121 | ||
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()); | |
127 | ||
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()); | |
134 | ||
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()); | |
141 | } | |
142 | else { | |
143 | fail(); | |
144 | } | |
145 | } | |
146 | ||
147 | // Get children of a specific event type | |
148 | childrenTreeNode = fStatsData.getChildren(childrenTreeNode.iterator().next().getPath()); | |
149 | assertEquals("getChildren", 0, childrenTreeNode.size()); | |
150 | } | |
151 | ||
152 | // ------------------------------------------------------------------------ | |
153 | // GetAllChildren | |
154 | // ------------------------------------------------------------------------ | |
155 | ||
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()); | |
162 | ||
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()); | |
168 | ||
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()); | |
177 | ||
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()); | |
184 | } | |
185 | else { | |
186 | fail(); | |
187 | } | |
188 | } | |
189 | ||
190 | // Get children of a specific event type | |
191 | childrenTreeNode = fStatsData.getAllChildren(childrenTreeNode.iterator().next().getPath()); | |
192 | assertEquals("getChildren", 0, childrenTreeNode.size()); | |
193 | } | |
194 | ||
195 | // ------------------------------------------------------------------------ | |
196 | // RegisterEvent | |
197 | // ------------------------------------------------------------------------ | |
198 | ||
199 | public void testRegisterEvent() { | |
200 | TmfStatisticsTreeNode trace = fStatsData.get(new TmfFixedArray<String>(fTestName)); | |
201 | assertEquals("registerEvent", 3, trace.getValue().nbEvents); | |
202 | ||
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); | |
207 | } | |
208 | else if (child.getKey().compareTo(fEvent3.getType().toString()) == 0) { | |
209 | assertEquals("registerEvent", 1, child.getValue().nbEvents); | |
210 | } | |
211 | } | |
212 | } | |
213 | ||
214 | // ------------------------------------------------------------------------ | |
215 | // Get a node | |
216 | // ------------------------------------------------------------------------ | |
217 | ||
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()); | |
224 | } | |
225 | ||
226 | // ------------------------------------------------------------------------ | |
227 | // GetOrCreate | |
228 | // ------------------------------------------------------------------------ | |
229 | ||
230 | public void testGetOrCreate() { | |
231 | TmfFixedArray<String> newEventType = new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, "Fancy Type"); | |
232 | TmfStatisticsTreeNode newEventTypeNode; | |
233 | ||
234 | // newEventType is not in the tree | |
235 | newEventTypeNode = fStatsData.get(newEventType); | |
236 | assertNull("getOrCreate", newEventTypeNode); | |
237 | ||
238 | newEventTypeNode = fStatsData.getOrCreate(newEventType); | |
239 | assertNotNull("getOrCreate", newEventTypeNode); | |
240 | assertTrue("getOrCreate", newEventTypeNode.getPath().equals(newEventType)); | |
241 | ||
242 | // newEventType is in the tree | |
243 | newEventTypeNode.reset(); | |
244 | newEventTypeNode = fStatsData.get(newEventType); | |
245 | assertNotNull("getOrCreate", newEventTypeNode); | |
246 | ||
247 | newEventTypeNode = fStatsData.getOrCreate(newEventType); | |
248 | assertNotNull("getOrCreate", newEventTypeNode); | |
249 | assertTrue("getOrCreate", newEventTypeNode.getPath().equals(newEventType)); | |
250 | } | |
251 | ||
252 | // ------------------------------------------------------------------------ | |
253 | // GetParent | |
254 | // ------------------------------------------------------------------------ | |
255 | ||
256 | public void testGetParent() { | |
257 | TmfStatisticsTreeNode parentNode = fStatsData.getParent(AbsTmfStatisticsTree.ROOT); | |
258 | assertNull("getParent", parentNode); | |
259 | ||
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())); | |
263 | ||
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); | |
268 | ||
269 | parentNode = fStatsData.getParent(new TmfFixedArray<String>(fTestName)); | |
270 | assertNotNull("getParent", parentNode); | |
271 | assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(AbsTmfStatisticsTree.ROOT.toString())); | |
272 | ||
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())); | |
276 | } | |
277 | ||
278 | // ------------------------------------------------------------------------ | |
279 | // Reset | |
280 | // ------------------------------------------------------------------------ | |
281 | ||
282 | public void testReset() { | |
283 | fStatsData.reset(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes)); | |
284 | ||
285 | assertEquals("reset", 0, fStatsData.getChildren(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes)).size()); | |
4c564a2d FC |
286 | assertNull("reset", fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName()))); |
287 | assertNull("reset", fStatsData.get(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fType3.getName()))); | |
79e08fd0 BH |
288 | |
289 | fStatsData.reset(new TmfFixedArray<String>(fTestName)); | |
290 | ||
291 | // A rootz should always have at least one child that is eventType | |
292 | assertEquals("reset", 1, fStatsData.getChildren(new TmfFixedArray<String>(fTestName)).size()); | |
293 | } | |
294 | } |