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;
*/
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)}
@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());
@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());
}
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;
}
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;
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());
/* 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);
/* 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();