TMF: Rework some test code for XML analysis to make it easier to add test cases
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Wed, 12 Nov 2014 15:21:08 +0000 (10:21 -0500)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Fri, 21 Nov 2014 16:27:17 +0000 (11:27 -0500)
Change-Id: Ica13bc36c1c5312dd588ef87670f435ef01a76f2
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/36343
Tested-by: Hudson CI
Reviewed-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Tested-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
org.eclipse.tracecompass.tmf.analysis.xml.core.tests/build.properties
org.eclipse.tracecompass.tmf.analysis.xml.core.tests/common/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/common/TmfXmlTestFiles.java
org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/Activator.java
org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/module/XmlUtilsTest.java
org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderModuleTest.java
org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderTest.java
org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/tmf/analysis/xml/core/stateprovider/XmlStateSystemModule.java
org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/ui/tests/module/XmlAnalysisModuleSourceTest.java

index b92be859f2f834e91766c5435881fecee8e29baf..d08b64947d338b588be6804ab34a0bb96a03a209 100644 (file)
@@ -14,7 +14,8 @@ source.. = src/,\
 output.. = bin/
 bin.includes = META-INF/,\
                .,\
-               about.html
+               about.html,\
+               test_xml_files/
 src.includes = about.html
 additional.bundles = org.eclipse.jdt.annotation
 jars.extra.classpath = platform:/plugin/org.eclipse.jdt.annotation
index 598bedcfb52d40f3d98f8bf22719896642f894ec..60594b2281f8e2bd580e938b669c8ed9012066d8 100644 (file)
 
 package org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common;
 
+import static org.junit.Assert.fail;
+
 import java.io.File;
+import java.io.IOException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.Activator;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
 
 /**
  * Provides some test XML files to use
@@ -21,9 +34,9 @@ import java.io.File;
  */
 public enum TmfXmlTestFiles {
     /** A valid XML test file */
-    VALID_FILE("../org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_xml_files/test_valid/test_valid.xml"),
+    VALID_FILE("test_xml_files/test_valid/test_valid.xml"),
     /** An invalid test file */
-    INVALID_FILE("../org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_xml_files/test_invalid/test_invalid.xml");
+    INVALID_FILE("test_xml_files/test_invalid/test_invalid.xml");
 
     private final String fPath;
 
@@ -32,12 +45,16 @@ public enum TmfXmlTestFiles {
     }
 
     /**
-     * Get the file name part of the file
+     * Get the absolute path of this test file
      *
-     * @return The path of this test file
+     * @return The absolute path of this test file
      */
-    public String getPath() {
-        return fPath;
+    public IPath getPath() {
+        IPath absPath = Activator.getAbsolutePath(new Path(fPath));
+        if (absPath == null) {
+            fail("Cannot find file path for '" + fPath + "'");
+        }
+        return absPath;
     }
 
     /**
@@ -46,7 +63,30 @@ public enum TmfXmlTestFiles {
      * @return The file object for this test file
      */
     public File getFile() {
-        return new File(fPath);
+        return getPath().toFile();
+    }
+
+    /**
+     * Get the XML {@link Document} for this test xml file
+     *
+     * @return The XML {@link Document}
+     */
+    public Document getXmlDocument() {
+        /* Initialize the state provider module */
+        Document doc = null;
+        try {
+            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
+            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
+            doc = dBuilder.parse(getFile());
+            doc.getDocumentElement().normalize();
+        } catch (ParserConfigurationException e) {
+            fail("Xml document parse exception");
+        } catch (SAXException e) {
+            fail("Exception parsing xml file");
+        } catch (IOException e) {
+            fail("File io exception");
+        }
+        return doc;
     }
 
 }
index 7c535cab24770ba459ea0f116fcb1f4cfedba9c5..6061d107c7281e361bafe81bfade86270d391b8d 100644 (file)
 
 package org.eclipse.tracecompass.tmf.analysis.xml.core.tests;
 
+import java.io.IOException;
+import java.net.URL;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Plugin;
 import org.osgi.framework.BundleContext;
 
@@ -55,4 +61,29 @@ public class Activator extends Plugin {
         return plugin;
     }
 
+    /**
+     * Gets the absolute path from a path relative to this plugin's root
+     *
+     * @param relativePath
+     *            The path relative to this plugin
+     * @return The absolute path corresponding to this relative path
+     */
+    public static IPath getAbsolutePath(Path relativePath) {
+        Activator plugin2 = getDefault();
+        if (plugin2 == null) {
+            /*
+             * Shouldn't happen but at least throw something to get the test to
+             * fail early
+             */
+            return null;
+        }
+        URL location = FileLocator.find(plugin2.getBundle(), relativePath, null);
+        try {
+            IPath path = new Path(FileLocator.toFileURL(location).getPath());
+            return path;
+        } catch (IOException e) {
+            return null;
+        }
+    }
+
 }
