1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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 * Alexandre Montplaisir - Port to JUnit4
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.event
;
17 import static org
.junit
.Assert
.assertArrayEquals
;
18 import static org
.junit
.Assert
.assertEquals
;
19 import static org
.junit
.Assert
.assertFalse
;
20 import static org
.junit
.Assert
.assertNotEquals
;
21 import static org
.junit
.Assert
.assertNull
;
22 import static org
.junit
.Assert
.assertSame
;
23 import static org
.junit
.Assert
.assertTrue
;
24 import static org
.junit
.Assert
.fail
;
26 import java
.util
.Collection
;
28 import org
.eclipse
.jdt
.annotation
.NonNull
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
33 import org
.junit
.Test
;
36 * Test suite for the TmfEventField class.
38 @SuppressWarnings("javadoc")
39 public class TmfEventFieldTest
{
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
45 private final @NonNull String fFieldName1
= "Field-1";
46 private final @NonNull String fFieldName2
= "Field-2";
48 private final Object fValue1
= "Value";
49 private final Object fValue2
= Integer
.valueOf(10);
51 private final TmfEventField fField1
= new TmfEventField(fFieldName1
, fValue1
, null);
52 private final TmfEventField fField2
= new TmfEventField(fFieldName2
, fValue2
, null);
53 private final TmfEventField fField3
= new TmfEventField(fFieldName1
, fValue2
, null);
55 private final @NonNull String fStructRootFieldName
= "Root-S";
56 private final String
[] fStructFieldNames
= new String
[] { fFieldName1
, fFieldName2
};
57 private final TmfEventField fStructTerminalField1
= new TmfEventField(fFieldName1
, null, null);
58 private final TmfEventField fStructTerminalField2
= new TmfEventField(fFieldName2
, null, null);
59 private final TmfEventField fStructTerminalField3
= new TmfEventField(fFieldName1
, null, null);
60 private final TmfEventField fStructRootField
= new TmfEventField(fStructRootFieldName
, null,
61 new ITmfEventField
[] { fStructTerminalField1
, fStructTerminalField2
});
63 private final @NonNull String fRootFieldName
= "Root";
64 private final String
[] fFieldNames
= new String
[] { fFieldName1
, fFieldName2
};
65 private final TmfEventField fRootField
= new TmfEventField(fRootFieldName
, null,
66 new ITmfEventField
[] { fField1
, fField2
});
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
73 public void testTerminalStructConstructor() {
74 assertSame("getName", fFieldName1
, fStructTerminalField1
.getName());
75 assertNull("getValue", fStructTerminalField1
.getValue());
76 assertEquals("getFields", 0, fStructTerminalField1
.getFields().size());
77 assertNull("getField(name)", fStructTerminalField1
.getField(fFieldName1
));
78 assertEquals("getFieldNames", 0, fStructTerminalField1
.getFieldNames().size());
82 public void testNonTerminalStructConstructor() {
83 assertSame("getName", fStructRootFieldName
, fStructRootField
.getName());
84 assertNull("getValue", fStructRootField
.getValue());
85 assertEquals("getFields", 2, fStructRootField
.getFields().size());
86 assertSame("getField(name)", fStructTerminalField1
, fStructRootField
.getField(fFieldName1
));
87 assertSame("getField(name)", fStructTerminalField2
, fStructRootField
.getField(fFieldName2
));
89 final Collection
<String
> names
= fStructRootField
.getFieldNames();
90 assertEquals("getFieldNames length", 2, names
.size());
91 assertArrayEquals(fStructFieldNames
, names
.toArray(new String
[names
.size()]));
95 public void testTerminalConstructor() {
96 assertSame("getName", fFieldName1
, fField1
.getName());
97 assertSame("getValue", fValue1
, fField1
.getValue());
98 assertEquals("getFields", 0, fField1
.getFields().size());
99 assertNull("getField(name)", fField1
.getField(fFieldName1
));
100 assertEquals("getFieldNames", 0, fField1
.getFieldNames().size());
102 assertSame("getName", fFieldName2
, fField2
.getName());
103 assertSame("getValue", fValue2
, fField2
.getValue());
104 assertEquals("getFields", 0, fField2
.getFields().size());
105 assertNull("getField(name)", fField2
.getField(fFieldName2
));
109 public void testNonTerminalConstructor() {
110 assertSame("getName", fRootFieldName
, fRootField
.getName());
111 assertNull("getValue", fRootField
.getValue());
112 assertEquals("getFields", 2, fRootField
.getFields().size());
113 assertSame("getField(name)", fField1
, fRootField
.getField(fFieldName1
));
114 assertSame("getField(name)", fField2
, fRootField
.getField(fFieldName2
));
116 final Collection
<String
> names
= fRootField
.getFieldNames();
117 assertEquals("getFieldNames length", 2, names
.size());
118 assertArrayEquals(fFieldNames
, names
.toArray(new String
[names
.size()]));
122 public void testTerminalCopyConstructor() {
123 final TmfEventField copy
= new TmfEventField(fField1
);
124 assertSame("getName", fFieldName1
, copy
.getName());
125 assertSame("getValue", fValue1
, copy
.getValue());
126 assertEquals("getFields", 0, copy
.getFields().size());
127 assertNull("getField(name)", copy
.getField(fFieldName1
));
128 assertEquals("getFieldNames", 0, copy
.getFieldNames().size());
132 public void testNonTerminalCopyConstructor() {
133 assertSame("getName", fRootFieldName
, fRootField
.getName());
134 assertNull("getValue", fRootField
.getValue());
135 assertEquals("getFields", 2, fRootField
.getFields().size());
136 assertSame("getField(name)", fField1
, fRootField
.getField(fFieldName1
));
137 assertSame("getField(name)", fField2
, fRootField
.getField(fFieldName2
));
139 final Collection
<String
> names
= fRootField
.getFieldNames();
140 assertEquals("getFieldNames length", 2, names
.size());
141 assertArrayEquals(fFieldNames
, names
.toArray(new String
[names
.size()]));
145 public void testCopyConstructorBadArg() {
147 new TmfEventField(null);
148 fail("TmfEventField: null arguemnt");
149 } catch (final IllegalArgumentException e
) {
154 * Test that we correctly fail to create a field with subfields having the
158 public void testDuplicateFieldNames() {
159 ITmfEventField
[] fields
= {
160 new TmfEventField("samename", null, null),
161 new TmfEventField("samename", null, null)
165 new TmfEventField("field", null, fields
);
166 fail("TmfEventField: Duplicate field names");
167 } catch (IllegalArgumentException e
) {
168 /* Expected exception */
172 // ------------------------------------------------------------------------
174 // ------------------------------------------------------------------------
177 public void testHashCode() {
178 TmfEventField copy
= new TmfEventField(fField1
);
179 assertTrue("hashCode", fField1
.hashCode() == copy
.hashCode());
180 assertTrue("hashCode", fField1
.hashCode() != fField2
.hashCode());
182 copy
= new TmfEventField(fStructTerminalField1
);
183 assertTrue("hashCode", fStructTerminalField1
.hashCode() == copy
.hashCode());
184 assertTrue("hashCode", fStructTerminalField1
.hashCode() != fStructTerminalField2
.hashCode());
187 // ------------------------------------------------------------------------
189 // ------------------------------------------------------------------------
192 public void testEqualsReflexivity() {
193 assertTrue("equals", fField1
.equals(fField1
));
194 assertTrue("equals", fField2
.equals(fField2
));
196 assertFalse("equals", fField1
.equals(fField2
));
197 assertFalse("equals", fField2
.equals(fField1
));
199 assertTrue("equals", fStructTerminalField1
.equals(fStructTerminalField1
));
200 assertTrue("equals", fStructTerminalField2
.equals(fStructTerminalField2
));
202 assertFalse("equals", fStructTerminalField1
.equals(fStructTerminalField2
));
203 assertFalse("equals", fStructTerminalField2
.equals(fStructTerminalField1
));
207 public void testEqualsSymmetry() {
208 final TmfEventField copy0
= new TmfEventField(fField1
);
209 assertTrue("equals", fField1
.equals(copy0
));
210 assertTrue("equals", copy0
.equals(fField1
));
212 final TmfEventField copy3
= new TmfEventField(fField2
);
213 assertTrue("equals", fField2
.equals(copy3
));
214 assertTrue("equals", copy3
.equals(fField2
));
218 public void testEqualsTransivity() {
219 TmfEventField copy1
= new TmfEventField(fField1
);
220 TmfEventField copy2
= new TmfEventField(copy1
);
221 assertTrue("equals", fField1
.equals(copy1
));
222 assertTrue("equals", copy1
.equals(copy2
));
223 assertTrue("equals", fField1
.equals(copy2
));
225 copy1
= new TmfEventField(fField2
);
226 copy2
= new TmfEventField(copy1
);
227 assertTrue("equals", fField2
.equals(copy1
));
228 assertTrue("equals", copy1
.equals(copy2
));
229 assertTrue("equals", fField2
.equals(copy2
));
233 public void testEquals() {
234 assertTrue("equals", fStructTerminalField1
.equals(fStructTerminalField3
));
235 assertTrue("equals", fStructTerminalField3
.equals(fStructTerminalField1
));
237 assertFalse("equals", fStructTerminalField1
.equals(fField3
));
238 assertFalse("equals", fField3
.equals(fStructTerminalField1
));
242 public void testEqualsNull() {
243 assertFalse("equals", fField1
.equals(null));
244 assertFalse("equals", fField2
.equals(null));
248 public void testNonEqualClasses() {
249 assertFalse("equals", fField1
.equals(fStructTerminalField1
));
250 assertFalse("equals", fField1
.equals(fValue1
));
254 public void testNonEqualValues() {
255 final TmfEventField copy1
= new TmfEventField(fFieldName1
, fValue1
, null);
256 TmfEventField copy2
= new TmfEventField(fFieldName1
, fValue1
, null);
257 assertTrue("equals", copy1
.equals(copy2
));
258 assertTrue("equals", copy2
.equals(copy1
));
260 copy2
= new TmfEventField(fFieldName1
, fValue2
, null);
261 assertFalse("equals", copy1
.equals(copy2
));
262 assertFalse("equals", copy2
.equals(copy1
));
264 copy2
= new TmfEventField(fFieldName1
, null, null);
265 assertFalse("equals", copy1
.equals(copy2
));
266 assertFalse("equals", copy2
.equals(copy1
));
270 public void testNonEquals() {
271 assertFalse("equals", fField1
.equals(fField2
));
272 assertFalse("equals", fField2
.equals(fField1
));
274 assertFalse("equals", fField1
.equals(fStructTerminalField1
));
278 * Test with same fields, but different values (should not be equal)
281 public void testNonEqualsValue() {
282 final String fieldName
= "myfield";
283 final Object value1
= new String("test-string");
284 final Object value2
= new TmfEvent(null, ITmfContext
.UNKNOWN_RANK
, null, null, null);
285 final TmfEventField
[] fields
= { fField1
, fField2
};
287 final TmfEventField field1
= new TmfEventField(fieldName
, value1
, fields
);
288 final TmfEventField field2
= new TmfEventField(fieldName
, value2
, fields
);
290 assertNotEquals(field1
, field2
);
291 assertNotEquals(field2
, field1
);
295 * Test with same value, but different fields (should not be equal)
298 public void testNonEqualsFields() {
299 final String fieldName
= "myfield";
300 final Object value
= new String("test-string");
301 final TmfEventField
[] fields1
= { fField1
, fField2
};
302 final TmfEventField
[] fields2
= { fField2
, fField3
};
304 final TmfEventField field1
= new TmfEventField(fieldName
, value
, fields1
);
305 final TmfEventField field2
= new TmfEventField(fieldName
, value
, fields2
);
307 assertNotEquals(field1
, field2
);
308 assertNotEquals(field2
, field1
);
312 * Test with same field and values (should be equals)
315 public void testEqualsEverything() {
316 final String fieldName
= "myfield";
317 final Object value
= new String("test-string");
318 final TmfEventField
[] fields
= { fField1
, fField2
};
320 final TmfEventField field1
= new TmfEventField(fieldName
, value
, fields
);
321 final TmfEventField field2
= new TmfEventField(fieldName
, value
, fields
);
323 assertEquals(field1
, field2
);
324 assertEquals(field2
, field1
);
327 // ------------------------------------------------------------------------
329 // ------------------------------------------------------------------------
332 public void testToString() {
333 final String expected1
= fFieldName1
+ "=" + fValue1
.toString();
334 TmfEventField field
= new TmfEventField(fFieldName1
, fValue1
, null);
335 assertEquals("toString", expected1
, field
.toString());
337 final String expected2
= fFieldName1
+ "=" + fValue2
.toString();
338 field
= new TmfEventField(fFieldName1
, fValue2
, null);
339 assertEquals("toString", expected2
, field
.toString());
342 // ------------------------------------------------------------------------
344 // ------------------------------------------------------------------------
347 public void testMakeRoot() {
348 ITmfEventField root
= TmfEventField
.makeRoot(fStructFieldNames
);
349 Collection
<String
> names
= root
.getFieldNames();
350 assertEquals("getFieldNames length", 2, names
.size());
351 assertArrayEquals(fStructFieldNames
, names
.toArray(new String
[names
.size()]));
353 root
= TmfEventField
.makeRoot(fFieldNames
);
354 names
= root
.getFieldNames();
355 assertEquals("getFieldNames length", 2, names
.size());
356 assertArrayEquals(fFieldNames
, names
.toArray(new String
[names
.size()]));