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
;
16 import static org
.junit
.Assume
.assumeTrue
;
19 import java
.io
.IOException
;
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
.StringDeclaration
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDefinition
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDefinition
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.tests
.shared
.CtfTestTrace
;
30 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFReaderException
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFResponse
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFStream
;
33 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFStreamInput
;
34 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFStreamInputReader
;
35 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
36 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.EventDeclaration
;
37 import org
.junit
.Before
;
38 import org
.junit
.Test
;
40 import com
.google
.common
.collect
.ImmutableList
;
43 * The class <code>StreamInputReaderTest</code> contains tests for the class
44 * <code>{@link CTFStreamInputReader}</code>.
47 * @version $Revision: 1.0 $
49 @SuppressWarnings("javadoc")
50 public class CTFStreamInputReaderTest
{
52 private static final CtfTestTrace testTrace
= CtfTestTrace
.KERNEL
;
54 private CTFStreamInputReader fixture
;
56 private static ImmutableList
<String
> wrap(String s
) {
57 return ImmutableList
.<String
> builder().add(s
).build();
61 * Perform pre-test initialization.
63 * @throws CTFReaderException
66 public void setUp() throws CTFReaderException
{
67 fixture
= getStreamInputReader();
69 fixture
.setCurrentEvent(new EventDefinition(new EventDeclaration(),
70 getStreamInputReader(), 0, null, null,
72 new StructDeclaration(0),
76 new Definition
[] { new StringDefinition(StringDeclaration
.getStringDeclaration(Encoding
.UTF8
), null, "field", "test") }),
81 private static CTFStreamInputReader
getStreamInputReader() throws CTFReaderException
{
82 assumeTrue(testTrace
.exists());
83 CTFTrace trace
= testTrace
.getTrace();
84 CTFStream s
= trace
.getStream((long) 0);
85 Set
<CTFStreamInput
> streamInput
= s
.getStreamInputs();
86 CTFStreamInputReader retVal
= null;
87 for (CTFStreamInput si
: streamInput
) {
89 * For the tests, we'll use the stream input corresponding to the
92 if (si
.getFilename().endsWith("0_0")) {
93 retVal
= new CTFStreamInputReader(si
);
101 * Run the StreamInputReader(StreamInput) constructor test, with a valid
105 public void testStreamInputReader_valid() {
106 assertNotNull(fixture
);
110 * Run the StreamInputReader(StreamInput) constructor test, with an invalid
113 * @throws CTFReaderException
114 * @throws IOException
116 @Test(expected
= CTFReaderException
.class)
117 public void testStreamInputReader_invalid() throws CTFReaderException
, IOException
{
118 CTFStreamInput streamInput
= new CTFStreamInput(new CTFStream(new CTFTrace("")), new File(""));
119 try (CTFStreamInputReader result
= new CTFStreamInputReader(streamInput
)) {
120 assertNotNull(result
);
125 * Run the int getCPU() method test.
128 public void testGetCPU() {
129 int result
= fixture
.getCPU();
130 assertEquals(0, result
);
134 * Run the EventDefinition getCurrentEvent() method test.
137 public void testGetCurrentEvent() {
138 EventDefinition result
= fixture
.getCurrentEvent();
139 assertNotNull(result
);
143 * Run the StructDefinition getCurrentPacketContext() method test.
146 public void testGetCurrentPacketContext() {
147 StructDefinition result
= fixture
.getCurrentEvent().getPacketContext();
148 assertNotNull(result
);
152 * Run the int getName() method test.
155 public void testGetName() {
156 int result
= fixture
.getName();
157 assertEquals(1, result
);
161 * Run the void goToLastEvent() method test.
163 * @throws CTFReaderException
167 public void testGoToLastEvent1() throws CTFReaderException
{
168 final long endTimestamp
= goToEnd();
169 final long endTime
= 4287422460315L;
170 assertEquals(endTime
, endTimestamp
);
174 * Run the void goToLastEvent() method test.
176 * @throws CTFReaderException
180 public void testGoToLastEvent2() throws CTFReaderException
{
182 while (fixture
.readNextEvent().equals(CTFResponse
.OK
)) {
183 timestamp
= fixture
.getCurrentEvent().getTimestamp();
185 long endTimestamp
= goToEnd();
186 assertEquals(0, timestamp
- endTimestamp
);
189 private long goToEnd() throws CTFReaderException
{
190 fixture
.goToLastEvent();
191 return fixture
.getCurrentEvent().getTimestamp();
195 * Run the boolean readNextEvent() method test.
197 * @throws CTFReaderException
201 public void testReadNextEvent() throws CTFReaderException
{
202 assertEquals(CTFResponse
.OK
, fixture
.readNextEvent());
206 * Run the void seek(long) method test. Seek by direct timestamp
208 * @throws CTFReaderException
212 public void testSeek_timestamp() throws CTFReaderException
{
214 fixture
.seek(timestamp
);
218 * Run the seek test. Seek by passing an EventDefinition to which we've
219 * given the timestamp we want.
221 * @throws CTFReaderException
224 public void testSeek_eventDefinition() throws CTFReaderException
{
225 EventDefinition eventDefinition
= new EventDefinition(
226 new EventDeclaration(), getStreamInputReader(), 1L, null, null, null, null);
227 fixture
.setCurrentEvent(eventDefinition
);