ctf: Make CTFTrace and its reader classes AutoCloseable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ctf.core.tests / src / org / eclipse / linuxtools / tmf / ctf / core / tests / CtfTmfEventTest.java
CommitLineData
95bf10e7 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2012, 2014 Ericsson
95bf10e7
AM
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 * Matthew Khouzam - Initial generation with CodePro tools
11 * Alexandre Montplaisir - Clean up, consolidate redundant tests
12 *******************************************************************************/
13
91e7f946 14package org.eclipse.linuxtools.tmf.ctf.core.tests;
a3fc8213 15
b4f71e4a
FC
16import static org.junit.Assert.assertArrayEquals;
17import static org.junit.Assert.assertEquals;
18import static org.junit.Assert.assertNotNull;
8e964be1 19import static org.junit.Assert.assertNull;
6cfa0200 20import static org.junit.Assert.assertSame;
5dd1fa65 21import static org.junit.Assume.assumeTrue;
8e964be1
MK
22
23import java.util.Set;
a3fc8213 24
db8e8f7d 25import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
a3fc8213 26import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
81c8e6f7 27import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
6cfa0200 28import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
91e7f946
AM
29import org.eclipse.linuxtools.tmf.ctf.core.CtfIterator;
30import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfEvent;
31import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfEventFactory;
32import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace;
33import org.eclipse.linuxtools.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
a3fc8213 34import org.junit.Before;
6cfa0200 35import org.junit.BeforeClass;
a3fc8213
AM
36import org.junit.Test;
37
38/**
95bf10e7
AM
39 * The class <code>CtfTmfEventTest</code> contains tests for the class
40 * <code>{@link CtfTmfEvent}</code>.
a3fc8213
AM
41 *
42 * @author ematkho
43 * @version $Revision: 1.0 $
44 */
45public class CtfTmfEventTest {
46
9ac63b5b 47 private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL;
5dd1fa65 48
6cfa0200 49 private static CtfTmfEvent nullEvent;
a3fc8213
AM
50 private CtfTmfEvent fixture;
51
6cfa0200
AM
52 /**
53 * Test class initialization
54 */
55 @BeforeClass
56 public static void initialize() {
57 nullEvent = CtfTmfEventFactory.getNullEvent();
58 }
59
a3fc8213
AM
60 /**
61 * Perform pre-test initialization.
db8e8f7d 62 * @throws CTFReaderException error
a3fc8213
AM
63 */
64 @Before
db8e8f7d 65 public void setUp() throws CTFReaderException {
9ac63b5b
AM
66 assumeTrue(testTrace.exists());
67 CtfTmfTrace trace = testTrace.getTrace();
dd9752d5
AM
68 try (CtfIterator tr = new CtfIterator(trace);) {
69 tr.advance();
70 fixture = tr.getCurrentEvent();
71 }
a3fc8213
AM
72 }
73
a3fc8213
AM
74 /**
75 * Run the CTFEvent(EventDefinition,StreamInputReader) constructor test.
76 */
77 @Test
78 public void testCTFEvent_read() {
79 assertNotNull(fixture);
80 }
81
82 /**
83 * Run the int getCPU() method test.
84 */
85 @Test
86 public void testGetCPU() {
a3fc8213 87 int result = nullEvent.getCPU();
a3fc8213
AM
88 assertEquals(-1, result);
89 }
90
a3fc8213
AM
91 /**
92 * Run the String getEventName() method test.
93 */
94 @Test
95 public void testGetEventName() {
33803b9b 96 String result = nullEvent.getType().getName();
cad06250 97 assertEquals("Empty CTF event", result);
a3fc8213
AM
98 }
99
100 /**
101 * Run the ArrayList<String> getFieldNames() method test.
102 */
103 @Test
104 public void testGetFieldNames() {
105 String[] result = fixture.getContent().getFieldNames();
106 assertNotNull(result);
107 }
108
109 /**
110 * Run the Object getFieldValue(String) method test.
111 */
112 @Test
113 public void testGetFieldValue() {
cad06250 114 String fieldName = "pid";
788ddcbc 115 ITmfEventField result = fixture.getContent().getField(fieldName);
a3fc8213
AM
116
117 assertNotNull(result);
788ddcbc 118 assertNotNull(result.getValue());
a3fc8213
AM
119 }
120
121 /**
122 * Run the HashMap<String, CTFEventField> getFields() method test.
123 */
124 @Test
125 public void testGetFields() {
a3fc8213 126 ITmfEventField[] fields = nullEvent.getContent().getFields();
81c8e6f7
MK
127 ITmfEventField[] fields2 = new ITmfEventField[0];
128 assertArrayEquals(fields2, fields);
a3fc8213
AM
129 }
130
6c204912
GB
131 /**
132 * Run the ITmfEventField getSubFieldValue(String[]) method test.
133 */
134 @Test
135 public void testGetSubFieldValue() {
136 /* Field exists */
137 String[] names = { "pid" };
138 assertNotNull(fixture.getContent().getSubField(names));
139
140 /* First field exists, not the second */
141 String[] names2 = { "pid", "abcd" };
142 assertNull(fixture.getContent().getSubField(names2));
143
144 /* Both field do not exist */
145 String[] names3 = { "pfid", "abcd" };
146 assertNull(fixture.getContent().getSubField(names3));
147
148 /* TODO Missing case of embedded field, need event for it */
149 }
150
a3fc8213
AM
151 /**
152 * Run the long getID() method test.
153 */
154 @Test
155 public void testGetID() {
a3fc8213 156 long result = nullEvent.getID();
a3fc8213
AM
157 assertEquals(-1L, result);
158 }
159
a3fc8213
AM
160 /**
161 * Run the long getTimestamp() method test.
a3fc8213
AM
162 */
163 @Test
164 public void testGetTimestamp() {
58f3bc52 165 long result = nullEvent.getTimestamp().getValue();
a3fc8213
AM
166 assertEquals(-1L, result);
167 }
81c8e6f7 168
95bf10e7 169 /**
6cfa0200 170 * Test the getters for the reference, source and type.
95bf10e7 171 */
81c8e6f7 172 @Test
95bf10e7 173 public void testGetters() {
81c8e6f7
MK
174 long rank = fixture.getRank();
175 CtfTmfTrace trace = fixture.getTrace();
81c8e6f7
MK
176 String reference = fixture.getReference();
177 String source = fixture.getSource();
178 ITmfEventType type = fixture.getType();
5dd1fa65 179 assertEquals(ITmfContext.UNKNOWN_RANK, rank);
05383968 180 assertEquals("kernel", trace.getName());
cad06250
AM
181 assertEquals("channel0_1", reference);
182 assertEquals("1", source);
183 assertEquals("lttng_statedump_vm_map", type.toString());
81c8e6f7
MK
184 }
185
8e964be1
MK
186 /**
187 * Test the custom CTF attributes methods. The test trace doesn't have any,
188 * so the list of attributes should be empty.
189 */
190 @Test
191 public void testCustomAttributes() {
192 Set<String> attributes = fixture.listCustomAttributes();
193 assertEquals(0, attributes.size());
194
cad06250 195 String attrib = fixture.getCustomAttribute("bozo");
8e964be1
MK
196 assertNull(attrib);
197 }
198
95bf10e7
AM
199 /**
200 * Test the toString() method
201 */
81c8e6f7 202 @Test
0879b6b9 203 public void testToString() {
81c8e6f7 204 String s = fixture.getContent().toString();
cad06250 205 assertEquals("pid=1922, start=0xb73ea000, end=0xb73ec000, flags=0x8000075, inode=917738, pgoff=0", s);
81c8e6f7 206 }
6cfa0200
AM
207
208 /**
209 * Test the {@link CtfTmfEventFactory#getNullEvent()} method, and the
210 * nullEvent's values.
211 */
212 @Test
213 public void testNullEvent() {
214 CtfTmfEvent nullEvent2 = CtfTmfEventFactory.getNullEvent();
215 assertSame(nullEvent2, nullEvent);
216 assertNotNull(nullEvent);
217 assertEquals(-1, nullEvent.getCPU());
33803b9b 218 assertEquals("Empty CTF event", nullEvent.getType().getName());
cad06250 219 assertEquals("No stream", nullEvent.getReference());
6cfa0200
AM
220 assertArrayEquals(new ITmfEventField[0], nullEvent.getContent().getFields());
221 assertEquals(-1L, nullEvent.getID());
222 assertEquals(-1L, nullEvent.getTimestamp().getValue());
223 }
a3fc8213 224}
This page took 0.070446 seconds and 5 git commands to generate.