index ae2a8527495be27e15ea75d7dece66652cf6f540..b39da5fe20ee55b9713582d539d7f7275a039c86 100644 (file)
@@ -19,13 +19,10 @@ import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.File;
-import java.io.IOException;
-import java.net.URL;
 import java.util.List;
 
 import org.eclipse.core.resources.IWorkspace;
 import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.FileLocator;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Path;
@@ -48,24 +45,6 @@ public class XmlUtilsTest {
     private static final Path PATH_INVALID = new Path("test_xml_files/test_invalid");
     private static final Path PATH_VALID = new Path("test_xml_files/test_valid");
 
-    private static IPath getAbsolutePath(Path relativePath) {
-        Activator plugin = Activator.getDefault();
-        if (plugin == null) {
-            /*
-             * Shouldn't happen but at least throw something to get the test to
-             * fail early
-             */
-            throw new IllegalStateException();
-        }
-        URL location = FileLocator.find(plugin.getBundle(), relativePath, null);
-        try {
-            IPath path = new Path(FileLocator.toFileURL(location).getPath());
-            return path;
-        } catch (IOException e) {
-            throw new IllegalStateException();
-        }
-    }
-
     /**
      * Empty the XML directory after the test
      */
@@ -125,7 +104,7 @@ public class XmlUtilsTest {
      */
     @Test
     public void testXmlValidateInvalid() {
-        IPath path = getAbsolutePath(PATH_INVALID);
+        IPath path = Activator.getAbsolutePath(PATH_INVALID);
         File file = path.toFile();
 
         File[] invalidFiles = file.listFiles();
@@ -140,7 +119,7 @@ public class XmlUtilsTest {
      */
     @Test
     public void testXmlValidateValid() {
-        IPath path = getAbsolutePath(PATH_VALID);
+        IPath path = Activator.getAbsolutePath(PATH_VALID);
         File file = path.toFile();
 
         File[] validFiles = file.listFiles();
index 7edee8809e452d46890f2b22a7555656c67db004..be7998088df866a1522e58b25b50412caf74ff76 100644 (file)
@@ -18,15 +18,7 @@ import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.junit.Assume.assumeTrue;
 
-import java.io.File;
-import java.io.IOException;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-
 import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.core.runtime.Path;
 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.XmlStateSystemModule;
 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles;
@@ -37,7 +29,6 @@ import org.junit.Test;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
-import org.xml.sax.SAXException;
 
 /**
  * Test suite for the XmlStateSystemModule Test. It tests the reading of the
@@ -53,33 +44,13 @@ public class StateProviderModuleTest {
 
     private XmlStateSystemModule fModule;
 
-    private static Document getXmlDocumentFromFile(File file) {
-        /* Initialize the state provider module */
-        try {
-            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
-            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
-
-            Document doc = dBuilder.parse(file);
-
-            doc.getDocumentElement().normalize();
-            return doc;
-        } catch (ParserConfigurationException e) {
-            fail("Xml document parse exception");
-        } catch (SAXException e) {
-            fail("Exception parsing xml file");
-        } catch (IOException e) {
-            fail("File io exception");
-        }
-        return null;
-    }
-
     /**
      * Test the module construction
      */
     @Test
     public void testModuleConstruction() {
 
-        Document doc = getXmlDocumentFromFile(TmfXmlTestFiles.VALID_FILE.getFile());
+        Document doc = TmfXmlTestFiles.VALID_FILE.getXmlDocument();
         assertNotNull(doc);
 
         /* get State Providers modules */
@@ -93,7 +64,7 @@ public class StateProviderModuleTest {
         fModule.setId(moduleId);
         assertEquals(ANALYSIS_ID, fModule.getId());
 
-        fModule.setXmlFile(new Path(TmfXmlTestFiles.VALID_FILE.getFile().getAbsolutePath()));
+        fModule.setXmlFile(TmfXmlTestFiles.VALID_FILE.getPath());
 
         assertEquals(ANALYSIS_NAME, fModule.getName());
     }
@@ -105,7 +76,7 @@ public class StateProviderModuleTest {
     public void testModuleExecution() {
         assumeTrue(CtfTmfTestTrace.KERNEL.exists());
 
-        Document doc = getXmlDocumentFromFile(TmfXmlTestFiles.VALID_FILE.getFile());
+        Document doc = TmfXmlTestFiles.VALID_FILE.getXmlDocument();
         assertNotNull(doc);
 
         /* get State Providers modules */
@@ -117,7 +88,7 @@ public class StateProviderModuleTest {
         assertNotNull(moduleId);
         fModule.setId(moduleId);
 
-        fModule.setXmlFile(new Path(TmfXmlTestFiles.VALID_FILE.getFile().getAbsolutePath()));
+        fModule.setXmlFile(TmfXmlTestFiles.VALID_FILE.getPath());
 
         try (CtfTmfTrace trace = CtfTmfTestTrace.KERNEL.getTrace();) {
             fModule.setTrace(trace);
index 068ae027708bf1dfcfdf5e306817bf53ea894f32..9db995ea24c56ce98c82717199d6c8d997e16e53 100644 (file)
@@ -18,13 +18,8 @@ import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.junit.Assume.assumeTrue;
 
-import java.io.IOException;
 import java.util.List;
 
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
@@ -40,7 +35,6 @@ import org.junit.Test;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
-import org.xml.sax.SAXException;
 
 /**
  * Test suite for the xml state providers
@@ -66,19 +60,7 @@ public class StateProviderTest {
         fTrace = trace;
 
         /* Initialize the state provider module */
-        Document doc = null;
-        try {
-            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
-            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
-            doc = dBuilder.parse(TmfXmlTestFiles.VALID_FILE.getFile());
-            doc.getDocumentElement().normalize();
-        } catch (ParserConfigurationException e) {
-            fail("Xml document parse exception");
-        } catch (SAXException e) {
-            fail("Exception parsing xml file");
-        } catch (IOException e) {
-            fail("File io exception");
-        }
+        Document doc = TmfXmlTestFiles.VALID_FILE.getXmlDocument();
         assertNotNull(doc);
 
         /* get State Providers modules */
index 4bfdd7f3a4400f4d1d61b1f13bc8cfa9dedf9a84..0bb33274818377d5e9b7f8cc7e6b7ff57a6e8c7e 100644 (file)
@@ -67,7 +67,7 @@ public class XmlStateSystemModule extends TmfStateSystemAnalysisModule {
      * Sets the file path of the XML file containing the state provider
      *
      * @param file
-     *            The full path to the XML file
+     *            The absolute path to the XML file
      */
     public void setXmlFile(IPath file) {
         fXmlFile = file;
index 45f7c0a09cdeb3a58e95a7d0f6d791b18b96d011..bf4a921ae96f9b96b74bf353043f718c77183604 100644 (file)
@@ -73,10 +73,10 @@ public class XmlAnalysisModuleSourceTest {
         XmlAnalysisModuleSource module = new XmlAnalysisModuleSource();
 
         Iterable<IAnalysisModuleHelper> modules = module.getAnalysisModules();
-        assertFalse(findModule(modules, SS_MODULE));
+        assertFalse("Module present", findModule(modules, SS_MODULE));
 
         /* Test that the builtin module is present */
-        assertTrue(findModule(modules, BUILTIN_MODULE));
+        assertTrue("builtin module present", findModule(modules, BUILTIN_MODULE));
 
         /* use the valid XML test file */
         File testXmlFile = TmfXmlTestFiles.VALID_FILE.getFile();
@@ -88,9 +88,9 @@ public class XmlAnalysisModuleSourceTest {
         XmlAnalysisModuleSource.notifyModuleChange();
         modules = module.getAnalysisModules();
 
-        assertTrue(modules.iterator().hasNext());
-        assertTrue(findModule(modules, SS_MODULE));
-        assertTrue(findModule(modules, BUILTIN_MODULE));
+        assertTrue("Modules available from source", modules.iterator().hasNext());
+        assertTrue("Module present after add file", findModule(modules, SS_MODULE));
+        assertTrue("Builtin module present after add file", findModule(modules, BUILTIN_MODULE));
     }
 
     private static boolean findModule(Iterable<IAnalysisModuleHelper> modules, String moduleName) {
This page took 0.095477 seconds and 5 git commands to generate.