b39da5fe20ee55b9713582d539d7f7275a039c86
[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 École Polytechnique de Montréal
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.List;
23
24 import org.eclipse.core.resources.IWorkspace;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
31 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
32 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.Activator;
33 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles;
34 import org.junit.After;
35 import org.junit.Test;
36 import org.w3c.dom.Element;
37
38 /**
39 * Tests for the {@link XmlUtils} class
40 *
41 * @author Geneviève Bastien
42 */
43 public class XmlUtilsTest {
44
45 private static final Path PATH_INVALID = new Path("test_xml_files/test_invalid");
46 private static final Path PATH_VALID = new Path("test_xml_files/test_valid");
47
48 /**
49 * Empty the XML directory after the test
50 */
51 @After
52 public void emptyXmlFolder() {
53 File fFolder = XmlUtils.getXmlFilesPath().toFile();
54 if (!(fFolder.isDirectory() && fFolder.exists())) {
55 return;
56 }
57 for (File xmlFile : fFolder.listFiles()) {
58 xmlFile.delete();
59 }
60 }
61
62 /**
63 * Test the {@link XmlUtils#getXmlFilesPath()} method
64 */
65 @Test
66 public void testXmlPath() {
67 IPath xmlPath = XmlUtils.getXmlFilesPath();
68
69 IWorkspace workspace = ResourcesPlugin.getWorkspace();
70 IPath workspacePath = workspace.getRoot().getRawLocation();
71 workspacePath = workspacePath.addTrailingSeparator()
72 .append(".metadata").addTrailingSeparator().append(".plugins")
73 .addTrailingSeparator()
74 .append("org.eclipse.tracecompass.tmf.analysis.xml.core")
75 .addTrailingSeparator().append("xml_files");
76
77 assertEquals(xmlPath, workspacePath);
78 }
79
80 /**
81 * test the {@link XmlUtils#xmlValidate(File)} method
82 */
83 @Test
84 public void testXmlValidate() {
85 File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile();
86 if ((testXmlFile == null) || !testXmlFile.exists()) {
87 fail("XML test file does not exist");
88 }
89 IStatus status = XmlUtils.xmlValidate(testXmlFile);
90 if (!status.isOK()) {
91 fail(status.getMessage());
92 }
93
94 testXmlFile = TmfXmlTestFiles.INVALID_FILE.getFile();
95 if ((testXmlFile == null) || !testXmlFile.exists()) {
96 fail("XML test file does not exist");
97 }
98 assertFalse(XmlUtils.xmlValidate(testXmlFile).isOK());
99
100 }
101
102 /**
103 * Test various invalid files and make sure they are invalid
104 */
105 @Test
106 public void testXmlValidateInvalid() {
107 IPath path = Activator.getAbsolutePath(PATH_INVALID);
108 File file = path.toFile();
109
110 File[] invalidFiles = file.listFiles();
111 assertTrue(invalidFiles.length > 0);
112 for (File f : invalidFiles) {
113 assertFalse("File " + f.getName(), XmlUtils.xmlValidate(f).isOK());
114 }
115 }
116
117 /**
118 * Test various valid files and make sure they are valid
119 */
120 @Test
121 public void testXmlValidateValid() {
122 IPath path = Activator.getAbsolutePath(PATH_VALID);
123 File file = path.toFile();
124
125 File[] validFiles = file.listFiles();
126 assertTrue(validFiles.length > 0);
127 for (File f : validFiles) {
128 assertTrue("File " + f.getName(), XmlUtils.xmlValidate(f).isOK());
129 }
130 }
131
132 /**
133 * test the {@link XmlUtils#addXmlFile(File)} method
134 */
135 @Test
136 public void testXmlAddFile() {
137 /* Check the file does not exist */
138 IPath xmlPath = XmlUtils.getXmlFilesPath().addTrailingSeparator().append("test_valid.xml");
139 File destFile = xmlPath.toFile();
140 assertFalse(destFile.exists());
141
142 /* Add test_valid.xml file */
143 File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile();
144 if ((testXmlFile == null) || !testXmlFile.exists()) {
145 fail("XML test file does not exist");
146 }
147
148 XmlUtils.addXmlFile(testXmlFile);
149 assertTrue(destFile.exists());
150 }
151
152 private static final @NonNull String ANALYSIS_ID = "kernel.linux.sp";
153
154 /**
155 * Test the {@link XmlUtils#getElementInFile(String, String, String)} method
156 */
157 @Test
158 public void testGetElementInFile() {
159 File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile();
160 if ((testXmlFile == null) || !testXmlFile.exists()) {
161 fail("XML test file does not exist");
162 }
163 /*
164 * This sounds useless, but I get a potential null pointer warning
165 * otherwise
166 */
167 if (testXmlFile == null) {
168 return;
169 }
170
171 Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID);
172 assertNotNull(analysis);
173 }
174
175 /**
176 * Test the {@link XmlUtils#getChildElements(Element)} and
177 * {@link XmlUtils#getChildElements(Element, String)} methods
178 */
179 @Test
180 public void testGetChildElements() {
181 File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile();
182 if ((testXmlFile == null) || !testXmlFile.exists()) {
183 fail("XML test file does not exist");
184 }
185 /*
186 * This sounds useless, but I get a potential null pointer warning
187 * otherwise
188 */
189 if (testXmlFile == null) {
190 return;
191 }
192
193 Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID);
194
195 List<Element> values = XmlUtils.getChildElements(analysis, TmfXmlStrings.LOCATION);
196 assertEquals(5, values.size());
197
198 Element aLocation = values.get(0);
199 List<Element> attributes = XmlUtils.getChildElements(aLocation, TmfXmlStrings.STATE_ATTRIBUTE);
200 assertEquals(2, attributes.size());
201
202 values = XmlUtils.getChildElements(analysis, TmfXmlStrings.HEAD);
203 assertEquals(1, values.size());
204
205 Element head = values.get(0);
206 values = XmlUtils.getChildElements(head);
207 assertEquals(2, values.size());
208
209 }
210
211 }
This page took 0.03576 seconds and 5 git commands to generate.