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