| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2013 Ericsson |
| 3 | * |
| 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 |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Mathieu Denis <mathieu.denis@polymtl.ca> - Initial design and implementation |
| 11 | * Bernd Hufmann - Fixed warnings |
| 12 | * Alexandre Montplaisir - Port to JUnit4 |
| 13 | *******************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.linuxtools.tmf.ui.tests.statistics; |
| 16 | |
| 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 | |
| 23 | import java.util.Arrays; |
| 24 | import java.util.Collection; |
| 25 | import java.util.Iterator; |
| 26 | import java.util.Vector; |
| 27 | |
| 28 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 29 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
| 30 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
| 31 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
| 32 | import org.eclipse.linuxtools.tmf.core.event.TmfEventType; |
| 33 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; |
| 34 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.Messages; |
| 35 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTree; |
| 36 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode; |
| 37 | import org.junit.Test; |
| 38 | |
| 39 | /** |
| 40 | * TmfBaseStatistics Test Cases. |
| 41 | */ |
| 42 | public class TmfBaseStatisticsDataTest { |
| 43 | |
| 44 | // ------------------------------------------------------------------------ |
| 45 | // Fields |
| 46 | // ------------------------------------------------------------------------ |
| 47 | |
| 48 | private static final String fTestName = "StatisticsDataTest"; |
| 49 | |
| 50 | private final String fContext = "UnitTest"; |
| 51 | private final String fTypeId1 = "Some type1"; |
| 52 | private final String fTypeId2 = "Some type2"; |
| 53 | |
| 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 | |
| 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); |
| 62 | |
| 63 | private final String fSource = "Source"; |
| 64 | |
| 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)); |
| 68 | |
| 69 | private final String fReference = "Some reference"; |
| 70 | |
| 71 | private final ITmfEvent fEvent1; |
| 72 | private final ITmfEvent fEvent2; |
| 73 | private final ITmfEvent fEvent3; |
| 74 | |
| 75 | private final TmfEventField fContent1; |
| 76 | private final TmfEventField fContent2; |
| 77 | private final TmfEventField fContent3; |
| 78 | |
| 79 | private final TmfStatisticsTree fStatsTree; |
| 80 | |
| 81 | // ------------------------------------------------------------------------ |
| 82 | // Housekeeping |
| 83 | // ------------------------------------------------------------------------ |
| 84 | |
| 85 | /** |
| 86 | * Constructor |
| 87 | */ |
| 88 | public TmfBaseStatisticsDataTest() { |
| 89 | fContent1 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some content"); |
| 90 | fEvent1 = new TmfEvent(null, fTimestamp1, fSource, fType1, fContent1, fReference); |
| 91 | |
| 92 | fContent2 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other content"); |
| 93 | fEvent2 = new TmfEvent(null, fTimestamp2, fSource, fType2, fContent2, fReference); |
| 94 | |
| 95 | fContent3 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other different content"); |
| 96 | fEvent3 = new TmfEvent(null, fTimestamp3, fSource, fType3, fContent3, fReference); |
| 97 | |
| 98 | fStatsTree = new TmfStatisticsTree(); |
| 99 | |
| 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); |
| 104 | } |
| 105 | |
| 106 | // ------------------------------------------------------------------------ |
| 107 | // Test methods |
| 108 | // ------------------------------------------------------------------------ |
| 109 | |
| 110 | /** |
| 111 | * Test getting of children. |
| 112 | */ |
| 113 | @Test |
| 114 | public void testGetChildren() { |
| 115 | // Getting children of the ROOT |
| 116 | Collection<TmfStatisticsTreeNode> childrenTreeNode = fStatsTree.getRootNode().getChildren(); |
| 117 | assertEquals("getChildren", 1, childrenTreeNode.size()); |
| 118 | TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next(); |
| 119 | assertEquals("getChildren", fTestName, treeNode.getName()); |
| 120 | |
| 121 | // Getting children of the trace |
| 122 | childrenTreeNode = fStatsTree.getNode(fTestName).getChildren(); |
| 123 | assertEquals("getChildren", 1, childrenTreeNode.size()); |
| 124 | treeNode = childrenTreeNode.iterator().next(); |
| 125 | assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getName()); |
| 126 | |
| 127 | Vector<String> keyExpected = new Vector<String>(); |
| 128 | keyExpected.add(fEvent1.getType().getName()); |
| 129 | keyExpected.add(fEvent3.getType().getName()); |
| 130 | // Getting children of a category |
| 131 | childrenTreeNode = treeNode.getChildren(); |
| 132 | assertEquals("getChildren", 2, childrenTreeNode.size()); |
| 133 | |
| 134 | Iterator<TmfStatisticsTreeNode> iterChild = childrenTreeNode.iterator(); |
| 135 | TmfStatisticsTreeNode temp; |
| 136 | while (iterChild.hasNext()) { |
| 137 | temp = iterChild.next(); |
| 138 | assertEquals(0, temp.getChildren().size()); |
| 139 | if (keyExpected.contains(temp.getName())) { |
| 140 | keyExpected.removeElement(temp.getName()); |
| 141 | } else { |
| 142 | fail(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Get children of a specific event type |
| 147 | childrenTreeNode = childrenTreeNode.iterator().next().getChildren(); |
| 148 | assertEquals("getChildren", 0, childrenTreeNode.size()); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Test registering of events. |
| 153 | */ |
| 154 | @Test |
| 155 | public void testRegisterEvent() { |
| 156 | TmfStatisticsTreeNode trace = fStatsTree.getNode(fTestName); |
| 157 | assertEquals("registerEvent", 3, trace.getValues().getTotal()); |
| 158 | |
| 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()); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Test getter. |
| 171 | */ |
| 172 | @Test |
| 173 | public void testGet() { |
| 174 | TmfStatisticsTreeNode traceRoot = fStatsTree.getNode(fTestName); |
| 175 | assertNotNull("get", traceRoot); |
| 176 | assertEquals("get", 0, traceRoot.getPath()[0].compareTo(fTestName)); |
| 177 | assertEquals("get", 3, traceRoot.getValues().getTotal()); |
| 178 | assertEquals("get", 1, traceRoot.getNbChildren()); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Test getting or creating of node entries. |
| 183 | */ |
| 184 | @Test |
| 185 | public void testGetOrCreate() { |
| 186 | String[] newEventType = new String[] { fTestName, Messages.TmfStatisticsData_EventTypes, "Fancy Type" }; |
| 187 | TmfStatisticsTreeNode newEventTypeNode; |
| 188 | |
| 189 | // newEventType is not in the tree |
| 190 | newEventTypeNode = fStatsTree.getNode(newEventType); |
| 191 | assertNull(newEventTypeNode); |
| 192 | |
| 193 | newEventTypeNode = fStatsTree.getOrCreateNode(newEventType); |
| 194 | assertNotNull(newEventTypeNode); |
| 195 | assertTrue(Arrays.equals(newEventType, newEventTypeNode.getPath())); |
| 196 | |
| 197 | // newEventType is in the tree |
| 198 | newEventTypeNode.reset(); |
| 199 | newEventTypeNode = fStatsTree.getNode(newEventType); |
| 200 | assertNotNull(newEventTypeNode); |
| 201 | |
| 202 | newEventTypeNode = fStatsTree.getOrCreateNode(newEventType); |
| 203 | assertNotNull(newEventTypeNode); |
| 204 | assertTrue(Arrays.equals(newEventType, newEventTypeNode.getPath())); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Test getting of parent node. |
| 209 | */ |
| 210 | @Test |
| 211 | public void testGetParent() { |
| 212 | TmfStatisticsTreeNode parentNode = fStatsTree.getRootNode().getParent(); |
| 213 | assertNull(parentNode); |
| 214 | |
| 215 | parentNode = fStatsTree.getNode(fTestName).getParent(); |
| 216 | assertNotNull(parentNode); |
| 217 | assertEquals(parentNode.getPath().toString(), fStatsTree.getRootNode().getPath().toString()); |
| 218 | |
| 219 | parentNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).getParent(); |
| 220 | assertNotNull(parentNode); |
| 221 | assertEquals(parentNode.getPath().toString(), fStatsTree.getNode(fTestName).getPath().toString()); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Test reset method |
| 226 | */ |
| 227 | @Test |
| 228 | public void testReset() { |
| 229 | fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).reset(); |
| 230 | |
| 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())); |
| 234 | |
| 235 | fStatsTree.getNode(fTestName).reset(); |
| 236 | assertEquals(0, fStatsTree.getNode(fTestName).getChildren().size()); |
| 237 | } |
| 238 | } |