1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.tests
.module
;
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
;
22 import java
.util
.List
;
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
.jdt
.annotation
.NonNull
;
29 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
30 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
31 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.tests
.common
.TmfXmlTestFiles
;
32 import org
.junit
.After
;
33 import org
.junit
.Test
;
34 import org
.w3c
.dom
.Element
;
37 * Tests for the {@link XmlUtils} class
39 * @author Geneviève Bastien
41 public class XmlUtilsTest
{
43 private static final String pathname
= "test_xml_files/test_invalid";
46 * Empty the XML directory after the test
49 public void emptyXmlFolder() {
50 File fFolder
= XmlUtils
.getXmlFilesPath().toFile();
51 if (!(fFolder
.isDirectory() && fFolder
.exists())) {
54 for (File xmlFile
: fFolder
.listFiles()) {
60 * Test the {@link XmlUtils#getXmlFilesPath()} method
63 public void testXmlPath() {
64 IPath xmlPath
= XmlUtils
.getXmlFilesPath();
66 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
67 IPath workspacePath
= workspace
.getRoot().getRawLocation();
68 workspacePath
= workspacePath
.addTrailingSeparator()
69 .append(".metadata").addTrailingSeparator().append(".plugins")
70 .addTrailingSeparator()
71 .append("org.eclipse.linuxtools.tmf.analysis.xml.core")
72 .addTrailingSeparator().append("xml_files");
74 assertEquals(xmlPath
, workspacePath
);
78 * test the {@link XmlUtils#xmlValidate(File)} method
81 public void testXmlValidate() {
82 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
83 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
84 fail("XML test file does not exist");
86 IStatus status
= XmlUtils
.xmlValidate(testXmlFile
);
88 fail(status
.getMessage());
91 testXmlFile
= TmfXmlTestFiles
.INVALID_FILE
.getFile();
92 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
93 fail("XML test file does not exist");
95 assertFalse(XmlUtils
.xmlValidate(testXmlFile
).isOK());
100 * Test various invalid files and make sure they are invalid
103 public void testXmlValidateInvalid() {
104 File
[] validFiles
= (new File(pathname
)).listFiles();
105 for (File f
: validFiles
) {
106 assertFalse("File " + f
.getName(), XmlUtils
.xmlValidate(f
).isOK());
112 * test the {@link XmlUtils#addXmlFile(File)} method
115 public void testXmlAddFile() {
116 /* Check the file does not exist */
117 IPath xmlPath
= XmlUtils
.getXmlFilesPath().addTrailingSeparator().append("test_valid.xml");
118 File destFile
= xmlPath
.toFile();
119 assertFalse(destFile
.exists());
121 /* Add test_valid.xml file */
122 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
123 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
124 fail("XML test file does not exist");
127 XmlUtils
.addXmlFile(testXmlFile
);
128 assertTrue(destFile
.exists());
131 @NonNull private static final String ANALYSIS_ID
= "kernel.linux.sp";
134 * Test the {@link XmlUtils#getElementInFile(String, String, String)} method
137 public void testGetElementInFile() {
138 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
139 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
140 fail("XML test file does not exist");
143 * This sounds useless, but I get a potential null pointer warning
146 if (testXmlFile
== null) {
150 Element analysis
= XmlUtils
.getElementInFile(testXmlFile
.getAbsolutePath(), TmfXmlStrings
.STATE_PROVIDER
, ANALYSIS_ID
);
151 assertNotNull(analysis
);
155 * Test the {@link XmlUtils#getChildElements(Element)} and
156 * {@link XmlUtils#getChildElements(Element, String)} methods
159 public void testGetChildElements() {
160 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
161 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
162 fail("XML test file does not exist");
165 * This sounds useless, but I get a potential null pointer warning
168 if (testXmlFile
== null) {
172 Element analysis
= XmlUtils
.getElementInFile(testXmlFile
.getAbsolutePath(), TmfXmlStrings
.STATE_PROVIDER
, ANALYSIS_ID
);
174 List
<Element
> values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.LOCATION
);
175 assertEquals(5, values
.size());
177 Element aLocation
= values
.get(0);
178 List
<Element
> attributes
= XmlUtils
.getChildElements(aLocation
, TmfXmlStrings
.STATE_ATTRIBUTE
);
179 assertEquals(2, attributes
.size());
181 values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.HEAD
);
182 assertEquals(1, values
.size());
184 Element head
= values
.get(0);
185 values
= XmlUtils
.getChildElements(head
);
186 assertEquals(2, values
.size());
This page took 0.036083 seconds and 6 git commands to generate.