Commit | Line | Data |
---|---|---|
a1492e22 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 Ericsson |
a1492e22 AM |
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 | ||
f357bcd4 | 13 | package org.eclipse.tracecompass.ctf.core.tests.ctftestsuite; |
a1492e22 AM |
14 | |
15 | import static org.junit.Assert.assertNotNull; | |
16 | import static org.junit.Assert.fail; | |
17 | ||
519b3cab AM |
18 | import java.io.IOException; |
19 | import java.nio.file.DirectoryStream; | |
20 | import java.nio.file.Files; | |
21 | import java.nio.file.Path; | |
22 | import java.nio.file.Paths; | |
a1492e22 AM |
23 | import java.util.LinkedList; |
24 | import java.util.List; | |
d291a715 | 25 | import java.util.concurrent.TimeUnit; |
a1492e22 | 26 | |
680f9173 | 27 | import org.eclipse.tracecompass.ctf.core.CTFException; |
f357bcd4 AM |
28 | import org.eclipse.tracecompass.ctf.core.trace.CTFTrace; |
29 | import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader; | |
a1492e22 AM |
30 | import org.junit.Rule; |
31 | import org.junit.Test; | |
32 | import org.junit.rules.TestRule; | |
33 | import org.junit.rules.Timeout; | |
34 | import org.junit.runner.RunWith; | |
35 | import org.junit.runners.Parameterized; | |
36 | import org.junit.runners.Parameterized.Parameters; | |
37 | ||
38 | /** | |
28e4760e MK |
39 | * Parameterized test class running the CTF Test Suite |
40 | * | |
a1492e22 AM |
41 | * (from https://github.com/efficios/ctf-testsuite). |
42 | * | |
43 | * @author Alexandre Montplaisir | |
44 | */ | |
45 | @RunWith(Parameterized.class) | |
46 | public class CtfTestSuiteTests { | |
47 | ||
48 | /** Time-out tests after 10 seconds. */ | |
49 | @Rule | |
d291a715 | 50 | public TestRule globalTimeout = new Timeout(10, TimeUnit.SECONDS); |
a1492e22 | 51 | |
519b3cab | 52 | private static final Path BASE_PATH = Paths.get("traces", "ctf-testsuite", "tests", "1.8"); |
076fe600 AM |
53 | |
54 | /** | |
55 | * Test we know are currently failing. Ignore them so we can at least run | |
56 | * the others. | |
57 | * | |
58 | * TODO Actually fix them! | |
59 | */ | |
519b3cab AM |
60 | private static final Path[] IGNORED_TESTS = { |
61 | BASE_PATH.resolve(Paths.get("regression", "metadata", "pass", "sequence-typedef-length")), | |
2dcb4dbc | 62 | BASE_PATH.resolve(Paths.get("regression", "metadata", "pass", "array-of-struct")), |
802b615d | 63 | BASE_PATH.resolve(Paths.get("regression", "stream", "pass", "integer-large-size")), |
076fe600 | 64 | }; |
a1492e22 AM |
65 | |
66 | private final String fTracePath; | |
67 | private final boolean fExpectSuccess; | |
68 | ||
69 | // ------------------------------------------------------------------------ | |
70 | // Methods for the Parametrized runner | |
71 | // ------------------------------------------------------------------------ | |
72 | ||
73 | /** | |
74 | * Get the existing trace paths in the CTF-Testsuite git tree. | |
75 | * | |
76 | * @return The list of CTF traces (directories) to test | |
77 | */ | |
78 | @Parameters(name = "{index}: {0}") | |
79 | public static Iterable<Object[]> getTracePaths() { | |
3de23137 | 80 | final List<Object[]> dirs = new LinkedList<>(); |
a1492e22 | 81 | |
519b3cab AM |
82 | addDirsFrom(dirs, BASE_PATH.resolve(Paths.get("fuzzing", "metadata", "fail")), false); |
83 | addDirsFrom(dirs, BASE_PATH.resolve(Paths.get("fuzzing", "metadata", "pass")), true); | |
84 | addDirsFrom(dirs, BASE_PATH.resolve(Paths.get("fuzzing", "stream", "fail")), false); | |
85 | addDirsFrom(dirs, BASE_PATH.resolve(Paths.get("fuzzing", "stream", "pass")), true); | |
a1492e22 | 86 | |
519b3cab AM |
87 | addDirsFrom(dirs, BASE_PATH.resolve(Paths.get("regression", "metadata", "fail")), false); |
88 | addDirsFrom(dirs, BASE_PATH.resolve(Paths.get("regression", "metadata", "pass")), true); | |
89 | addDirsFrom(dirs, BASE_PATH.resolve(Paths.get("regression", "stream", "fail")), false); | |
90 | addDirsFrom(dirs, BASE_PATH.resolve(Paths.get("regression", "stream", "pass")), true); | |
a1492e22 | 91 | |
519b3cab | 92 | addDirsFrom(dirs, BASE_PATH.resolve(Paths.get("stress", "metadata", "fail")), false); |
2dcb4dbc | 93 | addDirsOneLevelDeepFrom(dirs, BASE_PATH.resolve(Paths.get("stress", "metadata", "pass")), true); |
519b3cab | 94 | addDirsFrom(dirs, BASE_PATH.resolve(Paths.get("stress", "stream", "fail")), false); |
50115d63 | 95 | addDirsOneLevelDeepFrom(dirs, BASE_PATH.resolve(Paths.get("stress", "stream", "pass")), true); |
a1492e22 AM |
96 | |
97 | return dirs; | |
98 | } | |
99 | ||
519b3cab AM |
100 | private static void addDirsFrom(List<Object[]> dirs, Path path, boolean expectSuccess) { |
101 | if (!Files.exists(path)) { | |
102 | /* Some planned directories may not exist yet in the test suite */ | |
a1492e22 AM |
103 | return; |
104 | } | |
519b3cab AM |
105 | try (DirectoryStream<Path> ds = Files.newDirectoryStream(path, DIR_FILTER);) { |
106 | for (Path p : ds) { | |
107 | /* Add this test case to the list of tests to run */ | |
108 | Object array[] = new Object[] { p.toString(), expectSuccess }; | |
109 | dirs.add(array); | |
076fe600 | 110 | } |
519b3cab AM |
111 | } catch (IOException e) { |
112 | /* Something is wrong with the layout of the test suite? */ | |
113 | e.printStackTrace(); | |
076fe600 AM |
114 | } |
115 | } | |
116 | ||
50115d63 AM |
117 | /** |
118 | * Some test traces are not in pass/trace1, pass/trace2, etc. but rather | |
119 | * pass/test1/trace1, pass/test1/trace2, etc. | |
120 | * | |
121 | * This methods adds the directories one level "down" instead of the very | |
122 | * next level. | |
123 | */ | |
124 | private static void addDirsOneLevelDeepFrom(List<Object[]> dirs, Path path, | |
125 | boolean expectSuccess) { | |
126 | if (!Files.exists(path)) { | |
127 | return; | |
128 | } | |
129 | try (DirectoryStream<Path> ds = Files.newDirectoryStream(path, DIR_FILTER);) { | |
130 | for (Path p : ds) { | |
131 | addDirsFrom(dirs, p, expectSuccess); | |
132 | } | |
133 | } catch (IOException e) { | |
134 | e.printStackTrace(); | |
135 | } | |
136 | } | |
137 | ||
519b3cab AM |
138 | private static final DirectoryStream.Filter<Path> DIR_FILTER = |
139 | new DirectoryStream.Filter<Path>() { | |
140 | @Override | |
141 | public boolean accept(Path entry) { | |
142 | /* Only accept directories and non-blacklisted tests */ | |
143 | if (!Files.isDirectory(entry)) { | |
144 | return false; | |
145 | } | |
146 | for (Path ignoredTestPath : IGNORED_TESTS) { | |
147 | if (entry.equals(ignoredTestPath)) { | |
148 | System.err.println("Skipping test " + entry.toString() + " as requested."); | |
149 | return false; | |
150 | } | |
151 | } | |
152 | return true; | |
153 | } | |
154 | }; | |
a1492e22 AM |
155 | |
156 | // ------------------------------------------------------------------------ | |
157 | // Test constructor | |
158 | // ------------------------------------------------------------------------ | |
159 | ||
160 | /** | |
161 | * Constructor for the parametrized tests | |
162 | * | |
163 | * @param tracePath | |
164 | * The complete path to the trace to test | |
165 | * @param expectSuccess | |
166 | * Should this trace parse successfully, or not. | |
167 | */ | |
168 | public CtfTestSuiteTests(String tracePath, boolean expectSuccess) { | |
169 | fTracePath = tracePath; | |
170 | fExpectSuccess = expectSuccess; | |
171 | } | |
172 | ||
173 | // ------------------------------------------------------------------------ | |
174 | // Test methods | |
175 | // ------------------------------------------------------------------------ | |
176 | ||
177 | /** | |
178 | * Test opening and reading the trace | |
179 | */ | |
180 | @Test | |
181 | public void testTrace() { | |
b562a24f MK |
182 | try { |
183 | /* Instantiate the trace (which implies parsing the metadata) */ | |
184 | CTFTrace trace = new CTFTrace(fTracePath); | |
185 | /* Read the trace until the end */ | |
186 | try (CTFTraceReader reader = new CTFTraceReader(trace);) { | |
187 | ||
188 | reader.getCurrentEventDef(); | |
189 | while (reader.advance()) { | |
190 | assertNotNull(reader.getCurrentEventDef()); | |
191 | } | |
a1492e22 | 192 | |
b562a24f MK |
193 | checkIfWeShoudlSucceed(); |
194 | } | |
680f9173 | 195 | } catch (CTFException e) { |
d5444116 MK |
196 | checkIfWeShouldFail(e); |
197 | } catch (OutOfMemoryError e) { | |
198 | checkIfWeShouldFail(e); | |
a1492e22 AM |
199 | } |
200 | } | |
d5444116 MK |
201 | |
202 | private void checkIfWeShoudlSucceed() { | |
203 | if (!fExpectSuccess) { | |
204 | fail("Trace was expected to fail parsing: " + fTracePath); | |
205 | } | |
206 | } | |
207 | ||
208 | private void checkIfWeShouldFail(Throwable e) { | |
209 | if (fExpectSuccess) { | |
210 | fail("Trace was expected to succeed, but failed parsing: " + | |
211 | fTracePath + " (" + e.getMessage() + ")"); | |
212 | } | |
213 | } | |
a1492e22 | 214 | } |