ctf: Make sure CTFTraceTest's are independent
[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 *******************************************************************************/
11
12 package org.eclipse.linuxtools.ctf.core.tests.trace;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertNull;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
20 import static org.junit.Assume.assumeTrue;
21
22 import java.io.File;
23 import java.nio.ByteOrder;
24 import java.util.Map;
25 import java.util.UUID;
26
27 import org.eclipse.linuxtools.ctf.core.event.CTFClock;
28 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
29 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
30 import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTraces;
31 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
32 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
33 import org.eclipse.linuxtools.ctf.core.trace.Stream;
34 import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
35 import org.junit.Before;
36 import org.junit.Test;
37
38 /**
39 * The class <code>CTFTraceTest</code> contains tests for the class
40 * <code>{@link CTFTrace}</code>.
41 *
42 * @author ematkho
43 */
44 @SuppressWarnings("nls")
45 public class CTFTraceTest {
46
47 private static final int TRACE_INDEX = 0;
48
49 private CTFTrace fixture;
50
51 /**
52 * Perform pre-test initialization.
53 */
54 @Before
55 public void setUp() {
56 assumeTrue(CtfTestTraces.tracesExist());
57 fixture = CtfTestTraces.getTestTraceFromFile(TRACE_INDEX);
58 fixture.setMinor(1L);
59 fixture.setUUID(UUID.randomUUID());
60 fixture.setPacketHeader(new StructDeclaration(1L));
61 fixture.setMajor(1L);
62 fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
63 }
64
65 /**
66 * Run the CTFTrace(File) constructor test with a known existing trace.
67 */
68 @Test
69 public void testOpen_existing() {
70 CTFTrace result = CtfTestTraces.getTestTraceFromFile(TRACE_INDEX);
71 assertNotNull(result.getUUID());
72 }
73
74 /**
75 * Run the CTFTrace(File) constructor test with an invalid path.
76 *
77 * @throws CTFReaderException
78 * is expected
79 */
80 @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class)
81 public void testOpen_invalid() throws CTFReaderException {
82 File path = new File("");
83 CTFTrace result = new CTFTrace(path);
84 assertNotNull(result);
85 }
86
87 /**
88 * Run the boolean UUIDIsSet() method test.
89 */
90 @Test
91 public void testUUIDIsSet() {
92 boolean result = fixture.UUIDIsSet();
93 assertTrue(result);
94 }
95
96 /**
97 * Run the void addStream(Stream) method test.
98 */
99 @Test
100 public void testAddStream() {
101 // test number of streams
102 int nbStreams = fixture.nbStreams();
103 assertEquals(1, nbStreams);
104
105 // Add a stream
106 try {
107 Stream stream = new Stream(CtfTestTraces.getTestTrace(TRACE_INDEX));
108 stream.setId(1234);
109 fixture.addStream(stream);
110 } catch (CTFReaderException e) {
111 fail();
112 } catch (ParseException e) {
113 fail();
114 }
115
116 // test number of streams
117 nbStreams = fixture.nbStreams();
118 assertEquals(2, nbStreams);
119 }
120
121 /**
122 * Run the boolean byteOrderIsSet() method test.
123 */
124 @Test
125 public void testByteOrderIsSet() {
126 boolean result = fixture.byteOrderIsSet();
127 assertTrue(result);
128 }
129
130 /**
131 * Run the ByteOrder getByteOrder() method test.
132 */
133 @Test
134 public void testGetByteOrder_1() {
135 ByteOrder result = fixture.getByteOrder();
136 assertNotNull(result);
137 }
138
139 /**
140 * Run the long getMajor() method test.
141 */
142 @Test
143 public void testGetMajor() {
144 long result = fixture.getMajor();
145 assertEquals(1L, result);
146 }
147
148 /**
149 * Run the long getMinor() method test.
150 */
151 @Test
152 public void testGetMinor() {
153 long result = fixture.getMinor();
154 assertEquals(1L, result);
155 }
156
157 /**
158 * Run the StructDeclaration getPacketHeader() method test.
159 */
160 @Test
161 public void testGetPacketHeader() {
162 StructDeclaration result = fixture.getPacketHeader();
163 assertNotNull(result);
164 }
165
166 /**
167 * Run the String getPath() method test.
168 */
169 @Test
170 public void testGetPath() {
171 String result = fixture.getPath();
172 assertNotNull(result);
173 }
174
175 /**
176 * Run the Stream getStream(Long) method test.
177 */
178 @Test
179 public void testGetStream() {
180 Long id = new Long(0L);
181 Stream result = fixture.getStream(id);
182 assertNotNull(result);
183 }
184
185 /**
186 * Run the Map<Long, Stream> getStreams() method test.
187 */
188 @Test
189 public void testGetStreams() {
190 Map<Long, Stream> result = fixture.getStreams();
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 majortIsSet() method test.
224 */
225 @Test
226 public void testMajortIsSet() {
227 boolean result = fixture.majortIsSet();
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 CTFTrace fixture2 = CtfTestTraces.getTestTraceFromFile(TRACE_INDEX);
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 }
265
266 /**
267 * Run the void setByteOrder(ByteOrder) method test.
268 */
269 @Test
270 public void testSetByteOrder() {
271 ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
272 fixture.setByteOrder(byteOrder);
273 }
274
275 /**
276 * Run the void setMajor(long) method test.
277 */
278 @Test
279 public void testSetMajor() {
280 long major = 1L;
281 fixture.setMajor(major);
282 }
283
284 /**
285 * Run the void setMinor(long) method test.
286 */
287 @Test
288 public void testSetMinor() {
289 long minor = 1L;
290 fixture.setMinor(minor);
291 }
292
293 /**
294 * Run the void setPacketHeader(StructDeclaration) method test.
295 */
296 @Test
297 public void testSetPacketHeader() {
298 StructDeclaration packetHeader = new StructDeclaration(1L);
299 fixture.setPacketHeader(packetHeader);
300 }
301
302 /**
303 * Run the void setUUID(UUID) method test.
304 */
305 @Test
306 public void testSetUUID() {
307 UUID uuid = UUID.randomUUID();
308 fixture.setUUID(uuid);
309 }
310
311 /**
312 * Run the CTFClock getClock/setClock method test.
313 */
314 @Test
315 public void testGetSetClock_1() {
316 String name = "clockyClock";
317 fixture.addClock(name, new CTFClock());
318 CTFClock result = fixture.getClock(name);
319
320 assertNotNull(result);
321 }
322
323 /**
324 * Run the CTFClock getClock/setClock method test.
325 */
326 @Test
327 public void testGetSetClock_2() {
328 String name = "";
329 CTFClock ctfClock = new CTFClock();
330 ctfClock.addAttribute("name", "Bob");
331 ctfClock.addAttribute("pi", new Double(java.lang.Math.PI));
332 fixture.addClock(name, ctfClock);
333 CTFClock result = fixture.getClock(name);
334
335 assertNotNull(result);
336 assertTrue((Double) ctfClock.getProperty("pi") > 3.0);
337 assertTrue(ctfClock.getName().equals("Bob"));
338 }
339
340 /**
341 * Run the String lookupEnvironment(String) method test.
342 */
343 @Test
344 public void testLookupEnvironment_1() {
345 String key = "";
346 String result = fixture.lookupEnvironment(key);
347 assertNull(result);
348 }
349
350 /**
351 * Run the String lookupEnvironment(String) method test.
352 */
353 @Test
354 public void testLookupEnvironment_2() {
355 String key = "otherTest";
356 String result = fixture.lookupEnvironment(key);
357 assertNull(result);
358 }
359
360 /**
361 * Run the String lookupEnvironment(String) method test.
362 */
363 @Test
364 public void testLookupEnvironment_3() {
365 String key = "test";
366 fixture.addEnvironmentVar(key, key);
367 String result = fixture.lookupEnvironment(key);
368 assertTrue(result.equals(key));
369 }
370
371 /**
372 * Run the String lookupEnvironment(String) method test.
373 */
374 @Test
375 public void testLookupEnvironment_4() {
376 String key = "test";
377 fixture.addEnvironmentVar(key, "bozo");
378 fixture.addEnvironmentVar(key, "the clown");
379 String result = fixture.lookupEnvironment(key);
380 assertNotNull(result);
381 }
382
383 }
This page took 0.0417 seconds and 6 git commands to generate.