b81c68fe4c018601db66c62130d4888ecffd31e3
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core.tests / common / org / eclipse / tracecompass / tmf / analysis / xml / core / tests / common / TmfXmlTestFiles.java
1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common;
14
15 import static org.junit.Assert.fail;
16
17 import java.io.File;
18 import java.io.IOException;
19
20 import javax.xml.parsers.DocumentBuilder;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.parsers.ParserConfigurationException;
23
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.Activator;
27 import org.w3c.dom.Document;
28 import org.xml.sax.SAXException;
29
30 /**
31 * Provides some test XML files to use
32 *
33 * @author Geneviève Bastien
34 */
35 public enum TmfXmlTestFiles {
36 /** A valid XML test file */
37 VALID_FILE("test_xml_files/test_valid/test_valid.xml"),
38 /** An invalid test file */
39 INVALID_FILE("test_xml_files/test_invalid/test_invalid.xml"),
40 /** A valid file for state attribute tests */
41 ATTRIBUTE_FILE("test_xml_files/test_valid/test_attributes.xml"),
42 /** A valid file for conditions tests */
43 CONDITION_FILE("test_xml_files/test_valid/test_conditions.xml"),
44 /** A valid file for doubles tests */
45 DOUBLES_FILE("test_xml_files/test_valid/test_doubles.xml"),
46 /** A valid file for pattern tests */
47 VALID_PATTERN_FILE("test_xml_files/test_valid/test_valid_pattern.xml"),
48 /** A valid pattern file to test the pattern segment **/
49 VALID_PATTERN_SEGMENT("test_xml_files/test_valid/test_pattern_segment.xml");
50
51 private final String fPath;
52
53 private TmfXmlTestFiles(String file) {
54 fPath = file;
55 }
56
57 /**
58 * Get the absolute path of this test file
59 *
60 * @return The absolute path of this test file
61 */
62 public IPath getPath() {
63 IPath absPath = Activator.getAbsolutePath(new Path(fPath));
64 if (absPath == null) {
65 fail("Cannot find file path for '" + fPath + "'");
66 }
67 return absPath;
68 }
69
70 /**
71 * Returns the file object corresponding to the test XML file
72 *
73 * @return The file object for this test file
74 */
75 public File getFile() {
76 return getPath().toFile();
77 }
78
79 /**
80 * Get the XML {@link Document} for this test xml file
81 *
82 * @return The XML {@link Document}
83 */
84 public Document getXmlDocument() {
85 /* Initialize the state provider module */
86 Document doc = null;
87 try {
88 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
89 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
90 doc = dBuilder.parse(getFile());
91 doc.getDocumentElement().normalize();
92 } catch (ParserConfigurationException e) {
93 fail("Xml document parse exception");
94 } catch (SAXException e) {
95 fail("Exception parsing xml file");
96 } catch (IOException e) {
97 fail("File io exception");
98 }
99 return doc;
100 }
101
102 }
This page took 0.054391 seconds and 4 git commands to generate.