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