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());
116 * Test various invalid files and make sure they are invalid
119 public void testXmlValidateInvalid() {
120 IPath path
= Activator
.getAbsolutePath(PATH_INVALID
);
121 File file
= path
.toFile();
123 File
[] invalidFiles
= file
.listFiles();
124 assertTrue(invalidFiles
.length
> 0);
125 for (File f
: invalidFiles
) {
126 assertFalse("File " + f
.getName(), XmlUtils
.xmlValidate(f
).isOK());
131 * Test various valid files and make sure they are valid
134 public void testXmlValidateValid() {
135 IPath path
= Activator
.getAbsolutePath(PATH_VALID
);
136 File file
= path
.toFile();
138 File
[] validFiles
= file
.listFiles();
139 assertTrue(validFiles
.length
> 0);
140 for (File f
: validFiles
) {
141 assertTrue("File " + f
.getName(), XmlUtils
.xmlValidate(f
).isOK());
146 * test the {@link XmlUtils#addXmlFile(File)} method
149 public void testXmlAddFile() {
150 /* Check the file does not exist */
151 IPath xmlPath
= XmlUtils
.getXmlFilesPath().addTrailingSeparator().append("test_valid.xml");
152 File destFile
= xmlPath
.toFile();
153 assertFalse(destFile
.exists());
155 /* Add test_valid.xml file */
156 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
157 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
158 fail("XML test file does not exist");
161 XmlUtils
.addXmlFile(testXmlFile
);
162 assertTrue(destFile
.exists());
165 private static final @NonNull String ANALYSIS_ID
= "kernel.linux.sp";
168 * Test the {@link XmlUtils#getElementInFile(String, String, String)} method
171 public void testGetElementInFile() {
172 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
173 assertNotNull("XML test file does not exist", testXmlFile
);
174 assertTrue("XML test file does not exist", testXmlFile
.exists());
175 Element analysis
= XmlUtils
.getElementInFile(testXmlFile
.getAbsolutePath(), TmfXmlStrings
.STATE_PROVIDER
, ANALYSIS_ID
);
176 assertNotNull(analysis
);
180 * Test the {@link XmlUtils#getChildElements(Element)} and
181 * {@link XmlUtils#getChildElements(Element, String)} methods
184 public void testGetChildElements() {
185 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
186 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
187 fail("XML test file does not exist");
190 * This sounds useless, but I get a potential null pointer warning
193 if (testXmlFile
== null) {
197 Element analysis
= XmlUtils
.getElementInFile(testXmlFile
.getAbsolutePath(), TmfXmlStrings
.STATE_PROVIDER
, ANALYSIS_ID
);
199 List
<Element
> values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.LOCATION
);
200 assertEquals(5, values
.size());
202 Element aLocation
= values
.get(0);
203 List
<Element
> attributes
= XmlUtils
.getChildElements(aLocation
, TmfXmlStrings
.STATE_ATTRIBUTE
);
204 assertEquals(2, attributes
.size());
206 values
= XmlUtils
.getChildElements(analysis
, TmfXmlStrings
.HEAD
);
207 assertEquals(1, values
.size());
209 Element head
= values
.get(0);
210 values
= XmlUtils
.getChildElements(head
);
211 assertEquals(2, values
.size());
216 * Initialize a new trace based using the input file path
222 public static @NonNull ITmfTrace
initializeTrace(String traceFile
) {
223 /* Initialize the trace */
224 TmfXmlTraceStub trace
= new TmfXmlTraceStub();
226 trace
.initTrace(null, Activator
.getAbsolutePath(new Path(traceFile
)).toOSString(), TmfEvent
.class);
227 } catch (TmfTraceException e1
) {
228 fail(e1
.getMessage());
234 * Initialize a new module using the xml file
236 * @param xmlAnalysisFile
237 * The xml file used to initialize the module
240 public static @NonNull XmlStateSystemModule
initializeModule(TmfXmlTestFiles xmlAnalysisFile
) {
242 /* Initialize the state provider module */
243 Document doc
= xmlAnalysisFile
.getXmlDocument();
246 /* get State Providers modules */
247 NodeList stateproviderNodes
= doc
.getElementsByTagName(TmfXmlStrings
.STATE_PROVIDER
);
248 assertFalse(stateproviderNodes
.getLength() == 0);
250 Element node
= (Element
) stateproviderNodes
.item(0);
251 XmlStateSystemModule module
= new XmlStateSystemModule();
252 String moduleId
= node
.getAttribute(TmfXmlStrings
.ID
);
253 assertNotNull(moduleId
);
254 module
.setId(moduleId
);
256 module
.setXmlFile(xmlAnalysisFile
.getPath());
262 * This function test the data provided by the state intervals queried
267 * The state system associated to this test
269 * The quark we want to query
270 * @param expectedStarts
271 * The expected start timestamps for the intervals generated for
273 * @param expectedValues
274 * The expected content values for this quark
275 * @throws AttributeNotFoundException
276 * If the quark we want to query is invalid
277 * @throws StateSystemDisposedException
278 * If the state system has been disposed before the end of the
281 public static void verifyStateIntervals(String testId
, @NonNull ITmfStateSystem ss
, Integer quark
, int[] expectedStarts
, ITmfStateValue
[] expectedValues
) throws AttributeNotFoundException
, StateSystemDisposedException
{
282 int expectedCount
= expectedStarts
.length
- 1;
283 List
<ITmfStateInterval
> intervals
= StateSystemUtils
.queryHistoryRange(ss
, quark
, expectedStarts
[0], expectedStarts
[expectedCount
]);
284 assertEquals(testId
+ ": Interval count", expectedCount
, intervals
.size());
285 for (int i
= 0; i
< expectedCount
; i
++) {
286 ITmfStateInterval interval
= intervals
.get(i
);
287 assertEquals(testId
+ ": Start time of interval " + i
, expectedStarts
[i
], interval
.getStartTime());
288 long actualEnd
= (i
== expectedCount
- 1) ?
(expectedStarts
[i
+ 1]) : (expectedStarts
[i
+ 1]) - 1;
289 assertEquals(testId
+ ": End time of interval " + i
, actualEnd
, interval
.getEndTime());
290 assertEquals(testId
+ ": Expected value of interval " + i
, expectedValues
[i
], interval
.getStateValue());