tmf: Add unit tests for CTF event contexts
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / shared / org / eclipse / linuxtools / ctf / core / tests / shared / CtfTestTraces.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 * Alexandre Montplaisir - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.linuxtools.ctf.core.tests.shared;
13
14 import java.io.File;
15
16 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
17 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
18
19 /**
20 * Here are the definitions common to all the CTF parser tests.
21 *
22 * @author alexmont
23 */
24 public abstract class CtfTestTraces {
25
26 /*
27 * Path to test traces. Make sure you run the traces/get-traces.sh script
28 * first!
29 */
30 private static final String[] testTracePaths = {
31 "../org.eclipse.linuxtools.ctf.core.tests/traces/kernel",
32 "../org.eclipse.linuxtools.ctf.core.tests/traces/trace2",
33 "../org.eclipse.linuxtools.ctf.core.tests/traces/kernel_vm"
34 };
35
36 private static CTFTrace[] testTraces = new CTFTrace[testTracePaths.length];
37 private static CTFTrace[] testTracesFromFile = new CTFTrace[testTracePaths.length];
38
39 private static final File testTraceFile1 = new File(testTracePaths[0] + "/channel0_0");
40
41 private static final File emptyFile = new File("");
42 private static CTFTrace emptyTrace = null;
43
44 /**
45 * Return an empty file (new File("");)
46 *
47 * @return An empty file
48 */
49 public static File getEmptyFile() {
50 return emptyFile;
51 }
52
53 /**
54 * Return a file in test trace #1 (channel0_0).
55 *
56 * Make sure {@link #tracesExist()} before calling this!
57 *
58 * @return A file in a test trace
59 */
60 public static File getTraceFile(){
61 return testTraceFile1;
62 }
63
64 /**
65 * Return a trace out of an empty file (new CTFTrace("");)
66 *
67 * @return An empty trace
68 */
69 public static CTFTrace getEmptyTrace() {
70 if (emptyTrace == null) {
71 try {
72 emptyTrace = new CTFTrace("");
73 } catch (CTFReaderException e) {
74 /* Should always work... */
75 throw new RuntimeException(e);
76 }
77 }
78 return emptyTrace;
79 }
80
81 /**
82 * Get a CTFTrace reference to the given test trace.
83 *
84 * Make sure {@link #tracesExist()} before calling this!
85 *
86 * @param idx
87 * The index of the trace among all the available ones
88 * @return Reference to test trace #1
89 * @throws CTFReaderException
90 * If the trace cannot be found
91 */
92 public static CTFTrace getTestTrace(int idx) throws CTFReaderException {
93 if (testTraces[idx] == null) {
94 testTraces[idx] = new CTFTrace(testTracePaths[idx]);
95 }
96 return testTraces[idx];
97 }
98
99 /**
100 * Get the (string) path to a given test trace.
101 *
102 * You should call {@link #tracesExist()} before calling this if you are
103 * going to use this trace for real.
104 *
105 * @param idx
106 * The index of the trace among all the available ones
107 * @return The path to the test trace
108 */
109 public static String getTestTracePath(int idx) {
110 return testTracePaths[idx];
111 }
112
113 /**
114 * Same as {@link #getTestTrace}, except the CTFTrace is create from the
115 * File object and not the path.
116 *
117 * Make sure {@link #tracesExist()} before calling this!
118 *
119 * @param idx
120 * The index of the trace among all the available ones
121 * @return Reference to test trace #1
122 */
123 public static CTFTrace getTestTraceFromFile(int idx) {
124 if (testTracesFromFile[idx] == null) {
125 try {
126 testTracesFromFile[idx] = new CTFTrace(new File(testTracePaths[idx]));
127 } catch (CTFReaderException e) {
128 /* This trace should exist */
129 throw new RuntimeException(e);
130 }
131 }
132 return testTracesFromFile[idx];
133 }
134
135 /**
136 * Check if the test traces are present in the tree. If not, you can get
137 * them by running traces/get-traces.sh or traces/get-traces.xml
138 *
139 * @return True if *all* the test files could be found, false otherwise.
140 */
141 public static boolean tracesExist() {
142 for (int i = 0; i < testTracePaths.length; i++) {
143 if (!traceExists(i)) {
144 return false;
145 }
146 }
147 return true;
148 }
149
150 private static boolean traceExists(int idx) {
151 if (testTraces[idx] != null) {
152 return true;
153 }
154 try {
155 getTestTrace(idx);
156 } catch (CTFReaderException e) {
157 return false;
158 }
159 return true;
160 }
161 }
This page took 0.033311 seconds and 5 git commands to generate.