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