Analysis: Add unit tests for dependency graph builder module
authorFrancis Giraldeau <francis.giraldeau@gmail.com>
Tue, 30 Jun 2015 02:15:03 +0000 (22:15 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Mon, 14 Sep 2015 19:20:02 +0000 (15:20 -0400)
Change-Id: I214e1539b93705789bbb04e007ad893383d5cbbd
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Signed-off-by: Francis Giraldeau <francis.giraldeau@gmail.com>
Reviewed-on: https://git.eclipse.org/r/51080
Reviewed-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
analysis/org.eclipse.tracecompass.analysis.graph.core.tests/build.properties
analysis/org.eclipse.tracecompass.analysis.graph.core.tests/plugin.xml [new file with mode: 0644]
analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/graph/AllTests.java
analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/graph/TmfGraphBuilderModuleTest.java [new file with mode: 0644]
analysis/org.eclipse.tracecompass.analysis.graph.core.tests/stubs/org/eclipse/tracecompass/analysis/graph/core/tests/stubs/module/GraphBuilderModuleStub.java [new file with mode: 0644]
analysis/org.eclipse.tracecompass.analysis.graph.core.tests/stubs/org/eclipse/tracecompass/analysis/graph/core/tests/stubs/module/GraphProviderStub.java [new file with mode: 0644]
analysis/org.eclipse.tracecompass.analysis.graph.core.tests/stubs/org/eclipse/tracecompass/analysis/graph/core/tests/stubs/package-info.java [new file with mode: 0644]
analysis/org.eclipse.tracecompass.analysis.graph.core.tests/testfiles/stubtrace.xml [new file with mode: 0644]

index 182296cc96828bb089c672737abb1fedbe38741b..96deafb4eebc3aef64dc9227a05801db484f8cad 100644 (file)
@@ -15,6 +15,7 @@ source.. = src/,\
 output.. = bin/
 bin.includes = META-INF/,\
                plugin.properties,\
-               .
+               .,\
+               plugin.xml
 additional.bundles = org.eclipse.jdt.annotation
 jars.extra.classpath = platform:/plugin/org.eclipse.jdt.annotation
diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/plugin.xml b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/plugin.xml
new file mode 100644 (file)
index 0000000..fb874b7
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<?eclipse version="3.4"?>
+<plugin>
+   <extension
+         point="org.eclipse.linuxtools.tmf.core.analysis">
+      <module
+            analysis_module="org.eclipse.tracecompass.analysis.graph.core.tests.stubs.module.GraphBuilderModuleStub"
+            automatic="false"
+            id="org.eclipse.linuxtools.tmf.analysis.graph.tests.stub"
+            name="Graph builder stub">
+         <tracetype
+               class="org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub">
+         </tracetype>
+      </module>
+   </extension>
+
+</plugin>
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
new file mode 100644 (file)
index 0000000..f5f0570
--- /dev/null
@@ -0,0 +1,135 @@
+/*******************************************************************************
+ * Copyright (c) 2015 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+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;
+import org.eclipse.tracecompass.analysis.graph.core.building.TmfGraphBuilderModule;
+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;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
+import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
+import org.junit.Test;
+
+/**
+ * Test suite for the {@link TmfGraphBuilderModule} class
+ *
+ * @author Geneviève Bastien
+ * @author Francis Giraldeau
+ */
+public class TmfGraphBuilderModuleTest {
+
+    private static final String STUB_TRACE_FILE = "testfiles/stubtrace.xml";
+
+    /**
+     * With this trace, the resulting graph should look like this:
+     *
+     * <pre>
+     *           0  1  2  3  4  5  6  7  8  9  10  11  12  13
+     * Player 1     *--*        *-----*                *
+     *                 |        |                      |
+     * Player 2        *--------*               *------*
+     * </pre>
+     *
+     * @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());
+        }
+        ((TmfTrace) trace).traceOpened(new TmfTraceOpenedSignal(this, trace, null));
+        GraphBuilderModuleStub module = null;
+        for (GraphBuilderModuleStub mod : TmfTraceUtils.getAnalysisModulesOfClass(trace, GraphBuilderModuleStub.class)) {
+            module = mod;
+        }
+        assertNotNull(module);
+
+        return module;
+    }
+
+    /**
+     * Test the graph builder execution
+     */
+    @Test
+    public void testBuildGraph() {
+
+        TmfGraphBuilderModule module = getModule();
+        module.schedule();
+        module.waitForCompletion();
+
+        TmfGraph graph = module.getGraph();
+        assertNotNull(graph);
+
+        assertEquals(2, graph.getWorkers().size());
+        assertEquals(9, graph.size());
+
+        List<TmfVertex> vertices = graph.getNodesOf(new TestGraphWorker(1));
+        assertEquals(5, vertices.size());
+
+        long timestamps1[] = { 1, 2, 5, 7, 12 };
+        boolean hasEdges1[][] = {
+                { false, true, false, false },
+                { true, false, false, true },
+                { false, true, true, false },
+                { true, false, false, false},
+                { false, false, true, false} };
+        for (int i = 0; i < vertices.size(); i++) {
+            TmfVertex v = vertices.get(i);
+            assertEquals(timestamps1[i], v.getTs());
+            assertEquals(hasEdges1[i][0], v.getEdge(EdgeDirection.INCOMING_HORIZONTAL_EDGE) != null);
+            assertEquals(hasEdges1[i][1], v.getEdge(EdgeDirection.OUTGOING_HORIZONTAL_EDGE) != null);
+            assertEquals(hasEdges1[i][2], v.getEdge(EdgeDirection.INCOMING_VERTICAL_EDGE) != null);
+            assertEquals(hasEdges1[i][3], v.getEdge(EdgeDirection.OUTGOING_VERTICAL_EDGE) != null);
+        }
+
+        vertices = graph.getNodesOf(new TestGraphWorker(2));
+        assertEquals(4, vertices.size());
+
+        long timestamps2[] = { 2, 5, 10, 12 };
+        boolean hasEdges2[][] = {
+                { false, true, true, false },
+                { true, false, false, true },
+                { false, true, false, false },
+                { true, false, false, true} };
+        for (int i = 0; i < vertices.size(); i++) {
+            TmfVertex v = vertices.get(i);
+            assertEquals(timestamps2[i], v.getTs());
+            assertEquals(hasEdges2[i][0], v.getEdge(EdgeDirection.INCOMING_HORIZONTAL_EDGE) != null);
+            assertEquals(hasEdges2[i][1], v.getEdge(EdgeDirection.OUTGOING_HORIZONTAL_EDGE) != null);
+            assertEquals(hasEdges2[i][2], v.getEdge(EdgeDirection.INCOMING_VERTICAL_EDGE) != null);
+            assertEquals(hasEdges2[i][3], v.getEdge(EdgeDirection.OUTGOING_VERTICAL_EDGE) != null);
+        }
+
+    }
+
+}
diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/stubs/org/eclipse/tracecompass/analysis/graph/core/tests/stubs/module/GraphBuilderModuleStub.java b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/stubs/org/eclipse/tracecompass/analysis/graph/core/tests/stubs/module/GraphBuilderModuleStub.java
new file mode 100644 (file)
index 0000000..b821e06
--- /dev/null
@@ -0,0 +1,36 @@
+/*******************************************************************************
+ * Copyright (c) 2015 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.analysis.graph.core.tests.stubs.module;
+
+import org.eclipse.tracecompass.analysis.graph.core.building.ITmfGraphProvider;
+import org.eclipse.tracecompass.analysis.graph.core.building.TmfGraphBuilderModule;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+
+/**
+ * Graph builder module stub
+ *
+ * @author Geneviève Bastien
+ * @author Francis Giraldeau
+ */
+public class GraphBuilderModuleStub extends TmfGraphBuilderModule {
+
+    /** The analysis id */
+    public static final String ANALYSIS_ID = "org.eclipse.linuxtools.tmf.analysis.graph.tests.stub";
+
+    @Override
+    protected ITmfGraphProvider getGraphProvider() {
+        ITmfTrace trace = getTrace();
+        if (trace == null) {
+            throw new NullPointerException();
+        }
+        return new GraphProviderStub(trace);
+    }
+
+}
diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/stubs/org/eclipse/tracecompass/analysis/graph/core/tests/stubs/module/GraphProviderStub.java b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/stubs/org/eclipse/tracecompass/analysis/graph/core/tests/stubs/module/GraphProviderStub.java
new file mode 100644 (file)
index 0000000..5c8a99d
--- /dev/null
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * Copyright (c) 2015 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.analysis.graph.core.tests.stubs.module;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.analysis.graph.core.base.IGraphWorker;
+import org.eclipse.tracecompass.analysis.graph.core.base.TmfEdge.EdgeType;
+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.building.AbstractTmfGraphProvider;
+import org.eclipse.tracecompass.analysis.graph.core.building.AbstractTraceEventHandler;
+import org.eclipse.tracecompass.analysis.graph.core.tests.stubs.TestGraphWorker;
+import org.eclipse.tracecompass.common.core.NonNullUtils;
+import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+
+/**
+ * Simple graph provider stub
+ *
+ * @author Geneviève Bastien
+ * @author Francis Giraldeau
+ */
+public class GraphProviderStub extends AbstractTmfGraphProvider {
+
+    /**
+     * Constructor
+     *
+     * @param trace
+     *            The trace
+     */
+    public GraphProviderStub(@NonNull ITmfTrace trace) {
+        super(trace, "Graph Provider Stub");
+        registerHandler(new StubEventHandler());
+    }
+
+    private class StubEventHandler extends AbstractTraceEventHandler {
+
+        @Override
+        public void handleEvent(ITmfEvent event) {
+            String evname = event.getType().getName();
+
+            TmfGraph graph = getAssignedGraph();
+            if (graph == null) {
+                throw new IllegalStateException();
+            }
+
+            switch (evname) {
+            case "take": {
+                IGraphWorker player = new TestGraphWorker(NonNullUtils.checkNotNull((Integer) event.getContent().getField("player").getValue()));
+                TmfVertex v = new TmfVertex(event.getTimestamp().getValue());
+                graph.add(player, v);
+                break;
+            }
+            case "pass": {
+                IGraphWorker playerFrom = new TestGraphWorker(NonNullUtils.checkNotNull((Integer) event.getContent().getField("from").getValue()));
+                IGraphWorker playerTo = new TestGraphWorker(NonNullUtils.checkNotNull((Integer) event.getContent().getField("to").getValue()));
+                TmfVertex vFrom = new TmfVertex(event.getTimestamp().getValue());
+                graph.append(playerFrom, vFrom);
+                TmfVertex vTo = new TmfVertex(event.getTimestamp().getValue());
+                graph.add(playerTo, vTo);
+                graph.link(vFrom, vTo, EdgeType.NETWORK);
+                break;
+            }
+            case "kick": {
+                IGraphWorker player = new TestGraphWorker(NonNullUtils.checkNotNull((Integer) event.getContent().getField("player").getValue()));
+                TmfVertex v = new TmfVertex(event.getTimestamp().getValue());
+                graph.append(player, v);
+                break;
+            }
+            default:
+                break;
+            }
+        }
+
+    }
+
+}
diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/stubs/org/eclipse/tracecompass/analysis/graph/core/tests/stubs/package-info.java b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/stubs/org/eclipse/tracecompass/analysis/graph/core/tests/stubs/package-info.java
new file mode 100644 (file)
index 0000000..c616813
--- /dev/null
@@ -0,0 +1,11 @@
+/*******************************************************************************
+ * Copyright (c) 2015 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+@org.eclipse.jdt.annotation.NonNullByDefault
+package org.eclipse.tracecompass.analysis.graph.core.tests.stubs;
\ No newline at end of file
diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/testfiles/stubtrace.xml b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/testfiles/stubtrace.xml
new file mode 100644 (file)
index 0000000..5e43b8a
--- /dev/null
@@ -0,0 +1,23 @@
+<trace>
+       <event timestamp="1" name="take">
+               <field name="player" value="1" type="int" />
+       </event>
+       <event timestamp="2" name="pass">
+               <field name="from" value="1" type="int" />
+               <field name="to" value="2" type="int" />
+       </event>
+       <event timestamp="5" name="pass">
+               <field name="from" value="2" type="int" />
+               <field name="to" value="1" type="int" />
+       </event>
+       <event timestamp="7" name="kick">
+               <field name="player" value="1" type="int" />
+       </event>
+       <event timestamp="10" name="take">
+               <field name="player" value="2" type="int" />
+       </event>
+       <event timestamp="12" name="pass">
+               <field name="from" value="2" type="int" />
+               <field name="to" value="1" type="int" />
+       </event>
+</trace>
\ No newline at end of file
This page took 0.030989 seconds and 5 git commands to generate.