Commit | Line | Data |
---|---|---|
79e08fd0 | 1 | /******************************************************************************* |
90ed5958 | 2 | * Copyright (c) 2011, 2013 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 | 11 | * Bernd Hufmann - Fixed warnings |
90ed5958 | 12 | * Alexandre Montplaisir - Port to JUnit4 |
79e08fd0 BH |
13 | *******************************************************************************/ |
14 | ||
2bdf0193 | 15 | package org.eclipse.tracecompass.tmf.ui.tests.statistics; |
79e08fd0 | 16 | |
90ed5958 AM |
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; | |
22 | ||
7588c810 | 23 | import java.util.Arrays; |
79e08fd0 BH |
24 | import java.util.Collection; |
25 | import java.util.Iterator; | |
26 | import java.util.Vector; | |
27 | ||
2bdf0193 AM |
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.ui.viewers.statistics.model.Messages; | |
35 | import org.eclipse.tracecompass.tmf.ui.viewers.statistics.model.TmfStatisticsTree; | |
36 | import org.eclipse.tracecompass.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode; | |
90ed5958 | 37 | import org.junit.Test; |
79e08fd0 | 38 | |
64636df8 BH |
39 | /** |
40 | * TmfBaseStatistics Test Cases. | |
41 | */ | |
90ed5958 | 42 | public class TmfBaseStatisticsDataTest { |
64636df8 | 43 | |
79e08fd0 BH |
44 | // ------------------------------------------------------------------------ |
45 | // Fields | |
46 | // ------------------------------------------------------------------------ | |
90ed5958 AM |
47 | |
48 | private static final String fTestName = "StatisticsDataTest"; | |
64636df8 | 49 | |
cbbcc354 | 50 | private final String fTypeId1 = "Some type1"; |
51 | private final String fTypeId2 = "Some type2"; | |
64636df8 | 52 | |
79e08fd0 BH |
53 | private final String fLabel0 = "label1"; |
54 | private final String fLabel1 = "label2"; | |
55 | private final String fLabel2 = "label3"; | |
56 | private final String[] fLabels = new String[] { fLabel0, fLabel1, fLabel2 }; | |
57 | ||
4641c2f7 FC |
58 | private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5); |
59 | private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5); | |
60 | private final TmfTimestamp fTimestamp3 = new TmfTimestamp(12355, (byte) 2, 5); | |
64636df8 | 61 | |
4641c2f7 | 62 | private final String fSource = "Source"; |
64636df8 | 63 | |
e600c338 AM |
64 | private final TmfEventType fType1 = new TmfEventType(fTypeId1, TmfEventField.makeRoot(fLabels)); |
65 | private final TmfEventType fType2 = new TmfEventType(fTypeId1, TmfEventField.makeRoot(fLabels)); | |
66 | private final TmfEventType fType3 = new TmfEventType(fTypeId2, TmfEventField.makeRoot(fLabels)); | |
64636df8 | 67 | |
4641c2f7 | 68 | private final String fReference = "Some reference"; |
79e08fd0 | 69 | |
2771b032 AM |
70 | private final ITmfEvent fEvent1; |
71 | private final ITmfEvent fEvent2; | |
72 | private final ITmfEvent fEvent3; | |
79e08fd0 | 73 | |
4c564a2d FC |
74 | private final TmfEventField fContent1; |
75 | private final TmfEventField fContent2; | |
76 | private final TmfEventField fContent3; | |
64636df8 | 77 | |
7588c810 | 78 | private final TmfStatisticsTree fStatsTree; |
64636df8 | 79 | |
79e08fd0 BH |
80 | // ------------------------------------------------------------------------ |
81 | // Housekeeping | |
82 | // ------------------------------------------------------------------------ | |
64636df8 | 83 | |
79e08fd0 | 84 | /** |
90ed5958 | 85 | * Constructor |
79e08fd0 | 86 | */ |
90ed5958 | 87 | public TmfBaseStatisticsDataTest() { |
214cc822 | 88 | fContent1 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some content", null); |
7b477cc3 | 89 | fEvent1 = new TmfEvent(null, fTimestamp1, fSource, fType1, fContent1, fReference); |
79e08fd0 | 90 | |
214cc822 | 91 | fContent2 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other content", null); |
7b477cc3 | 92 | fEvent2 = new TmfEvent(null, fTimestamp2, fSource, fType2, fContent2, fReference); |
64636df8 | 93 | |
214cc822 | 94 | fContent3 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other different content", null); |
7b477cc3 | 95 | fEvent3 = new TmfEvent(null, fTimestamp3, fSource, fType3, fContent3, fReference); |
64636df8 | 96 | |
7588c810 | 97 | fStatsTree = new TmfStatisticsTree(); |
89c06060 | 98 | |
7588c810 AM |
99 | fStatsTree.setTotal(fTestName, true, 3); |
100 | fStatsTree.setTypeCount(fTestName, fEvent1.getType().getName(), true, 1); | |
101 | fStatsTree.setTypeCount(fTestName, fEvent2.getType().getName(), true, 1); | |
102 | fStatsTree.setTypeCount(fTestName, fEvent3.getType().getName(), true, 1); | |
79e08fd0 | 103 | } |
64636df8 | 104 | |
79e08fd0 | 105 | // ------------------------------------------------------------------------ |
90ed5958 | 106 | // Test methods |
79e08fd0 BH |
107 | // ------------------------------------------------------------------------ |
108 | ||
64636df8 BH |
109 | /** |
110 | * Test getting of children. | |
111 | */ | |
90ed5958 | 112 | @Test |
79e08fd0 BH |
113 | public void testGetChildren() { |
114 | // Getting children of the ROOT | |
7588c810 | 115 | Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsTree.getRootNode().getChildren(); |
79e08fd0 BH |
116 | assertEquals("getChildren", 1, childrenTreeNode.size()); |
117 | TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next(); | |
7588c810 | 118 | assertEquals("getChildren", fTestName, treeNode.getName()); |
64636df8 | 119 | |
79e08fd0 | 120 | // Getting children of the trace |
7588c810 | 121 | childrenTreeNode = fStatsTree.getNode(fTestName).getChildren(); |
79e08fd0 BH |
122 | assertEquals("getChildren", 1, childrenTreeNode.size()); |
123 | treeNode = childrenTreeNode.iterator().next(); | |
7588c810 | 124 | assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getName()); |
64636df8 | 125 | |
ab410d88 | 126 | Vector<String> keyExpected = new Vector<>(); |
59b50985 MD |
127 | keyExpected.add(fEvent1.getType().getName()); |
128 | keyExpected.add(fEvent3.getType().getName()); | |
79e08fd0 | 129 | // Getting children of a category |
7588c810 | 130 | childrenTreeNode = treeNode.getChildren(); |
79e08fd0 | 131 | assertEquals("getChildren", 2, childrenTreeNode.size()); |
64636df8 | 132 | |
79e08fd0 BH |
133 | Iterator<TmfStatisticsTreeNode> iterChild = childrenTreeNode.iterator(); |
134 | TmfStatisticsTreeNode temp; | |
135 | while (iterChild.hasNext()) { | |
136 | temp = iterChild.next(); | |
7588c810 AM |
137 | assertEquals(0, temp.getChildren().size()); |
138 | if (keyExpected.contains(temp.getName())) { | |
139 | keyExpected.removeElement(temp.getName()); | |
09667aa4 | 140 | } else { |
79e08fd0 BH |
141 | fail(); |
142 | } | |
143 | } | |
64636df8 | 144 | |
79e08fd0 | 145 | // Get children of a specific event type |
7588c810 | 146 | childrenTreeNode = childrenTreeNode.iterator().next().getChildren(); |
79e08fd0 BH |
147 | assertEquals("getChildren", 0, childrenTreeNode.size()); |
148 | } | |
149 | ||
64636df8 BH |
150 | /** |
151 | * Test registering of events. | |
152 | */ | |
90ed5958 | 153 | @Test |
79e08fd0 | 154 | public void testRegisterEvent() { |
7588c810 | 155 | TmfStatisticsTreeNode trace = fStatsTree.getNode(fTestName); |
89c06060 | 156 | assertEquals("registerEvent", 3, trace.getValues().getTotal()); |
64636df8 | 157 | |
7588c810 | 158 | Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).getChildren(); |
79e08fd0 | 159 | for (TmfStatisticsTreeNode child : childrenTreeNode) { |
7588c810 | 160 | if (child.getName().compareTo(fEvent1.getType().getName()) == 0) { |
89c06060 | 161 | assertEquals("registerEvent", 1, child.getValues().getTotal()); |
7588c810 | 162 | } else if (child.getName().compareTo(fEvent3.getType().getName()) == 0) { |
89c06060 | 163 | assertEquals("registerEvent", 1, child.getValues().getTotal()); |
79e08fd0 BH |
164 | } |
165 | } | |
166 | } | |
64636df8 | 167 | |
64636df8 BH |
168 | /** |
169 | * Test getter. | |
170 | */ | |
90ed5958 | 171 | @Test |
79e08fd0 | 172 | public void testGet() { |
7588c810 | 173 | TmfStatisticsTreeNode traceRoot = fStatsTree.getNode(fTestName); |
79e08fd0 | 174 | assertNotNull("get", traceRoot); |
5673a177 | 175 | assertEquals("get", 0, traceRoot.getPath()[0].compareTo(fTestName)); |
89c06060 | 176 | assertEquals("get", 3, traceRoot.getValues().getTotal()); |
79e08fd0 BH |
177 | assertEquals("get", 1, traceRoot.getNbChildren()); |
178 | } | |
64636df8 | 179 | |
64636df8 BH |
180 | /** |
181 | * Test getting or creating of node entries. | |
182 | */ | |
90ed5958 | 183 | @Test |
79e08fd0 | 184 | public void testGetOrCreate() { |
5673a177 | 185 | String[] newEventType = new String[] { fTestName, Messages.TmfStatisticsData_EventTypes, "Fancy Type" }; |
79e08fd0 | 186 | TmfStatisticsTreeNode newEventTypeNode; |
64636df8 | 187 | |
79e08fd0 | 188 | // newEventType is not in the tree |
7588c810 AM |
189 | newEventTypeNode = fStatsTree.getNode(newEventType); |
190 | assertNull(newEventTypeNode); | |
64636df8 | 191 | |
7588c810 AM |
192 | newEventTypeNode = fStatsTree.getOrCreateNode(newEventType); |
193 | assertNotNull(newEventTypeNode); | |
194 | assertTrue(Arrays.equals(newEventType, newEventTypeNode.getPath())); | |
64636df8 | 195 | |
79e08fd0 BH |
196 | // newEventType is in the tree |
197 | newEventTypeNode.reset(); | |
7588c810 AM |
198 | newEventTypeNode = fStatsTree.getNode(newEventType); |
199 | assertNotNull(newEventTypeNode); | |
64636df8 | 200 | |
7588c810 AM |
201 | newEventTypeNode = fStatsTree.getOrCreateNode(newEventType); |
202 | assertNotNull(newEventTypeNode); | |
203 | assertTrue(Arrays.equals(newEventType, newEventTypeNode.getPath())); | |
79e08fd0 | 204 | } |
64636df8 | 205 | |
64636df8 BH |
206 | /** |
207 | * Test getting of parent node. | |
208 | */ | |
90ed5958 | 209 | @Test |
79e08fd0 | 210 | public void testGetParent() { |
7588c810 AM |
211 | TmfStatisticsTreeNode parentNode = fStatsTree.getRootNode().getParent(); |
212 | assertNull(parentNode); | |
64636df8 | 213 | |
7588c810 AM |
214 | parentNode = fStatsTree.getNode(fTestName).getParent(); |
215 | assertNotNull(parentNode); | |
216 | assertEquals(parentNode.getPath().toString(), fStatsTree.getRootNode().getPath().toString()); | |
64636df8 | 217 | |
7588c810 AM |
218 | parentNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).getParent(); |
219 | assertNotNull(parentNode); | |
220 | assertEquals(parentNode.getPath().toString(), fStatsTree.getNode(fTestName).getPath().toString()); | |
79e08fd0 BH |
221 | } |
222 | ||
64636df8 BH |
223 | /** |
224 | * Test reset method | |
225 | */ | |
90ed5958 | 226 | @Test |
79e08fd0 | 227 | public void testReset() { |
7588c810 | 228 | fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).reset(); |
64636df8 | 229 | |
7588c810 AM |
230 | assertEquals(0, fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).getChildren().size()); |
231 | assertNull(fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName())); | |
232 | assertNull(fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fType3.getName())); | |
64636df8 | 233 | |
7588c810 AM |
234 | fStatsTree.getNode(fTestName).reset(); |
235 | assertEquals(0, fStatsTree.getNode(fTestName).getChildren().size()); | |
79e08fd0 BH |
236 | } |
237 | } |