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