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; | |
e5acb357 | 16 | import static org.junit.Assume.assumeTrue; |
866e5b51 | 17 | |
9ac63b5b | 18 | import java.io.File; |
05ce5fef | 19 | import java.io.IOException; |
866e5b51 FC |
20 | import java.util.Set; |
21 | ||
680f9173 | 22 | import org.eclipse.tracecompass.ctf.core.CTFException; |
f357bcd4 AM |
23 | import org.eclipse.tracecompass.ctf.core.event.EventDefinition; |
24 | import org.eclipse.tracecompass.ctf.core.event.types.Definition; | |
d890ec37 | 25 | import org.eclipse.tracecompass.ctf.core.event.types.Encoding; |
778bce67 | 26 | import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition; |
f357bcd4 AM |
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; | |
f357bcd4 AM |
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; | |
866e5b51 FC |
38 | import org.junit.Before; |
39 | import org.junit.Test; | |
40 | ||
41 | /** | |
42 | * The class <code>StreamInputReaderTest</code> contains tests for the class | |
d84419e1 | 43 | * <code>{@link CTFStreamInputReader}</code>. |
ce2388e0 | 44 | * |
866e5b51 FC |
45 | * @author ematkho |
46 | * @version $Revision: 1.0 $ | |
47 | */ | |
be6df2d8 | 48 | @SuppressWarnings("javadoc") |
d84419e1 | 49 | public class CTFStreamInputReaderTest { |
866e5b51 | 50 | |
9ac63b5b | 51 | private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL; |
32bf80d2 | 52 | |
d84419e1 | 53 | private CTFStreamInputReader fixture; |
866e5b51 | 54 | |
866e5b51 FC |
55 | /** |
56 | * Perform pre-test initialization. | |
ce2388e0 | 57 | * |
680f9173 | 58 | * @throws CTFException |
866e5b51 FC |
59 | */ |
60 | @Before | |
680f9173 | 61 | public void setUp() throws CTFException { |
13be1a8f | 62 | fixture = getStreamInputReader(); |
866e5b51 FC |
63 | fixture.setName(1); |
64 | fixture.setCurrentEvent(new EventDefinition(new EventDeclaration(), | |
408f954e | 65 | getStreamInputReader().getCPU(), 0, null, null, null, |
a4fa4e36 MK |
66 | new StructDefinition( |
67 | new StructDeclaration(0), | |
68 | null, | |
69 | "packet", | |
d890ec37 | 70 | new Definition[] { new StringDefinition(StringDeclaration.getStringDeclaration(Encoding.UTF8), null, "field", "test") }), |
a4fa4e36 MK |
71 | null) |
72 | ); | |
866e5b51 FC |
73 | } |
74 | ||
680f9173 | 75 | private static CTFStreamInputReader getStreamInputReader() throws CTFException { |
9ac63b5b AM |
76 | assumeTrue(testTrace.exists()); |
77 | CTFTrace trace = testTrace.getTrace(); | |
d84419e1 AM |
78 | CTFStream s = trace.getStream((long) 0); |
79 | Set<CTFStreamInput> streamInput = s.getStreamInputs(); | |
80 | CTFStreamInputReader retVal = null; | |
81 | for (CTFStreamInput si : streamInput) { | |
866e5b51 FC |
82 | /* |
83 | * For the tests, we'll use the stream input corresponding to the | |
84 | * CPU 0 | |
85 | */ | |
4a9c1f07 | 86 | if (si.getFilename().endsWith("0_0")) { |
d84419e1 | 87 | retVal = new CTFStreamInputReader(si); |
866e5b51 FC |
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. | |
ce2388e0 | 106 | * |
680f9173 | 107 | * @throws CTFException |
05ce5fef | 108 | * @throws IOException |
866e5b51 | 109 | */ |
680f9173 MK |
110 | @Test(expected = CTFException.class) |
111 | public void testStreamInputReader_invalid() throws CTFException, IOException { | |
46167f03 MK |
112 | CTFStreamInput streamInput = new CTFStreamInput(new CTFStream(new CTFTrace("")), new File("")); |
113 | try (CTFStreamInputReader result = new CTFStreamInputReader(streamInput)) { | |
05ce5fef AM |
114 | assertNotNull(result); |
115 | } | |
866e5b51 FC |
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() { | |
778bce67 | 141 | ICompositeDefinition result = fixture.getCurrentEvent().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)) { |
ec6f5beb MK |
177 | timestamp = fixture.getCurrentEvent().getTimestamp(); |
178 | } | |
179 | long endTimestamp = goToEnd(); | |
a4fa4e36 | 180 | assertEquals(0, timestamp - endTimestamp); |
ec6f5beb MK |
181 | } |
182 | ||
680f9173 | 183 | private long goToEnd() throws CTFException { |
866e5b51 | 184 | fixture.goToLastEvent(); |
ec6f5beb | 185 | return fixture.getCurrentEvent().getTimestamp(); |
866e5b51 FC |
186 | } |
187 | ||
188 | /** | |
189 | * Run the boolean readNextEvent() method test. | |
a4fa4e36 | 190 | * |
680f9173 | 191 | * @throws CTFException |
a4fa4e36 | 192 | * error |
866e5b51 FC |
193 | */ |
194 | @Test | |
680f9173 | 195 | public void testReadNextEvent() throws CTFException { |
6a5251eb | 196 | assertEquals(CTFResponse.OK, fixture.readNextEvent()); |
866e5b51 FC |
197 | } |
198 | ||
199 | /** | |
200 | * Run the void seek(long) method test. Seek by direct timestamp | |
a4fa4e36 | 201 | * |
680f9173 | 202 | * @throws CTFException |
a4fa4e36 | 203 | * error |
866e5b51 FC |
204 | */ |
205 | @Test | |
680f9173 | 206 | public void testSeek_timestamp() throws CTFException { |
866e5b51 FC |
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. | |
ce2388e0 | 214 | * |
680f9173 | 215 | * @throws CTFException |
866e5b51 FC |
216 | */ |
217 | @Test | |
680f9173 | 218 | public void testSeek_eventDefinition() throws CTFException { |
866e5b51 | 219 | EventDefinition eventDefinition = new EventDefinition( |
408f954e | 220 | new EventDeclaration(), getStreamInputReader().getCPU(), 1L, null, null, null, null, null); |
866e5b51 FC |
221 | fixture.setCurrentEvent(eventDefinition); |
222 | } | |
05ce5fef | 223 | } |