b39da5fe20ee55b9713582d539d7f7275a039c86
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
.tracecompass
.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
.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
;
39 * Tests for the {@link XmlUtils} class
41 * @author Geneviève Bastien
43 public class XmlUtilsTest
{
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");
49 * Empty the XML directory after the test
52 public void emptyXmlFolder() {
53 File fFolder
= XmlUtils
.getXmlFilesPath().toFile();
54 if (!(fFolder
.isDirectory() && fFolder
.exists())) {
57 for (File xmlFile
: fFolder
.listFiles()) {
63 * Test the {@link XmlUtils#getXmlFilesPath()} method
66 public void testXmlPath() {
67 IPath xmlPath
= XmlUtils
.getXmlFilesPath();
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");
77 assertEquals(xmlPath
, workspacePath
);
81 * test the {@link XmlUtils#xmlValidate(File)} method
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");
89 IStatus status
= XmlUtils
.xmlValidate(testXmlFile
);
91 fail(status
.getMessage());
94 testXmlFile
= TmfXmlTestFiles
.INVALID_FILE
.getFile();
95 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
96 fail("XML test file does not exist");
98 assertFalse(XmlUtils
.xmlValidate(testXmlFile
).isOK());
103 * Test various invalid files and make sure they are invalid
106 public void testXmlValidateInvalid() {
107 IPath path
= Activator
.getAbsolutePath(PATH_INVALID
);
108 File file
= path
.toFile();
110 File
[] invalidFiles
= file
.listFiles();
111 assertTrue(invalidFiles
.length
> 0);
112 for (File f
: invalidFiles
) {
113 assertFalse("File " + f
.getName(), XmlUtils
.xmlValidate(f
).isOK());
118 * Test various valid files and make sure they are valid
121 public void testXmlValidateValid() {
122 IPath path
= Activator
.getAbsolutePath(PATH_VALID
);
123 File file
= path
.toFile();
125 File
[] validFiles
= file
.listFiles();
126 assertTrue(validFiles
.length
> 0);
127 for (File f
: validFiles
) {
128 assertTrue("File " + f
.getName(), XmlUtils
.xmlValidate(f
).isOK());
133 * test the {@link XmlUtils#addXmlFile(File)} method
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());
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");
148 XmlUtils
.addXmlFile(testXmlFile
);
149 assertTrue(destFile
.exists());
152 private static final @NonNull String ANALYSIS_ID
= "kernel.linux.sp";
155 * Test the {@link XmlUtils#getElementInFile(String, String, String)} method
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");
164 * This sounds useless, but I get a potential null pointer warning
167 if (testXmlFile
== null) {
171 Element analysis
= XmlUtils
.getElementInFile(testXmlFile
.getAbsolutePath(), TmfXmlStrings
.STATE_PROVIDER
, ANALYSIS_ID
);
172 assertNotNull(analysis
);
176 * Test the {@link XmlUtils#getChildElements(Element)} and
177 * {@link XmlUtils#getChildElements(Element, String)} methods
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");
186 * This sounds useless, but I get a potential null pointer warning
189 if (testXmlFile
== null) {
193 Element analysis
= XmlUtils
.getElementInFile(testXmlFile
.getAbsolutePath(), TmfXmlStrings
.STATE_PROVIDER
, ANALYSIS_ID
);
195 List
<Element
> values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.LOCATION
);
196 assertEquals(5, values
.size());
198 Element aLocation
= values
.get(0);
199 List
<Element
> attributes
= XmlUtils
.getChildElements(aLocation
, TmfXmlStrings
.STATE_ATTRIBUTE
);
200 assertEquals(2, attributes
.size());
202 values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.HEAD
);
203 assertEquals(1, values
.size());
205 Element head
= values
.get(0);
206 values
= XmlUtils
.getChildElements(head
);
207 assertEquals(2, values
.size());
This page took 0.03576 seconds and 5 git commands to generate.