Adjusted filters for matching meta-fields
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / event / TmfEventFieldTest.java
CommitLineData
d18dd09b 1/*******************************************************************************
75d42a16 2 * Copyright (c) 2009, 2012 Ericsson
d18dd09b
ASL
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
75d42a16 11 * Francois Chouinard - Adjusted for new Event Model
d18dd09b
ASL
12 *******************************************************************************/
13
6c13869b 14package org.eclipse.linuxtools.tmf.core.tests.event;
d18dd09b 15
cbd4ad82
FC
16import junit.framework.TestCase;
17
4c564a2d 18import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
6c13869b 19import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
d18dd09b
ASL
20
21/**
22 * <b><u>TmfEventFieldTest</u></b>
23 * <p>
cbd4ad82 24 * Test suite for the TmfEventField class.
d18dd09b 25 */
3b38ea61 26@SuppressWarnings("nls")
d18dd09b
ASL
27public class TmfEventFieldTest extends TestCase {
28
75d42a16
FC
29 // ------------------------------------------------------------------------
30 // Variables
31 // ------------------------------------------------------------------------
32
33 private final String fFieldName1 = "Field-1";
34 private final String fFieldName2 = "Field-2";
35
36 private final Object fValue1 = new String("Value");
37 private final Object fValue2 = new Integer(10);
38
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);
42
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 });
50
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 });
55
56 // ------------------------------------------------------------------------
57 // Housekeeping
58 // ------------------------------------------------------------------------
59
60 /**
61 * @param name the test name
62 */
63 public TmfEventFieldTest(String name) {
64 super(name);
65 }
66
67 @Override
68 protected void setUp() throws Exception {
69 super.setUp();
70 }
71
72 @Override
73 protected void tearDown() throws Exception {
74 super.tearDown();
75 }
76
77 // ------------------------------------------------------------------------
78 // Constructors
79 // ------------------------------------------------------------------------
80
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));
90 }
91
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));
100
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));
106 }
107 assertNull("getFieldName", fStructRootField.getFieldName(-1));
108 assertNull("getFieldName", fStructRootField.getFieldName(names.length));
109 }
110
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));
119
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));
127 }
128
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));
137
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));
143 }
144 assertNull("getFieldName", fRootField.getFieldName(-1));
145 assertNull("getFieldName", fRootField.getFieldName(names.length));
146 }
147
148 public void testConstructorBadArg() {
149 try {
150 new TmfEventField(null, fValue1, null);
151 fail("Invalid (null) field name");
152 } catch (IllegalArgumentException e) {
153 }
154 }
155
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));
165 }
166
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));
175
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));
181 }
182 assertNull("getFieldName", fRootField.getFieldName(names.length));
183 }
184
185 public void testCopyConstructorBadArg() {
186 try {
187 new TmfEventField(null);
188 fail("TmfEventField: null arguemnt");
189 } catch (IllegalArgumentException e) {
190 }
191 }
192
193 // ------------------------------------------------------------------------
194 // Modifiers
195 // ------------------------------------------------------------------------
196
197 private class MyField extends TmfEventField {
198
199 public MyField(String id, Object value) {
200 super(id, value);
201 }
202
203 public MyField(TmfEventField field) {
204 super(field);
205 }
206
207 @Override
208 public void setValue(Object value, ITmfEventField[] subfields) {
209 super.setValue(value, subfields);
210 }
211 }
212
213 public void testSetValue() {
214 TmfEventField field = new TmfEventField(fFieldName1, fValue1, null);
d18dd09b 215
75d42a16
FC
216 MyField myField = new MyField(field);
217 assertSame("getValue", fValue1, myField.getValue());
218 myField.setValue(fValue2, null);
219 assertSame("getValue", fValue2, myField.getValue());
220 }
cbd4ad82
FC
221
222 // ------------------------------------------------------------------------
75d42a16
FC
223 // clone
224 // ------------------------------------------------------------------------
225
226 public void testClone() throws Exception {
227 ITmfEventField clone = fStructTerminalField1.clone();
228 assertEquals("clone", fStructTerminalField1, clone);
229
230 clone = fField1.clone();
231 assertEquals("clone", fField1, clone);
232
233 clone = fRootField.clone();
234 assertEquals("clone", fRootField, clone);
235
236 clone = fStructRootField.clone();
237 assertEquals("clone", fStructRootField, clone);
238 }
239
240 // ------------------------------------------------------------------------
241 // hashCode
242 // ------------------------------------------------------------------------
243
244 public void testHashCode() throws Exception {
245 TmfEventField copy = new TmfEventField(fField1);
246 assertTrue("hashCode", fField1.hashCode() == copy.hashCode());
247 assertTrue("hashCode", fField1.hashCode() != fField2.hashCode());
248
249 copy = new TmfEventField(fStructTerminalField1);
250 assertTrue("hashCode", fStructTerminalField1.hashCode() == copy.hashCode());
251 assertTrue("hashCode", fStructTerminalField1.hashCode() != fStructTerminalField2.hashCode());
252 }
253
254 // ------------------------------------------------------------------------
255 // equals
256 // ------------------------------------------------------------------------
257
258 public void testEqualsReflexivity() throws Exception {
259 assertTrue("equals", fField1.equals(fField1));
260 assertTrue("equals", fField2.equals(fField2));
261
262 assertFalse("equals", fField1.equals(fField2));
263 assertFalse("equals", fField2.equals(fField1));
264
265 assertTrue("equals", fStructTerminalField1.equals(fStructTerminalField1));
266 assertTrue("equals", fStructTerminalField2.equals(fStructTerminalField2));
267
268 assertFalse("equals", fStructTerminalField1.equals(fStructTerminalField2));
269 assertFalse("equals", fStructTerminalField2.equals(fStructTerminalField1));
270 }
271
272 public void testEqualsSymmetry() throws Exception {
273 TmfEventField copy0 = new TmfEventField(fField1);
274 assertTrue("equals", fField1.equals(copy0));
275 assertTrue("equals", copy0.equals(fField1));
276
277 TmfEventField copy3 = new TmfEventField(fField2);
278 assertTrue("equals", fField2.equals(copy3));
279 assertTrue("equals", copy3.equals(fField2));
280 }
281
282 public void testEqualsTransivity() throws Exception {
283 TmfEventField copy1 = new TmfEventField(fField1);
284 TmfEventField copy2 = new TmfEventField(copy1);
285 assertTrue("equals", fField1.equals(copy1));
286 assertTrue("equals", copy1.equals(copy2));
287 assertTrue("equals", fField1.equals(copy2));
288
289 copy1 = new TmfEventField(fField2);
290 copy2 = new TmfEventField(copy1);
291 assertTrue("equals", fField2.equals(copy1));
292 assertTrue("equals", copy1.equals(copy2));
293 assertTrue("equals", fField2.equals(copy2));
294 }
295
75d42a16
FC
296 public void testEquals() throws Exception {
297 assertTrue("equals", fStructTerminalField1.equals(fStructTerminalField3));
298 assertTrue("equals", fStructTerminalField3.equals(fStructTerminalField1));
299
300 assertFalse("equals", fStructTerminalField1.equals(fField3));
301 assertFalse("equals", fField3.equals(fStructTerminalField1));
302 }
303
39f9eadb
FC
304 public void testEqualsNull() throws Exception {
305 assertFalse("equals", fField1.equals(null));
306 assertFalse("equals", fField2.equals(null));
307 }
308
309 public void testNonEqualClasses() throws Exception {
310 assertFalse("equals", fField1.equals(fStructTerminalField1));
311 assertFalse("equals", fField1.equals(fValue1));
312 }
313
314 public void testNonEqualValues() throws Exception {
315 TmfEventField copy1 = new TmfEventField(fFieldName1, fValue1);
316 TmfEventField copy2 = new TmfEventField(fFieldName1, fValue1);
317 assertTrue("equals", copy1.equals(copy2));
318 assertTrue("equals", copy2.equals(copy1));
319
320 copy2 = new TmfEventField(fFieldName1, fValue2);
321 assertFalse("equals", copy1.equals(copy2));
322 assertFalse("equals", copy2.equals(copy1));
323
324 copy2 = new TmfEventField(fFieldName1, null);
325 assertFalse("equals", copy1.equals(copy2));
326 assertFalse("equals", copy2.equals(copy1));
327 }
328
75d42a16
FC
329 public void testNonEquals() throws Exception {
330 assertFalse("equals", fField1.equals(fField2));
331 assertFalse("equals", fField2.equals(fField1));
332
333 assertFalse("equals", fField1.equals(fStructTerminalField1));
334 }
335
75d42a16
FC
336 // ------------------------------------------------------------------------
337 // toString
338 // ------------------------------------------------------------------------
339
340 public void testToString() {
341 String expected1 = "TmfEventField [fFieldId=" + fFieldName1 + ", fValue=" + fValue1.toString() + "]";
342 TmfEventField field = new TmfEventField(fFieldName1, fValue1, null);
343 assertEquals("toString", expected1, field.toString());
344
345 String expected2 = "TmfEventField [fFieldId=" + fFieldName1 + ", fValue=" + fValue2.toString() + "]";
346 field = new TmfEventField(fFieldName1, fValue2, null);
347 assertEquals("toString", expected2, field.toString());
348 }
349
350 // ------------------------------------------------------------------------
351 // makeRoot
352 // ------------------------------------------------------------------------
353
354 public void testMakeRoot() {
355 ITmfEventField root = TmfEventField.makeRoot(fStructFieldNames);
356 String[] names = root.getFieldNames();
357 assertEquals("getFieldNames length", 2, names.length);
358 for (int i = 0; i < names.length; i++) {
359 assertSame("getFieldNames", fStructFieldNames[i], names[i]);
360 assertSame("getFieldName", fStructFieldNames[i], root.getFieldName(i));
361 assertNull("getValue", root.getField(i).getValue());
362 }
363 assertNull("getFieldName", root.getFieldName(-1));
364 assertNull("getFieldName", root.getFieldName(names.length));
365
366 root = TmfEventField.makeRoot(fFieldNames);
367 names = root.getFieldNames();
368 assertEquals("getFieldNames length", 2, names.length);
369 for (int i = 0; i < names.length; i++) {
370 assertSame("getFieldNames", fFieldNames[i], names[i]);
371 assertSame("getFieldName", fFieldNames[i], root.getFieldName(i));
372 assertNull("getValue", root.getField(i).getValue());
373 }
374 assertNull("getFieldName", root.getFieldName(-1));
375 assertNull("getFieldName", root.getFieldName(names.length));
376 }
d18dd09b
ASL
377
378}
This page took 0.047567 seconds and 5 git commands to generate.