1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
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
10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Initial design and implementation
11 * Bernd Hufmann - Fixed warnings
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.tests
.statistics
;
16 import java
.util
.Arrays
;
17 import java
.util
.Collection
;
18 import java
.util
.Iterator
;
19 import java
.util
.Vector
;
21 import junit
.framework
.TestCase
;
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
;
33 * TmfBaseStatistics Test Cases.
35 @SuppressWarnings("nls")
36 public class TmfBaseStatisticsDataTest
extends TestCase
{
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
41 private String fTestName
= null;
43 private final String fContext
= "UnitTest";
44 private final String fTypeId1
= "Some type1";
45 private final String fTypeId2
= "Some type2";
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
};
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);
56 private final String fSource
= "Source";
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
));
62 private final String fReference
= "Some reference";
64 private final TmfEvent fEvent1
;
65 private final TmfEvent fEvent2
;
66 private final TmfEvent fEvent3
;
68 private final TmfEventField fContent1
;
69 private final TmfEventField fContent2
;
70 private final TmfEventField fContent3
;
72 private final TmfStatisticsTree fStatsTree
;
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
82 public TmfBaseStatisticsDataTest(final String name
) {
87 fContent1
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some content");
88 fEvent1
= new TmfEvent(null, fTimestamp1
, fSource
, fType1
, fContent1
, fReference
);
90 fContent2
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some other content");
91 fEvent2
= new TmfEvent(null, fTimestamp2
, fSource
, fType2
, fContent2
, fReference
);
93 fContent3
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some other different content");
94 fEvent3
= new TmfEvent(null, fTimestamp3
, fSource
, fType3
, fContent3
, fReference
);
96 fStatsTree
= new TmfStatisticsTree();
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);
104 // ------------------------------------------------------------------------
106 // ------------------------------------------------------------------------
109 * Test getting of children.
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());
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());
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());
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());
143 // Get children of a specific event type
144 childrenTreeNode
= childrenTreeNode
.iterator().next().getChildren();
145 assertEquals("getChildren", 0, childrenTreeNode
.size());
148 // ------------------------------------------------------------------------
150 // ------------------------------------------------------------------------
153 * Test registering of events.
155 public void testRegisterEvent() {
156 TmfStatisticsTreeNode trace
= fStatsTree
.getNode(fTestName
);
157 assertEquals("registerEvent", 3, trace
.getValues().getTotal());
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());
169 // ------------------------------------------------------------------------
171 // ------------------------------------------------------------------------
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());
184 // ------------------------------------------------------------------------
186 // ------------------------------------------------------------------------
189 * Test getting or creating of node entries.
191 public void testGetOrCreate() {
192 String
[] newEventType
= new String
[] { fTestName
, Messages
.TmfStatisticsData_EventTypes
, "Fancy Type" };
193 TmfStatisticsTreeNode newEventTypeNode
;
195 // newEventType is not in the tree
196 newEventTypeNode
= fStatsTree
.getNode(newEventType
);
197 assertNull(newEventTypeNode
);
199 newEventTypeNode
= fStatsTree
.getOrCreateNode(newEventType
);
200 assertNotNull(newEventTypeNode
);
201 assertTrue(Arrays
.equals(newEventType
, newEventTypeNode
.getPath()));
203 // newEventType is in the tree
204 newEventTypeNode
.reset();
205 newEventTypeNode
= fStatsTree
.getNode(newEventType
);
206 assertNotNull(newEventTypeNode
);
208 newEventTypeNode
= fStatsTree
.getOrCreateNode(newEventType
);
209 assertNotNull(newEventTypeNode
);
210 assertTrue(Arrays
.equals(newEventType
, newEventTypeNode
.getPath()));
213 // ------------------------------------------------------------------------
215 // ------------------------------------------------------------------------
218 * Test getting of parent node.
220 public void testGetParent() {
221 TmfStatisticsTreeNode parentNode
= fStatsTree
.getRootNode().getParent();
222 assertNull(parentNode
);
224 parentNode
= fStatsTree
.getNode(fTestName
).getParent();
225 assertNotNull(parentNode
);
226 assertEquals(parentNode
.getPath().toString(), fStatsTree
.getRootNode().getPath().toString());
228 parentNode
= fStatsTree
.getNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
).getParent();
229 assertNotNull(parentNode
);
230 assertEquals(parentNode
.getPath().toString(), fStatsTree
.getNode(fTestName
).getPath().toString());
233 // ------------------------------------------------------------------------
235 // ------------------------------------------------------------------------
240 public void testReset() {
241 fStatsTree
.getNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
).reset();
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()));
247 fStatsTree
.getNode(fTestName
).reset();
248 assertEquals(0, fStatsTree
.getNode(fTestName
).getChildren().size());