851674af913c090829ea6a2ceb02aa2799c4f6da
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core.tests / src / org / eclipse / tracecompass / tmf / analysis / xml / core / tests / module / XmlUtilsTest.java
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.List;
23
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;
50
51 /**
52 * Tests for the {@link XmlUtils} class
53 *
54 * @author Geneviève Bastien
55 */
56 public class XmlUtilsTest {
57
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");
60
61 /**
62 * Empty the XML directory after the test
63 */
64 @After
65 public void emptyXmlFolder() {
66 File fFolder = XmlUtils.getXmlFilesPath().toFile();
67 if (!(fFolder.isDirectory() && fFolder.exists())) {
68 return;
69 }
70 for (File xmlFile : fFolder.listFiles()) {
71 xmlFile.delete();
72 }
73 }
74
75 /**
76 * Test the {@link XmlUtils#getXmlFilesPath()} method
77 */
78 @Test
79 public void testXmlPath() {
80 IPath xmlPath = XmlUtils.getXmlFilesPath();
81
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");
89
90 assertEquals(xmlPath, workspacePath);
91 }
92
93 /**
94 * test the {@link XmlUtils#xmlValidate(File)} method
95 */
96 @Test
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");
101 }
102 IStatus status = XmlUtils.xmlValidate(testXmlFile);
103 if (!status.isOK()) {
104 fail(status.getMessage());
105 }
106
107 testXmlFile = TmfXmlTestFiles.INVALID_FILE.getFile();
108 if ((testXmlFile == null) || !testXmlFile.exists()) {
109 fail("XML test file does not exist");
110 }
111 assertFalse(XmlUtils.xmlValidate(testXmlFile).isOK());
112 }
113
114 /**
115 * Test various invalid files and make sure they are invalid
116 */
117 @Test
118 public void testXmlValidateInvalid() {
119 IPath path = Activator.getAbsolutePath(PATH_INVALID);
120 File file = path.toFile();
121
122 File[] invalidFiles = file.listFiles();
123 assertTrue(invalidFiles.length > 0);
124 for (File f : invalidFiles) {
125 assertFalse("File " + f.getName(), XmlUtils.xmlValidate(f).isOK());
126 }
127 }
128
129 /**
130 * Test various valid files and make sure they are valid
131 */
132 @Test
133 public void testXmlValidateValid() {
134 IPath path = Activator.getAbsolutePath(PATH_VALID);
135 File file = path.toFile();
136
137 File[] validFiles = file.listFiles();
138 assertTrue(validFiles.length > 0);
139 for (File f : validFiles) {
140 assertTrue("File " + f.getName(), XmlUtils.xmlValidate(f).isOK());
141 }
142 }
143
144 /**
145 * test the {@link XmlUtils#addXmlFile(File)} method
146 */
147 @Test
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());
153
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");
158 }
159
160 XmlUtils.addXmlFile(testXmlFile);
161 assertTrue(destFile.exists());
162 }
163
164 private static final @NonNull String ANALYSIS_ID = "kernel.linux.sp";
165
166 /**
167 * Test the {@link XmlUtils#getElementInFile(String, String, String)} method
168 */
169 @Test
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);
176 }
177
178 /**
179 * Test the {@link XmlUtils#getChildElements(Element)} and
180 * {@link XmlUtils#getChildElements(Element, String)} methods
181 */
182 @Test
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");
187 }
188 /*
189 * This sounds useless, but I get a potential null pointer warning
190 * otherwise
191 */
192 if (testXmlFile == null) {
193 return;
194 }
195
196 Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID);
197
198 List<Element> values = XmlUtils.getChildElements(analysis, TmfXmlStrings.LOCATION);
199 assertEquals(5, values.size());
200
201 Element aLocation = values.get(0);
202 List<Element> attributes = XmlUtils.getChildElements(aLocation, TmfXmlStrings.STATE_ATTRIBUTE);
203 assertEquals(2, attributes.size());
204
205 values = XmlUtils.getChildElements(analysis, TmfXmlStrings.HEAD);
206 assertEquals(1, values.size());
207
208 Element head = values.get(0);
209 values = XmlUtils.getChildElements(head);
210 assertEquals(2, values.size());
211
212 }
213
214 /**
215 * Initialize a new trace based using the input file path
216 *
217 * @param traceFile
218 * The trace file
219 * @return The trace
220 */
221 public static @NonNull ITmfTrace initializeTrace(String traceFile) {
222 /* Initialize the trace */
223 TmfXmlTraceStub trace = new TmfXmlTraceStub();
224 try {
225 trace.initTrace(null, Activator.getAbsolutePath(new Path(traceFile)).toOSString(), TmfEvent.class);
226 } catch (TmfTraceException e1) {
227 fail(e1.getMessage());
228 }
229 return trace;
230 }
231
232 /**
233 * Initialize a new module using the xml file
234 *
235 * @param xmlAnalysisFile
236 * The xml file used to initialize the module
237 * @return The module
238 */
239 public static @NonNull XmlStateSystemModule initializeModule(TmfXmlTestFiles xmlAnalysisFile) {
240
241 /* Initialize the state provider module */
242 Document doc = xmlAnalysisFile.getXmlDocument();
243 assertNotNull(doc);
244
245 /* get State Providers modules */
246 NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER);
247 assertFalse(stateproviderNodes.getLength() == 0);
248
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);
254
255 module.setXmlFile(xmlAnalysisFile.getPath());
256
257 return module;
258 }
259
260 /**
261 * This function test the data provided by the state intervals queried
262 *
263 * @param testId
264 * The id of the test
265 * @param ss
266 * The state system associated to this test
267 * @param quark
268 * The quark we want to query
269 * @param expectedStarts
270 * The expected start timestamps for the intervals generated for
271 * this quark
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
278 * queries
279 */
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());
290 }
291 }
292
293 }
This page took 0.037448 seconds and 4 git commands to generate.