Commit | Line | Data |
---|---|---|
39f9eadb FC |
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 | /** | |
39f9eadb | 25 | * Test suite for the TmfEventTypeManager class. |
39f9eadb FC |
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"; | |
085d898f | 46 | |
39f9eadb FC |
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 | */ | |
085d898f | 63 | public TmfEventTypeManagerTest(final String name) { |
39f9eadb FC |
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 | ||
085d898f | 88 | final String[] contexts = fInstance.getContexts(); |
39f9eadb FC |
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); | |
085d898f | 104 | if (fType0 == types[0]) |
39f9eadb | 105 | assertSame("getTypes", fType1, types[1]); |
085d898f | 106 | else { |
39f9eadb FC |
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); | |
085d898f | 113 | if (fType2 == types[0]) |
39f9eadb | 114 | assertSame("getTypes", fType3, types[1]); |
085d898f | 115 | else { |
39f9eadb FC |
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); | |
8b63ffb9 | 154 | assertEquals("clear", 0, fInstance.getTypes(null).length); |
39f9eadb FC |
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 | ||
085d898f | 168 | final String[] contexts = fInstance.getContexts(); |
39f9eadb FC |
169 | assertEquals("clear context", 1, contexts.length); |
170 | assertEquals("clear context", fContext2, contexts[0]); | |
171 | ||
172 | ITmfEventType[] types = fInstance.getTypes(fContext1); | |
8b63ffb9 | 173 | assertEquals("clear context", 0, types.length); |
39f9eadb FC |
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); | |
085d898f | 182 | if (fType2 == types[0]) |
39f9eadb | 183 | assertSame("clear context", fType3, types[1]); |
085d898f | 184 | else { |
39f9eadb FC |
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 | ||
085d898f | 194 | final String[] contexts = fInstance.getContexts(); |
39f9eadb FC |
195 | assertEquals("add", 1, contexts.length); |
196 | assertEquals("add", fContext1, contexts[0]); | |
197 | ||
085d898f | 198 | final ITmfEventType[] types = fInstance.getTypes(contexts[0]); |
39f9eadb FC |
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 | ||
085d898f | 216 | final String[] contexts = fInstance.getContexts(); |
39f9eadb FC |
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); | |
085d898f | 224 | if (fType0 == types[0]) |
39f9eadb | 225 | assertSame("add", fType1, types[1]); |
085d898f | 226 | else { |
39f9eadb FC |
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); | |
085d898f | 233 | if (fType2 == types[0]) |
39f9eadb | 234 | assertSame("add", fType3, types[1]); |
085d898f | 235 | else { |
39f9eadb FC |
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 | } |