analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / 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
43 private final String fPath;
44
45 private TmfXmlTestFiles(String file) {
46 fPath = file;
47 }
48
49 /**
50 * Get the absolute path of this test file
51 *
52 * @return The absolute path of this test file
53 */
54 public IPath getPath() {
55 IPath absPath = Activator.getAbsolutePath(new Path(fPath));
56 if (absPath == null) {
57 fail("Cannot find file path for '" + fPath + "'");
58 }
59 return absPath;
60 }
61
62 /**
63 * Returns the file object corresponding to the test XML file
64 *
65 * @return The file object for this test file
66 */
67 public File getFile() {
68 return getPath().toFile();
69 }
70
71 /**
72 * Get the XML {@link Document} for this test xml file
73 *
74 * @return The XML {@link Document}
75 */
76 public Document getXmlDocument() {
77 /* Initialize the state provider module */
78 Document doc = null;
79 try {
80 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
81 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
82 doc = dBuilder.parse(getFile());
83 doc.getDocumentElement().normalize();
84 } catch (ParserConfigurationException e) {
85 fail("Xml document parse exception");
86 } catch (SAXException e) {
87 fail("Exception parsing xml file");
88 } catch (IOException e) {
89 fail("File io exception");
90 }
91 return doc;
92 }
93
94 }
This page took 0.035373 seconds and 5 git commands to generate.