tmf: Make CtfTmfTestTrace null-friendly
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ctf.core.tests / shared / org / eclipse / linuxtools / tmf / ctf / core / tests / shared / CtfTmfTestTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2013 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ctf.core.tests.shared;
14
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace;
19 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
20 import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfEvent;
21 import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace;
22 import org.eclipse.linuxtools.tmf.ctf.core.tests.stubs.CtfTmfTraceStub;
23
24 /**
25 * Available CTF TMF test traces. Kind-of-extends {@link CtfTestTrace}.
26 *
27 * To run tests using these, you first need to run the "get-traces.[xml|sh]"
28 * script located under lttng/org.eclipse.linuxtools.ctf.core.tests/traces/ .
29 *
30 * @author Alexandre Montplaisir
31 */
32 @NonNullByDefault
33 public enum CtfTmfTestTrace {
34 /** Example kernel trace */
35 KERNEL,
36 /** Another kernel trace */
37 TRACE2,
38 /** Kernel trace with event contexts */
39 KERNEL_VM,
40 /** Trace synchronization: source trace */
41 SYNC_SRC,
42 /** Trace synchronization: destination trace */
43 SYNC_DEST,
44 /** UST trace with lots of lost events */
45 HELLO_LOST,
46 /** UST trace with lttng-ust-cyg-profile events (aka -finstrument-functions) */
47 CYG_PROFILE,
48 /** UST trace with lttng-ust-cyg-profile-fast events (no address in func_exit) */
49 CYG_PROFILE_FAST,
50 /** Autogenerated Syntetic trace */
51 SYNTHETIC_TRACE,
52 /** Trace with non-standard field sizes */
53 FUNKY_TRACE;
54
55
56 private final String fPath;
57 private @Nullable CtfTmfTraceStub fTrace = null;
58
59 private CtfTmfTestTrace() {
60 @SuppressWarnings("null")
61 @NonNull String path = CtfTestTrace.valueOf(this.name()).getPath();
62 fPath = path;
63 }
64
65 /**
66 * @return The path of this trace
67 */
68 public String getPath() {
69 return fPath;
70 }
71
72 /**
73 * Return a CtfTmfTraceStub object of this test trace. It will be already
74 * initTrace()'ed.
75 *
76 * Make sure you call {@link #exists()} before calling this!
77 *
78 * After being used by unit tests, traces must be properly disposed of by
79 * calling the {@link CtfTmfTestTrace#dispose()} method.
80 *
81 * @return A CtfTmfTrace reference to this trace
82 */
83 public synchronized CtfTmfTrace getTrace() {
84 CtfTmfTraceStub trace = fTrace;
85 if (trace != null) {
86 trace.close();
87 }
88 trace = new CtfTmfTraceStub();
89 try {
90 trace.initTrace(null, fPath, CtfTmfEvent.class);
91 } catch (TmfTraceException e) {
92 /* Should not happen if tracesExist() passed */
93 throw new RuntimeException(e);
94 }
95 fTrace = trace;
96 return trace;
97 }
98
99 /**
100 * Check if the trace actually exists on disk or not.
101 *
102 * @return If the trace is present
103 */
104 public boolean exists() {
105 return CtfTestTrace.valueOf(this.name()).exists();
106 }
107
108 /**
109 * Dispose of the trace
110 */
111 public void dispose() {
112 if (fTrace != null) {
113 fTrace.dispose();
114 fTrace = null;
115 }
116 }
117 }
This page took 0.037384 seconds and 6 git commands to generate.