Commit | Line | Data |
---|---|---|
e11e382c FW |
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; | |
6d20d989 | 17 | import static org.junit.Assert.assertNotNull; |
e11e382c FW |
18 | import static org.junit.Assert.assertTrue; |
19 | import static org.junit.Assert.fail; | |
20 | ||
21 | import java.io.File; | |
6d20d989 | 22 | import java.util.List; |
e11e382c FW |
23 | |
24 | import org.eclipse.core.resources.IWorkspace; | |
25 | import org.eclipse.core.resources.ResourcesPlugin; | |
26 | import org.eclipse.core.runtime.IPath; | |
8945f67f | 27 | import org.eclipse.core.runtime.IStatus; |
6d20d989 | 28 | import org.eclipse.jdt.annotation.NonNull; |
e11e382c | 29 | import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils; |
6d20d989 | 30 | import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; |
e11e382c FW |
31 | import org.eclipse.linuxtools.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles; |
32 | import org.junit.After; | |
33 | import org.junit.Test; | |
6d20d989 | 34 | import org.w3c.dom.Element; |
e11e382c FW |
35 | |
36 | /** | |
37 | * Tests for the {@link XmlUtils} class | |
38 | * | |
39 | * @author Geneviève Bastien | |
40 | */ | |
41 | public class XmlUtilsTest { | |
42 | ||
43 | /** | |
44 | * Empty the XML directory after the test | |
45 | */ | |
46 | @After | |
47 | public void emptyXmlFolder() { | |
48 | File fFolder = XmlUtils.getXmlFilesPath().toFile(); | |
49 | if (!(fFolder.isDirectory() && fFolder.exists())) { | |
50 | return; | |
51 | } | |
52 | for (File xmlFile : fFolder.listFiles()) { | |
53 | xmlFile.delete(); | |
54 | } | |
55 | } | |
56 | ||
57 | /** | |
58 | * Test the {@link XmlUtils#getXmlFilesPath()} method | |
59 | */ | |
60 | @Test | |
61 | public void testXmlPath() { | |
62 | IPath xmlPath = XmlUtils.getXmlFilesPath(); | |
63 | ||
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"); | |
71 | ||
72 | assertEquals(xmlPath, workspacePath); | |
73 | } | |
74 | ||
75 | /** | |
76 | * test the {@link XmlUtils#xmlValidate(File)} method | |
77 | */ | |
78 | @Test | |
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"); | |
83 | } | |
8945f67f GB |
84 | IStatus status = XmlUtils.xmlValidate(testXmlFile); |
85 | if (!status.isOK()) { | |
86 | fail(status.getMessage()); | |
87 | } | |
e11e382c FW |
88 | |
89 | testXmlFile = TmfXmlTestFiles.INVALID_FILE.getFile(); | |
90 | if ((testXmlFile == null) || !testXmlFile.exists()) { | |
91 | fail("XML test file does not exist"); | |
92 | } | |
93 | assertFalse(XmlUtils.xmlValidate(testXmlFile).isOK()); | |
94 | } | |
95 | ||
96 | /** | |
97 | * test the {@link XmlUtils#addXmlFile(File)} method | |
98 | */ | |
99 | @Test | |
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()); | |
105 | ||
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"); | |
110 | } | |
111 | ||
112 | XmlUtils.addXmlFile(testXmlFile); | |
113 | assertTrue(destFile.exists()); | |
114 | } | |
115 | ||
6d20d989 GB |
116 | @NonNull private static final String ANALYSIS_ID = "kernel.linux.sp"; |
117 | ||
118 | /** | |
119 | * Test the {@link XmlUtils#getElementInFile(String, String, String)} method | |
120 | */ | |
121 | @Test | |
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"); | |
126 | } | |
127 | /* | |
128 | * This sounds useless, but I get a potential null pointer warning | |
129 | * otherwise | |
130 | */ | |
131 | if (testXmlFile == null) { | |
132 | return; | |
133 | } | |
134 | ||
135 | Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID); | |
136 | assertNotNull(analysis); | |
137 | } | |
138 | ||
139 | /** | |
140 | * Test the {@link XmlUtils#getChildElements(Element)} and | |
141 | * {@link XmlUtils#getChildElements(Element, String)} methods | |
142 | */ | |
143 | @Test | |
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"); | |
148 | } | |
149 | /* | |
150 | * This sounds useless, but I get a potential null pointer warning | |
151 | * otherwise | |
152 | */ | |
153 | if (testXmlFile == null) { | |
154 | return; | |
155 | } | |
156 | ||
157 | Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID); | |
158 | ||
159 | List<Element> values = XmlUtils.getChildElements(analysis, TmfXmlStrings.DEFINED_VALUE); | |
160 | assertEquals(12, values.size()); | |
161 | ||
162 | values = XmlUtils.getChildElements(analysis, TmfXmlStrings.HEAD); | |
163 | assertEquals(1, values.size()); | |
164 | ||
165 | Element head = values.get(0); | |
166 | values = XmlUtils.getChildElements(head); | |
167 | assertEquals(2, values.size()); | |
168 | ||
169 | } | |
170 | ||
e11e382c | 171 | } |