1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
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
10 * Matthew Khouzam - Initial generation with CodePro tools
11 * Alexandre Montplaisir - Clean up, consolidate redundant tests
12 * Patrick Tasse - Remove getSubField
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.event
;
17 import static org
.junit
.Assert
.assertEquals
;
18 import static org
.junit
.Assert
.assertNotNull
;
19 import static org
.junit
.Assert
.assertNull
;
20 import static org
.junit
.Assert
.assertSame
;
21 import static org
.junit
.Assume
.assumeTrue
;
23 import java
.util
.Collection
;
26 import org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.trace
.iterator
.CtfIterator
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
30 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEvent
;
31 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEventFactory
;
32 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
33 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
34 import org
.junit
.Before
;
35 import org
.junit
.Test
;
38 * The class <code>CtfTmfEventTest</code> contains tests for the class
39 * <code>{@link CtfTmfEvent}</code>.
42 * @version $Revision: 1.0 $
44 public class CtfTmfEventTest
{
46 private static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.KERNEL
;
48 private static CtfTmfEvent nullEvent
;
49 private CtfTmfEvent fixture
;
52 * Perform pre-test initialization.
56 assumeTrue(testTrace
.exists());
57 try (CtfTmfTrace trace
= testTrace
.getTrace();
58 CtfIterator tr
= (CtfIterator
) trace
.createIterator();) {
60 fixture
= tr
.getCurrentEvent();
61 nullEvent
= CtfTmfEventFactory
.getNullEvent(trace
);
66 * Run the CTFEvent(EventDefinition,StreamInputReader) constructor test.
69 public void testCTFEvent_read() {
70 assertNotNull(fixture
);
74 * Run the int getCPU() method test.
77 public void testGetCPU() {
78 int result
= nullEvent
.getCPU();
79 assertEquals(-1, result
);
83 * Run the String getEventName() method test.
86 public void testGetEventName() {
87 String result
= nullEvent
.getType().getName();
88 assertEquals("Empty CTF event", result
);
92 * Run the ArrayList<String> getFieldNames() method test.
95 public void testGetFieldNames() {
96 Collection
<String
> result
= fixture
.getContent().getFieldNames();
97 assertNotNull(result
);
101 * Run the Object getFieldValue(String) method test.
104 public void testGetFieldValue() {
105 String fieldName
= "pid";
106 ITmfEventField result
= fixture
.getContent().getField(fieldName
);
108 assertNotNull(result
);
109 assertNotNull(result
.getValue());
113 * Run the HashMap<String, CTFEventField> getFields() method test.
116 public void testGetFields() {
117 Collection
<?
extends ITmfEventField
> fields
= nullEvent
.getContent().getFields();
118 assertEquals(0, fields
.size());
122 * Run the ITmfEventField getSubFieldValue(String[]) method test.
125 public void testGetSubFieldValue() {
127 String
[] names
= { "pid" };
128 assertNotNull(fixture
.getContent().getField(names
));
130 /* First field exists, not the second */
131 String
[] names2
= { "pid", "abcd" };
132 assertNull(fixture
.getContent().getField(names2
));
134 /* Both field do not exist */
135 String
[] names3
= { "pfid", "abcd" };
136 assertNull(fixture
.getContent().getField(names3
));
138 /* TODO Missing case of embedded field, need event for it */
142 * Run the long getID() method test.
145 public void testGetID() {
146 long result
= nullEvent
.getID();
147 assertEquals(-1L, result
);
151 * Run the long getTimestamp() method test.
154 public void testGetTimestamp() {
155 long result
= nullEvent
.getTimestamp().getValue();
156 assertEquals(-1L, result
);
160 * Test the getters for the reference, source and type.
163 public void testGetters() {
164 long rank
= fixture
.getRank();
165 try (CtfTmfTrace trace
= fixture
.getTrace();) {
166 assertEquals("kernel", trace
.getName());
168 String reference
= fixture
.getReference();
169 int cpu
= fixture
.getCPU();
170 ITmfEventType type
= fixture
.getType();
171 assertEquals(ITmfContext
.UNKNOWN_RANK
, rank
);
173 assertEquals("channel0_1", reference
);
174 assertEquals(1, cpu
);
175 assertEquals("lttng_statedump_vm_map", type
.toString());
179 * Test the custom CTF attributes methods. The test trace doesn't have any,
180 * so the list of attributes should be empty.
183 public void testCustomAttributes() {
184 Set
<String
> attributes
= fixture
.listCustomAttributes();
185 assertEquals(0, attributes
.size());
187 String attrib
= fixture
.getCustomAttribute("bozo");
192 * Test the toString() method
195 public void testToString() {
196 String s
= fixture
.getContent().toString();
197 assertEquals("pid=1922, start=0xb73ea000, end=0xb73ec000, flags=0x8000075, inode=917738, pgoff=0", s
);
201 * Test the {@link CtfTmfEventFactory#getNullEvent(CtfTmfTrace)} method, and
202 * the nullEvent's values.
205 public void testNullEvent() {
206 CtfTmfEvent nullEvent2
= CtfTmfEventFactory
.getNullEvent(fixture
.getTrace());
207 assertSame(nullEvent2
, nullEvent
);
208 assertNotNull(nullEvent
);
209 assertEquals(-1, nullEvent
.getCPU());
210 assertEquals("Empty CTF event", nullEvent
.getType().getName());
211 assertNull(nullEvent
.getReference());
212 assertEquals(0, nullEvent
.getContent().getFields().size());
213 assertEquals(-1L, nullEvent
.getID());
214 assertEquals(-1L, nullEvent
.getTimestamp().getValue());