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 * <b><u>TmfEventFieldTest</u></b>
24 * Test suite for the TmfEventField class.
26 @SuppressWarnings("nls")
27 public class TmfEventFieldTest
extends TestCase
{
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
33 private final String fFieldName1
= "Field-1";
34 private final String fFieldName2
= "Field-2";
36 private final Object fValue1
= "Value";
37 private final Object fValue2
= Integer
.valueOf(10);
39 private TmfEventField fField1
= new TmfEventField(fFieldName1
, fValue1
);
40 private TmfEventField fField2
= new TmfEventField(fFieldName2
, fValue2
, null);
41 private TmfEventField fField3
= new TmfEventField(fFieldName1
, fValue2
, null);
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
});
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
});
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
61 * @param name the test name
63 public TmfEventFieldTest(String name
) {
68 protected void setUp() throws Exception
{
73 protected void tearDown() throws Exception
{
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
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));
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));
101 String
[] names
= fStructRootField
.getFieldNames();
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
));
107 assertNull("getFieldName", fStructRootField
.getFieldName(-1));
108 assertNull("getFieldName", fStructRootField
.getFieldName(names
.length
));
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));
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));
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));
138 String
[] names
= fRootField
.getFieldNames();
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
));
144 assertNull("getFieldName", fRootField
.getFieldName(-1));
145 assertNull("getFieldName", fRootField
.getFieldName(names
.length
));
148 public void testConstructorBadArg() {
150 new TmfEventField(null, fValue1
, null);
151 fail("Invalid (null) field name");
152 } catch (IllegalArgumentException e
) {
156 public void testTerminalCopyConstructor() {
157 TmfEventField copy
= new TmfEventField(fField1
);
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));
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));
176 String
[] names
= fRootField
.getFieldNames();
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
));
182 assertNull("getFieldName", fRootField
.getFieldName(names
.length
));
185 public void testCopyConstructorBadArg() {
187 new TmfEventField(null);
188 fail("TmfEventField: null arguemnt");
189 } catch (IllegalArgumentException e
) {
193 // ------------------------------------------------------------------------
195 // ------------------------------------------------------------------------
197 private static class MyField
extends TmfEventField
{
199 public MyField(String id
, Object value
) {
203 public MyField(TmfEventField field
) {
208 public void setValue(Object value
, ITmfEventField
[] subfields
) {
209 super.setValue(value
, subfields
);
213 public void testSetValue() {
214 TmfEventField field
= new TmfEventField(fFieldName1
, fValue1
, null);
216 MyField myField
= new MyField(field
);
217 assertSame("getValue", fValue1
, myField
.getValue());
218 myField
.setValue(fValue2
, null);
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 TmfEventField copy0
= new TmfEventField(fField1
);
288 assertTrue("equals", fField1
.equals(copy0
));
289 assertTrue("equals", copy0
.equals(fField1
));
291 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 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 String expected1
= "TmfEventField [fFieldId=" + fFieldName1
+ ", fValue=" + fValue1
.toString() + "]";
356 TmfEventField field
= new TmfEventField(fFieldName1
, fValue1
, null);
357 assertEquals("toString", expected1
, field
.toString());
359 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
));