10a00c8efb314e0fb0f46fcede2abc55f4a5e015
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / event / TmfEventFieldTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adjusted for new Event Model
12 * Alexandre Montplaisir - Port to JUnit4
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.core.tests.event;
16
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;
25
26 import java.util.Collection;
27
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;
34
35 /**
36 * Test suite for the TmfEventField class.
37 */
38 @SuppressWarnings("javadoc")
39 public class TmfEventFieldTest {
40
41 // ------------------------------------------------------------------------
42 // Variables
43 // ------------------------------------------------------------------------
44
45 private final @NonNull String fFieldName1 = "Field-1";
46 private final @NonNull String fFieldName2 = "Field-2";
47
48 private final Object fValue1 = "Value";
49 private final Object fValue2 = Integer.valueOf(10);
50
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);
54
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 });
62
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 });
67
68 // ------------------------------------------------------------------------
69 // Constructors
70 // ------------------------------------------------------------------------
71
72 @Test
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());
79 }
80
81 @Test
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));
88
89 final Collection<String> names = fStructRootField.getFieldNames();
90 assertEquals("getFieldNames length", 2, names.size());
91 assertArrayEquals(fStructFieldNames, names.toArray(new String[names.size()]));
92 }
93
94 @Test
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());
101
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));
106 }
107
108 @Test
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));
115
116 final Collection<String> names = fRootField.getFieldNames();
117 assertEquals("getFieldNames length", 2, names.size());
118 assertArrayEquals(fFieldNames, names.toArray(new String[names.size()]));
119 }
120
121 @Test
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());
129 }
130
131 @Test
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));
138
139 final Collection<String> names = fRootField.getFieldNames();
140 assertEquals("getFieldNames length", 2, names.size());
141 assertArrayEquals(fFieldNames, names.toArray(new String[names.size()]));
142 }
143
144 @Test
145 public void testCopyConstructorBadArg() {
146 try {
147 new TmfEventField(null);
148 fail("TmfEventField: null arguemnt");
149 } catch (final IllegalArgumentException e) {
150 }
151 }
152
153 /**
154 * Test that we correctly fail to create a field with subfields having the
155 * same name.
156 */
157 @Test
158 public void testDuplicateFieldNames() {
159 ITmfEventField[] fields = {
160 new TmfEventField("samename", null, null),
161 new TmfEventField("samename", null, null)
162 };
163
164 try {
165 new TmfEventField("field", null, fields);
166 fail("TmfEventField: Duplicate field names");
167 } catch (IllegalArgumentException e) {
168 /* Expected exception */
169 }
170 }
171
172 // ------------------------------------------------------------------------
173 // hashCode
174 // ------------------------------------------------------------------------
175
176 @Test
177 public void testHashCode() {
178 TmfEventField copy = new TmfEventField(fField1);
179 assertTrue("hashCode", fField1.hashCode() == copy.hashCode());
180 assertTrue("hashCode", fField1.hashCode() != fField2.hashCode());
181
182 copy = new TmfEventField(fStructTerminalField1);
183 assertTrue("hashCode", fStructTerminalField1.hashCode() == copy.hashCode());
184 assertTrue("hashCode", fStructTerminalField1.hashCode() != fStructTerminalField2.hashCode());
185 }
186
187 // ------------------------------------------------------------------------
188 // equals
189 // ------------------------------------------------------------------------
190
191 @Test
192 public void testEqualsReflexivity() {
193 assertTrue("equals", fField1.equals(fField1));
194 assertTrue("equals", fField2.equals(fField2));
195
196 assertFalse("equals", fField1.equals(fField2));
197 assertFalse("equals", fField2.equals(fField1));
198
199 assertTrue("equals", fStructTerminalField1.equals(fStructTerminalField1));
200 assertTrue("equals", fStructTerminalField2.equals(fStructTerminalField2));
201
202 assertFalse("equals", fStructTerminalField1.equals(fStructTerminalField2));
203 assertFalse("equals", fStructTerminalField2.equals(fStructTerminalField1));
204 }
205
206 @Test
207 public void testEqualsSymmetry() {
208 final TmfEventField copy0 = new TmfEventField(fField1);
209 assertTrue("equals", fField1.equals(copy0));
210 assertTrue("equals", copy0.equals(fField1));
211
212 final TmfEventField copy3 = new TmfEventField(fField2);
213 assertTrue("equals", fField2.equals(copy3));
214 assertTrue("equals", copy3.equals(fField2));
215 }
216
217 @Test
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));
224
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));
230 }
231
232 @Test
233 public void testEquals() {
234 assertTrue("equals", fStructTerminalField1.equals(fStructTerminalField3));
235 assertTrue("equals", fStructTerminalField3.equals(fStructTerminalField1));
236
237 assertFalse("equals", fStructTerminalField1.equals(fField3));
238 assertFalse("equals", fField3.equals(fStructTerminalField1));
239 }
240
241 @Test
242 public void testEqualsNull() {
243 assertFalse("equals", fField1.equals(null));
244 assertFalse("equals", fField2.equals(null));
245 }
246
247 @Test
248 public void testNonEqualClasses() {
249 assertFalse("equals", fField1.equals(fStructTerminalField1));
250 assertFalse("equals", fField1.equals(fValue1));
251 }
252
253 @Test
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));
259
260 copy2 = new TmfEventField(fFieldName1, fValue2, null);
261 assertFalse("equals", copy1.equals(copy2));
262 assertFalse("equals", copy2.equals(copy1));
263
264 copy2 = new TmfEventField(fFieldName1, null, null);
265 assertFalse("equals", copy1.equals(copy2));
266 assertFalse("equals", copy2.equals(copy1));
267 }
268
269 @Test
270 public void testNonEquals() {
271 assertFalse("equals", fField1.equals(fField2));
272 assertFalse("equals", fField2.equals(fField1));
273
274 assertFalse("equals", fField1.equals(fStructTerminalField1));
275 }
276
277 /**
278 * Test with same fields, but different values (should not be equal)
279 */
280 @Test
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 };
286
287 final TmfEventField field1 = new TmfEventField(fieldName, value1, fields);
288 final TmfEventField field2 = new TmfEventField(fieldName, value2, fields);
289
290 assertNotEquals(field1, field2);
291 assertNotEquals(field2, field1);
292 }
293
294 /**
295 * Test with same value, but different fields (should not be equal)
296 */
297 @Test
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 };
303
304 final TmfEventField field1 = new TmfEventField(fieldName, value, fields1);
305 final TmfEventField field2 = new TmfEventField(fieldName, value, fields2);
306
307 assertNotEquals(field1, field2);
308 assertNotEquals(field2, field1);
309 }
310
311 /**
312 * Test with same field and values (should be equals)
313 */
314 @Test
315 public void testEqualsEverything() {
316 final String fieldName = "myfield";
317 final Object value = new String("test-string");
318 final TmfEventField[] fields = { fField1, fField2 };
319
320 final TmfEventField field1 = new TmfEventField(fieldName, value, fields);
321 final TmfEventField field2 = new TmfEventField(fieldName, value, fields);
322
323 assertEquals(field1, field2);
324 assertEquals(field2, field1);
325 }
326
327 // ------------------------------------------------------------------------
328 // toString
329 // ------------------------------------------------------------------------
330
331 @Test
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());
336
337 final String expected2 = fFieldName1 + "=" + fValue2.toString();
338 field = new TmfEventField(fFieldName1, fValue2, null);
339 assertEquals("toString", expected2, field.toString());
340 }
341
342 // ------------------------------------------------------------------------
343 // makeRoot
344 // ------------------------------------------------------------------------
345
346 @Test
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()]));
352
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()]));
357 }
358 }
This page took 0.041429 seconds and 4 git commands to generate.