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