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 */ |
d2e62865 GB |
39 | INVALID_FILE("test_xml_files/test_invalid/test_invalid.xml"), |
40 | /** A valid file for state attribute tests */ | |
900cbf89 JCK |
41 | ATTRIBUTE_FILE("test_xml_files/test_valid/test_attributes.xml"), |
42 | /** A valid file for conditions tests */ | |
74f8a7ec JCK |
43 | CONDITION_FILE("test_xml_files/test_valid/test_conditions.xml"), |
44 | /** A valid file for pattern tests */ | |
45 | VALID_PATTERN_FILE("test_xml_files/test_valid/test_valid_pattern.xml"); | |
e11e382c FW |
46 | |
47 | private final String fPath; | |
48 | ||
49 | private TmfXmlTestFiles(String file) { | |
50 | fPath = file; | |
51 | } | |
52 | ||
53 | /** | |
9a0aa59e | 54 | * Get the absolute path of this test file |
e11e382c | 55 | * |
9a0aa59e | 56 | * @return The absolute path of this test file |
e11e382c | 57 | */ |
9a0aa59e GB |
58 | public IPath getPath() { |
59 | IPath absPath = Activator.getAbsolutePath(new Path(fPath)); | |
60 | if (absPath == null) { | |
61 | fail("Cannot find file path for '" + fPath + "'"); | |
62 | } | |
63 | return absPath; | |
e11e382c FW |
64 | } |
65 | ||
66 | /** | |
67 | * Returns the file object corresponding to the test XML file | |
68 | * | |
69 | * @return The file object for this test file | |
70 | */ | |
71 | public File getFile() { | |
9a0aa59e GB |
72 | return getPath().toFile(); |
73 | } | |
74 | ||
75 | /** | |
76 | * Get the XML {@link Document} for this test xml file | |
77 | * | |
78 | * @return The XML {@link Document} | |
79 | */ | |
80 | public Document getXmlDocument() { | |
81 | /* Initialize the state provider module */ | |
82 | Document doc = null; | |
83 | try { | |
84 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
85 | DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |
86 | doc = dBuilder.parse(getFile()); | |
87 | doc.getDocumentElement().normalize(); | |
88 | } catch (ParserConfigurationException e) { | |
89 | fail("Xml document parse exception"); | |
90 | } catch (SAXException e) { | |
91 | fail("Exception parsing xml file"); | |
92 | } catch (IOException e) { | |
93 | fail("File io exception"); | |
94 | } | |
95 | return doc; | |
e11e382c FW |
96 | } |
97 | ||
98 | } |