xml: bug 497272 Populate views from built-in XML files
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Tue, 9 Aug 2016 20:14:35 +0000 (16:14 -0400)
committerJean-Christian Kouame <jean-christian.kouame@ericsson.com>
Fri, 19 Aug 2016 19:36:17 +0000 (15:36 -0400)
This patch makes sure that outputs defined in built-in XML files
advertised through the extension point are added under their analyses.

It moves the method to list the built-in files from the analysis module
source to the XmlUtils class and adds a unit test for this use case.

Change-Id: I5984d1cc9adbbc7e6348632c0f58390f73db41bd
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/78723
Reviewed-by: Jean-Christian Kouame <jean-christian.kouame@ericsson.com>
Tested-by: Jean-Christian Kouame <jean-christian.kouame@ericsson.com>
Reviewed-by: Hudson CI
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_xml_files/test_builtin.xml
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/META-INF/MANIFEST.MF
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlAnalysisModuleSource.java
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlUtils.java
tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/META-INF/MANIFEST.MF
tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/ui/tests/module/XmlAnalysisOutputSourceTest.java [new file with mode: 0644]
tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/META-INF/MANIFEST.MF
tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/module/TmfXmlAnalysisOutputSource.java

index 3e7a1e8715ce6f5b5c654bf1346fdf6eb9d67275..b4f433c04ac73af1b394794d02f16bfd37be62d0 100644 (file)
             </stateChange>
         </eventHandler>
     </stateProvider>
+
+    <timeGraphView id="test.builtin.sp.output">
+        <head>
+            <analysis id="test.builtin.sp" />
+            <label value="Test output of XML builtin module" />
+        </head>
+        <entry path="cpu">
+            <display type="self" />
+        </entry>
+    </timeGraphView>
 </tmfxml>
\ No newline at end of file
index 3f9d0bb48d1c42f1c838a64a73cc3caccb04f2d8..ce566b032abc8df15a3fa114ec2884a6f4598deb 100644 (file)
@@ -17,8 +17,11 @@ Export-Package: org.eclipse.tracecompass.internal.tmf.analysis.xml.core;x-friend
  org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests,org.eclipse.tracecompass.tmf.analysis.xml.ui",
  org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.readonly;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests,org.eclipse.tracecompass.tmf.analysis.xml.ui",
  org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.readwrite;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests,org.eclipse.tracecompass.tmf.analysis.xml.ui",
- org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests,org.eclipse.tracecompass.tmf.analysis.xml.ui,
- org.eclipse.tracecompass.tmf.analysis.xml.ui.swtbot.tests",
+ org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module;
+  x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests,
+   org.eclipse.tracecompass.tmf.analysis.xml.ui,
+   org.eclipse.tracecompass.tmf.analysis.xml.ui.swtbot.tests,
+   org.eclipse.tracecompass.tmf.analysis.xml.ui.tests",
  org.eclipse.tracecompass.internal.tmf.analysis.xml.core.pattern.stateprovider;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.ui,org.eclipse.tracecompass.tmf.analysis.xml.core.tests",
  org.eclipse.tracecompass.internal.tmf.analysis.xml.core.segment;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests",
  org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests,org.eclipse.tracecompass.tmf.analysis.xml.ui"
index 8900602e60bd18ec29135db8b7b156aeaad46aaa..ac3e3ffdc630bd1a2df7f4432af0db60a8c8d934 100644 (file)
@@ -11,10 +11,8 @@ package org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module;
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.net.URL;
 import java.nio.channels.FileChannel;
 import java.util.ArrayList;
 import java.util.List;
@@ -22,12 +20,7 @@ import java.util.Map;
 
 import javax.xml.parsers.ParserConfigurationException;
 
-import org.eclipse.core.runtime.FileLocator;
-import org.eclipse.core.runtime.IConfigurationElement;
 import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.ISafeRunnable;
-import org.eclipse.core.runtime.Platform;
-import org.eclipse.core.runtime.SafeRunner;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator;
 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.TmfAnalysisModuleHelperXml.XmlAnalysisModuleType;
@@ -35,7 +28,6 @@ import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.Tmf
 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper;
 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleSource;
 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
