Commit | Line | Data |
---|---|---|
e11e382c FW |
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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common; |
e11e382c | 14 | |
9a0aa59e GB |
15 | import static org.junit.Assert.fail; |
16 | ||
e11e382c | 17 | import java.io.File; |
9a0aa59e GB |
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; | |
e11e382c FW |
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 */ | |
9a0aa59e | 37 | VALID_FILE("test_xml_files/test_valid/test_valid.xml"), |
e11e382c | 38 | /** An invalid test file */ |
9a0aa59e | 39 | INVALID_FILE("test_xml_files/test_invalid/test_invalid.xml"); |
e11e382c FW |
40 | |
41 | private final String fPath; | |
42 | ||
43 | private TmfXmlTestFiles(String file) { | |
44 | fPath = file; | |
45 | } | |
46 | ||
47 | /** | |
9a0aa59e | 48 | * Get the absolute path of this test file |
e11e382c | 49 | * |
9a0aa59e | 50 | * @return The absolute path of this test file |
e11e382c | 51 | */ |
9a0aa59e GB |
52 | public IPath getPath() { |
53 | IPath absPath = Activator.getAbsolutePath(new Path(fPath)); | |
54 | if (absPath == null) { | |
55 | fail("Cannot find file path for '" + fPath + "'"); | |
56 | } | |
57 | return absPath; | |
e11e382c FW |
58 | } |
59 | ||
60 | /** | |
61 | * Returns the file object corresponding to the test XML file | |
62 | * | |
63 | * @return The file object for this test file | |
64 | */ | |
65 | public File getFile() { | |
9a0aa59e GB |
66 | return getPath().toFile(); |
67 | } | |
68 | ||
69 | /** | |
70 | * Get the XML {@link Document} for this test xml file | |
71 | * | |
72 | * @return The XML {@link Document} | |
73 | */ | |
74 | public Document getXmlDocument() { | |
75 | /* Initialize the state provider module */ | |
76 | Document doc = null; | |
77 | try { | |
78 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
79 | DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |
80 | doc = dBuilder.parse(getFile()); | |
81 | doc.getDocumentElement().normalize(); | |
82 | } catch (ParserConfigurationException e) { | |
83 | fail("Xml document parse exception"); | |
84 | } catch (SAXException e) { | |
85 | fail("Exception parsing xml file"); | |
86 | } catch (IOException e) { | |
87 | fail("File io exception"); | |
88 | } | |
89 | return doc; | |
e11e382c FW |
90 | } |
91 | ||
92 | } |