tmf: Modernize ITmfEventField
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / event / TmfEventTypeManagerTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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 * Alexandre Montplaisir - Port to JUnit4
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.tests.event;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNotSame;
18 import static org.junit.Assert.assertNull;
19 import static org.junit.Assert.assertSame;
20
21 import java.util.Arrays;
22
23 import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
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.TmfEventTypeManager;
27 import org.junit.Test;
28
29 /**
30 * Test suite for the TmfEventTypeManager class.
31 */
32 @SuppressWarnings("javadoc")
33 public class TmfEventTypeManagerTest {
34
35 // ------------------------------------------------------------------------
36 // Variables
37 // ------------------------------------------------------------------------
38
39 private static final TmfEventTypeManager fInstance = TmfEventTypeManager.getInstance();
40
41 private final String fContext1 = "JUnit context 1";
42 private final String fContext2 = "JUnit context 2";
43
44 private final String fTypeId1 = "Some type";
45 private final String fTypeId2 = "Some other type";
46 private final String fTypeId3 = "Yet another type";
47 private final String fTypeId4 = "A final type";
48
49 private final String fLabel0 = "label1";
50 private final String fLabel1 = "label2";
51 private final String fLabel2 = "label3";
52
53 private final String[] fLabels0 = new String[] { };
54 private final String[] fLabels1 = new String[] { fLabel0, fLabel1 };
55 private final String[] fLabels2 = new String[] { fLabel1, fLabel0, fLabel2 };
56
57 private final TmfEventType fType0 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels0));
58 private final TmfEventType fType1 = new TmfEventType(fContext1, fTypeId2, TmfEventField.makeRoot(fLabels1));
59 private final TmfEventType fType2 = new TmfEventType(fContext2, fTypeId3, TmfEventField.makeRoot(fLabels2));
60 private final TmfEventType fType3 = new TmfEventType(fContext2, fTypeId4, TmfEventField.makeRoot(fLabels1));
61
62 // ------------------------------------------------------------------------
63 // Getters
64 // ------------------------------------------------------------------------
65
66 @Test
67 public void testGetContexts() {
68 fInstance.clear();
69 fInstance.add(fContext1, fType0);
70 fInstance.add(fContext1, fType1);
71 fInstance.add(fContext2, fType2);
72 fInstance.add(fContext2, fType3);
73
74 final String[] contexts = fInstance.getContexts();
75 Arrays.sort(contexts);
76 assertEquals("getContexts", 2, contexts.length);
77 assertEquals("getContexts", fContext1, contexts[0]);
78 assertEquals("getContexts", fContext2, contexts[1]);
79 }
80
81 @Test
82 public void testGetTypes() {
83 fInstance.clear();
84 fInstance.add(fContext1, fType0);
85 fInstance.add(fContext1, fType1);
86 fInstance.add(fContext2, fType2);
87 fInstance.add(fContext2, fType3);
88
89 ITmfEventType[] types = fInstance.getTypes(fContext1);
90 assertEquals("getTypes", 2, types.length);
91 if (fType0 == types[0]) {
92 assertSame("getTypes", fType1, types[1]);
93 } else {
94 assertSame("getTypes", fType0, types[1]);
95 assertSame("getTypes", fType1, types[0]);
96 }
97
98 types = fInstance.getTypes(fContext2);
99 assertEquals("getTypes", 2, types.length);
100 if (fType2 == types[0]) {
101 assertSame("getTypes", fType3, types[1]);
102 } else {
103 assertSame("getTypes", fType2, types[1]);
104 assertSame("getTypes", fType3, types[0]);
105 }
106 }
107
108 @Test
109 public void testGetType() {
110 fInstance.clear();
111 fInstance.add(fContext1, fType0);
112 fInstance.add(fContext1, fType1);
113 fInstance.add(fContext2, fType2);
114 fInstance.add(fContext2, fType3);
115
116 ITmfEventType type = fInstance.getType(fContext1, fType0.getName());
117 assertSame("getType", fType0, type);
118 type = fInstance.getType(fContext1, fType1.getName());
119 assertSame("getType", fType1, type);
120 type = fInstance.getType(fContext1, fType2.getName());
121 assertNull("getType", type);
122 type = fInstance.getType(fContext1, fType3.getName());
123 assertNull("getType", type);
124
125 type = fInstance.getType(fContext2, fType2.getName());
126 assertSame("getType", fType2, type);
127 type = fInstance.getType(fContext2, fType3.getName());
128 assertSame("getType", fType3, type);
129 type = fInstance.getType(fContext2, fType0.getName());
130 assertNull("getType", type);
131 type = fInstance.getType(fContext2, fType1.getName());
132 assertNull("getType", type);
133 }
134
135 // ------------------------------------------------------------------------
136 // Operations
137 // ------------------------------------------------------------------------
138
139 @Test
140 public void testClear() {
141 fInstance.clear();
142 assertEquals("clear", 0, fInstance.getContexts().length);
143 assertEquals("clear", 0, fInstance.getTypes(null).length);
144 assertNull("clear", fInstance.getType(null, null));
145 assertEquals("clear", "TmfEventTypeManager [fEventTypes={}]", fInstance.toString());
146 }
147
148 @Test
149 public void testClearContext() {
150 fInstance.clear();
151 fInstance.add(fContext1, fType0);
152 fInstance.add(fContext1, fType1);
153 fInstance.add(fContext2, fType2);
154 fInstance.add(fContext2, fType3);
155
156 fInstance.clear(fContext1);
157
158 final String[] contexts = fInstance.getContexts();
159 assertEquals("clear context", 1, contexts.length);
160 assertEquals("clear context", fContext2, contexts[0]);
161
162 ITmfEventType[] types = fInstance.getTypes(fContext1);
163 assertEquals("clear context", 0, types.length);
164
165 ITmfEventType type = fInstance.getType(fContext1, fType0.getName());
166 assertNull("clear context", type);
167 type = fInstance.getType(fContext1, fType1.getName());
168 assertNull("clear context", type);
169
170 types = fInstance.getTypes(fContext2);
171 assertEquals("clear context", 2, types.length);
172 if (fType2 == types[0]) {
173 assertSame("clear context", fType3, types[1]);
174 } else {
175 assertSame("clear context", fType2, types[1]);
176 assertSame("clear context", fType3, types[0]);
177 }
178 }
179
180 @Test
181 public void testBasicAdd() {
182 fInstance.clear();
183 fInstance.add(fContext1, fType0);
184
185 final String[] contexts = fInstance.getContexts();
186 assertEquals("add", 1, contexts.length);
187 assertEquals("add", fContext1, contexts[0]);
188
189 final ITmfEventType[] types = fInstance.getTypes(contexts[0]);
190 assertEquals("add", 1, types.length);
191 assertSame("add", fType0, types[0]);
192
193 ITmfEventType type = fInstance.getType(contexts[0], fType0.getName());
194 assertSame("add", fType0, type);
195
196 type = fInstance.getType(contexts[0], fType1.getName());
197 assertNotSame("add", fType0, type);
198 }
199
200 @Test
201 public void testAdd() {
202 fInstance.clear();
203 fInstance.add(fContext1, fType0);
204 fInstance.add(fContext1, fType1);
205 fInstance.add(fContext2, fType2);
206 fInstance.add(fContext2, fType3);
207
208 final String[] contexts = fInstance.getContexts();
209 Arrays.sort(contexts);
210 assertEquals("add", 2, contexts.length);
211 assertEquals("add", fContext1, contexts[0]);
212 assertEquals("add", fContext2, contexts[1]);
213
214 ITmfEventType[] types = fInstance.getTypes(fContext1);
215 assertEquals("add", 2, types.length);
216 if (fType0 == types[0]) {
217 assertSame("add", fType1, types[1]);
218 } else {
219 assertSame("add", fType0, types[1]);
220 assertSame("add", fType1, types[0]);
221 }
222
223 types = fInstance.getTypes(fContext2);
224 assertEquals("add", 2, types.length);
225 if (fType2 == types[0]) {
226 assertSame("add", fType3, types[1]);
227 } else {
228 assertSame("add", fType2, types[1]);
229 assertSame("add", fType3, types[0]);
230 }
231
232 ITmfEventType type = fInstance.getType(fContext1, fType0.getName());
233 assertSame("add", fType0, type);
234 type = fInstance.getType(fContext1, fType1.getName());
235 assertSame("add", fType1, type);
236 type = fInstance.getType(fContext2, fType2.getName());
237 assertSame("add", fType2, type);
238 type = fInstance.getType(fContext2, fType3.getName());
239 assertSame("add", fType3, type);
240
241 type = fInstance.getType(fContext1, fType2.getName());
242 assertNull("add", type);
243 type = fInstance.getType(fContext2, fType0.getName());
244 assertNull("add", type);
245 }
246
247 // ------------------------------------------------------------------------
248 // Object
249 // ------------------------------------------------------------------------
250
251 @Test
252 public void testToString() {
253 fInstance.clear();
254 assertEquals("toString", "TmfEventTypeManager [fEventTypes={}]", fInstance.toString());
255
256 fInstance.add(fContext1, fType0);
257 assertEquals("toString", "TmfEventTypeManager [fEventTypes={" + fContext1 + "={" + fTypeId1 + "=" + fType0 + "}}]", fInstance.toString());
258 }
259
260 }
This page took 0.039623 seconds and 5 git commands to generate.