ctf: Depend on the tracecompass-test-traces project
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / trace / CTFStreamInputReaderTest.java
CommitLineData
4bd7f2db 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2013, 2014 Ericsson
4bd7f2db
AM
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
f357bcd4 12package org.eclipse.tracecompass.ctf.core.tests.trace;
866e5b51
FC
13
14import static org.junit.Assert.assertEquals;
15import static org.junit.Assert.assertNotNull;
866e5b51 16
9ac63b5b 17import java.io.File;
05ce5fef 18import java.io.IOException;
866e5b51
FC
19import java.util.Set;
20
680f9173 21import org.eclipse.tracecompass.ctf.core.CTFException;
f357bcd4
AM
22import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
23import org.eclipse.tracecompass.ctf.core.event.types.Definition;
d890ec37 24import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
778bce67 25import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
f357bcd4
AM
26import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration;
27import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition;
28import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
29import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
c4d57ac1 30import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTraceUtils;
f357bcd4
AM
31import org.eclipse.tracecompass.ctf.core.trace.CTFResponse;
32import org.eclipse.tracecompass.ctf.core.trace.CTFStream;
33import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInput;
34import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInputReader;
35import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
36import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration;
c4d57ac1 37import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
866e5b51
FC
38import org.junit.Before;
39import org.junit.Test;
40
41/**
42 * The class <code>StreamInputReaderTest</code> contains tests for the class
d84419e1 43 * <code>{@link CTFStreamInputReader}</code>.
ce2388e0 44 *
866e5b51
FC
45 * @author ematkho
46 * @version $Revision: 1.0 $
47 */
be6df2d8 48@SuppressWarnings("javadoc")
d84419e1 49public class CTFStreamInputReaderTest {
866e5b51 50
9ac63b5b 51 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
32bf80d2 52
d84419e1 53 private CTFStreamInputReader fixture;
866e5b51 54
866e5b51
FC
55 /**
56 * Perform pre-test initialization.
ce2388e0 57 *
680f9173 58 * @throws CTFException
866e5b51
FC
59 */
60 @Before
680f9173 61 public void setUp() throws CTFException {
13be1a8f 62 fixture = getStreamInputReader();
866e5b51
FC
63 fixture.setName(1);
64 fixture.setCurrentEvent(new EventDefinition(new EventDeclaration(),
408f954e 65 getStreamInputReader().getCPU(), 0, null, null, null,
a4fa4e36
MK
66 new StructDefinition(
67 new StructDeclaration(0),
68 null,
69 "packet",
d890ec37 70 new Definition[] { new StringDefinition(StringDeclaration.getStringDeclaration(Encoding.UTF8), null, "field", "test") }),
a4fa4e36
MK
71 null)
72 );
866e5b51
FC
73 }
74
680f9173 75 private static CTFStreamInputReader getStreamInputReader() throws CTFException {
c4d57ac1 76 CTFTrace trace = CtfTestTraceUtils.getTrace(testTrace);
d84419e1
AM
77 CTFStream s = trace.getStream((long) 0);
78 Set<CTFStreamInput> streamInput = s.getStreamInputs();
79 CTFStreamInputReader retVal = null;
80 for (CTFStreamInput si : streamInput) {
866e5b51
FC
81 /*
82 * For the tests, we'll use the stream input corresponding to the
83 * CPU 0
84 */
4a9c1f07 85 if (si.getFilename().endsWith("0_0")) {
d84419e1 86 retVal = new CTFStreamInputReader(si);
866e5b51
FC
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.
ce2388e0 105 *
680f9173 106 * @throws CTFException
05ce5fef 107 * @throws IOException
866e5b51 108 */
680f9173
MK
109 @Test(expected = CTFException.class)
110 public void testStreamInputReader_invalid() throws CTFException, IOException {
46167f03
MK
111 CTFStreamInput streamInput = new CTFStreamInput(new CTFStream(new CTFTrace("")), new File(""));
112 try (CTFStreamInputReader result = new CTFStreamInputReader(streamInput)) {
05ce5fef
AM
113 assertNotNull(result);
114 }
866e5b51
FC
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() {
90cefe9f
MK
140 EventDefinition currentEvent = fixture.getCurrentEvent();
141 assertNotNull(currentEvent);
142 ICompositeDefinition result = currentEvent.getPacketContext();
866e5b51
FC
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
866e5b51
FC
155 /**
156 * Run the void goToLastEvent() method test.
a4fa4e36 157 *
680f9173 158 * @throws CTFException
a4fa4e36 159 * error
866e5b51
FC
160 */
161 @Test
680f9173 162 public void testGoToLastEvent1() throws CTFException {
ec6f5beb
MK
163 final long endTimestamp = goToEnd();
164 final long endTime = 4287422460315L;
a4fa4e36 165 assertEquals(endTime, endTimestamp);
ec6f5beb
MK
166 }
167
168 /**
169 * Run the void goToLastEvent() method test.
a4fa4e36 170 *
680f9173 171 * @throws CTFException
a4fa4e36 172 * error
ec6f5beb
MK
173 */
174 @Test
680f9173 175 public void testGoToLastEvent2() throws CTFException {
ec6f5beb 176 long timestamp = -1;
a4fa4e36 177 while (fixture.readNextEvent().equals(CTFResponse.OK)) {
90cefe9f
MK
178 EventDefinition currentEvent = fixture.getCurrentEvent();
179 assertNotNull(currentEvent);
180 timestamp = currentEvent.getTimestamp();
ec6f5beb
MK
181 }
182 long endTimestamp = goToEnd();
a4fa4e36 183 assertEquals(0, timestamp - endTimestamp);
ec6f5beb
MK
184 }
185
680f9173 186 private long goToEnd() throws CTFException {
866e5b51 187 fixture.goToLastEvent();
90cefe9f
MK
188 EventDefinition currentEvent = fixture.getCurrentEvent();
189 assertNotNull(currentEvent);
190 return currentEvent.getTimestamp();
866e5b51
FC
191 }
192
193 /**
194 * Run the boolean readNextEvent() method test.
a4fa4e36 195 *
680f9173 196 * @throws CTFException
a4fa4e36 197 * error
866e5b51
FC
198 */
199 @Test
680f9173 200 public void testReadNextEvent() throws CTFException {
6a5251eb 201 assertEquals(CTFResponse.OK, fixture.readNextEvent());
866e5b51
FC
202 }
203
204 /**
205 * Run the void seek(long) method test. Seek by direct timestamp
a4fa4e36 206 *
680f9173 207 * @throws CTFException
a4fa4e36 208 * error
866e5b51
FC
209 */
210 @Test
680f9173 211 public void testSeek_timestamp() throws CTFException {
866e5b51
FC
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.
ce2388e0 219 *
680f9173 220 * @throws CTFException
866e5b51
FC
221 */
222 @Test
680f9173 223 public void testSeek_eventDefinition() throws CTFException {
866e5b51 224 EventDefinition eventDefinition = new EventDefinition(
408f954e 225 new EventDeclaration(), getStreamInputReader().getCPU(), 1L, null, null, null, null, null);
866e5b51
FC
226 fixture.setCurrentEvent(eventDefinition);
227 }
05ce5fef 228}
This page took 0.139945 seconds and 5 git commands to generate.