ctf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / 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.tracecompass.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.tracecompass.ctf.core.CTFException;
23 import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
24 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
25 import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
26 import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
27 import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration;
28 import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition;
29 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
30 import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
31 import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTrace;
32 import org.eclipse.tracecompass.ctf.core.trace.CTFResponse;
33 import org.eclipse.tracecompass.ctf.core.trace.CTFStream;
34 import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInput;
35 import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInputReader;
36 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
37 import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration;
38 import org.junit.Before;
39 import org.junit.Test;
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 /**
56 * Perform pre-test initialization.
57 *
58 * @throws CTFException
59 */
60 @Before
61 public void setUp() throws CTFException {
62 fixture = getStreamInputReader();
63 fixture.setName(1);
64 fixture.setCurrentEvent(new EventDefinition(new EventDeclaration(),
65 getStreamInputReader(), 0, null, null,
66 new StructDefinition(
67 new StructDeclaration(0),
68 null,
69 "packet",
70 new Definition[] { new StringDefinition(StringDeclaration.getStringDeclaration(Encoding.UTF8), null, "field", "test") }),
71 null)
72 );
73 }
74
75 private static CTFStreamInputReader getStreamInputReader() throws CTFException {
76 assumeTrue(testTrace.exists());
77 CTFTrace trace = testTrace.getTrace();
78 CTFStream s = trace.getStream((long) 0);
79 Set<CTFStreamInput> streamInput = s.getStreamInputs();
80 CTFStreamInputReader retVal = null;
81 for (CTFStreamInput si : streamInput) {
82 /*
83 * For the tests, we'll use the stream input corresponding to the
84 * CPU 0
85 */
86 if (si.getFilename().endsWith("0_0")) {
87 retVal = new CTFStreamInputReader(si);
88 break;
89 }
90 }
91 return retVal;
92 }
93
94 /**
95 * Run the StreamInputReader(StreamInput) constructor test, with a valid
96 * trace.
97 */
98 @Test
99 public void testStreamInputReader_valid() {
100 assertNotNull(fixture);
101 }
102
103 /**
104 * Run the StreamInputReader(StreamInput) constructor test, with an invalid
105 * trace.
106 *
107 * @throws CTFException
108 * @throws IOException
109 */
110 @Test(expected = CTFException.class)
111 public void testStreamInputReader_invalid() throws CTFException, IOException {
112 CTFStreamInput streamInput = new CTFStreamInput(new CTFStream(new CTFTrace("")), new File(""));
113 try (CTFStreamInputReader result = new CTFStreamInputReader(streamInput)) {
114 assertNotNull(result);
115 }
116 }
117
118 /**
119 * Run the int getCPU() method test.
120 */
121 @Test
122 public void testGetCPU() {
123 int result = fixture.getCPU();
124 assertEquals(0, result);
125 }
126
127 /**
128 * Run the EventDefinition getCurrentEvent() method test.
129 */
130 @Test
131 public void testGetCurrentEvent() {
132 EventDefinition result = fixture.getCurrentEvent();
133 assertNotNull(result);
134 }
135
136 /**
137 * Run the StructDefinition getCurrentPacketContext() method test.
138 */
139 @Test
140 public void testGetCurrentPacketContext() {
141 ICompositeDefinition result = fixture.getCurrentEvent().getPacketContext();
142 assertNotNull(result);
143 }
144
145 /**
146 * Run the int getName() method test.
147 */
148 @Test
149 public void testGetName() {
150 int result = fixture.getName();
151 assertEquals(1, result);
152 }
153
154 /**
155 * Run the void goToLastEvent() method test.
156 *
157 * @throws CTFException
158 * error
159 */
160 @Test
161 public void testGoToLastEvent1() throws CTFException {
162 final long endTimestamp = goToEnd();
163 final long endTime = 4287422460315L;
164 assertEquals(endTime, endTimestamp);
165 }
166
167 /**
168 * Run the void goToLastEvent() method test.
169 *
170 * @throws CTFException
171 * error
172 */
173 @Test
174 public void testGoToLastEvent2() throws CTFException {
175 long timestamp = -1;
176 while (fixture.readNextEvent().equals(CTFResponse.OK)) {
177 timestamp = fixture.getCurrentEvent().getTimestamp();
178 }
179 long endTimestamp = goToEnd();
180 assertEquals(0, timestamp - endTimestamp);
181 }
182
183 private long goToEnd() throws CTFException {
184 fixture.goToLastEvent();
185 return fixture.getCurrentEvent().getTimestamp();
186 }
187
188 /**
189 * Run the boolean readNextEvent() method test.
190 *
191 * @throws CTFException
192 * error
193 */
194 @Test
195 public void testReadNextEvent() throws CTFException {
196 assertEquals(CTFResponse.OK, fixture.readNextEvent());
197 }
198
199 /**
200 * Run the void seek(long) method test. Seek by direct timestamp
201 *
202 * @throws CTFException
203 * error
204 */
205 @Test
206 public void testSeek_timestamp() throws CTFException {
207 long timestamp = 1L;
208 fixture.seek(timestamp);
209 }
210
211 /**
212 * Run the seek test. Seek by passing an EventDefinition to which we've
213 * given the timestamp we want.
214 *
215 * @throws CTFException
216 */
217 @Test
218 public void testSeek_eventDefinition() throws CTFException {
219 EventDefinition eventDefinition = new EventDefinition(
220 new EventDeclaration(), getStreamInputReader(), 1L, null, null, null, null);
221 fixture.setCurrentEvent(eventDefinition);
222 }
223 }
This page took 0.036566 seconds and 6 git commands to generate.