1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adjusted for new Event Model
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.event
;
16 import junit
.framework
.TestCase
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
22 * Test suite for the TmfEventField class.
24 @SuppressWarnings("nls")
25 public class TmfEventFieldTest
extends TestCase
{
27 // ------------------------------------------------------------------------
29 // ------------------------------------------------------------------------
31 private final String fFieldName1
= "Field-1";
32 private final String fFieldName2
= "Field-2";
34 private final Object fValue1
= "Value";
35 private final Object fValue2
= Integer
.valueOf(10);
37 private final TmfEventField fField1
= new TmfEventField(fFieldName1
, fValue1
);
38 private final TmfEventField fField2
= new TmfEventField(fFieldName2
, fValue2
, null);
39 private final TmfEventField fField3
= new TmfEventField(fFieldName1
, fValue2
, null);
41 private final String fStructRootFieldName
= "Root-S";
42 private final String
[] fStructFieldNames
= new String
[] { fFieldName1
, fFieldName2
};
43 private final TmfEventField fStructTerminalField1
= new TmfEventField(fFieldName1
, null);
44 private final TmfEventField fStructTerminalField2
= new TmfEventField(fFieldName2
, null);
45 private final TmfEventField fStructTerminalField3
= new TmfEventField(fFieldName1
, null);
46 private final TmfEventField fStructRootField
= new TmfEventField(fStructRootFieldName
,
47 new ITmfEventField
[] { fStructTerminalField1
, fStructTerminalField2
});
49 private final String fRootFieldName
= "Root";
50 private final String
[] fFieldNames
= new String
[] { fFieldName1
, fFieldName2
};
51 private final TmfEventField fRootField
= new TmfEventField(fRootFieldName
,
52 new ITmfEventField
[] { fField1
, fField2
});
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
59 * @param name the test name
61 public TmfEventFieldTest(final String name
) {
66 protected void setUp() throws Exception
{
71 protected void tearDown() throws Exception
{
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
82 public void testTerminalStructConstructor() {
83 assertSame("getName", fFieldName1
, fStructTerminalField1
.getName());
84 assertNull("getValue", fStructTerminalField1
.getValue());
85 assertEquals("getFields", 0, fStructTerminalField1
.getFields().length
);
86 assertNull("getField(name)", fStructTerminalField1
.getField(fFieldName1
));
87 assertNull("getField(index)", fStructTerminalField1
.getField(0));
88 assertEquals("getFieldNames", 0, fStructTerminalField1
.getFieldNames().length
);
89 assertNull("getFieldName", fStructTerminalField1
.getFieldName(-1));
90 assertNull("getFieldName", fStructTerminalField1
.getFieldName(0));
96 public void testNonTerminalStructConstructor() {
97 assertSame("getName", fStructRootFieldName
, fStructRootField
.getName());
98 assertNull("getValue", fStructRootField
.getValue());
99 assertEquals("getFields", 2, fStructRootField
.getFields().length
);
100 assertSame("getField(name)", fStructTerminalField1
, fStructRootField
.getField(fFieldName1
));
101 assertSame("getField(name)", fStructTerminalField2
, fStructRootField
.getField(fFieldName2
));
102 assertSame("getField(index)", fStructTerminalField1
, fStructRootField
.getField(0));
103 assertSame("getField(index)", fStructTerminalField2
, fStructRootField
.getField(1));
105 final String
[] names
= fStructRootField
.getFieldNames();
106 assertEquals("getFieldNames length", 2, names
.length
);
107 for (int i
= 0; i
< names
.length
; i
++) {
108 assertSame("getFieldNames", fStructFieldNames
[i
], names
[i
]);
109 assertSame("getFieldName", fFieldNames
[i
], fStructRootField
.getFieldName(i
));
111 assertNull("getFieldName", fStructRootField
.getFieldName(-1));
112 assertNull("getFieldName", fStructRootField
.getFieldName(names
.length
));
118 public void testTerminalConstructor() {
119 assertSame("getName", fFieldName1
, fField1
.getName());
120 assertSame("getValue", fValue1
, fField1
.getValue());
121 assertEquals("getFields", 0, fField1
.getFields().length
);
122 assertNull("getField(name)", fField1
.getField(fFieldName1
));
123 assertNull("getField(index)", fField1
.getField(0));
124 assertEquals("getFieldNames", 0, fField1
.getFieldNames().length
);
125 assertNull("getFieldName", fField1
.getFieldName(0));
127 assertSame("getName", fFieldName2
, fField2
.getName());
128 assertSame("getValue", fValue2
, fField2
.getValue());
129 assertEquals("getFields", 0, fField2
.getFields().length
);
130 assertNull("getField(name)", fField2
.getField(fFieldName2
));
131 assertNull("getField(index)", fField2
.getField(0));
132 assertEquals("getFieldNames", 0, fField2
.getFieldNames().length
);
133 assertNull("getFieldName", fField2
.getFieldName(0));
139 public void testNonTerminalConstructor() {
140 assertSame("getName", fRootFieldName
, fRootField
.getName());
141 assertNull("getValue", fRootField
.getValue());
142 assertEquals("getFields", 2, fRootField
.getFields().length
);
143 assertSame("getField(name)", fField1
, fRootField
.getField(fFieldName1
));
144 assertSame("getField(name)", fField2
, fRootField
.getField(fFieldName2
));
145 assertSame("getField(index)", fField1
, fRootField
.getField(0));
146 assertSame("getField(index)", fField2
, fRootField
.getField(1));
148 final String
[] names
= fRootField
.getFieldNames();
149 assertEquals("getFieldNames length", 2, names
.length
);
150 for (int i
= 0; i
< names
.length
; i
++) {
151 assertSame("getFieldNames", fFieldNames
[i
], names
[i
]);
152 assertSame("getFieldName", fFieldNames
[i
], fRootField
.getFieldName(i
));
154 assertNull("getFieldName", fRootField
.getFieldName(-1));
155 assertNull("getFieldName", fRootField
.getFieldName(names
.length
));
161 public void testConstructorBadArg() {
163 new TmfEventField(null, fValue1
, null);
164 fail("Invalid (null) field name");
165 } catch (final IllegalArgumentException e
) {
172 public void testTerminalCopyConstructor() {
173 final TmfEventField copy
= new TmfEventField(fField1
);
174 assertSame("getName", fFieldName1
, copy
.getName());
175 assertSame("getValue", fValue1
, copy
.getValue());
176 assertEquals("getFields", 0, copy
.getFields().length
);
177 assertNull("getField(name)", copy
.getField(fFieldName1
));
178 assertNull("getField(index)", copy
.getField(0));
179 assertEquals("getFieldNames", 0, copy
.getFieldNames().length
);
180 assertNull("getFieldName", copy
.getFieldName(0));
186 public void testNonTerminalCopyConstructor() {
187 assertSame("getName", fRootFieldName
, fRootField
.getName());
188 assertNull("getValue", fRootField
.getValue());
189 assertEquals("getFields", 2, fRootField
.getFields().length
);
190 assertSame("getField(name)", fField1
, fRootField
.getField(fFieldName1
));
191 assertSame("getField(name)", fField2
, fRootField
.getField(fFieldName2
));
192 assertSame("getField(index)", fField1
, fRootField
.getField(0));
193 assertSame("getField(index)", fField2
, fRootField
.getField(1));
195 final String
[] names
= fRootField
.getFieldNames();
196 assertEquals("getFieldNames length", 2, names
.length
);
197 for (int i
= 0; i
< names
.length
; i
++) {
198 assertSame("getFieldNames", fFieldNames
[i
], names
[i
]);
199 assertSame("getFieldName", fFieldNames
[i
], fRootField
.getFieldName(i
));
201 assertNull("getFieldName", fRootField
.getFieldName(names
.length
));
207 public void testCopyConstructorBadArg() {
209 new TmfEventField(null);
210 fail("TmfEventField: null arguemnt");
211 } catch (final IllegalArgumentException e
) {
215 // ------------------------------------------------------------------------
217 // ------------------------------------------------------------------------
219 private static class MyField
extends TmfEventField
{
221 public MyField(final String id
, final Object value
) {
225 public MyField(final TmfEventField field
) {
230 public void setValue(final Object value
, final ITmfEventField
[] subfields
) {
231 super.setValue(value
, subfields
);
238 public void testSetValue() {
239 final TmfEventField field
= new TmfEventField(fFieldName1
, fValue1
, null);
241 final MyField myField
= new MyField(field
);
242 assertSame("getValue", fValue1
, myField
.getValue());
243 myField
.setValue(fValue2
, null);
244 assertSame("getValue", fValue2
, myField
.getValue());
245 myField
.setValue(fValue2
, new TmfEventField
[] { field
});
246 assertSame("getValue", fValue2
, myField
.getValue());
249 // ------------------------------------------------------------------------
251 // ------------------------------------------------------------------------
256 public void testFieldClone() {
257 TmfEventField clone
= fField1
.clone();
258 assertTrue("clone", fField1
.clone().equals(fField1
));
259 assertTrue("clone", clone
.clone().equals(clone
));
260 assertEquals("clone", fField1
, clone
);
261 assertEquals("clone", clone
, fField1
);
263 clone
= fRootField
.clone();
264 assertTrue("clone", fRootField
.clone().equals(fRootField
));
265 assertTrue("clone", clone
.clone().equals(clone
));
266 assertEquals("clone", fRootField
, clone
);
267 assertEquals("clone", clone
, fRootField
);
273 public void testStructFieldClone() {
274 TmfEventField clone
= fStructTerminalField1
.clone();
275 assertTrue("clone", fStructTerminalField1
.clone().equals(fStructTerminalField1
));
276 assertTrue("clone", clone
.clone().equals(clone
));
277 assertEquals("clone", fStructTerminalField1
, clone
);
278 assertEquals("clone", clone
, fStructTerminalField1
);
280 clone
= fStructRootField
.clone();
281 assertTrue("clone", fStructRootField
.clone().equals(fStructRootField
));
282 assertTrue("clone", clone
.clone().equals(clone
));
283 assertEquals("clone", fStructRootField
, clone
);
284 assertEquals("clone", clone
, fStructRootField
);
287 // ------------------------------------------------------------------------
289 // ------------------------------------------------------------------------
294 public void testHashCode() {
295 TmfEventField copy
= new TmfEventField(fField1
);
296 assertTrue("hashCode", fField1
.hashCode() == copy
.hashCode());
297 assertTrue("hashCode", fField1
.hashCode() != fField2
.hashCode());
299 copy
= new TmfEventField(fStructTerminalField1
);
300 assertTrue("hashCode", fStructTerminalField1
.hashCode() == copy
.hashCode());
301 assertTrue("hashCode", fStructTerminalField1
.hashCode() != fStructTerminalField2
.hashCode());
304 // ------------------------------------------------------------------------
306 // ------------------------------------------------------------------------
311 public void testEqualsReflexivity() {
312 assertTrue("equals", fField1
.equals(fField1
));
313 assertTrue("equals", fField2
.equals(fField2
));
315 assertFalse("equals", fField1
.equals(fField2
));
316 assertFalse("equals", fField2
.equals(fField1
));
318 assertTrue("equals", fStructTerminalField1
.equals(fStructTerminalField1
));
319 assertTrue("equals", fStructTerminalField2
.equals(fStructTerminalField2
));
321 assertFalse("equals", fStructTerminalField1
.equals(fStructTerminalField2
));
322 assertFalse("equals", fStructTerminalField2
.equals(fStructTerminalField1
));
328 public void testEqualsSymmetry() {
329 final TmfEventField copy0
= new TmfEventField(fField1
);
330 assertTrue("equals", fField1
.equals(copy0
));
331 assertTrue("equals", copy0
.equals(fField1
));
333 final TmfEventField copy3
= new TmfEventField(fField2
);
334 assertTrue("equals", fField2
.equals(copy3
));
335 assertTrue("equals", copy3
.equals(fField2
));
341 public void testEqualsTransivity() {
342 TmfEventField copy1
= new TmfEventField(fField1
);
343 TmfEventField copy2
= new TmfEventField(copy1
);
344 assertTrue("equals", fField1
.equals(copy1
));
345 assertTrue("equals", copy1
.equals(copy2
));
346 assertTrue("equals", fField1
.equals(copy2
));
348 copy1
= new TmfEventField(fField2
);
349 copy2
= new TmfEventField(copy1
);
350 assertTrue("equals", fField2
.equals(copy1
));
351 assertTrue("equals", copy1
.equals(copy2
));
352 assertTrue("equals", fField2
.equals(copy2
));
358 public void testEquals() {
359 assertTrue("equals", fStructTerminalField1
.equals(fStructTerminalField3
));
360 assertTrue("equals", fStructTerminalField3
.equals(fStructTerminalField1
));
362 assertFalse("equals", fStructTerminalField1
.equals(fField3
));
363 assertFalse("equals", fField3
.equals(fStructTerminalField1
));
369 public void testEqualsNull() {
370 assertFalse("equals", fField1
.equals(null));
371 assertFalse("equals", fField2
.equals(null));
377 public void testNonEqualClasses() {
378 assertFalse("equals", fField1
.equals(fStructTerminalField1
));
379 assertFalse("equals", fField1
.equals(fValue1
));
385 public void testNonEqualValues() {
386 final TmfEventField copy1
= new TmfEventField(fFieldName1
, fValue1
);
387 TmfEventField copy2
= new TmfEventField(fFieldName1
, fValue1
);
388 assertTrue("equals", copy1
.equals(copy2
));
389 assertTrue("equals", copy2
.equals(copy1
));
391 copy2
= new TmfEventField(fFieldName1
, fValue2
);
392 assertFalse("equals", copy1
.equals(copy2
));
393 assertFalse("equals", copy2
.equals(copy1
));
395 copy2
= new TmfEventField(fFieldName1
, null);
396 assertFalse("equals", copy1
.equals(copy2
));
397 assertFalse("equals", copy2
.equals(copy1
));
403 public void testNonEquals() {
404 assertFalse("equals", fField1
.equals(fField2
));
405 assertFalse("equals", fField2
.equals(fField1
));
407 assertFalse("equals", fField1
.equals(fStructTerminalField1
));
410 // ------------------------------------------------------------------------
412 // ------------------------------------------------------------------------
417 public void testToString() {
418 final String expected1
= fFieldName1
+ "=" + fValue1
.toString();
419 TmfEventField field
= new TmfEventField(fFieldName1
, fValue1
, null);
420 assertEquals("toString", expected1
, field
.toString());
422 final String expected2
= fFieldName1
+ "=" + fValue2
.toString();
423 field
= new TmfEventField(fFieldName1
, fValue2
, null);
424 assertEquals("toString", expected2
, field
.toString());
427 // ------------------------------------------------------------------------
429 // ------------------------------------------------------------------------
434 public void testMakeRoot() {
435 ITmfEventField root
= TmfEventField
.makeRoot(fStructFieldNames
);
436 String
[] names
= root
.getFieldNames();
437 assertEquals("getFieldNames length", 2, names
.length
);
438 for (int i
= 0; i
< names
.length
; i
++) {
439 assertSame("getFieldNames", fStructFieldNames
[i
], names
[i
]);
440 assertSame("getFieldName", fStructFieldNames
[i
], root
.getFieldName(i
));
441 assertNull("getValue", root
.getField(i
).getValue());
443 assertNull("getFieldName", root
.getFieldName(-1));
444 assertNull("getFieldName", root
.getFieldName(names
.length
));
446 root
= TmfEventField
.makeRoot(fFieldNames
);
447 names
= root
.getFieldNames();
448 assertEquals("getFieldNames length", 2, names
.length
);
449 for (int i
= 0; i
< names
.length
; i
++) {
450 assertSame("getFieldNames", fFieldNames
[i
], names
[i
]);
451 assertSame("getFieldName", fFieldNames
[i
], root
.getFieldName(i
));
452 assertNull("getValue", root
.getField(i
).getValue());
454 assertNull("getFieldName", root
.getFieldName(-1));
455 assertNull("getFieldName", root
.getFieldName(names
.length
));