Commit | Line | Data |
---|---|---|
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 |
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; | |
e5acb357 | 17 | import static org.junit.Assume.assumeTrue; |
866e5b51 | 18 | |
9ac63b5b | 19 | import java.io.File; |
866e5b51 FC |
20 | import java.nio.channels.FileChannel; |
21 | import java.util.Set; | |
22 | ||
866e5b51 FC |
23 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
24 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
9ac63b5b | 25 | import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace; |
866e5b51 FC |
26 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
27 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; | |
486efb2e AM |
28 | import org.eclipse.linuxtools.ctf.core.trace.Stream; |
29 | import org.eclipse.linuxtools.ctf.core.trace.StreamInput; | |
866e5b51 | 30 | import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; |
8e964be1 | 31 | import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration; |
866e5b51 FC |
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>. | |
ce2388e0 | 38 | * |
866e5b51 FC |
39 | * @author ematkho |
40 | * @version $Revision: 1.0 $ | |
41 | */ | |
be6df2d8 | 42 | @SuppressWarnings("javadoc") |
866e5b51 FC |
43 | public class StreamInputReaderTest { |
44 | ||
9ac63b5b | 45 | private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL; |
32bf80d2 | 46 | |
866e5b51 FC |
47 | private StreamInputReader fixture; |
48 | ||
866e5b51 FC |
49 | /** |
50 | * Perform pre-test initialization. | |
ce2388e0 FC |
51 | * |
52 | * @throws CTFReaderException | |
866e5b51 FC |
53 | */ |
54 | @Before | |
13be1a8f AM |
55 | public void setUp() throws CTFReaderException { |
56 | fixture = getStreamInputReader(); | |
866e5b51 FC |
57 | fixture.setName(1); |
58 | fixture.setCurrentEvent(new EventDefinition(new EventDeclaration(), | |
13be1a8f | 59 | getStreamInputReader())); |
866e5b51 FC |
60 | } |
61 | ||
13be1a8f | 62 | private static StreamInputReader getStreamInputReader() throws CTFReaderException { |
9ac63b5b AM |
63 | assumeTrue(testTrace.exists()); |
64 | CTFTrace trace = testTrace.getTrace(); | |
866e5b51 FC |
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 | */ | |
4a9c1f07 | 73 | if (si.getFilename().endsWith("0_0")) { |
866e5b51 FC |
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. | |
ce2388e0 | 93 | * |
866e5b51 FC |
94 | * @throws CTFReaderException |
95 | */ | |
96 | @Test(expected = CTFReaderException.class) | |
97 | public void testStreamInputReader_invalid() throws CTFReaderException { | |
98 | StreamInput streamInput = new StreamInput( | |
9ac63b5b | 99 | new Stream(new CTFTrace("")), (FileChannel) null, new File("")); |
866e5b51 FC |
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 | ||
866e5b51 FC |
141 | /** |
142 | * Run the void goToLastEvent() method test. | |
db8e8f7d | 143 | * @throws CTFReaderException error |
866e5b51 FC |
144 | */ |
145 | @Test | |
db8e8f7d | 146 | public void testGoToLastEvent1() throws CTFReaderException { |
ec6f5beb MK |
147 | final long endTimestamp = goToEnd(); |
148 | final long endTime = 4287422460315L; | |
149 | assertEquals(endTime , endTimestamp ); | |
150 | } | |
151 | ||
152 | /** | |
153 | * Run the void goToLastEvent() method test. | |
db8e8f7d | 154 | * @throws CTFReaderException error |
ec6f5beb MK |
155 | */ |
156 | @Test | |
db8e8f7d | 157 | public void testGoToLastEvent2() throws CTFReaderException { |
ec6f5beb MK |
158 | long timestamp = -1; |
159 | while(fixture.readNextEvent()) { | |
160 | timestamp = fixture.getCurrentEvent().getTimestamp(); | |
161 | } | |
162 | long endTimestamp = goToEnd(); | |
163 | assertEquals(0 , timestamp- endTimestamp ); | |
164 | } | |
165 | ||
db8e8f7d | 166 | private long goToEnd() throws CTFReaderException { |
866e5b51 | 167 | fixture.goToLastEvent(); |
ec6f5beb | 168 | return fixture.getCurrentEvent().getTimestamp(); |
866e5b51 FC |
169 | } |
170 | ||
171 | /** | |
172 | * Run the boolean readNextEvent() method test. | |
db8e8f7d | 173 | * @throws CTFReaderException error |
866e5b51 FC |
174 | */ |
175 | @Test | |
db8e8f7d | 176 | public void testReadNextEvent() throws CTFReaderException { |
866e5b51 FC |
177 | boolean result = fixture.readNextEvent(); |
178 | assertTrue(result); | |
179 | } | |
180 | ||
181 | /** | |
182 | * Run the void seek(long) method test. Seek by direct timestamp | |
db8e8f7d | 183 | * @throws CTFReaderException error |
866e5b51 FC |
184 | */ |
185 | @Test | |
db8e8f7d | 186 | public void testSeek_timestamp() throws CTFReaderException { |
866e5b51 FC |
187 | long timestamp = 1L; |
188 | fixture.seek(timestamp); | |
189 | } | |
190 | ||
191 | /** | |
192 | * Run the seek test. Seek by passing an EventDefinition to which we've | |
193 | * given the timestamp we want. | |
ce2388e0 FC |
194 | * |
195 | * @throws CTFReaderException | |
866e5b51 FC |
196 | */ |
197 | @Test | |
13be1a8f | 198 | public void testSeek_eventDefinition() throws CTFReaderException { |
866e5b51 | 199 | EventDefinition eventDefinition = new EventDefinition( |
13be1a8f | 200 | new EventDeclaration(), getStreamInputReader()); |
aa572e22 | 201 | eventDefinition.setTimestamp(1L); |
866e5b51 FC |
202 | fixture.setCurrentEvent(eventDefinition); |
203 | } | |
204 | } |