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
.linuxtools
.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
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDeclaration
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.tests
.shared
.CtfTestTrace
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFResponse
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.Stream
;
32 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.StreamInput
;
33 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.StreamInputReader
;
34 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.EventDeclaration
;
35 import org
.junit
.Before
;
36 import org
.junit
.Test
;
38 import com
.google
.common
.collect
.ImmutableList
;
41 * The class <code>StreamInputReaderTest</code> contains tests for the class
42 * <code>{@link StreamInputReader}</code>.
45 * @version $Revision: 1.0 $
47 @SuppressWarnings("javadoc")
48 public class StreamInputReaderTest
{
50 private static final CtfTestTrace testTrace
= CtfTestTrace
.KERNEL
;
52 private StreamInputReader fixture
;
54 private static ImmutableList
<String
> wrap(String s
) {
55 return ImmutableList
.<String
> builder().add(s
).build();
59 * Perform pre-test initialization.
61 * @throws CTFReaderException
64 public void setUp() throws CTFReaderException
{
65 fixture
= getStreamInputReader();
67 fixture
.setCurrentEvent(new EventDefinition(new EventDeclaration(),
68 getStreamInputReader(), 0, null, null,
70 new StructDeclaration(0),
74 new Definition
[] { new StringDefinition(new StringDeclaration(), null, "field", "test") }),
79 private static StreamInputReader
getStreamInputReader() throws CTFReaderException
{
80 assumeTrue(testTrace
.exists());
81 CTFTrace trace
= testTrace
.getTrace();
82 Stream s
= trace
.getStream((long) 0);
83 Set
<StreamInput
> streamInput
= s
.getStreamInputs();
84 StreamInputReader retVal
= null;
85 for (StreamInput si
: streamInput
) {
87 * For the tests, we'll use the stream input corresponding to the
90 if (si
.getFilename().endsWith("0_0")) {
91 retVal
= new StreamInputReader(si
);
99 * Run the StreamInputReader(StreamInput) constructor test, with a valid
103 public void testStreamInputReader_valid() {
104 assertNotNull(fixture
);
108 * Run the StreamInputReader(StreamInput) constructor test, with an invalid
111 * @throws CTFReaderException
113 @Test(expected
= CTFReaderException
.class)
114 public void testStreamInputReader_invalid() throws CTFReaderException
{
115 StreamInput streamInput
= new StreamInput(
116 new Stream(new CTFTrace("")), new File(""));
118 StreamInputReader result
= new StreamInputReader(streamInput
);
119 assertNotNull(result
);
123 * Run the int getCPU() method test.
126 public void testGetCPU() {
127 int result
= fixture
.getCPU();
128 assertEquals(0, result
);
132 * Run the EventDefinition getCurrentEvent() method test.
135 public void testGetCurrentEvent() {
136 EventDefinition result
= fixture
.getCurrentEvent();
137 assertNotNull(result
);
141 * Run the StructDefinition getCurrentPacketContext() method test.
144 public void testGetCurrentPacketContext() {
145 StructDefinition result
= fixture
.getCurrentEvent().getPacketContext();
146 assertNotNull(result
);
150 * Run the int getName() method test.
153 public void testGetName() {
154 int result
= fixture
.getName();
155 assertEquals(1, result
);
159 * Run the void goToLastEvent() method test.
161 * @throws CTFReaderException
165 public void testGoToLastEvent1() throws CTFReaderException
{
166 final long endTimestamp
= goToEnd();
167 final long endTime
= 4287422460315L;
168 assertEquals(endTime
, endTimestamp
);
172 * Run the void goToLastEvent() method test.
174 * @throws CTFReaderException
178 public void testGoToLastEvent2() throws CTFReaderException
{
180 while (fixture
.readNextEvent().equals(CTFResponse
.OK
)) {
181 timestamp
= fixture
.getCurrentEvent().getTimestamp();
183 long endTimestamp
= goToEnd();
184 assertEquals(0, timestamp
- endTimestamp
);
187 private long goToEnd() throws CTFReaderException
{
188 fixture
.goToLastEvent();
189 return fixture
.getCurrentEvent().getTimestamp();
193 * Run the boolean readNextEvent() method test.
195 * @throws CTFReaderException
199 public void testReadNextEvent() throws CTFReaderException
{
200 assertEquals(CTFResponse
.OK
, fixture
.readNextEvent());
204 * Run the void seek(long) method test. Seek by direct timestamp
206 * @throws CTFReaderException
210 public void testSeek_timestamp() throws CTFReaderException
{
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 CTFReaderException
222 public void testSeek_eventDefinition() throws CTFReaderException
{
223 EventDefinition eventDefinition
= new EventDefinition(
224 new EventDeclaration(), getStreamInputReader(), 1L, null, null, null, null);
225 fixture
.setCurrentEvent(eventDefinition
);