ctf: Move CTF-testsuite tests to a separate file
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / trace / CTFTraceTest.java
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 * Marc-Andre Laperle - Test in traces directory recursively
11 * Simon Delisle - Add test for getCallsite(eventName, ip)
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.ctf.core.tests.trace;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertNotNull;
19 import static org.junit.Assert.assertNull;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 import static org.junit.Assume.assumeTrue;
23
24 import java.io.File;
25 import java.nio.ByteOrder;
26 import java.util.Map;
27 import java.util.UUID;
28
29 import org.eclipse.linuxtools.ctf.core.event.CTFClock;
30 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
31 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
32 import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace;
33 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
34 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
35 import org.eclipse.linuxtools.ctf.core.trace.Stream;
36 import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
37 import org.junit.Before;
38 import org.junit.Test;
39
40 /**
41 * The class <code>CTFTraceTest</code> contains tests for the class
42 * <code>{@link CTFTrace}</code>.
43 *
44 * @author ematkho
45 */
46 public class CTFTraceTest {
47
48 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
49
50 private CTFTrace fixture;
51
52 /**
53 * Perform pre-test initialization.
54 */
55 @Before
56 public void setUp() {
57 assumeTrue(testTrace.exists());
58 try {
59 fixture = testTrace.getTraceFromFile();
60 } catch (CTFReaderException e) {
61 /* If the assumeTrue() call passed, this should not happen. */
62 fail();
63 }
64 fixture.setMinor(1L);
65 fixture.setUUID(UUID.randomUUID());
66 fixture.setPacketHeader(new StructDeclaration(1L));
67 fixture.setMajor(1L);
68 fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
69 }
70
71 /**
72 * Run the CTFTrace(File) constructor test with a known existing trace.
73 */
74 @Test
75 public void testOpen_existing() {
76 try {
77 CTFTrace result = testTrace.getTraceFromFile();
78 assertNotNull(result.getUUID());
79 } catch (CTFReaderException e) {
80 fail();
81 }
82 }
83
84 /**
85 * Run the CTFTrace(File) constructor test with an invalid path.
86 *
87 * @throws CTFReaderException
88 * is expected
89 */
90 @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class)
91 public void testOpen_invalid() throws CTFReaderException {
92 File path = new File("");
93 CTFTrace result = new CTFTrace(path);
94 assertNotNull(result);
95 }
96
97 /**
98 * Run the boolean UUIDIsSet() method test.
99 */
100 @Test
101 public void testUUIDIsSet() {
102 boolean result = fixture.uuidIsSet();
103 assertTrue(result);
104 }
105
106 /**
107 * Run the void addStream(Stream) method test.
108 */
109 @Test
110 public void testAddStream() {
111 // test number of streams
112 int nbStreams = fixture.nbStreams();
113 assertEquals(1, nbStreams);
114
115 // Add a stream
116 try {
117 Stream stream = new Stream(testTrace.getTrace());
118 stream.setId(1234);
119 fixture.addStream(stream);
120 } catch (CTFReaderException e) {
121 fail();
122 } catch (ParseException e) {
123 fail();
124 }
125
126 // test number of streams
127 nbStreams = fixture.nbStreams();
128 assertEquals(2, nbStreams);
129 }
130
131 /**
132 * Run the boolean byteOrderIsSet() method test.
133 */
134 @Test
135 public void testByteOrderIsSet() {
136 boolean result = fixture.byteOrderIsSet();
137 assertTrue(result);
138 }
139
140 /**
141 * Run the ByteOrder getByteOrder() method test.
142 */
143 @Test
144 public void testGetByteOrder_1() {
145 ByteOrder result = fixture.getByteOrder();
146 assertNotNull(result);
147 }
148
149 /**
150 * Run the long getMajor() method test.
151 */
152 @Test
153 public void testGetMajor() {
154 long result = fixture.getMajor();
155 assertEquals(1L, result);
156 }
157
158 /**
159 * Run the long getMinor() method test.
160 */
161 @Test
162 public void testGetMinor() {
163 long result = fixture.getMinor();
164 assertEquals(1L, result);
165 }
166
167 /**
168 * Run the StructDeclaration getPacketHeader() method test.
169 */
170 @Test
171 public void testGetPacketHeader() {
172 StructDeclaration result = fixture.getPacketHeader();
173 assertNotNull(result);
174 }
175
176 /**
177 * Run the String getPath() method test.
178 */
179 @Test
180 public void testGetPath() {
181 String result = fixture.getPath();
182 assertNotNull(result);
183 }
184
185 /**
186 * Run the Stream getStream(Long) method test.
187 */
188 @Test
189 public void testGetStream() {
190 Long id = new Long(0L);
191 Stream result = fixture.getStream(id);
192 assertNotNull(result);
193 }
194
195 /**
196 * Run the Map<Long, Stream> getStreams() method test.
197 */
198 @Test
199 public void testGetStreams() {
200 Map<Long, Stream> result = fixture.getStreams();
201 assertNotNull(result);
202 }
203
204 /**
205 * Run the File getTraceDirectory() method test.
206 */
207 @Test
208 public void testGetTraceDirectory() {
209 File result = fixture.getTraceDirectory();
210 assertNotNull(result);
211 }
212
213 /**
214 * Run the UUID getUUID() method test.
215 */
216 @Test
217 public void testGetUUID() {
218 UUID result = fixture.getUUID();
219 assertNotNull(result);
220 }
221
222 /**
223 * Run the Definition lookupDefinition(String) method test.
224 */
225 @Test
226 public void testLookupDefinition() {
227 String lookupPath = "trace.packet.header";
228 Definition result = fixture.lookupDefinition(lookupPath);
229 assertNotNull(result);
230 }
231
232 /**
233 * Run the boolean majortIsSet() method test.
234 */
235 @Test
236 public void testMajortIsSet() {
237 boolean result = fixture.majortIsSet();
238 assertTrue(result);
239 }
240
241 /**
242 * Run the boolean minorIsSet() method test.
243 */
244 @Test
245 public void testMinorIsSet() {
246 boolean result = fixture.minorIsSet();
247 assertTrue(result);
248 }
249
250 /**
251 * Run the boolean packetHeaderIsSet() method test with a valid header set.
252 */
253 @Test
254 public void testPacketHeaderIsSet_valid() {
255 boolean result = fixture.packetHeaderIsSet();
256 assertTrue(result);
257 }
258
259 /**
260 * Run the boolean packetHeaderIsSet() method test, without having a valid
261 * header set.
262 */
263 @Test
264 public void testPacketHeaderIsSet_invalid() {
265 try {
266 CTFTrace fixture2 = testTrace.getTraceFromFile();
267 fixture2.setMinor(1L);
268 fixture2.setUUID(UUID.randomUUID());
269 fixture2.setPacketHeader((StructDeclaration) null); /* it's null here! */
270 fixture2.setMajor(1L);
271 fixture2.setByteOrder(ByteOrder.BIG_ENDIAN);
272
273 boolean result = fixture2.packetHeaderIsSet();
274 assertFalse(result);
275 } catch (CTFReaderException e) {
276 fail();
277 }
278 }
279
280 /**
281 * Run the void setByteOrder(ByteOrder) method test.
282 */
283 @Test
284 public void testSetByteOrder() {
285 ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
286 fixture.setByteOrder(byteOrder);
287 }
288
289 /**
290 * Run the void setMajor(long) method test.
291 */
292 @Test
293 public void testSetMajor() {
294 long major = 1L;
295 fixture.setMajor(major);
296 }
297
298 /**
299 * Run the void setMinor(long) method test.
300 */
301 @Test
302 public void testSetMinor() {
303 long minor = 1L;
304 fixture.setMinor(minor);
305 }
306
307 /**
308 * Run the void setPacketHeader(StructDeclaration) method test.
309 */
310 @Test
311 public void testSetPacketHeader() {
312 StructDeclaration packetHeader = new StructDeclaration(1L);
313 fixture.setPacketHeader(packetHeader);
314 }
315
316 /**
317 * Run the void setUUID(UUID) method test.
318 */
319 @Test
320 public void testSetUUID() {
321 UUID uuid = UUID.randomUUID();
322 fixture.setUUID(uuid);
323 }
324
325 /**
326 * Run the CTFClock getClock/setClock method test.
327 */
328 @Test
329 public void testGetSetClock_1() {
330 String name = "clockyClock";
331 fixture.addClock(name, new CTFClock());
332 CTFClock result = fixture.getClock(name);
333
334 assertNotNull(result);
335 }
336
337 /**
338 * Run the CTFClock getClock/setClock method test.
339 */
340 @Test
341 public void testGetSetClock_2() {
342 String name = "";
343 CTFClock ctfClock = new CTFClock();
344 ctfClock.addAttribute("name", "Bob");
345 ctfClock.addAttribute("pi", new Double(java.lang.Math.PI));
346 fixture.addClock(name, ctfClock);
347 CTFClock result = fixture.getClock(name);
348
349 assertNotNull(result);
350 assertTrue((Double) ctfClock.getProperty("pi") > 3.0);
351 assertTrue(ctfClock.getName().equals("Bob"));
352 }
353
354 /**
355 * Run the String lookupEnvironment(String) method test.
356 */
357 @Test
358 public void testLookupEnvironment_1() {
359 String key = "";
360 String result = fixture.lookupEnvironment(key);
361 assertNull(result);
362 }
363
364 /**
365 * Run the String lookupEnvironment(String) method test.
366 */
367 @Test
368 public void testLookupEnvironment_2() {
369 String key = "otherTest";
370 String result = fixture.lookupEnvironment(key);
371 assertNull(result);
372 }
373
374 /**
375 * Run the String lookupEnvironment(String) method test.
376 */
377 @Test
378 public void testLookupEnvironment_3() {
379 String key = "test";
380 fixture.addEnvironmentVar(key, key);
381 String result = fixture.lookupEnvironment(key);
382 assertTrue(result.equals(key));
383 }
384
385 /**
386 * Run the String lookupEnvironment(String) method test.
387 */
388 @Test
389 public void testLookupEnvironment_4() {
390 String key = "test";
391 fixture.addEnvironmentVar(key, "bozo");
392 fixture.addEnvironmentVar(key, "the clown");
393 String result = fixture.lookupEnvironment(key);
394 assertNotNull(result);
395 }
396
397 /**
398 * Test for getCallsite(eventName, ip)
399 * @throws CTFReaderException not expected
400 */
401 @Test
402 public void callsitePosition() throws CTFReaderException{
403 long ip1 = 2;
404 long ip2 = 5;
405 long ip3 = 7;
406 CTFTrace callsiteTest = testTrace.getTraceFromFile();
407 callsiteTest.addCallsite("testEvent", null, ip1, null, 23);
408 callsiteTest.addCallsite("testEvent", null, ip2, null, 50);
409 callsiteTest.addCallsite("testEvent", null, ip3, null, 15);
410
411 assertEquals(2, (callsiteTest.getCallsite("testEvent", 1)).getIp());
412 assertEquals(2, (callsiteTest.getCallsite("testEvent", 2)).getIp());
413 assertEquals(5, (callsiteTest.getCallsite("testEvent", 3)).getIp());
414 assertEquals(5, (callsiteTest.getCallsite("testEvent", 5)).getIp());
415 assertEquals(7, (callsiteTest.getCallsite("testEvent", 6)).getIp());
416 assertEquals(7, (callsiteTest.getCallsite("testEvent", 7)).getIp());
417 assertEquals(7, (callsiteTest.getCallsite("testEvent", 8)).getIp());
418 }
419
420 }
This page took 0.044179 seconds and 6 git commands to generate.