Some cleanups related to investigating redundant null checks
[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 /**
116 * Test various invalid files and make sure they are invalid
117 */
118 @Test
119 public void testXmlValidateInvalid() {
120 IPath path = Activator.getAbsolutePath(PATH_INVALID);
121 File file = path.toFile();
122
123 File[] invalidFiles = file.listFiles();
124 assertTrue(invalidFiles.length > 0);
125 for (File f : invalidFiles) {
126 assertFalse("File " + f.getName(), XmlUtils.xmlValidate(f).isOK());
127 }
128 }
129
130 /**
131 * Test various valid files and make sure they are valid
132 */
133 @Test
134 public void testXmlValidateValid() {
135 IPath path = Activator.getAbsolutePath(PATH_VALID);
136 File file = path.toFile();
137
138 File[] validFiles = file.listFiles();
139 assertTrue(validFiles.length > 0);
140 for (File f : validFiles) {
141 assertTrue("File " + f.getName(), XmlUtils.xmlValidate(f).isOK());
142 }
143 }
144
145 /**
146 * test the {@link XmlUtils#addXmlFile(File)} method
147 */
148 @Test
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());
154
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");
159 }
160
161 XmlUtils.addXmlFile(testXmlFile);
162 assertTrue(destFile.exists());
163 }
164
165 private static final @NonNull String ANALYSIS_ID = "kernel.linux.sp";
166
167 /**
168 * Test the {@link XmlUtils#getElementInFile(String, String, String)} method
169 */
170 @Test
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);
177 }
178
179 /**
180 * Test the {@link XmlUtils#getChildElements(Element)} and
181 * {@link XmlUtils#getChildElements(Element, String)} methods
182 */
183 @Test
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");
188 }
189 /*
190 * This sounds useless, but I get a potential null pointer warning
191 * otherwise
192 */
193 if (testXmlFile == null) {
194 return;
195 }
196
197 Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID);
198
199 List<Element> values = XmlUtils.getChildElements(analysis, TmfXmlStrings.LOCATION);
200 assertEquals(5, values.size());
201
202 Element aLocation = values.get(0);
203 List<Element> attributes = XmlUtils.getChildElements(aLocation, TmfXmlStrings.STATE_ATTRIBUTE);
204 assertEquals(2, attributes.size());
205
206 values = XmlUtils.getChildElements(analysis, TmfXmlStrings.HEAD);
207 assertEquals(1, values.size());
208
209 Element head = values.get(0);
210 values = XmlUtils.getChildElements(head);
211 assertEquals(2, values.size());
212
213 }
214
215 /**
216 * Initialize a new trace based using the input file path
217 *
218 * @param traceFile
219 * The trace file
220 * @return The trace
221 */
222 public static @NonNull ITmfTrace initializeTrace(String traceFile) {
223 /* Initialize the trace */
224 TmfXmlTraceStub trace = new TmfXmlTraceStub();
225 try {
226 trace.initTrace(null, Activator.getAbsolutePath(new Path(traceFile)).toOSString(), TmfEvent.class);
227 } catch (TmfTraceException e1) {
228 fail(e1.getMessage());
229 }
230 return trace;
231 }
232
233 /**
234 * Initialize a new module using the xml file
235 *
236 * @param xmlAnalysisFile
237 * The xml file used to initialize the module
238 * @return The module
239 */
240 public static @NonNull XmlStateSystemModule initializeModule(TmfXmlTestFiles xmlAnalysisFile) {
241
242 /* Initialize the state provider module */
243 Document doc = xmlAnalysisFile.getXmlDocument();
244 assertNotNull(doc);
245
246 /* get State Providers modules */
247 NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER);
248 assertFalse(stateproviderNodes.getLength() == 0);
249
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);
255
256 module.setXmlFile(xmlAnalysisFile.getPath());
257
258 return module;
259 }
260
261 /**
262 * This function test the data provided by the state intervals queried
263 *
264 * @param testId
265 * The id of the test
266 * @param ss
267 * The state system associated to this test
268 * @param quark
269 * The quark we want to query
270 * @param expectedStarts
271 * The expected start timestamps for the intervals generated for
272 * this quark
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
279 * queries
280 */
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());
291 }
292 }
293
294 }
This page took 0.037239 seconds and 6 git commands to generate.