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
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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 * Matthew Khouzam - Initial generation with CodePro tools
11 * Alexandre Montplaisir - Clean up, consolidate redundant tests
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ctf.core.tests;
15
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;
22
23 import java.util.Set;
24
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;
37
38 /**
39 * The class <code>CtfTmfEventTest</code> contains tests for the class
40 * <code>{@link CtfTmfEvent}</code>.
41 *
42 * @author ematkho
43 * @version $Revision: 1.0 $
44 */
45 public class CtfTmfEventTest {
46
47 private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL;
48
49 private static CtfTmfEvent nullEvent;
50 private CtfTmfEvent fixture;
51
52 /**
53 * Test class initialization
54 */
55 @BeforeClass
56 public static void initialize() {
57 nullEvent = CtfTmfEventFactory.getNullEvent();
58 }
59
60 /**
61 * Perform pre-test initialization.
62 * @throws CTFReaderException error
63 */
64 @Before
65 public void setUp() throws CTFReaderException {
66 assumeTrue(testTrace.exists());
67 CtfTmfTrace trace = testTrace.getTrace();
68 try (CtfIterator tr = new CtfIterator(trace);) {
69 tr.advance();
70 fixture = tr.getCurrentEvent();
71 }
72 }
73
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() {
87 int result = nullEvent.getCPU();
88 assertEquals(-1, result);
89 }
90
91 /**
92 * Run the String getEventName() method test.
93 */
94 @Test
95 public void testGetEventName() {
96 String result = nullEvent.getType().getName();
97 assertEquals("Empty CTF event", result);
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() {
114 String fieldName = "pid";
115 ITmfEventField result = fixture.getContent().getField(fieldName);
116
117 assertNotNull(result);
118 assertNotNull(result.getValue());
119 }
120
121 /**
122 * Run the HashMap<String, CTFEventField> getFields() method test.
123 */
124 @Test
125 public void testGetFields() {
126 ITmfEventField[] fields = nullEvent.getContent().getFields();
127 ITmfEventField[] fields2 = new ITmfEventField[0];
128 assertArrayEquals(fields2, fields);
129 }
130
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
151 /**
152 * Run the long getID() method test.
153 */
154 @Test
155 public void testGetID() {
156 long result = nullEvent.getID();
157 assertEquals(-1L, result);
158 }
159
160 /**
161 * Run the long getTimestamp() method test.
162 */
163 @Test
164 public void testGetTimestamp() {
165 long result = nullEvent.getTimestamp().getValue();
166 assertEquals(-1L, result);
167 }
168
169 /**
170 * Test the getters for the reference, source and type.
171 */
172 @Test
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());
184 }
185
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
195 String attrib = fixture.getCustomAttribute("bozo");
196 assertNull(attrib);
197 }
198
199 /**
200 * Test the toString() method
201 */
202 @Test
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);
206 }
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());
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());
223 }
224 }
This page took 0.034944 seconds and 5 git commands to generate.