tmf.core.tests: Add method to simplify XML Trace stub setup
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Mon, 4 Apr 2016 19:58:40 +0000 (15:58 -0400)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Tue, 26 Apr 2016 13:53:58 +0000 (09:53 -0400)
Change-Id: I7c59f26d0bf89b85aa648a452641d3b3edae59b2
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/70034
Reviewed-by: Hudson CI
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/Activator.java
analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/graph/TmfGraphBuilderModuleTest.java
tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/TmfCoreTestPlugin.java
tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/stub/XmlStubTraceTest.java
tmf/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java

index 44dcc43094e66e6bf7f4b85e64624ccc3d2ce51c..828eb6863fed09e203f05d4eb0d227294d24d63f 100644 (file)
@@ -19,6 +19,7 @@ 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.eclipse.jdt.annotation.NonNull;
 import org.osgi.framework.BundleContext;
 
 /**
@@ -87,7 +88,7 @@ public class Activator extends Plugin {
      *            The path relative to the plugin's root directory
      * @return The path corresponding to the relative path in parameter
      */
-    public static IPath getAbsoluteFilePath(String relativePath) {
+    public static @NonNull IPath getAbsoluteFilePath(String relativePath) {
         Activator plugin = Activator.getDefault();
         if (plugin == null) {
             /*
index f5f057036968d8e568169ea30409c4009d3dd26d..a9540d5d6c56297fa80eb571fb2cf266e546b0a4 100644 (file)
@@ -11,12 +11,9 @@ package org.eclipse.tracecompass.analysis.graph.core.tests.graph;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
 
 import java.util.List;
 
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IStatus;
 import org.eclipse.tracecompass.analysis.graph.core.base.TmfGraph;
 import org.eclipse.tracecompass.analysis.graph.core.base.TmfVertex;
 import org.eclipse.tracecompass.analysis.graph.core.base.TmfVertex.EdgeDirection;
@@ -24,8 +21,6 @@ import org.eclipse.tracecompass.analysis.graph.core.building.TmfGraphBuilderModu
 import org.eclipse.tracecompass.analysis.graph.core.tests.Activator;
 import org.eclipse.tracecompass.analysis.graph.core.tests.stubs.TestGraphWorker;
 import org.eclipse.tracecompass.analysis.graph.core.tests.stubs.module.GraphBuilderModuleStub;
-import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
-import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
@@ -56,17 +51,7 @@ public class TmfGraphBuilderModuleTest {
      * @return
      */
     private TmfGraphBuilderModule getModule() {
-        ITmfTrace trace = new TmfXmlTraceStub();
-        IPath filePath = Activator.getAbsoluteFilePath(STUB_TRACE_FILE);
-        IStatus status = trace.validate(null, filePath.toOSString());
-        if (!status.isOK()) {
-            fail(status.getException().getMessage());
-        }
-        try {
-            trace.initTrace(null, filePath.toOSString(), TmfEvent.class);
-        } catch (TmfTraceException e) {
-            fail(e.getMessage());
-        }
+        ITmfTrace trace = TmfXmlTraceStub.setupTrace(Activator.getAbsoluteFilePath(STUB_TRACE_FILE));
         ((TmfTrace) trace).traceOpened(new TmfTraceOpenedSignal(this, trace, null));
         GraphBuilderModuleStub module = null;
         for (GraphBuilderModuleStub mod : TmfTraceUtils.getAnalysisModulesOfClass(trace, GraphBuilderModuleStub.class)) {
index 9e27b90d4d1eb8b52bc5b92da91ffbc801876bce..2000417cedf3df64642568f6e12b4b75d4ed3dd1 100644 (file)
 
 package org.eclipse.tracecompass.tmf.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.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.internal.tmf.core.TmfCoreTracer;
 import org.osgi.framework.BundleContext;
 
@@ -81,4 +88,28 @@ public class TmfCoreTestPlugin extends Plugin {
         super.stop(context);
     }
 
+    /**
+     * Return a path to a file relative to this plugin's base directory
+     *
+     * @param relativePath
+     *            The path relative to the plugin's root directory
+     * @return The path corresponding to the relative path in parameter
+     */
+    public static @NonNull IPath getAbsoluteFilePath(String relativePath) {
+        Plugin plugin = TmfCoreTestPlugin.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(), new Path(relativePath), null);
+        try {
+            return new Path(FileLocator.toFileURL(location).getPath());
+        } catch (IOException e) {
+            throw new IllegalStateException();
+        }
+    }
+
 }
index d3abb8c22751f764edce72099b73535ebacdb4db..14fb960654ca1a6dd3a49f3b81b3e088f592bb78 100644 (file)
@@ -18,21 +18,13 @@ 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 org.eclipse.core.runtime.FileLocator;
-import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Path;
-import org.eclipse.core.runtime.Plugin;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
-import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect;
-import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
 import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
@@ -49,33 +41,15 @@ import org.junit.Test;
  */
 public class XmlStubTraceTest {
 
-    private static final Path VALID_FILE = new Path("testfiles/stub_xml_traces/valid/test.xml");
-    private static final Path VALID_PATH = new Path("testfiles/stub_xml_traces/valid");
-    private static final Path INVALID_PATH = new Path("testfiles/stub_xml_traces/invalid");
+    private static final String VALID_FILE = "testfiles/stub_xml_traces/valid/test.xml";
+    private static final String VALID_PATH = "testfiles/stub_xml_traces/valid";
+    private static final String INVALID_PATH = "testfiles/stub_xml_traces/invalid";
 
     private static final String EVENT_A = "A";
     private static final String EVENT_B = "B";
     private static final String FIELD_A = "b";
     private static final String FIELD_B = "f";
 
-    private static IPath getAbsolutePath(Path relativePath) {
-        Plugin plugin = TmfCoreTestPlugin.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();
-        }
-    }
-
     /**
      * Test the
      * {@link TmfXmlTraceStub#validate(org.eclipse.core.resources.IProject, String)}
@@ -84,13 +58,13 @@ public class XmlStubTraceTest {
     @Test
     public void testValidate() {
         TmfXmlTraceStub trace = new TmfXmlTraceStub();
-        File[] invalidFiles = getAbsolutePath(INVALID_PATH).toFile().listFiles();
+        File[] invalidFiles = TmfCoreTestPlugin.getAbsoluteFilePath(INVALID_PATH).toFile().listFiles();
         assertTrue(invalidFiles.length > 0);
         for (File f : invalidFiles) {
             assertTrue(!trace.validate(null, f.getAbsolutePath()).isOK());
         }
 
-        File[] validFiles = getAbsolutePath(VALID_PATH).toFile().listFiles();
+        File[] validFiles = TmfCoreTestPlugin.getAbsoluteFilePath(VALID_PATH).toFile().listFiles();
         assertTrue(validFiles.length > 0);
         for (File f : validFiles) {
             assertTrue(trace.validate(null, f.getAbsolutePath()).isOK());
@@ -103,17 +77,7 @@ public class XmlStubTraceTest {
      */
     @Test
     public void testDevelopmentTrace() {
-        TmfXmlTraceStub trace = new TmfXmlTraceStub();
-        IStatus status = trace.validate(null, getAbsolutePath(VALID_FILE).toOSString());
-        if (!status.isOK()) {
-            fail(status.getException().getMessage());
-        }
-
-        try {
-            trace.initTrace(null, getAbsolutePath(VALID_FILE).toOSString(), TmfEvent.class);
-        } catch (TmfTraceException e1) {
-            fail(e1.getMessage());
-        }
+        TmfXmlTraceStub trace = TmfXmlTraceStub.setupTrace(TmfCoreTestPlugin.getAbsoluteFilePath(VALID_FILE));
 
         CustomEventRequest req = new CustomEventRequest(trace);
         trace.sendRequest(req);
@@ -133,17 +97,7 @@ public class XmlStubTraceTest {
      */
     @Test
     public void testAspects() {
-        TmfXmlTraceStub trace = new TmfXmlTraceStub();
-        IStatus status = trace.validate(null, getAbsolutePath(VALID_FILE).toOSString());
-        if (!status.isOK()) {
-            fail(status.getException().getMessage());
-        }
-
-        try {
-            trace.initTrace(null, getAbsolutePath(VALID_FILE).toOSString(), TmfEvent.class);
-        } catch (TmfTraceException e1) {
-            fail(e1.getMessage());
-        }
+        TmfXmlTraceStub trace = TmfXmlTraceStub.setupTrace(TmfCoreTestPlugin.getAbsoluteFilePath(VALID_FILE));
 
         ITmfEventAspect<?> cpuAspect = null;
         ITmfEventAspect<?> testAspect = null;
index b2bb7a3fbd82e24ef230c51d7b1e2b5464f521a3..b03109400a76d7f3726037c7ba0d821d94d2c68d 100644 (file)
@@ -14,6 +14,7 @@
 package org.eclipse.tracecompass.tmf.tests.stubs.trace.xml;
 
 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.IOException;
@@ -31,6 +32,7 @@ import javax.xml.validation.Validator;
 
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jdt.annotation.Nullable;
@@ -102,6 +104,27 @@ public class TmfXmlTraceStub extends TmfTrace {
     private Collection<ITmfEventAspect<?>> fAspects = TmfTrace.BASE_ASPECTS;
     private final Collection<ITmfEventAspect<?>> fAdditionalAspects = new HashSet<>();
 
+    /**
+     * Validate and initialize a {@link TmfXmlTraceStub} object
+     *
+     * @param absolutePath
+     *            The absolute file path of the trace file
+     * @return The trace
+     */
+    public static TmfXmlTraceStub setupTrace(IPath absolutePath) {
+        TmfXmlTraceStub trace = new TmfXmlTraceStub();
+        IStatus status = trace.validate(null, absolutePath.toOSString());
+        if (!status.isOK()) {
+            fail(status.getException().getMessage());
+        }
+        try {
+            trace.initTrace(null, absolutePath.toOSString(), TmfEvent.class);
+        } catch (TmfTraceException e) {
+            fail(e.getMessage());
+        }
+        return trace;
+    }
+
     /**
      * Constructor. Constructs the custom XML trace with the appropriate
      * definition.
This page took 0.030087 seconds and 5 git commands to generate.