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
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
36 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
38 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
39 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.segment
.TmfXmlPatternSegment
;
40 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
41 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.XmlStateSystemModule
;
42 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.tests
.Activator
;
43 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.tests
.common
.TmfXmlTestFiles
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
47 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.TmfXmlTraceStub
;
48 import org
.junit
.After
;
49 import org
.junit
.Test
;
50 import org
.w3c
.dom
.Document
;
51 import org
.w3c
.dom
.Element
;
52 import org
.w3c
.dom
.NodeList
;
55 * Tests for the {@link XmlUtils} class
57 * @author Geneviève Bastien
59 public class XmlUtilsTest
{
61 private static final Path PATH_INVALID
= new Path("test_xml_files/test_invalid");
62 private static final Path PATH_VALID
= new Path("test_xml_files/test_valid");
65 * Empty the XML directory after the test
68 public void emptyXmlFolder() {
69 File fFolder
= XmlUtils
.getXmlFilesPath().toFile();
70 if (!(fFolder
.isDirectory() && fFolder
.exists())) {
73 for (File xmlFile
: fFolder
.listFiles()) {
79 * Test the {@link XmlUtils#getXmlFilesPath()} method
82 public void testXmlPath() {
83 IPath xmlPath
= XmlUtils
.getXmlFilesPath();
85 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
86 IPath workspacePath
= workspace
.getRoot().getRawLocation();
87 workspacePath
= workspacePath
.addTrailingSeparator()
88 .append(".metadata").addTrailingSeparator().append(".plugins")
89 .addTrailingSeparator()
90 .append("org.eclipse.tracecompass.tmf.analysis.xml.core")
91 .addTrailingSeparator().append("xml_files");
93 assertEquals(xmlPath
, workspacePath
);
97 * test the {@link XmlUtils#xmlValidate(File)} method
100 public void testXmlValidate() {
101 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
102 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
103 fail("XML test file does not exist");
105 IStatus status
= XmlUtils
.xmlValidate(testXmlFile
);
106 if (!status
.isOK()) {
107 fail(status
.getMessage());
110 testXmlFile
= TmfXmlTestFiles
.INVALID_FILE
.getFile();
111 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
112 fail("XML test file does not exist");
114 assertFalse(XmlUtils
.xmlValidate(testXmlFile
).isOK());
118 * Test various invalid files and make sure they are invalid
121 public void testXmlValidateInvalid() {
122 IPath path
= Activator
.getAbsolutePath(PATH_INVALID
);
123 File file
= path
.toFile();
125 File
[] invalidFiles
= file
.listFiles();
126 assertTrue(invalidFiles
.length
> 0);
127 for (File f
: invalidFiles
) {
128 assertFalse("File " + f
.getName(), XmlUtils
.xmlValidate(f
).isOK());
133 * Test various valid files and make sure they are valid
136 public void testXmlValidateValid() {
137 IPath path
= Activator
.getAbsolutePath(PATH_VALID
);
138 File file
= path
.toFile();
140 File
[] validFiles
= file
.listFiles();
141 assertTrue(validFiles
.length
> 0);
142 for (File f
: validFiles
) {
143 assertTrue("File " + f
.getName(), XmlUtils
.xmlValidate(f
).isOK());
148 * test the {@link XmlUtils#addXmlFile(File)} method
151 public void testXmlAddFile() {
152 /* Check the file does not exist */
153 IPath xmlPath
= XmlUtils
.getXmlFilesPath().addTrailingSeparator().append("test_valid.xml");
154 File destFile
= xmlPath
.toFile();
155 assertFalse(destFile
.exists());
157 /* Add test_valid.xml file */
158 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
159 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
160 fail("XML test file does not exist");
163 XmlUtils
.addXmlFile(testXmlFile
);
164 assertTrue(destFile
.exists());
167 private static final @NonNull String ANALYSIS_ID
= "kernel.linux.sp";
170 * Test the {@link XmlUtils#getElementInFile(String, String, String)} method
173 public void testGetElementInFile() {
174 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
175 assertNotNull("XML test file does not exist", testXmlFile
);
176 assertTrue("XML test file does not exist", testXmlFile
.exists());
177 Element analysis
= XmlUtils
.getElementInFile(testXmlFile
.getAbsolutePath(), TmfXmlStrings
.STATE_PROVIDER
, ANALYSIS_ID
);
178 assertNotNull(analysis
);
182 * Test the {@link XmlUtils#getChildElements(Element)} and
183 * {@link XmlUtils#getChildElements(Element, String)} methods
186 public void testGetChildElements() {
187 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
188 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
189 fail("XML test file does not exist");
192 * This sounds useless, but I get a potential null pointer warning
195 if (testXmlFile
== null) {
199 Element analysis
= XmlUtils
.getElementInFile(testXmlFile
.getAbsolutePath(), TmfXmlStrings
.STATE_PROVIDER
, ANALYSIS_ID
);
201 List
<Element
> values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.LOCATION
);
202 assertEquals(5, values
.size());
204 Element aLocation
= values
.get(0);
205 List
<Element
> attributes
= XmlUtils
.getChildElements(aLocation
, TmfXmlStrings
.STATE_ATTRIBUTE
);
206 assertEquals(2, attributes
.size());
208 values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.HEAD
);
209 assertEquals(1, values
.size());
211 Element head
= values
.get(0);
212 values
= XmlUtils
.getChildElements(head
);
213 assertEquals(2, values
.size());
218 * Initialize a new trace based using the input file path
224 public static @NonNull ITmfTrace
initializeTrace(String traceFile
) {
225 /* Initialize the trace */
226 TmfXmlTraceStub trace
= new TmfXmlTraceStub();
228 trace
.initTrace(null, Activator
.getAbsolutePath(new Path(traceFile
)).toOSString(), TmfEvent
.class);
229 } catch (TmfTraceException e1
) {
230 fail(e1
.getMessage());
236 * Initialize a new module using the xml file
238 * @param xmlAnalysisFile
239 * The xml file used to initialize the module
242 public static @NonNull XmlStateSystemModule
initializeModule(TmfXmlTestFiles xmlAnalysisFile
) {
244 /* Initialize the state provider module */
245 Document doc
= xmlAnalysisFile
.getXmlDocument();
248 /* get State Providers modules */
249 NodeList stateproviderNodes
= doc
.getElementsByTagName(TmfXmlStrings
.STATE_PROVIDER
);
250 assertFalse(stateproviderNodes
.getLength() == 0);
252 Element node
= (Element
) stateproviderNodes
.item(0);
253 XmlStateSystemModule module
= new XmlStateSystemModule();
254 String moduleId
= node
.getAttribute(TmfXmlStrings
.ID
);
255 assertNotNull(moduleId
);
256 module
.setId(moduleId
);
258 module
.setXmlFile(xmlAnalysisFile
.getPath());
264 * This function test the data provided by the state intervals queried
269 * The state system associated to this test
271 * The quark we want to query
272 * @param expectedStarts
273 * The expected start timestamps for the intervals generated for
275 * @param expectedValues
276 * The expected content values for this quark
277 * @throws AttributeNotFoundException
278 * If the quark we want to query is invalid
279 * @throws StateSystemDisposedException
280 * If the state system has been disposed before the end of the
283 public static void verifyStateIntervals(String testId
, @NonNull ITmfStateSystem ss
, Integer quark
, int[] expectedStarts
, ITmfStateValue
[] expectedValues
) throws AttributeNotFoundException
, StateSystemDisposedException
{
284 int expectedCount
= expectedStarts
.length
- 1;
285 List
<ITmfStateInterval
> intervals
= StateSystemUtils
.queryHistoryRange(ss
, quark
, expectedStarts
[0], expectedStarts
[expectedCount
]);
286 assertEquals(testId
+ ": Interval count", expectedCount
, intervals
.size());
287 for (int i
= 0; i
< expectedCount
; i
++) {
288 ITmfStateInterval interval
= intervals
.get(i
);
289 assertEquals(testId
+ ": Start time of interval " + i
, expectedStarts
[i
], interval
.getStartTime());
290 long actualEnd
= (i
== expectedCount
- 1) ?
(expectedStarts
[i
+ 1]) : (expectedStarts
[i
+ 1]) - 1;
291 assertEquals(testId
+ ": End time of interval " + i
, actualEnd
, interval
.getEndTime());
292 assertEquals(testId
+ ": Expected value of interval " + i
, expectedValues
[i
], interval
.getStateValue());
297 * Test a pattern segment against what is expected
300 * The expected pattern segment
302 * The actual pattern segment
304 public static void testPatternSegmentData(TmfXmlPatternSegment expected
, TmfXmlPatternSegment actual
) {
305 assertEquals("getStart", expected
.getStart(), actual
.getStart());
306 assertEquals("getEnd", expected
.getEnd(), actual
.getEnd());
307 assertEquals("getScale", expected
.getScale(), actual
.getScale());
308 assertEquals("getName", expected
.getName(), actual
.getName());
309 assertNotNull("getContent", actual
.getContent());
311 // Test the content of the pattern segment
312 assertEquals("content size", expected
.getContent().size(), actual
.getContent().size());
313 Iterator
<Map
.Entry
<String
, @NonNull ITmfStateValue
>> it2
= expected
.getContent().entrySet().iterator();
314 for (int i
= 0; i
< expected
.getContent().size(); i
++) {
315 Map
.Entry
<String
, @NonNull ITmfStateValue
> expectedContent
= it2
.next();
316 ITmfStateValue actualValue
= actual
.getContent().get(expectedContent
.getKey());
317 assertNotNull("Content " + expectedContent
.getKey() + " exists", actualValue
);
318 assertEquals("Content value comparison " + i
, 0, expectedContent
.getValue().compareTo(actualValue
));