tmf: Add a trace-getting method in TmfView
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / trace / StreamInputReaderTest.java
CommitLineData
4bd7f2db
AM
1/*******************************************************************************
2 * Copyright (c) 2013 Ericsson
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;
16import static org.junit.Assert.assertTrue;
e5acb357 17import static org.junit.Assume.assumeTrue;
866e5b51
FC
18
19import java.nio.channels.FileChannel;
20import java.util.Set;
21
866e5b51
FC
22import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
23import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
32bf80d2 24import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTraces;
866e5b51
FC
25import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
26import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
486efb2e
AM
27import org.eclipse.linuxtools.ctf.core.trace.Stream;
28import org.eclipse.linuxtools.ctf.core.trace.StreamInput;
866e5b51 29import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
8e964be1 30import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration;
866e5b51
FC
31import org.junit.After;
32import org.junit.Before;
33import org.junit.Test;
34
35/**
36 * The class <code>StreamInputReaderTest</code> contains tests for the class
37 * <code>{@link StreamInputReader}</code>.
ce2388e0 38 *
866e5b51
FC
39 * @author ematkho
40 * @version $Revision: 1.0 $
41 */
be6df2d8 42@SuppressWarnings("javadoc")
866e5b51
FC
43public class StreamInputReaderTest {
44
32bf80d2
AM
45 private static final int TRACE_INDEX = 0;
46
866e5b51
FC
47 private StreamInputReader fixture;
48
49 /**
50 * Launch the test.
ce2388e0 51 *
866e5b51
FC
52 * @param args
53 * the command line arguments
54 */
55 public static void main(String[] args) {
56 new org.junit.runner.JUnitCore().run(StreamInputReaderTest.class);
57 }
58
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(),
13be1a8f 69 getStreamInputReader()));
866e5b51
FC
70 }
71
72 /**
73 * Perform post-test clean-up.
74 */
75 @After
76 public void tearDown() {
77 // Add additional tear down code here
78 }
79
13be1a8f 80 private static StreamInputReader getStreamInputReader() throws CTFReaderException {
32bf80d2
AM
81 assumeTrue(CtfTestTraces.tracesExist());
82 CTFTrace trace = CtfTestTraces.getTestTrace(TRACE_INDEX);
866e5b51
FC
83 Stream s = trace.getStream((long) 0);
84 Set<StreamInput> streamInput = s.getStreamInputs();
85 StreamInputReader retVal = null;
86 for (StreamInput si : streamInput) {
87 /*
88 * For the tests, we'll use the stream input corresponding to the
89 * CPU 0
90 */
91 if (si.getFilename().endsWith("0_0")) { //$NON-NLS-1$
92 retVal = new StreamInputReader(si);
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
FC
112 * @throws CTFReaderException
113 */
114 @Test(expected = CTFReaderException.class)
115 public void testStreamInputReader_invalid() throws CTFReaderException {
116 StreamInput streamInput = new StreamInput(
32bf80d2 117 new Stream(new CTFTrace("")), (FileChannel) null, CtfTestTraces.getEmptyFile()); //$NON-NLS-1$
866e5b51
FC
118
119 StreamInputReader result = new StreamInputReader(streamInput);
120 assertNotNull(result);
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() {
146 StructDefinition result = fixture.getCurrentPacketContext();
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.
866e5b51
FC
161 */
162 @Test
be6df2d8 163 public void testGoToLastEvent1() {
ec6f5beb
MK
164 final long endTimestamp = goToEnd();
165 final long endTime = 4287422460315L;
166 assertEquals(endTime , endTimestamp );
167 }
168
169 /**
170 * Run the void goToLastEvent() method test.
ec6f5beb
MK
171 */
172 @Test
be6df2d8 173 public void testGoToLastEvent2() {
ec6f5beb
MK
174 long timestamp = -1;
175 while(fixture.readNextEvent()) {
176 timestamp = fixture.getCurrentEvent().getTimestamp();
177 }
178 long endTimestamp = goToEnd();
179 assertEquals(0 , timestamp- endTimestamp );
180 }
181
be6df2d8 182 private long goToEnd() {
866e5b51 183 fixture.goToLastEvent();
ec6f5beb 184 return fixture.getCurrentEvent().getTimestamp();
866e5b51
FC
185 }
186
187 /**
188 * Run the boolean readNextEvent() method test.
189 */
190 @Test
191 public void testReadNextEvent() {
192 boolean result = fixture.readNextEvent();
193 assertTrue(result);
194 }
195
196 /**
197 * Run the void seek(long) method test. Seek by direct timestamp
198 */
199 @Test
200 public void testSeek_timestamp() {
201 long timestamp = 1L;
202 fixture.seek(timestamp);
203 }
204
205 /**
206 * Run the seek test. Seek by passing an EventDefinition to which we've
207 * given the timestamp we want.
ce2388e0
FC
208 *
209 * @throws CTFReaderException
866e5b51
FC
210 */
211 @Test
13be1a8f 212 public void testSeek_eventDefinition() throws CTFReaderException {
866e5b51 213 EventDefinition eventDefinition = new EventDefinition(
13be1a8f 214 new EventDeclaration(), getStreamInputReader());
aa572e22 215 eventDefinition.setTimestamp(1L);
866e5b51
FC
216 fixture.setCurrentEvent(eventDefinition);
217 }
218}
This page took 0.046488 seconds and 5 git commands to generate.