TMF: Add unit tests for XML state providers
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.core.tests / src / org / eclipse / linuxtools / 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.linuxtools.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.assertTrue;
18 import static org.junit.Assert.fail;
19
20 import java.io.File;
21
22 import org.eclipse.core.resources.IWorkspace;
23 import org.eclipse.core.resources.ResourcesPlugin;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils;
27 import org.eclipse.linuxtools.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles;
28 import org.junit.After;
29 import org.junit.Test;
30
31 /**
32 * Tests for the {@link XmlUtils} class
33 *
34 * @author Geneviève Bastien
35 */
36 public class XmlUtilsTest {
37
38 /**
39 * Empty the XML directory after the test
40 */
41 @After
42 public void emptyXmlFolder() {
43 File fFolder = XmlUtils.getXmlFilesPath().toFile();
44 if (!(fFolder.isDirectory() && fFolder.exists())) {
45 return;
46 }
47 for (File xmlFile : fFolder.listFiles()) {
48 xmlFile.delete();
49 }
50 }
51
52 /**
53 * Test the {@link XmlUtils#getXmlFilesPath()} method
54 */
55 @Test
56 public void testXmlPath() {
57 IPath xmlPath = XmlUtils.getXmlFilesPath();
58
59 IWorkspace workspace = ResourcesPlugin.getWorkspace();
60 IPath workspacePath = workspace.getRoot().getRawLocation();
61 workspacePath = workspacePath.addTrailingSeparator()
62 .append(".metadata").addTrailingSeparator().append(".plugins")
63 .addTrailingSeparator()
64 .append("org.eclipse.linuxtools.tmf.analysis.xml.core")
65 .addTrailingSeparator().append("xml_files");
66
67 assertEquals(xmlPath, workspacePath);
68 }
69
70 /**
71 * test the {@link XmlUtils#xmlValidate(File)} method
72 */
73 @Test
74 public void testXmlValidate() {
75 File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile();
76 if ((testXmlFile == null) || !testXmlFile.exists()) {
77 fail("XML test file does not exist");
78 }
79 IStatus status = XmlUtils.xmlValidate(testXmlFile);
80 if (!status.isOK()) {
81 fail(status.getMessage());
82 }
83
84 testXmlFile = TmfXmlTestFiles.INVALID_FILE.getFile();
85 if ((testXmlFile == null) || !testXmlFile.exists()) {
86 fail("XML test file does not exist");
87 }
88 assertFalse(XmlUtils.xmlValidate(testXmlFile).isOK());
89 }
90
91 /**
92 * test the {@link XmlUtils#addXmlFile(File)} method
93 */
94 @Test
95 public void testXmlAddFile() {
96 /* Check the file does not exist */
97 IPath xmlPath = XmlUtils.getXmlFilesPath().addTrailingSeparator().append("test_valid.xml");
98 File destFile = xmlPath.toFile();
99 assertFalse(destFile.exists());
100
101 /* Add test_valid.xml file */
102 File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile();
103 if ((testXmlFile == null) || !testXmlFile.exists()) {
104 fail("XML test file does not exist");
105 }
106
107 XmlUtils.addXmlFile(testXmlFile);
108 assertTrue(destFile.exists());
109 }
110
111 }
This page took 0.033392 seconds and 5 git commands to generate.