ctf: Depend on the tracecompass-test-traces project
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / shared / org / eclipse / tracecompass / tmf / ctf / core / tests / shared / CtfTmfTestTraceUtils.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 Ericsson, EfficiOS Inc. and others
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
10 package org.eclipse.tracecompass.tmf.ctf.core.tests.shared;
11
12 import java.io.IOException;
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import org.eclipse.core.runtime.FileLocator;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.tracecompass.ctf.core.tests.shared.LttngKernelTraceGenerator;
19 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
20 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
21 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
22 import org.eclipse.tracecompass.tmf.ctf.core.tests.stubs.CtfTmfTraceStub;
23 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
24
25 /**
26 * Wrapper for the CTF test traces, instantiating {@link CtfTmfTrace} objects
27 * from them.
28 *
29 * @author Alexandre Montplaisir
30 */
31 @NonNullByDefault
32 public final class CtfTmfTestTraceUtils {
33
34 private static final Map<CtfTestTrace, CtfTmfTrace> CTF_TMF_TRACES = new HashMap<>();
35
36 private CtfTmfTestTraceUtils() {}
37
38 /**
39 * Return a CtfTmfTraceStub object of this test trace. It will be already
40 * initTrace()'ed.
41 *
42 * After being used by unit tests, traces should be properly disposed by
43 * calling the {@link #dispose(CtfTestTrace)} method.
44 *
45 * @param ctfTrace
46 * The test trace to initialize
47 * @return A CtfTmfTrace reference to this trace
48 */
49 public static synchronized CtfTmfTrace getTrace(CtfTestTrace ctfTrace) {
50 String tracePath;
51 try {
52 tracePath = FileLocator.toFileURL(ctfTrace.getTraceURL()).getPath();
53 } catch (IOException e) {
54 throw new IllegalStateException();
55 }
56
57 dispose(ctfTrace);
58 CtfTmfTrace trace = new CtfTmfTraceStub();
59 try {
60 trace.initTrace(null, tracePath, CtfTmfEvent.class);
61 } catch (TmfTraceException e) {
62 /* Should not happen if tracesExist() passed */
63 throw new RuntimeException(e);
64 }
65 CTF_TMF_TRACES.put(ctfTrace, trace);
66 return trace;
67 }
68
69 /**
70 * Dispose of the trace
71 *
72 * @param ctfTrace
73 * Trace to dispose
74 */
75 public static synchronized void dispose(CtfTestTrace ctfTrace) {
76 CtfTmfTrace trace = CTF_TMF_TRACES.remove(ctfTrace);
77 if (trace != null) {
78 trace.dispose();
79 }
80 }
81
82 /**
83 * Get an initialized version of the synthetic trace.
84 *
85 * @return A reference to the synthetic trace
86 */
87 public static CtfTmfTrace getSyntheticTrace() {
88 CtfTmfTrace trace = new CtfTmfTrace();
89 try {
90 trace.initTrace(null, LttngKernelTraceGenerator.getPath(), CtfTmfEvent.class);
91 } catch (TmfTraceException e) {
92 throw new IllegalStateException();
93 }
94 return trace;
95 }
96 }
This page took 0.077537 seconds and 5 git commands to generate.