tmf : Add test suite for the pattern segment builder
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core.tests / src / org / eclipse / tracecompass / tmf / analysis / xml / core / tests / module / XmlUtilsTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal and others
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.analysis.xml.core.tests.module;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
20
21 import java.io.File;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.eclipse.core.resources.IWorkspace;
27 import org.eclipse.core.resources.ResourcesPlugin;
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.Path;
31 import org.eclipse.jdt.annotation.NonNull;
32 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
33 import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
34 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
35 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
36 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
37 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
38 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
39 import org.eclipse.tracecompass.tmf.analysis.xml.core.segment.TmfXmlPatternSegment;
40 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
41 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.XmlStateSystemModule;
42 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.Activator;
43 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles;
44 import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
45 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
46 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
47 import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
48 import org.junit.After;
49 import org.junit.Test;
50 import org.w3c.dom.Document;
51 import org.w3c.dom.Element;
52 import org.w3c.dom.NodeList;
53
54 /**
55 * Tests for the {@link XmlUtils} class
56 *
57 * @author Geneviève Bastien
58 */
59 public class XmlUtilsTest {
60
61 private static final Path PATH_INVALID = new Path("test_xml_files/test_invalid");
62 private static final Path PATH_VALID = new Path("test_xml_files/test_valid");
63
64 /**
65 * Empty the XML directory after the test
66 */
67 @After
68 public void emptyXmlFolder() {
69 File fFolder = XmlUtils.getXmlFilesPath().toFile();
70 if (!(fFolder.isDirectory() && fFolder.exists())) {
71 return;
72 }
73 for (File xmlFile : fFolder.listFiles()) {
74 xmlFile.delete();
75 }
76 }
77
78 /**
79 * Test the {@link XmlUtils#getXmlFilesPath()} method
80 */
81 @Test
82 public void testXmlPath() {
83 IPath xmlPath = XmlUtils.getXmlFilesPath();
84
85 IWorkspace workspace = ResourcesPlugin.getWorkspace();
86 IPath workspacePath = workspace.getRoot().getRawLocation();
87 workspacePath = workspacePath.addTrailingSeparator()
88 .append(".metadata").addTrailingSeparator().append(".plugins")
89 .addTrailingSeparator()
90 .append("org.eclipse.tracecompass.tmf.analysis.xml.core")
91 .addTrailingSeparator().append("xml_files");
92
93 assertEquals(xmlPath, workspacePath);
94 }
95
96 /**
97 * test the {@link XmlUtils#xmlValidate(File)} method
98 */
99 @Test
100 public void testXmlValidate() {
101 File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile();
102 if ((testXmlFile == null) || !testXmlFile.exists()) {
103 fail("XML test file does not exist");
104 }
105 IStatus status = XmlUtils.xmlValidate(testXmlFile);
106 if (!status.isOK()) {
107 fail(status.getMessage());
108 }
109
110 testXmlFile = TmfXmlTestFiles.INVALID_FILE.getFile();
111 if ((testXmlFile == null) || !testXmlFile.exists()) {
112 fail("XML test file does not exist");
113 }
114 assertFalse(XmlUtils.xmlValidate(testXmlFile).isOK());
115 }
116
117 /**
118 * Test various invalid files and make sure they are invalid
119 */
120 @Test
121 public void testXmlValidateInvalid() {
122 IPath path = Activator.getAbsolutePath(PATH_INVALID);
123 File file = path.toFile();
124
125 File[] invalidFiles = file.listFiles();
126 assertTrue(invalidFiles.length > 0);
127 for (File f : invalidFiles) {
128 assertFalse("File " + f.getName(), XmlUtils.xmlValidate(f).isOK());
129 }
130 }
131
132 /**
133 * Test various valid files and make sure they are valid
134 */
135 @Test
136 public void testXmlValidateValid() {
137 IPath path = Activator.getAbsolutePath(PATH_VALID);
138 File file = path.toFile();
139
140 File[] validFiles = file.listFiles();
141 assertTrue(validFiles.length > 0);
142 for (File f : validFiles) {
143 assertTrue("File " + f.getName(), XmlUtils.xmlValidate(f).isOK());
144 }
145 }
146
147 /**
148 * test the {@link XmlUtils#addXmlFile(File)} method
149 */
150 @Test
151 public void testXmlAddFile() {
152 /* Check the file does not exist */
153 IPath xmlPath = XmlUtils.getXmlFilesPath().addTrailingSeparator().append("test_valid.xml");
154 File destFile = xmlPath.toFile();
155 assertFalse(destFile.exists());
156
157 /* Add test_valid.xml file */
158 File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile();
159 if ((testXmlFile == null) || !testXmlFile.exists()) {
160 fail("XML test file does not exist");
161 }
162
163 XmlUtils.addXmlFile(testXmlFile);
164 assertTrue(destFile.exists());
165 }
166
167 private static final @NonNull String ANALYSIS_ID = "kernel.linux.sp";
168
169 /**
170 * Test the {@link XmlUtils#getElementInFile(String, String, String)} method
171 */
172 @Test
173 public void testGetElementInFile() {
174 File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile();
175 assertNotNull("XML test file does not exist", testXmlFile);
176 assertTrue("XML test file does not exist", testXmlFile.exists());
177 Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID);
178 assertNotNull(analysis);
179 }
180
181 /**
182 * Test the {@link XmlUtils#getChildElements(Element)} and
183 * {@link XmlUtils#getChildElements(Element, String)} methods
184 */
185 @Test
186 public void testGetChildElements() {
187 File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile();
188 if ((testXmlFile == null) || !testXmlFile.exists()) {
189 fail("XML test file does not exist");
190 }
191 /*
192 * This sounds useless, but I get a potential null pointer warning
193 * otherwise
194 */
195 if (testXmlFile == null) {
196 return;
197 }
198
199 Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID);
200
201 List<Element> values = XmlUtils.getChildElements(analysis, TmfXmlStrings.LOCATION);
202 assertEquals(5, values.size());
203
204 Element aLocation = values.get(0);
205 List<Element> attributes = XmlUtils.getChildElements(aLocation, TmfXmlStrings.STATE_ATTRIBUTE);
206 assertEquals(2, attributes.size());
207
208 values = XmlUtils.getChildElements(analysis, TmfXmlStrings.HEAD);
209 assertEquals(1, values.size());
210
211 Element head = values.get(0);
212 values = XmlUtils.getChildElements(head);
213 assertEquals(2, values.size());
214
215 }
216
217 /**
218 * Initialize a new trace based using the input file path
219 *
220 * @param traceFile
221 * The trace file
222 * @return The trace
223 */
224 public static @NonNull ITmfTrace initializeTrace(String traceFile) {
225 /* Initialize the trace */
226 TmfXmlTraceStub trace = new TmfXmlTraceStub();
227 try {
228 trace.initTrace(null, Activator.getAbsolutePath(new Path(traceFile)).toOSString(), TmfEvent.class);
229 } catch (TmfTraceException e1) {
230 fail(e1.getMessage());
231 }
232 return trace;
233 }
234
235 /**
236 * Initialize a new module using the xml file
237 *
238 * @param xmlAnalysisFile
239 * The xml file used to initialize the module
240 * @return The module
241 */
242 public static @NonNull XmlStateSystemModule initializeModule(TmfXmlTestFiles xmlAnalysisFile) {
243
244 /* Initialize the state provider module */
245 Document doc = xmlAnalysisFile.getXmlDocument();
246 assertNotNull(doc);
247
248 /* get State Providers modules */
249 NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER);
250 assertFalse(stateproviderNodes.getLength() == 0);
251
252 Element node = (Element) stateproviderNodes.item(0);
253 XmlStateSystemModule module = new XmlStateSystemModule();
254 String moduleId = node.getAttribute(TmfXmlStrings.ID);
255 assertNotNull(moduleId);
256 module.setId(moduleId);
257
258 module.setXmlFile(xmlAnalysisFile.getPath());
259
260 return module;
261 }
262
263 /**
264 * This function test the data provided by the state intervals queried
265 *
266 * @param testId
267 * The id of the test
268 * @param ss
269 * The state system associated to this test
270 * @param quark
271 * The quark we want to query
272 * @param expectedStarts
273 * The expected start timestamps for the intervals generated for
274 * this quark
275 * @param expectedValues
276 * The expected content values for this quark
277 * @throws AttributeNotFoundException
278 * If the quark we want to query is invalid
279 * @throws StateSystemDisposedException
280 * If the state system has been disposed before the end of the
281 * queries
282 */
283 public static void verifyStateIntervals(String testId, @NonNull ITmfStateSystem ss, Integer quark, int[] expectedStarts, ITmfStateValue[] expectedValues) throws AttributeNotFoundException, StateSystemDisposedException {
284 int expectedCount = expectedStarts.length - 1;
285 List<ITmfStateInterval> intervals = StateSystemUtils.queryHistoryRange(ss, quark, expectedStarts[0], expectedStarts[expectedCount]);
286 assertEquals(testId + ": Interval count", expectedCount, intervals.size());
287 for (int i = 0; i < expectedCount; i++) {
288 ITmfStateInterval interval = intervals.get(i);
289 assertEquals(testId + ": Start time of interval " + i, expectedStarts[i], interval.getStartTime());
290 long actualEnd = (i == expectedCount - 1) ? (expectedStarts[i + 1]) : (expectedStarts[i + 1]) - 1;
291 assertEquals(testId + ": End time of interval " + i, actualEnd, interval.getEndTime());
292 assertEquals(testId + ": Expected value of interval " + i, expectedValues[i], interval.getStateValue());
293 }
294 }
295
296 /**
297 * Test a pattern segment against what is expected
298 *
299 * @param expected
300 * The expected pattern segment
301 * @param actual
302 * The actual pattern segment
303 */
304 public static void testPatternSegmentData(TmfXmlPatternSegment expected, TmfXmlPatternSegment actual) {
305 assertEquals("getStart", expected.getStart(), actual.getStart());
306 assertEquals("getEnd", expected.getEnd(), actual.getEnd());
307 assertEquals("getScale", expected.getScale(), actual.getScale());
308 assertEquals("getName", expected.getName(), actual.getName());
309 assertNotNull("getContent", actual.getContent());
310
311 // Test the content of the pattern segment
312 assertEquals("content size", expected.getContent().size(), actual.getContent().size());
313 Iterator<Map.Entry<String, @NonNull ITmfStateValue>> it2 = expected.getContent().entrySet().iterator();
314 for (int i = 0; i < expected.getContent().size(); i++) {
315 Map.Entry<String, @NonNull ITmfStateValue> expectedContent = it2.next();
316 ITmfStateValue actualValue = actual.getContent().get(expectedContent.getKey());
317 assertNotNull("Content " + expectedContent.getKey() + " exists", actualValue);
318 assertEquals("Content value comparison " + i, 0, expectedContent.getValue().compareTo(actualValue));
319 }
320 }
321
322 }
This page took 0.050773 seconds and 5 git commands to generate.