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