-import org.osgi.framework.Bundle;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
@@ -50,11 +42,6 @@ import org.xml.sax.SAXException;
  */
 public class XmlAnalysisModuleSource implements IAnalysisModuleSource {
 
-    /** Extension point ID */
-    private static final String TMF_XML_BUILTIN_ID = "org.eclipse.linuxtools.tmf.analysis.xml.core.files"; //$NON-NLS-1$
-    private static final String XML_FILE_ELEMENT = "xmlfile"; //$NON-NLS-1$
-
-    private static final String XML_FILE_ATTRIB = "file"; //$NON-NLS-1$
 
     /*
      * Legacy (Linux Tools) XML directory.
@@ -117,39 +104,9 @@ public class XmlAnalysisModuleSource implements IAnalysisModuleSource {
     }
 
     private static void populateBuiltinModules() {
-        /* Get the XML files advertised through the extension point */
-        IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(TMF_XML_BUILTIN_ID);
-        for (IConfigurationElement element : elements) {
-            if (element.getName().equals(XML_FILE_ELEMENT)) {
-                final String filename = element.getAttribute(XML_FILE_ATTRIB);
-                final String name = element.getContributor().getName();
-                // Run this in a safe runner in case there is an exception
-                // (IOException, FileNotFoundException, NPE, etc).
-                // This makes sure other extensions are not prevented from
-                // working if one is faulty.
-                SafeRunner.run(new ISafeRunnable() {
-
-                    @Override
-                    public void run() throws IOException {
-                        if (name != null) {
-                            Bundle bundle = Platform.getBundle(name);
-                            if (bundle != null) {
-                                URL xmlUrl = bundle.getResource(filename);
-                                if (xmlUrl == null) {
-                                    throw new FileNotFoundException(filename);
-                                }
-                                URL locatedURL = FileLocator.toFileURL(xmlUrl);
-                                processFile(new File(locatedURL.getFile()));
-                            }
-                        }
-                    }
-
-                    @Override
-                    public void handleException(Throwable exception) {
-                        // Handled sufficiently in SafeRunner
-                    }
-                });
-            }
+        Map<String, IPath> files = XmlUtils.listBuiltinFiles();
+        for (IPath xmlPath : files.values()) {
+            processFile(xmlPath.toFile());
         }
     }
 
index 28105b0444bafd01643850e8b1f7ee831f9f7288..626e73c7444fe6ad6438dad3e6e2ab6379ba2a4f 100644 (file)
@@ -16,6 +16,7 @@ import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyStrin
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.net.URL;
@@ -35,15 +36,21 @@ import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
 import javax.xml.validation.Validator;
 
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IConfigurationElement;
 import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.ISafeRunnable;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.SafeRunner;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.osgi.util.NLS;
 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator;
 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
+import org.osgi.framework.Bundle;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -65,9 +72,13 @@ public class XmlUtils {
     /** Name of the XSD schema file */
     private static final String XSD = "xmlDefinition.xsd"; //$NON-NLS-1$
 
+    /** Extension point ID and attributes */
+    private static final String TMF_XML_BUILTIN_ID = "org.eclipse.linuxtools.tmf.analysis.xml.core.files"; //$NON-NLS-1$
+    private static final String XML_FILE_ELEMENT = "xmlfile"; //$NON-NLS-1$
+    private static final String XML_FILE_ATTRIB = "file"; //$NON-NLS-1$
+
     /**
      * Extension for XML files
-     * @since 2.0
      */
     public static final String XML_EXTENSION = "xml"; //$NON-NLS-1$
 
@@ -148,7 +159,6 @@ public class XmlUtils {
      * the key is the file name.
      *
      * @return A map with all the XML analysis files
-     * @since 2.0
      */
     public static synchronized @NonNull Map<String, File> listFiles() {
         IPath pathToFiles = XmlUtils.getXmlFilesPath();
@@ -171,12 +181,55 @@ public class XmlUtils {
         return Collections.unmodifiableMap(fileMap);
     }
 
+    /**
+     * List all files advertised through the builtin extension point. It returns a map where the key is the file name.
+     *
+     * @return A map with all the XMl analysis builtin files
+     */
+    public static synchronized @NonNull Map<String, IPath> listBuiltinFiles() {
+        /* Get the XML files advertised through the extension point */
+        IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(TMF_XML_BUILTIN_ID);
+        Map<String, IPath> map = new HashMap<>();
+        for (IConfigurationElement element : elements) {
+            if (element.getName().equals(XML_FILE_ELEMENT)) {
+                final String filename = element.getAttribute(XML_FILE_ATTRIB);
+                final String name = element.getContributor().getName();
+                // Run this in a safe runner in case there is an exception
+                // (IOException, FileNotFoundException, NPE, etc).
+                // This makes sure other extensions are not prevented from
+                // working if one is faulty.
+                SafeRunner.run(new ISafeRunnable() {
+
+                    @Override
+                    public void run() throws IOException {
+                        if (name != null) {
+                            Bundle bundle = Platform.getBundle(name);
+                            if (bundle != null) {
+                                URL xmlUrl = bundle.getResource(filename);
+                                if (xmlUrl == null) {
+                                    throw new FileNotFoundException(filename);
+                                }
+                                URL locatedURL = FileLocator.toFileURL(xmlUrl);
+                                map.put(filename, new Path(locatedURL.getPath()));
+                            }
+                        }
+                    }
+
+                    @Override
+                    public void handleException(Throwable exception) {
+                        // Handled sufficiently in SafeRunner
+                    }
+                });
+            }
+        }
+        return map;
+    }
+
     /**
      * Delete an XML analysis file
      *
      * @param name
      *            The XML file to delete
-     * @since 2.0
      */
     public static void deleteFile(String name) {
         Map<String, File> files = listFiles();
@@ -195,7 +248,6 @@ public class XmlUtils {
      * @param to
      *            The full path of the file to write to
      * @return Whether the file was successfully exported
-     * @since 2.0
      */
     public static IStatus exportXmlFile(String from, String to) {
 
@@ -242,7 +294,6 @@ public class XmlUtils {
      * @param fileName
      *            The file name
      * @return The list of IDs
-     * @since 2.0
      */
     public static List<String> getAnalysisIdsFromFile(String fileName) {
         List<String> ids = new ArrayList<>();
@@ -281,7 +332,6 @@ public class XmlUtils {
      *             If any parse errors occur.
      * @throws IOException
      *             If any IO errors occur.
-     * @since 2.0
      */
     public static Document getDocumentFromFile(File file) throws ParserConfigurationException, SAXException, IOException {
         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
index e8368206ba3951ce3dc0de12f0ce4f00664202a0..a1137eae44bba826daaa4a9c0d2d6ab25ff7c069 100644 (file)
@@ -14,7 +14,8 @@ Require-Bundle: org.eclipse.ui,
  org.eclipse.tracecompass.tmf.analysis.xml.core,
  org.eclipse.tracecompass.tmf.analysis.xml.ui,
  org.eclipse.tracecompass.tmf.ui,
- org.eclipse.tracecompass.tmf.analysis.xml.core.tests
+ org.eclipse.tracecompass.tmf.analysis.xml.core.tests,
+ org.eclipse.tracecompass.tmf.core.tests
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Bundle-ActivationPolicy: lazy
 Import-Package: com.google.common.collect;version="15.0.0",
diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/ui/tests/module/XmlAnalysisOutputSourceTest.java b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/ui/tests/module/XmlAnalysisOutputSourceTest.java
new file mode 100644 (file)
index 0000000..eb37dea
--- /dev/null
@@ -0,0 +1,61 @@
+/*******************************************************************************
+ * Copyright (c) 2016 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.tmf.analysis.xml.ui.tests.module;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Iterator;
+
+import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.module.TmfXmlAnalysisOutputSource;
+import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
+import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisOutput;
+import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
+import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
+import org.junit.Test;
+
+/**
+ * Test that XML-defined outputs are added to the analysis modules they are for
+ *
+ * @author Geneviève Bastien
+ */
+public class XmlAnalysisOutputSourceTest {
+
+    private static final String BUILTIN_MODULE = "test.builtin.sp";
+    private static final String BUILTIN_OUTPUT= "Test output of XML builtin module";
+
+    /**
+     * Test the
+     * {@link TmfXmlAnalysisOutputSource#moduleCreated(IAnalysisModule)} method
+     */
+    @Test
+    public void testBuiltinOutput() {
+
+        TmfTrace trace = new TmfXmlTraceStub();
+        try {
+            trace.traceOpened(new TmfTraceOpenedSignal(this, trace, null));
+
+            IAnalysisModule module = trace.getAnalysisModule(BUILTIN_MODULE);
+            assertNotNull(module);
+
+            Iterator<IAnalysisOutput> iterator = module.getOutputs().iterator();
+            assertTrue(iterator.hasNext());
+            IAnalysisOutput output = iterator.next();
+            assertEquals(BUILTIN_OUTPUT, output.getName());
+
+        } finally {
+            trace.dispose();
+        }
+
+    }
+
+}
\ No newline at end of file
index 4111c9334e51fecd38eb9bde2a2970a281374373..cb72168756ef0478748d8171be65ac9e59aac6a6 100644 (file)
@@ -24,7 +24,7 @@ Require-Bundle: org.eclipse.ui,
  org.eclipse.core.filesystem
 Export-Package: org.eclipse.tracecompass.internal.tmf.analysis.xml.ui;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.ui.tests",
  org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.handler;x-internal:=true,
- org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.module;x-internal:=true,
+ org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.module;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.ui.tests",
  org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views;x-internal:=true,
  org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.ui.swtbot.tests",
  org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.timegraph;x-internal:=true,
index bf668a74d304977473c5b401f667490486c3f31d..9e09080c294ace42af29108c6940d0c0de801de6 100644 (file)
@@ -15,7 +15,8 @@ package org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.module;
 import java.io.File;
 import java.io.IOException;
 import java.util.List;
-import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 import javax.xml.parsers.ParserConfigurationException;
 
@@ -142,8 +143,14 @@ public class TmfXmlAnalysisOutputSource implements ITmfNewAnalysisModuleListener
 
     @Override
     public void moduleCreated(IAnalysisModule module) {
-        Map<String, File> files = XmlUtils.listFiles();
-        for (File xmlFile : files.values()) {
+        // Get all the XML files, builtin and not builtin
+        Set<File> files = XmlUtils.listBuiltinFiles().values().stream()
+                .map(p -> p.toFile())
+                .collect(Collectors.toSet());
+        XmlUtils.listFiles().values().stream()
+                .forEach(f -> files.add(f));
+
+        for (File xmlFile : files) {
             if (!XmlUtils.xmlValidate(xmlFile).isOK()) {
                 continue;
             }
This page took 0.032936 seconds and 5 git commands to generate.