timing.core: Add local statistics to the latency statistics
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / stubs / org / eclipse / tracecompass / tmf / tests / stubs / trace / xml / TmfXmlTraceStub.java
index 7b91eeafacd2b11895ee35d0104b64ab709c6768..af595419c838e1d081581a434b89a8b072cea2d3 100644 (file)
 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;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.Collection;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.stream.StreamSupport;
 
 import javax.xml.XMLConstants;
 import javax.xml.transform.Source;
@@ -30,12 +34,16 @@ 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.DefaultLocation;
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.osgi.util.NLS;
 import org.eclipse.tracecompass.internal.tmf.core.Activator;
+import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEventType;
@@ -43,6 +51,7 @@ import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
 import org.eclipse.tracecompass.tmf.core.event.TmfEventType;
 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
+import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects;
 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfContentFieldAspect;
 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect;
 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
@@ -52,7 +61,7 @@ import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace;
 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition;
 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
-import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
+import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
@@ -62,6 +71,7 @@ import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
 import org.xml.sax.SAXException;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
 
 /**
  * An XML development trace using a custom XML trace definition and schema.
@@ -99,7 +109,30 @@ public class TmfXmlTraceStub extends TmfTrace {
     private final CustomXmlTraceDefinition fDefinition;
     private CustomXmlTrace fTrace;
 
-    private Collection<ITmfEventAspect> fAspects = TmfTrace.BASE_ASPECTS;
+    private Collection<ITmfEventAspect<?>> fAspects = TmfTrace.BASE_ASPECTS;
+    private final Collection<ITmfEventAspect<?>> fAdditionalAspects = new HashSet<>();
+    private final Collection<IAnalysisModule> fAdditionalModules = 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
@@ -130,6 +163,7 @@ public class TmfXmlTraceStub extends TmfTrace {
     }
 
     @Override
+    @NonNullByDefault({DefaultLocation.TYPE_ARGUMENT})
     public void initTrace(@Nullable IResource resource, @Nullable String path, @Nullable Class<? extends ITmfEvent> type) throws TmfTraceException {
         super.initTrace(resource, path, type);
         ITmfContext ctx;
@@ -213,10 +247,7 @@ public class TmfXmlTraceStub extends TmfTrace {
         } catch (IOException e) {
             return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_IoError, path), e);
         }
-        @SuppressWarnings("null")
-        @NonNull
-        IStatus status = Status.OK_STATUS;
-        return status;
+        return Status.OK_STATUS;
     }
 
     private static String getStringValue(ITmfEventField content, String fieldName) {
@@ -297,7 +328,10 @@ public class TmfXmlTraceStub extends TmfTrace {
             fieldsArray[i] = new TmfEventField(checkNotNull(fields[i]), val, null);
         }
 
-        /* Generate the aspects for this trace if it is the 'set_aspects' definition */
+        /*
+         * Generate the aspects for this trace if it is the 'set_aspects'
+         * definition
+         */
         if (fTrace.getDefinition() != fDefinition) {
             generateAspects(fieldsArray);
             return null;
@@ -314,23 +348,23 @@ public class TmfXmlTraceStub extends TmfTrace {
          * original is in second and we need to convert it. We should do that at
          * the source when it is supported
          */
-        ITmfTimestamp timestamp = new TmfNanoTimestamp(event.getTimestamp().getValue() / SECONDS_TO_NS);
+        ITmfTimestamp timestamp = TmfTimestamp.fromNanos(event.getTimestamp().getValue() / SECONDS_TO_NS);
         TmfEvent newEvent = new TmfEvent(this, ITmfContext.UNKNOWN_RANK, timestamp, eventType, eventFields);
         updateAttributes(savedContext, event);
         return newEvent;
     }
 
     private void generateAspects(ITmfEventField[] fieldsArray) {
-        ImmutableList.Builder<ITmfEventAspect> builder = new ImmutableList.Builder<>();
+        ImmutableList.Builder<ITmfEventAspect<?>> builder = new ImmutableList.Builder<>();
 
         /* Initialize the first default trace aspects */
-        builder.add(ITmfEventAspect.BaseAspects.TIMESTAMP);
-        builder.add(ITmfEventAspect.BaseAspects.EVENT_TYPE);
+        builder.add(TmfBaseAspects.getTimestampAspect());
+        builder.add(TmfBaseAspects.getEventTypeAspect());
 
         /* Add custom aspects in between */
         for (ITmfEventField field : fieldsArray) {
             String name = field.getName();
-            final ITmfEventAspect aspect = new TmfContentFieldAspect(name, name);
+            final ITmfEventAspect<?> aspect = new TmfContentFieldAspect(name, name);
             if (name.equals(ASPECT_CPU)) {
                 builder.add(new TmfCpuAspect() {
                     @Override
@@ -348,13 +382,52 @@ public class TmfXmlTraceStub extends TmfTrace {
         }
 
         /* Add the big content aspect */
-        builder.add(ITmfEventAspect.BaseAspects.CONTENTS);
+        builder.add(TmfBaseAspects.getContentsAspect());
+        /* Add the additional aspects */
+        builder.addAll(fAdditionalAspects);
         fAspects = builder.build();
     }
 
     @Override
-    public Iterable<ITmfEventAspect> getEventAspects() {
+    public Iterable<ITmfEventAspect<?>> getEventAspects() {
         return fAspects;
     }
 
+    /**
+     * Adds a new event aspect to this type of trace. Since this trace type is
+     * used to build custom traces that mimic the behavior of real traces, the
+     * required aspects may be missing and this method allows to add them. This
+     * method should be called before calling
+     * {@link #initTrace(IResource, String, Class)} otherwise the additional
+     * aspects will not be picked up when generating the aspects.
+     *
+     * @param aspect
+     *            The aspect to have
+     */
+    public void addEventAspect(ITmfEventAspect<?> aspect) {
+        fAdditionalAspects.add(aspect);
+    }
+
+    /**
+     * Add an additional new module
+     *
+     * @param module
+     *            The new module
+     */
+    public void addAnalysisModule(IAnalysisModule module) {
+        fAdditionalModules.add(module);
+    }
+
+    @Override
+    public Iterable<@NonNull IAnalysisModule> getAnalysisModules() {
+        @NonNull Iterable<IAnalysisModule> modules = super.getAnalysisModules();
+        return checkNotNull(Iterables.concat(modules, fAdditionalModules));
+    }
+
+    @Override
+    public @Nullable IAnalysisModule getAnalysisModule(@Nullable String analysisId) {
+        Iterable<@NonNull IAnalysisModule> modules = getAnalysisModules();
+        Optional<IAnalysisModule> opt = StreamSupport.stream(modules.spliterator(), false).filter(analysis -> analysis.getId().equals(analysisId)).findFirst();
+        return opt.isPresent() ? opt.get() : null;
+    }
 }
This page took 0.028471 seconds and 5 git commands to generate.