Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | package org.eclipse.linuxtools.ctf.core.tests.trace; |
2 | ||
ce2388e0 | 3 | import static org.junit.Assert.assertEquals; |
866e5b51 FC |
4 | import static org.junit.Assert.assertFalse; |
5 | import static org.junit.Assert.assertNotNull; | |
6 | import static org.junit.Assert.assertTrue; | |
e5acb357 | 7 | import static org.junit.Assume.assumeTrue; |
866e5b51 FC |
8 | |
9 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; | |
32bf80d2 | 10 | import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTraces; |
866e5b51 FC |
11 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
12 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; | |
13 | import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader; | |
14 | import org.junit.After; | |
15 | import org.junit.Before; | |
16 | import org.junit.Test; | |
17 | ||
18 | /** | |
19 | * The class <code>CTFTraceReaderTest</code> contains tests for the class | |
20 | * <code>{@link CTFTraceReader}</code>. | |
21 | * | |
22 | * @author ematkho | |
23 | * @version $Revision: 1.0 $ | |
24 | */ | |
be6df2d8 | 25 | @SuppressWarnings("javadoc") |
866e5b51 FC |
26 | public class CTFTraceReaderTest { |
27 | ||
32bf80d2 AM |
28 | private static final int TRACE_INDEX = 0; |
29 | ||
30 | private CTFTraceReader fixture; | |
866e5b51 FC |
31 | |
32 | /** | |
33 | * Launch the test. | |
34 | * | |
35 | * @param args | |
36 | * the command line arguments | |
37 | */ | |
38 | public static void main(String[] args) { | |
39 | new org.junit.runner.JUnitCore().run(CTFTraceReaderTest.class); | |
40 | } | |
41 | ||
42 | /** | |
43 | * Perform pre-test initialization. | |
bfe038ff | 44 | * |
ce2388e0 | 45 | * @throws CTFReaderException |
866e5b51 FC |
46 | */ |
47 | @Before | |
13be1a8f | 48 | public void setUp() throws CTFReaderException { |
32bf80d2 AM |
49 | assumeTrue(CtfTestTraces.tracesExist()); |
50 | fixture = new CTFTraceReader(CtfTestTraces.getTestTrace(TRACE_INDEX)); | |
866e5b51 FC |
51 | } |
52 | ||
53 | /** | |
54 | * Perform post-test clean-up. | |
55 | */ | |
56 | @After | |
57 | public void tearDown() { | |
58 | // Add additional tear down code here | |
59 | } | |
60 | ||
61 | /** | |
62 | * Run the CTFTraceReader(CTFTrace) constructor test. Open a known good | |
63 | * trace. | |
bfe038ff | 64 | * |
ce2388e0 | 65 | * @throws CTFReaderException |
866e5b51 FC |
66 | */ |
67 | @Test | |
13be1a8f | 68 | public void testOpen_existing() throws CTFReaderException { |
32bf80d2 | 69 | CTFTrace trace = CtfTestTraces.getTestTrace(TRACE_INDEX); |
866e5b51 FC |
70 | |
71 | CTFTraceReader result = new CTFTraceReader(trace); | |
72 | assertNotNull(result); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Run the CTFTraceReader(CTFTrace) constructor test. Open a non-existing | |
77 | * trace, expect the exception. | |
78 | * | |
79 | * @throws CTFReaderException | |
80 | */ | |
81 | @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class) | |
82 | public void testOpen_nonexisting() throws CTFReaderException { | |
83 | CTFTrace trace = new CTFTrace("badfile.bad"); //$NON-NLS-1$ | |
84 | ||
85 | CTFTraceReader result = new CTFTraceReader(trace); | |
86 | assertNotNull(result); | |
87 | } | |
88 | ||
89 | /** | |
90 | * Run the CTFTraceReader(CTFTrace) constructor test. Try to pen an invalid | |
91 | * path, expect exception. | |
92 | * | |
93 | * @throws CTFReaderException | |
94 | */ | |
95 | @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class) | |
96 | public void testOpen_invalid() throws CTFReaderException { | |
97 | CTFTrace trace = new CTFTrace(""); //$NON-NLS-1$ | |
98 | ||
99 | CTFTraceReader result = new CTFTraceReader(trace); | |
100 | assertNotNull(result); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Run the boolean advance() method test. Test advancing normally. | |
105 | */ | |
106 | @Test | |
107 | public void testAdvance_normal() { | |
108 | boolean result = fixture.advance(); | |
109 | assertTrue(result); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Run the boolean advance() method test. Test advancing when we're at the | |
114 | * end, so we expect that there is no more events. | |
866e5b51 FC |
115 | */ |
116 | @Test | |
bfe038ff MK |
117 | public void testAdvance_end() { |
118 | int i = 0; | |
119 | boolean result = fixture.advance(); | |
120 | while (result) { | |
121 | result = fixture.advance(); | |
122 | i++; | |
123 | } | |
124 | fixture.seek(0); | |
125 | fixture.advance(); | |
866e5b51 | 126 | fixture.goToLastEvent(); |
bfe038ff MK |
127 | i = 1; |
128 | result = fixture.advance(); | |
129 | while (result) { | |
130 | result = fixture.advance(); | |
131 | i++; | |
866e5b51 | 132 | } |
866e5b51 | 133 | assertFalse(result); |
bfe038ff | 134 | assertEquals(i, 1); |
866e5b51 FC |
135 | } |
136 | ||
137 | /** | |
138 | * Run the CTFTraceReader copy constructor test. | |
139 | */ | |
140 | @Test | |
141 | public void testCopyFrom() { | |
142 | CTFTraceReader result = fixture.copyFrom(); | |
143 | assertNotNull(result); | |
144 | } | |
145 | ||
146 | /** | |
147 | * Test the hashCode method. | |
148 | */ | |
149 | @Test | |
150 | public void testHash() { | |
151 | int result = fixture.hashCode(); | |
152 | assertTrue(0 != result); | |
153 | } | |
154 | ||
155 | /** | |
156 | * Test the equals method. Uses the class-wide 'fixture' and another | |
157 | * method-local 'fixture2', which both point to the same trace. | |
158 | * | |
159 | * Both trace reader are different objects, so they shouldn't "equals" each | |
160 | * other. | |
bfe038ff | 161 | * |
ce2388e0 | 162 | * @throws CTFReaderException |
866e5b51 FC |
163 | */ |
164 | @Test | |
13be1a8f | 165 | public void testEquals() throws CTFReaderException { |
32bf80d2 | 166 | CTFTraceReader fixture2 = new CTFTraceReader(CtfTestTraces.getTestTrace(TRACE_INDEX)); |
bfe038ff | 167 | assertEquals(fixture, fixture2); |
866e5b51 FC |
168 | } |
169 | ||
170 | /** | |
171 | * Run the getCurrentEventDef() method test. Get the first event's | |
172 | * definition. | |
173 | */ | |
174 | @Test | |
175 | public void testGetCurrentEventDef_first() { | |
176 | EventDefinition result = fixture.getCurrentEventDef(); | |
177 | assertNotNull(result); | |
178 | } | |
179 | ||
180 | /** | |
181 | * Run the getCurrentEventDef() method test. Get the last event's | |
182 | * definition. | |
866e5b51 FC |
183 | */ |
184 | @Test | |
bfe038ff | 185 | public void testGetCurrentEventDef_last() { |
866e5b51 FC |
186 | fixture.goToLastEvent(); |
187 | EventDefinition result = fixture.getCurrentEventDef(); | |
188 | assertNotNull(result); | |
189 | } | |
190 | ||
191 | /** | |
192 | * Run the long getEndTime() method test. | |
193 | */ | |
194 | @Test | |
195 | public void testGetEndTime() { | |
196 | long result = fixture.getEndTime(); | |
197 | assertTrue(0L < result); | |
198 | } | |
199 | ||
200 | /** | |
201 | * Run the long getStartTime() method test. | |
202 | */ | |
203 | @Test | |
204 | public void testGetStartTime() { | |
205 | long result = fixture.getStartTime(); | |
206 | assertTrue(0L < result); | |
207 | } | |
208 | ||
209 | /** | |
210 | * Run the void goToLastEvent() method test. | |
866e5b51 FC |
211 | */ |
212 | @Test | |
be6df2d8 | 213 | public void testGoToLastEvent() { |
866e5b51 | 214 | fixture.goToLastEvent(); |
ce2388e0 | 215 | long ts1 = getTimestamp(); |
866e5b51 | 216 | long ts2 = fixture.getEndTime(); |
bfe038ff | 217 | assertEquals(ts1, ts2); |
866e5b51 FC |
218 | } |
219 | ||
220 | /** | |
221 | * Run the boolean hasMoreEvents() method test. | |
222 | * | |
223 | * @throws CTFReaderException | |
224 | */ | |
225 | @Test | |
226 | public void testHasMoreEvents() { | |
227 | boolean result = fixture.hasMoreEvents(); | |
228 | assertTrue(result); | |
229 | } | |
230 | ||
231 | /** | |
232 | * Run the void printStats() method test with no 'width' parameter. | |
233 | */ | |
234 | @Test | |
235 | public void testPrintStats_noparam() { | |
236 | fixture.advance(); | |
237 | fixture.printStats(); | |
238 | } | |
239 | ||
240 | /** | |
241 | * Run the void printStats(int) method test with width = 0. | |
242 | */ | |
243 | @Test | |
244 | public void testPrintStats_width0() { | |
245 | fixture.advance(); | |
246 | fixture.printStats(0); | |
247 | } | |
248 | ||
249 | /** | |
250 | * Run the void printStats(int) method test with width = 1. | |
251 | */ | |
252 | @Test | |
253 | public void testPrintStats_width1() { | |
254 | fixture.advance(); | |
255 | fixture.printStats(1); | |
256 | } | |
257 | ||
258 | /** | |
259 | * Run the void printStats(int) method test with width = 2. | |
260 | */ | |
261 | @Test | |
262 | public void testPrintStats_width2() { | |
263 | fixture.advance(); | |
264 | fixture.printStats(2); | |
265 | } | |
266 | ||
267 | /** | |
268 | * Run the void printStats(int) method test with width = 10. | |
269 | */ | |
270 | @Test | |
271 | public void testPrintStats_width10() { | |
272 | fixture.advance(); | |
273 | fixture.printStats(10); | |
274 | } | |
275 | ||
276 | /** | |
277 | * Run the void printStats(int) method test with width = 100. | |
278 | */ | |
279 | @Test | |
280 | public void testPrintStats_100() { | |
281 | for (int i = 0; i < 1000; i++) { | |
282 | fixture.advance(); | |
283 | } | |
284 | fixture.printStats(100); | |
285 | } | |
286 | ||
287 | /** | |
288 | * Run the boolean seek(long) method test. | |
289 | */ | |
290 | @Test | |
291 | public void testSeek() { | |
292 | long timestamp = 1L; | |
293 | boolean result = fixture.seek(timestamp); | |
294 | assertTrue(result); | |
295 | } | |
ce2388e0 FC |
296 | |
297 | ||
ce2388e0 FC |
298 | |
299 | /** | |
300 | * @return | |
301 | */ | |
302 | private long getTimestamp() { | |
bfe038ff | 303 | if (fixture.getCurrentEventDef() != null) { |
1d7277f3 | 304 | return fixture.getTrace().timestampCyclesToNanos(fixture.getCurrentEventDef().getTimestamp()); |
bfe038ff MK |
305 | } |
306 | return -1; | |
ce2388e0 | 307 | } |
866e5b51 | 308 | } |