1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal and others
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
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.tests
.module
;
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
;
22 import java
.util
.Iterator
;
23 import java
.util
.List
;
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
;
56 * Tests for the {@link XmlUtils} class
58 * @author Geneviève Bastien
60 public class XmlUtilsTest
{
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");
66 * Empty the XML directory after the test
69 public void emptyXmlFolder() {
70 File fFolder
= XmlUtils
.getXmlFilesPath().toFile();
71 if (!(fFolder
.isDirectory() && fFolder
.exists())) {
74 for (File xmlFile
: fFolder
.listFiles()) {
80 * Test the {@link XmlUtils#getXmlFilesPath()} method
83 public void testXmlPath() {
84 IPath xmlPath
= XmlUtils
.getXmlFilesPath();
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");
94 assertEquals(xmlPath
, workspacePath
);
98 * test the {@link XmlUtils#xmlValidate(File)} method
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");
106 IStatus status
= XmlUtils
.xmlValidate(testXmlFile
);
107 if (!status
.isOK()) {
108 fail(status
.getMessage());
111 testXmlFile
= TmfXmlTestFiles
.INVALID_FILE
.getFile();
112 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
113 fail("XML test file does not exist");
115 assertFalse(XmlUtils
.xmlValidate(testXmlFile
).isOK());
119 * Test various invalid files and make sure they are invalid
122 public void testXmlValidateInvalid() {
123 IPath path
= Activator
.getAbsolutePath(PATH_INVALID
);
124 File file
= path
.toFile();
126 File
[] invalidFiles
= file
.listFiles();
127 assertTrue(invalidFiles
.length
> 0);
128 for (File f
: invalidFiles
) {
129 assertFalse("File " + f
.getName(), XmlUtils
.xmlValidate(f
).isOK());
134 * Test various valid files and make sure they are valid
137 public void testXmlValidateValid() {
138 IPath path
= Activator
.getAbsolutePath(PATH_VALID
);
139 File file
= path
.toFile();
141 File
[] validFiles
= file
.listFiles();
142 assertTrue(validFiles
.length
> 0);
143 for (File f
: validFiles
) {
144 assertTrue("File " + f
.getName(), XmlUtils
.xmlValidate(f
).isOK());
149 * test the {@link XmlUtils#addXmlFile(File)} method
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());
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");
164 XmlUtils
.addXmlFile(testXmlFile
);
165 assertTrue(destFile
.exists());
168 private static final @NonNull String ANALYSIS_ID
= "kernel.linux.sp";
171 * Test the {@link XmlUtils#getElementInFile(String, String, String)} method
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
);
183 * Test the {@link XmlUtils#getChildElements(Element)} and
184 * {@link XmlUtils#getChildElements(Element, String)} methods
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");
193 * This sounds useless, but I get a potential null pointer warning
196 if (testXmlFile
== null) {
200 Element analysis
= XmlUtils
.getElementInFile(testXmlFile
.getAbsolutePath(), TmfXmlStrings
.STATE_PROVIDER
, ANALYSIS_ID
);
202 List
<Element
> values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.LOCATION
);
203 assertEquals(5, values
.size());
205 Element aLocation
= values
.get(0);
206 List
<Element
> attributes
= XmlUtils
.getChildElements(aLocation
, TmfXmlStrings
.STATE_ATTRIBUTE
);
207 assertEquals(2, attributes
.size());
209 values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.HEAD
);
210 assertEquals(1, values
.size());
212 Element head
= values
.get(0);
213 values
= XmlUtils
.getChildElements(head
);
214 assertEquals(2, values
.size());
219 * Initialize a new trace based using the input file path
225 public static @NonNull ITmfTrace
initializeTrace(String traceFile
) {
226 /* Initialize the trace */
227 TmfXmlTraceStub trace
= new TmfXmlTraceStub();
229 trace
.initTrace(null, Activator
.getAbsolutePath(new Path(traceFile
)).toOSString(), TmfEvent
.class);
230 } catch (TmfTraceException e1
) {
231 fail(e1
.getMessage());
237 * Initialize a new module using the xml file
239 * @param xmlAnalysisFile
240 * The xml file used to initialize the module
243 public static @NonNull XmlStateSystemModule
initializeModule(TmfXmlTestFiles xmlAnalysisFile
) {
245 /* Initialize the state provider module */
246 Document doc
= xmlAnalysisFile
.getXmlDocument();
249 /* get State Providers modules */
250 NodeList stateproviderNodes
= doc
.getElementsByTagName(TmfXmlStrings
.STATE_PROVIDER
);
251 assertFalse(stateproviderNodes
.getLength() == 0);
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
);
259 module
.setXmlFile(xmlAnalysisFile
.getPath());
265 * This function test the data provided by the state intervals queried
270 * The state system associated to this test
272 * The quark we want to query
273 * @param expectedStarts
274 * The expected start timestamps for the intervals generated for
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
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());
298 * This function test the data provided by the state intervals queried on a stack
303 * The state system associated to this test
305 * The quark we want to query
306 * @param expectedStarts
307 * The expected start timestamps for the intervals generated for
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
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());
333 * Test a pattern segment against what is expected
336 * The expected pattern segment
338 * The actual pattern segment
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());
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
));