tmf: Rework test trace classes
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / trace / StreamInputReaderTest.java
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
12 package org.eclipse.linuxtools.ctf.core.tests.trace;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assume.assumeTrue;
18
19 import java.io.File;
20 import java.nio.channels.FileChannel;
21 import java.util.Set;
22
23 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
24 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
25 import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace;
26 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
27 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
28 import org.eclipse.linuxtools.ctf.core.trace.Stream;
29 import org.eclipse.linuxtools.ctf.core.trace.StreamInput;
30 import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
31 import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration;
32 import org.junit.Before;
33 import org.junit.Test;
34
35 /**
36 * The class <code>StreamInputReaderTest</code> contains tests for the class
37 * <code>{@link StreamInputReader}</code>.
38 *
39 * @author ematkho
40 * @version $Revision: 1.0 $
41 */
42 @SuppressWarnings("javadoc")
43 public class StreamInputReaderTest {
44
45 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
46
47 private StreamInputReader fixture;
48
49 /**
50 * Perform pre-test initialization.
51 *
52 * @throws CTFReaderException
53 */
54 @Before
55 public void setUp() throws CTFReaderException {
56 fixture = getStreamInputReader();
57 fixture.setName(1);
58 fixture.setCurrentEvent(new EventDefinition(new EventDeclaration(),
59 getStreamInputReader()));
60 }
61
62 private static StreamInputReader getStreamInputReader() throws CTFReaderException {
63 assumeTrue(testTrace.exists());
64 CTFTrace trace = testTrace.getTrace();
65 Stream s = trace.getStream((long) 0);
66 Set<StreamInput> streamInput = s.getStreamInputs();
67 StreamInputReader retVal = null;
68 for (StreamInput si : streamInput) {
69 /*
70 * For the tests, we'll use the stream input corresponding to the
71 * CPU 0
72 */
73 if (si.getFilename().endsWith("0_0")) {
74 retVal = new StreamInputReader(si);
75 break;
76 }
77 }
78 return retVal;
79 }
80
81 /**
82 * Run the StreamInputReader(StreamInput) constructor test, with a valid
83 * trace.
84 */
85 @Test
86 public void testStreamInputReader_valid() {
87 assertNotNull(fixture);
88 }
89
90 /**
91 * Run the StreamInputReader(StreamInput) constructor test, with an invalid
92 * trace.
93 *
94 * @throws CTFReaderException
95 */
96 @Test(expected = CTFReaderException.class)
97 public void testStreamInputReader_invalid() throws CTFReaderException {
98 StreamInput streamInput = new StreamInput(
99 new Stream(new CTFTrace("")), (FileChannel) null, new File(""));
100
101 StreamInputReader result = new StreamInputReader(streamInput);
102 assertNotNull(result);
103 }
104
105 /**
106 * Run the int getCPU() method test.
107 */
108 @Test
109 public void testGetCPU() {
110 int result = fixture.getCPU();
111 assertEquals(0, result);
112 }
113
114 /**
115 * Run the EventDefinition getCurrentEvent() method test.
116 */
117 @Test
118 public void testGetCurrentEvent() {
119 EventDefinition result = fixture.getCurrentEvent();
120 assertNotNull(result);
121 }
122
123 /**
124 * Run the StructDefinition getCurrentPacketContext() method test.
125 */
126 @Test
127 public void testGetCurrentPacketContext() {
128 StructDefinition result = fixture.getCurrentPacketContext();
129 assertNotNull(result);
130 }
131
132 /**
133 * Run the int getName() method test.
134 */
135 @Test
136 public void testGetName() {
137 int result = fixture.getName();
138 assertEquals(1, result);
139 }
140
141 /**
142 * Run the void goToLastEvent() method test.
143 */
144 @Test
145 public void testGoToLastEvent1() {
146 final long endTimestamp = goToEnd();
147 final long endTime = 4287422460315L;
148 assertEquals(endTime , endTimestamp );
149 }
150
151 /**
152 * Run the void goToLastEvent() method test.
153 */
154 @Test
155 public void testGoToLastEvent2() {
156 long timestamp = -1;
157 while(fixture.readNextEvent()) {
158 timestamp = fixture.getCurrentEvent().getTimestamp();
159 }
160 long endTimestamp = goToEnd();
161 assertEquals(0 , timestamp- endTimestamp );
162 }
163
164 private long goToEnd() {
165 fixture.goToLastEvent();
166 return fixture.getCurrentEvent().getTimestamp();
167 }
168
169 /**
170 * Run the boolean readNextEvent() method test.
171 */
172 @Test
173 public void testReadNextEvent() {
174 boolean result = fixture.readNextEvent();
175 assertTrue(result);
176 }
177
178 /**
179 * Run the void seek(long) method test. Seek by direct timestamp
180 */
181 @Test
182 public void testSeek_timestamp() {
183 long timestamp = 1L;
184 fixture.seek(timestamp);
185 }
186
187 /**
188 * Run the seek test. Seek by passing an EventDefinition to which we've
189 * given the timestamp we want.
190 *
191 * @throws CTFReaderException
192 */
193 @Test
194 public void testSeek_eventDefinition() throws CTFReaderException {
195 EventDefinition eventDefinition = new EventDefinition(
196 new EventDeclaration(), getStreamInputReader());
197 eventDefinition.setTimestamp(1L);
198 fixture.setCurrentEvent(eventDefinition);
199 }
200 }
This page took 0.038558 seconds and 5 git commands to generate.