f5f057036968d8e568169ea30409c4009d3dd26d
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.graph.core.tests / src / org / eclipse / tracecompass / analysis / graph / core / tests / graph / TmfGraphBuilderModuleTest.java
1 /*******************************************************************************
2 * Copyright (c) 2015 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9
10 package org.eclipse.tracecompass.analysis.graph.core.tests.graph;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.fail;
15
16 import java.util.List;
17
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.tracecompass.analysis.graph.core.base.TmfGraph;
21 import org.eclipse.tracecompass.analysis.graph.core.base.TmfVertex;
22 import org.eclipse.tracecompass.analysis.graph.core.base.TmfVertex.EdgeDirection;
23 import org.eclipse.tracecompass.analysis.graph.core.building.TmfGraphBuilderModule;
24 import org.eclipse.tracecompass.analysis.graph.core.tests.Activator;
25 import org.eclipse.tracecompass.analysis.graph.core.tests.stubs.TestGraphWorker;
26 import org.eclipse.tracecompass.analysis.graph.core.tests.stubs.module.GraphBuilderModuleStub;
27 import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
28 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
29 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
30 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
31 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
32 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
33 import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
34 import org.junit.Test;
35
36 /**
37 * Test suite for the {@link TmfGraphBuilderModule} class
38 *
39 * @author Geneviève Bastien
40 * @author Francis Giraldeau
41 */
42 public class TmfGraphBuilderModuleTest {
43
44 private static final String STUB_TRACE_FILE = "testfiles/stubtrace.xml";
45
46 /**
47 * With this trace, the resulting graph should look like this:
48 *
49 * <pre>
50 * 0 1 2 3 4 5 6 7 8 9 10 11 12 13
51 * Player 1 *--* *-----* *
52 * | | |
53 * Player 2 *--------* *------*
54 * </pre>
55 *
56 * @return
57 */
58 private TmfGraphBuilderModule getModule() {
59 ITmfTrace trace = new TmfXmlTraceStub();
60 IPath filePath = Activator.getAbsoluteFilePath(STUB_TRACE_FILE);
61 IStatus status = trace.validate(null, filePath.toOSString());
62 if (!status.isOK()) {
63 fail(status.getException().getMessage());
64 }
65 try {
66 trace.initTrace(null, filePath.toOSString(), TmfEvent.class);
67 } catch (TmfTraceException e) {
68 fail(e.getMessage());
69 }
70 ((TmfTrace) trace).traceOpened(new TmfTraceOpenedSignal(this, trace, null));
71 GraphBuilderModuleStub module = null;
72 for (GraphBuilderModuleStub mod : TmfTraceUtils.getAnalysisModulesOfClass(trace, GraphBuilderModuleStub.class)) {
73 module = mod;
74 }
75 assertNotNull(module);
76
77 return module;
78 }
79
80 /**
81 * Test the graph builder execution
82 */
83 @Test
84 public void testBuildGraph() {
85
86 TmfGraphBuilderModule module = getModule();
87 module.schedule();
88 module.waitForCompletion();
89
90 TmfGraph graph = module.getGraph();
91 assertNotNull(graph);
92
93 assertEquals(2, graph.getWorkers().size());
94 assertEquals(9, graph.size());
95
96 List<TmfVertex> vertices = graph.getNodesOf(new TestGraphWorker(1));
97 assertEquals(5, vertices.size());
98
99 long timestamps1[] = { 1, 2, 5, 7, 12 };
100 boolean hasEdges1[][] = {
101 { false, true, false, false },
102 { true, false, false, true },
103 { false, true, true, false },
104 { true, false, false, false},
105 { false, false, true, false} };
106 for (int i = 0; i < vertices.size(); i++) {
107 TmfVertex v = vertices.get(i);
108 assertEquals(timestamps1[i], v.getTs());
109 assertEquals(hasEdges1[i][0], v.getEdge(EdgeDirection.INCOMING_HORIZONTAL_EDGE) != null);
110 assertEquals(hasEdges1[i][1], v.getEdge(EdgeDirection.OUTGOING_HORIZONTAL_EDGE) != null);
111 assertEquals(hasEdges1[i][2], v.getEdge(EdgeDirection.INCOMING_VERTICAL_EDGE) != null);
112 assertEquals(hasEdges1[i][3], v.getEdge(EdgeDirection.OUTGOING_VERTICAL_EDGE) != null);
113 }
114
115 vertices = graph.getNodesOf(new TestGraphWorker(2));
116 assertEquals(4, vertices.size());
117
118 long timestamps2[] = { 2, 5, 10, 12 };
119 boolean hasEdges2[][] = {
120 { false, true, true, false },
121 { true, false, false, true },
122 { false, true, false, false },
123 { true, false, false, true} };
124 for (int i = 0; i < vertices.size(); i++) {
125 TmfVertex v = vertices.get(i);
126 assertEquals(timestamps2[i], v.getTs());
127 assertEquals(hasEdges2[i][0], v.getEdge(EdgeDirection.INCOMING_HORIZONTAL_EDGE) != null);
128 assertEquals(hasEdges2[i][1], v.getEdge(EdgeDirection.OUTGOING_HORIZONTAL_EDGE) != null);
129 assertEquals(hasEdges2[i][2], v.getEdge(EdgeDirection.INCOMING_VERTICAL_EDGE) != null);
130 assertEquals(hasEdges2[i][3], v.getEdge(EdgeDirection.OUTGOING_VERTICAL_EDGE) != null);
131 }
132
133 }
134
135 }
This page took 0.047589 seconds and 4 git commands to generate.