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
.AbsTmfStatisticsTree
;
26 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.Messages
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.TmfBaseStatisticsTree
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.TmfStatisticsTreeNode
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
.TmfTreeContentProvider
;
32 * TmfTreeContentProvider Test Cases.
34 @SuppressWarnings("nls")
35 public class TmfTreeContentProviderTest
extends TestCase
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
41 private String fTestName
= null;
43 private final String fContext
= "UnitTest";
44 private final String fTypeId1
= "Some type1";
45 private final String fTypeId2
= "Some type2";
47 private final String fLabel0
= "label1";
48 private final String fLabel1
= "label2";
49 private final String
[] fLabels
= new String
[] { fLabel0
, fLabel1
};
51 private final TmfTimestamp fTimestamp1
= new TmfTimestamp(12345, (byte) 2, 5);
52 private final TmfTimestamp fTimestamp2
= new TmfTimestamp(12350, (byte) 2, 5);
54 private final String fSource
= "Source";
56 private final TmfEventType fType1
= new TmfEventType(fContext
, fTypeId1
, TmfEventField
.makeRoot(fLabels
));
57 private final TmfEventType fType2
= new TmfEventType(fContext
, fTypeId2
, TmfEventField
.makeRoot(fLabels
));
59 private final String fReference
= "Some reference";
61 private final TmfEvent fEvent1
;
62 private final TmfEvent fEvent2
;
64 private final TmfEventField fContent1
;
65 private final TmfEventField fContent2
;
67 private final TmfBaseStatisticsTree fStatsData
;
69 private final TmfTreeContentProvider treeProvider
;
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
79 public TmfTreeContentProviderTest(final String name
) {
84 fContent1
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some content");
85 fEvent1
= new TmfEvent(null, fTimestamp1
, fSource
, fType1
, fContent1
, fReference
);
87 fContent2
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some other content");
88 fEvent2
= new TmfEvent(null, fTimestamp2
, fSource
, fType2
, fContent2
, fReference
);
90 fStatsData
= new TmfBaseStatisticsTree();
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...
108 public void testGetChildren() {
109 Object
[] objectArray
= treeProvider
.getChildren(fStatsData
.getOrCreate(fTestName
, Messages
.TmfStatisticsData_EventTypes
));
110 TmfStatisticsTreeNode
[] childrenNode
= Arrays
.asList(objectArray
).toArray(new TmfStatisticsTreeNode
[0]);
112 String
[][] childrenExpected
= new String
[][] {
113 new String
[] { fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().getName() },
114 new String
[] { fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent2
.getType().getName() }
117 assertEquals("getChildren", childrenExpected
.length
, childrenNode
.length
);
118 // assertTrue("getChildren", childrenPath.equals(childrenExpected));
119 for (TmfStatisticsTreeNode childNode
: childrenNode
) {
120 if (!arrayOfArraysContains(childrenExpected
, childNode
.getPath())) {
126 private static boolean arrayOfArraysContains(String
[][] arrayOfArrays
, String
[] array
) {
127 for (String
[] curArray
: arrayOfArrays
) {
128 if (arraysEqual(curArray
, array
)) {
135 private static boolean arraysEqual(String
[] array1
, String
[] array2
) {
136 if (array1
.length
!= array2
.length
) {
139 for (int i
= 0; i
< array1
.length
; i
++) {
140 if (!array1
[i
].equals(array2
[i
])) {
147 // ------------------------------------------------------------------------
149 // ------------------------------------------------------------------------
152 * Test getting of parent.
154 public void testGetParent() {
155 TmfStatisticsTreeNode parent
= (TmfStatisticsTreeNode
) treeProvider
.getParent(fStatsData
.get(fTestName
));
157 assertNotNull("getParent", parent
);
158 assertTrue("getParent", parent
.getPath().equals(AbsTmfStatisticsTree
.ROOT
));
161 // ------------------------------------------------------------------------
163 // ------------------------------------------------------------------------
165 * Test checking for children.
167 public void testHasChildren() {
168 Boolean hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreate(AbsTmfStatisticsTree
.ROOT
));
169 assertTrue("hasChildren", hasChildren
);
171 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreate(fTestName
));
172 assertTrue("hasChildren", hasChildren
);
174 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreate(fTestName
, Messages
.TmfStatisticsData_EventTypes
));
175 assertTrue("hasChildren", hasChildren
);
177 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreate(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().getName()));
178 assertFalse("hasChildren", hasChildren
);
181 // ------------------------------------------------------------------------
183 // ------------------------------------------------------------------------
186 * Test getting of elements.
188 public void testGetElements() {
189 Object
[] objectElements
= treeProvider
.getElements(fStatsData
.get(AbsTmfStatisticsTree
.ROOT
));
190 TmfStatisticsTreeNode
[] nodeElements
= Arrays
.asList(objectElements
).toArray(new TmfStatisticsTreeNode
[0]);
191 assertEquals("getElements", 1, nodeElements
.length
);
192 assertTrue("getElements", nodeElements
[0].getPath()[0].equals(fTestName
));