1 /*******************************************************************************
2 * Copyright (c) 2011 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 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.tests
.statistics
;
16 import java
.util
.Arrays
;
18 import junit
.framework
.TestCase
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventType
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.Messages
;
26 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.TmfStatisticsTree
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.TmfStatisticsTreeNode
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.TmfTreeContentProvider
;
31 * TmfTreeContentProvider Test Cases.
33 @SuppressWarnings("nls")
34 public class TmfTreeContentProviderTest
extends TestCase
{
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
40 private String fTestName
= null;
42 private final String fContext
= "UnitTest";
43 private final String fTypeId1
= "Some type1";
44 private final String fTypeId2
= "Some type2";
46 private final String fLabel0
= "label1";
47 private final String fLabel1
= "label2";
48 private final String
[] fLabels
= new String
[] { fLabel0
, fLabel1
};
50 private final TmfTimestamp fTimestamp1
= new TmfTimestamp(12345, (byte) 2, 5);
51 private final TmfTimestamp fTimestamp2
= new TmfTimestamp(12350, (byte) 2, 5);
53 private final String fSource
= "Source";
55 private final TmfEventType fType1
= new TmfEventType(fContext
, fTypeId1
, TmfEventField
.makeRoot(fLabels
));
56 private final TmfEventType fType2
= new TmfEventType(fContext
, fTypeId2
, TmfEventField
.makeRoot(fLabels
));
58 private final String fReference
= "Some reference";
60 private final TmfEvent fEvent1
;
61 private final TmfEvent fEvent2
;
63 private final TmfEventField fContent1
;
64 private final TmfEventField fContent2
;
66 private final TmfStatisticsTree fStatsData
;
68 private final TmfTreeContentProvider treeProvider
;
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
78 public TmfTreeContentProviderTest(final String name
) {
83 fContent1
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some content");
84 fEvent1
= new TmfEvent(null, fTimestamp1
, fSource
, fType1
, fContent1
, fReference
);
86 fContent2
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some other content");
87 fEvent2
= new TmfEvent(null, fTimestamp2
, fSource
, fType2
, fContent2
, fReference
);
89 fStatsData
= new TmfStatisticsTree();
91 fStatsData
.setTotal(fTestName
, true, 2);
92 fStatsData
.setTypeCount(fTestName
, fEvent1
.getType().getName(), true, 1);
93 fStatsData
.setTypeCount(fTestName
, fEvent2
.getType().getName(), true, 1);
95 treeProvider
= new TmfTreeContentProvider();
98 // ------------------------------------------------------------------------
100 // ------------------------------------------------------------------------
103 * Test getting of children.
104 * FIXME this test was quickly adapted when we removed the TmfFixedArray,
105 * but it could be rewritten to be much more simple...
107 public void testGetChildren() {
108 Object
[] objectArray
= treeProvider
.getChildren(fStatsData
.getOrCreateNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
));
109 TmfStatisticsTreeNode
[] childrenNode
= Arrays
.asList(objectArray
).toArray(new TmfStatisticsTreeNode
[0]);
111 String
[][] childrenExpected
= new String
[][] {
112 new String
[] { fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().getName() },
113 new String
[] { fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent2
.getType().getName() }
116 assertEquals("getChildren", childrenExpected
.length
, childrenNode
.length
);
117 // assertTrue("getChildren", childrenPath.equals(childrenExpected));
118 for (TmfStatisticsTreeNode childNode
: childrenNode
) {
119 if (!arrayOfArraysContains(childrenExpected
, childNode
.getPath())) {
125 private static boolean arrayOfArraysContains(String
[][] arrayOfArrays
, String
[] array
) {
126 for (String
[] curArray
: arrayOfArrays
) {
127 if (arraysEqual(curArray
, array
)) {
134 private static boolean arraysEqual(String
[] array1
, String
[] array2
) {
135 if (array1
.length
!= array2
.length
) {
138 for (int i
= 0; i
< array1
.length
; i
++) {
139 if (!array1
[i
].equals(array2
[i
])) {
146 // ------------------------------------------------------------------------
148 // ------------------------------------------------------------------------
151 * Test getting of parent.
153 public void testGetParent() {
154 TmfStatisticsTreeNode parent
= (TmfStatisticsTreeNode
) treeProvider
.getParent(fStatsData
.getNode(fTestName
));
156 assertNotNull("getParent", parent
);
157 assertTrue("getParent", parent
.getPath().equals(fStatsData
.getRootNode().getPath()));
160 // ------------------------------------------------------------------------
162 // ------------------------------------------------------------------------
164 * Test checking for children.
166 public void testHasChildren() {
167 Boolean hasChildren
= treeProvider
.hasChildren(fStatsData
.getRootNode());
168 assertTrue("hasChildren", hasChildren
);
170 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreateNode(fTestName
));
171 assertTrue("hasChildren", hasChildren
);
173 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreateNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
));
174 assertTrue("hasChildren", hasChildren
);
176 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreateNode(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().getName()));
177 assertFalse("hasChildren", hasChildren
);
180 // ------------------------------------------------------------------------
182 // ------------------------------------------------------------------------
185 * Test getting of elements.
187 public void testGetElements() {
188 Object
[] objectElements
= treeProvider
.getElements(fStatsData
.getRootNode());
189 TmfStatisticsTreeNode
[] nodeElements
= Arrays
.asList(objectElements
).toArray(new TmfStatisticsTreeNode
[0]);
190 assertEquals("getElements", 1, nodeElements
.length
);
191 assertTrue("getElements", nodeElements
[0].getPath()[0].equals(fTestName
));