Commit | Line | Data |
---|---|---|
e11e382c | 1 | /******************************************************************************* |
900cbf89 | 2 | * Copyright (c) 2014, 2015 École Polytechnique de Montréal and others |
e11e382c FW |
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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.analysis.xml.core.tests.module; |
e11e382c FW |
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; |
cc4dbd9e | 28 | import org.eclipse.core.runtime.Path; |
6d20d989 | 29 | import org.eclipse.jdt.annotation.NonNull; |
900cbf89 JCK |
30 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
31 | import org.eclipse.tracecompass.statesystem.core.StateSystemUtils; | |
32 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; | |
33 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException; | |
34 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; | |
35 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; | |
2bdf0193 AM |
36 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils; |
37 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; | |
900cbf89 | 38 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.XmlStateSystemModule; |
2bdf0193 AM |
39 | import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.Activator; |
40 | import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles; | |
900cbf89 JCK |
41 | import org.eclipse.tracecompass.tmf.core.event.TmfEvent; |
42 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; | |
43 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
44 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub; | |
e11e382c FW |
45 | import org.junit.After; |
46 | import org.junit.Test; | |
900cbf89 | 47 | import org.w3c.dom.Document; |
6d20d989 | 48 | import org.w3c.dom.Element; |
900cbf89 | 49 | import org.w3c.dom.NodeList; |
e11e382c FW |
50 | |
51 | /** | |
52 | * Tests for the {@link XmlUtils} class | |
53 | * | |
54 | * @author Geneviève Bastien | |
55 | */ | |
56 | public class XmlUtilsTest { | |
57 | ||
e2ea8484 GB |
58 | private static final Path PATH_INVALID = new Path("test_xml_files/test_invalid"); |
59 | private static final Path PATH_VALID = new Path("test_xml_files/test_valid"); | |
60 | ||
e11e382c FW |
61 | /** |
62 | * Empty the XML directory after the test | |
63 | */ | |
64 | @After | |
65 | public void emptyXmlFolder() { | |
66 | File fFolder = XmlUtils.getXmlFilesPath().toFile(); | |
67 | if (!(fFolder.isDirectory() && fFolder.exists())) { | |
68 | return; | |
69 | } | |
70 | for (File xmlFile : fFolder.listFiles()) { | |
71 | xmlFile.delete(); | |
72 | } | |
73 | } | |
74 | ||
75 | /** | |
76 | * Test the {@link XmlUtils#getXmlFilesPath()} method | |
77 | */ | |
78 | @Test | |
79 | public void testXmlPath() { | |
80 | IPath xmlPath = XmlUtils.getXmlFilesPath(); | |
81 | ||
82 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); | |
83 | IPath workspacePath = workspace.getRoot().getRawLocation(); | |
84 | workspacePath = workspacePath.addTrailingSeparator() | |
85 | .append(".metadata").addTrailingSeparator().append(".plugins") | |
86 | .addTrailingSeparator() | |
c77a695a | 87 | .append("org.eclipse.tracecompass.tmf.analysis.xml.core") |
e11e382c FW |
88 | .addTrailingSeparator().append("xml_files"); |
89 | ||
90 | assertEquals(xmlPath, workspacePath); | |
91 | } | |
92 | ||
93 | /** | |
94 | * test the {@link XmlUtils#xmlValidate(File)} method | |
95 | */ | |
96 | @Test | |
97 | public void testXmlValidate() { | |
98 | File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile(); | |
99 | if ((testXmlFile == null) || !testXmlFile.exists()) { | |
100 | fail("XML test file does not exist"); | |
101 | } | |
8945f67f GB |
102 | IStatus status = XmlUtils.xmlValidate(testXmlFile); |
103 | if (!status.isOK()) { | |
104 | fail(status.getMessage()); | |
105 | } | |
e11e382c FW |
106 | |
107 | testXmlFile = TmfXmlTestFiles.INVALID_FILE.getFile(); | |
108 | if ((testXmlFile == null) || !testXmlFile.exists()) { | |
109 | fail("XML test file does not exist"); | |
110 | } | |
111 | assertFalse(XmlUtils.xmlValidate(testXmlFile).isOK()); | |
6b5218d0 | 112 | |
e11e382c FW |
113 | } |
114 | ||
6b5218d0 FW |
115 | /** |
116 | * Test various invalid files and make sure they are invalid | |
117 | */ | |
118 | @Test | |
119 | public void testXmlValidateInvalid() { | |
9a0aa59e | 120 | IPath path = Activator.getAbsolutePath(PATH_INVALID); |
e2ea8484 GB |
121 | File file = path.toFile(); |
122 | ||
123 | File[] invalidFiles = file.listFiles(); | |
124 | assertTrue(invalidFiles.length > 0); | |
125 | for (File f : invalidFiles) { | |
126 | assertFalse("File " + f.getName(), XmlUtils.xmlValidate(f).isOK()); | |
cc4dbd9e | 127 | } |
e2ea8484 GB |
128 | } |
129 | ||
130 | /** | |
131 | * Test various valid files and make sure they are valid | |
132 | */ | |
133 | @Test | |
134 | public void testXmlValidateValid() { | |
9a0aa59e | 135 | IPath path = Activator.getAbsolutePath(PATH_VALID); |
e2ea8484 | 136 | File file = path.toFile(); |
cc4dbd9e MAL |
137 | |
138 | File[] validFiles = file.listFiles(); | |
e2ea8484 | 139 | assertTrue(validFiles.length > 0); |
6b5218d0 | 140 | for (File f : validFiles) { |
e2ea8484 | 141 | assertTrue("File " + f.getName(), XmlUtils.xmlValidate(f).isOK()); |
6b5218d0 FW |
142 | } |
143 | } | |
144 | ||
e11e382c FW |
145 | /** |
146 | * test the {@link XmlUtils#addXmlFile(File)} method | |
147 | */ | |
148 | @Test | |
149 | public void testXmlAddFile() { | |
150 | /* Check the file does not exist */ | |
151 | IPath xmlPath = XmlUtils.getXmlFilesPath().addTrailingSeparator().append("test_valid.xml"); | |
152 | File destFile = xmlPath.toFile(); | |
153 | assertFalse(destFile.exists()); | |
154 | ||
155 | /* Add test_valid.xml file */ | |
156 | File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile(); | |
157 | if ((testXmlFile == null) || !testXmlFile.exists()) { | |
158 | fail("XML test file does not exist"); | |
159 | } | |
160 | ||
161 | XmlUtils.addXmlFile(testXmlFile); | |
162 | assertTrue(destFile.exists()); | |
163 | } | |
164 | ||
e2ea8484 | 165 | private static final @NonNull String ANALYSIS_ID = "kernel.linux.sp"; |
6d20d989 GB |
166 | |
167 | /** | |
168 | * Test the {@link XmlUtils#getElementInFile(String, String, String)} method | |
169 | */ | |
170 | @Test | |
171 | public void testGetElementInFile() { | |
172 | File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile(); | |
173 | if ((testXmlFile == null) || !testXmlFile.exists()) { | |
174 | fail("XML test file does not exist"); | |
175 | } | |
176 | /* | |
177 | * This sounds useless, but I get a potential null pointer warning | |
178 | * otherwise | |
179 | */ | |
180 | if (testXmlFile == null) { | |
181 | return; | |
182 | } | |
183 | ||
184 | Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID); | |
185 | assertNotNull(analysis); | |
186 | } | |
187 | ||
188 | /** | |
189 | * Test the {@link XmlUtils#getChildElements(Element)} and | |
190 | * {@link XmlUtils#getChildElements(Element, String)} methods | |
191 | */ | |
192 | @Test | |
193 | public void testGetChildElements() { | |
194 | File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile(); | |
195 | if ((testXmlFile == null) || !testXmlFile.exists()) { | |
196 | fail("XML test file does not exist"); | |
197 | } | |
198 | /* | |
199 | * This sounds useless, but I get a potential null pointer warning | |
200 | * otherwise | |
201 | */ | |
202 | if (testXmlFile == null) { | |
203 | return; | |
204 | } | |
205 | ||
206 | Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID); | |
207 | ||
66471052 GB |
208 | List<Element> values = XmlUtils.getChildElements(analysis, TmfXmlStrings.LOCATION); |
209 | assertEquals(5, values.size()); | |
210 | ||
211 | Element aLocation = values.get(0); | |
212 | List<Element> attributes = XmlUtils.getChildElements(aLocation, TmfXmlStrings.STATE_ATTRIBUTE); | |
213 | assertEquals(2, attributes.size()); | |
6d20d989 GB |
214 | |
215 | values = XmlUtils.getChildElements(analysis, TmfXmlStrings.HEAD); | |
216 | assertEquals(1, values.size()); | |
217 | ||
218 | Element head = values.get(0); | |
219 | values = XmlUtils.getChildElements(head); | |
220 | assertEquals(2, values.size()); | |
221 | ||
222 | } | |
223 | ||
900cbf89 JCK |
224 | /** |
225 | * Initialize a new trace based using the input file path | |
226 | * | |
227 | * @param traceFile | |
228 | * The trace file | |
229 | * @return The trace | |
230 | */ | |
231 | public static @NonNull ITmfTrace initializeTrace(String traceFile) { | |
232 | /* Initialize the trace */ | |
233 | TmfXmlTraceStub trace = new TmfXmlTraceStub(); | |
234 | try { | |
235 | trace.initTrace(null, Activator.getAbsolutePath(new Path(traceFile)).toOSString(), TmfEvent.class); | |
236 | } catch (TmfTraceException e1) { | |
237 | fail(e1.getMessage()); | |
238 | } | |
239 | return trace; | |
240 | } | |
241 | ||
242 | /** | |
243 | * Initialize a new module using the xml file | |
244 | * | |
245 | * @param xmlAnalysisFile | |
246 | * The xml file used to initialize the module | |
247 | * @return The module | |
248 | */ | |
249 | public static @NonNull XmlStateSystemModule initializeModule(TmfXmlTestFiles xmlAnalysisFile) { | |
250 | ||
251 | /* Initialize the state provider module */ | |
252 | Document doc = xmlAnalysisFile.getXmlDocument(); | |
253 | assertNotNull(doc); | |
254 | ||
255 | /* get State Providers modules */ | |
256 | NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER); | |
257 | assertFalse(stateproviderNodes.getLength() == 0); | |
258 | ||
259 | Element node = (Element) stateproviderNodes.item(0); | |
260 | XmlStateSystemModule module = new XmlStateSystemModule(); | |
261 | String moduleId = node.getAttribute(TmfXmlStrings.ID); | |
262 | assertNotNull(moduleId); | |
263 | module.setId(moduleId); | |
264 | ||
265 | module.setXmlFile(xmlAnalysisFile.getPath()); | |
266 | ||
267 | return module; | |
268 | } | |
269 | ||
270 | /** | |
271 | * This function test the data provided by the state intervals queried | |
272 | * | |
273 | * @param testId | |
274 | * The id of the test | |
275 | * @param ss | |
276 | * The state system associated to this test | |
277 | * @param quark | |
278 | * The quark we want to query | |
279 | * @param expectedStarts | |
280 | * The expected start timestamps for the intervals generated for | |
281 | * this quark | |
282 | * @param expectedValues | |
283 | * The expected content values for this quark | |
284 | * @throws AttributeNotFoundException | |
285 | * If the quark we want to query is invalid | |
286 | * @throws StateSystemDisposedException | |
287 | * If the state system has been disposed before the end of the | |
288 | * queries | |
289 | */ | |
290 | public static void verifyStateIntervals(String testId, @NonNull ITmfStateSystem ss, Integer quark, int[] expectedStarts, ITmfStateValue[] expectedValues) throws AttributeNotFoundException, StateSystemDisposedException { | |
291 | int expectedCount = expectedStarts.length - 1; | |
292 | List<ITmfStateInterval> intervals = StateSystemUtils.queryHistoryRange(ss, quark, expectedStarts[0], expectedStarts[expectedCount]); | |
293 | assertEquals(testId + ": Interval count", expectedCount, intervals.size()); | |
294 | for (int i = 0; i < expectedCount; i++) { | |
295 | ITmfStateInterval interval = intervals.get(i); | |
296 | assertEquals(testId + ": Start time of interval " + i, expectedStarts[i], interval.getStartTime()); | |
297 | long actualEnd = (i == expectedCount - 1) ? (expectedStarts[i + 1]) : (expectedStarts[i + 1]) - 1; | |
298 | assertEquals(testId + ": End time of interval " + i, actualEnd, interval.getEndTime()); | |
299 | assertEquals(testId + ": Expected value of interval " + i, expectedValues[i], interval.getStateValue()); | |
300 | } | |
301 | } | |
302 | ||
e11e382c | 303 | } |