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
* Test suite for the TmfEventTypeManager class.
27
@SuppressWarnings("nls")
28
public class TmfEventTypeManagerTest extends TestCase {
30
// ------------------------------------------------------------------------
32
// ------------------------------------------------------------------------
34
private static final TmfEventTypeManager fInstance = TmfEventTypeManager.getInstance();
36
private final String fContext1 = "JUnit context 1";
37
private final String fContext2 = "JUnit context 2";
39
private final String fTypeId1 = "Some type";
40
private final String fTypeId2 = "Some other type";
41
private final String fTypeId3 = "Yet another type";
42
private final String fTypeId4 = "A final type";
44
private final String fLabel0 = "label1";
45
private final String fLabel1 = "label2";
47
private final String[] fLabels0 = new String[] { };
48
private final String[] fLabels1 = new String[] { fLabel0, fLabel1 };
49
private final String[] fLabels2 = new String[] { fLabel1, fLabel0, fLabel1 };
51
private final TmfEventType fType0 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels0));
52
private final TmfEventType fType1 = new TmfEventType(fContext1, fTypeId2, TmfEventField.makeRoot(fLabels1));
53
private final TmfEventType fType2 = new TmfEventType(fContext2, fTypeId3, TmfEventField.makeRoot(fLabels2));
54
private final TmfEventType fType3 = new TmfEventType(fContext2, fTypeId4, TmfEventField.makeRoot(fLabels1));
56
// ------------------------------------------------------------------------
58
// ------------------------------------------------------------------------
61
* @param name the test name
63
public TmfEventTypeManagerTest(final String name) {
68
protected void setUp() throws Exception {
73
protected void tearDown() throws Exception {
77
// ------------------------------------------------------------------------
79
// ------------------------------------------------------------------------
81
public void testGetContexts() {
83
fInstance.add(fContext1, fType0);
84
fInstance.add(fContext1, fType1);
85
fInstance.add(fContext2, fType2);
86
fInstance.add(fContext2, fType3);
88
final String[] contexts = fInstance.getContexts();
89
Arrays.sort(contexts);
90
assertEquals("getContexts", 2, contexts.length);
91
assertEquals("getContexts", fContext1, contexts[0]);
92
assertEquals("getContexts", fContext2, contexts[1]);
95
public void testGetTypes() {
97
fInstance.add(fContext1, fType0);
98
fInstance.add(fContext1, fType1);
99
fInstance.add(fContext2, fType2);
100
fInstance.add(fContext2, fType3);
102
ITmfEventType[] types = fInstance.getTypes(fContext1);
103
assertEquals("getTypes", 2, types.length);
104
if (fType0 == types[0])
105
assertSame("getTypes", fType1, types[1]);
107
assertSame("getTypes", fType0, types[1]);
108
assertSame("getTypes", fType1, types[0]);
111
types = fInstance.getTypes(fContext2);
112
assertEquals("getTypes", 2, types.length);
113
if (fType2 == types[0])
114
assertSame("getTypes", fType3, types[1]);
116
assertSame("getTypes", fType2, types[1]);
117
assertSame("getTypes", fType3, types[0]);
121
public void testGetType() {
123
fInstance.add(fContext1, fType0);
124
fInstance.add(fContext1, fType1);
125
fInstance.add(fContext2, fType2);
126
fInstance.add(fContext2, fType3);
128
ITmfEventType type = fInstance.getType(fContext1, fType0.getName());
129
assertSame("getType", fType0, type);
130
type = fInstance.getType(fContext1, fType1.getName());
131
assertSame("getType", fType1, type);
132
type = fInstance.getType(fContext1, fType2.getName());
133
assertNull("getType", type);
134
type = fInstance.getType(fContext1, fType3.getName());
135
assertNull("getType", type);
137
type = fInstance.getType(fContext2, fType2.getName());
138
assertSame("getType", fType2, type);
139
type = fInstance.getType(fContext2, fType3.getName());
140
assertSame("getType", fType3, type);
141
type = fInstance.getType(fContext2, fType0.getName());
142
assertNull("getType", type);
143
type = fInstance.getType(fContext2, fType1.getName());
144
assertNull("getType", type);
147
// ------------------------------------------------------------------------
149
// ------------------------------------------------------------------------
151
public void testClear() {
153
assertEquals("clear", 0, fInstance.getContexts().length);
154
assertEquals("clear", 0, fInstance.getTypes(null).length);
155
assertNull("clear", fInstance.getType(null, null));
156
assertEquals("clear", "TmfEventTypeManager [fEventTypes={}]", fInstance.toString());
159
public void testClearContext() {
161
fInstance.add(fContext1, fType0);
162
fInstance.add(fContext1, fType1);
163
fInstance.add(fContext2, fType2);
164
fInstance.add(fContext2, fType3);
166
fInstance.clear(fContext1);
168
final String[] contexts = fInstance.getContexts();
169
assertEquals("clear context", 1, contexts.length);
170
assertEquals("clear context", fContext2, contexts[0]);
172
ITmfEventType[] types = fInstance.getTypes(fContext1);
173
assertEquals("clear context", 0, types.length);
175
ITmfEventType type = fInstance.getType(fContext1, fType0.getName());
176
assertNull("clear context", type);
177
type = fInstance.getType(fContext1, fType1.getName());
178
assertNull("clear context", type);
180
types = fInstance.getTypes(fContext2);
181
assertEquals("clear context", 2, types.length);
182
if (fType2 == types[0])
183
assertSame("clear context", fType3, types[1]);
185
assertSame("clear context", fType2, types[1]);
186
assertSame("clear context", fType3, types[0]);
190
public void testBasicAdd() {
192
fInstance.add(fContext1, fType0);
194
final String[] contexts = fInstance.getContexts();
195
assertEquals("add", 1, contexts.length);
196
assertEquals("add", fContext1, contexts[0]);
198
final ITmfEventType[] types = fInstance.getTypes(contexts[0]);
199
assertEquals("add", 1, types.length);
200
assertSame("add", fType0, types[0]);
202
ITmfEventType type = fInstance.getType(contexts[0], fType0.getName());
203
assertSame("add", fType0, type);
205
type = fInstance.getType(contexts[0], fType1.getName());
206
assertNotSame("add", fType0, type);
209
public void testAdd() {
211
fInstance.add(fContext1, fType0);
212
fInstance.add(fContext1, fType1);
213
fInstance.add(fContext2, fType2);
214
fInstance.add(fContext2, fType3);
216
final String[] contexts = fInstance.getContexts();
217
Arrays.sort(contexts);
218
assertEquals("add", 2, contexts.length);
219
assertEquals("add", fContext1, contexts[0]);
220
assertEquals("add", fContext2, contexts[1]);
222
ITmfEventType[] types = fInstance.getTypes(fContext1);
223
assertEquals("add", 2, types.length);
224
if (fType0 == types[0])
225
assertSame("add", fType1, types[1]);
227
assertSame("add", fType0, types[1]);
228
assertSame("add", fType1, types[0]);
231
types = fInstance.getTypes(fContext2);
232
assertEquals("add", 2, types.length);
233
if (fType2 == types[0])
234
assertSame("add", fType3, types[1]);
236
assertSame("add", fType2, types[1]);
237
assertSame("add", fType3, types[0]);
240
ITmfEventType type = fInstance.getType(fContext1, fType0.getName());
241
assertSame("add", fType0, type);
242
type = fInstance.getType(fContext1, fType1.getName());
243
assertSame("add", fType1, type);
244
type = fInstance.getType(fContext2, fType2.getName());
245
assertSame("add", fType2, type);
246
type = fInstance.getType(fContext2, fType3.getName());
247
assertSame("add", fType3, type);
249
type = fInstance.getType(fContext1, fType2.getName());
250
assertNull("add", type);
251
type = fInstance.getType(fContext2, fType0.getName());
252
assertNull("add", type);
255
// ------------------------------------------------------------------------
257
// ------------------------------------------------------------------------
259
public void testToString() {
261
assertEquals("toString", "TmfEventTypeManager [fEventTypes={}]", fInstance.toString());
263
fInstance.add(fContext1, fType0);
264
assertEquals("toString", "TmfEventTypeManager [fEventTypes={" + fContext1 + "={" + fTypeId1 + "=" + fType0 + "}}]", fInstance.toString());