ctf: Make packet descriptor information available to event
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / trace / CTFStreamInputReaderTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.tracecompass.ctf.core.tests.trace;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.util.Set;
20
21 import org.eclipse.tracecompass.ctf.core.CTFException;
22 import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
23 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
24 import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
25 import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
26 import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration;
27 import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition;
28 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
29 import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
30 import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTraceUtils;
31 import org.eclipse.tracecompass.ctf.core.trace.CTFResponse;
32 import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInput;
33 import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInputReader;
34 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
35 import org.eclipse.tracecompass.ctf.core.trace.ICTFStream;
36 import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration;
37 import org.eclipse.tracecompass.internal.ctf.core.trace.CTFStream;
38 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
39 import org.junit.Before;
40 import org.junit.Test;
41
42 /**
43 * The class <code>StreamInputReaderTest</code> contains tests for the class
44 * <code>{@link CTFStreamInputReader}</code>.
45 *
46 * @author ematkho
47 * @version $Revision: 1.0 $
48 */
49 @SuppressWarnings("javadoc")
50 public class CTFStreamInputReaderTest {
51
52 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
53
54 private CTFStreamInputReader fixture;
55
56 /**
57 * Perform pre-test initialization.
58 *
59 * @throws CTFException
60 */
61 @Before
62 public void setUp() throws CTFException {
63 fixture = getStreamInputReader();
64 fixture.setName(1);
65 fixture.setCurrentEvent(new EventDefinition(new EventDeclaration(),
66 fixture.getCPU(), 0, null, null, null,
67 new StructDefinition(
68 new StructDeclaration(0),
69 null,
70 "packet",
71 new Definition[] { new StringDefinition(StringDeclaration.getStringDeclaration(Encoding.UTF8), null, "field", "test") }),
72 null, fixture.getCurrentPacketReader().getCurrentPacket()));
73 }
74
75 private static CTFStreamInputReader getStreamInputReader() throws CTFException {
76 CTFTrace trace = CtfTestTraceUtils.getTrace(testTrace);
77 ICTFStream s = trace.getStream((long) 0);
78 Set<CTFStreamInput> streamInput = s.getStreamInputs();
79 CTFStreamInputReader retVal = null;
80 for (CTFStreamInput si : streamInput) {
81 /*
82 * For the tests, we'll use the stream input corresponding to the
83 * CPU 0
84 */
85 if (si.getFilename().endsWith("0_0")) {
86 retVal = new CTFStreamInputReader(si);
87 break;
88 }
89 }
90 return retVal;
91 }
92
93 /**
94 * Run the StreamInputReader(StreamInput) constructor test, with a valid
95 * trace.
96 */
97 @Test
98 public void testStreamInputReader_valid() {
99 assertNotNull(fixture);
100 }
101
102 /**
103 * Run the StreamInputReader(StreamInput) constructor test, with an invalid
104 * trace.
105 *
106 * @throws CTFException
107 * @throws IOException
108 */
109 @Test(expected = CTFException.class)
110 public void testStreamInputReader_invalid() throws CTFException, IOException {
111 CTFStreamInput streamInput = new CTFStreamInput(new CTFStream(new CTFTrace("")), new File(""));
112 try (CTFStreamInputReader result = new CTFStreamInputReader(streamInput)) {
113 assertNotNull(result);
114 }
115 }
116
117 /**
118 * Run the int getCPU() method test.
119 */
120 @Test
121 public void testGetCPU() {
122 int result = fixture.getCPU();
123 assertEquals(0, result);
124 }
125
126 /**
127 * Run the EventDefinition getCurrentEvent() method test.
128 */
129 @Test
130 public void testGetCurrentEvent() {
131 EventDefinition result = fixture.getCurrentEvent();
132 assertNotNull(result);
133 }
134
135 /**
136 * Run the StructDefinition getCurrentPacketContext() method test.
137 */
138 @Test
139 public void testGetCurrentPacketContext() {
140 EventDefinition currentEvent = fixture.getCurrentEvent();
141 assertNotNull(currentEvent);
142 ICompositeDefinition result = currentEvent.getPacketContext();
143 assertNotNull(result);
144 }
145
146 /**
147 * Run the int getName() method test.
148 */
149 @Test
150 public void testGetName() {
151 int result = fixture.getName();
152 assertEquals(1, result);
153 }
154
155 /**
156 * Run the void goToLastEvent() method test.
157 *
158 * @throws CTFException
159 * error
160 */
161 @Test
162 public void testGoToLastEvent1() throws CTFException {
163 final long endTimestamp = goToEnd();
164 final long endTime = 4287422460315L;
165 assertEquals(endTime, endTimestamp);
166 }
167
168 /**
169 * Run the void goToLastEvent() method test.
170 *
171 * @throws CTFException
172 * error
173 */
174 @Test
175 public void testGoToLastEvent2() throws CTFException {
176 long timestamp = -1;
177 while (fixture.readNextEvent().equals(CTFResponse.OK)) {
178 EventDefinition currentEvent = fixture.getCurrentEvent();
179 assertNotNull(currentEvent);
180 timestamp = currentEvent.getTimestamp();
181 }
182 long endTimestamp = goToEnd();
183 assertEquals(0, timestamp - endTimestamp);
184 }
185
186 private long goToEnd() throws CTFException {
187 fixture.goToLastEvent();
188 EventDefinition currentEvent = fixture.getCurrentEvent();
189 assertNotNull(currentEvent);
190 return currentEvent.getTimestamp();
191 }
192
193 /**
194 * Run the boolean readNextEvent() method test.
195 *
196 * @throws CTFException
197 * error
198 */
199 @Test
200 public void testReadNextEvent() throws CTFException {
201 assertEquals(CTFResponse.OK, fixture.readNextEvent());
202 }
203
204 /**
205 * Run the void seek(long) method test. Seek by direct timestamp
206 *
207 * @throws CTFException
208 * error
209 */
210 @Test
211 public void testSeek_timestamp() throws CTFException {
212 long timestamp = 1L;
213 fixture.seek(timestamp);
214 }
215
216 /**
217 * Run the seek test. Seek by passing an EventDefinition to which we've
218 * given the timestamp we want.
219 *
220 * @throws CTFException
221 * error
222 * @throws IOException
223 * file not there
224 */
225 @Test
226 public void testSeek_eventDefinition() throws CTFException, IOException {
227 try (CTFStreamInputReader streamInputReader = getStreamInputReader()) {
228 EventDefinition eventDefinition = new EventDefinition(
229 new EventDeclaration(), streamInputReader.getCPU(), 1L, null, null, null, null, null, streamInputReader.getCurrentPacketReader().getCurrentPacket());
230 fixture.setCurrentEvent(eventDefinition);
231 }
232 }
233 }
This page took 0.035964 seconds and 5 git commands to generate.