From 56e9f8300c5890af5940afd1aee3290b5678cb9e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Genevi=C3=A8ve=20Bastien?= Date: Mon, 22 Sep 2014 15:32:46 -0400 Subject: [PATCH] TMF: Address comments on the trace stub for unit test patch MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Some comments were mentioned in patch https://git.eclipse.org/r/#/c/21265 They are addressed in this patch. Change-Id: I6dc940d5a5fcb7e920c934ce0b6a86adc8fbf860 Signed-off-by: Geneviève Bastien Reviewed-on: https://git.eclipse.org/r/33718 Tested-by: Hudson CI Reviewed-by: Marc-Andre Laperle Tested-by: Marc-Andre Laperle --- .../tests/trace/stub/XmlStubTraceTest.java | 50 ++++++++++++++----- .../stubs/trace/xml/TmfXmlTraceStub.java | 26 ++++++++-- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/stub/XmlStubTraceTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/stub/XmlStubTraceTest.java index 2147a8d4c9..5705e938a9 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/stub/XmlStubTraceTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/stub/XmlStubTraceTest.java @@ -17,16 +17,22 @@ 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.linuxtools.internal.tmf.core.Activator; import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; import org.eclipse.linuxtools.tmf.core.event.TmfEvent; import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; +import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin; import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; import org.eclipse.linuxtools.tmf.tests.stubs.trace.xml.TmfXmlTraceStub; @@ -39,15 +45,33 @@ import org.junit.Test; */ public class XmlStubTraceTest { - private static final String VALID_FILE = "../org.eclipse.linuxtools.tmf.core.tests/testfiles/stub_xml_traces/valid/test.xml"; - private static final String VALID_PATH = "../org.eclipse.linuxtools.tmf.core.tests/testfiles/stub_xml_traces/valid"; - private static final String INVALID_PATH = "../org.eclipse.linuxtools.tmf.core.tests/testfiles/stub_xml_traces/invalid"; + 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 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)} @@ -56,13 +80,13 @@ public class XmlStubTraceTest { @Test public void testValidate() { TmfXmlTraceStub trace = new TmfXmlTraceStub(); - File[] invalidFiles = (new File(INVALID_PATH)).listFiles(); + File[] invalidFiles = getAbsolutePath(INVALID_PATH).toFile().listFiles(); assertTrue(invalidFiles.length > 0); for (File f : invalidFiles) { assertTrue(!trace.validate(null, f.getAbsolutePath()).isOK()); } - File[] validFiles = (new File(VALID_PATH)).listFiles(); + File[] validFiles = getAbsolutePath(VALID_PATH).toFile().listFiles(); assertTrue(validFiles.length > 0); for (File f : validFiles) { assertTrue(trace.validate(null, f.getAbsolutePath()).isOK()); @@ -76,13 +100,13 @@ public class XmlStubTraceTest { @Test public void testDevelopmentTrace() { TmfXmlTraceStub trace = new TmfXmlTraceStub(); - IStatus status = trace.validate(null, VALID_FILE); + IStatus status = trace.validate(null, getAbsolutePath(VALID_FILE).toOSString()); if (!status.isOK()) { fail(status.getException().getMessage()); } try { - trace.initTrace(null, VALID_FILE, TmfEvent.class); + trace.initTrace(null, getAbsolutePath(VALID_FILE).toOSString(), TmfEvent.class); } catch (TmfTraceException e1) { fail(e1.getMessage()); } @@ -105,25 +129,25 @@ public class XmlStubTraceTest { case EVENT_A: { ITmfEventField content = event.getContent(); if (!event.getSource().equals("1")) { - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Events of type A should have source 1 but was " + event.getSource()); + return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, "Events of type A should have source 1 but was " + event.getSource()); } if (content.getField(FIELD_A) == null) { - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_A, EVENT_A)); + return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_A, EVENT_A)); } break; } case EVENT_B: { ITmfEventField content = event.getContent(); if (!event.getSource().equals("2")) { - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Events of type B should have source 2 but was " + event.getSource()); + return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, "Events of type B should have source 2 but was " + event.getSource()); } if (content.getField(FIELD_B) == null) { - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_B, EVENT_B)); + return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_B, EVENT_B)); } break; } default: - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unexpected event " + event.getType().getName()); + return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, "Unexpected event " + event.getType().getName()); } return Status.OK_STATUS; } diff --git a/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java b/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java index d356e04980..47e7e89330 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java +++ b/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java @@ -28,6 +28,7 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.linuxtools.internal.tmf.core.Activator; import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; @@ -163,6 +164,18 @@ public class TmfXmlTraceStub extends TmfTrace { return Status.OK_STATUS; } + private static String getStringValue(@NonNull ITmfEventField content, String fieldName) { + ITmfEventField field = content.getField(fieldName); + if (field == null) { + return EMPTY; + } + Object val = field.getValue(); + if (!(val instanceof String)) { + return EMPTY; + } + return (String) val; + } + @Override public synchronized ITmfEvent getNext(ITmfContext context) { final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank()); @@ -176,9 +189,12 @@ public class TmfXmlTraceStub extends TmfTrace { /* The "values" field contains a | separated list of field values */ /* the "type" field contains a | separated list of field types */ ITmfEventField content = event.getContent(); - String fieldString = (String) content.getField(FIELD_NAMES_FIELD).getValue(); - String valueString = (String) content.getField(VALUES_FIELD).getValue(); - String typeString = (String) content.getField(TYPES_FIELD).getValue(); + if (content == null) { + return null; + } + String fieldString = getStringValue(content, FIELD_NAMES_FIELD); + String valueString = getStringValue(content, VALUES_FIELD); + String typeString = getStringValue(content, TYPES_FIELD); String[] fields = fieldString.split(VALUES_SEPARATOR); String[] values = valueString.split(VALUES_SEPARATOR); @@ -224,9 +240,9 @@ public class TmfXmlTraceStub extends TmfTrace { /* Create a new event with new fields and name */ ITmfEventType customEventType = event.getType(); - TmfEventType eventType = new TmfEventType(customEventType.getContext(), (String) content.getField(EVENT_NAME_FIELD).getValue(), customEventType.getRootField()); + TmfEventType eventType = new TmfEventType(customEventType.getContext(), getStringValue(content, EVENT_NAME_FIELD), customEventType.getRootField()); ITmfEventField eventFields = new CustomEventContent(content.getName(), content.getValue(), fieldsArray); - TmfEvent newEvent = new TmfEvent(this, event.getTimestamp(), (String) content.getField(SOURCE_FIELD).getValue(), eventType, eventFields, event.getReference()); + TmfEvent newEvent = new TmfEvent(this, event.getTimestamp(), getStringValue(content, SOURCE_FIELD), eventType, eventFields, event.getReference()); updateAttributes(savedContext, event.getTimestamp()); context.increaseRank(); -- 2.34.1