Commit | Line | Data |
---|---|---|
4bd7f2db AM |
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 | ||
32bf80d2 | 12 | package org.eclipse.linuxtools.ctf.core.tests.shared; |
866e5b51 FC |
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. | |
ce2388e0 | 21 | * |
866e5b51 | 22 | * @author alexmont |
866e5b51 | 23 | */ |
32bf80d2 | 24 | public abstract class CtfTestTraces { |
ce2388e0 | 25 | |
813ecc8e AM |
26 | /* |
27 | * Path to test traces. Make sure you run the traces/get-traces.sh script | |
28 | * first! | |
29 | */ | |
32bf80d2 AM |
30 | private static final String[] testTracePaths = { |
31 | "../org.eclipse.linuxtools.ctf.core.tests/traces/kernel", | |
9824473b AM |
32 | "../org.eclipse.linuxtools.ctf.core.tests/traces/trace2", |
33 | "../org.eclipse.linuxtools.ctf.core.tests/traces/kernel_vm" | |
32bf80d2 AM |
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"); | |
ce2388e0 | 40 | |
e5acb357 | 41 | private static final File emptyFile = new File(""); |
866e5b51 | 42 | private static CTFTrace emptyTrace = null; |
ce2388e0 | 43 | |
e5acb357 AM |
44 | /** |
45 | * Return an empty file (new File("");) | |
46 | * | |
47 | * @return An empty file | |
48 | */ | |
866e5b51 FC |
49 | public static File getEmptyFile() { |
50 | return emptyFile; | |
51 | } | |
ce2388e0 | 52 | |
e5acb357 AM |
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 | */ | |
024373d7 MK |
60 | public static File getTraceFile(){ |
61 | return testTraceFile1; | |
62 | } | |
e5acb357 AM |
63 | |
64 | /** | |
65 | * Return a trace out of an empty file (new CTFTrace("");) | |
66 | * | |
67 | * @return An empty trace | |
68 | */ | |
866e5b51 FC |
69 | public static CTFTrace getEmptyTrace() { |
70 | if (emptyTrace == null) { | |
71 | try { | |
e5acb357 | 72 | emptyTrace = new CTFTrace(""); |
866e5b51 | 73 | } catch (CTFReaderException e) { |
e5acb357 | 74 | /* Should always work... */ |
866e5b51 | 75 | throw new RuntimeException(e); |
ce2388e0 | 76 | } |
866e5b51 FC |
77 | } |
78 | return emptyTrace; | |
79 | } | |
ce2388e0 | 80 | |
e5acb357 | 81 | /** |
32bf80d2 | 82 | * Get a CTFTrace reference to the given test trace. |
e5acb357 AM |
83 | * |
84 | * Make sure {@link #tracesExist()} before calling this! | |
85 | * | |
32bf80d2 AM |
86 | * @param idx |
87 | * The index of the trace among all the available ones | |
e5acb357 AM |
88 | * @return Reference to test trace #1 |
89 | * @throws CTFReaderException | |
90 | * If the trace cannot be found | |
91 | */ | |
32bf80d2 AM |
92 | public static CTFTrace getTestTrace(int idx) throws CTFReaderException { |
93 | if (testTraces[idx] == null) { | |
94 | testTraces[idx] = new CTFTrace(testTracePaths[idx]); | |
866e5b51 | 95 | } |
32bf80d2 | 96 | return testTraces[idx]; |
866e5b51 FC |
97 | } |
98 | ||
e5acb357 | 99 | /** |
32bf80d2 AM |
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 | |
e5acb357 AM |
115 | * File object and not the path. |
116 | * | |
117 | * Make sure {@link #tracesExist()} before calling this! | |
118 | * | |
32bf80d2 AM |
119 | * @param idx |
120 | * The index of the trace among all the available ones | |
e5acb357 AM |
121 | * @return Reference to test trace #1 |
122 | */ | |
32bf80d2 AM |
123 | public static CTFTrace getTestTraceFromFile(int idx) { |
124 | if (testTracesFromFile[idx] == null) { | |
866e5b51 | 125 | try { |
32bf80d2 | 126 | testTracesFromFile[idx] = new CTFTrace(new File(testTracePaths[idx])); |
866e5b51 | 127 | } catch (CTFReaderException e) { |
e5acb357 | 128 | /* This trace should exist */ |
866e5b51 FC |
129 | throw new RuntimeException(e); |
130 | } | |
131 | } | |
32bf80d2 | 132 | return testTracesFromFile[idx]; |
866e5b51 | 133 | } |
e5acb357 AM |
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() { | |
32bf80d2 AM |
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) { | |
e5acb357 AM |
152 | return true; |
153 | } | |
154 | try { | |
32bf80d2 | 155 | getTestTrace(idx); |
e5acb357 AM |
156 | } catch (CTFReaderException e) { |
157 | return false; | |
158 | } | |
159 | return true; | |
160 | } | |
866e5b51 | 161 | } |