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 // ------------------------------------------------------------------------
79 public void testTerminalStructConstructor() {
80 assertSame("getName", fFieldName1
, fStructTerminalField1
.getName());
81 assertNull("getValue", fStructTerminalField1
.getValue());
82 assertEquals("getFields", 0, fStructTerminalField1
.getFields().length
);
83 assertNull("getField(name)", fStructTerminalField1
.getField(fFieldName1
));
84 assertNull("getField(index)", fStructTerminalField1
.getField(0));
85 assertEquals("getFieldNames", 0, fStructTerminalField1
.getFieldNames().length
);
86 assertNull("getFieldName", fStructTerminalField1
.getFieldName(-1));
87 assertNull("getFieldName", fStructTerminalField1
.getFieldName(0));
90 public void testNonTerminalStructConstructor() {
91 assertSame("getName", fStructRootFieldName
, fStructRootField
.getName());
92 assertNull("getValue", fStructRootField
.getValue());
93 assertEquals("getFields", 2, fStructRootField
.getFields().length
);
94 assertSame("getField(name)", fStructTerminalField1
, fStructRootField
.getField(fFieldName1
));
95 assertSame("getField(name)", fStructTerminalField2
, fStructRootField
.getField(fFieldName2
));
96 assertSame("getField(index)", fStructTerminalField1
, fStructRootField
.getField(0));
97 assertSame("getField(index)", fStructTerminalField2
, fStructRootField
.getField(1));
99 final String
[] names
= fStructRootField
.getFieldNames();
100 assertEquals("getFieldNames length", 2, names
.length
);
101 for (int i
= 0; i
< names
.length
; i
++) {
102 assertSame("getFieldNames", fStructFieldNames
[i
], names
[i
]);
103 assertSame("getFieldName", fFieldNames
[i
], fStructRootField
.getFieldName(i
));
105 assertNull("getFieldName", fStructRootField
.getFieldName(-1));
106 assertNull("getFieldName", fStructRootField
.getFieldName(names
.length
));
109 public void testTerminalConstructor() {
110 assertSame("getName", fFieldName1
, fField1
.getName());
111 assertSame("getValue", fValue1
, fField1
.getValue());
112 assertEquals("getFields", 0, fField1
.getFields().length
);
113 assertNull("getField(name)", fField1
.getField(fFieldName1
));
114 assertNull("getField(index)", fField1
.getField(0));
115 assertEquals("getFieldNames", 0, fField1
.getFieldNames().length
);
116 assertNull("getFieldName", fField1
.getFieldName(0));
118 assertSame("getName", fFieldName2
, fField2
.getName());
119 assertSame("getValue", fValue2
, fField2
.getValue());
120 assertEquals("getFields", 0, fField2
.getFields().length
);
121 assertNull("getField(name)", fField2
.getField(fFieldName2
));
122 assertNull("getField(index)", fField2
.getField(0));
123 assertEquals("getFieldNames", 0, fField2
.getFieldNames().length
);
124 assertNull("getFieldName", fField2
.getFieldName(0));
127 public void testNonTerminalConstructor() {
128 assertSame("getName", fRootFieldName
, fRootField
.getName());
129 assertNull("getValue", fRootField
.getValue());
130 assertEquals("getFields", 2, fRootField
.getFields().length
);
131 assertSame("getField(name)", fField1
, fRootField
.getField(fFieldName1
));
132 assertSame("getField(name)", fField2
, fRootField
.getField(fFieldName2
));
133 assertSame("getField(index)", fField1
, fRootField
.getField(0));
134 assertSame("getField(index)", fField2
, fRootField
.getField(1));
136 final String
[] names
= fRootField
.getFieldNames();
137 assertEquals("getFieldNames length", 2, names
.length
);
138 for (int i
= 0; i
< names
.length
; i
++) {
139 assertSame("getFieldNames", fFieldNames
[i
], names
[i
]);
140 assertSame("getFieldName", fFieldNames
[i
], fRootField
.getFieldName(i
));
142 assertNull("getFieldName", fRootField
.getFieldName(-1));
143 assertNull("getFieldName", fRootField
.getFieldName(names
.length
));
146 public void testConstructorBadArg() {
148 new TmfEventField(null, fValue1
, null);
149 fail("Invalid (null) field name");
150 } catch (final IllegalArgumentException e
) {
154 public void testTerminalCopyConstructor() {
155 final TmfEventField copy
= new TmfEventField(fField1
);
156 assertSame("getName", fFieldName1
, copy
.getName());
157 assertSame("getValue", fValue1
, copy
.getValue());
158 assertEquals("getFields", 0, copy
.getFields().length
);
159 assertNull("getField(name)", copy
.getField(fFieldName1
));
160 assertNull("getField(index)", copy
.getField(0));
161 assertEquals("getFieldNames", 0, copy
.getFieldNames().length
);
162 assertNull("getFieldName", copy
.getFieldName(0));
165 public void testNonTerminalCopyConstructor() {
166 assertSame("getName", fRootFieldName
, fRootField
.getName());
167 assertNull("getValue", fRootField
.getValue());
168 assertEquals("getFields", 2, fRootField
.getFields().length
);
169 assertSame("getField(name)", fField1
, fRootField
.getField(fFieldName1
));
170 assertSame("getField(name)", fField2
, fRootField
.getField(fFieldName2
));
171 assertSame("getField(index)", fField1
, fRootField
.getField(0));
172 assertSame("getField(index)", fField2
, fRootField
.getField(1));
174 final String
[] names
= fRootField
.getFieldNames();
175 assertEquals("getFieldNames length", 2, names
.length
);
176 for (int i
= 0; i
< names
.length
; i
++) {
177 assertSame("getFieldNames", fFieldNames
[i
], names
[i
]);
178 assertSame("getFieldName", fFieldNames
[i
], fRootField
.getFieldName(i
));
180 assertNull("getFieldName", fRootField
.getFieldName(names
.length
));
183 public void testCopyConstructorBadArg() {
185 new TmfEventField(null);
186 fail("TmfEventField: null arguemnt");
187 } catch (final IllegalArgumentException e
) {
191 // ------------------------------------------------------------------------
193 // ------------------------------------------------------------------------
195 private static class MyField
extends TmfEventField
{
197 public MyField(final String id
, final Object value
) {
201 public MyField(final TmfEventField field
) {
206 public void setValue(final Object value
, final ITmfEventField
[] subfields
) {
207 super.setValue(value
, subfields
);
211 public void testSetValue() {
212 final TmfEventField field
= new TmfEventField(fFieldName1
, fValue1
, null);
214 final MyField myField
= new MyField(field
);
215 assertSame("getValue", fValue1
, myField
.getValue());
216 myField
.setValue(fValue2
, null);
217 assertSame("getValue", fValue2
, myField
.getValue());
218 myField
.setValue(fValue2
, new TmfEventField
[] { field
});
219 assertSame("getValue", fValue2
, myField
.getValue());
222 // ------------------------------------------------------------------------
224 // ------------------------------------------------------------------------
226 public void testFieldClone() throws Exception
{
227 TmfEventField clone
= fField1
.clone();
228 assertTrue("clone", fField1
.clone().equals(fField1
));
229 assertTrue("clone", clone
.clone().equals(clone
));
230 assertEquals("clone", fField1
, clone
);
231 assertEquals("clone", clone
, fField1
);
233 clone
= fRootField
.clone();
234 assertTrue("clone", fRootField
.clone().equals(fRootField
));
235 assertTrue("clone", clone
.clone().equals(clone
));
236 assertEquals("clone", fRootField
, clone
);
237 assertEquals("clone", clone
, fRootField
);
240 public void testStructFieldClone() throws Exception
{
241 TmfEventField clone
= fStructTerminalField1
.clone();
242 assertTrue("clone", fStructTerminalField1
.clone().equals(fStructTerminalField1
));
243 assertTrue("clone", clone
.clone().equals(clone
));
244 assertEquals("clone", fStructTerminalField1
, clone
);
245 assertEquals("clone", clone
, fStructTerminalField1
);
247 clone
= fStructRootField
.clone();
248 assertTrue("clone", fStructRootField
.clone().equals(fStructRootField
));
249 assertTrue("clone", clone
.clone().equals(clone
));
250 assertEquals("clone", fStructRootField
, clone
);
251 assertEquals("clone", clone
, fStructRootField
);
254 // ------------------------------------------------------------------------
256 // ------------------------------------------------------------------------
258 public void testHashCode() throws Exception
{
259 TmfEventField copy
= new TmfEventField(fField1
);
260 assertTrue("hashCode", fField1
.hashCode() == copy
.hashCode());
261 assertTrue("hashCode", fField1
.hashCode() != fField2
.hashCode());
263 copy
= new TmfEventField(fStructTerminalField1
);
264 assertTrue("hashCode", fStructTerminalField1
.hashCode() == copy
.hashCode());
265 assertTrue("hashCode", fStructTerminalField1
.hashCode() != fStructTerminalField2
.hashCode());
268 // ------------------------------------------------------------------------
270 // ------------------------------------------------------------------------
272 public void testEqualsReflexivity() throws Exception
{
273 assertTrue("equals", fField1
.equals(fField1
));
274 assertTrue("equals", fField2
.equals(fField2
));
276 assertFalse("equals", fField1
.equals(fField2
));
277 assertFalse("equals", fField2
.equals(fField1
));
279 assertTrue("equals", fStructTerminalField1
.equals(fStructTerminalField1
));
280 assertTrue("equals", fStructTerminalField2
.equals(fStructTerminalField2
));
282 assertFalse("equals", fStructTerminalField1
.equals(fStructTerminalField2
));
283 assertFalse("equals", fStructTerminalField2
.equals(fStructTerminalField1
));
286 public void testEqualsSymmetry() throws Exception
{
287 final TmfEventField copy0
= new TmfEventField(fField1
);
288 assertTrue("equals", fField1
.equals(copy0
));
289 assertTrue("equals", copy0
.equals(fField1
));
291 final TmfEventField copy3
= new TmfEventField(fField2
);
292 assertTrue("equals", fField2
.equals(copy3
));
293 assertTrue("equals", copy3
.equals(fField2
));
296 public void testEqualsTransivity() throws Exception
{
297 TmfEventField copy1
= new TmfEventField(fField1
);
298 TmfEventField copy2
= new TmfEventField(copy1
);
299 assertTrue("equals", fField1
.equals(copy1
));
300 assertTrue("equals", copy1
.equals(copy2
));
301 assertTrue("equals", fField1
.equals(copy2
));
303 copy1
= new TmfEventField(fField2
);
304 copy2
= new TmfEventField(copy1
);
305 assertTrue("equals", fField2
.equals(copy1
));
306 assertTrue("equals", copy1
.equals(copy2
));
307 assertTrue("equals", fField2
.equals(copy2
));
310 public void testEquals() throws Exception
{
311 assertTrue("equals", fStructTerminalField1
.equals(fStructTerminalField3
));
312 assertTrue("equals", fStructTerminalField3
.equals(fStructTerminalField1
));
314 assertFalse("equals", fStructTerminalField1
.equals(fField3
));
315 assertFalse("equals", fField3
.equals(fStructTerminalField1
));
318 public void testEqualsNull() throws Exception
{
319 assertFalse("equals", fField1
.equals(null));
320 assertFalse("equals", fField2
.equals(null));
323 public void testNonEqualClasses() throws Exception
{
324 assertFalse("equals", fField1
.equals(fStructTerminalField1
));
325 assertFalse("equals", fField1
.equals(fValue1
));
328 public void testNonEqualValues() throws Exception
{
329 final TmfEventField copy1
= new TmfEventField(fFieldName1
, fValue1
);
330 TmfEventField copy2
= new TmfEventField(fFieldName1
, fValue1
);
331 assertTrue("equals", copy1
.equals(copy2
));
332 assertTrue("equals", copy2
.equals(copy1
));
334 copy2
= new TmfEventField(fFieldName1
, fValue2
);
335 assertFalse("equals", copy1
.equals(copy2
));
336 assertFalse("equals", copy2
.equals(copy1
));
338 copy2
= new TmfEventField(fFieldName1
, null);
339 assertFalse("equals", copy1
.equals(copy2
));
340 assertFalse("equals", copy2
.equals(copy1
));
343 public void testNonEquals() throws Exception
{
344 assertFalse("equals", fField1
.equals(fField2
));
345 assertFalse("equals", fField2
.equals(fField1
));
347 assertFalse("equals", fField1
.equals(fStructTerminalField1
));
350 // ------------------------------------------------------------------------
352 // ------------------------------------------------------------------------
354 public void testToString() {
355 final String expected1
= "TmfEventField [fFieldId=" + fFieldName1
+ ", fValue=" + fValue1
.toString() + "]";
356 TmfEventField field
= new TmfEventField(fFieldName1
, fValue1
, null);
357 assertEquals("toString", expected1
, field
.toString());
359 final String expected2
= "TmfEventField [fFieldId=" + fFieldName1
+ ", fValue=" + fValue2
.toString() + "]";
360 field
= new TmfEventField(fFieldName1
, fValue2
, null);
361 assertEquals("toString", expected2
, field
.toString());
364 // ------------------------------------------------------------------------
366 // ------------------------------------------------------------------------
368 public void testMakeRoot() {
369 ITmfEventField root
= TmfEventField
.makeRoot(fStructFieldNames
);
370 String
[] names
= root
.getFieldNames();
371 assertEquals("getFieldNames length", 2, names
.length
);
372 for (int i
= 0; i
< names
.length
; i
++) {
373 assertSame("getFieldNames", fStructFieldNames
[i
], names
[i
]);
374 assertSame("getFieldName", fStructFieldNames
[i
], root
.getFieldName(i
));
375 assertNull("getValue", root
.getField(i
).getValue());
377 assertNull("getFieldName", root
.getFieldName(-1));
378 assertNull("getFieldName", root
.getFieldName(names
.length
));
380 root
= TmfEventField
.makeRoot(fFieldNames
);
381 names
= root
.getFieldNames();
382 assertEquals("getFieldNames length", 2, names
.length
);
383 for (int i
= 0; i
< names
.length
; i
++) {
384 assertSame("getFieldNames", fFieldNames
[i
], names
[i
]);
385 assertSame("getFieldName", fFieldNames
[i
], root
.getFieldName(i
));
386 assertNull("getValue", root
.getField(i
).getValue());
388 assertNull("getFieldName", root
.getFieldName(-1));
389 assertNull("getFieldName", root
.getFieldName(names
.length
));