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 | ||
6b5218d0 FW |
43 | private static final String pathname = "test_xml_files/test_invalid"; |
44 | ||
e11e382c FW |
45 | /** |
46 | * Empty the XML directory after the test | |
47 | */ | |
48 | @After | |
49 | public void emptyXmlFolder() { | |
50 | File fFolder = XmlUtils.getXmlFilesPath().toFile(); | |
51 | if (!(fFolder.isDirectory() && fFolder.exists())) { | |
52 | return; | |
53 | } | |
54 | for (File xmlFile : fFolder.listFiles()) { | |
55 | xmlFile.delete(); | |
56 | } | |
57 | } | |
58 | ||
59 | /** | |
60 | * Test the {@link XmlUtils#getXmlFilesPath()} method | |
61 | */ | |
62 | @Test | |
63 | public void testXmlPath() { | |
64 | IPath xmlPath = XmlUtils.getXmlFilesPath(); | |
65 | ||
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"); | |
73 | ||
74 | assertEquals(xmlPath, workspacePath); | |
75 | } | |
76 | ||
77 | /** | |
78 | * test the {@link XmlUtils#xmlValidate(File)} method | |
79 | */ | |
80 | @Test | |
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"); | |
85 | } | |
8945f67f GB |
86 | IStatus status = XmlUtils.xmlValidate(testXmlFile); |
87 | if (!status.isOK()) { | |
88 | fail(status.getMessage()); | |
89 | } | |
e11e382c FW |
90 | |
91 | testXmlFile = TmfXmlTestFiles.INVALID_FILE.getFile(); | |
92 | if ((testXmlFile == null) || !testXmlFile.exists()) { | |
93 | fail("XML test file does not exist"); | |
94 | } | |
95 | assertFalse(XmlUtils.xmlValidate(testXmlFile).isOK()); | |
6b5218d0 | 96 | |
e11e382c FW |
97 | } |
98 | ||
6b5218d0 FW |
99 | /** |
100 | * Test various invalid files and make sure they are invalid | |
101 | */ | |
102 | @Test | |
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()); | |
107 | } | |
108 | } | |
109 | ||
110 | ||
e11e382c FW |
111 | /** |
112 | * test the {@link XmlUtils#addXmlFile(File)} method | |
113 | */ | |
114 | @Test | |
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()); | |
120 | ||
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"); | |
125 | } | |
126 | ||
127 | XmlUtils.addXmlFile(testXmlFile); | |
128 | assertTrue(destFile.exists()); | |
129 | } | |
130 | ||
6d20d989 GB |
131 | @NonNull private static final String ANALYSIS_ID = "kernel.linux.sp"; |
132 | ||
133 | /** | |
134 | * Test the {@link XmlUtils#getElementInFile(String, String, String)} method | |
135 | */ | |
136 | @Test | |
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"); | |
141 | } | |
142 | /* | |
143 | * This sounds useless, but I get a potential null pointer warning | |
144 | * otherwise | |
145 | */ | |
146 | if (testXmlFile == null) { | |
147 | return; | |
148 | } | |
149 | ||
150 | Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID); | |
151 | assertNotNull(analysis); | |
152 | } | |
153 | ||
154 | /** | |
155 | * Test the {@link XmlUtils#getChildElements(Element)} and | |
156 | * {@link XmlUtils#getChildElements(Element, String)} methods | |
157 | */ | |
158 | @Test | |
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"); | |
163 | } | |
164 | /* | |
165 | * This sounds useless, but I get a potential null pointer warning | |
166 | * otherwise | |
167 | */ | |
168 | if (testXmlFile == null) { | |
169 | return; | |
170 | } | |
171 | ||
172 | Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID); | |
173 | ||
66471052 GB |
174 | List<Element> values = XmlUtils.getChildElements(analysis, TmfXmlStrings.LOCATION); |
175 | assertEquals(5, values.size()); | |
176 | ||
177 | Element aLocation = values.get(0); | |
178 | List<Element> attributes = XmlUtils.getChildElements(aLocation, TmfXmlStrings.STATE_ATTRIBUTE); | |
179 | assertEquals(2, attributes.size()); | |
6d20d989 GB |
180 | |
181 | values = XmlUtils.getChildElements(analysis, TmfXmlStrings.HEAD); | |
182 | assertEquals(1, values.size()); | |
183 | ||
184 | Element head = values.get(0); | |
185 | values = XmlUtils.getChildElements(head); | |
186 | assertEquals(2, values.size()); | |
187 | ||
188 | } | |
189 | ||
e11e382c | 190 | } |