import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
+import java.util.List;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils;
+import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
import org.eclipse.linuxtools.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles;
import org.junit.After;
import org.junit.Test;
+import org.w3c.dom.Element;
/**
* Tests for the {@link XmlUtils} class
*/
public class XmlUtilsTest {
+ private static final String pathname = "test_xml_files/test_invalid";
+
/**
* Empty the XML directory after the test
*/
fail("XML test file does not exist");
}
assertFalse(XmlUtils.xmlValidate(testXmlFile).isOK());
+
+ }
+
+ /**
+ * Test various invalid files and make sure they are invalid
+ */
+ @Test
+ public void testXmlValidateInvalid() {
+ File[] validFiles = (new File(pathname)).listFiles();
+ for (File f : validFiles) {
+ assertFalse("File " + f.getName(), XmlUtils.xmlValidate(f).isOK());
+ }
}
+
/**
* test the {@link XmlUtils#addXmlFile(File)} method
*/
assertTrue(destFile.exists());
}
+ @NonNull private static final String ANALYSIS_ID = "kernel.linux.sp";
+
+ /**
+ * Test the {@link XmlUtils#getElementInFile(String, String, String)} method
+ */
+ @Test
+ public void testGetElementInFile() {
+ File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile();
+ if ((testXmlFile == null) || !testXmlFile.exists()) {
+ fail("XML test file does not exist");
+ }
+ /*
+ * This sounds useless, but I get a potential null pointer warning
+ * otherwise
+ */
+ if (testXmlFile == null) {
+ return;
+ }
+
+ Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID);
+ assertNotNull(analysis);
+ }
+
+ /**
+ * Test the {@link XmlUtils#getChildElements(Element)} and
+ * {@link XmlUtils#getChildElements(Element, String)} methods
+ */
+ @Test
+ public void testGetChildElements() {
+ File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile();
+ if ((testXmlFile == null) || !testXmlFile.exists()) {
+ fail("XML test file does not exist");
+ }
+ /*
+ * This sounds useless, but I get a potential null pointer warning
+ * otherwise
+ */
+ if (testXmlFile == null) {
+ return;
+ }
+
+ Element analysis = XmlUtils.getElementInFile(testXmlFile.getAbsolutePath(), TmfXmlStrings.STATE_PROVIDER, ANALYSIS_ID);
+
+ List<Element> values = XmlUtils.getChildElements(analysis, TmfXmlStrings.LOCATION);
+ assertEquals(5, values.size());
+
+ Element aLocation = values.get(0);
+ List<Element> attributes = XmlUtils.getChildElements(aLocation, TmfXmlStrings.STATE_ATTRIBUTE);
+ assertEquals(2, attributes.size());
+
+ values = XmlUtils.getChildElements(analysis, TmfXmlStrings.HEAD);
+ assertEquals(1, values.size());
+
+ Element head = values.get(0);
+ values = XmlUtils.getChildElements(head);
+ assertEquals(2, values.size());
+
+ }
+
}