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