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
;
17 import java
.util
.Collection
;
18 import java
.util
.Vector
;
20 import junit
.framework
.TestCase
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventType
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.util
.TmfFixedArray
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
.ITmfExtraEventInfo
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
.model
.AbsTmfStatisticsTree
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
.model
.Messages
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
.model
.TmfBaseStatisticsTree
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
.model
.TmfStatisticsTreeNode
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
.model
.TmfTreeContentProvider
;
35 @SuppressWarnings("nls")
36 public class TmfTreeContentProviderTest
extends TestCase
{
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
42 private String fTestName
= null;
44 private final String fContext
= "UnitTest";
45 private final String fTypeId1
= "Some type1";
46 private final String fTypeId2
= "Some type2";
48 private final String fLabel0
= "label1";
49 private final String fLabel1
= "label2";
50 private final String
[] fLabels
= new String
[] { fLabel0
, fLabel1
};
52 private final TmfTimestamp fTimestamp1
= new TmfTimestamp(12345, (byte) 2, 5);
53 private final TmfTimestamp fTimestamp2
= new TmfTimestamp(12350, (byte) 2, 5);
55 private final String fSource
= "Source";
57 private final TmfEventType fType1
= new TmfEventType(fContext
, fTypeId1
, TmfEventField
.makeRoot(fLabels
));
58 private final TmfEventType fType2
= new TmfEventType(fContext
, fTypeId2
, TmfEventField
.makeRoot(fLabels
));
60 private final String fReference
= "Some reference";
62 private final TmfEvent fEvent1
;
63 private final TmfEvent fEvent2
;
65 private final TmfEventField fContent1
;
66 private final TmfEventField fContent2
;
68 private final TmfBaseStatisticsTree fStatsData
;
70 private final ITmfExtraEventInfo fExtraInfo
;
72 private final TmfTreeContentProvider treeProvider
;
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
82 public TmfTreeContentProviderTest(final String name
) {
87 fContent1
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some content");
88 fEvent1
= new TmfEvent(null, fTimestamp1
, fSource
, fType1
, fContent1
, fReference
);
90 fContent2
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, "Some other content");
91 fEvent2
= new TmfEvent(null, fTimestamp2
, fSource
, fType2
, fContent2
, fReference
);
93 fStatsData
= new TmfBaseStatisticsTree();
94 fExtraInfo
= new ITmfExtraEventInfo() {
96 public String
getTraceName() {
100 fStatsData
.registerEvent(fEvent1
, fExtraInfo
);
101 fStatsData
.registerEvent(fEvent2
, fExtraInfo
);
103 treeProvider
= new TmfTreeContentProvider();
106 // ------------------------------------------------------------------------
108 // ------------------------------------------------------------------------
110 public void testGetChildren() {
111 Object
[] objectArray
= treeProvider
.getChildren(fStatsData
.getOrCreate(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
)));
112 TmfStatisticsTreeNode
[] childrenNode
= Arrays
.asList(objectArray
).toArray(new TmfStatisticsTreeNode
[0]);
114 Collection
<TmfFixedArray
<String
>> childrenExpected
= new Vector
<TmfFixedArray
<String
>>();
115 childrenExpected
.add(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString()));
116 childrenExpected
.add(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent2
.getType().toString()));
118 assertEquals("getChildren", childrenExpected
.size(), childrenNode
.length
);
119 // assertTrue("getChildren", childrenPath.equals(childrenExpected));
120 for (TmfStatisticsTreeNode childNode
: childrenNode
) {
121 if (childrenExpected
.contains(childNode
.getPath())) {
122 childrenExpected
.remove(childNode
.getPath());
129 // ------------------------------------------------------------------------
131 // ------------------------------------------------------------------------
133 public void testGetParent() {
134 TmfStatisticsTreeNode parent
= (TmfStatisticsTreeNode
) treeProvider
.getParent(fStatsData
.get(new TmfFixedArray
<String
>(fTestName
)));
136 assertNotNull("getParent", parent
);
137 assertTrue("getParent", parent
.getPath().equals(AbsTmfStatisticsTree
.ROOT
));
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
144 public void testHasChildren() {
145 Boolean hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreate(AbsTmfStatisticsTree
.ROOT
));
146 assertTrue("hasChildren", hasChildren
);
148 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreate(new TmfFixedArray
<String
>(fTestName
)));
149 assertTrue("hasChildren", hasChildren
);
151 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreate(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
)));
152 assertTrue("hasChildren", hasChildren
);
154 hasChildren
= treeProvider
.hasChildren(fStatsData
.getOrCreate(new TmfFixedArray
<String
>(fTestName
, Messages
.TmfStatisticsData_EventTypes
, fEvent1
.getType().toString())));
155 assertFalse("hasChildren", hasChildren
);
158 // ------------------------------------------------------------------------
160 // ------------------------------------------------------------------------
162 public void testGetElements() {
163 Object
[] objectElements
= treeProvider
.getElements(fStatsData
.get(AbsTmfStatisticsTree
.ROOT
));
164 TmfStatisticsTreeNode
[] nodeElements
= Arrays
.asList(objectElements
).toArray(new TmfStatisticsTreeNode
[0]);
165 assertEquals("getElements", 1, nodeElements
.length
);
166 assertTrue("getElements", nodeElements
[0].getPath().equals(new TmfFixedArray
<String
>(fTestName
)));