Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2009, 2013 Ericsson |
54a7a54c | 3 | * |
d18dd09b ASL |
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 | |
54a7a54c | 8 | * |
d18dd09b ASL |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
75d42a16 | 11 | * Francois Chouinard - Adjusted for new Event Model |
6e1886bc | 12 | * Alexandre Montplaisir - Port to JUnit4 |
d18dd09b ASL |
13 | *******************************************************************************/ |
14 | ||
6c13869b | 15 | package org.eclipse.linuxtools.tmf.core.tests.event; |
d18dd09b | 16 | |
b742c196 | 17 | import static org.junit.Assert.assertArrayEquals; |
6e1886bc AM |
18 | import static org.junit.Assert.assertEquals; |
19 | import static org.junit.Assert.assertFalse; | |
40d8c779 | 20 | import static org.junit.Assert.assertNotEquals; |
6e1886bc AM |
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; | |
cbd4ad82 | 25 | |
b742c196 AM |
26 | import java.util.Collection; |
27 | ||
4c564a2d | 28 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
40d8c779 | 29 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
6c13869b | 30 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
6e1886bc | 31 | import org.junit.Test; |
d18dd09b ASL |
32 | |
33 | /** | |
cbd4ad82 | 34 | * Test suite for the TmfEventField class. |
d18dd09b | 35 | */ |
cad06250 | 36 | @SuppressWarnings("javadoc") |
6e1886bc | 37 | public class TmfEventFieldTest { |
d18dd09b | 38 | |
75d42a16 FC |
39 | // ------------------------------------------------------------------------ |
40 | // Variables | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
43 | private final String fFieldName1 = "Field-1"; | |
44 | private final String fFieldName2 = "Field-2"; | |
45 | ||
ea2b103b | 46 | private final Object fValue1 = "Value"; |
47 | private final Object fValue2 = Integer.valueOf(10); | |
75d42a16 | 48 | |
214cc822 | 49 | private final TmfEventField fField1 = new TmfEventField(fFieldName1, fValue1, null); |
085d898f FC |
50 | private final TmfEventField fField2 = new TmfEventField(fFieldName2, fValue2, null); |
51 | private final TmfEventField fField3 = new TmfEventField(fFieldName1, fValue2, null); | |
75d42a16 FC |
52 | |
53 | private final String fStructRootFieldName = "Root-S"; | |
54 | private final String[] fStructFieldNames = new String[] { fFieldName1, fFieldName2 }; | |
214cc822 AM |
55 | private final TmfEventField fStructTerminalField1 = new TmfEventField(fFieldName1, null, null); |
56 | private final TmfEventField fStructTerminalField2 = new TmfEventField(fFieldName2, null, null); | |
57 | private final TmfEventField fStructTerminalField3 = new TmfEventField(fFieldName1, null, null); | |
58 | private final TmfEventField fStructRootField = new TmfEventField(fStructRootFieldName, null, | |
75d42a16 FC |
59 | new ITmfEventField[] { fStructTerminalField1, fStructTerminalField2 }); |
60 | ||
61 | private final String fRootFieldName = "Root"; | |
62 | private final String[] fFieldNames = new String[] { fFieldName1, fFieldName2 }; | |
214cc822 | 63 | private final TmfEventField fRootField = new TmfEventField(fRootFieldName, null, |
75d42a16 FC |
64 | new ITmfEventField[] { fField1, fField2 }); |
65 | ||
75d42a16 FC |
66 | // ------------------------------------------------------------------------ |
67 | // Constructors | |
68 | // ------------------------------------------------------------------------ | |
69 | ||
6e1886bc | 70 | @Test |
75d42a16 FC |
71 | public void testTerminalStructConstructor() { |
72 | assertSame("getName", fFieldName1, fStructTerminalField1.getName()); | |
73 | assertNull("getValue", fStructTerminalField1.getValue()); | |
b742c196 | 74 | assertEquals("getFields", 0, fStructTerminalField1.getFields().size()); |
75d42a16 | 75 | assertNull("getField(name)", fStructTerminalField1.getField(fFieldName1)); |
b742c196 | 76 | assertEquals("getFieldNames", 0, fStructTerminalField1.getFieldNames().size()); |
75d42a16 FC |
77 | } |
78 | ||
6e1886bc | 79 | @Test |
75d42a16 FC |
80 | public void testNonTerminalStructConstructor() { |
81 | assertSame("getName", fStructRootFieldName, fStructRootField.getName()); | |
82 | assertNull("getValue", fStructRootField.getValue()); | |
b742c196 | 83 | assertEquals("getFields", 2, fStructRootField.getFields().size()); |
75d42a16 FC |
84 | assertSame("getField(name)", fStructTerminalField1, fStructRootField.getField(fFieldName1)); |
85 | assertSame("getField(name)", fStructTerminalField2, fStructRootField.getField(fFieldName2)); | |
b742c196 AM |
86 | |
87 | final Collection<String> names = fStructRootField.getFieldNames(); | |
88 | assertEquals("getFieldNames length", 2, names.size()); | |
89 | assertArrayEquals(fStructFieldNames, names.toArray(new String[names.size()])); | |
75d42a16 FC |
90 | } |
91 | ||
6e1886bc | 92 | @Test |
75d42a16 FC |
93 | public void testTerminalConstructor() { |
94 | assertSame("getName", fFieldName1, fField1.getName()); | |
95 | assertSame("getValue", fValue1, fField1.getValue()); | |
b742c196 | 96 | assertEquals("getFields", 0, fField1.getFields().size()); |
75d42a16 | 97 | assertNull("getField(name)", fField1.getField(fFieldName1)); |
b742c196 | 98 | assertEquals("getFieldNames", 0, fField1.getFieldNames().size()); |
75d42a16 FC |
99 | |
100 | assertSame("getName", fFieldName2, fField2.getName()); | |
101 | assertSame("getValue", fValue2, fField2.getValue()); | |
b742c196 | 102 | assertEquals("getFields", 0, fField2.getFields().size()); |
75d42a16 | 103 | assertNull("getField(name)", fField2.getField(fFieldName2)); |
75d42a16 FC |
104 | } |
105 | ||
6e1886bc | 106 | @Test |
75d42a16 FC |
107 | public void testNonTerminalConstructor() { |
108 | assertSame("getName", fRootFieldName, fRootField.getName()); | |
109 | assertNull("getValue", fRootField.getValue()); | |
b742c196 | 110 | assertEquals("getFields", 2, fRootField.getFields().size()); |
75d42a16 FC |
111 | assertSame("getField(name)", fField1, fRootField.getField(fFieldName1)); |
112 | assertSame("getField(name)", fField2, fRootField.getField(fFieldName2)); | |
b742c196 AM |
113 | |
114 | final Collection<String> names = fRootField.getFieldNames(); | |
115 | assertEquals("getFieldNames length", 2, names.size()); | |
116 | assertArrayEquals(fFieldNames, names.toArray(new String[names.size()])); | |
75d42a16 FC |
117 | } |
118 | ||
6e1886bc | 119 | @Test |
75d42a16 FC |
120 | public void testConstructorBadArg() { |
121 | try { | |
122 | new TmfEventField(null, fValue1, null); | |
123 | fail("Invalid (null) field name"); | |
085d898f | 124 | } catch (final IllegalArgumentException e) { |
75d42a16 FC |
125 | } |
126 | } | |
127 | ||
6e1886bc | 128 | @Test |
75d42a16 | 129 | public void testTerminalCopyConstructor() { |
085d898f | 130 | final TmfEventField copy = new TmfEventField(fField1); |
75d42a16 FC |
131 | assertSame("getName", fFieldName1, copy.getName()); |
132 | assertSame("getValue", fValue1, copy.getValue()); | |
b742c196 | 133 | assertEquals("getFields", 0, copy.getFields().size()); |
75d42a16 | 134 | assertNull("getField(name)", copy.getField(fFieldName1)); |
b742c196 | 135 | assertEquals("getFieldNames", 0, copy.getFieldNames().size()); |
75d42a16 FC |
136 | } |
137 | ||
6e1886bc | 138 | @Test |
75d42a16 FC |
139 | public void testNonTerminalCopyConstructor() { |
140 | assertSame("getName", fRootFieldName, fRootField.getName()); | |
141 | assertNull("getValue", fRootField.getValue()); | |
b742c196 | 142 | assertEquals("getFields", 2, fRootField.getFields().size()); |
75d42a16 FC |
143 | assertSame("getField(name)", fField1, fRootField.getField(fFieldName1)); |
144 | assertSame("getField(name)", fField2, fRootField.getField(fFieldName2)); | |
b742c196 AM |
145 | |
146 | final Collection<String> names = fRootField.getFieldNames(); | |
147 | assertEquals("getFieldNames length", 2, names.size()); | |
148 | assertArrayEquals(fFieldNames, names.toArray(new String[names.size()])); | |
75d42a16 FC |
149 | } |
150 | ||
6e1886bc | 151 | @Test |
75d42a16 FC |
152 | public void testCopyConstructorBadArg() { |
153 | try { | |
154 | new TmfEventField(null); | |
155 | fail("TmfEventField: null arguemnt"); | |
085d898f | 156 | } catch (final IllegalArgumentException e) { |
75d42a16 FC |
157 | } |
158 | } | |
159 | ||
b742c196 AM |
160 | /** |
161 | * Test that we correctly fail to create a field with subfields having the | |
162 | * same name. | |
163 | */ | |
164 | @Test | |
165 | public void testDuplicateFieldNames() { | |
166 | ITmfEventField[] fields = { | |
167 | new TmfEventField("samename", null, null), | |
168 | new TmfEventField("samename", null, null) | |
169 | }; | |
170 | ||
171 | try { | |
172 | new TmfEventField("field", null, fields); | |
173 | fail("TmfEventField: Duplicate field names"); | |
174 | } catch (IllegalArgumentException e) { | |
175 | /* Expected exception */ | |
176 | } | |
177 | } | |
178 | ||
75d42a16 | 179 | // ------------------------------------------------------------------------ |
75d42a16 FC |
180 | // hashCode |
181 | // ------------------------------------------------------------------------ | |
182 | ||
6e1886bc | 183 | @Test |
54a7a54c | 184 | public void testHashCode() { |
75d42a16 FC |
185 | TmfEventField copy = new TmfEventField(fField1); |
186 | assertTrue("hashCode", fField1.hashCode() == copy.hashCode()); | |
187 | assertTrue("hashCode", fField1.hashCode() != fField2.hashCode()); | |
188 | ||
189 | copy = new TmfEventField(fStructTerminalField1); | |
190 | assertTrue("hashCode", fStructTerminalField1.hashCode() == copy.hashCode()); | |
191 | assertTrue("hashCode", fStructTerminalField1.hashCode() != fStructTerminalField2.hashCode()); | |
192 | } | |
193 | ||
194 | // ------------------------------------------------------------------------ | |
195 | // equals | |
196 | // ------------------------------------------------------------------------ | |
197 | ||
6e1886bc | 198 | @Test |
54a7a54c | 199 | public void testEqualsReflexivity() { |
75d42a16 FC |
200 | assertTrue("equals", fField1.equals(fField1)); |
201 | assertTrue("equals", fField2.equals(fField2)); | |
202 | ||
203 | assertFalse("equals", fField1.equals(fField2)); | |
204 | assertFalse("equals", fField2.equals(fField1)); | |
205 | ||
206 | assertTrue("equals", fStructTerminalField1.equals(fStructTerminalField1)); | |
207 | assertTrue("equals", fStructTerminalField2.equals(fStructTerminalField2)); | |
208 | ||
209 | assertFalse("equals", fStructTerminalField1.equals(fStructTerminalField2)); | |
210 | assertFalse("equals", fStructTerminalField2.equals(fStructTerminalField1)); | |
211 | } | |
212 | ||
6e1886bc | 213 | @Test |
54a7a54c | 214 | public void testEqualsSymmetry() { |
085d898f | 215 | final TmfEventField copy0 = new TmfEventField(fField1); |
75d42a16 FC |
216 | assertTrue("equals", fField1.equals(copy0)); |
217 | assertTrue("equals", copy0.equals(fField1)); | |
085d898f FC |
218 | |
219 | final TmfEventField copy3 = new TmfEventField(fField2); | |
75d42a16 FC |
220 | assertTrue("equals", fField2.equals(copy3)); |
221 | assertTrue("equals", copy3.equals(fField2)); | |
222 | } | |
223 | ||
6e1886bc | 224 | @Test |
54a7a54c | 225 | public void testEqualsTransivity() { |
75d42a16 FC |
226 | TmfEventField copy1 = new TmfEventField(fField1); |
227 | TmfEventField copy2 = new TmfEventField(copy1); | |
228 | assertTrue("equals", fField1.equals(copy1)); | |
229 | assertTrue("equals", copy1.equals(copy2)); | |
230 | assertTrue("equals", fField1.equals(copy2)); | |
231 | ||
232 | copy1 = new TmfEventField(fField2); | |
233 | copy2 = new TmfEventField(copy1); | |
234 | assertTrue("equals", fField2.equals(copy1)); | |
235 | assertTrue("equals", copy1.equals(copy2)); | |
236 | assertTrue("equals", fField2.equals(copy2)); | |
237 | } | |
238 | ||
6e1886bc | 239 | @Test |
54a7a54c | 240 | public void testEquals() { |
75d42a16 FC |
241 | assertTrue("equals", fStructTerminalField1.equals(fStructTerminalField3)); |
242 | assertTrue("equals", fStructTerminalField3.equals(fStructTerminalField1)); | |
243 | ||
244 | assertFalse("equals", fStructTerminalField1.equals(fField3)); | |
245 | assertFalse("equals", fField3.equals(fStructTerminalField1)); | |
246 | } | |
247 | ||
6e1886bc | 248 | @Test |
54a7a54c | 249 | public void testEqualsNull() { |
39f9eadb FC |
250 | assertFalse("equals", fField1.equals(null)); |
251 | assertFalse("equals", fField2.equals(null)); | |
252 | } | |
253 | ||
6e1886bc | 254 | @Test |
54a7a54c | 255 | public void testNonEqualClasses() { |
39f9eadb FC |
256 | assertFalse("equals", fField1.equals(fStructTerminalField1)); |
257 | assertFalse("equals", fField1.equals(fValue1)); | |
258 | } | |
259 | ||
6e1886bc | 260 | @Test |
54a7a54c | 261 | public void testNonEqualValues() { |
214cc822 AM |
262 | final TmfEventField copy1 = new TmfEventField(fFieldName1, fValue1, null); |
263 | TmfEventField copy2 = new TmfEventField(fFieldName1, fValue1, null); | |
39f9eadb FC |
264 | assertTrue("equals", copy1.equals(copy2)); |
265 | assertTrue("equals", copy2.equals(copy1)); | |
266 | ||
214cc822 | 267 | copy2 = new TmfEventField(fFieldName1, fValue2, null); |
39f9eadb FC |
268 | assertFalse("equals", copy1.equals(copy2)); |
269 | assertFalse("equals", copy2.equals(copy1)); | |
270 | ||
214cc822 | 271 | copy2 = new TmfEventField(fFieldName1, null, null); |
39f9eadb FC |
272 | assertFalse("equals", copy1.equals(copy2)); |
273 | assertFalse("equals", copy2.equals(copy1)); | |
274 | } | |
275 | ||
6e1886bc | 276 | @Test |
54a7a54c | 277 | public void testNonEquals() { |
75d42a16 FC |
278 | assertFalse("equals", fField1.equals(fField2)); |
279 | assertFalse("equals", fField2.equals(fField1)); | |
280 | ||
281 | assertFalse("equals", fField1.equals(fStructTerminalField1)); | |
282 | } | |
283 | ||
40d8c779 AM |
284 | /** |
285 | * Test with same fields, but different values (should not be equal) | |
286 | */ | |
287 | @Test | |
288 | public void testNonEqualsValue() { | |
289 | final String fieldName = "myfield"; | |
290 | final Object value1 = new String("test-string"); | |
291 | final Object value2 = new TmfEvent(); | |
292 | final TmfEventField[] fields = { fField1, fField2 }; | |
293 | ||
294 | final TmfEventField field1 = new TmfEventField(fieldName, value1, fields); | |
295 | final TmfEventField field2 = new TmfEventField(fieldName, value2, fields); | |
296 | ||
297 | assertNotEquals(field1, field2); | |
298 | assertNotEquals(field2, field1); | |
299 | } | |
300 | ||
301 | /** | |
302 | * Test with same value, but different fields (should not be equal) | |
303 | */ | |
304 | @Test | |
305 | public void testNonEqualsFields() { | |
306 | final String fieldName = "myfield"; | |
307 | final Object value = new String("test-string"); | |
308 | final TmfEventField[] fields1 = { fField1, fField2 }; | |
309 | final TmfEventField[] fields2 = { fField2, fField3 }; | |
310 | ||
311 | final TmfEventField field1 = new TmfEventField(fieldName, value, fields1); | |
312 | final TmfEventField field2 = new TmfEventField(fieldName, value, fields2); | |
313 | ||
314 | assertNotEquals(field1, field2); | |
315 | assertNotEquals(field2, field1); | |
316 | } | |
317 | ||
318 | /** | |
319 | * Test with same field and values (should be equals) | |
320 | */ | |
321 | @Test | |
322 | public void testEqualsEverything() { | |
323 | final String fieldName = "myfield"; | |
324 | final Object value = new String("test-string"); | |
325 | final TmfEventField[] fields = { fField1, fField2 }; | |
326 | ||
327 | final TmfEventField field1 = new TmfEventField(fieldName, value, fields); | |
328 | final TmfEventField field2 = new TmfEventField(fieldName, value, fields); | |
329 | ||
330 | assertEquals(field1, field2); | |
331 | assertEquals(field2, field1); | |
332 | } | |
333 | ||
75d42a16 FC |
334 | // ------------------------------------------------------------------------ |
335 | // toString | |
336 | // ------------------------------------------------------------------------ | |
337 | ||
6e1886bc | 338 | @Test |
75d42a16 | 339 | public void testToString() { |
306dc902 | 340 | final String expected1 = fFieldName1 + "=" + fValue1.toString(); |
75d42a16 FC |
341 | TmfEventField field = new TmfEventField(fFieldName1, fValue1, null); |
342 | assertEquals("toString", expected1, field.toString()); | |
343 | ||
306dc902 | 344 | final String expected2 = fFieldName1 + "=" + fValue2.toString(); |
75d42a16 FC |
345 | field = new TmfEventField(fFieldName1, fValue2, null); |
346 | assertEquals("toString", expected2, field.toString()); | |
347 | } | |
348 | ||
349 | // ------------------------------------------------------------------------ | |
350 | // makeRoot | |
351 | // ------------------------------------------------------------------------ | |
352 | ||
6e1886bc | 353 | @Test |
75d42a16 FC |
354 | public void testMakeRoot() { |
355 | ITmfEventField root = TmfEventField.makeRoot(fStructFieldNames); | |
b742c196 AM |
356 | Collection<String> names = root.getFieldNames(); |
357 | assertEquals("getFieldNames length", 2, names.size()); | |
358 | assertArrayEquals(fStructFieldNames, names.toArray(new String[names.size()])); | |
75d42a16 FC |
359 | |
360 | root = TmfEventField.makeRoot(fFieldNames); | |
361 | names = root.getFieldNames(); | |
b742c196 AM |
362 | assertEquals("getFieldNames length", 2, names.size()); |
363 | assertArrayEquals(fFieldNames, names.toArray(new String[names.size()])); | |
75d42a16 | 364 | } |
d18dd09b | 365 | } |