ctf: Remove AutoCloseable from CtfTmfTrace
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / event / CtfTmfEventTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 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 * Patrick Tasse - Remove getSubField
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.ctf.core.tests.event;
16
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.Collection;
24 import java.util.Set;
25
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;
36
37 /**
38 * The class <code>CtfTmfEventTest</code> contains tests for the class
39 * <code>{@link CtfTmfEvent}</code>.
40 *
41 * @author ematkho
42 * @version $Revision: 1.0 $
43 */
44 public class CtfTmfEventTest {
45
46 private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL;
47
48 private static CtfTmfEvent nullEvent;
49 private CtfTmfEvent fixture;
50
51 /**
52 * Perform pre-test initialization.
53 */
54 @Before
55 public void setUp() {
56 assumeTrue(testTrace.exists());
57 CtfTmfTrace trace = testTrace.getTrace();
58 try (CtfIterator tr = (CtfIterator) trace.createIterator();) {
59 tr.advance();
60 fixture = tr.getCurrentEvent();
61 nullEvent = CtfTmfEventFactory.getNullEvent(trace);
62 }
63 trace.dispose();
64 }
65
66 /**
67 * Run the CTFEvent(EventDefinition,StreamInputReader) constructor test.
68 */
69 @Test
70 public void testCTFEvent_read() {
71 assertNotNull(fixture);
72 }
73
74 /**
75 * Run the int getCPU() method test.
76 */
77 @Test
78 public void testGetCPU() {
79 int result = nullEvent.getCPU();
80 assertEquals(-1, result);
81 }
82
83 /**
84 * Run the String getEventName() method test.
85 */
86 @Test
87 public void testGetEventName() {
88 String result = nullEvent.getType().getName();
89 assertEquals("Empty CTF event", result);
90 }
91
92 /**
93 * Run the ArrayList<String> getFieldNames() method test.
94 */
95 @Test
96 public void testGetFieldNames() {
97 Collection<String> result = fixture.getContent().getFieldNames();
98 assertNotNull(result);
99 }
100
101 /**
102 * Run the Object getFieldValue(String) method test.
103 */
104 @Test
105 public void testGetFieldValue() {
106 String fieldName = "pid";
107 ITmfEventField result = fixture.getContent().getField(fieldName);
108
109 assertNotNull(result);
110 assertNotNull(result.getValue());
111 }
112
113 /**
114 * Run the HashMap<String, CTFEventField> getFields() method test.
115 */
116 @Test
117 public void testGetFields() {
118 Collection<? extends ITmfEventField> fields = nullEvent.getContent().getFields();
119 assertEquals(0, fields.size());
120 }
121
122 /**
123 * Run the ITmfEventField getSubFieldValue(String[]) method test.
124 */
125 @Test
126 public void testGetSubFieldValue() {
127 /* Field exists */
128 String[] names = { "pid" };
129 assertNotNull(fixture.getContent().getField(names));
130
131 /* First field exists, not the second */
132 String[] names2 = { "pid", "abcd" };
133 assertNull(fixture.getContent().getField(names2));
134
135 /* Both field do not exist */
136 String[] names3 = { "pfid", "abcd" };
137 assertNull(fixture.getContent().getField(names3));
138
139 /* TODO Missing case of embedded field, need event for it */
140 }
141
142 /**
143 * Run the long getTimestamp() method test.
144 */
145 @Test
146 public void testGetTimestamp() {
147 long result = nullEvent.getTimestamp().getValue();
148 assertEquals(-1L, result);
149 }
150
151 /**
152 * Test the getters for the reference, source and type.
153 */
154 @Test
155 public void testGetters() {
156 long rank = fixture.getRank();
157 CtfTmfTrace trace = fixture.getTrace();
158 assertEquals("kernel", trace.getName());
159
160 String reference = fixture.getChannel();
161 int cpu = fixture.getCPU();
162 ITmfEventType type = fixture.getType();
163 assertEquals(ITmfContext.UNKNOWN_RANK, rank);
164
165 assertEquals("channel0_1", reference);
166 assertEquals(1, cpu);
167 assertEquals("lttng_statedump_vm_map", type.toString());
168 }
169
170 /**
171 * Test the custom CTF attributes methods. The test trace doesn't have any,
172 * so the list of attributes should be empty.
173 */
174 @Test
175 public void testCustomAttributes() {
176 Set<String> attributes = fixture.listCustomAttributes();
177 assertEquals(0, attributes.size());
178
179 String attrib = fixture.getCustomAttribute("bozo");
180 assertNull(attrib);
181 }
182
183 /**
184 * Test the toString() method
185 */
186 @Test
187 public void testToString() {
188 String s = fixture.getContent().toString();
189 assertEquals("pid=1922, start=0xb73ea000, end=0xb73ec000, flags=0x8000075, inode=917738, pgoff=0", s);
190 }
191
192 /**
193 * Test the {@link CtfTmfEventFactory#getNullEvent(CtfTmfTrace)} method, and
194 * the nullEvent's values.
195 */
196 @Test
197 public void testNullEvent() {
198 CtfTmfEvent nullEvent2 = CtfTmfEventFactory.getNullEvent(fixture.getTrace());
199 assertSame(nullEvent2, nullEvent);
200 assertNotNull(nullEvent);
201 assertEquals(-1, nullEvent.getCPU());
202 assertEquals("Empty CTF event", nullEvent.getType().getName());
203 assertEquals("", nullEvent.getChannel());
204 assertEquals(0, nullEvent.getContent().getFields().size());
205 assertEquals(-1L, nullEvent.getTimestamp().getValue());
206 }
207 }
This page took 0.037616 seconds and 6 git commands to generate.