1 /*******************************************************************************
2 * Copyright (c) 2009, 2012, 2013 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
.linuxtools
.tmf
.core
.tests
.event
;
17 import static org
.junit
.Assert
.assertEquals
;
18 import static org
.junit
.Assert
.assertFalse
;
19 import static org
.junit
.Assert
.assertNull
;
20 import static org
.junit
.Assert
.assertSame
;
21 import static org
.junit
.Assert
.assertTrue
;
22 import static org
.junit
.Assert
.fail
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
26 import org
.junit
.Test
;
29 * Test suite for the TmfEventField class.
31 @SuppressWarnings({"nls", "javadoc"})
32 public class TmfEventFieldTest
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 private final String fFieldName1
= "Field-1";
39 private final String fFieldName2
= "Field-2";
41 private final Object fValue1
= "Value";
42 private final Object fValue2
= Integer
.valueOf(10);
44 private final TmfEventField fField1
= new TmfEventField(fFieldName1
, fValue1
);
45 private final TmfEventField fField2
= new TmfEventField(fFieldName2
, fValue2
, null);
46 private final TmfEventField fField3
= new TmfEventField(fFieldName1
, fValue2
, null);
48 private final String fStructRootFieldName
= "Root-S";
49 private final String
[] fStructFieldNames
= new String
[] { fFieldName1
, fFieldName2
};
50 private final TmfEventField fStructTerminalField1
= new TmfEventField(fFieldName1
, null);
51 private final TmfEventField fStructTerminalField2
= new TmfEventField(fFieldName2
, null);
52 private final TmfEventField fStructTerminalField3
= new TmfEventField(fFieldName1
, null);
53 private final TmfEventField fStructRootField
= new TmfEventField(fStructRootFieldName
,
54 new ITmfEventField
[] { fStructTerminalField1
, fStructTerminalField2
});
56 private final String fRootFieldName
= "Root";
57 private final String
[] fFieldNames
= new String
[] { fFieldName1
, fFieldName2
};
58 private final TmfEventField fRootField
= new TmfEventField(fRootFieldName
,
59 new ITmfEventField
[] { fField1
, fField2
});
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
66 public void testTerminalStructConstructor() {
67 assertSame("getName", fFieldName1
, fStructTerminalField1
.getName());
68 assertNull("getValue", fStructTerminalField1
.getValue());
69 assertEquals("getFields", 0, fStructTerminalField1
.getFields().length
);
70 assertNull("getField(name)", fStructTerminalField1
.getField(fFieldName1
));
71 assertNull("getField(index)", fStructTerminalField1
.getField(0));
72 assertEquals("getFieldNames", 0, fStructTerminalField1
.getFieldNames().length
);
73 assertNull("getFieldName", fStructTerminalField1
.getFieldName(-1));
74 assertNull("getFieldName", fStructTerminalField1
.getFieldName(0));
78 public void testNonTerminalStructConstructor() {
79 assertSame("getName", fStructRootFieldName
, fStructRootField
.getName());
80 assertNull("getValue", fStructRootField
.getValue());
81 assertEquals("getFields", 2, fStructRootField
.getFields().length
);
82 assertSame("getField(name)", fStructTerminalField1
, fStructRootField
.getField(fFieldName1
));
83 assertSame("getField(name)", fStructTerminalField2
, fStructRootField
.getField(fFieldName2
));
84 assertSame("getField(index)", fStructTerminalField1
, fStructRootField
.getField(0));
85 assertSame("getField(index)", fStructTerminalField2
, fStructRootField
.getField(1));
87 final String
[] names
= fStructRootField
.getFieldNames();
88 assertEquals("getFieldNames length", 2, names
.length
);
89 for (int i
= 0; i
< names
.length
; i
++) {
90 assertSame("getFieldNames", fStructFieldNames
[i
], names
[i
]);
91 assertSame("getFieldName", fFieldNames
[i
], fStructRootField
.getFieldName(i
));
93 assertNull("getFieldName", fStructRootField
.getFieldName(-1));
94 assertNull("getFieldName", fStructRootField
.getFieldName(names
.length
));
98 public void testTerminalConstructor() {
99 assertSame("getName", fFieldName1
, fField1
.getName());
100 assertSame("getValue", fValue1
, fField1
.getValue());
101 assertEquals("getFields", 0, fField1
.getFields().length
);
102 assertNull("getField(name)", fField1
.getField(fFieldName1
));
103 assertNull("getField(index)", fField1
.getField(0));
104 assertEquals("getFieldNames", 0, fField1
.getFieldNames().length
);
105 assertNull("getFieldName", fField1
.getFieldName(0));
107 assertSame("getName", fFieldName2
, fField2
.getName());
108 assertSame("getValue", fValue2
, fField2
.getValue());
109 assertEquals("getFields", 0, fField2
.getFields().length
);
110 assertNull("getField(name)", fField2
.getField(fFieldName2
));
111 assertNull("getField(index)", fField2
.getField(0));
112 assertEquals("getFieldNames", 0, fField2
.getFieldNames().length
);
113 assertNull("getFieldName", fField2
.getFieldName(0));
117 public void testNonTerminalConstructor() {
118 assertSame("getName", fRootFieldName
, fRootField
.getName());
119 assertNull("getValue", fRootField
.getValue());
120 assertEquals("getFields", 2, fRootField
.getFields().length
);
121 assertSame("getField(name)", fField1
, fRootField
.getField(fFieldName1
));
122 assertSame("getField(name)", fField2
, fRootField
.getField(fFieldName2
));
123 assertSame("getField(index)", fField1
, fRootField
.getField(0));
124 assertSame("getField(index)", fField2
, fRootField
.getField(1));
126 final String
[] names
= fRootField
.getFieldNames();
127 assertEquals("getFieldNames length", 2, names
.length
);
128 for (int i
= 0; i
< names
.length
; i
++) {
129 assertSame("getFieldNames", fFieldNames
[i
], names
[i
]);
130 assertSame("getFieldName", fFieldNames
[i
], fRootField
.getFieldName(i
));
132 assertNull("getFieldName", fRootField
.getFieldName(-1));
133 assertNull("getFieldName", fRootField
.getFieldName(names
.length
));
137 public void testConstructorBadArg() {
139 new TmfEventField(null, fValue1
, null);
140 fail("Invalid (null) field name");
141 } catch (final IllegalArgumentException e
) {
146 public void testTerminalCopyConstructor() {
147 final TmfEventField copy
= new TmfEventField(fField1
);
148 assertSame("getName", fFieldName1
, copy
.getName());
149 assertSame("getValue", fValue1
, copy
.getValue());
150 assertEquals("getFields", 0, copy
.getFields().length
);
151 assertNull("getField(name)", copy
.getField(fFieldName1
));
152 assertNull("getField(index)", copy
.getField(0));
153 assertEquals("getFieldNames", 0, copy
.getFieldNames().length
);
154 assertNull("getFieldName", copy
.getFieldName(0));
158 public void testNonTerminalCopyConstructor() {
159 assertSame("getName", fRootFieldName
, fRootField
.getName());
160 assertNull("getValue", fRootField
.getValue());
161 assertEquals("getFields", 2, fRootField
.getFields().length
);
162 assertSame("getField(name)", fField1
, fRootField
.getField(fFieldName1
));
163 assertSame("getField(name)", fField2
, fRootField
.getField(fFieldName2
));
164 assertSame("getField(index)", fField1
, fRootField
.getField(0));
165 assertSame("getField(index)", fField2
, fRootField
.getField(1));
167 final String
[] names
= fRootField
.getFieldNames();
168 assertEquals("getFieldNames length", 2, names
.length
);
169 for (int i
= 0; i
< names
.length
; i
++) {
170 assertSame("getFieldNames", fFieldNames
[i
], names
[i
]);
171 assertSame("getFieldName", fFieldNames
[i
], fRootField
.getFieldName(i
));
173 assertNull("getFieldName", fRootField
.getFieldName(names
.length
));
177 public void testCopyConstructorBadArg() {
179 new TmfEventField(null);
180 fail("TmfEventField: null arguemnt");
181 } catch (final IllegalArgumentException e
) {
185 // ------------------------------------------------------------------------
187 // ------------------------------------------------------------------------
190 public void testHashCode() {
191 TmfEventField copy
= new TmfEventField(fField1
);
192 assertTrue("hashCode", fField1
.hashCode() == copy
.hashCode());
193 assertTrue("hashCode", fField1
.hashCode() != fField2
.hashCode());
195 copy
= new TmfEventField(fStructTerminalField1
);
196 assertTrue("hashCode", fStructTerminalField1
.hashCode() == copy
.hashCode());
197 assertTrue("hashCode", fStructTerminalField1
.hashCode() != fStructTerminalField2
.hashCode());
200 // ------------------------------------------------------------------------
202 // ------------------------------------------------------------------------
205 public void testEqualsReflexivity() {
206 assertTrue("equals", fField1
.equals(fField1
));
207 assertTrue("equals", fField2
.equals(fField2
));
209 assertFalse("equals", fField1
.equals(fField2
));
210 assertFalse("equals", fField2
.equals(fField1
));
212 assertTrue("equals", fStructTerminalField1
.equals(fStructTerminalField1
));
213 assertTrue("equals", fStructTerminalField2
.equals(fStructTerminalField2
));
215 assertFalse("equals", fStructTerminalField1
.equals(fStructTerminalField2
));
216 assertFalse("equals", fStructTerminalField2
.equals(fStructTerminalField1
));
220 public void testEqualsSymmetry() {
221 final TmfEventField copy0
= new TmfEventField(fField1
);
222 assertTrue("equals", fField1
.equals(copy0
));
223 assertTrue("equals", copy0
.equals(fField1
));
225 final TmfEventField copy3
= new TmfEventField(fField2
);
226 assertTrue("equals", fField2
.equals(copy3
));
227 assertTrue("equals", copy3
.equals(fField2
));
231 public void testEqualsTransivity() {
232 TmfEventField copy1
= new TmfEventField(fField1
);
233 TmfEventField copy2
= new TmfEventField(copy1
);
234 assertTrue("equals", fField1
.equals(copy1
));
235 assertTrue("equals", copy1
.equals(copy2
));
236 assertTrue("equals", fField1
.equals(copy2
));
238 copy1
= new TmfEventField(fField2
);
239 copy2
= new TmfEventField(copy1
);
240 assertTrue("equals", fField2
.equals(copy1
));
241 assertTrue("equals", copy1
.equals(copy2
));
242 assertTrue("equals", fField2
.equals(copy2
));
246 public void testEquals() {
247 assertTrue("equals", fStructTerminalField1
.equals(fStructTerminalField3
));
248 assertTrue("equals", fStructTerminalField3
.equals(fStructTerminalField1
));
250 assertFalse("equals", fStructTerminalField1
.equals(fField3
));
251 assertFalse("equals", fField3
.equals(fStructTerminalField1
));
255 public void testEqualsNull() {
256 assertFalse("equals", fField1
.equals(null));
257 assertFalse("equals", fField2
.equals(null));
261 public void testNonEqualClasses() {
262 assertFalse("equals", fField1
.equals(fStructTerminalField1
));
263 assertFalse("equals", fField1
.equals(fValue1
));
267 public void testNonEqualValues() {
268 final TmfEventField copy1
= new TmfEventField(fFieldName1
, fValue1
);
269 TmfEventField copy2
= new TmfEventField(fFieldName1
, fValue1
);
270 assertTrue("equals", copy1
.equals(copy2
));
271 assertTrue("equals", copy2
.equals(copy1
));
273 copy2
= new TmfEventField(fFieldName1
, fValue2
);
274 assertFalse("equals", copy1
.equals(copy2
));
275 assertFalse("equals", copy2
.equals(copy1
));
277 copy2
= new TmfEventField(fFieldName1
, null);
278 assertFalse("equals", copy1
.equals(copy2
));
279 assertFalse("equals", copy2
.equals(copy1
));
283 public void testNonEquals() {
284 assertFalse("equals", fField1
.equals(fField2
));
285 assertFalse("equals", fField2
.equals(fField1
));
287 assertFalse("equals", fField1
.equals(fStructTerminalField1
));
290 // ------------------------------------------------------------------------
292 // ------------------------------------------------------------------------
295 public void testToString() {
296 final String expected1
= fFieldName1
+ "=" + fValue1
.toString();
297 TmfEventField field
= new TmfEventField(fFieldName1
, fValue1
, null);
298 assertEquals("toString", expected1
, field
.toString());
300 final String expected2
= fFieldName1
+ "=" + fValue2
.toString();
301 field
= new TmfEventField(fFieldName1
, fValue2
, null);
302 assertEquals("toString", expected2
, field
.toString());
305 // ------------------------------------------------------------------------
307 // ------------------------------------------------------------------------
310 public void testMakeRoot() {
311 ITmfEventField root
= TmfEventField
.makeRoot(fStructFieldNames
);
312 String
[] names
= root
.getFieldNames();
313 assertEquals("getFieldNames length", 2, names
.length
);
314 for (int i
= 0; i
< names
.length
; i
++) {
315 assertSame("getFieldNames", fStructFieldNames
[i
], names
[i
]);
316 assertSame("getFieldName", fStructFieldNames
[i
], root
.getFieldName(i
));
317 assertNull("getValue", root
.getField(i
).getValue());
319 assertNull("getFieldName", root
.getFieldName(-1));
320 assertNull("getFieldName", root
.getFieldName(names
.length
));
322 root
= TmfEventField
.makeRoot(fFieldNames
);
323 names
= root
.getFieldNames();
324 assertEquals("getFieldNames length", 2, names
.length
);
325 for (int i
= 0; i
< names
.length
; i
++) {
326 assertSame("getFieldNames", fFieldNames
[i
], names
[i
]);
327 assertSame("getFieldName", fFieldNames
[i
], root
.getFieldName(i
));
328 assertNull("getValue", root
.getField(i
).getValue());
330 assertNull("getFieldName", root
.getFieldName(-1));
331 assertNull("getFieldName", root
.getFieldName(names
.length
));