Merge corrected branch 'master'
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / event / TmfEventTypeManagerTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.tests.event;
14
15 import java.util.Arrays;
16
17 import junit.framework.TestCase;
18
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;
23
24 /**
25 * Test suite for the TmfEventTypeManager class.
26 */
27 @SuppressWarnings("nls")
28 public class TmfEventTypeManagerTest extends TestCase {
29
30 // ------------------------------------------------------------------------
31 // Variables
32 // ------------------------------------------------------------------------
33
34 private static final TmfEventTypeManager fInstance = TmfEventTypeManager.getInstance();
35
36 private final String fContext1 = "JUnit context 1";
37 private final String fContext2 = "JUnit context 2";
38
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";
43
44 private final String fLabel0 = "label1";
45 private final String fLabel1 = "label2";
46
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 };
50
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));
55
56 // ------------------------------------------------------------------------
57 // Housekeeping
58 // ------------------------------------------------------------------------
59
60 /**
61 * @param name the test name
62 */
63 public TmfEventTypeManagerTest(final String name) {
64 super(name);
65 }
66
67 @Override
68 protected void setUp() throws Exception {
69 super.setUp();
70 }
71
72 @Override
73 protected void tearDown() throws Exception {
74 super.tearDown();
75 }
76
77 // ------------------------------------------------------------------------
78 // Getters
79 // ------------------------------------------------------------------------
80
81 public void testGetContexts() {
82 fInstance.clear();
83 fInstance.add(fContext1, fType0);
84 fInstance.add(fContext1, fType1);
85 fInstance.add(fContext2, fType2);
86 fInstance.add(fContext2, fType3);
87
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]);
93 }
94
95 public void testGetTypes() {
96 fInstance.clear();
97 fInstance.add(fContext1, fType0);
98 fInstance.add(fContext1, fType1);
99 fInstance.add(fContext2, fType2);
100 fInstance.add(fContext2, fType3);
101
102 ITmfEventType[] types = fInstance.getTypes(fContext1);
103 assertEquals("getTypes", 2, types.length);
104 if (fType0 == types[0])
105 assertSame("getTypes", fType1, types[1]);
106 else {
107 assertSame("getTypes", fType0, types[1]);
108 assertSame("getTypes", fType1, types[0]);
109 }
110
111 types = fInstance.getTypes(fContext2);
112 assertEquals("getTypes", 2, types.length);
113 if (fType2 == types[0])
114 assertSame("getTypes", fType3, types[1]);
115 else {
116 assertSame("getTypes", fType2, types[1]);
117 assertSame("getTypes", fType3, types[0]);
118 }
119 }
120
121 public void testGetType() {
122 fInstance.clear();
123 fInstance.add(fContext1, fType0);
124 fInstance.add(fContext1, fType1);
125 fInstance.add(fContext2, fType2);
126 fInstance.add(fContext2, fType3);
127
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);
136
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);
145 }
146
147 // ------------------------------------------------------------------------
148 // Operations
149 // ------------------------------------------------------------------------
150
151 public void testClear() {
152 fInstance.clear();
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());
157 }
158
159 public void testClearContext() {
160 fInstance.clear();
161 fInstance.add(fContext1, fType0);
162 fInstance.add(fContext1, fType1);
163 fInstance.add(fContext2, fType2);
164 fInstance.add(fContext2, fType3);
165
166 fInstance.clear(fContext1);
167
168 final String[] contexts = fInstance.getContexts();
169 assertEquals("clear context", 1, contexts.length);
170 assertEquals("clear context", fContext2, contexts[0]);
171
172 ITmfEventType[] types = fInstance.getTypes(fContext1);
173 assertEquals("clear context", 0, types.length);
174
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);
179
180 types = fInstance.getTypes(fContext2);
181 assertEquals("clear context", 2, types.length);
182 if (fType2 == types[0])
183 assertSame("clear context", fType3, types[1]);
184 else {
185 assertSame("clear context", fType2, types[1]);
186 assertSame("clear context", fType3, types[0]);
187 }
188 }
189
190 public void testBasicAdd() {
191 fInstance.clear();
192 fInstance.add(fContext1, fType0);
193
194 final String[] contexts = fInstance.getContexts();
195 assertEquals("add", 1, contexts.length);
196 assertEquals("add", fContext1, contexts[0]);
197
198 final ITmfEventType[] types = fInstance.getTypes(contexts[0]);
199 assertEquals("add", 1, types.length);
200 assertSame("add", fType0, types[0]);
201
202 ITmfEventType type = fInstance.getType(contexts[0], fType0.getName());
203 assertSame("add", fType0, type);
204
205 type = fInstance.getType(contexts[0], fType1.getName());
206 assertNotSame("add", fType0, type);
207 }
208
209 public void testAdd() {
210 fInstance.clear();
211 fInstance.add(fContext1, fType0);
212 fInstance.add(fContext1, fType1);
213 fInstance.add(fContext2, fType2);
214 fInstance.add(fContext2, fType3);
215
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]);
221
222 ITmfEventType[] types = fInstance.getTypes(fContext1);
223 assertEquals("add", 2, types.length);
224 if (fType0 == types[0])
225 assertSame("add", fType1, types[1]);
226 else {
227 assertSame("add", fType0, types[1]);
228 assertSame("add", fType1, types[0]);
229 }
230
231 types = fInstance.getTypes(fContext2);
232 assertEquals("add", 2, types.length);
233 if (fType2 == types[0])
234 assertSame("add", fType3, types[1]);
235 else {
236 assertSame("add", fType2, types[1]);
237 assertSame("add", fType3, types[0]);
238 }
239
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);
248
249 type = fInstance.getType(fContext1, fType2.getName());
250 assertNull("add", type);
251 type = fInstance.getType(fContext2, fType0.getName());
252 assertNull("add", type);
253 }
254
255 // ------------------------------------------------------------------------
256 // Object
257 // ------------------------------------------------------------------------
258
259 public void testToString() {
260 fInstance.clear();
261 assertEquals("toString", "TmfEventTypeManager [fEventTypes={}]", fInstance.toString());
262
263 fInstance.add(fContext1, fType0);
264 assertEquals("toString", "TmfEventTypeManager [fEventTypes={" + fContext1 + "={" + fTypeId1 + "=" + fType0 + "}}]", fInstance.toString());
265 }
266
267 }
This page took 0.044201 seconds and 6 git commands to generate.