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 | ||
866e5b51 FC |
12 | package org.eclipse.linuxtools.ctf.core.tests.trace; |
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 | ||
866e5b51 | 22 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
a4fa4e36 MK |
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; | |
866e5b51 | 27 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; |
9ac63b5b | 28 | import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace; |
866e5b51 | 29 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
6a5251eb | 30 | import org.eclipse.linuxtools.ctf.core.trace.CTFResponse; |
866e5b51 | 31 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; |
d84419e1 AM |
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; | |
8e964be1 | 35 | import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration; |
866e5b51 FC |
36 | import org.junit.Before; |
37 | import org.junit.Test; | |
38 | ||
a4fa4e36 MK |
39 | import com.google.common.collect.ImmutableList; |
40 | ||
866e5b51 FC |
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 | |
a4fa4e36 MK |
55 | private static ImmutableList<String> wrap(String s) { |
56 | return ImmutableList.<String> builder().add(s).build(); | |
57 | } | |
58 | ||
866e5b51 FC |
59 | /** |
60 | * Perform pre-test initialization. | |
ce2388e0 FC |
61 | * |
62 | * @throws CTFReaderException | |
866e5b51 FC |
63 | */ |
64 | @Before | |
13be1a8f AM |
65 | public void setUp() throws CTFReaderException { |
66 | fixture = getStreamInputReader(); | |
866e5b51 FC |
67 | fixture.setName(1); |
68 | fixture.setCurrentEvent(new EventDefinition(new EventDeclaration(), | |
a4fa4e36 MK |
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 | ); | |
866e5b51 FC |
78 | } |
79 | ||
d84419e1 | 80 | private static CTFStreamInputReader getStreamInputReader() throws CTFReaderException { |
9ac63b5b AM |
81 | assumeTrue(testTrace.exists()); |
82 | CTFTrace trace = testTrace.getTrace(); | |
d84419e1 AM |
83 | CTFStream s = trace.getStream((long) 0); |
84 | Set<CTFStreamInput> streamInput = s.getStreamInputs(); | |
85 | CTFStreamInputReader retVal = null; | |
86 | for (CTFStreamInput si : streamInput) { | |
866e5b51 FC |
87 | /* |
88 | * For the tests, we'll use the stream input corresponding to the | |
89 | * CPU 0 | |
90 | */ | |
4a9c1f07 | 91 | if (si.getFilename().endsWith("0_0")) { |
d84419e1 | 92 | retVal = new CTFStreamInputReader(si); |
866e5b51 FC |
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. | |
ce2388e0 | 111 | * |
866e5b51 | 112 | * @throws CTFReaderException |
05ce5fef | 113 | * @throws IOException |
866e5b51 FC |
114 | */ |
115 | @Test(expected = CTFReaderException.class) | |
05ce5fef AM |
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 | } | |
866e5b51 FC |
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() { | |
a4fa4e36 | 146 | StructDefinition result = fixture.getCurrentEvent().getPacketContext(); |
866e5b51 FC |
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 | ||
866e5b51 FC |
159 | /** |
160 | * Run the void goToLastEvent() method test. | |
a4fa4e36 MK |
161 | * |
162 | * @throws CTFReaderException | |
163 | * error | |
866e5b51 FC |
164 | */ |
165 | @Test | |
db8e8f7d | 166 | public void testGoToLastEvent1() throws CTFReaderException { |
ec6f5beb MK |
167 | final long endTimestamp = goToEnd(); |
168 | final long endTime = 4287422460315L; | |
a4fa4e36 | 169 | assertEquals(endTime, endTimestamp); |
ec6f5beb MK |
170 | } |
171 | ||
172 | /** | |
173 | * Run the void goToLastEvent() method test. | |
a4fa4e36 MK |
174 | * |
175 | * @throws CTFReaderException | |
176 | * error | |
ec6f5beb MK |
177 | */ |
178 | @Test | |
db8e8f7d | 179 | public void testGoToLastEvent2() throws CTFReaderException { |
ec6f5beb | 180 | long timestamp = -1; |
a4fa4e36 | 181 | while (fixture.readNextEvent().equals(CTFResponse.OK)) { |
ec6f5beb MK |
182 | timestamp = fixture.getCurrentEvent().getTimestamp(); |
183 | } | |
184 | long endTimestamp = goToEnd(); | |
a4fa4e36 | 185 | assertEquals(0, timestamp - endTimestamp); |
ec6f5beb MK |
186 | } |
187 | ||
db8e8f7d | 188 | private long goToEnd() throws CTFReaderException { |
866e5b51 | 189 | fixture.goToLastEvent(); |
ec6f5beb | 190 | return fixture.getCurrentEvent().getTimestamp(); |
866e5b51 FC |
191 | } |
192 | ||
193 | /** | |
194 | * Run the boolean readNextEvent() method test. | |
a4fa4e36 MK |
195 | * |
196 | * @throws CTFReaderException | |
197 | * error | |
866e5b51 FC |
198 | */ |
199 | @Test | |
db8e8f7d | 200 | public void testReadNextEvent() throws CTFReaderException { |
6a5251eb | 201 | assertEquals(CTFResponse.OK, fixture.readNextEvent()); |
866e5b51 FC |
202 | } |
203 | ||
204 | /** | |
205 | * Run the void seek(long) method test. Seek by direct timestamp | |
a4fa4e36 MK |
206 | * |
207 | * @throws CTFReaderException | |
208 | * error | |
866e5b51 FC |
209 | */ |
210 | @Test | |
db8e8f7d | 211 | public void testSeek_timestamp() throws CTFReaderException { |
866e5b51 FC |
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. | |
ce2388e0 FC |
219 | * |
220 | * @throws CTFReaderException | |
866e5b51 FC |
221 | */ |
222 | @Test | |
13be1a8f | 223 | public void testSeek_eventDefinition() throws CTFReaderException { |
866e5b51 | 224 | EventDefinition eventDefinition = new EventDefinition( |
a4fa4e36 | 225 | new EventDeclaration(), getStreamInputReader(), 1L, null, null, null, null); |
866e5b51 FC |
226 | fixture.setCurrentEvent(eventDefinition); |
227 | } | |
05ce5fef | 228 | } |