From 8ab3306142317d7bb8e1a7dcb7c902d7549cc78d Mon Sep 17 00:00:00 2001 From: Vyacheslav Boyko Date: Thu, 14 Jul 2022 13:55:19 +0300 Subject: [PATCH] test to ensure that all state handlers are called only once --- src/test/java/me/bvn13/fsm/tests/FsmTest.java | 86 ++++++++++++++++++- 1 file changed, 83 insertions(+), 3 deletions(-) diff --git a/src/test/java/me/bvn13/fsm/tests/FsmTest.java b/src/test/java/me/bvn13/fsm/tests/FsmTest.java index f79ff1e..963e62a 100644 --- a/src/test/java/me/bvn13/fsm/tests/FsmTest.java +++ b/src/test/java/me/bvn13/fsm/tests/FsmTest.java @@ -7,6 +7,7 @@ import org.junit.Assert; import org.junit.Test; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; /** * Created by bvn13 on 28.12.2017. @@ -148,7 +149,7 @@ public class FsmTest { .withAfterHandler(fsm -> initAfter.set(true)) .withProcessor((fsm, event) -> initProcess.set(true)) .end() - .state("intermediate") + .state("intermediate-1") .withBeforeHandler(fsm -> intermediateBefore.set(true)) .withAfterHandler(fsm -> intermediateAfter.set(true)) .withProcessor((fsm, event) -> intermediateProcess.set(true)) @@ -160,11 +161,11 @@ public class FsmTest { .end() .withTransition() .from("init") - .to("intermediate") + .to("intermediate-1") .checking((fsm, event) -> true) .end() .withTransition() - .from("intermediate") + .from("intermediate-1") .to("finish") .checking((fsm, event) -> true) .end() @@ -188,4 +189,83 @@ public class FsmTest { } + @Test + public void newSyntaxCountingEveryStep() { + + Counter initCounter = new Counter(); + Counter firstIntermediateCounter = new Counter(); + Counter secondIntermediateCounter = new Counter(); + Counter finishCounter = new Counter(); + + // @formatter:off + + SimpleFsm simpleFsm = Fsm + ., String>from(SimpleFsm::new) + .withStates() + .from("init") + .withBeforeHandler(fsm -> initCounter.before.incrementAndGet()) + .withAfterHandler(fsm -> initCounter.after.incrementAndGet()) + .withProcessor((fsm, event) -> initCounter.process.incrementAndGet()) + .end() + .state("intermediate-1") + .withBeforeHandler(fsm -> firstIntermediateCounter.before.incrementAndGet()) + .withAfterHandler(fsm -> firstIntermediateCounter.after.incrementAndGet()) + .withProcessor((fsm, event) -> firstIntermediateCounter.process.incrementAndGet()) + .end() + .state("intermediate-2") + .withBeforeHandler(fsm -> secondIntermediateCounter.before.incrementAndGet()) + .withAfterHandler(fsm -> secondIntermediateCounter.after.incrementAndGet()) + .withProcessor((fsm, event) -> secondIntermediateCounter.process.incrementAndGet()) + .end() + .finish("finish") + .withBeforeHandler(fsm -> finishCounter.before.incrementAndGet()) + .withAfterHandler(fsm -> finishCounter.after.incrementAndGet()) + .withProcessor((fsm, event) -> finishCounter.process.incrementAndGet()) + .end() + .withTransition() + .from("init") + .to("intermediate-1") + .checking((fsm, event) -> true) + .end() + .withTransition() + .from("intermediate-1") + .to("intermediate-2") + .checking((fsm, event) -> true) + .end() + .withTransition() + .from("intermediate-2") + .to("finish") + .checking((fsm, event) -> true) + .end() + .create() + ; + + // @formatter:on + + simpleFsm.process(""); + simpleFsm.process(""); + simpleFsm.process(""); + + Assert.assertEquals("finish", simpleFsm.getCurrentState().getName()); + Assert.assertEquals(1, initCounter.before.get()); + Assert.assertEquals(1, initCounter.after.get()); + Assert.assertEquals(1, initCounter.process.get()); + Assert.assertEquals(1, firstIntermediateCounter.before.get()); + Assert.assertEquals(1, firstIntermediateCounter.after.get()); + Assert.assertEquals(1, firstIntermediateCounter.process.get()); + Assert.assertEquals(1, secondIntermediateCounter.before.get()); + Assert.assertEquals(1, secondIntermediateCounter.after.get()); + Assert.assertEquals(1, secondIntermediateCounter.process.get()); + Assert.assertEquals(1, finishCounter.before.get()); + Assert.assertEquals(0, finishCounter.after.get()); + Assert.assertEquals(0, finishCounter.process.get()); + + } + + static class Counter { + final AtomicInteger before = new AtomicInteger(0); + final AtomicInteger after = new AtomicInteger(0); + final AtomicInteger process = new AtomicInteger(0); + } + }