From 9a0aa59e8ee28305e82f63e4738304c79a8070f3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Genevi=C3=A8ve=20Bastien?= Date: Wed, 12 Nov 2014 10:21:08 -0500 Subject: [PATCH] TMF: Rework some test code for XML analysis to make it easier to add test cases MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: Ica13bc36c1c5312dd588ef87670f435ef01a76f2 Signed-off-by: Geneviève Bastien Reviewed-on: https://git.eclipse.org/r/36343 Tested-by: Hudson CI Reviewed-by: Marc-Andre Laperle Tested-by: Marc-Andre Laperle --- .../build.properties | 3 +- .../core/tests/common/TmfXmlTestFiles.java | 54 ++++++++++++++++--- .../analysis/xml/core/tests/Activator.java | 31 +++++++++++ .../xml/core/tests/module/XmlUtilsTest.java | 25 +-------- .../StateProviderModuleTest.java | 37 ++----------- .../stateprovider/StateProviderTest.java | 20 +------ .../stateprovider/XmlStateSystemModule.java | 2 +- .../module/XmlAnalysisModuleSourceTest.java | 10 ++-- 8 files changed, 93 insertions(+), 89 deletions(-) diff --git a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/build.properties b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/build.properties index b92be859f2..d08b64947d 100644 --- a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/build.properties +++ b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/build.properties @@ -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 diff --git a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/common/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/common/TmfXmlTestFiles.java b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/common/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/common/TmfXmlTestFiles.java index 598bedcfb5..60594b2281 100644 --- a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/common/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/common/TmfXmlTestFiles.java +++ b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/common/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/common/TmfXmlTestFiles.java @@ -12,7 +12,20 @@ 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; } } diff --git a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/Activator.java b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/Activator.java index 7c535cab24..6061d107c7 100644 --- a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/Activator.java +++ b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/Activator.java @@ -12,6 +12,12 @@ 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; + } + } + } diff --git a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/module/XmlUtilsTest.java b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/module/XmlUtilsTest.java index ae2a852749..b39da5fe20 100644 --- a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/module/XmlUtilsTest.java +++ b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/module/XmlUtilsTest.java @@ -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(); diff --git a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderModuleTest.java b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderModuleTest.java index 7edee8809e..be7998088d 100644 --- a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderModuleTest.java +++ b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderModuleTest.java @@ -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); diff --git a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderTest.java b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderTest.java index 068ae02770..9db995ea24 100644 --- a/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderTest.java +++ b/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/stateprovider/StateProviderTest.java @@ -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 */ diff --git a/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/tmf/analysis/xml/core/stateprovider/XmlStateSystemModule.java b/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/tmf/analysis/xml/core/stateprovider/XmlStateSystemModule.java index 4bfdd7f3a4..0bb3327481 100644 --- a/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/tmf/analysis/xml/core/stateprovider/XmlStateSystemModule.java +++ b/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/tmf/analysis/xml/core/stateprovider/XmlStateSystemModule.java @@ -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; diff --git a/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/ui/tests/module/XmlAnalysisModuleSourceTest.java b/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/ui/tests/module/XmlAnalysisModuleSourceTest.java index 45f7c0a09c..bf4a921ae9 100644 --- a/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/ui/tests/module/XmlAnalysisModuleSourceTest.java +++ b/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/ui/tests/module/XmlAnalysisModuleSourceTest.java @@ -73,10 +73,10 @@ public class XmlAnalysisModuleSourceTest { XmlAnalysisModuleSource module = new XmlAnalysisModuleSource(); Iterable 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 modules, String moduleName) { -- 2.34.1