1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson
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
10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Initial design and implementation
11 * Bernd Hufmann - Fixed warnings
12 * Alexandre Montplaisir - Port to JUnit4
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.ui
.tests
.statistics
;
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
;
23 import java
.util
.Arrays
;
25 import org
.eclipse
.jdt
.annotation
.NonNull
;
26 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.viewers
.statistics
.model
.Messages
;
27 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.viewers
.statistics
.model
.TmfStatisticsTree
;
28 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.viewers
.statistics
.model
.TmfStatisticsTreeNode
;
29 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.viewers
.statistics
.model
.TmfTreeContentProvider
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventType
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
37 import org
.junit
.Test
;
40 * TmfTreeContentProvider Test Cases.
42 public class TmfTreeContentProviderTest
{
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
48 private static final String fTestName
= "TreeContentProviderTest";
50 private final @NonNull String fTypeId1
= "Some type1";
51 private final @NonNull String fTypeId2
= "Some type2";
53 private final String fLabel0
= "label1";
54 private final String fLabel1
= "label2";
55 private final String
[] fLabels
= new String
[] { fLabel0
, fLabel1
};
57 private final TmfTimestamp fTimestamp1
= new TmfTimestamp(12345, (byte) 2);
58 private final TmfTimestamp fTimestamp2
= new TmfTimestamp(12350, (byte) 2);
60 private final TmfEventType fType1
= new TmfEventType(fTypeId1
, TmfEventField
.makeRoot(fLabels
));
61 private final TmfEventType fType2
= new TmfEventType(fTypeId2
, TmfEventField
.makeRoot(fLabels
));
63 private final ITmfEvent fEvent1
;
64 private final ITmfEvent fEvent2
;
66 private final TmfEventField fContent1
;
67 private final TmfEventField fContent2
;
69 private final TmfStatisticsTree fStatsData
;
71 private final TmfTreeContentProvider treeProvider
;
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
80 public TmfTreeContentProviderTest() {
81 fContent1
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some content", null);
82 fEvent1
= new TmfEvent(null, ITmfContext
.UNKNOWN_RANK
, fTimestamp1
, fType1
, fContent1
);
84 fContent2
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some other content", null);
85 fEvent2
= new TmfEvent(null, ITmfContext
.UNKNOWN_RANK
, fTimestamp2
, fType2
, fContent2
);
87 fStatsData
= new TmfStatisticsTree();
89 fStatsData
.setTotal(fTestName
, true, 2);
90 fStatsData
.setTypeCount(fTestName
, fEvent1
.getName(), true, 1);
91 fStatsData
.setTypeCount(fTestName
, fEvent2
.getName(), true, 1);
93 treeProvider
= new TmfTreeContentProvider();
96 // ------------------------------------------------------------------------
98 // ------------------------------------------------------------------------
101 * Test getting of children.
102 * FIXME this test was quickly adapted when we removed the TmfFixedArray,
103 * but it could be rewritten to be much more simple...
106 public void testGetChildren() {
107 Object
[] objectArray
= treeProvider
.getChildren(fStatsData
.getOrCreateNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
));
108 TmfStatisticsTreeNode
[] childrenNode
= Arrays
.asList(objectArray
).toArray(new TmfStatisticsTreeNode
[0]);
110 String
[][] childrenExpected
= new String
[][] {
111 new String
[] { fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getName() },
112 new String
[] { fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent2
.getName() }
115 assertEquals("getChildren", childrenExpected
.length
, childrenNode
.length
);
116 // assertTrue("getChildren", childrenPath.equals(childrenExpected));
117 for (TmfStatisticsTreeNode childNode
: childrenNode
) {
118 if (!arrayOfArraysContains(childrenExpected
, childNode
.getPath())) {
124 private static boolean arrayOfArraysContains(String
[][] arrayOfArrays
, String
[] array
) {
125 for (String
[] curArray
: arrayOfArrays
) {
126 if (arraysEqual(curArray
, array
)) {
133 private static boolean arraysEqual(String
[] array1
, String
[] array2
) {
134 if (array1
.length
!= array2
.length
) {
137 for (int i
= 0; i
< array1
.length
; i
++) {
138 if (!array1
[i
].equals(array2
[i
])) {
146 * Test getting of parent.
149 public void testGetParent() {
150 TmfStatisticsTreeNode parent
= (TmfStatisticsTreeNode
) treeProvider
.getParent(fStatsData
.getNode(fTestName
));
152 assertNotNull("getParent", parent
);
153 assertTrue("getParent", parent
.getPath().equals(fStatsData
.getRootNode().getPath()));
157 * Test checking for children.
160 public void testHasChildren() {
161 boolean hasChildren
= treeProvider
.hasChildren(fStatsData
.getRootNode());
162 assertTrue("hasChildren", hasChildren
);
164 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreateNode(fTestName
));
165 assertTrue("hasChildren", hasChildren
);
167 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreateNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
));
168 assertTrue("hasChildren", hasChildren
);
170 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreateNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getName()));
171 assertFalse("hasChildren", hasChildren
);
175 * Test getting of elements.
178 public void testGetElements() {
179 Object
[] objectElements
= treeProvider
.getElements(fStatsData
.getRootNode());
180 TmfStatisticsTreeNode
[] nodeElements
= Arrays
.asList(objectElements
).toArray(new TmfStatisticsTreeNode
[0]);
181 assertEquals("getElements", 1, nodeElements
.length
);
182 assertTrue("getElements", nodeElements
[0].getPath()[0].equals(fTestName
));