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
.List
;
24 import org
.eclipse
.core
.resources
.IWorkspace
;
25 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
26 import org
.eclipse
.core
.runtime
.IPath
;
27 import org
.eclipse
.core
.runtime
.IStatus
;
28 import org
.eclipse
.core
.runtime
.Path
;
29 import org
.eclipse
.jdt
.annotation
.NonNull
;
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
;
36 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
37 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
38 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.XmlStateSystemModule
;
39 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.tests
.Activator
;
40 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.tests
.common
.TmfXmlTestFiles
;
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
;
45 import org
.junit
.After
;
46 import org
.junit
.Test
;
47 import org
.w3c
.dom
.Document
;
48 import org
.w3c
.dom
.Element
;
49 import org
.w3c
.dom
.NodeList
;
52 * Tests for the {@link XmlUtils} class
54 * @author Geneviève Bastien
56 public class XmlUtilsTest
{
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");
62 * Empty the XML directory after the test
65 public void emptyXmlFolder() {
66 File fFolder
= XmlUtils
.getXmlFilesPath().toFile();
67 if (!(fFolder
.isDirectory() && fFolder
.exists())) {
70 for (File xmlFile
: fFolder
.listFiles()) {
76 * Test the {@link XmlUtils#getXmlFilesPath()} method
79 public void testXmlPath() {
80 IPath xmlPath
= XmlUtils
.getXmlFilesPath();
82 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
83 IPath workspacePath
= workspace
.getRoot().getRawLocation();
84 workspacePath
= workspacePath
.addTrailingSeparator()
85 .append(".metadata").addTrailingSeparator().append(".plugins")
86 .addTrailingSeparator()
87 .append("org.eclipse.tracecompass.tmf.analysis.xml.core")
88 .addTrailingSeparator().append("xml_files");
90 assertEquals(xmlPath
, workspacePath
);
94 * test the {@link XmlUtils#xmlValidate(File)} method
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");
102 IStatus status
= XmlUtils
.xmlValidate(testXmlFile
);
103 if (!status
.isOK()) {
104 fail(status
.getMessage());
107 testXmlFile
= TmfXmlTestFiles
.INVALID_FILE
.getFile();
108 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
109 fail("XML test file does not exist");
111 assertFalse(XmlUtils
.xmlValidate(testXmlFile
).isOK());
115 * Test various invalid files and make sure they are invalid
118 public void testXmlValidateInvalid() {
119 IPath path
= Activator
.getAbsolutePath(PATH_INVALID
);
120 File file
= path
.toFile();
122 File
[] invalidFiles
= file
.listFiles();
123 assertTrue(invalidFiles
.length
> 0);
124 for (File f
: invalidFiles
) {
125 assertFalse("File " + f
.getName(), XmlUtils
.xmlValidate(f
).isOK());
130 * Test various valid files and make sure they are valid
133 public void testXmlValidateValid() {
134 IPath path
= Activator
.getAbsolutePath(PATH_VALID
);
135 File file
= path
.toFile();
137 File
[] validFiles
= file
.listFiles();
138 assertTrue(validFiles
.length
> 0);
139 for (File f
: validFiles
) {
140 assertTrue("File " + f
.getName(), XmlUtils
.xmlValidate(f
).isOK());
145 * test the {@link XmlUtils#addXmlFile(File)} method
148 public void testXmlAddFile() {
149 /* Check the file does not exist */
150 IPath xmlPath
= XmlUtils
.getXmlFilesPath().addTrailingSeparator().append("test_valid.xml");
151 File destFile
= xmlPath
.toFile();
152 assertFalse(destFile
.exists());
154 /* Add test_valid.xml file */
155 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
156 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
157 fail("XML test file does not exist");
160 XmlUtils
.addXmlFile(testXmlFile
);
161 assertTrue(destFile
.exists());
164 private static final @NonNull String ANALYSIS_ID
= "kernel.linux.sp";
167 * Test the {@link XmlUtils#getElementInFile(String, String, String)} method
170 public void testGetElementInFile() {
171 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
172 assertNotNull("XML test file does not exist", testXmlFile
);
173 assertTrue("XML test file does not exist", testXmlFile
.exists());
174 Element analysis
= XmlUtils
.getElementInFile(testXmlFile
.getAbsolutePath(), TmfXmlStrings
.STATE_PROVIDER
, ANALYSIS_ID
);
175 assertNotNull(analysis
);
179 * Test the {@link XmlUtils#getChildElements(Element)} and
180 * {@link XmlUtils#getChildElements(Element, String)} methods
183 public void testGetChildElements() {
184 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
185 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
186 fail("XML test file does not exist");
189 * This sounds useless, but I get a potential null pointer warning
192 if (testXmlFile
== null) {
196 Element analysis
= XmlUtils
.getElementInFile(testXmlFile
.getAbsolutePath(), TmfXmlStrings
.STATE_PROVIDER
, ANALYSIS_ID
);
198 List
<Element
> values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.LOCATION
);
199 assertEquals(5, values
.size());
201 Element aLocation
= values
.get(0);
202 List
<Element
> attributes
= XmlUtils
.getChildElements(aLocation
, TmfXmlStrings
.STATE_ATTRIBUTE
);
203 assertEquals(2, attributes
.size());
205 values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.HEAD
);
206 assertEquals(1, values
.size());
208 Element head
= values
.get(0);
209 values
= XmlUtils
.getChildElements(head
);
210 assertEquals(2, values
.size());
215 * Initialize a new trace based using the input file path
221 public static @NonNull ITmfTrace
initializeTrace(String traceFile
) {
222 /* Initialize the trace */
223 TmfXmlTraceStub trace
= new TmfXmlTraceStub();
225 trace
.initTrace(null, Activator
.getAbsolutePath(new Path(traceFile
)).toOSString(), TmfEvent
.class);
226 } catch (TmfTraceException e1
) {
227 fail(e1
.getMessage());
233 * Initialize a new module using the xml file
235 * @param xmlAnalysisFile
236 * The xml file used to initialize the module
239 public static @NonNull XmlStateSystemModule
initializeModule(TmfXmlTestFiles xmlAnalysisFile
) {
241 /* Initialize the state provider module */
242 Document doc
= xmlAnalysisFile
.getXmlDocument();
245 /* get State Providers modules */
246 NodeList stateproviderNodes
= doc
.getElementsByTagName(TmfXmlStrings
.STATE_PROVIDER
);
247 assertFalse(stateproviderNodes
.getLength() == 0);
249 Element node
= (Element
) stateproviderNodes
.item(0);
250 XmlStateSystemModule module
= new XmlStateSystemModule();
251 String moduleId
= node
.getAttribute(TmfXmlStrings
.ID
);
252 assertNotNull(moduleId
);
253 module
.setId(moduleId
);
255 module
.setXmlFile(xmlAnalysisFile
.getPath());
261 * This function test the data provided by the state intervals queried
266 * The state system associated to this test
268 * The quark we want to query
269 * @param expectedStarts
270 * The expected start timestamps for the intervals generated for
272 * @param expectedValues
273 * The expected content values for this quark
274 * @throws AttributeNotFoundException
275 * If the quark we want to query is invalid
276 * @throws StateSystemDisposedException
277 * If the state system has been disposed before the end of the
280 public static void verifyStateIntervals(String testId
, @NonNull ITmfStateSystem ss
, Integer quark
, int[] expectedStarts
, ITmfStateValue
[] expectedValues
) throws AttributeNotFoundException
, StateSystemDisposedException
{
281 int expectedCount
= expectedStarts
.length
- 1;
282 List
<ITmfStateInterval
> intervals
= StateSystemUtils
.queryHistoryRange(ss
, quark
, expectedStarts
[0], expectedStarts
[expectedCount
]);
283 assertEquals(testId
+ ": Interval count", expectedCount
, intervals
.size());
284 for (int i
= 0; i
< expectedCount
; i
++) {
285 ITmfStateInterval interval
= intervals
.get(i
);
286 assertEquals(testId
+ ": Start time of interval " + i
, expectedStarts
[i
], interval
.getStartTime());
287 long actualEnd
= (i
== expectedCount
- 1) ?
(expectedStarts
[i
+ 1]) : (expectedStarts
[i
+ 1]) - 1;
288 assertEquals(testId
+ ": End time of interval " + i
, actualEnd
, interval
.getEndTime());
289 assertEquals(testId
+ ": Expected value of interval " + i
, expectedValues
[i
], interval
.getStateValue());