tmf : Introduce pattern segment and pattern segment builder
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core.tests / src / org / eclipse / tracecompass / tmf / analysis / xml / core / tests / module / XmlUtilsTest.java
CommitLineData
e11e382c 1/*******************************************************************************
900cbf89 2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal and others
e11e382c
FW
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
2bdf0193 13package org.eclipse.tracecompass.tmf.analysis.xml.core.tests.module;
e11e382c
FW
14
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertFalse;
6d20d989 17import static org.junit.Assert.assertNotNull;
e11e382c
FW
18import static org.junit.Assert.assertTrue;
19import static org.junit.Assert.fail;
20
21import java.io.File;
6d20d989 22import java.util.List;
e11e382c
FW
23
24import org.eclipse.core.resources.IWorkspace;
25import org.eclipse.core.resources.ResourcesPlugin;
26import org.eclipse.core.runtime.IPath;
8945f67f 27import org.eclipse.core.runtime.IStatus;
cc4dbd9e 28import org.eclipse.core.runtime.Path;
6d20d989 29import org.eclipse.jdt.annotation.NonNull;
900cbf89
JCK
30import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
31import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
32import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
33import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
34import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
35import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
2bdf0193
AM
36import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
37import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
900cbf89 38import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.XmlStateSystemModule;
2bdf0193
AM
39import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.Activator;
40import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles;
900cbf89
JCK
41import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
42import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
43import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
44import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
e11e382c
FW
45import org.junit.After;
46import org.junit.Test;
900cbf89 47import org.w3c.dom.Document;
6d20d989 48import org.w3c.dom.Element;
900cbf89 49import org.w3c.dom.NodeList;
e11e382c
FW
50
51/**
52 * Tests for the {@link XmlUtils} class
53 *
54 * @author Geneviève Bastien
55 */
56public class XmlUtilsTest {
57
e2ea8484
GB
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
e11e382c
FW
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()
c77a695a 87 .append("org.eclipse.tracecompass.tmf.analysis.xml.core")
e11e382c
FW
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 }
8945f67f
GB
102 IStatus status = XmlUtils.xmlValidate(testXmlFile);
103 if (!status.isOK()) {
104 fail(status.getMessage());
105 }
e11e382c
FW
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
6b5218d0
FW
114 /**
115 * Test various invalid files and make sure they are invalid
116 */
117 @Test
118 public void testXmlValidateInvalid() {
9a0aa59e 119 IPath path = Activator.getAbsolutePath(PATH_INVALID);
e2ea8484
GB
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());
cc4dbd9e 126 }
e2ea8484
GB
127 }
128
129 /**
130 * Test various valid files and make sure they are valid
131 */
132 @Test
133 public void testXmlValidateValid() {
9a0aa59e 134 IPath path = Activator.getAbsolutePath(PATH_VALID);
e2ea8484 135 File file = path.toFile();
cc4dbd9e
MAL
136
137 File[] validFiles = file.listFiles();
e2ea8484 138 assertTrue(validFiles.length > 0);
6b5218d0 139 for (File f : validFiles) {
e2ea8484 140 assertTrue("File " + f.getName(), XmlUtils.xmlValidate(f).isOK());
6b5218d0
FW
141 }
142 }
143
e11e382c
FW
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
e2ea8484 164 private static final @NonNull String ANALYSIS_ID = "kernel.linux.sp";
6d20d989
GB
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();
7ab857a1
MAL
172 assertNotNull("XML test file does not exist", testXmlFile);
173 assertTrue("XML test file does not exist", testXmlFile.exists());
6d20d989
GB
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
66471052
GB
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());
6d20d989
GB
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
900cbf89
JCK
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
e11e382c 293}
This page took 0.090905 seconds and 5 git commands to generate.