0a570fe63800f329d0a48660eab9333176dc0280
[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.Arrays;
17 import java.util.Collection;
18 import java.util.Iterator;
19 import java.util.Vector;
20
21 import junit.framework.TestCase;
22
23 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
24 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
25 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
26 import org.eclipse.linuxtools.tmf.core.event.TmfEventType;
27 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
28 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.Messages;
29 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTree;
30 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode;
31
32 /**
33 * TmfBaseStatistics Test Cases.
34 */
35 @SuppressWarnings("nls")
36 public class TmfBaseStatisticsDataTest extends TestCase {
37
38 // ------------------------------------------------------------------------
39 // Fields
40 // ------------------------------------------------------------------------
41 private String fTestName = null;
42
43 private final String fContext = "UnitTest";
44 private final String fTypeId1 = "Some type1";
45 private final String fTypeId2 = "Some type2";
46
47 private final String fLabel0 = "label1";
48 private final String fLabel1 = "label2";
49 private final String fLabel2 = "label3";
50 private final String[] fLabels = new String[] { fLabel0, fLabel1, fLabel2 };
51
52 private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5);
53 private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5);
54 private final TmfTimestamp fTimestamp3 = new TmfTimestamp(12355, (byte) 2, 5);
55
56 private final String fSource = "Source";
57
58 private final TmfEventType fType1 = new TmfEventType(fContext, fTypeId1, TmfEventField.makeRoot(fLabels));
59 private final TmfEventType fType2 = new TmfEventType(fContext, fTypeId1, TmfEventField.makeRoot(fLabels));
60 private final TmfEventType fType3 = new TmfEventType(fContext, fTypeId2, TmfEventField.makeRoot(fLabels));
61
62 private final String fReference = "Some reference";
63
64 private final TmfEvent fEvent1;
65 private final TmfEvent fEvent2;
66 private final TmfEvent fEvent3;
67
68 private final TmfEventField fContent1;
69 private final TmfEventField fContent2;
70 private final TmfEventField fContent3;
71
72 private final TmfStatisticsTree fStatsTree;
73
74 // ------------------------------------------------------------------------
75 // Housekeeping
76 // ------------------------------------------------------------------------
77
78 /**
79 * @param name
80 * Test name
81 */
82 public TmfBaseStatisticsDataTest(final String name) {
83 super(name);
84
85 fTestName = name;
86
87 fContent1 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some content");
88 fEvent1 = new TmfEvent(null, fTimestamp1, fSource, fType1, fContent1, fReference);
89
90 fContent2 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other content");
91 fEvent2 = new TmfEvent(null, fTimestamp2, fSource, fType2, fContent2, fReference);
92
93 fContent3 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other different content");
94 fEvent3 = new TmfEvent(null, fTimestamp3, fSource, fType3, fContent3, fReference);
95
96 fStatsTree = new TmfStatisticsTree();
97
98 fStatsTree.setTotal(fTestName, true, 3);
99 fStatsTree.setTypeCount(fTestName, fEvent1.getType().getName(), true, 1);
100 fStatsTree.setTypeCount(fTestName, fEvent2.getType().getName(), true, 1);
101 fStatsTree.setTypeCount(fTestName, fEvent3.getType().getName(), true, 1);
102 }
103
104 // ------------------------------------------------------------------------
105 // GetChildren
106 // ------------------------------------------------------------------------
107
108 /**
109 * Test getting of children.
110 */
111 public void testGetChildren() {
112 // Getting children of the ROOT
113 Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsTree.getRootNode().getChildren();
114 assertEquals("getChildren", 1, childrenTreeNode.size());
115 TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next();
116 assertEquals("getChildren", fTestName, treeNode.getName());
117
118 // Getting children of the trace
119 childrenTreeNode = fStatsTree.getNode(fTestName).getChildren();
120 assertEquals("getChildren", 1, childrenTreeNode.size());
121 treeNode = childrenTreeNode.iterator().next();
122 assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getName());
123
124 Vector<String> keyExpected = new Vector<String>();
125 keyExpected.add(fEvent1.getType().getName());
126 keyExpected.add(fEvent3.getType().getName());
127 // Getting children of a category
128 childrenTreeNode = treeNode.getChildren();
129 assertEquals("getChildren", 2, childrenTreeNode.size());
130
131 Iterator<TmfStatisticsTreeNode> iterChild = childrenTreeNode.iterator();
132 TmfStatisticsTreeNode temp;
133 while (iterChild.hasNext()) {
134 temp = iterChild.next();
135 assertEquals(0, temp.getChildren().size());
136 if (keyExpected.contains(temp.getName())) {
137 keyExpected.removeElement(temp.getName());
138 } else {
139 fail();
140 }
141 }
142
143 // Get children of a specific event type
144 childrenTreeNode = childrenTreeNode.iterator().next().getChildren();
145 assertEquals("getChildren", 0, childrenTreeNode.size());
146 }
147
148 // ------------------------------------------------------------------------
149 // RegisterEvent
150 // ------------------------------------------------------------------------
151
152 /**
153 * Test registering of events.
154 */
155 public void testRegisterEvent() {
156 TmfStatisticsTreeNode trace = fStatsTree.getNode(fTestName);
157 assertEquals("registerEvent", 3, trace.getValues().getTotal());
158
159 Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).getChildren();
160 for (TmfStatisticsTreeNode child : childrenTreeNode) {
161 if (child.getName().compareTo(fEvent1.getType().getName()) == 0) {
162 assertEquals("registerEvent", 1, child.getValues().getTotal());
163 } else if (child.getName().compareTo(fEvent3.getType().getName()) == 0) {
164 assertEquals("registerEvent", 1, child.getValues().getTotal());
165 }
166 }
167 }
168
169 // ------------------------------------------------------------------------
170 // Get a node
171 // ------------------------------------------------------------------------
172
173 /**
174 * Test getter.
175 */
176 public void testGet() {
177 TmfStatisticsTreeNode traceRoot = fStatsTree.getNode(fTestName);
178 assertNotNull("get", traceRoot);
179 assertEquals("get", 0, traceRoot.getPath()[0].compareTo(fTestName));
180 assertEquals("get", 3, traceRoot.getValues().getTotal());
181 assertEquals("get", 1, traceRoot.getNbChildren());
182 }
183
184 // ------------------------------------------------------------------------
185 // GetOrCreate
186 // ------------------------------------------------------------------------
187
188 /**
189 * Test getting or creating of node entries.
190 */
191 public void testGetOrCreate() {
192 String[] newEventType = new String[] { fTestName, Messages.TmfStatisticsData_EventTypes, "Fancy Type" };
193 TmfStatisticsTreeNode newEventTypeNode;
194
195 // newEventType is not in the tree
196 newEventTypeNode = fStatsTree.getNode(newEventType);
197 assertNull(newEventTypeNode);
198
199 newEventTypeNode = fStatsTree.getOrCreateNode(newEventType);
200 assertNotNull(newEventTypeNode);
201 assertTrue(Arrays.equals(newEventType, newEventTypeNode.getPath()));
202
203 // newEventType is in the tree
204 newEventTypeNode.reset();
205 newEventTypeNode = fStatsTree.getNode(newEventType);
206 assertNotNull(newEventTypeNode);
207
208 newEventTypeNode = fStatsTree.getOrCreateNode(newEventType);
209 assertNotNull(newEventTypeNode);
210 assertTrue(Arrays.equals(newEventType, newEventTypeNode.getPath()));
211 }
212
213 // ------------------------------------------------------------------------
214 // GetParent
215 // ------------------------------------------------------------------------
216
217 /**
218 * Test getting of parent node.
219 */
220 public void testGetParent() {
221 TmfStatisticsTreeNode parentNode = fStatsTree.getRootNode().getParent();
222 assertNull(parentNode);
223
224 parentNode = fStatsTree.getNode(fTestName).getParent();
225 assertNotNull(parentNode);
226 assertEquals(parentNode.getPath().toString(), fStatsTree.getRootNode().getPath().toString());
227
228 parentNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).getParent();
229 assertNotNull(parentNode);
230 assertEquals(parentNode.getPath().toString(), fStatsTree.getNode(fTestName).getPath().toString());
231 }
232
233 // ------------------------------------------------------------------------
234 // Reset
235 // ------------------------------------------------------------------------
236
237 /**
238 * Test reset method
239 */
240 public void testReset() {
241 fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).reset();
242
243 assertEquals(0, fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).getChildren().size());
244 assertNull(fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName()));
245 assertNull(fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fType3.getName()));
246
247 fStatsTree.getNode(fTestName).reset();
248 assertEquals(0, fStatsTree.getNode(fTestName).getChildren().size());
249 }
250 }
This page took 0.076257 seconds and 5 git commands to generate.