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
.linuxtools
.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
.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
;
38 * TmfTreeContentProvider Test Cases.
40 public class TmfTreeContentProviderTest
{
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
46 private static final String fTestName
= "TreeContentProviderTest";
48 private final String fContext
= "UnitTest";
49 private final String fTypeId1
= "Some type1";
50 private final String fTypeId2
= "Some type2";
52 private final String fLabel0
= "label1";
53 private final String fLabel1
= "label2";
54 private final String
[] fLabels
= new String
[] { fLabel0
, fLabel1
};
56 private final TmfTimestamp fTimestamp1
= new TmfTimestamp(12345, (byte) 2, 5);
57 private final TmfTimestamp fTimestamp2
= new TmfTimestamp(12350, (byte) 2, 5);
59 private final String fSource
= "Source";
61 private final TmfEventType fType1
= new TmfEventType(fContext
, fTypeId1
, TmfEventField
.makeRoot(fLabels
));
62 private final TmfEventType fType2
= new TmfEventType(fContext
, fTypeId2
, TmfEventField
.makeRoot(fLabels
));
64 private final String fReference
= "Some reference";
66 private final ITmfEvent fEvent1
;
67 private final ITmfEvent fEvent2
;
69 private final TmfEventField fContent1
;
70 private final TmfEventField fContent2
;
72 private final TmfStatisticsTree fStatsData
;
74 private final TmfTreeContentProvider treeProvider
;
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
83 public TmfTreeContentProviderTest() {
84 fContent1
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some content", null);
85 fEvent1
= new TmfEvent(null, fTimestamp1
, fSource
, fType1
, fContent1
, fReference
);
87 fContent2
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some other content", null);
88 fEvent2
= new TmfEvent(null, fTimestamp2
, fSource
, fType2
, fContent2
, fReference
);
90 fStatsData
= new TmfStatisticsTree();
92 fStatsData
.setTotal(fTestName
, true, 2);
93 fStatsData
.setTypeCount(fTestName
, fEvent1
.getType().getName(), true, 1);
94 fStatsData
.setTypeCount(fTestName
, fEvent2
.getType().getName(), true, 1);
96 treeProvider
= new TmfTreeContentProvider();
99 // ------------------------------------------------------------------------
101 // ------------------------------------------------------------------------
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...
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]);
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() }
118 assertEquals("getChildren", childrenExpected
.length
, childrenNode
.length
);
119 // assertTrue("getChildren", childrenPath.equals(childrenExpected));
120 for (TmfStatisticsTreeNode childNode
: childrenNode
) {
121 if (!arrayOfArraysContains(childrenExpected
, childNode
.getPath())) {
127 private static boolean arrayOfArraysContains(String
[][] arrayOfArrays
, String
[] array
) {
128 for (String
[] curArray
: arrayOfArrays
) {
129 if (arraysEqual(curArray
, array
)) {
136 private static boolean arraysEqual(String
[] array1
, String
[] array2
) {
137 if (array1
.length
!= array2
.length
) {
140 for (int i
= 0; i
< array1
.length
; i
++) {
141 if (!array1
[i
].equals(array2
[i
])) {
149 * Test getting of parent.
152 public void testGetParent() {
153 TmfStatisticsTreeNode parent
= (TmfStatisticsTreeNode
) treeProvider
.getParent(fStatsData
.getNode(fTestName
));
155 assertNotNull("getParent", parent
);
156 assertTrue("getParent", parent
.getPath().equals(fStatsData
.getRootNode().getPath()));
160 * Test checking for children.
163 public void testHasChildren() {
164 boolean hasChildren
= treeProvider
.hasChildren(fStatsData
.getRootNode());
165 assertTrue("hasChildren", hasChildren
);
167 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreateNode(fTestName
));
168 assertTrue("hasChildren", hasChildren
);
170 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreateNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
));
171 assertTrue("hasChildren", hasChildren
);
173 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreateNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().getName()));
174 assertFalse("hasChildren", hasChildren
);
178 * Test getting of elements.
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
));