1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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 * Alexandre Montplaisir - Port to JUnit4
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.ui
.tests
.statistics
;
17 import static org
.junit
.Assert
.assertEquals
;
18 import static org
.junit
.Assert
.assertNotNull
;
19 import static org
.junit
.Assert
.assertNull
;
20 import static org
.junit
.Assert
.assertTrue
;
21 import static org
.junit
.Assert
.fail
;
23 import java
.util
.Arrays
;
24 import java
.util
.Collection
;
25 import java
.util
.Iterator
;
26 import java
.util
.Vector
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventType
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
35 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.statistics
.model
.Messages
;
36 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.statistics
.model
.TmfStatisticsTree
;
37 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.statistics
.model
.TmfStatisticsTreeNode
;
38 import org
.junit
.Test
;
41 * TmfBaseStatistics Test Cases.
43 public class TmfBaseStatisticsDataTest
{
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
49 private static final String fTestName
= "StatisticsDataTest";
51 private final String fTypeId1
= "Some type1";
52 private final String fTypeId2
= "Some type2";
54 private final String fLabel0
= "label1";
55 private final String fLabel1
= "label2";
56 private final String fLabel2
= "label3";
57 private final String
[] fLabels
= new String
[] { fLabel0
, fLabel1
, fLabel2
};
59 private final TmfTimestamp fTimestamp1
= new TmfTimestamp(12345, (byte) 2);
60 private final TmfTimestamp fTimestamp2
= new TmfTimestamp(12350, (byte) 2);
61 private final TmfTimestamp fTimestamp3
= new TmfTimestamp(12355, (byte) 2);
63 private final TmfEventType fType1
= new TmfEventType(fTypeId1
, TmfEventField
.makeRoot(fLabels
));
64 private final TmfEventType fType2
= new TmfEventType(fTypeId1
, TmfEventField
.makeRoot(fLabels
));
65 private final TmfEventType fType3
= new TmfEventType(fTypeId2
, TmfEventField
.makeRoot(fLabels
));
67 private final ITmfEvent fEvent1
;
68 private final ITmfEvent fEvent2
;
69 private final ITmfEvent fEvent3
;
71 private final TmfEventField fContent1
;
72 private final TmfEventField fContent2
;
73 private final TmfEventField fContent3
;
75 private final TmfStatisticsTree fStatsTree
;
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
84 public TmfBaseStatisticsDataTest() {
85 fContent1
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some content", null);
86 fEvent1
= new TmfEvent(null, ITmfContext
.UNKNOWN_RANK
, fTimestamp1
, fType1
, fContent1
);
88 fContent2
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some other content", null);
89 fEvent2
= new TmfEvent(null, ITmfContext
.UNKNOWN_RANK
, fTimestamp2
, fType2
, fContent2
);
91 fContent3
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some other different content", null);
92 fEvent3
= new TmfEvent(null, ITmfContext
.UNKNOWN_RANK
, fTimestamp3
, fType3
, fContent3
);
94 fStatsTree
= new TmfStatisticsTree();
96 fStatsTree
.setTotal(fTestName
, true, 3);
97 fStatsTree
.setTypeCount(fTestName
, fEvent1
.getType().getName(), true, 1);
98 fStatsTree
.setTypeCount(fTestName
, fEvent2
.getType().getName(), true, 1);
99 fStatsTree
.setTypeCount(fTestName
, fEvent3
.getType().getName(), true, 1);
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
107 * Test getting of children.
110 public void testGetChildren() {
111 // Getting children of the ROOT
112 Collection
<TmfStatisticsTreeNode
> childrenTreeNode
= fStatsTree
.getRootNode().getChildren();
113 assertEquals("getChildren", 1, childrenTreeNode
.size());
114 TmfStatisticsTreeNode treeNode
= childrenTreeNode
.iterator().next();
115 assertEquals("getChildren", fTestName
, treeNode
.getName());
117 // Getting children of the trace
118 childrenTreeNode
= fStatsTree
.getNode(fTestName
).getChildren();
119 assertEquals("getChildren", 1, childrenTreeNode
.size());
120 treeNode
= childrenTreeNode
.iterator().next();
121 assertEquals("getChildren", Messages
.TmfStatisticsData_EventTypes
, treeNode
.getName());
123 Vector
<String
> keyExpected
= new Vector
<>();
124 keyExpected
.add(fEvent1
.getType().getName());
125 keyExpected
.add(fEvent3
.getType().getName());
126 // Getting children of a category
127 childrenTreeNode
= treeNode
.getChildren();
128 assertEquals("getChildren", 2, childrenTreeNode
.size());
130 Iterator
<TmfStatisticsTreeNode
> iterChild
= childrenTreeNode
.iterator();
131 TmfStatisticsTreeNode temp
;
132 while (iterChild
.hasNext()) {
133 temp
= iterChild
.next();
134 assertEquals(0, temp
.getChildren().size());
135 if (keyExpected
.contains(temp
.getName())) {
136 keyExpected
.removeElement(temp
.getName());
142 // Get children of a specific event type
143 childrenTreeNode
= childrenTreeNode
.iterator().next().getChildren();
144 assertEquals("getChildren", 0, childrenTreeNode
.size());
148 * Test registering of events.
151 public void testRegisterEvent() {
152 TmfStatisticsTreeNode trace
= fStatsTree
.getNode(fTestName
);
153 assertEquals("registerEvent", 3, trace
.getValues().getTotal());
155 Collection
<TmfStatisticsTreeNode
> childrenTreeNode
= fStatsTree
.getNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
).getChildren();
156 for (TmfStatisticsTreeNode child
: childrenTreeNode
) {
157 if (child
.getName().compareTo(fEvent1
.getType().getName()) == 0) {
158 assertEquals("registerEvent", 1, child
.getValues().getTotal());
159 } else if (child
.getName().compareTo(fEvent3
.getType().getName()) == 0) {
160 assertEquals("registerEvent", 1, child
.getValues().getTotal());
169 public void testGet() {
170 TmfStatisticsTreeNode traceRoot
= fStatsTree
.getNode(fTestName
);
171 assertNotNull("get", traceRoot
);
172 assertEquals("get", 0, traceRoot
.getPath()[0].compareTo(fTestName
));
173 assertEquals("get", 3, traceRoot
.getValues().getTotal());
174 assertEquals("get", 1, traceRoot
.getNbChildren());
178 * Test getting or creating of node entries.
181 public void testGetOrCreate() {
182 String
[] newEventType
= new String
[] { fTestName
, Messages
.TmfStatisticsData_EventTypes
, "Fancy Type" };
183 TmfStatisticsTreeNode newEventTypeNode
;
185 // newEventType is not in the tree
186 newEventTypeNode
= fStatsTree
.getNode(newEventType
);
187 assertNull(newEventTypeNode
);
189 newEventTypeNode
= fStatsTree
.getOrCreateNode(newEventType
);
190 assertNotNull(newEventTypeNode
);
191 assertTrue(Arrays
.equals(newEventType
, newEventTypeNode
.getPath()));
193 // newEventType is in the tree
194 newEventTypeNode
.reset();
195 newEventTypeNode
= fStatsTree
.getNode(newEventType
);
196 assertNotNull(newEventTypeNode
);
198 newEventTypeNode
= fStatsTree
.getOrCreateNode(newEventType
);
199 assertNotNull(newEventTypeNode
);
200 assertTrue(Arrays
.equals(newEventType
, newEventTypeNode
.getPath()));
204 * Test getting of parent node.
207 public void testGetParent() {
208 TmfStatisticsTreeNode parentNode
= fStatsTree
.getRootNode().getParent();
209 assertNull(parentNode
);
211 parentNode
= fStatsTree
.getNode(fTestName
).getParent();
212 assertNotNull(parentNode
);
213 assertEquals(parentNode
.getPath().toString(), fStatsTree
.getRootNode().getPath().toString());
215 parentNode
= fStatsTree
.getNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
).getParent();
216 assertNotNull(parentNode
);
217 assertEquals(parentNode
.getPath().toString(), fStatsTree
.getNode(fTestName
).getPath().toString());
224 public void testReset() {
225 fStatsTree
.getNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
).reset();
227 assertEquals(0, fStatsTree
.getNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
).getChildren().size());
228 assertNull(fStatsTree
.getNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType1
.getName()));
229 assertNull(fStatsTree
.getNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fType3
.getName()));
231 fStatsTree
.getNode(fTestName
).reset();
232 assertEquals(0, fStatsTree
.getNode(fTestName
).getChildren().size());