tmf: Refactor TMF statistics
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.tests / src / org / eclipse / linuxtools / tmf / ui / tests / statistics / TmfTreeContentProviderTest.java
CommitLineData
79e08fd0
BH
1/*******************************************************************************
2 * Copyright (c) 2011 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
BH
11 * Bernd Hufmann - Fixed warnings
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.tmf.ui.tests.statistics;
15
16import java.util.Arrays;
79e08fd0
BH
17
18import junit.framework.TestCase;
19
4c564a2d 20import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
6c13869b 21import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
4c564a2d 22import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
6c13869b
FC
23import org.eclipse.linuxtools.tmf.core.event.TmfEventType;
24import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
cfd22ad0 25import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.Messages;
36033ff0 26import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTree;
cfd22ad0
MD
27import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode;
28import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfTreeContentProvider;
79e08fd0 29
64636df8
BH
30/**
31 * TmfTreeContentProvider Test Cases.
32 */
79e08fd0
BH
33@SuppressWarnings("nls")
34public class TmfTreeContentProviderTest extends TestCase {
35
36 // ------------------------------------------------------------------------
37 // Fields
38 // ------------------------------------------------------------------------
39
40 private String fTestName = null;
41
cbbcc354 42 private final String fContext = "UnitTest";
79e08fd0
BH
43 private final String fTypeId1 = "Some type1";
44 private final String fTypeId2 = "Some type2";
64636df8 45
79e08fd0
BH
46 private final String fLabel0 = "label1";
47 private final String fLabel1 = "label2";
48 private final String[] fLabels = new String[] { fLabel0, fLabel1 };
49
50 private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5);
51 private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5);
52
99005796 53 private final String fSource = "Source";
79e08fd0 54
4c564a2d
FC
55 private final TmfEventType fType1 = new TmfEventType(fContext, fTypeId1, TmfEventField.makeRoot(fLabels));
56 private final TmfEventType fType2 = new TmfEventType(fContext, fTypeId2, TmfEventField.makeRoot(fLabels));
79e08fd0 57
4641c2f7 58 private final String fReference = "Some reference";
79e08fd0
BH
59
60 private final TmfEvent fEvent1;
61 private final TmfEvent fEvent2;
62
4c564a2d
FC
63 private final TmfEventField fContent1;
64 private final TmfEventField fContent2;
79e08fd0 65
36033ff0 66 private final TmfStatisticsTree fStatsData;
79e08fd0 67
79e08fd0
BH
68 private final TmfTreeContentProvider treeProvider;
69
70 // ------------------------------------------------------------------------
71 // Housekeeping
72 // ------------------------------------------------------------------------
73
74 /**
75 * @param name
76 * of the test
77 */
78 public TmfTreeContentProviderTest(final String name) {
79 super(name);
80
81 fTestName = name;
82
a4115405 83 fContent1 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some content");
7b477cc3 84 fEvent1 = new TmfEvent(null, fTimestamp1, fSource, fType1, fContent1, fReference);
79e08fd0 85
a4115405 86 fContent2 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other content");
7b477cc3 87 fEvent2 = new TmfEvent(null, fTimestamp2, fSource, fType2, fContent2, fReference);
79e08fd0 88
36033ff0 89 fStatsData = new TmfStatisticsTree();
89c06060
AM
90
91 fStatsData.setTotal(fTestName, true, 2);
92 fStatsData.setTypeCount(fTestName, fEvent1.getType().getName(), true, 1);
93 fStatsData.setTypeCount(fTestName, fEvent2.getType().getName(), true, 1);
79e08fd0
BH
94
95 treeProvider = new TmfTreeContentProvider();
96 }
97
98 // ------------------------------------------------------------------------
99 // GetChildren
100 // ------------------------------------------------------------------------
101
64636df8
BH
102 /**
103 * Test getting of children.
5673a177
AM
104 * FIXME this test was quickly adapted when we removed the TmfFixedArray,
105 * but it could be rewritten to be much more simple...
64636df8 106 */
79e08fd0 107 public void testGetChildren() {
5673a177 108 Object[] objectArray = treeProvider.getChildren(fStatsData.getOrCreate(fTestName, Messages.TmfStatisticsData_EventTypes));
79e08fd0
BH
109 TmfStatisticsTreeNode[] childrenNode = Arrays.asList(objectArray).toArray(new TmfStatisticsTreeNode[0]);
110
5673a177
AM
111 String[][] childrenExpected = new String[][] {
112 new String[] { fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName() },
113 new String[] { fTestName, Messages.TmfStatisticsData_EventTypes, fEvent2.getType().getName() }
114 };
79e08fd0 115
5673a177 116 assertEquals("getChildren", childrenExpected.length, childrenNode.length);
79e08fd0
BH
117 // assertTrue("getChildren", childrenPath.equals(childrenExpected));
118 for (TmfStatisticsTreeNode childNode : childrenNode) {
5673a177 119 if (!arrayOfArraysContains(childrenExpected, childNode.getPath())) {
79e08fd0
BH
120 fail();
121 }
122 }
123 }
124
5673a177
AM
125 private static boolean arrayOfArraysContains(String[][] arrayOfArrays, String[] array) {
126 for (String[] curArray : arrayOfArrays) {
127 if (arraysEqual(curArray, array)) {
128 return true;
129 }
130 }
131 return false;
132 }
133
134 private static boolean arraysEqual(String[] array1, String[] array2) {
135 if (array1.length != array2.length) {
136 return false;
137 }
138 for (int i = 0; i < array1.length; i++) {
139 if (!array1[i].equals(array2[i])) {
140 return false;
141 }
142 }
143 return true;
144 }
145
79e08fd0
BH
146 // ------------------------------------------------------------------------
147 // GetParent
148 // ------------------------------------------------------------------------
149
64636df8
BH
150 /**
151 * Test getting of parent.
152 */
79e08fd0 153 public void testGetParent() {
5673a177 154 TmfStatisticsTreeNode parent = (TmfStatisticsTreeNode) treeProvider.getParent(fStatsData.get(fTestName));
79e08fd0
BH
155
156 assertNotNull("getParent", parent);
36033ff0 157 assertTrue("getParent", parent.getPath().equals(TmfStatisticsTree.ROOT));
79e08fd0
BH
158 }
159
160 // ------------------------------------------------------------------------
161 // HasChildren
162 // ------------------------------------------------------------------------
64636df8
BH
163 /**
164 * Test checking for children.
165 */
79e08fd0 166 public void testHasChildren() {
36033ff0 167 Boolean hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(TmfStatisticsTree.ROOT));
79e08fd0 168 assertTrue("hasChildren", hasChildren);
64636df8 169
5673a177 170 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(fTestName));
79e08fd0 171 assertTrue("hasChildren", hasChildren);
64636df8 172
5673a177 173 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(fTestName, Messages.TmfStatisticsData_EventTypes));
79e08fd0
BH
174 assertTrue("hasChildren", hasChildren);
175
5673a177 176 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName()));
79e08fd0
BH
177 assertFalse("hasChildren", hasChildren);
178 }
179
180 // ------------------------------------------------------------------------
181 // GetElements
182 // ------------------------------------------------------------------------
183
64636df8
BH
184 /**
185 * Test getting of elements.
186 */
79e08fd0 187 public void testGetElements() {
36033ff0 188 Object[] objectElements = treeProvider.getElements(fStatsData.get(TmfStatisticsTree.ROOT));
79e08fd0
BH
189 TmfStatisticsTreeNode[] nodeElements = Arrays.asList(objectElements).toArray(new TmfStatisticsTreeNode[0]);
190 assertEquals("getElements", 1, nodeElements.length);
5673a177 191 assertTrue("getElements", nodeElements[0].getPath()[0].equals(fTestName));
79e08fd0
BH
192 }
193}
This page took 0.041086 seconds and 5 git commands to generate.