1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ctf
.core
.tests
;
16 import static org
.junit
.Assert
.assertArrayEquals
;
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
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
29 import org
.eclipse
.linuxtools
.tmf
.ctf
.core
.CtfIterator
;
30 import org
.eclipse
.linuxtools
.tmf
.ctf
.core
.CtfTmfEvent
;
31 import org
.eclipse
.linuxtools
.tmf
.ctf
.core
.CtfTmfEventFactory
;
32 import org
.eclipse
.linuxtools
.tmf
.ctf
.core
.CtfTmfTrace
;
33 import org
.eclipse
.linuxtools
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
34 import org
.junit
.Before
;
35 import org
.junit
.BeforeClass
;
36 import org
.junit
.Test
;
39 * The class <code>CtfTmfEventTest</code> contains tests for the class
40 * <code>{@link CtfTmfEvent}</code>.
43 * @version $Revision: 1.0 $
45 public class CtfTmfEventTest
{
47 private static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.KERNEL
;
49 private static CtfTmfEvent nullEvent
;
50 private CtfTmfEvent fixture
;
53 * Test class initialization
56 public static void initialize() {
57 nullEvent
= CtfTmfEventFactory
.getNullEvent();
61 * Perform pre-test initialization.
62 * @throws CTFReaderException error
65 public void setUp() throws CTFReaderException
{
66 assumeTrue(testTrace
.exists());
67 CtfTmfTrace trace
= testTrace
.getTrace();
68 try (CtfIterator tr
= new CtfIterator(trace
);) {
70 fixture
= tr
.getCurrentEvent();
75 * Run the CTFEvent(EventDefinition,StreamInputReader) constructor test.
78 public void testCTFEvent_read() {
79 assertNotNull(fixture
);
83 * Run the int getCPU() method test.
86 public void testGetCPU() {
87 int result
= nullEvent
.getCPU();
88 assertEquals(-1, result
);
92 * Run the String getEventName() method test.
95 public void testGetEventName() {
96 String result
= nullEvent
.getType().getName();
97 assertEquals("Empty CTF event", result
);
101 * Run the ArrayList<String> getFieldNames() method test.
104 public void testGetFieldNames() {
105 String
[] result
= fixture
.getContent().getFieldNames();
106 assertNotNull(result
);
110 * Run the Object getFieldValue(String) method test.
113 public void testGetFieldValue() {
114 String fieldName
= "pid";
115 ITmfEventField result
= fixture
.getContent().getField(fieldName
);
117 assertNotNull(result
);
118 assertNotNull(result
.getValue());
122 * Run the HashMap<String, CTFEventField> getFields() method test.
125 public void testGetFields() {
126 ITmfEventField
[] fields
= nullEvent
.getContent().getFields();
127 ITmfEventField
[] fields2
= new ITmfEventField
[0];
128 assertArrayEquals(fields2
, fields
);
132 * Run the ITmfEventField getSubFieldValue(String[]) method test.
135 public void testGetSubFieldValue() {
137 String
[] names
= { "pid" };
138 assertNotNull(fixture
.getContent().getSubField(names
));
140 /* First field exists, not the second */
141 String
[] names2
= { "pid", "abcd" };
142 assertNull(fixture
.getContent().getSubField(names2
));
144 /* Both field do not exist */
145 String
[] names3
= { "pfid", "abcd" };
146 assertNull(fixture
.getContent().getSubField(names3
));
148 /* TODO Missing case of embedded field, need event for it */
152 * Run the long getID() method test.
155 public void testGetID() {
156 long result
= nullEvent
.getID();
157 assertEquals(-1L, result
);
161 * Run the long getTimestamp() method test.
164 public void testGetTimestamp() {
165 long result
= nullEvent
.getTimestamp().getValue();
166 assertEquals(-1L, result
);
170 * Test the getters for the reference, source and type.
173 public void testGetters() {
174 long rank
= fixture
.getRank();
175 CtfTmfTrace trace
= fixture
.getTrace();
176 String reference
= fixture
.getReference();
177 String source
= fixture
.getSource();
178 ITmfEventType type
= fixture
.getType();
179 assertEquals(ITmfContext
.UNKNOWN_RANK
, rank
);
180 assertEquals("kernel", trace
.getName());
181 assertEquals("channel0_1", reference
);
182 assertEquals("1", source
);
183 assertEquals("lttng_statedump_vm_map", type
.toString());
187 * Test the custom CTF attributes methods. The test trace doesn't have any,
188 * so the list of attributes should be empty.
191 public void testCustomAttributes() {
192 Set
<String
> attributes
= fixture
.listCustomAttributes();
193 assertEquals(0, attributes
.size());
195 String attrib
= fixture
.getCustomAttribute("bozo");
200 * Test the toString() method
203 public void testToString() {
204 String s
= fixture
.getContent().toString();
205 assertEquals("pid=1922, start=0xb73ea000, end=0xb73ec000, flags=0x8000075, inode=917738, pgoff=0", s
);
209 * Test the {@link CtfTmfEventFactory#getNullEvent()} method, and the
210 * nullEvent's values.
213 public void testNullEvent() {
214 CtfTmfEvent nullEvent2
= CtfTmfEventFactory
.getNullEvent();
215 assertSame(nullEvent2
, nullEvent
);
216 assertNotNull(nullEvent
);
217 assertEquals(-1, nullEvent
.getCPU());
218 assertEquals("Empty CTF event", nullEvent
.getType().getName());
219 assertEquals("No stream", nullEvent
.getReference());
220 assertArrayEquals(new ITmfEventField
[0], nullEvent
.getContent().getFields());
221 assertEquals(-1L, nullEvent
.getID());
222 assertEquals(-1L, nullEvent
.getTimestamp().getValue());