os.tests: Add a kernel stub trace class and use that in unit tests
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.kernel.core.tests / shared / org / eclipse / tracecompass / lttng2 / lttng / kernel / core / tests / shared / vm / VmTraces.java
CommitLineData
03722d5b
GB
1/*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.tracecompass.lttng2.lttng.kernel.core.tests.shared.vm;
14
15import java.io.File;
16import java.io.IOException;
17import java.net.URL;
18
19import org.eclipse.core.runtime.FileLocator;
20import org.eclipse.core.runtime.IPath;
21import org.eclipse.core.runtime.IStatus;
22import org.eclipse.core.runtime.Path;
23import org.eclipse.jdt.annotation.NonNull;
aa353506 24import org.eclipse.jdt.annotation.Nullable;
698fde87 25import org.eclipse.tracecompass.analysis.os.linux.core.tests.stubs.trace.TmfXmlKernelTraceStub;
03722d5b
GB
26import org.eclipse.tracecompass.lttng2.kernel.core.tests.Activator;
27import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
28import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
29import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
03722d5b
GB
30
31/**
32 * List the available virtual machine host and guest traces
33 *
34 * @author Geneviève Bastien
35 */
36public enum VmTraces {
37
38 /** Host from simple QEMU/KVM experiment */
39 HOST_ONE_QEMUKVM("vm/OneQemuKvm/host.xml"),
40 /** Guest from simple QEMU/KVM experiment */
41 GUEST_ONE_QEMUKVM("vm/OneQemuKvm/guest.xml");
42
43 private static final @NonNull String filePath = "testfiles";
44
45 private final IPath fPath;
46
47 VmTraces(String path) {
48 IPath relativePath = new Path(filePath + File.separator + path);
49 Activator plugin = Activator.getDefault();
50 if (plugin == null) {
51 /*
52 * Shouldn't happen but at least throw something to get the test to
53 * fail early
54 */
55 throw new IllegalStateException();
56 }
57 URL location = FileLocator.find(plugin.getBundle(), relativePath, null);
58 try {
59 fPath = new Path(FileLocator.toFileURL(location).getPath());
60 } catch (IOException e) {
61 throw new IllegalStateException();
62 }
63 }
64
65 /**
66 * Return a TmfXmlTraceStub object of this test trace. It will be already
67 * initTrace()'ed.
68 *
69 * Make sure you call {@link #exists()} before calling this!
70 *
71 * @return A TmfXmlTraceStub reference to this trace
72 */
aa353506 73 public @Nullable ITmfTrace getTrace() {
698fde87 74 ITmfTrace trace = new TmfXmlKernelTraceStub();
03722d5b
GB
75 IStatus status = trace.validate(null, fPath.toOSString());
76 if (!status.isOK()) {
77 return null;
78 }
79 try {
80 trace.initTrace(null, fPath.toOSString(), TmfEvent.class);
81 } catch (TmfTraceException e) {
82 return null;
83 }
84 return trace;
85 }
86
87 /**
88 * Check if the trace actually exists on disk or not.
89 *
90 * @return If the trace is present
91 */
92 public boolean exists() {
93 return fPath.toFile().exists();
94 }
95}
This page took 0.048641 seconds and 5 git commands to generate.