TMF: Address comments on the trace stub for unit test patch
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Mon, 22 Sep 2014 19:32:46 +0000 (15:32 -0400)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Wed, 24 Sep 2014 13:36:45 +0000 (09:36 -0400)
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 <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/33718
Tested-by: Hudson CI
Reviewed-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Tested-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/stub/XmlStubTraceTest.java
org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java

index 2147a8d4c960357722923925b476b73f59739680..5705e938a9cdeef253a9b48411517b3edc03e194 100644 (file)
@@ -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;
     }
index d356e0498074dd6731824212bfd09937f5772b18..47e7e893302ddd09a54bd79002b6cee153ac1ae2 100644 (file)
@@ -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();
 
This page took 0.02969 seconds and 5 git commands to generate.