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
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.ctf
.core
.tests
.trace
;
14 import static org
.junit
.Assert
.assertEquals
;
15 import static org
.junit
.Assert
.assertNotNull
;
18 import java
.io
.IOException
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDefinition
;
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
.event
.EventDefinition
;
38 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.trace
.CTFStream
;
39 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
40 import org
.junit
.Before
;
41 import org
.junit
.Test
;
44 * The class <code>StreamInputReaderTest</code> contains tests for the class
45 * <code>{@link CTFStreamInputReader}</code>.
47 * @author Matthew Khouzam
49 @SuppressWarnings("javadoc")
50 public class CTFStreamInputReaderTest
{
52 private static final CtfTestTrace testTrace
= CtfTestTrace
.KERNEL
;
54 private CTFStreamInputReader fixture
;
57 * Perform pre-test initialization.
59 * @throws CTFException
62 public void setUp() throws CTFException
{
63 fixture
= getStreamInputReader();
65 fixture
.setCurrentEvent(new EventDefinition(new EventDeclaration(),
66 fixture
.getCPU(), 0, null, null, null,
68 new StructDeclaration(0),
71 new Definition
[] { new StringDefinition(StringDeclaration
.getStringDeclaration(Encoding
.UTF8
), null, "field", "test") }),
72 null, fixture
.getCurrentPacketReader().getCurrentPacket()));
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
) {
82 * For the tests, we'll use the stream input corresponding to the
85 if (si
.getFilename().endsWith("0_0")) {
86 retVal
= new CTFStreamInputReader(si
);
94 * Run the StreamInputReader(StreamInput) constructor test, with a valid
98 public void testStreamInputReader_valid() {
99 assertNotNull(fixture
);
103 * Run the StreamInputReader(StreamInput) constructor test, with an invalid
106 * @throws CTFException
107 * @throws IOException
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
);
118 * Run the int getCPU() method test.
121 public void testGetCPU() {
122 int result
= fixture
.getCPU();
123 assertEquals(0, result
);
127 * Run the EventDefinition getCurrentEvent() method test.
130 public void testGetCurrentEvent() {
131 assertNotNull(fixture
.getCurrentEvent());
135 * Run the StructDefinition getCurrentPacketContext() method test.
138 public void testGetCurrentPacketContext() {
139 IEventDefinition currentEvent
= fixture
.getCurrentEvent();
140 assertNotNull(currentEvent
);
141 ICompositeDefinition result
= currentEvent
.getPacketContext();
142 assertNotNull(result
);
146 * Run the int getName() method test.
149 public void testGetName() {
150 int result
= fixture
.getName();
151 assertEquals(1, result
);
155 * Run the void goToLastEvent() method test.
157 * @throws CTFException
161 public void testGoToLastEvent1() throws CTFException
{
162 final long endTimestamp
= goToEnd();
163 final long endTime
= 4287422460315L;
164 assertEquals(endTime
, endTimestamp
);
168 * Run the void goToLastEvent() method test.
170 * @throws CTFException
174 public void testGoToLastEvent2() throws CTFException
{
176 while (fixture
.readNextEvent().equals(CTFResponse
.OK
)) {
177 IEventDefinition currentEvent
= fixture
.getCurrentEvent();
178 assertNotNull(currentEvent
);
179 timestamp
= currentEvent
.getTimestamp();
181 long endTimestamp
= goToEnd();
182 assertEquals(0, timestamp
- endTimestamp
);
185 private long goToEnd() throws CTFException
{
186 fixture
.goToLastEvent();
187 IEventDefinition currentEvent
= fixture
.getCurrentEvent();
188 assertNotNull(currentEvent
);
189 return currentEvent
.getTimestamp();
193 * Run the boolean readNextEvent() method test.
195 * @throws CTFException
199 public void testReadNextEvent() throws CTFException
{
200 assertEquals(CTFResponse
.OK
, fixture
.readNextEvent());
204 * Run the void seek(long) method test. Seek by direct timestamp
206 * @throws CTFException
210 public void testSeek_timestamp() throws CTFException
{
212 fixture
.seek(timestamp
);
216 * Run the seek test. Seek by passing an EventDefinition to which we've
217 * given the timestamp we want.
219 * @throws CTFException
221 * @throws IOException
225 public void testSeek_eventDefinition() throws CTFException
, IOException
{
226 try (CTFStreamInputReader streamInputReader
= getStreamInputReader()) {
227 EventDefinition eventDefinition
= new EventDefinition(
228 new EventDeclaration(), streamInputReader
.getCPU(), 1L, null, null, null, null, null, streamInputReader
.getCurrentPacketReader().getCurrentPacket());
229 fixture
.setCurrentEvent(eventDefinition
);