lttng: Enable "potential resource leak" warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / 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
866e5b51
FC
12package org.eclipse.linuxtools.ctf.core.tests.trace;
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;
866e5b51
FC
19import java.util.Set;
20
866e5b51 21import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
a4fa4e36
MK
22import org.eclipse.linuxtools.ctf.core.event.types.Definition;
23import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
24import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
25import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
866e5b51 26import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
9ac63b5b 27import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace;
866e5b51 28import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
6a5251eb 29import org.eclipse.linuxtools.ctf.core.trace.CTFResponse;
866e5b51 30import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
d84419e1
AM
31import org.eclipse.linuxtools.ctf.core.trace.CTFStream;
32import org.eclipse.linuxtools.ctf.core.trace.CTFStreamInput;
33import org.eclipse.linuxtools.ctf.core.trace.CTFStreamInputReader;
8e964be1 34import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration;
866e5b51
FC
35import org.junit.Before;
36import org.junit.Test;
37
a4fa4e36
MK
38import com.google.common.collect.ImmutableList;
39
866e5b51
FC
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 48public class CTFStreamInputReaderTest {
866e5b51 49
9ac63b5b 50 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
32bf80d2 51
d84419e1 52 private CTFStreamInputReader fixture;
866e5b51 53
a4fa4e36
MK
54 private static ImmutableList<String> wrap(String s) {
55 return ImmutableList.<String> builder().add(s).build();
56 }
57
866e5b51
FC
58 /**
59 * Perform pre-test initialization.
ce2388e0
FC
60 *
61 * @throws CTFReaderException
866e5b51
FC
62 */
63 @Before
13be1a8f
AM
64 public void setUp() throws CTFReaderException {
65 fixture = getStreamInputReader();
866e5b51
FC
66 fixture.setName(1);
67 fixture.setCurrentEvent(new EventDefinition(new EventDeclaration(),
a4fa4e36
MK
68 getStreamInputReader(), 0, null, null,
69 new StructDefinition(
70 new StructDeclaration(0),
71 null,
72 "packet",
73 wrap( "field" ),
74 new Definition[] { new StringDefinition(new StringDeclaration(), null, "field", "test") }),
75 null)
76 );
866e5b51
FC
77 }
78
d84419e1 79 private static CTFStreamInputReader getStreamInputReader() throws CTFReaderException {
9ac63b5b
AM
80 assumeTrue(testTrace.exists());
81 CTFTrace trace = testTrace.getTrace();
d84419e1
AM
82 CTFStream s = trace.getStream((long) 0);
83 Set<CTFStreamInput> streamInput = s.getStreamInputs();
84 CTFStreamInputReader retVal = null;
85 for (CTFStreamInput si : streamInput) {
866e5b51
FC
86 /*
87 * For the tests, we'll use the stream input corresponding to the
88 * CPU 0
89 */
4a9c1f07 90 if (si.getFilename().endsWith("0_0")) {
d84419e1 91 retVal = new CTFStreamInputReader(si);
866e5b51
FC
92 break;
93 }
94 }
95 return retVal;
96 }
97
98 /**
99 * Run the StreamInputReader(StreamInput) constructor test, with a valid
100 * trace.
101 */
102 @Test
103 public void testStreamInputReader_valid() {
104 assertNotNull(fixture);
105 }
106
107 /**
108 * Run the StreamInputReader(StreamInput) constructor test, with an invalid
109 * trace.
ce2388e0 110 *
866e5b51
FC
111 * @throws CTFReaderException
112 */
113 @Test(expected = CTFReaderException.class)
114 public void testStreamInputReader_invalid() throws CTFReaderException {
d84419e1
AM
115 CTFStreamInput streamInput = new CTFStreamInput(
116 new CTFStream(new CTFTrace("")), new File(""));
866e5b51 117
d84419e1 118 CTFStreamInputReader result = new CTFStreamInputReader(streamInput);
866e5b51
FC
119 assertNotNull(result);
120 }
121
122 /**
123 * Run the int getCPU() method test.
124 */
125 @Test
126 public void testGetCPU() {
127 int result = fixture.getCPU();
128 assertEquals(0, result);
129 }
130
131 /**
132 * Run the EventDefinition getCurrentEvent() method test.
133 */
134 @Test
135 public void testGetCurrentEvent() {
136 EventDefinition result = fixture.getCurrentEvent();
137 assertNotNull(result);
138 }
139
140 /**
141 * Run the StructDefinition getCurrentPacketContext() method test.
142 */
143 @Test
144 public void testGetCurrentPacketContext() {
a4fa4e36 145 StructDefinition result = fixture.getCurrentEvent().getPacketContext();
866e5b51
FC
146 assertNotNull(result);
147 }
148
149 /**
150 * Run the int getName() method test.
151 */
152 @Test
153 public void testGetName() {
154 int result = fixture.getName();
155 assertEquals(1, result);
156 }
157
866e5b51
FC
158 /**
159 * Run the void goToLastEvent() method test.
a4fa4e36
MK
160 *
161 * @throws CTFReaderException
162 * error
866e5b51
FC
163 */
164 @Test
db8e8f7d 165 public void testGoToLastEvent1() throws CTFReaderException {
ec6f5beb
MK
166 final long endTimestamp = goToEnd();
167 final long endTime = 4287422460315L;
a4fa4e36 168 assertEquals(endTime, endTimestamp);
ec6f5beb
MK
169 }
170
171 /**
172 * Run the void goToLastEvent() method test.
a4fa4e36
MK
173 *
174 * @throws CTFReaderException
175 * error
ec6f5beb
MK
176 */
177 @Test
db8e8f7d 178 public void testGoToLastEvent2() throws CTFReaderException {
ec6f5beb 179 long timestamp = -1;
a4fa4e36 180 while (fixture.readNextEvent().equals(CTFResponse.OK)) {
ec6f5beb
MK
181 timestamp = fixture.getCurrentEvent().getTimestamp();
182 }
183 long endTimestamp = goToEnd();
a4fa4e36 184 assertEquals(0, timestamp - endTimestamp);
ec6f5beb
MK
185 }
186
db8e8f7d 187 private long goToEnd() throws CTFReaderException {
866e5b51 188 fixture.goToLastEvent();
ec6f5beb 189 return fixture.getCurrentEvent().getTimestamp();
866e5b51
FC
190 }
191
192 /**
193 * Run the boolean readNextEvent() method test.
a4fa4e36
MK
194 *
195 * @throws CTFReaderException
196 * error
866e5b51
FC
197 */
198 @Test
db8e8f7d 199 public void testReadNextEvent() throws CTFReaderException {
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
MK
205 *
206 * @throws CTFReaderException
207 * error
866e5b51
FC
208 */
209 @Test
db8e8f7d 210 public void testSeek_timestamp() throws CTFReaderException {
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
FC
218 *
219 * @throws CTFReaderException
866e5b51
FC
220 */
221 @Test
13be1a8f 222 public void testSeek_eventDefinition() throws CTFReaderException {
866e5b51 223 EventDefinition eventDefinition = new EventDefinition(
a4fa4e36 224 new EventDeclaration(), getStreamInputReader(), 1L, null, null, null, null);
866e5b51
FC
225 fixture.setCurrentEvent(eventDefinition);
226 }
227}
This page took 0.051733 seconds and 5 git commands to generate.