Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
75d42a16 | 2 | * Copyright (c) 2009, 2012 Ericsson |
d18dd09b ASL |
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 | |
75d42a16 | 11 | * Francois Chouinard - Adjusted for new Event Model |
d18dd09b ASL |
12 | *******************************************************************************/ |
13 | ||
6c13869b | 14 | package org.eclipse.linuxtools.tmf.core.tests.event; |
d18dd09b | 15 | |
cbd4ad82 FC |
16 | import junit.framework.TestCase; |
17 | ||
4c564a2d | 18 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
6c13869b | 19 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
d18dd09b ASL |
20 | |
21 | /** | |
22 | * <b><u>TmfEventFieldTest</u></b> | |
23 | * <p> | |
cbd4ad82 | 24 | * Test suite for the TmfEventField class. |
d18dd09b | 25 | */ |
3b38ea61 | 26 | @SuppressWarnings("nls") |
d18dd09b ASL |
27 | public class TmfEventFieldTest extends TestCase { |
28 | ||
75d42a16 FC |
29 | // ------------------------------------------------------------------------ |
30 | // Variables | |
31 | // ------------------------------------------------------------------------ | |
32 | ||
33 | private final String fFieldName1 = "Field-1"; | |
34 | private final String fFieldName2 = "Field-2"; | |
35 | ||
ea2b103b | 36 | private final Object fValue1 = "Value"; |
37 | private final Object fValue2 = Integer.valueOf(10); | |
75d42a16 | 38 | |
085d898f FC |
39 | private final TmfEventField fField1 = new TmfEventField(fFieldName1, fValue1); |
40 | private final TmfEventField fField2 = new TmfEventField(fFieldName2, fValue2, null); | |
41 | private final TmfEventField fField3 = new TmfEventField(fFieldName1, fValue2, null); | |
75d42a16 FC |
42 | |
43 | private final String fStructRootFieldName = "Root-S"; | |
44 | private final String[] fStructFieldNames = new String[] { fFieldName1, fFieldName2 }; | |
45 | private final TmfEventField fStructTerminalField1 = new TmfEventField(fFieldName1, null); | |
46 | private final TmfEventField fStructTerminalField2 = new TmfEventField(fFieldName2, null); | |
47 | private final TmfEventField fStructTerminalField3 = new TmfEventField(fFieldName1, null); | |
48 | private final TmfEventField fStructRootField = new TmfEventField(fStructRootFieldName, | |
49 | new ITmfEventField[] { fStructTerminalField1, fStructTerminalField2 }); | |
50 | ||
51 | private final String fRootFieldName = "Root"; | |
52 | private final String[] fFieldNames = new String[] { fFieldName1, fFieldName2 }; | |
53 | private final TmfEventField fRootField = new TmfEventField(fRootFieldName, | |
54 | new ITmfEventField[] { fField1, fField2 }); | |
55 | ||
56 | // ------------------------------------------------------------------------ | |
57 | // Housekeeping | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
60 | /** | |
61 | * @param name the test name | |
62 | */ | |
085d898f | 63 | public TmfEventFieldTest(final String name) { |
75d42a16 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 | // Constructors | |
79 | // ------------------------------------------------------------------------ | |
80 | ||
81 | public void testTerminalStructConstructor() { | |
82 | assertSame("getName", fFieldName1, fStructTerminalField1.getName()); | |
83 | assertNull("getValue", fStructTerminalField1.getValue()); | |
84 | assertNull("getFields", fStructTerminalField1.getFields()); | |
85 | assertNull("getField(name)", fStructTerminalField1.getField(fFieldName1)); | |
86 | assertNull("getField(index)", fStructTerminalField1.getField(0)); | |
87 | assertEquals("getFieldNames", 0, fStructTerminalField1.getFieldNames().length); | |
88 | assertNull("getFieldName", fStructTerminalField1.getFieldName(-1)); | |
89 | assertNull("getFieldName", fStructTerminalField1.getFieldName(0)); | |
90 | } | |
91 | ||
92 | public void testNonTerminalStructConstructor() { | |
93 | assertSame("getName", fStructRootFieldName, fStructRootField.getName()); | |
94 | assertNull("getValue", fStructRootField.getValue()); | |
95 | assertEquals("getFields", 2, fStructRootField.getFields().length); | |
96 | assertSame("getField(name)", fStructTerminalField1, fStructRootField.getField(fFieldName1)); | |
97 | assertSame("getField(name)", fStructTerminalField2, fStructRootField.getField(fFieldName2)); | |
98 | assertSame("getField(index)", fStructTerminalField1, fStructRootField.getField(0)); | |
99 | assertSame("getField(index)", fStructTerminalField2, fStructRootField.getField(1)); | |
100 | ||
085d898f | 101 | final String[] names = fStructRootField.getFieldNames(); |
75d42a16 FC |
102 | assertEquals("getFieldNames length", 2, names.length); |
103 | for (int i = 0; i < names.length; i++) { | |
104 | assertSame("getFieldNames", fStructFieldNames[i], names[i]); | |
105 | assertSame("getFieldName", fFieldNames[i], fStructRootField.getFieldName(i)); | |
106 | } | |
107 | assertNull("getFieldName", fStructRootField.getFieldName(-1)); | |
108 | assertNull("getFieldName", fStructRootField.getFieldName(names.length)); | |
109 | } | |
110 | ||
111 | public void testTerminalConstructor() { | |
112 | assertSame("getName", fFieldName1, fField1.getName()); | |
113 | assertSame("getValue", fValue1, fField1.getValue()); | |
114 | assertNull("getFields", fField1.getFields()); | |
115 | assertNull("getField(name)", fField1.getField(fFieldName1)); | |
116 | assertNull("getField(index)", fField1.getField(0)); | |
117 | assertEquals("getFieldNames", 0, fField1.getFieldNames().length); | |
118 | assertNull("getFieldName", fField1.getFieldName(0)); | |
119 | ||
120 | assertSame("getName", fFieldName2, fField2.getName()); | |
121 | assertSame("getValue", fValue2, fField2.getValue()); | |
122 | assertNull("getFields", fField2.getFields()); | |
123 | assertNull("getField(name)", fField2.getField(fFieldName2)); | |
124 | assertNull("getField(index)", fField2.getField(0)); | |
125 | assertEquals("getFieldNames", 0, fField2.getFieldNames().length); | |
126 | assertNull("getFieldName", fField2.getFieldName(0)); | |
127 | } | |
128 | ||
129 | public void testNonTerminalConstructor() { | |
130 | assertSame("getName", fRootFieldName, fRootField.getName()); | |
131 | assertNull("getValue", fRootField.getValue()); | |
132 | assertEquals("getFields", 2, fRootField.getFields().length); | |
133 | assertSame("getField(name)", fField1, fRootField.getField(fFieldName1)); | |
134 | assertSame("getField(name)", fField2, fRootField.getField(fFieldName2)); | |
135 | assertSame("getField(index)", fField1, fRootField.getField(0)); | |
136 | assertSame("getField(index)", fField2, fRootField.getField(1)); | |
137 | ||
085d898f | 138 | final String[] names = fRootField.getFieldNames(); |
75d42a16 FC |
139 | assertEquals("getFieldNames length", 2, names.length); |
140 | for (int i = 0; i < names.length; i++) { | |
141 | assertSame("getFieldNames", fFieldNames[i], names[i]); | |
142 | assertSame("getFieldName", fFieldNames[i], fRootField.getFieldName(i)); | |
143 | } | |
144 | assertNull("getFieldName", fRootField.getFieldName(-1)); | |
145 | assertNull("getFieldName", fRootField.getFieldName(names.length)); | |
146 | } | |
147 | ||
148 | public void testConstructorBadArg() { | |
149 | try { | |
150 | new TmfEventField(null, fValue1, null); | |
151 | fail("Invalid (null) field name"); | |
085d898f | 152 | } catch (final IllegalArgumentException e) { |
75d42a16 FC |
153 | } |
154 | } | |
155 | ||
156 | public void testTerminalCopyConstructor() { | |
085d898f | 157 | final TmfEventField copy = new TmfEventField(fField1); |
75d42a16 FC |
158 | assertSame("getName", fFieldName1, copy.getName()); |
159 | assertSame("getValue", fValue1, copy.getValue()); | |
160 | assertNull("getFields", copy.getFields()); | |
161 | assertNull("getField(name)", copy.getField(fFieldName1)); | |
162 | assertNull("getField(index)", copy.getField(0)); | |
163 | assertEquals("getFieldNames", 0, copy.getFieldNames().length); | |
164 | assertNull("getFieldName", copy.getFieldName(0)); | |
165 | } | |
166 | ||
167 | public void testNonTerminalCopyConstructor() { | |
168 | assertSame("getName", fRootFieldName, fRootField.getName()); | |
169 | assertNull("getValue", fRootField.getValue()); | |
170 | assertEquals("getFields", 2, fRootField.getFields().length); | |
171 | assertSame("getField(name)", fField1, fRootField.getField(fFieldName1)); | |
172 | assertSame("getField(name)", fField2, fRootField.getField(fFieldName2)); | |
173 | assertSame("getField(index)", fField1, fRootField.getField(0)); | |
174 | assertSame("getField(index)", fField2, fRootField.getField(1)); | |
175 | ||
085d898f | 176 | final String[] names = fRootField.getFieldNames(); |
75d42a16 FC |
177 | assertEquals("getFieldNames length", 2, names.length); |
178 | for (int i = 0; i < names.length; i++) { | |
179 | assertSame("getFieldNames", fFieldNames[i], names[i]); | |
180 | assertSame("getFieldName", fFieldNames[i], fRootField.getFieldName(i)); | |
181 | } | |
182 | assertNull("getFieldName", fRootField.getFieldName(names.length)); | |
183 | } | |
184 | ||
185 | public void testCopyConstructorBadArg() { | |
186 | try { | |
187 | new TmfEventField(null); | |
188 | fail("TmfEventField: null arguemnt"); | |
085d898f | 189 | } catch (final IllegalArgumentException e) { |
75d42a16 FC |
190 | } |
191 | } | |
192 | ||
193 | // ------------------------------------------------------------------------ | |
194 | // Modifiers | |
195 | // ------------------------------------------------------------------------ | |
196 | ||
ea2b103b | 197 | private static class MyField extends TmfEventField { |
75d42a16 | 198 | |
085d898f | 199 | public MyField(final String id, final Object value) { |
75d42a16 FC |
200 | super(id, value); |
201 | } | |
202 | ||
085d898f | 203 | public MyField(final TmfEventField field) { |
75d42a16 FC |
204 | super(field); |
205 | } | |
206 | ||
207 | @Override | |
085d898f | 208 | public void setValue(final Object value, final ITmfEventField[] subfields) { |
75d42a16 FC |
209 | super.setValue(value, subfields); |
210 | } | |
211 | } | |
212 | ||
213 | public void testSetValue() { | |
085d898f | 214 | final TmfEventField field = new TmfEventField(fFieldName1, fValue1, null); |
d18dd09b | 215 | |
085d898f | 216 | final MyField myField = new MyField(field); |
75d42a16 FC |
217 | assertSame("getValue", fValue1, myField.getValue()); |
218 | myField.setValue(fValue2, null); | |
219 | assertSame("getValue", fValue2, myField.getValue()); | |
cbdacf03 FC |
220 | myField.setValue(fValue2, new TmfEventField[] { field }); |
221 | assertSame("getValue", fValue2, myField.getValue()); | |
75d42a16 | 222 | } |
cbd4ad82 FC |
223 | |
224 | // ------------------------------------------------------------------------ | |
75d42a16 FC |
225 | // clone |
226 | // ------------------------------------------------------------------------ | |
227 | ||
de126dbb FC |
228 | public void testFieldClone() throws Exception { |
229 | TmfEventField clone = fField1.clone(); | |
230 | assertTrue("clone", fField1.clone().equals(fField1)); | |
231 | assertTrue("clone", clone.clone().equals(clone)); | |
75d42a16 | 232 | assertEquals("clone", fField1, clone); |
de126dbb | 233 | assertEquals("clone", clone, fField1); |
75d42a16 FC |
234 | |
235 | clone = fRootField.clone(); | |
de126dbb FC |
236 | assertTrue("clone", fRootField.clone().equals(fRootField)); |
237 | assertTrue("clone", clone.clone().equals(clone)); | |
75d42a16 | 238 | assertEquals("clone", fRootField, clone); |
de126dbb FC |
239 | assertEquals("clone", clone, fRootField); |
240 | } | |
241 | ||
242 | public void testStructFieldClone() throws Exception { | |
243 | TmfEventField clone = fStructTerminalField1.clone(); | |
244 | assertTrue("clone", fStructTerminalField1.clone().equals(fStructTerminalField1)); | |
245 | assertTrue("clone", clone.clone().equals(clone)); | |
246 | assertEquals("clone", fStructTerminalField1, clone); | |
247 | assertEquals("clone", clone, fStructTerminalField1); | |
75d42a16 FC |
248 | |
249 | clone = fStructRootField.clone(); | |
de126dbb FC |
250 | assertTrue("clone", fStructRootField.clone().equals(fStructRootField)); |
251 | assertTrue("clone", clone.clone().equals(clone)); | |
75d42a16 | 252 | assertEquals("clone", fStructRootField, clone); |
de126dbb | 253 | assertEquals("clone", clone, fStructRootField); |
75d42a16 FC |
254 | } |
255 | ||
256 | // ------------------------------------------------------------------------ | |
257 | // hashCode | |
258 | // ------------------------------------------------------------------------ | |
259 | ||
260 | public void testHashCode() throws Exception { | |
261 | TmfEventField copy = new TmfEventField(fField1); | |
262 | assertTrue("hashCode", fField1.hashCode() == copy.hashCode()); | |
263 | assertTrue("hashCode", fField1.hashCode() != fField2.hashCode()); | |
264 | ||
265 | copy = new TmfEventField(fStructTerminalField1); | |
266 | assertTrue("hashCode", fStructTerminalField1.hashCode() == copy.hashCode()); | |
267 | assertTrue("hashCode", fStructTerminalField1.hashCode() != fStructTerminalField2.hashCode()); | |
268 | } | |
269 | ||
270 | // ------------------------------------------------------------------------ | |
271 | // equals | |
272 | // ------------------------------------------------------------------------ | |
273 | ||
274 | public void testEqualsReflexivity() throws Exception { | |
275 | assertTrue("equals", fField1.equals(fField1)); | |
276 | assertTrue("equals", fField2.equals(fField2)); | |
277 | ||
278 | assertFalse("equals", fField1.equals(fField2)); | |
279 | assertFalse("equals", fField2.equals(fField1)); | |
280 | ||
281 | assertTrue("equals", fStructTerminalField1.equals(fStructTerminalField1)); | |
282 | assertTrue("equals", fStructTerminalField2.equals(fStructTerminalField2)); | |
283 | ||
284 | assertFalse("equals", fStructTerminalField1.equals(fStructTerminalField2)); | |
285 | assertFalse("equals", fStructTerminalField2.equals(fStructTerminalField1)); | |
286 | } | |
287 | ||
288 | public void testEqualsSymmetry() throws Exception { | |
085d898f | 289 | final TmfEventField copy0 = new TmfEventField(fField1); |
75d42a16 FC |
290 | assertTrue("equals", fField1.equals(copy0)); |
291 | assertTrue("equals", copy0.equals(fField1)); | |
085d898f FC |
292 | |
293 | final TmfEventField copy3 = new TmfEventField(fField2); | |
75d42a16 FC |
294 | assertTrue("equals", fField2.equals(copy3)); |
295 | assertTrue("equals", copy3.equals(fField2)); | |
296 | } | |
297 | ||
298 | public void testEqualsTransivity() throws Exception { | |
299 | TmfEventField copy1 = new TmfEventField(fField1); | |
300 | TmfEventField copy2 = new TmfEventField(copy1); | |
301 | assertTrue("equals", fField1.equals(copy1)); | |
302 | assertTrue("equals", copy1.equals(copy2)); | |
303 | assertTrue("equals", fField1.equals(copy2)); | |
304 | ||
305 | copy1 = new TmfEventField(fField2); | |
306 | copy2 = new TmfEventField(copy1); | |
307 | assertTrue("equals", fField2.equals(copy1)); | |
308 | assertTrue("equals", copy1.equals(copy2)); | |
309 | assertTrue("equals", fField2.equals(copy2)); | |
310 | } | |
311 | ||
75d42a16 FC |
312 | public void testEquals() throws Exception { |
313 | assertTrue("equals", fStructTerminalField1.equals(fStructTerminalField3)); | |
314 | assertTrue("equals", fStructTerminalField3.equals(fStructTerminalField1)); | |
315 | ||
316 | assertFalse("equals", fStructTerminalField1.equals(fField3)); | |
317 | assertFalse("equals", fField3.equals(fStructTerminalField1)); | |
318 | } | |
319 | ||
39f9eadb FC |
320 | public void testEqualsNull() throws Exception { |
321 | assertFalse("equals", fField1.equals(null)); | |
322 | assertFalse("equals", fField2.equals(null)); | |
323 | } | |
324 | ||
325 | public void testNonEqualClasses() throws Exception { | |
326 | assertFalse("equals", fField1.equals(fStructTerminalField1)); | |
327 | assertFalse("equals", fField1.equals(fValue1)); | |
328 | } | |
329 | ||
330 | public void testNonEqualValues() throws Exception { | |
085d898f | 331 | final TmfEventField copy1 = new TmfEventField(fFieldName1, fValue1); |
39f9eadb FC |
332 | TmfEventField copy2 = new TmfEventField(fFieldName1, fValue1); |
333 | assertTrue("equals", copy1.equals(copy2)); | |
334 | assertTrue("equals", copy2.equals(copy1)); | |
335 | ||
336 | copy2 = new TmfEventField(fFieldName1, fValue2); | |
337 | assertFalse("equals", copy1.equals(copy2)); | |
338 | assertFalse("equals", copy2.equals(copy1)); | |
339 | ||
340 | copy2 = new TmfEventField(fFieldName1, null); | |
341 | assertFalse("equals", copy1.equals(copy2)); | |
342 | assertFalse("equals", copy2.equals(copy1)); | |
343 | } | |
344 | ||
75d42a16 FC |
345 | public void testNonEquals() throws Exception { |
346 | assertFalse("equals", fField1.equals(fField2)); | |
347 | assertFalse("equals", fField2.equals(fField1)); | |
348 | ||
349 | assertFalse("equals", fField1.equals(fStructTerminalField1)); | |
350 | } | |
351 | ||
75d42a16 FC |
352 | // ------------------------------------------------------------------------ |
353 | // toString | |
354 | // ------------------------------------------------------------------------ | |
355 | ||
356 | public void testToString() { | |
085d898f | 357 | final String expected1 = "TmfEventField [fFieldId=" + fFieldName1 + ", fValue=" + fValue1.toString() + "]"; |
75d42a16 FC |
358 | TmfEventField field = new TmfEventField(fFieldName1, fValue1, null); |
359 | assertEquals("toString", expected1, field.toString()); | |
360 | ||
085d898f | 361 | final String expected2 = "TmfEventField [fFieldId=" + fFieldName1 + ", fValue=" + fValue2.toString() + "]"; |
75d42a16 FC |
362 | field = new TmfEventField(fFieldName1, fValue2, null); |
363 | assertEquals("toString", expected2, field.toString()); | |
364 | } | |
365 | ||
366 | // ------------------------------------------------------------------------ | |
367 | // makeRoot | |
368 | // ------------------------------------------------------------------------ | |
369 | ||
370 | public void testMakeRoot() { | |
371 | ITmfEventField root = TmfEventField.makeRoot(fStructFieldNames); | |
372 | String[] names = root.getFieldNames(); | |
373 | assertEquals("getFieldNames length", 2, names.length); | |
374 | for (int i = 0; i < names.length; i++) { | |
375 | assertSame("getFieldNames", fStructFieldNames[i], names[i]); | |
376 | assertSame("getFieldName", fStructFieldNames[i], root.getFieldName(i)); | |
377 | assertNull("getValue", root.getField(i).getValue()); | |
378 | } | |
379 | assertNull("getFieldName", root.getFieldName(-1)); | |
380 | assertNull("getFieldName", root.getFieldName(names.length)); | |
381 | ||
382 | root = TmfEventField.makeRoot(fFieldNames); | |
383 | names = root.getFieldNames(); | |
384 | assertEquals("getFieldNames length", 2, names.length); | |
385 | for (int i = 0; i < names.length; i++) { | |
386 | assertSame("getFieldNames", fFieldNames[i], names[i]); | |
387 | assertSame("getFieldName", fFieldNames[i], root.getFieldName(i)); | |
388 | assertNull("getValue", root.getField(i).getValue()); | |
389 | } | |
390 | assertNull("getFieldName", root.getFieldName(-1)); | |
391 | assertNull("getFieldName", root.getFieldName(names.length)); | |
392 | } | |
d18dd09b ASL |
393 | |
394 | } |