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