tmf: Disable NLS warnings in tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.tests / src / org / eclipse / linuxtools / tmf / ui / tests / statistics / TmfTreeContentProviderTest.java
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.assertFalse;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22
23 import java.util.Arrays;
24
25 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
26 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
27 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
28 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
29 import org.eclipse.linuxtools.tmf.core.event.TmfEventType;
30 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
31 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.Messages;
32 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTree;
33 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode;
34 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfTreeContentProvider;
35 import org.junit.Test;
36
37 /**
38 * TmfTreeContentProvider Test Cases.
39 */
40 public class TmfTreeContentProviderTest {
41
42 // ------------------------------------------------------------------------
43 // Fields
44 // ------------------------------------------------------------------------
45
46 private static final String fTestName = "TreeContentProviderTest";
47
48 private final String fContext = "UnitTest";
49 private final String fTypeId1 = "Some type1";
50 private final String fTypeId2 = "Some type2";
51
52 private final String fLabel0 = "label1";
53 private final String fLabel1 = "label2";
54 private final String[] fLabels = new String[] { fLabel0, fLabel1 };
55
56 private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5);
57 private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5);
58
59 private final String fSource = "Source";
60
61 private final TmfEventType fType1 = new TmfEventType(fContext, fTypeId1, TmfEventField.makeRoot(fLabels));
62 private final TmfEventType fType2 = new TmfEventType(fContext, fTypeId2, TmfEventField.makeRoot(fLabels));
63
64 private final String fReference = "Some reference";
65
66 private final ITmfEvent fEvent1;
67 private final ITmfEvent fEvent2;
68
69 private final TmfEventField fContent1;
70 private final TmfEventField fContent2;
71
72 private final TmfStatisticsTree fStatsData;
73
74 private final TmfTreeContentProvider treeProvider;
75
76 // ------------------------------------------------------------------------
77 // Housekeeping
78 // ------------------------------------------------------------------------
79
80 /**
81 * Constructor
82 */
83 public TmfTreeContentProviderTest() {
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 TmfStatisticsTree();
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 // Test methods
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 @Test
109 public void testGetChildren() {
110 Object[] objectArray = treeProvider.getChildren(fStatsData.getOrCreateNode(fTestName, Messages.TmfStatisticsData_EventTypes));
111 TmfStatisticsTreeNode[] childrenNode = Arrays.asList(objectArray).toArray(new TmfStatisticsTreeNode[0]);
112
113 String[][] childrenExpected = new String[][] {
114 new String[] { fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName() },
115 new String[] { fTestName, Messages.TmfStatisticsData_EventTypes, fEvent2.getType().getName() }
116 };
117
118 assertEquals("getChildren", childrenExpected.length, childrenNode.length);
119 // assertTrue("getChildren", childrenPath.equals(childrenExpected));
120 for (TmfStatisticsTreeNode childNode : childrenNode) {
121 if (!arrayOfArraysContains(childrenExpected, childNode.getPath())) {
122 fail();
123 }
124 }
125 }
126
127 private static boolean arrayOfArraysContains(String[][] arrayOfArrays, String[] array) {
128 for (String[] curArray : arrayOfArrays) {
129 if (arraysEqual(curArray, array)) {
130 return true;
131 }
132 }
133 return false;
134 }
135
136 private static boolean arraysEqual(String[] array1, String[] array2) {
137 if (array1.length != array2.length) {
138 return false;
139 }
140 for (int i = 0; i < array1.length; i++) {
141 if (!array1[i].equals(array2[i])) {
142 return false;
143 }
144 }
145 return true;
146 }
147
148 /**
149 * Test getting of parent.
150 */
151 @Test
152 public void testGetParent() {
153 TmfStatisticsTreeNode parent = (TmfStatisticsTreeNode) treeProvider.getParent(fStatsData.getNode(fTestName));
154
155 assertNotNull("getParent", parent);
156 assertTrue("getParent", parent.getPath().equals(fStatsData.getRootNode().getPath()));
157 }
158
159 /**
160 * Test checking for children.
161 */
162 @Test
163 public void testHasChildren() {
164 Boolean hasChildren = treeProvider.hasChildren(fStatsData.getRootNode());
165 assertTrue("hasChildren", hasChildren);
166
167 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreateNode(fTestName));
168 assertTrue("hasChildren", hasChildren);
169
170 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreateNode(fTestName, Messages.TmfStatisticsData_EventTypes));
171 assertTrue("hasChildren", hasChildren);
172
173 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreateNode(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName()));
174 assertFalse("hasChildren", hasChildren);
175 }
176
177 /**
178 * Test getting of elements.
179 */
180 @Test
181 public void testGetElements() {
182 Object[] objectElements = treeProvider.getElements(fStatsData.getRootNode());
183 TmfStatisticsTreeNode[] nodeElements = Arrays.asList(objectElements).toArray(new TmfStatisticsTreeNode[0]);
184 assertEquals("getElements", 1, nodeElements.length);
185 assertTrue("getElements", nodeElements[0].getPath()[0].equals(fTestName));
186 }
187 }
This page took 0.037432 seconds and 5 git commands to generate.