tmf: Split the state system in a separate plugin
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.core.tests / src / org / eclipse / linuxtools / tmf / analysis / xml / core / tests / module / XmlUtilsTest.java
index 181648a32420d13dbf2842a70daf9519bee6d11d..5867aa580b00f7b032636defd2fb23ad478b6474 100644 (file)
@@ -14,19 +14,24 @@ package org.eclipse.linuxtools.tmf.analysis.xml.core.tests.module;
 
 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
@@ -35,6 +40,8 @@ import org.junit.Test;
  */
 public class XmlUtilsTest {
 
+    private static final String pathname = "test_xml_files/test_invalid";
+
     /**
      * Empty the XML directory after the test
      */
@@ -86,8 +93,21 @@ public class XmlUtilsTest {
             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
      */
@@ -108,4 +128,63 @@ public class XmlUtilsTest {
         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());
+
+    }
+
 }
This page took 0.039516 seconds and 5 git commands to generate.