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
{
44 * Empty the XML directory after the test
47 public void emptyXmlFolder() {
48 File fFolder
= XmlUtils
.getXmlFilesPath().toFile();
49 if (!(fFolder
.isDirectory() && fFolder
.exists())) {
52 for (File xmlFile
: fFolder
.listFiles()) {
58 * Test the {@link XmlUtils#getXmlFilesPath()} method
61 public void testXmlPath() {
62 IPath xmlPath
= XmlUtils
.getXmlFilesPath();
64 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
65 IPath workspacePath
= workspace
.getRoot().getRawLocation();
66 workspacePath
= workspacePath
.addTrailingSeparator()
67 .append(".metadata").addTrailingSeparator().append(".plugins")
68 .addTrailingSeparator()
69 .append("org.eclipse.linuxtools.tmf.analysis.xml.core")
70 .addTrailingSeparator().append("xml_files");
72 assertEquals(xmlPath
, workspacePath
);
76 * test the {@link XmlUtils#xmlValidate(File)} method
79 public void testXmlValidate() {
80 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
81 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
82 fail("XML test file does not exist");
84 IStatus status
= XmlUtils
.xmlValidate(testXmlFile
);
86 fail(status
.getMessage());
89 testXmlFile
= TmfXmlTestFiles
.INVALID_FILE
.getFile();
90 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
91 fail("XML test file does not exist");
93 assertFalse(XmlUtils
.xmlValidate(testXmlFile
).isOK());
97 * test the {@link XmlUtils#addXmlFile(File)} method
100 public void testXmlAddFile() {
101 /* Check the file does not exist */
102 IPath xmlPath
= XmlUtils
.getXmlFilesPath().addTrailingSeparator().append("test_valid.xml");
103 File destFile
= xmlPath
.toFile();
104 assertFalse(destFile
.exists());
106 /* Add test_valid.xml file */
107 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
108 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
109 fail("XML test file does not exist");
112 XmlUtils
.addXmlFile(testXmlFile
);
113 assertTrue(destFile
.exists());
116 @NonNull private static final String ANALYSIS_ID
= "kernel.linux.sp";
119 * Test the {@link XmlUtils#getElementInFile(String, String, String)} method
122 public void testGetElementInFile() {
123 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
124 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
125 fail("XML test file does not exist");
128 * This sounds useless, but I get a potential null pointer warning
131 if (testXmlFile
== null) {
135 Element analysis
= XmlUtils
.getElementInFile(testXmlFile
.getAbsolutePath(), TmfXmlStrings
.STATE_PROVIDER
, ANALYSIS_ID
);
136 assertNotNull(analysis
);
140 * Test the {@link XmlUtils#getChildElements(Element)} and
141 * {@link XmlUtils#getChildElements(Element, String)} methods
144 public void testGetChildElements() {
145 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
146 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
147 fail("XML test file does not exist");
150 * This sounds useless, but I get a potential null pointer warning
153 if (testXmlFile
== null) {
157 Element analysis
= XmlUtils
.getElementInFile(testXmlFile
.getAbsolutePath(), TmfXmlStrings
.STATE_PROVIDER
, ANALYSIS_ID
);
159 List
<Element
> values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.DEFINED_VALUE
);
160 assertEquals(12, values
.size());
162 values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.HEAD
);
163 assertEquals(1, values
.size());
165 Element head
= values
.get(0);
166 values
= XmlUtils
.getChildElements(head
);
167 assertEquals(2, values
.size());
This page took 0.036147 seconds and 6 git commands to generate.