1 /*******************************************************************************
2 * Copyright (c) 2012 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 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.event
;
15 import java
.util
.Arrays
;
17 import junit
.framework
.TestCase
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventType
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventTypeManager
;
25 * <b><u>TmfEventTypeManagerTest</u></b>
27 * Test suite for the TmfEventTypeManager class.
30 @SuppressWarnings("nls")
31 public class TmfEventTypeManagerTest
extends TestCase
{
33 // ------------------------------------------------------------------------
35 // ------------------------------------------------------------------------
37 private static final TmfEventTypeManager fInstance
= TmfEventTypeManager
.getInstance();
39 private final String fContext1
= "JUnit context 1";
40 private final String fContext2
= "JUnit context 2";
42 private final String fTypeId1
= "Some type";
43 private final String fTypeId2
= "Some other type";
44 private final String fTypeId3
= "Yet another type";
45 private final String fTypeId4
= "A final type";
47 private final String fLabel0
= "label1";
48 private final String fLabel1
= "label2";
50 private final String
[] fLabels0
= new String
[] { };
51 private final String
[] fLabels1
= new String
[] { fLabel0
, fLabel1
};
52 private final String
[] fLabels2
= new String
[] { fLabel1
, fLabel0
, fLabel1
};
54 private final TmfEventType fType0
= new TmfEventType(fContext1
, fTypeId1
, TmfEventField
.makeRoot(fLabels0
));
55 private final TmfEventType fType1
= new TmfEventType(fContext1
, fTypeId2
, TmfEventField
.makeRoot(fLabels1
));
56 private final TmfEventType fType2
= new TmfEventType(fContext2
, fTypeId3
, TmfEventField
.makeRoot(fLabels2
));
57 private final TmfEventType fType3
= new TmfEventType(fContext2
, fTypeId4
, TmfEventField
.makeRoot(fLabels1
));
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
64 * @param name the test name
66 public TmfEventTypeManagerTest(String name
) {
71 protected void setUp() throws Exception
{
76 protected void tearDown() throws Exception
{
80 // ------------------------------------------------------------------------
82 // ------------------------------------------------------------------------
84 public void testGetContexts() {
86 fInstance
.add(fContext1
, fType0
);
87 fInstance
.add(fContext1
, fType1
);
88 fInstance
.add(fContext2
, fType2
);
89 fInstance
.add(fContext2
, fType3
);
91 String
[] contexts
= fInstance
.getContexts();
92 Arrays
.sort(contexts
);
93 assertEquals("getContexts", 2, contexts
.length
);
94 assertEquals("getContexts", fContext1
, contexts
[0]);
95 assertEquals("getContexts", fContext2
, contexts
[1]);
98 public void testGetTypes() {
100 fInstance
.add(fContext1
, fType0
);
101 fInstance
.add(fContext1
, fType1
);
102 fInstance
.add(fContext2
, fType2
);
103 fInstance
.add(fContext2
, fType3
);
105 ITmfEventType
[] types
= fInstance
.getTypes(fContext1
);
106 assertEquals("getTypes", 2, types
.length
);
107 if (fType0
== types
[0]) {
108 assertSame("getTypes", fType1
, types
[1]);
110 assertSame("getTypes", fType0
, types
[1]);
111 assertSame("getTypes", fType1
, types
[0]);
114 types
= fInstance
.getTypes(fContext2
);
115 assertEquals("getTypes", 2, types
.length
);
116 if (fType2
== types
[0]) {
117 assertSame("getTypes", fType3
, types
[1]);
119 assertSame("getTypes", fType2
, types
[1]);
120 assertSame("getTypes", fType3
, types
[0]);
124 public void testGetType() {
126 fInstance
.add(fContext1
, fType0
);
127 fInstance
.add(fContext1
, fType1
);
128 fInstance
.add(fContext2
, fType2
);
129 fInstance
.add(fContext2
, fType3
);
131 ITmfEventType type
= fInstance
.getType(fContext1
, fType0
.getName());
132 assertSame("getType", fType0
, type
);
133 type
= fInstance
.getType(fContext1
, fType1
.getName());
134 assertSame("getType", fType1
, type
);
135 type
= fInstance
.getType(fContext1
, fType2
.getName());
136 assertNull("getType", type
);
137 type
= fInstance
.getType(fContext1
, fType3
.getName());
138 assertNull("getType", type
);
140 type
= fInstance
.getType(fContext2
, fType2
.getName());
141 assertSame("getType", fType2
, type
);
142 type
= fInstance
.getType(fContext2
, fType3
.getName());
143 assertSame("getType", fType3
, type
);
144 type
= fInstance
.getType(fContext2
, fType0
.getName());
145 assertNull("getType", type
);
146 type
= fInstance
.getType(fContext2
, fType1
.getName());
147 assertNull("getType", type
);
150 // ------------------------------------------------------------------------
152 // ------------------------------------------------------------------------
154 public void testClear() {
156 assertEquals("clear", 0, fInstance
.getContexts().length
);
157 assertNull("clear", fInstance
.getTypes(null));
158 assertNull("clear", fInstance
.getType(null, null));
159 assertEquals("clear", "TmfEventTypeManager [fEventTypes={}]", fInstance
.toString());
162 public void testClearContext() {
164 fInstance
.add(fContext1
, fType0
);
165 fInstance
.add(fContext1
, fType1
);
166 fInstance
.add(fContext2
, fType2
);
167 fInstance
.add(fContext2
, fType3
);
169 fInstance
.clear(fContext1
);
171 String
[] contexts
= fInstance
.getContexts();
172 assertEquals("clear context", 1, contexts
.length
);
173 assertEquals("clear context", fContext2
, contexts
[0]);
175 ITmfEventType
[] types
= fInstance
.getTypes(fContext1
);
176 assertNull("clear context", types
);
178 ITmfEventType type
= fInstance
.getType(fContext1
, fType0
.getName());
179 assertNull("clear context", type
);
180 type
= fInstance
.getType(fContext1
, fType1
.getName());
181 assertNull("clear context", type
);
183 types
= fInstance
.getTypes(fContext2
);
184 assertEquals("clear context", 2, types
.length
);
185 if (fType2
== types
[0]) {
186 assertSame("clear context", fType3
, types
[1]);
188 assertSame("clear context", fType2
, types
[1]);
189 assertSame("clear context", fType3
, types
[0]);
193 public void testBasicAdd() {
195 fInstance
.add(fContext1
, fType0
);
197 String
[] contexts
= fInstance
.getContexts();
198 assertEquals("add", 1, contexts
.length
);
199 assertEquals("add", fContext1
, contexts
[0]);
201 ITmfEventType
[] types
= fInstance
.getTypes(contexts
[0]);
202 assertEquals("add", 1, types
.length
);
203 assertSame("add", fType0
, types
[0]);
205 ITmfEventType type
= fInstance
.getType(contexts
[0], fType0
.getName());
206 assertSame("add", fType0
, type
);
208 type
= fInstance
.getType(contexts
[0], fType1
.getName());
209 assertNotSame("add", fType0
, type
);
212 public void testAdd() {
214 fInstance
.add(fContext1
, fType0
);
215 fInstance
.add(fContext1
, fType1
);
216 fInstance
.add(fContext2
, fType2
);
217 fInstance
.add(fContext2
, fType3
);
219 String
[] contexts
= fInstance
.getContexts();
220 Arrays
.sort(contexts
);
221 assertEquals("add", 2, contexts
.length
);
222 assertEquals("add", fContext1
, contexts
[0]);
223 assertEquals("add", fContext2
, contexts
[1]);
225 ITmfEventType
[] types
= fInstance
.getTypes(fContext1
);
226 assertEquals("add", 2, types
.length
);
227 if (fType0
== types
[0]) {
228 assertSame("add", fType1
, types
[1]);
230 assertSame("add", fType0
, types
[1]);
231 assertSame("add", fType1
, types
[0]);
234 types
= fInstance
.getTypes(fContext2
);
235 assertEquals("add", 2, types
.length
);
236 if (fType2
== types
[0]) {
237 assertSame("add", fType3
, types
[1]);
239 assertSame("add", fType2
, types
[1]);
240 assertSame("add", fType3
, types
[0]);
243 ITmfEventType type
= fInstance
.getType(fContext1
, fType0
.getName());
244 assertSame("add", fType0
, type
);
245 type
= fInstance
.getType(fContext1
, fType1
.getName());
246 assertSame("add", fType1
, type
);
247 type
= fInstance
.getType(fContext2
, fType2
.getName());
248 assertSame("add", fType2
, type
);
249 type
= fInstance
.getType(fContext2
, fType3
.getName());
250 assertSame("add", fType3
, type
);
252 type
= fInstance
.getType(fContext1
, fType2
.getName());
253 assertNull("add", type
);
254 type
= fInstance
.getType(fContext2
, fType0
.getName());
255 assertNull("add", type
);
258 // ------------------------------------------------------------------------
260 // ------------------------------------------------------------------------
262 public void testToString() {
264 assertEquals("toString", "TmfEventTypeManager [fEventTypes={}]", fInstance
.toString());
266 fInstance
.add(fContext1
, fType0
);
267 assertEquals("toString", "TmfEventTypeManager [fEventTypes={" + fContext1
+ "={" + fTypeId1
+ "=" + fType0
+ "}}]", fInstance
.toString());