Commit | Line | Data |
---|---|---|
4bd7f2db | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 Ericsson |
4bd7f2db AM |
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 | ||
f357bcd4 | 12 | package org.eclipse.tracecompass.ctf.core.tests.trace; |
866e5b51 FC |
13 | |
14 | import static org.junit.Assert.assertEquals; | |
15 | import static org.junit.Assert.assertNotNull; | |
866e5b51 | 16 | |
9ac63b5b | 17 | import java.io.File; |
05ce5fef | 18 | import java.io.IOException; |
866e5b51 FC |
19 | import java.util.Set; |
20 | ||
680f9173 | 21 | import org.eclipse.tracecompass.ctf.core.CTFException; |
e8ece272 | 22 | import org.eclipse.tracecompass.ctf.core.event.IEventDefinition; |
f357bcd4 | 23 | import org.eclipse.tracecompass.ctf.core.event.types.Definition; |
d890ec37 | 24 | import org.eclipse.tracecompass.ctf.core.event.types.Encoding; |
778bce67 | 25 | import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition; |
f357bcd4 AM |
26 | import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration; |
27 | import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition; | |
28 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; | |
29 | import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition; | |
c4d57ac1 | 30 | import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTraceUtils; |
f357bcd4 | 31 | import org.eclipse.tracecompass.ctf.core.trace.CTFResponse; |
f357bcd4 AM |
32 | import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInput; |
33 | import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInputReader; | |
34 | import org.eclipse.tracecompass.ctf.core.trace.CTFTrace; | |
8aa463e0 | 35 | import org.eclipse.tracecompass.ctf.core.trace.ICTFStream; |
f357bcd4 | 36 | import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration; |
e8ece272 | 37 | import org.eclipse.tracecompass.internal.ctf.core.event.EventDefinition; |
8aa463e0 | 38 | import org.eclipse.tracecompass.internal.ctf.core.trace.CTFStream; |
c4d57ac1 | 39 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; |
866e5b51 FC |
40 | import org.junit.Before; |
41 | import org.junit.Test; | |
42 | ||
43 | /** | |
44 | * The class <code>StreamInputReaderTest</code> contains tests for the class | |
d84419e1 | 45 | * <code>{@link CTFStreamInputReader}</code>. |
ce2388e0 | 46 | * |
e8ece272 | 47 | * @author Matthew Khouzam |
866e5b51 | 48 | */ |
be6df2d8 | 49 | @SuppressWarnings("javadoc") |
d84419e1 | 50 | public class CTFStreamInputReaderTest { |
866e5b51 | 51 | |
9ac63b5b | 52 | private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL; |
32bf80d2 | 53 | |
d84419e1 | 54 | private CTFStreamInputReader fixture; |
866e5b51 | 55 | |
866e5b51 FC |
56 | /** |
57 | * Perform pre-test initialization. | |
ce2388e0 | 58 | * |
680f9173 | 59 | * @throws CTFException |
866e5b51 FC |
60 | */ |
61 | @Before | |
680f9173 | 62 | public void setUp() throws CTFException { |
13be1a8f | 63 | fixture = getStreamInputReader(); |
866e5b51 FC |
64 | fixture.setName(1); |
65 | fixture.setCurrentEvent(new EventDefinition(new EventDeclaration(), | |
6b8f960d | 66 | fixture.getCPU(), 0, null, null, null, |
a4fa4e36 MK |
67 | new StructDefinition( |
68 | new StructDeclaration(0), | |
69 | null, | |
70 | "packet", | |
d890ec37 | 71 | new Definition[] { new StringDefinition(StringDeclaration.getStringDeclaration(Encoding.UTF8), null, "field", "test") }), |
6b8f960d | 72 | null, fixture.getCurrentPacketReader().getCurrentPacket())); |
866e5b51 FC |
73 | } |
74 | ||
680f9173 | 75 | private static CTFStreamInputReader getStreamInputReader() throws CTFException { |
c4d57ac1 | 76 | CTFTrace trace = CtfTestTraceUtils.getTrace(testTrace); |
8aa463e0 | 77 | ICTFStream s = trace.getStream((long) 0); |
d84419e1 AM |
78 | Set<CTFStreamInput> streamInput = s.getStreamInputs(); |
79 | CTFStreamInputReader retVal = null; | |
80 | for (CTFStreamInput si : streamInput) { | |
866e5b51 FC |
81 | /* |
82 | * For the tests, we'll use the stream input corresponding to the | |
83 | * CPU 0 | |
84 | */ | |
4a9c1f07 | 85 | if (si.getFilename().endsWith("0_0")) { |
d84419e1 | 86 | retVal = new CTFStreamInputReader(si); |
866e5b51 FC |
87 | break; |
88 | } | |
89 | } | |
90 | return retVal; | |
91 | } | |
92 | ||
93 | /** | |
94 | * Run the StreamInputReader(StreamInput) constructor test, with a valid | |
95 | * trace. | |
96 | */ | |
97 | @Test | |
98 | public void testStreamInputReader_valid() { | |
99 | assertNotNull(fixture); | |
100 | } | |
101 | ||
102 | /** | |
103 | * Run the StreamInputReader(StreamInput) constructor test, with an invalid | |
104 | * trace. | |
ce2388e0 | 105 | * |
680f9173 | 106 | * @throws CTFException |
05ce5fef | 107 | * @throws IOException |
866e5b51 | 108 | */ |
680f9173 MK |
109 | @Test(expected = CTFException.class) |
110 | public void testStreamInputReader_invalid() throws CTFException, IOException { | |
46167f03 MK |
111 | CTFStreamInput streamInput = new CTFStreamInput(new CTFStream(new CTFTrace("")), new File("")); |
112 | try (CTFStreamInputReader result = new CTFStreamInputReader(streamInput)) { | |
05ce5fef AM |
113 | assertNotNull(result); |
114 | } | |
866e5b51 FC |
115 | } |
116 | ||
117 | /** | |
118 | * Run the int getCPU() method test. | |
119 | */ | |
120 | @Test | |
121 | public void testGetCPU() { | |
122 | int result = fixture.getCPU(); | |
123 | assertEquals(0, result); | |
124 | } | |
125 | ||
126 | /** | |
127 | * Run the EventDefinition getCurrentEvent() method test. | |
128 | */ | |
129 | @Test | |
130 | public void testGetCurrentEvent() { | |
e8ece272 | 131 | assertNotNull(fixture.getCurrentEvent()); |
866e5b51 FC |
132 | } |
133 | ||
134 | /** | |
135 | * Run the StructDefinition getCurrentPacketContext() method test. | |
136 | */ | |
137 | @Test | |
138 | public void testGetCurrentPacketContext() { | |
e8ece272 | 139 | IEventDefinition currentEvent = fixture.getCurrentEvent(); |
90cefe9f MK |
140 | assertNotNull(currentEvent); |
141 | ICompositeDefinition result = currentEvent.getPacketContext(); | |
866e5b51 FC |
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 | ||
866e5b51 FC |
154 | /** |
155 | * Run the void goToLastEvent() method test. | |
a4fa4e36 | 156 | * |
680f9173 | 157 | * @throws CTFException |
a4fa4e36 | 158 | * error |
866e5b51 FC |
159 | */ |
160 | @Test | |
680f9173 | 161 | public void testGoToLastEvent1() throws CTFException { |
ec6f5beb MK |
162 | final long endTimestamp = goToEnd(); |
163 | final long endTime = 4287422460315L; | |
a4fa4e36 | 164 | assertEquals(endTime, endTimestamp); |
ec6f5beb MK |
165 | } |
166 | ||
167 | /** | |
168 | * Run the void goToLastEvent() method test. | |
a4fa4e36 | 169 | * |
680f9173 | 170 | * @throws CTFException |
a4fa4e36 | 171 | * error |
ec6f5beb MK |
172 | */ |
173 | @Test | |
680f9173 | 174 | public void testGoToLastEvent2() throws CTFException { |
ec6f5beb | 175 | long timestamp = -1; |
a4fa4e36 | 176 | while (fixture.readNextEvent().equals(CTFResponse.OK)) { |
e8ece272 | 177 | IEventDefinition currentEvent = fixture.getCurrentEvent(); |
90cefe9f MK |
178 | assertNotNull(currentEvent); |
179 | timestamp = currentEvent.getTimestamp(); | |
ec6f5beb MK |
180 | } |
181 | long endTimestamp = goToEnd(); | |
a4fa4e36 | 182 | assertEquals(0, timestamp - endTimestamp); |
ec6f5beb MK |
183 | } |
184 | ||
680f9173 | 185 | private long goToEnd() throws CTFException { |
866e5b51 | 186 | fixture.goToLastEvent(); |
e8ece272 | 187 | IEventDefinition currentEvent = fixture.getCurrentEvent(); |
90cefe9f MK |
188 | assertNotNull(currentEvent); |
189 | return currentEvent.getTimestamp(); | |
866e5b51 FC |
190 | } |
191 | ||
192 | /** | |
193 | * Run the boolean readNextEvent() method test. | |
a4fa4e36 | 194 | * |
680f9173 | 195 | * @throws CTFException |
a4fa4e36 | 196 | * error |
866e5b51 FC |
197 | */ |
198 | @Test | |
680f9173 | 199 | public void testReadNextEvent() throws CTFException { |
6a5251eb | 200 | assertEquals(CTFResponse.OK, fixture.readNextEvent()); |
866e5b51 FC |
201 | } |
202 | ||
203 | /** | |
204 | * Run the void seek(long) method test. Seek by direct timestamp | |
a4fa4e36 | 205 | * |
680f9173 | 206 | * @throws CTFException |
a4fa4e36 | 207 | * error |
866e5b51 FC |
208 | */ |
209 | @Test | |
680f9173 | 210 | public void testSeek_timestamp() throws CTFException { |
866e5b51 FC |
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. | |
ce2388e0 | 218 | * |
680f9173 | 219 | * @throws CTFException |
6b8f960d MK |
220 | * error |
221 | * @throws IOException | |
222 | * file not there | |
866e5b51 FC |
223 | */ |
224 | @Test | |
6b8f960d MK |
225 | public void testSeek_eventDefinition() throws CTFException, IOException { |
226 | try (CTFStreamInputReader streamInputReader = getStreamInputReader()) { | |
227 | EventDefinition eventDefinition = new EventDefinition( | |
228 | new EventDeclaration(), streamInputReader.getCPU(), 1L, null, null, null, null, null, streamInputReader.getCurrentPacketReader().getCurrentPacket()); | |
229 | fixture.setCurrentEvent(eventDefinition); | |
230 | } | |
866e5b51 | 231 | } |
05ce5fef | 232 | } |