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