From 142305b744296569a31f6052bf34da7a4f8afbae Mon Sep 17 00:00:00 2001 From: Magnus Teekivi Date: Sat, 3 Apr 2021 11:28:15 +0300 Subject: [PATCH] Add and test SimpleProfiler.createFromExecutionPath --- .../core/statistics/SimpleProfiler.java | 17 +++++++++ .../core/machine/ReplayMachineTest.java | 35 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/graphwalker-core/src/main/java/org/graphwalker/core/statistics/SimpleProfiler.java b/graphwalker-core/src/main/java/org/graphwalker/core/statistics/SimpleProfiler.java index 0a4305030..2253938f6 100644 --- a/graphwalker-core/src/main/java/org/graphwalker/core/statistics/SimpleProfiler.java +++ b/graphwalker-core/src/main/java/org/graphwalker/core/statistics/SimpleProfiler.java @@ -200,4 +200,21 @@ public List getProfiles() { public Profile getProfile(Context context, Element element) { return new Profile(context, element, executions.get(context).get(element)); } + + public static SimpleProfiler createFromExecutionPath(List executionPath) { + SimpleProfiler profiler = new SimpleProfiler(); + for (Execution execution : executionPath) { + Context context = execution.getContext(); + if (!profiler.executions.containsKey(context)) { + profiler.executions.put(context, new HashMap<>()); + } + Element element = execution.getElement(); + if (!profiler.executions.get(context).containsKey(element)) { + profiler.executions.get(context).put(element, new ArrayList<>()); + } + profiler.executionPath.add(execution); + profiler.executions.get(context).get(element).add(execution); + } + return profiler; + } } diff --git a/graphwalker-core/src/test/java/org/graphwalker/core/machine/ReplayMachineTest.java b/graphwalker-core/src/test/java/org/graphwalker/core/machine/ReplayMachineTest.java index fc97e041a..6293e3bf5 100644 --- a/graphwalker-core/src/test/java/org/graphwalker/core/machine/ReplayMachineTest.java +++ b/graphwalker-core/src/test/java/org/graphwalker/core/machine/ReplayMachineTest.java @@ -27,14 +27,18 @@ */ import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertThat; import org.graphwalker.core.condition.EdgeCoverage; import org.graphwalker.core.generator.RandomPath; import org.graphwalker.core.model.*; import org.graphwalker.core.statistics.Execution; +import org.graphwalker.core.statistics.SimpleProfiler; import org.junit.Test; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -57,6 +61,37 @@ public void replayMachine() throws Exception { assertThat(replayedPath, is(expectedPath)); } + @Test + public void replayMachineFromExecutionPath() { + Machine machine = createMachineExecution(); + List executionPath = new ArrayList<>(machine.getProfiler().getExecutionPath()); + Machine replayMachine = new ReplayMachine(SimpleProfiler.createFromExecutionPath(executionPath)); + while (replayMachine.hasNextStep()) { + replayMachine.getNextStep(); + } + List expectedPath = machine.getProfiler().getExecutionPath().stream() + .map(Execution::getElement).collect(Collectors.toList()); + List replayedPath = replayMachine.getProfiler().getExecutionPath().stream() + .map(Execution::getElement).collect(Collectors.toList()); + assertEquals(expectedPath, replayedPath); + } + + @Test + public void replayMachineFromAlteredExecutionPath() { + Machine machine = createMachineExecution(); + List executionPath = new ArrayList<>(machine.getProfiler().getExecutionPath()); + executionPath.remove(executionPath.size() - 1); + Machine replayMachine = new ReplayMachine(SimpleProfiler.createFromExecutionPath(executionPath)); + while (replayMachine.hasNextStep()) { + replayMachine.getNextStep(); + } + List originalPath = machine.getProfiler().getExecutionPath().stream() + .map(Execution::getElement).collect(Collectors.toList()); + List replayedPath = replayMachine.getProfiler().getExecutionPath().stream() + .map(Execution::getElement).collect(Collectors.toList()); + assertNotEquals(originalPath, replayedPath); + } + private Machine createMachineExecution() { Vertex vertex = new Vertex(); Edge edge1 = new Edge().setSourceVertex(vertex).setTargetVertex(vertex).addAction(new Action("flag = true;")).setName("edge1");