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