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