ctf: Make CTFTrace and its reader classes AutoCloseable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / ctftestsuite / CtfTestSuiteTests.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
13 package org.eclipse.linuxtools.ctf.core.tests.ctftestsuite;
14
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.fail;
17
18 import java.io.File;
19 import java.util.LinkedList;
20 import java.util.List;
21
22 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
23 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
24 import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.TestRule;
28 import org.junit.rules.Timeout;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.Parameterized;
31 import org.junit.runners.Parameterized.Parameters;
32
33 /**
34 * Parameterized test class running the CTF Test Suite
35 *
36 * (from https://github.com/efficios/ctf-testsuite).
37 *
38 * @author Alexandre Montplaisir
39 */
40 @RunWith(Parameterized.class)
41 public class CtfTestSuiteTests {
42
43 /** Time-out tests after 10 seconds. */
44 @Rule
45 public TestRule globalTimeout = new Timeout(10000);
46
47 private static final String BASE_PATH = "traces/ctf-testsuite/tests/1.8/";
48
49 /**
50 * Test we know are currently failing. Ignore them so we can at least run
51 * the others.
52 *
53 * TODO Actually fix them!
54 */
55 private static final String[] IGNORED_TESTS = {
56 "regression/metadata/pass/sequence-typedef-length",
57 "regression/metadata/pass/array-of-struct"
58 };
59
60 private final String fTracePath;
61 private final boolean fExpectSuccess;
62
63 // ------------------------------------------------------------------------
64 // Methods for the Parametrized runner
65 // ------------------------------------------------------------------------
66
67 /**
68 * Get the existing trace paths in the CTF-Testsuite git tree.
69 *
70 * @return The list of CTF traces (directories) to test
71 */
72 @Parameters(name = "{index}: {0}")
73 public static Iterable<Object[]> getTracePaths() {
74 final List<Object[]> dirs = new LinkedList<>();
75
76 addDirsFrom(dirs, BASE_PATH + "fuzzing/metadata/fail", false);
77 addDirsFrom(dirs, BASE_PATH + "fuzzing/metadata/pass", true);
78 addDirsFrom(dirs, BASE_PATH + "fuzzing/stream/fail", false);
79 addDirsFrom(dirs, BASE_PATH + "fuzzing/stream/pass", true);
80
81 addDirsFrom(dirs, BASE_PATH + "regression/metadata/fail", false);
82 addDirsFrom(dirs, BASE_PATH + "regression/metadata/pass", true);
83 addDirsFrom(dirs, BASE_PATH + "regression/stream/fail", false);
84 addDirsFrom(dirs, BASE_PATH + "regression/stream/pass", true);
85
86 addDirsFrom(dirs, BASE_PATH + "stress/metadata/fail", false);
87 addDirsFrom(dirs, BASE_PATH + "stress/metadata/pass", true);
88 addDirsFrom(dirs, BASE_PATH + "stress/stream/fail", false);
89 addDirsFrom(dirs, BASE_PATH + "stress/stream/pass", true);
90
91 return dirs;
92 }
93
94 private static void addDirsFrom(List<Object[]> dirs, String path, boolean expectSuccess) {
95 File[] traceDirs = (new File(path)).listFiles();
96 if (traceDirs == null) {
97 return;
98 }
99 for (File traceDir : traceDirs) {
100 /* Skip the "run.sh" files and blacklisted tests */
101 if (!traceDir.isDirectory() || testIsBlacklisted(traceDir.getPath())) {
102 continue;
103 }
104
105 /* Add this test case to the list of tests to run */
106 Object array[] = new Object[] { traceDir.getPath(), expectSuccess };
107 dirs.add(array);
108 }
109 }
110
111 private static boolean testIsBlacklisted(String fullPath) {
112 for (String ignoredTest : IGNORED_TESTS) {
113 if (fullPath.contains(new File(ignoredTest).getPath())) {
114 return true;
115 }
116 }
117 return false;
118 }
119
120 // ------------------------------------------------------------------------
121 // Test constructor
122 // ------------------------------------------------------------------------
123
124 /**
125 * Constructor for the parametrized tests
126 *
127 * @param tracePath
128 * The complete path to the trace to test
129 * @param expectSuccess
130 * Should this trace parse successfully, or not.
131 */
132 public CtfTestSuiteTests(String tracePath, boolean expectSuccess) {
133 fTracePath = tracePath;
134 fExpectSuccess = expectSuccess;
135 }
136
137 // ------------------------------------------------------------------------
138 // Test methods
139 // ------------------------------------------------------------------------
140
141 /**
142 * Test opening and reading the trace
143 */
144 @Test
145 public void testTrace() {
146 try (/* Instantiate the trace (which implies parsing the metadata) */
147 CTFTrace trace = new CTFTrace(fTracePath);
148 /* Read the trace until the end */
149 CTFTraceReader reader = new CTFTraceReader(trace);) {
150
151 reader.getCurrentEventDef();
152 while (reader.advance()) {
153 assertNotNull(reader.getCurrentEventDef());
154 }
155
156 checkIfWeShoudlSucceed();
157 } catch (CTFReaderException e) {
158 checkIfWeShouldFail(e);
159 } catch (OutOfMemoryError e) {
160 checkIfWeShouldFail(e);
161 }
162 }
163
164 private void checkIfWeShoudlSucceed() {
165 if (!fExpectSuccess) {
166 fail("Trace was expected to fail parsing: " + fTracePath);
167 }
168 }
169
170 private void checkIfWeShouldFail(Throwable e) {
171 if (fExpectSuccess) {
172 fail("Trace was expected to succeed, but failed parsing: " +
173 fTracePath + " (" + e.getMessage() + ")");
174 }
175 }
176 }
This page took 0.035614 seconds and 5 git commands to generate.