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