Improve unit tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / event / TmfEventFieldTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 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 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.tests.event;
15
16 import junit.framework.TestCase;
17
18 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
19 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
20
21 /**
22 * <b><u>TmfEventFieldTest</u></b>
23 * <p>
24 * Test suite for the TmfEventField class.
25 */
26 @SuppressWarnings("nls")
27 public class TmfEventFieldTest extends TestCase {
28
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 = "Value";
37 private final Object fValue2 = Integer.valueOf(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 static 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);
215
216 MyField myField = new MyField(field);
217 assertSame("getValue", fValue1, myField.getValue());
218 myField.setValue(fValue2, null);
219 assertSame("getValue", fValue2, myField.getValue());
220 }
221
222 // ------------------------------------------------------------------------
223 // clone
224 // ------------------------------------------------------------------------
225
226 public void testFieldClone() throws Exception {
227 TmfEventField clone = fField1.clone();
228 assertTrue("clone", fField1.clone().equals(fField1));
229 assertTrue("clone", clone.clone().equals(clone));
230 assertEquals("clone", fField1, clone);
231 assertEquals("clone", clone, fField1);
232
233 clone = fRootField.clone();
234 assertTrue("clone", fRootField.clone().equals(fRootField));
235 assertTrue("clone", clone.clone().equals(clone));
236 assertEquals("clone", fRootField, clone);
237 assertEquals("clone", clone, fRootField);
238 }
239
240 public void testStructFieldClone() throws Exception {
241 TmfEventField clone = fStructTerminalField1.clone();
242 assertTrue("clone", fStructTerminalField1.clone().equals(fStructTerminalField1));
243 assertTrue("clone", clone.clone().equals(clone));
244 assertEquals("clone", fStructTerminalField1, clone);
245 assertEquals("clone", clone, fStructTerminalField1);
246
247 clone = fStructRootField.clone();
248 assertTrue("clone", fStructRootField.clone().equals(fStructRootField));
249 assertTrue("clone", clone.clone().equals(clone));
250 assertEquals("clone", fStructRootField, clone);
251 assertEquals("clone", clone, fStructRootField);
252 }
253
254 // ------------------------------------------------------------------------
255 // hashCode
256 // ------------------------------------------------------------------------
257
258 public void testHashCode() throws Exception {
259 TmfEventField copy = new TmfEventField(fField1);
260 assertTrue("hashCode", fField1.hashCode() == copy.hashCode());
261 assertTrue("hashCode", fField1.hashCode() != fField2.hashCode());
262
263 copy = new TmfEventField(fStructTerminalField1);
264 assertTrue("hashCode", fStructTerminalField1.hashCode() == copy.hashCode());
265 assertTrue("hashCode", fStructTerminalField1.hashCode() != fStructTerminalField2.hashCode());
266 }
267
268 // ------------------------------------------------------------------------
269 // equals
270 // ------------------------------------------------------------------------
271
272 public void testEqualsReflexivity() throws Exception {
273 assertTrue("equals", fField1.equals(fField1));
274 assertTrue("equals", fField2.equals(fField2));
275
276 assertFalse("equals", fField1.equals(fField2));
277 assertFalse("equals", fField2.equals(fField1));
278
279 assertTrue("equals", fStructTerminalField1.equals(fStructTerminalField1));
280 assertTrue("equals", fStructTerminalField2.equals(fStructTerminalField2));
281
282 assertFalse("equals", fStructTerminalField1.equals(fStructTerminalField2));
283 assertFalse("equals", fStructTerminalField2.equals(fStructTerminalField1));
284 }
285
286 public void testEqualsSymmetry() throws Exception {
287 TmfEventField copy0 = new TmfEventField(fField1);
288 assertTrue("equals", fField1.equals(copy0));
289 assertTrue("equals", copy0.equals(fField1));
290
291 TmfEventField copy3 = new TmfEventField(fField2);
292 assertTrue("equals", fField2.equals(copy3));
293 assertTrue("equals", copy3.equals(fField2));
294 }
295
296 public void testEqualsTransivity() throws Exception {
297 TmfEventField copy1 = new TmfEventField(fField1);
298 TmfEventField copy2 = new TmfEventField(copy1);
299 assertTrue("equals", fField1.equals(copy1));
300 assertTrue("equals", copy1.equals(copy2));
301 assertTrue("equals", fField1.equals(copy2));
302
303 copy1 = new TmfEventField(fField2);
304 copy2 = new TmfEventField(copy1);
305 assertTrue("equals", fField2.equals(copy1));
306 assertTrue("equals", copy1.equals(copy2));
307 assertTrue("equals", fField2.equals(copy2));
308 }
309
310 public void testEquals() throws Exception {
311 assertTrue("equals", fStructTerminalField1.equals(fStructTerminalField3));
312 assertTrue("equals", fStructTerminalField3.equals(fStructTerminalField1));
313
314 assertFalse("equals", fStructTerminalField1.equals(fField3));
315 assertFalse("equals", fField3.equals(fStructTerminalField1));
316 }
317
318 public void testEqualsNull() throws Exception {
319 assertFalse("equals", fField1.equals(null));
320 assertFalse("equals", fField2.equals(null));
321 }
322
323 public void testNonEqualClasses() throws Exception {
324 assertFalse("equals", fField1.equals(fStructTerminalField1));
325 assertFalse("equals", fField1.equals(fValue1));
326 }
327
328 public void testNonEqualValues() throws Exception {
329 TmfEventField copy1 = new TmfEventField(fFieldName1, fValue1);
330 TmfEventField copy2 = new TmfEventField(fFieldName1, fValue1);
331 assertTrue("equals", copy1.equals(copy2));
332 assertTrue("equals", copy2.equals(copy1));
333
334 copy2 = new TmfEventField(fFieldName1, fValue2);
335 assertFalse("equals", copy1.equals(copy2));
336 assertFalse("equals", copy2.equals(copy1));
337
338 copy2 = new TmfEventField(fFieldName1, null);
339 assertFalse("equals", copy1.equals(copy2));
340 assertFalse("equals", copy2.equals(copy1));
341 }
342
343 public void testNonEquals() throws Exception {
344 assertFalse("equals", fField1.equals(fField2));
345 assertFalse("equals", fField2.equals(fField1));
346
347 assertFalse("equals", fField1.equals(fStructTerminalField1));
348 }
349
350 // ------------------------------------------------------------------------
351 // toString
352 // ------------------------------------------------------------------------
353
354 public void testToString() {
355 String expected1 = "TmfEventField [fFieldId=" + fFieldName1 + ", fValue=" + fValue1.toString() + "]";
356 TmfEventField field = new TmfEventField(fFieldName1, fValue1, null);
357 assertEquals("toString", expected1, field.toString());
358
359 String expected2 = "TmfEventField [fFieldId=" + fFieldName1 + ", fValue=" + fValue2.toString() + "]";
360 field = new TmfEventField(fFieldName1, fValue2, null);
361 assertEquals("toString", expected2, field.toString());
362 }
363
364 // ------------------------------------------------------------------------
365 // makeRoot
366 // ------------------------------------------------------------------------
367
368 public void testMakeRoot() {
369 ITmfEventField root = TmfEventField.makeRoot(fStructFieldNames);
370 String[] names = root.getFieldNames();
371 assertEquals("getFieldNames length", 2, names.length);
372 for (int i = 0; i < names.length; i++) {
373 assertSame("getFieldNames", fStructFieldNames[i], names[i]);
374 assertSame("getFieldName", fStructFieldNames[i], root.getFieldName(i));
375 assertNull("getValue", root.getField(i).getValue());
376 }
377 assertNull("getFieldName", root.getFieldName(-1));
378 assertNull("getFieldName", root.getFieldName(names.length));
379
380 root = TmfEventField.makeRoot(fFieldNames);
381 names = root.getFieldNames();
382 assertEquals("getFieldNames length", 2, names.length);
383 for (int i = 0; i < names.length; i++) {
384 assertSame("getFieldNames", fFieldNames[i], names[i]);
385 assertSame("getFieldName", fFieldNames[i], root.getFieldName(i));
386 assertNull("getValue", root.getField(i).getValue());
387 }
388 assertNull("getFieldName", root.getFieldName(-1));
389 assertNull("getFieldName", root.getFieldName(names.length));
390 }
391
392 }
This page took 0.03999 seconds and 5 git commands to generate.