lttng: Port unit tests to JUnit4
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.core.tests / src / org / eclipse / linuxtools / lttng2 / core / tests / control / model / impl / BaseEventInfoTest.java
CommitLineData
eb1bab5b 1/**********************************************************************
2ba3d0a1 2 * Copyright (c) 2012, 2013 Ericsson
b0318660 3 *
eb1bab5b
BH
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
b0318660
AM
8 *
9 * Contributors:
eb1bab5b 10 * Bernd Hufmann - Initial API and implementation
2ba3d0a1 11 * Alexandre Montplaisir - Port to JUnit4
eb1bab5b 12 **********************************************************************/
2ba3d0a1 13
9315aeee 14package org.eclipse.linuxtools.lttng2.core.tests.control.model.impl;
eb1bab5b 15
2ba3d0a1
AM
16import static org.junit.Assert.*;
17
d4514365
BH
18import java.util.LinkedList;
19import java.util.List;
20
9315aeee 21import org.eclipse.linuxtools.internal.lttng2.core.control.model.IBaseEventInfo;
d4514365 22import org.eclipse.linuxtools.internal.lttng2.core.control.model.IFieldInfo;
9315aeee
BH
23import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceEventType;
24import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceLogLevel;
25import org.eclipse.linuxtools.internal.lttng2.core.control.model.impl.BaseEventInfo;
d4514365 26import org.eclipse.linuxtools.internal.lttng2.core.control.model.impl.FieldInfo;
2ba3d0a1
AM
27import org.junit.Before;
28import org.junit.Test;
eb1bab5b
BH
29
30/**
2ba3d0a1
AM
31 * The class <code>BaseEventInfoTest</code> contains test for the class
32 * <code>{@link BaseEventInfo}</code>.
eb1bab5b 33 */
2ba3d0a1
AM
34@SuppressWarnings("nls")
35public class BaseEventInfoTest {
eb1bab5b
BH
36
37 // ------------------------------------------------------------------------
38 // Test data
39 // ------------------------------------------------------------------------
2ba3d0a1 40
eb1bab5b
BH
41 private IBaseEventInfo fEventInfo1 = null;
42 private IBaseEventInfo fEventInfo2 = null;
b0318660 43
eb1bab5b
BH
44 // ------------------------------------------------------------------------
45 // Housekeeping
46 // ------------------------------------------------------------------------
47 /**
48 * Perform pre-test initialization.
eb1bab5b 49 */
2ba3d0a1
AM
50 @Before
51 public void setUp() {
b0318660 52 ModelImplFactory factory = new ModelImplFactory();
eb1bab5b
BH
53 fEventInfo1 = factory.getBaseEventInfo1();
54 fEventInfo2 = factory.getBaseEventInfo2();
55 }
56
eb1bab5b
BH
57 // ------------------------------------------------------------------------
58 // Tests
59 // ------------------------------------------------------------------------
60
61 /**
62 * Run the BaseEventInfo() constructor test.
eb1bab5b 63 */
2ba3d0a1 64 @Test
eb1bab5b
BH
65 public void testBaseEventInfo() {
66 BaseEventInfo fixture = new BaseEventInfo("event");
67 assertNotNull(fixture);
b0318660 68
eb1bab5b 69 TraceEventType result = fixture.getEventType();
b0318660 70
eb1bab5b
BH
71 assertEquals("event", fixture.getName());
72 assertEquals("unknown", result.getInName());
73 assertEquals("UNKNOWN", result.name());
74 assertEquals("UNKNOWN", result.toString());
ccc66d01 75 assertEquals(3, result.ordinal());
b0318660 76
eb1bab5b 77 TraceLogLevel level = fixture.getLogLevel();
4775bcbf
BH
78 assertEquals("TRACE_DEBUG", level.getInName());
79 assertEquals("TRACE_DEBUG", level.name());
80 assertEquals("TRACE_DEBUG", level.toString());
81 assertEquals(14, level.ordinal());
eb1bab5b
BH
82 }
83
84 /**
85 * Test Copy Constructor
86 */
2ba3d0a1 87 @Test
eb1bab5b
BH
88 public void testEventInfoCopy() {
89 BaseEventInfo info = new BaseEventInfo((BaseEventInfo)fEventInfo1);
b0318660 90
eb1bab5b
BH
91 assertEquals(fEventInfo1.getName(), info.getName());
92 assertEquals(fEventInfo1.getEventType(), info.getEventType());
93 assertEquals(fEventInfo1.getLogLevel(), info.getLogLevel());
d4514365
BH
94 assertEquals(fEventInfo1.getFilterExpression(), info.getFilterExpression());
95
96 IFieldInfo[] orignalFields = fEventInfo1.getFields();
97 IFieldInfo[] copiedFields = info.getFields();
98 assertEquals(orignalFields.length, copiedFields.length);
99
100 for (int i = 0; i < copiedFields.length; i++) {
101 assertEquals(orignalFields[i], copiedFields[i]);
102 }
eb1bab5b
BH
103 }
104
105 /**
106 * Test Copy Constructor
107 */
2ba3d0a1 108 @Test
eb1bab5b
BH
109 public void testEventCopy2() {
110 try {
111 BaseEventInfo info = null;
112 new BaseEventInfo(info);
113 fail("null copy");
114 }
115 catch (IllegalArgumentException e) {
116 // Success
117 }
118 }
b0318660 119
eb1bab5b
BH
120 /**
121 * Run the TraceEventType getEventType() method test.
eb1bab5b 122 */
2ba3d0a1 123 @Test
eb1bab5b
BH
124 public void testGetEventType_1() {
125 BaseEventInfo fixture = new BaseEventInfo("event");
126 fixture.setEventType("unknown");
127
128 TraceEventType result = fixture.getEventType();
129
130 assertNotNull(result);
131 assertEquals("unknown", result.getInName());
132 assertEquals("UNKNOWN", result.name());
133 assertEquals("UNKNOWN", result.toString());
ccc66d01 134 assertEquals(3, result.ordinal());
b0318660 135
eb1bab5b
BH
136 fixture.setEventType("");
137 result = fixture.getEventType();
138 assertEquals("unknown", result.getInName());
139 assertEquals("UNKNOWN", result.name());
140 assertEquals("UNKNOWN", result.toString());
ccc66d01 141 assertEquals(3, result.ordinal());
b0318660 142
eb1bab5b
BH
143 fixture.setEventType("tracepoint");
144 result = fixture.getEventType();
145 assertNotNull(result);
146 assertEquals("tracepoint", result.getInName());
147 assertEquals("TRACEPOINT", result.name());
148 assertEquals("TRACEPOINT", result.toString());
149 assertEquals(0, result.ordinal());
b0318660 150
ccc66d01
BH
151 fixture.setEventType("syscall");
152 result = fixture.getEventType();
153 assertNotNull(result);
154 assertEquals("syscall", result.getInName());
155 assertEquals("SYSCALL", result.name());
156 assertEquals("SYSCALL", result.toString());
157 assertEquals(1, result.ordinal());
b0318660 158
ccc66d01
BH
159 fixture.setEventType("probe");
160 result = fixture.getEventType();
161 assertNotNull(result);
162 assertEquals("probe", result.getInName());
163 assertEquals("PROBE", result.name());
164 assertEquals("PROBE", result.toString());
165 assertEquals(2, result.ordinal());
166
eb1bab5b
BH
167 }
168
169 /**
170 * Run the void setEventType(TraceEventType) method test.
eb1bab5b 171 */
2ba3d0a1 172 @Test
eb1bab5b
BH
173 public void testSetEventType_2() {
174 BaseEventInfo fixture = new BaseEventInfo("event");
175 fixture.setEventType(TraceEventType.TRACEPOINT);
b0318660 176
eb1bab5b 177 TraceEventType result = fixture.getEventType();
b0318660 178
eb1bab5b
BH
179 assertNotNull(result);
180 assertEquals("tracepoint", result.getInName());
181 assertEquals("TRACEPOINT", result.name());
182 assertEquals("TRACEPOINT", result.toString());
183 assertEquals(0, result.ordinal());
b0318660 184
eb1bab5b
BH
185 fixture.setEventType(TraceEventType.UNKNOWN);
186 result = fixture.getEventType();
b0318660 187
eb1bab5b
BH
188 assertNotNull(result);
189 assertEquals("unknown", result.getInName());
190 assertEquals("UNKNOWN", result.name());
191 assertEquals("UNKNOWN", result.toString());
ccc66d01 192 assertEquals(3, result.ordinal());
b0318660 193
ccc66d01
BH
194 fixture.setEventType(TraceEventType.SYSCALL);
195 result = fixture.getEventType();
196 assertNotNull(result);
197 assertEquals("syscall", result.getInName());
198 assertEquals("SYSCALL", result.name());
199 assertEquals("SYSCALL", result.toString());
200 assertEquals(1, result.ordinal());
b0318660 201
ccc66d01
BH
202 fixture.setEventType(TraceEventType.PROBE);
203 result = fixture.getEventType();
204 assertNotNull(result);
205 assertEquals("probe", result.getInName());
206 assertEquals("PROBE", result.name());
207 assertEquals("PROBE", result.toString());
9d8a90ad 208 assertEquals(2, result.ordinal());
ccc66d01 209
eb1bab5b 210 }
b0318660 211
eb1bab5b
BH
212 /**
213 * Run the void setLogLevel(TraceLogLevel) method test.
214 * Run the TraceLogLevel getLogLevel() method test
eb1bab5b 215 */
2ba3d0a1 216 @Test
eb1bab5b
BH
217 public void testSetLogLevel1() {
218 BaseEventInfo fixture = new BaseEventInfo("event");
219 fixture.setEventType(TraceEventType.TRACEPOINT);
220 fixture.setLogLevel(TraceLogLevel.TRACE_CRIT);
b0318660 221
eb1bab5b
BH
222 // 2 set/get-operations are enough to test the method
223 TraceLogLevel result = fixture.getLogLevel();
224 assertNotNull(result);
4775bcbf 225 assertEquals("TRACE_CRIT", result.getInName());
eb1bab5b
BH
226 assertEquals("TRACE_CRIT", result.name());
227 assertEquals("TRACE_CRIT", result.toString());
228 assertEquals(2, result.ordinal());
229
4775bcbf 230 fixture.setLogLevel(TraceLogLevel.TRACE_DEBUG_FUNCTION);
b0318660 231
eb1bab5b
BH
232 result = fixture.getLogLevel();
233 assertNotNull(result);
4775bcbf
BH
234 assertEquals("TRACE_DEBUG_FUNCTION", result.getInName());
235 assertEquals("TRACE_DEBUG_FUNCTION", result.name());
236 assertEquals("TRACE_DEBUG_FUNCTION", result.toString());
eb1bab5b
BH
237 assertEquals(12, result.ordinal());
238 }
b0318660 239
eb1bab5b
BH
240 /**
241 * Run the void setLogLevel(String) method test.
242 * Run the TraceLogLevel getLogLevel() method test
eb1bab5b 243 */
2ba3d0a1 244 @Test
eb1bab5b
BH
245 public void testSetLogLevel2() {
246 BaseEventInfo fixture = new BaseEventInfo("event");
247 fixture.setEventType(TraceEventType.TRACEPOINT);
4775bcbf 248 fixture.setLogLevel("TRACE_EMERG");
b0318660 249
eb1bab5b
BH
250 TraceLogLevel result = fixture.getLogLevel();
251 assertNotNull(result);
4775bcbf 252 assertEquals("TRACE_EMERG", result.getInName());
eb1bab5b
BH
253 assertEquals("TRACE_EMERG", result.name());
254 assertEquals(0, result.ordinal());
255
256 //------------------------
4775bcbf 257 fixture.setLogLevel("TRACE_ALERT");
b0318660 258
eb1bab5b
BH
259 result = fixture.getLogLevel();
260 assertNotNull(result);
4775bcbf 261 assertEquals("TRACE_ALERT", result.getInName());
eb1bab5b
BH
262 assertEquals("TRACE_ALERT", result.name());
263 assertEquals(1, result.ordinal());
b0318660 264
eb1bab5b 265 //------------------------
4775bcbf 266 fixture.setLogLevel("TRACE_CRIT");
b0318660 267
eb1bab5b
BH
268 result = fixture.getLogLevel();
269 assertNotNull(result);
4775bcbf 270 assertEquals("TRACE_CRIT", result.getInName());
eb1bab5b
BH
271 assertEquals("TRACE_CRIT", result.name());
272 assertEquals(2, result.ordinal());
273
274 //------------------------
4775bcbf 275 fixture.setLogLevel("TRACE_ERR");
b0318660 276
eb1bab5b
BH
277 result = fixture.getLogLevel();
278 assertNotNull(result);
4775bcbf 279 assertEquals("TRACE_ERR", result.getInName());
eb1bab5b
BH
280 assertEquals("TRACE_ERR", result.name());
281 assertEquals(3, result.ordinal());
282
283 //------------------------
4775bcbf 284 fixture.setLogLevel("TRACE_WARNING");
b0318660 285
eb1bab5b
BH
286 result = fixture.getLogLevel();
287 assertNotNull(result);
4775bcbf 288 assertEquals("TRACE_WARNING", result.getInName());
eb1bab5b
BH
289 assertEquals("TRACE_WARNING", result.name());
290 assertEquals(4, result.ordinal());
291
292 //------------------------
4775bcbf 293 fixture.setLogLevel("TRACE_NOTICE");
b0318660 294
eb1bab5b
BH
295 result = fixture.getLogLevel();
296 assertNotNull(result);
4775bcbf 297 assertEquals("TRACE_NOTICE", result.getInName());
eb1bab5b
BH
298 assertEquals("TRACE_NOTICE", result.name());
299 assertEquals(5, result.ordinal());
300
301 //------------------------
4775bcbf 302 fixture.setLogLevel("TRACE_INFO");
b0318660 303
eb1bab5b
BH
304 result = fixture.getLogLevel();
305 assertNotNull(result);
4775bcbf 306 assertEquals("TRACE_INFO", result.getInName());
eb1bab5b
BH
307 assertEquals("TRACE_INFO", result.name());
308 assertEquals(6, result.ordinal());
309
310 //------------------------
4775bcbf 311 fixture.setLogLevel("TRACE_DEBUG_SYSTEM");
b0318660 312
eb1bab5b
BH
313 result = fixture.getLogLevel();
314 assertNotNull(result);
4775bcbf
BH
315 assertEquals("TRACE_DEBUG_SYSTEM", result.getInName());
316 assertEquals("TRACE_DEBUG_SYSTEM", result.name());
eb1bab5b
BH
317 assertEquals(7, result.ordinal());
318
319 //------------------------
4775bcbf 320 fixture.setLogLevel("TRACE_DEBUG_PROGRAM");
b0318660 321
eb1bab5b
BH
322 result = fixture.getLogLevel();
323 assertNotNull(result);
4775bcbf
BH
324 assertEquals("TRACE_DEBUG_PROGRAM", result.getInName());
325 assertEquals("TRACE_DEBUG_PROGRAM", result.name());
eb1bab5b
BH
326 assertEquals(8, result.ordinal());
327
328 //------------------------
4775bcbf 329 fixture.setLogLevel("TRACE_DEBUG_PROCESS");
b0318660 330
eb1bab5b
BH
331 result = fixture.getLogLevel();
332 assertNotNull(result);
4775bcbf
BH
333 assertEquals("TRACE_DEBUG_PROCESS", result.getInName());
334 assertEquals("TRACE_DEBUG_PROCESS", result.name());
eb1bab5b
BH
335 assertEquals(9, result.ordinal());
336
337 //------------------------
4775bcbf 338 fixture.setLogLevel("TRACE_DEBUG_MODULE");
b0318660 339
eb1bab5b
BH
340 result = fixture.getLogLevel();
341 assertNotNull(result);
4775bcbf
BH
342 assertEquals("TRACE_DEBUG_MODULE", result.getInName());
343 assertEquals("TRACE_DEBUG_MODULE", result.name());
eb1bab5b
BH
344 assertEquals(10, result.ordinal());
345
346 //------------------------
4775bcbf 347 fixture.setLogLevel("TRACE_DEBUG_UNIT");
b0318660 348
eb1bab5b
BH
349 result = fixture.getLogLevel();
350 assertNotNull(result);
4775bcbf
BH
351 assertEquals("TRACE_DEBUG_UNIT", result.getInName());
352 assertEquals("TRACE_DEBUG_UNIT", result.name());
eb1bab5b
BH
353 assertEquals(11, result.ordinal());
354
355 //------------------------
4775bcbf 356 fixture.setLogLevel("TRACE_DEBUG_FUNCTION");
b0318660 357
eb1bab5b
BH
358 result = fixture.getLogLevel();
359 assertNotNull(result);
4775bcbf
BH
360 assertEquals("TRACE_DEBUG_FUNCTION", result.getInName());
361 assertEquals("TRACE_DEBUG_FUNCTION", result.name());
eb1bab5b
BH
362 assertEquals(12, result.ordinal());
363
364 //------------------------
4775bcbf 365 fixture.setLogLevel("TRACE_DEBUG_LINE");
b0318660 366
eb1bab5b
BH
367 result = fixture.getLogLevel();
368 assertNotNull(result);
4775bcbf
BH
369 assertEquals("TRACE_DEBUG_LINE", result.getInName());
370 assertEquals("TRACE_DEBUG_LINE", result.name());
eb1bab5b
BH
371 assertEquals(13, result.ordinal());
372
373 //------------------------
4775bcbf 374 fixture.setLogLevel("TRACE_DEBUG");
b0318660 375
eb1bab5b
BH
376 result = fixture.getLogLevel();
377 assertNotNull(result);
4775bcbf
BH
378 assertEquals("TRACE_DEBUG", result.getInName());
379 assertEquals("TRACE_DEBUG", result.name());
eb1bab5b
BH
380 assertEquals(14, result.ordinal());
381
4775bcbf
BH
382 //-------------------------
383 fixture.setLogLevel("LEVEL_UNKNOWN");
b0318660 384
eb1bab5b
BH
385 result = fixture.getLogLevel();
386 assertNotNull(result);
4775bcbf
BH
387 assertEquals("LEVEL_UNKNOWN", result.getInName());
388 assertEquals("LEVEL_UNKNOWN", result.name());
eb1bab5b 389 assertEquals(15, result.ordinal());
b0318660 390
4775bcbf 391 fixture.setLogLevel("garbage");
b0318660 392
eb1bab5b
BH
393 result = fixture.getLogLevel();
394 assertNotNull(result);
4775bcbf
BH
395 assertEquals("TRACE_DEBUG", result.getInName());
396 assertEquals("TRACE_DEBUG", result.name());
397 assertEquals(14, result.ordinal());
eb1bab5b 398 }
b0318660 399
d4514365
BH
400 /**
401 * test filter expression
402 */
2ba3d0a1 403 @Test
d4514365
BH
404 public void testSetFields() {
405 BaseEventInfo info = new BaseEventInfo((BaseEventInfo)fEventInfo2);
406 info.setFilterExpression("stringfield==test");
407 assertEquals("stringfield==test", info.getFilterExpression());
408 }
409
410
411 /**
412 * test add field
413 */
2ba3d0a1 414 @Test
d4514365
BH
415 public void testAddField() {
416 BaseEventInfo info = new BaseEventInfo((BaseEventInfo)fEventInfo2);
417
418 IFieldInfo field = new FieldInfo("intfield");
419 field.setFieldType("int");
420
421 info.addField(field);
422
423 // Verify the stored events
424 IFieldInfo[] result = info.getFields();
425
426 assertNotNull(result);
427 assertEquals(1, result.length);
428 assertNotNull(result[0]);
429 assertTrue(field.equals(result[0]));
430 }
431
432 /**
433 * test set fields
434 */
2ba3d0a1 435 @Test
d4514365
BH
436 public void testFields() {
437 BaseEventInfo info = new BaseEventInfo((BaseEventInfo)fEventInfo2);
438
439 IFieldInfo field1 = new FieldInfo("intfield");
440 field1.setFieldType("int");
441
442 IFieldInfo field2 = new FieldInfo("stringfield");
443 field2.setFieldType("string");
444
445 List<IFieldInfo> fields = new LinkedList<IFieldInfo>();
446 fields.add(field1);
447 fields.add(field2);
448 info.setFields(fields);
449
450 // Verify the stored events
451 IFieldInfo[] result = info.getFields();
452
453 assertNotNull(result);
454 assertEquals(2, result.length);
455
456 for (int i = 0; i < result.length; i++) {
457 assertNotNull(result[i]);
458 assertTrue(fields.get(i).equals(result[i]));
459 }
460 }
eb1bab5b
BH
461
462 /**
463 * Run the String toString() method test.
eb1bab5b 464 */
2ba3d0a1 465 @Test
eb1bab5b
BH
466 public void testToString_1() {
467 BaseEventInfo fixture = new BaseEventInfo("event");
468 fixture.setName("testName");
469 fixture.setEventType(TraceEventType.TRACEPOINT);
470 fixture.setLogLevel(TraceLogLevel.TRACE_ERR);
471
472 String result = fixture.toString();
473
474 // add additional test code here
475 assertEquals("[BaseEventInfo([TraceInfo(Name=testName)],type=TRACEPOINT,level=TRACE_ERR)]", result);
476 }
477
478 // ------------------------------------------------------------------------
479 // equals
480 // ------------------------------------------------------------------------
481
2ba3d0a1
AM
482 /**
483 * Test the .equals() method.
484 */
485 @Test
eb1bab5b
BH
486 public void testEqualsReflexivity() {
487 assertTrue("equals", fEventInfo1.equals(fEventInfo1));
488 assertTrue("equals", fEventInfo2.equals(fEventInfo2));
489
490 assertTrue("equals", !fEventInfo1.equals(fEventInfo2));
491 assertTrue("equals", !fEventInfo2.equals(fEventInfo1));
492 }
b0318660 493
2ba3d0a1
AM
494 /**
495 * Test the .equals() method.
496 */
497 @Test
eb1bab5b
BH
498 public void testEqualsSymmetry() {
499 BaseEventInfo info1 = new BaseEventInfo((BaseEventInfo)fEventInfo1);
500 BaseEventInfo info2 = new BaseEventInfo((BaseEventInfo)fEventInfo2);
501
502 assertTrue("equals", info1.equals(fEventInfo1));
503 assertTrue("equals", fEventInfo1.equals(info1));
504
505 assertTrue("equals", info2.equals(fEventInfo2));
506 assertTrue("equals", fEventInfo2.equals(info2));
507 }
b0318660 508
2ba3d0a1
AM
509 /**
510 * Test the .equals() method.
511 */
512 @Test
eb1bab5b
BH
513 public void testEqualsTransivity() {
514 BaseEventInfo info1 = new BaseEventInfo((BaseEventInfo)fEventInfo1);
515 BaseEventInfo info2 = new BaseEventInfo((BaseEventInfo)fEventInfo1);
516 BaseEventInfo info3 = new BaseEventInfo((BaseEventInfo)fEventInfo1);
517
518 assertTrue("equals", info1.equals(info2));
519 assertTrue("equals", info2.equals(info3));
520 assertTrue("equals", info1.equals(info3));
521 }
b0318660 522
2ba3d0a1
AM
523 /**
524 * Test the .equals() method.
525 */
526 @Test
eb1bab5b
BH
527 public void testEqualsNull() {
528 assertTrue("equals", !fEventInfo1.equals(null));
529 assertTrue("equals", !fEventInfo2.equals(null));
530 }
b0318660 531
eb1bab5b
BH
532 // ------------------------------------------------------------------------
533 // hashCode
534 // ------------------------------------------------------------------------
535
2ba3d0a1
AM
536 /**
537 * Test the hashCode() method.
538 */
539 @Test
eb1bab5b
BH
540 public void testHashCode() {
541 BaseEventInfo info1 = new BaseEventInfo((BaseEventInfo)fEventInfo1);
542 BaseEventInfo info2 = new BaseEventInfo((BaseEventInfo)fEventInfo2);
543
544 assertTrue("hashCode", fEventInfo1.hashCode() == info1.hashCode());
545 assertTrue("hashCode", fEventInfo2.hashCode() == info2.hashCode());
546
547 assertTrue("hashCode", fEventInfo1.hashCode() != info2.hashCode());
548 assertTrue("hashCode", fEventInfo2.hashCode() != info1.hashCode());
549 }
550}
This page took 0.092149 seconds and 5 git commands to generate.