8826f05267b1432be4a2b18e8bdd3c51cd2c587c
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / trace / CtfTmfTraceValidateTest.java
1 /*******************************************************************************
2 * Copyright (c) 2015 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 * Bernd Hufmann - IInitial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ctf.core.tests.trace;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19
20 import java.nio.file.Files;
21 import java.nio.file.Path;
22 import java.nio.file.Paths;
23 import java.util.LinkedList;
24 import java.util.List;
25
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
28 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
29 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTraceValidationStatus;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.Parameterized;
33 import org.junit.runners.Parameterized.Parameters;
34
35 /**
36 * The class <code>CtfTmfTraceValidateTest</code> contains tests for trace
37 * validation
38 * <code>{@link CtfTmfTrace#validate(org.eclipse.core.resources.IProject, String)}</code>
39 * .
40 *
41 * @author Bernd Hufmann
42 */
43 @RunWith(Parameterized.class)
44 public class CtfTmfTraceValidateTest {
45
46 private static final Path BASE_PATH = Paths.get("../org.eclipse.tracecompass.ctf.core.tests", "traces");
47 private static final Path CTF_SUITE_BASE_PATH = Paths.get("../org.eclipse.tracecompass.ctf.core.tests", "traces", "ctf-testsuite", "tests", "1.8");
48
49 private String fTrace;
50 private int fServerity;
51 private int fConfidence;
52 private boolean fHasException;
53
54 /**
55 * Gets a list of test case parameters.
56 *
57 * @return The list of CTF traces (directories) to test
58 */
59 @Parameters(name = "{index}: {0}")
60 public static Iterable<Object[]> getTracePaths() {
61 final List<Object[]> dirs = new LinkedList<>();
62 // text-only metadata, valid CTF trace (lttle-endian)
63 addDirsFrom(dirs, CTF_SUITE_BASE_PATH.resolve(Paths.get("regression", "metadata", "pass", "literal-integers")), IStatus.OK, 10, false);
64 // packet-based metadata, valid CTF trace (lttle-endian)
65 addDirsFrom(dirs, BASE_PATH.resolve(Paths.get("kernel")), IStatus.OK, 10, false);
66 // text-only metadata, but invalid
67 addDirsFrom(dirs, CTF_SUITE_BASE_PATH.resolve(Paths.get("regression", "metadata", "fail", "enum-empty")), IStatus.WARNING, 1, true);
68 // packet-based metadata, but invalid
69 addDirsFrom(dirs, CTF_SUITE_BASE_PATH.resolve(Paths.get("regression", "metadata", "fail", "lttng-modules-2.0-pre1")), IStatus.WARNING, 1, true);
70 // pass file instead of directory
71 addDirsFrom(dirs, BASE_PATH.resolve(Paths.get("trace2.tar.bz2")), IStatus.ERROR, 1, false);
72
73 return dirs;
74 }
75
76 private static void addDirsFrom(List<Object[]> dirs, Path path, int severity, int confidence, boolean hasException) {
77 if (!Files.exists(path)) {
78 /* Some planned directories may not exist yet in the test suite */
79 return;
80 }
81
82 Object array[] = new Object[] { path.toString(), severity, confidence, hasException };
83 dirs.add(array);
84 }
85
86 /**
87 * @param trace
88 * a trace path
89 * @param severity
90 * severity of validation status expected
91 * @param confidence
92 * confidence of validation status expected
93 * @param hasException
94 * flag whether validation status should contain exception
95 */
96 public CtfTmfTraceValidateTest(String trace, int severity, int confidence, boolean hasException) {
97 fTrace = trace;
98 fServerity = severity;
99 fConfidence = confidence;
100 fHasException = hasException;
101 }
102
103 /**
104 * Main test cases
105 */
106 @Test
107 public void testValidate() {
108 try (CtfTmfTrace trace = new CtfTmfTrace();) {
109 IStatus status = trace.validate(null, fTrace);
110 assertEquals(toString(), fServerity, status.getSeverity());
111
112 if (fHasException) {
113 assertNotNull(toString(), status.getException());
114 }
115 switch (status.getSeverity()) {
116 case IStatus.OK: {
117 assertTrue(status instanceof CtfTraceValidationStatus);
118 CtfTraceValidationStatus ctfStatus = (CtfTraceValidationStatus) status;
119 assertEquals(toString(), fConfidence, ctfStatus.getConfidence());
120 assertNotNull(ctfStatus.getEnvironment());
121 break;
122 }
123 case IStatus.WARNING: {
124 assertTrue(status instanceof TraceValidationStatus);
125 TraceValidationStatus ctfStatus = (TraceValidationStatus) status;
126 assertEquals(fConfidence, ctfStatus.getConfidence());
127 break;
128 }
129 case IStatus.ERROR: {
130 // nothing else to check here
131 break;
132 }
133 default:
134 // no other severity should be returned
135 fail();
136 break;
137 }
138 assertEquals(fServerity, status.getSeverity());
139 }
140 }
141
142 }
This page took 0.047503 seconds and 4 git commands to generate.