From ec52ed4abd76a6530a713d9e4a9ca92a5a70dee3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Genevi=C3=A8ve=20Bastien?= Date: Mon, 4 Apr 2016 15:58:40 -0400 Subject: [PATCH] tmf.core.tests: Add method to simplify XML Trace stub setup MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I7c59f26d0bf89b85aa648a452641d3b3edae59b2 Signed-off-by: Geneviève Bastien Reviewed-on: https://git.eclipse.org/r/70034 Reviewed-by: Hudson CI Reviewed-by: Bernd Hufmann Tested-by: Bernd Hufmann --- .../analysis/graph/core/tests/Activator.java | 3 +- .../graph/TmfGraphBuilderModuleTest.java | 17 +----- .../tmf/core/tests/TmfCoreTestPlugin.java | 31 ++++++++++ .../tests/trace/stub/XmlStubTraceTest.java | 60 +++---------------- .../stubs/trace/xml/TmfXmlTraceStub.java | 23 +++++++ 5 files changed, 64 insertions(+), 70 deletions(-) diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/Activator.java b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/Activator.java index 44dcc43094..828eb6863f 100644 --- a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/Activator.java +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/Activator.java @@ -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) { /* diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/graph/TmfGraphBuilderModuleTest.java b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/graph/TmfGraphBuilderModuleTest.java index f5f0570369..a9540d5d6c 100644 --- a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/graph/TmfGraphBuilderModuleTest.java +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/graph/TmfGraphBuilderModuleTest.java @@ -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)) { diff --git a/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/TmfCoreTestPlugin.java b/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/TmfCoreTestPlugin.java index 9e27b90d4d..2000417ced 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/TmfCoreTestPlugin.java +++ b/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/TmfCoreTestPlugin.java @@ -12,7 +12,14 @@ 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(); + } + } + } diff --git a/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/stub/XmlStubTraceTest.java b/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/stub/XmlStubTraceTest.java index d3abb8c227..14fb960654 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/stub/XmlStubTraceTest.java +++ b/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/stub/XmlStubTraceTest.java @@ -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; diff --git a/tmf/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java b/tmf/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java index b2bb7a3fbd..b03109400a 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java +++ b/tmf/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java @@ -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> fAspects = TmfTrace.BASE_ASPECTS; private final Collection> 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. -- 2.34.1