Merge branch 'master' into lttng-kepler
[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.Messages;
26 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTree;
27 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode;
28 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfTreeContentProvider;
29
30 /**
31 * TmfTreeContentProvider Test Cases.
32 */
33 @SuppressWarnings("nls")
34 public class TmfTreeContentProviderTest extends TestCase {
35
36 // ------------------------------------------------------------------------
37 // Fields
38 // ------------------------------------------------------------------------
39
40 private String fTestName = null;
41
42 private final String fContext = "UnitTest";
43 private final String fTypeId1 = "Some type1";
44 private final String fTypeId2 = "Some type2";
45
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
53 private final String fSource = "Source";
54
55 private final TmfEventType fType1 = new TmfEventType(fContext, fTypeId1, TmfEventField.makeRoot(fLabels));
56 private final TmfEventType fType2 = new TmfEventType(fContext, fTypeId2, TmfEventField.makeRoot(fLabels));
57
58 private final String fReference = "Some reference";
59
60 private final TmfEvent fEvent1;
61 private final TmfEvent fEvent2;
62
63 private final TmfEventField fContent1;
64 private final TmfEventField fContent2;
65
66 private final TmfStatisticsTree fStatsData;
67
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
83 fContent1 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some content");
84 fEvent1 = new TmfEvent(null, fTimestamp1, fSource, fType1, fContent1, fReference);
85
86 fContent2 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other content");
87 fEvent2 = new TmfEvent(null, fTimestamp2, fSource, fType2, fContent2, fReference);
88
89 fStatsData = new TmfStatisticsTree();
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);
94
95 treeProvider = new TmfTreeContentProvider();
96 }
97
98 // ------------------------------------------------------------------------
99 // GetChildren
100 // ------------------------------------------------------------------------
101
102 /**
103 * Test getting of children.
104 * FIXME this test was quickly adapted when we removed the TmfFixedArray,
105 * but it could be rewritten to be much more simple...
106 */
107 public void testGetChildren() {
108 Object[] objectArray = treeProvider.getChildren(fStatsData.getOrCreateNode(fTestName, Messages.TmfStatisticsData_EventTypes));
109 TmfStatisticsTreeNode[] childrenNode = Arrays.asList(objectArray).toArray(new TmfStatisticsTreeNode[0]);
110
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 };
115
116 assertEquals("getChildren", childrenExpected.length, childrenNode.length);
117 // assertTrue("getChildren", childrenPath.equals(childrenExpected));
118 for (TmfStatisticsTreeNode childNode : childrenNode) {
119 if (!arrayOfArraysContains(childrenExpected, childNode.getPath())) {
120 fail();
121 }
122 }
123 }
124
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
146 // ------------------------------------------------------------------------
147 // GetParent
148 // ------------------------------------------------------------------------
149
150 /**
151 * Test getting of parent.
152 */
153 public void testGetParent() {
154 TmfStatisticsTreeNode parent = (TmfStatisticsTreeNode) treeProvider.getParent(fStatsData.getNode(fTestName));
155
156 assertNotNull("getParent", parent);
157 assertTrue("getParent", parent.getPath().equals(fStatsData.getRootNode().getPath()));
158 }
159
160 // ------------------------------------------------------------------------
161 // HasChildren
162 // ------------------------------------------------------------------------
163 /**
164 * Test checking for children.
165 */
166 public void testHasChildren() {
167 Boolean hasChildren = treeProvider.hasChildren(fStatsData.getRootNode());
168 assertTrue("hasChildren", hasChildren);
169
170 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreateNode(fTestName));
171 assertTrue("hasChildren", hasChildren);
172
173 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreateNode(fTestName, Messages.TmfStatisticsData_EventTypes));
174 assertTrue("hasChildren", hasChildren);
175
176 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreateNode(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName()));
177 assertFalse("hasChildren", hasChildren);
178 }
179
180 // ------------------------------------------------------------------------
181 // GetElements
182 // ------------------------------------------------------------------------
183
184 /**
185 * Test getting of elements.
186 */
187 public void testGetElements() {
188 Object[] objectElements = treeProvider.getElements(fStatsData.getRootNode());
189 TmfStatisticsTreeNode[] nodeElements = Arrays.asList(objectElements).toArray(new TmfStatisticsTreeNode[0]);
190 assertEquals("getElements", 1, nodeElements.length);
191 assertTrue("getElements", nodeElements[0].getPath()[0].equals(fTestName));
192 }
193 }
This page took 0.053078 seconds and 6 git commands to generate.