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
;
19 import java
.io
.IOException
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDeclaration
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.tests
.shared
.CtfTestTrace
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFResponse
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
32 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFStream
;
33 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFStreamInput
;
34 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFStreamInputReader
;
35 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.EventDeclaration
;
36 import org
.junit
.Before
;
37 import org
.junit
.Test
;
39 import com
.google
.common
.collect
.ImmutableList
;
42 * The class <code>StreamInputReaderTest</code> contains tests for the class
43 * <code>{@link CTFStreamInputReader}</code>.
46 * @version $Revision: 1.0 $
48 @SuppressWarnings("javadoc")
49 public class CTFStreamInputReaderTest
{
51 private static final CtfTestTrace testTrace
= CtfTestTrace
.KERNEL
;
53 private CTFStreamInputReader fixture
;
55 private static ImmutableList
<String
> wrap(String s
) {
56 return ImmutableList
.<String
> builder().add(s
).build();
60 * Perform pre-test initialization.
62 * @throws CTFReaderException
65 public void setUp() throws CTFReaderException
{
66 fixture
= getStreamInputReader();
68 fixture
.setCurrentEvent(new EventDefinition(new EventDeclaration(),
69 getStreamInputReader(), 0, null, null,
71 new StructDeclaration(0),
75 new Definition
[] { new StringDefinition(new StringDeclaration(), null, "field", "test") }),
80 private static CTFStreamInputReader
getStreamInputReader() throws CTFReaderException
{
81 assumeTrue(testTrace
.exists());
82 CTFTrace trace
= testTrace
.getTrace();
83 CTFStream s
= trace
.getStream((long) 0);
84 Set
<CTFStreamInput
> streamInput
= s
.getStreamInputs();
85 CTFStreamInputReader retVal
= null;
86 for (CTFStreamInput si
: streamInput
) {
88 * For the tests, we'll use the stream input corresponding to the
91 if (si
.getFilename().endsWith("0_0")) {
92 retVal
= new CTFStreamInputReader(si
);
100 * Run the StreamInputReader(StreamInput) constructor test, with a valid
104 public void testStreamInputReader_valid() {
105 assertNotNull(fixture
);
109 * Run the StreamInputReader(StreamInput) constructor test, with an invalid
112 * @throws CTFReaderException
113 * @throws IOException
115 @Test(expected
= CTFReaderException
.class)
116 public void testStreamInputReader_invalid() throws CTFReaderException
, IOException
{
117 try (CTFStreamInput streamInput
= new CTFStreamInput(new CTFStream(new CTFTrace("")), new File(""));
118 CTFStreamInputReader result
= new CTFStreamInputReader(streamInput
)) {
119 assertNotNull(result
);
124 * Run the int getCPU() method test.
127 public void testGetCPU() {
128 int result
= fixture
.getCPU();
129 assertEquals(0, result
);
133 * Run the EventDefinition getCurrentEvent() method test.
136 public void testGetCurrentEvent() {
137 EventDefinition result
= fixture
.getCurrentEvent();
138 assertNotNull(result
);
142 * Run the StructDefinition getCurrentPacketContext() method test.
145 public void testGetCurrentPacketContext() {
146 StructDefinition result
= fixture
.getCurrentEvent().getPacketContext();
147 assertNotNull(result
);
151 * Run the int getName() method test.
154 public void testGetName() {
155 int result
= fixture
.getName();
156 assertEquals(1, result
);
160 * Run the void goToLastEvent() method test.
162 * @throws CTFReaderException
166 public void testGoToLastEvent1() throws CTFReaderException
{
167 final long endTimestamp
= goToEnd();
168 final long endTime
= 4287422460315L;
169 assertEquals(endTime
, endTimestamp
);
173 * Run the void goToLastEvent() method test.
175 * @throws CTFReaderException
179 public void testGoToLastEvent2() throws CTFReaderException
{
181 while (fixture
.readNextEvent().equals(CTFResponse
.OK
)) {
182 timestamp
= fixture
.getCurrentEvent().getTimestamp();
184 long endTimestamp
= goToEnd();
185 assertEquals(0, timestamp
- endTimestamp
);
188 private long goToEnd() throws CTFReaderException
{
189 fixture
.goToLastEvent();
190 return fixture
.getCurrentEvent().getTimestamp();
194 * Run the boolean readNextEvent() method test.
196 * @throws CTFReaderException
200 public void testReadNextEvent() throws CTFReaderException
{
201 assertEquals(CTFResponse
.OK
, fixture
.readNextEvent());
205 * Run the void seek(long) method test. Seek by direct timestamp
207 * @throws CTFReaderException
211 public void testSeek_timestamp() throws CTFReaderException
{
213 fixture
.seek(timestamp
);
217 * Run the seek test. Seek by passing an EventDefinition to which we've
218 * given the timestamp we want.
220 * @throws CTFReaderException
223 public void testSeek_eventDefinition() throws CTFReaderException
{
224 EventDefinition eventDefinition
= new EventDefinition(
225 new EventDeclaration(), getStreamInputReader(), 1L, null, null, null, null);
226 fixture
.setCurrentEvent(eventDefinition
);