Analysis: Add unit tests for the critical path module
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / trace / CTFStreamInputReaderTest.java
CommitLineData
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 12package org.eclipse.tracecompass.ctf.core.tests.trace;
866e5b51
FC
13
14import static org.junit.Assert.assertEquals;
15import static org.junit.Assert.assertNotNull;
e5acb357 16import static org.junit.Assume.assumeTrue;
866e5b51 17
9ac63b5b 18import java.io.File;
05ce5fef 19import java.io.IOException;
866e5b51
FC
20import java.util.Set;
21
680f9173 22import org.eclipse.tracecompass.ctf.core.CTFException;
f357bcd4
AM
23import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
24import org.eclipse.tracecompass.ctf.core.event.types.Definition;
d890ec37 25import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
778bce67 26import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
f357bcd4
AM
27import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration;
28import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition;
29import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
30import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
31import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTrace;
f357bcd4
AM
32import org.eclipse.tracecompass.ctf.core.trace.CTFResponse;
33import org.eclipse.tracecompass.ctf.core.trace.CTFStream;
34import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInput;
35import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInputReader;
36import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
37import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration;
866e5b51
FC
38import org.junit.Before;
39import 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 49public 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() {
90cefe9f
MK
141 EventDefinition currentEvent = fixture.getCurrentEvent();
142 assertNotNull(currentEvent);
143 ICompositeDefinition result = currentEvent.getPacketContext();
866e5b51
FC
144 assertNotNull(result);
145 }
146
147 /**
148 * Run the int getName() method test.
149 */
150 @Test
151 public void testGetName() {
152 int result = fixture.getName();
153 assertEquals(1, result);
154 }
155
866e5b51
FC
156 /**
157 * Run the void goToLastEvent() method test.
a4fa4e36 158 *
680f9173 159 * @throws CTFException
a4fa4e36 160 * error
866e5b51
FC
161 */
162 @Test
680f9173 163 public void testGoToLastEvent1() throws CTFException {
ec6f5beb
MK
164 final long endTimestamp = goToEnd();
165 final long endTime = 4287422460315L;
a4fa4e36 166 assertEquals(endTime, endTimestamp);
ec6f5beb
MK
167 }
168
169 /**
170 * Run the void goToLastEvent() method test.
a4fa4e36 171 *
680f9173 172 * @throws CTFException
a4fa4e36 173 * error
ec6f5beb
MK
174 */
175 @Test
680f9173 176 public void testGoToLastEvent2() throws CTFException {
ec6f5beb 177 long timestamp = -1;
a4fa4e36 178 while (fixture.readNextEvent().equals(CTFResponse.OK)) {
90cefe9f
MK
179 EventDefinition currentEvent = fixture.getCurrentEvent();
180 assertNotNull(currentEvent);
181 timestamp = currentEvent.getTimestamp();
ec6f5beb
MK
182 }
183 long endTimestamp = goToEnd();
a4fa4e36 184 assertEquals(0, timestamp - endTimestamp);
ec6f5beb
MK
185 }
186
680f9173 187 private long goToEnd() throws CTFException {
866e5b51 188 fixture.goToLastEvent();
90cefe9f
MK
189 EventDefinition currentEvent = fixture.getCurrentEvent();
190 assertNotNull(currentEvent);
191 return currentEvent.getTimestamp();
866e5b51
FC
192 }
193
194 /**
195 * Run the boolean readNextEvent() method test.
a4fa4e36 196 *
680f9173 197 * @throws CTFException
a4fa4e36 198 * error
866e5b51
FC
199 */
200 @Test
680f9173 201 public void testReadNextEvent() throws CTFException {
6a5251eb 202 assertEquals(CTFResponse.OK, fixture.readNextEvent());
866e5b51
FC
203 }
204
205 /**
206 * Run the void seek(long) method test. Seek by direct timestamp
a4fa4e36 207 *
680f9173 208 * @throws CTFException
a4fa4e36 209 * error
866e5b51
FC
210 */
211 @Test
680f9173 212 public void testSeek_timestamp() throws CTFException {
866e5b51
FC
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.
ce2388e0 220 *
680f9173 221 * @throws CTFException
866e5b51
FC
222 */
223 @Test
680f9173 224 public void testSeek_eventDefinition() throws CTFException {
866e5b51 225 EventDefinition eventDefinition = new EventDefinition(
408f954e 226 new EventDeclaration(), getStreamInputReader().getCPU(), 1L, null, null, null, null, null);
866e5b51
FC
227 fixture.setCurrentEvent(eventDefinition);
228 }
05ce5fef 229}
This page took 0.073775 seconds and 5 git commands to generate.