tmf/lttng: Update 2014 copyrights
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / trace / CTFTraceTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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.UUID;
27
28 import org.eclipse.linuxtools.ctf.core.event.CTFClock;
29 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
30 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
31 import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace;
32 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
33 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
34 import org.eclipse.linuxtools.ctf.core.trace.Stream;
35 import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
36 import org.junit.Before;
37 import org.junit.Test;
38
39 /**
40 * The class <code>CTFTraceTest</code> contains tests for the class
41 * <code>{@link CTFTrace}</code>.
42 *
43 * @author ematkho
44 */
45 public class CTFTraceTest {
46
47 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
48
49 private CTFTrace fixture;
50
51 /**
52 * Perform pre-test initialization.
53 */
54 @Before
55 public void setUp() {
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 }
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
70 /**
71 * Run the CTFTrace(File) constructor test with a known existing trace.
72 */
73 @Test
74 public void testOpen_existing() {
75 try {
76 CTFTrace result = testTrace.getTraceFromFile();
77 assertNotNull(result.getUUID());
78 } catch (CTFReaderException e) {
79 fail();
80 }
81 }
82
83 /**
84 * Run the CTFTrace(File) constructor test with an invalid path.
85 *
86 * @throws CTFReaderException
87 * is expected
88 */
89 @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class)
90 public void testOpen_invalid() throws CTFReaderException {
91 File path = new File("");
92 CTFTrace result = new CTFTrace(path);
93 assertNotNull(result);
94 }
95
96 /**
97 * Run the boolean UUIDIsSet() method test.
98 */
99 @Test
100 public void testUUIDIsSet() {
101 boolean result = fixture.uuidIsSet();
102 assertTrue(result);
103 }
104
105 /**
106 * Run the void addStream(Stream) method test.
107 */
108 @Test
109 public void testAddStream() {
110 // test number of streams
111 int nbStreams = fixture.nbStreams();
112 assertEquals(1, nbStreams);
113
114 // Add a stream
115 try {
116 Stream stream = new Stream(testTrace.getTrace());
117 stream.setId(1234);
118 fixture.addStream(stream);
119 } catch (CTFReaderException e) {
120 fail();
121 } catch (ParseException e) {
122 fail();
123 }
124
125 // test number of streams
126 nbStreams = fixture.nbStreams();
127 assertEquals(2, nbStreams);
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);
190 Stream result = fixture.getStream(id);
191 assertNotNull(result);
192 }
193
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() {
217 String lookupPath = "trace.packet.header";
218 Definition result = fixture.lookupDefinition(lookupPath);
219 assertNotNull(result);
220 }
221
222 /**
223 * Run the boolean majorIsSet() method test.
224 */
225 @Test
226 public void testMajorIsSet() {
227 boolean result = fixture.majorIsSet();
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
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() {
255 try {
256 CTFTrace fixture2 = testTrace.getTraceFromFile();
257 fixture2.setMinor(1L);
258 fixture2.setUUID(UUID.randomUUID());
259 fixture2.setPacketHeader((StructDeclaration) null); /* it's null here! */
260 fixture2.setMajor(1L);
261 fixture2.setByteOrder(ByteOrder.BIG_ENDIAN);
262
263 boolean result = fixture2.packetHeaderIsSet();
264 assertFalse(result);
265 } catch (CTFReaderException e) {
266 fail();
267 }
268 }
269
270 /**
271 * Run the void setByteOrder(ByteOrder) method test.
272 */
273 @Test
274 public void testSetByteOrder() {
275 ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
276 fixture.setByteOrder(byteOrder);
277 }
278
279 /**
280 * Run the void setMajor(long) method test.
281 */
282 @Test
283 public void testSetMajor() {
284 long major = 1L;
285 fixture.setMajor(major);
286 }
287
288 /**
289 * Run the void setMinor(long) method test.
290 */
291 @Test
292 public void testSetMinor() {
293 long minor = 1L;
294 fixture.setMinor(minor);
295 }
296
297 /**
298 * Run the void setPacketHeader(StructDeclaration) method test.
299 */
300 @Test
301 public void testSetPacketHeader() {
302 StructDeclaration packetHeader = new StructDeclaration(1L);
303 fixture.setPacketHeader(packetHeader);
304 }
305
306 /**
307 * Run the void setUUID(UUID) method test.
308 */
309 @Test
310 public void testSetUUID() {
311 UUID uuid = UUID.randomUUID();
312 fixture.setUUID(uuid);
313 }
314
315 /**
316 * Run the CTFClock getClock/setClock method test.
317 */
318 @Test
319 public void testGetSetClock_1() {
320 String name = "clockyClock";
321 fixture.addClock(name, new CTFClock());
322 CTFClock result = fixture.getClock(name);
323
324 assertNotNull(result);
325 }
326
327 /**
328 * Run the CTFClock getClock/setClock method test.
329 */
330 @Test
331 public void testGetSetClock_2() {
332 String name = "";
333 CTFClock ctfClock = new CTFClock();
334 ctfClock.addAttribute("name", "Bob");
335 ctfClock.addAttribute("pi", new Double(java.lang.Math.PI));
336 fixture.addClock(name, ctfClock);
337 CTFClock result = fixture.getClock(name);
338
339 assertNotNull(result);
340 assertTrue((Double) ctfClock.getProperty("pi") > 3.0);
341 assertTrue(ctfClock.getName().equals("Bob"));
342 }
343
344 /**
345 * Run the String lookupEnvironment(String) method test.
346 */
347 @Test
348 public void testLookupEnvironment_1() {
349 String key = "";
350 String result = fixture.getEnvironment().get(key);
351 assertNull(result);
352 }
353
354 /**
355 * Run the String lookupEnvironment(String) method test.
356 */
357 @Test
358 public void testLookupEnvironment_2() {
359 String key = "otherTest";
360 String result = fixture.getEnvironment().get(key);
361 assertNull(result);
362 }
363
364 /**
365 * Run the String lookupEnvironment(String) method test.
366 */
367 @Test
368 public void testLookupEnvironment_3() {
369 String key = "test";
370 fixture.addEnvironmentVar(key, key);
371 String result = fixture.getEnvironment().get(key);
372 assertTrue(result.equals(key));
373 }
374
375 /**
376 * Run the String lookupEnvironment(String) method test.
377 */
378 @Test
379 public void testLookupEnvironment_4() {
380 String key = "test";
381 fixture.addEnvironmentVar(key, "bozo");
382 fixture.addEnvironmentVar(key, "the clown");
383 String result = fixture.getEnvironment().get(key);
384 assertNotNull(result);
385 }
386
387 /**
388 * Test for getCallsite(eventName, ip)
389 * @throws CTFReaderException not expected
390 */
391 @Test
392 public void callsitePosition() throws CTFReaderException{
393 long ip1 = 2;
394 long ip2 = 5;
395 long ip3 = 7;
396 CTFTrace callsiteTest = testTrace.getTraceFromFile();
397 callsiteTest.addCallsite("testEvent", null, ip1, null, 23);
398 callsiteTest.addCallsite("testEvent", null, ip2, null, 50);
399 callsiteTest.addCallsite("testEvent", null, ip3, null, 15);
400
401 assertEquals(2, (callsiteTest.getCallsite("testEvent", 1)).getIp());
402 assertEquals(2, (callsiteTest.getCallsite("testEvent", 2)).getIp());
403 assertEquals(5, (callsiteTest.getCallsite("testEvent", 3)).getIp());
404 assertEquals(5, (callsiteTest.getCallsite("testEvent", 5)).getIp());
405 assertEquals(7, (callsiteTest.getCallsite("testEvent", 6)).getIp());
406 assertEquals(7, (callsiteTest.getCallsite("testEvent", 7)).getIp());
407 assertEquals(7, (callsiteTest.getCallsite("testEvent", 8)).getIp());
408 }
409
410 }
This page took 0.04793 seconds and 5 git commands to generate.