| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014, 2015 École Polytechnique de Montréal and others |
| 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.tracecompass.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.assertNotNull; |
| 18 | import static org.junit.Assert.assertTrue; |
| 19 | import static org.junit.Assert.fail; |
| 20 | |
| 21 | import java.io.File; |
| 22 | import java.util.Iterator; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import org.eclipse.core.resources.IWorkspace; |
| 27 | import org.eclipse.core.resources.ResourcesPlugin; |
| 28 | import org.eclipse.core.runtime.IPath; |
| 29 | import org.eclipse.core.runtime.IStatus; |
| 30 | import org.eclipse.core.runtime.Path; |
| 31 | import org.eclipse.jdt.annotation.NonNull; |
| 32 | import org.eclipse.jdt.annotation.Nullable; |
| 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; |
| 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; |
| 43 | import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.Activator; |
| 44 | import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles; |
| 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; |
| 49 | import org.junit.After; |
| 50 | import org.junit.Test; |
| 51 | import org.w3c.dom.Document; |
| 52 | import org.w3c.dom.Element; |
| 53 | import org.w3c.dom.NodeList; |
| 54 | |
| 55 | /** |
| 56 | * Tests for the {@link XmlUtils} class |
| 57 | * |
| 58 | * @author Geneviève Bastien |
| 59 | */ |
| 60 | public class XmlUtilsTest { |
| 61 | |
| 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 | |
| 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() |
| 91 | .append("org.eclipse.tracecompass.tmf.analysis.xml.core") |
| 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 | } |
| 106 | IStatus status = XmlUtils.xmlValidate(testXmlFile); |
| 107 | if (!status.isOK()) { |
| 108 | fail(status.getMessage()); |
| 109 | } |
| 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 | |
| 118 | /** |
| 119 | * Test various invalid files and make sure they are invalid |
| 120 | */ |
| 121 | @Test |
| 122 | public void testXmlValidateInvalid() { |
| 123 | IPath path = Activator.getAbsolutePath(PATH_INVALID); |
| 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()); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Test various valid files and make sure they are valid |
| 135 | */ |
| 136 | @Test |
| 137 | public void testXmlValidateValid() { |
| 138 | IPath path = Activator.getAbsolutePath(PATH_VALID); |
| 139 | File file = path.toFile(); |
| 140 | |
| 141 | File[] validFiles = file.listFiles(); |
| 142 | assertTrue(validFiles.length > 0); |
| 143 | for (File f : validFiles) { |
| 144 | assertTrue("File " + f.getName(), XmlUtils.xmlValidate(f).isOK()); |
| 145 | } |
| 146 | } |
| 147 | |
| 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 | |
| 168 | private static final @NonNull String ANALYSIS_ID = "kernel.linux.sp"; |
| 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(); |
| 176 | assertNotNull("XML test file does not exist", testXmlFile); |
| 177 | assertTrue("XML test file does not exist", testXmlFile.exists()); |
| 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 | |
| 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()); |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 358 | } |