From: Francis Giraldeau Date: Tue, 30 Jun 2015 02:15:03 +0000 (-0400) Subject: Analysis: Add unit tests for dependency graph builder module X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=e5b213336de87033f19c09d10229fc5a385a8a43;p=deliverable%2Ftracecompass.git Analysis: Add unit tests for dependency graph builder module Change-Id: I214e1539b93705789bbb04e007ad893383d5cbbd Signed-off-by: Geneviève Bastien Signed-off-by: Francis Giraldeau Reviewed-on: https://git.eclipse.org/r/51080 Reviewed-by: Hudson CI Reviewed-by: Matthew Khouzam Tested-by: Matthew Khouzam --- diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/build.properties b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/build.properties index 182296cc96..96deafb4ee 100644 --- a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/build.properties +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/build.properties @@ -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 index 0000000000..fb874b74aa --- /dev/null +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/plugin.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/graph/AllTests.java b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/graph/AllTests.java index b6d9e2fdc7..8b29f0cde7 100644 --- a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/graph/AllTests.java +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/graph/AllTests.java @@ -20,7 +20,8 @@ import org.junit.runners.Suite; */ @RunWith(Suite.class) @Suite.SuiteClasses({ - TmfGraphTest.class + TmfGraphTest.class, + TmfGraphBuilderModuleTest.class }) public class AllTests { 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 index 0000000000..f5f0570369 --- /dev/null +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/graph/TmfGraphBuilderModuleTest.java @@ -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: + * + *
+     *           0  1  2  3  4  5  6  7  8  9  10  11  12  13
+     * Player 1     *--*        *-----*                *
+     *                 |        |                      |
+     * Player 2        *--------*               *------*
+     * 
+ * + * @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 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 index 0000000000..b821e06b44 --- /dev/null +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/stubs/org/eclipse/tracecompass/analysis/graph/core/tests/stubs/module/GraphBuilderModuleStub.java @@ -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 index 0000000000..5c8a99d1db --- /dev/null +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/stubs/org/eclipse/tracecompass/analysis/graph/core/tests/stubs/module/GraphProviderStub.java @@ -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 index 0000000000..c616813f7e --- /dev/null +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/stubs/org/eclipse/tracecompass/analysis/graph/core/tests/stubs/package-info.java @@ -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 index 0000000000..5e43b8a575 --- /dev/null +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/testfiles/stubtrace.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file