lttng: Add Lttng relayd connector
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / trace / StreamInputReaderTest.java
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
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.linuxtools.ctf.core.tests.trace;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assume.assumeTrue;
17
18 import java.io.File;
19 import java.util.Set;
20
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;
37
38 import com.google.common.collect.ImmutableList;
39
40 /**
41 * The class <code>StreamInputReaderTest</code> contains tests for the class
42 * <code>{@link StreamInputReader}</code>.
43 *
44 * @author ematkho
45 * @version $Revision: 1.0 $
46 */
47 @SuppressWarnings("javadoc")
48 public class StreamInputReaderTest {
49
50 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
51
52 private StreamInputReader fixture;
53
54 private static ImmutableList<String> wrap(String s) {
55 return ImmutableList.<String> builder().add(s).build();
56 }
57
58 /**
59 * Perform pre-test initialization.
60 *
61 * @throws CTFReaderException
62 */
63 @Before
64 public void setUp() throws CTFReaderException {
65 fixture = getStreamInputReader();
66 fixture.setName(1);
67 fixture.setCurrentEvent(new EventDefinition(new EventDeclaration(),
68 getStreamInputReader(), 0, null, null,
69 new StructDefinition(
70 new StructDeclaration(0),
71 null,
72 "packet",
73 wrap( "field" ),
74 new Definition[] { new StringDefinition(new StringDeclaration(), null, "field", "test") }),
75 null)
76 );
77 }
78
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) {
86 /*
87 * For the tests, we'll use the stream input corresponding to the
88 * CPU 0
89 */
90 if (si.getFilename().endsWith("0_0")) {
91 retVal = new StreamInputReader(si);
92 break;
93 }
94 }
95 return retVal;
96 }
97
98 /**
99 * Run the StreamInputReader(StreamInput) constructor test, with a valid
100 * trace.
101 */
102 @Test
103 public void testStreamInputReader_valid() {
104 assertNotNull(fixture);
105 }
106
107 /**
108 * Run the StreamInputReader(StreamInput) constructor test, with an invalid
109 * trace.
110 *
111 * @throws CTFReaderException
112 */
113 @Test(expected = CTFReaderException.class)
114 public void testStreamInputReader_invalid() throws CTFReaderException {
115 StreamInput streamInput = new StreamInput(
116 new Stream(new CTFTrace("")), new File(""));
117
118 StreamInputReader result = new StreamInputReader(streamInput);
119 assertNotNull(result);
120 }
121
122 /**
123 * Run the int getCPU() method test.
124 */
125 @Test
126 public void testGetCPU() {
127 int result = fixture.getCPU();
128 assertEquals(0, result);
129 }
130
131 /**
132 * Run the EventDefinition getCurrentEvent() method test.
133 */
134 @Test
135 public void testGetCurrentEvent() {
136 EventDefinition result = fixture.getCurrentEvent();
137 assertNotNull(result);
138 }
139
140 /**
141 * Run the StructDefinition getCurrentPacketContext() method test.
142 */
143 @Test
144 public void testGetCurrentPacketContext() {
145 StructDefinition result = fixture.getCurrentEvent().getPacketContext();
146 assertNotNull(result);
147 }
148
149 /**
150 * Run the int getName() method test.
151 */
152 @Test
153 public void testGetName() {
154 int result = fixture.getName();
155 assertEquals(1, result);
156 }
157
158 /**
159 * Run the void goToLastEvent() method test.
160 *
161 * @throws CTFReaderException
162 * error
163 */
164 @Test
165 public void testGoToLastEvent1() throws CTFReaderException {
166 final long endTimestamp = goToEnd();
167 final long endTime = 4287422460315L;
168 assertEquals(endTime, endTimestamp);
169 }
170
171 /**
172 * Run the void goToLastEvent() method test.
173 *
174 * @throws CTFReaderException
175 * error
176 */
177 @Test
178 public void testGoToLastEvent2() throws CTFReaderException {
179 long timestamp = -1;
180 while (fixture.readNextEvent().equals(CTFResponse.OK)) {
181 timestamp = fixture.getCurrentEvent().getTimestamp();
182 }
183 long endTimestamp = goToEnd();
184 assertEquals(0, timestamp - endTimestamp);
185 }
186
187 private long goToEnd() throws CTFReaderException {
188 fixture.goToLastEvent();
189 return fixture.getCurrentEvent().getTimestamp();
190 }
191
192 /**
193 * Run the boolean readNextEvent() method test.
194 *
195 * @throws CTFReaderException
196 * error
197 */
198 @Test
199 public void testReadNextEvent() throws CTFReaderException {
200 assertEquals(CTFResponse.OK, fixture.readNextEvent());
201 }
202
203 /**
204 * Run the void seek(long) method test. Seek by direct timestamp
205 *
206 * @throws CTFReaderException
207 * error
208 */
209 @Test
210 public void testSeek_timestamp() throws CTFReaderException {
211 long timestamp = 1L;
212 fixture.seek(timestamp);
213 }
214
215 /**
216 * Run the seek test. Seek by passing an EventDefinition to which we've
217 * given the timestamp we want.
218 *
219 * @throws CTFReaderException
220 */
221 @Test
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);
226 }
227 }
This page took 0.034852 seconds and 5 git commands to generate.