Improve test coverage for TmfEvent and TmfEventTypeManager
[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 * <b><u>TmfEventTypeManagerTest</u></b>
26 * <p>
27 * Test suite for the TmfEventTypeManager class.
28 * <p>
29 */
30 @SuppressWarnings("nls")
31 public class TmfEventTypeManagerTest extends TestCase {
32
33 // ------------------------------------------------------------------------
34 // Variables
35 // ------------------------------------------------------------------------
36
37 private static final TmfEventTypeManager fInstance = TmfEventTypeManager.getInstance();
38
39 private final String fContext1 = "JUnit context 1";
40 private final String fContext2 = "JUnit context 2";
41
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";
46
47 private final String fLabel0 = "label1";
48 private final String fLabel1 = "label2";
49
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 };
53
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));
58
59 // ------------------------------------------------------------------------
60 // Housekeeping
61 // ------------------------------------------------------------------------
62
63 /**
64 * @param name the test name
65 */
66 public TmfEventTypeManagerTest(String name) {
67 super(name);
68 }
69
70 @Override
71 protected void setUp() throws Exception {
72 super.setUp();
73 }
74
75 @Override
76 protected void tearDown() throws Exception {
77 super.tearDown();
78 }
79
80 // ------------------------------------------------------------------------
81 // Getters
82 // ------------------------------------------------------------------------
83
84 public void testGetContexts() {
85 fInstance.clear();
86 fInstance.add(fContext1, fType0);
87 fInstance.add(fContext1, fType1);
88 fInstance.add(fContext2, fType2);
89 fInstance.add(fContext2, fType3);
90
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]);
96 }
97
98 public void testGetTypes() {
99 fInstance.clear();
100 fInstance.add(fContext1, fType0);
101 fInstance.add(fContext1, fType1);
102 fInstance.add(fContext2, fType2);
103 fInstance.add(fContext2, fType3);
104
105 ITmfEventType[] types = fInstance.getTypes(fContext1);
106 assertEquals("getTypes", 2, types.length);
107 if (fType0 == types[0]) {
108 assertSame("getTypes", fType1, types[1]);
109 } else {
110 assertSame("getTypes", fType0, types[1]);
111 assertSame("getTypes", fType1, types[0]);
112 }
113
114 types = fInstance.getTypes(fContext2);
115 assertEquals("getTypes", 2, types.length);
116 if (fType2 == types[0]) {
117 assertSame("getTypes", fType3, types[1]);
118 } else {
119 assertSame("getTypes", fType2, types[1]);
120 assertSame("getTypes", fType3, types[0]);
121 }
122 }
123
124 public void testGetType() {
125 fInstance.clear();
126 fInstance.add(fContext1, fType0);
127 fInstance.add(fContext1, fType1);
128 fInstance.add(fContext2, fType2);
129 fInstance.add(fContext2, fType3);
130
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);
139
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);
148 }
149
150 // ------------------------------------------------------------------------
151 // Operations
152 // ------------------------------------------------------------------------
153
154 public void testClear() {
155 fInstance.clear();
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());
160 }
161
162 public void testClearContext() {
163 fInstance.clear();
164 fInstance.add(fContext1, fType0);
165 fInstance.add(fContext1, fType1);
166 fInstance.add(fContext2, fType2);
167 fInstance.add(fContext2, fType3);
168
169 fInstance.clear(fContext1);
170
171 String[] contexts = fInstance.getContexts();
172 assertEquals("clear context", 1, contexts.length);
173 assertEquals("clear context", fContext2, contexts[0]);
174
175 ITmfEventType[] types = fInstance.getTypes(fContext1);
176 assertNull("clear context", types);
177
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);
182
183 types = fInstance.getTypes(fContext2);
184 assertEquals("clear context", 2, types.length);
185 if (fType2 == types[0]) {
186 assertSame("clear context", fType3, types[1]);
187 } else {
188 assertSame("clear context", fType2, types[1]);
189 assertSame("clear context", fType3, types[0]);
190 }
191 }
192
193 public void testBasicAdd() {
194 fInstance.clear();
195 fInstance.add(fContext1, fType0);
196
197 String[] contexts = fInstance.getContexts();
198 assertEquals("add", 1, contexts.length);
199 assertEquals("add", fContext1, contexts[0]);
200
201 ITmfEventType[] types = fInstance.getTypes(contexts[0]);
202 assertEquals("add", 1, types.length);
203 assertSame("add", fType0, types[0]);
204
205 ITmfEventType type = fInstance.getType(contexts[0], fType0.getName());
206 assertSame("add", fType0, type);
207
208 type = fInstance.getType(contexts[0], fType1.getName());
209 assertNotSame("add", fType0, type);
210 }
211
212 public void testAdd() {
213 fInstance.clear();
214 fInstance.add(fContext1, fType0);
215 fInstance.add(fContext1, fType1);
216 fInstance.add(fContext2, fType2);
217 fInstance.add(fContext2, fType3);
218
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]);
224
225 ITmfEventType[] types = fInstance.getTypes(fContext1);
226 assertEquals("add", 2, types.length);
227 if (fType0 == types[0]) {
228 assertSame("add", fType1, types[1]);
229 } else {
230 assertSame("add", fType0, types[1]);
231 assertSame("add", fType1, types[0]);
232 }
233
234 types = fInstance.getTypes(fContext2);
235 assertEquals("add", 2, types.length);
236 if (fType2 == types[0]) {
237 assertSame("add", fType3, types[1]);
238 } else {
239 assertSame("add", fType2, types[1]);
240 assertSame("add", fType3, types[0]);
241 }
242
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);
251
252 type = fInstance.getType(fContext1, fType2.getName());
253 assertNull("add", type);
254 type = fInstance.getType(fContext2, fType0.getName());
255 assertNull("add", type);
256 }
257
258 // ------------------------------------------------------------------------
259 // Object
260 // ------------------------------------------------------------------------
261
262 public void testToString() {
263 fInstance.clear();
264 assertEquals("toString", "TmfEventTypeManager [fEventTypes={}]", fInstance.toString());
265
266 fInstance.add(fContext1, fType0);
267 assertEquals("toString", "TmfEventTypeManager [fEventTypes={" + fContext1 + "={" + fTypeId1 + "=" + fType0 + "}}]", fInstance.toString());
268 }
269
270 }
This page took 0.038064 seconds and 6 git commands to generate.