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; |
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; | |
30 | import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTrace; | |
f357bcd4 AM |
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; | |
866e5b51 FC |
37 | import org.junit.Before; |
38 | import org.junit.Test; | |
39 | ||
40 | /** | |
41 | * The class <code>StreamInputReaderTest</code> contains tests for the class | |
d84419e1 | 42 | * <code>{@link CTFStreamInputReader}</code>. |
ce2388e0 | 43 | * |
866e5b51 FC |
44 | * @author ematkho |
45 | * @version $Revision: 1.0 $ | |
46 | */ | |
be6df2d8 | 47 | @SuppressWarnings("javadoc") |
d84419e1 | 48 | public class CTFStreamInputReaderTest { |
866e5b51 | 49 | |
9ac63b5b | 50 | private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL; |
32bf80d2 | 51 | |
d84419e1 | 52 | private CTFStreamInputReader fixture; |
866e5b51 | 53 | |
866e5b51 FC |
54 | /** |
55 | * Perform pre-test initialization. | |
ce2388e0 | 56 | * |
680f9173 | 57 | * @throws CTFException |
866e5b51 FC |
58 | */ |
59 | @Before | |
680f9173 | 60 | public void setUp() throws CTFException { |
13be1a8f | 61 | fixture = getStreamInputReader(); |
866e5b51 FC |
62 | fixture.setName(1); |
63 | fixture.setCurrentEvent(new EventDefinition(new EventDeclaration(), | |
a4fa4e36 MK |
64 | getStreamInputReader(), 0, null, null, |
65 | new StructDefinition( | |
66 | new StructDeclaration(0), | |
67 | null, | |
68 | "packet", | |
d890ec37 | 69 | new Definition[] { new StringDefinition(StringDeclaration.getStringDeclaration(Encoding.UTF8), null, "field", "test") }), |
a4fa4e36 MK |
70 | null) |
71 | ); | |
866e5b51 FC |
72 | } |
73 | ||
680f9173 | 74 | private static CTFStreamInputReader getStreamInputReader() throws CTFException { |
9ac63b5b AM |
75 | assumeTrue(testTrace.exists()); |
76 | CTFTrace trace = testTrace.getTrace(); | |
d84419e1 AM |
77 | CTFStream s = trace.getStream((long) 0); |
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() { | |
131 | EventDefinition result = fixture.getCurrentEvent(); | |
132 | assertNotNull(result); | |
133 | } | |
134 | ||
135 | /** | |
136 | * Run the StructDefinition getCurrentPacketContext() method test. | |
137 | */ | |
138 | @Test | |
139 | public void testGetCurrentPacketContext() { | |
a4fa4e36 | 140 | StructDefinition result = fixture.getCurrentEvent().getPacketContext(); |
866e5b51 FC |
141 | assertNotNull(result); |
142 | } | |
143 | ||
144 | /** | |
145 | * Run the int getName() method test. | |
146 | */ | |
147 | @Test | |
148 | public void testGetName() { | |
149 | int result = fixture.getName(); | |
150 | assertEquals(1, result); | |
151 | } | |
152 | ||
866e5b51 FC |
153 | /** |
154 | * Run the void goToLastEvent() method test. | |
a4fa4e36 | 155 | * |
680f9173 | 156 | * @throws CTFException |
a4fa4e36 | 157 | * error |
866e5b51 FC |
158 | */ |
159 | @Test | |
680f9173 | 160 | public void testGoToLastEvent1() throws CTFException { |
ec6f5beb MK |
161 | final long endTimestamp = goToEnd(); |
162 | final long endTime = 4287422460315L; | |
a4fa4e36 | 163 | assertEquals(endTime, endTimestamp); |
ec6f5beb MK |
164 | } |
165 | ||
166 | /** | |
167 | * Run the void goToLastEvent() method test. | |
a4fa4e36 | 168 | * |
680f9173 | 169 | * @throws CTFException |
a4fa4e36 | 170 | * error |
ec6f5beb MK |
171 | */ |
172 | @Test | |
680f9173 | 173 | public void testGoToLastEvent2() throws CTFException { |
ec6f5beb | 174 | long timestamp = -1; |
a4fa4e36 | 175 | while (fixture.readNextEvent().equals(CTFResponse.OK)) { |
ec6f5beb MK |
176 | timestamp = fixture.getCurrentEvent().getTimestamp(); |
177 | } | |
178 | long endTimestamp = goToEnd(); | |
a4fa4e36 | 179 | assertEquals(0, timestamp - endTimestamp); |
ec6f5beb MK |
180 | } |
181 | ||
680f9173 | 182 | private long goToEnd() throws CTFException { |
866e5b51 | 183 | fixture.goToLastEvent(); |
ec6f5beb | 184 | return fixture.getCurrentEvent().getTimestamp(); |
866e5b51 FC |
185 | } |
186 | ||
187 | /** | |
188 | * Run the boolean readNextEvent() method test. | |
a4fa4e36 | 189 | * |
680f9173 | 190 | * @throws CTFException |
a4fa4e36 | 191 | * error |
866e5b51 FC |
192 | */ |
193 | @Test | |
680f9173 | 194 | public void testReadNextEvent() throws CTFException { |
6a5251eb | 195 | assertEquals(CTFResponse.OK, fixture.readNextEvent()); |
866e5b51 FC |
196 | } |
197 | ||
198 | /** | |
199 | * Run the void seek(long) method test. Seek by direct timestamp | |
a4fa4e36 | 200 | * |
680f9173 | 201 | * @throws CTFException |
a4fa4e36 | 202 | * error |
866e5b51 FC |
203 | */ |
204 | @Test | |
680f9173 | 205 | public void testSeek_timestamp() throws CTFException { |
866e5b51 FC |
206 | long timestamp = 1L; |
207 | fixture.seek(timestamp); | |
208 | } | |
209 | ||
210 | /** | |
211 | * Run the seek test. Seek by passing an EventDefinition to which we've | |
212 | * given the timestamp we want. | |
ce2388e0 | 213 | * |
680f9173 | 214 | * @throws CTFException |
866e5b51 FC |
215 | */ |
216 | @Test | |
680f9173 | 217 | public void testSeek_eventDefinition() throws CTFException { |
866e5b51 | 218 | EventDefinition eventDefinition = new EventDefinition( |
a4fa4e36 | 219 | new EventDeclaration(), getStreamInputReader(), 1L, null, null, null, null); |
866e5b51 FC |
220 | fixture.setCurrentEvent(eventDefinition); |
221 | } | |
05ce5fef | 222 | } |