From f05b09382738c7f217f003eaf590439d013e8e2c Mon Sep 17 00:00:00 2001 From: Tamino Dauth Date: Tue, 9 Apr 2019 17:30:27 +0200 Subject: [PATCH] Remove unnecessary helper methods #28 These methods existed due to the legacy code. --- .../guistatemachine/api/GuiStateMachine.scala | 23 ++------- .../api/impl/GuiStateMachineImpl.scala | 47 ++++--------------- .../api/impl/GuiStateMachineImplSpec.scala | 24 ++-------- 3 files changed, 16 insertions(+), 78 deletions(-) diff --git a/src/main/scala/de/retest/guistatemachine/api/GuiStateMachine.scala b/src/main/scala/de/retest/guistatemachine/api/GuiStateMachine.scala index b9c0d3b..514c931 100644 --- a/src/main/scala/de/retest/guistatemachine/api/GuiStateMachine.scala +++ b/src/main/scala/de/retest/guistatemachine/api/GuiStateMachine.scala @@ -29,30 +29,15 @@ trait GuiStateMachine { * @param from The state the action is executed from * @param a The action which is executed by the user. * @param to The state which the execution leads to. - * @return The current state which the transition of a leads to. + * @return The number of times the action has been executed. */ - def executeAction(from: State, a: ActionIdentifier, to: State): State - def executeAction(from: State, a: Action, to: State): State = executeAction(from, new ActionIdentifier(a), to) - def executeAction(fromSutState: SutState, a: Action, toSutState: SutState): State = + def executeAction(from: State, a: ActionIdentifier, to: State): Int = from.addTransition(a, to) + def executeAction(from: State, a: Action, to: State): Int = executeAction(from, new ActionIdentifier(a), to) + def executeAction(fromSutState: SutState, a: Action, toSutState: SutState): Int = executeAction(getState(fromSutState), a, getState(toSutState)) def getAllStates: Map[SutStateIdentifier, State] - /** - * In the legacy code this was only used to show the number of actions which have been explored by Monkey Testing. - * - * @return All actions which have been explored and therefore have a corresponding transition. - */ - def getAllExploredActions: Set[ActionIdentifier] - - /** - * In the legacy code this was only used to calculate all never explored actions. - * It could be used for the visualization of the NFA to see how often actions are executed. - * - * @return The number of times every explored action has been executed in the NFA. Never explored actions are not part of it. - */ - def getActionExecutionTimes: Map[ActionIdentifier, Int] - /** * Clears all states, transitions and never explored actions etc. */ diff --git a/src/main/scala/de/retest/guistatemachine/api/impl/GuiStateMachineImpl.scala b/src/main/scala/de/retest/guistatemachine/api/impl/GuiStateMachineImpl.scala index 7d7a725..2d9eb11 100644 --- a/src/main/scala/de/retest/guistatemachine/api/impl/GuiStateMachineImpl.scala +++ b/src/main/scala/de/retest/guistatemachine/api/impl/GuiStateMachineImpl.scala @@ -1,9 +1,9 @@ package de.retest.guistatemachine.api.impl import com.typesafe.scalalogging.Logger -import de.retest.guistatemachine.api.{ActionIdentifier, GuiStateMachine, State, SutStateIdentifier} +import de.retest.guistatemachine.api.{GuiStateMachine, State, SutStateIdentifier} -import scala.collection.immutable.{HashMap, HashSet} +import scala.collection.concurrent.TrieMap /** * Thread-safe implementation of a GUI state machine. @@ -11,20 +11,9 @@ import scala.collection.immutable.{HashMap, HashSet} @SerialVersionUID(1L) class GuiStateMachineImpl extends GuiStateMachine with Serializable { @transient private val logger = Logger[GuiStateMachineImpl] - private var states = new HashMap[SutStateIdentifier, State] + private var states = TrieMap[SutStateIdentifier, State]() - /** - * The legacy code stored execution counters for every action. - */ - private var allExploredActions = new HashSet[ActionIdentifier] - - /** - * `actionExecutionCounter` from the legacy code. - * Stores the total number of executions per action. - */ - private var actionExecutionTimes = new HashMap[ActionIdentifier, Int] - - override def getState(sutState: SutStateIdentifier): State = this.synchronized { + override def getState(sutState: SutStateIdentifier): State = if (states.contains(sutState)) { states(sutState) } else { @@ -33,36 +22,16 @@ class GuiStateMachineImpl extends GuiStateMachine with Serializable { states += (sutState -> s) s } - } - - override def executeAction(from: State, a: ActionIdentifier, to: State): State = this.synchronized { - allExploredActions += a - val old = actionExecutionTimes.get(a) - old match { - case Some(o) => actionExecutionTimes += (a -> (o + 1)) - case None => actionExecutionTimes += (a -> 1) - } - from.addTransition(a, to) - to - } - - override def getAllStates: Map[SutStateIdentifier, State] = this.synchronized { states } - - override def getAllExploredActions: Set[ActionIdentifier] = this.synchronized { allExploredActions } - override def getActionExecutionTimes: Map[ActionIdentifier, Int] = this.synchronized { actionExecutionTimes } + override def getAllStates: Map[SutStateIdentifier, State] = states.toMap - override def clear(): Unit = this.synchronized { - states = new HashMap[SutStateIdentifier, State] - allExploredActions = new HashSet[ActionIdentifier] - actionExecutionTimes = new HashMap[ActionIdentifier, Int] + override def clear(): Unit = { + states = TrieMap[SutStateIdentifier, State]() } - override def assignFrom(other: GuiStateMachine): Unit = this.synchronized { + override def assignFrom(other: GuiStateMachine): Unit = { clear() val otherStateMachine = other.asInstanceOf[GuiStateMachineImpl] states = otherStateMachine.states - allExploredActions = otherStateMachine.allExploredActions - actionExecutionTimes = otherStateMachine.actionExecutionTimes } } diff --git a/src/test/scala/de/retest/guistatemachine/api/impl/GuiStateMachineImplSpec.scala b/src/test/scala/de/retest/guistatemachine/api/impl/GuiStateMachineImplSpec.scala index 2bbc886..abc0a93 100644 --- a/src/test/scala/de/retest/guistatemachine/api/impl/GuiStateMachineImplSpec.scala +++ b/src/test/scala/de/retest/guistatemachine/api/impl/GuiStateMachineImplSpec.scala @@ -39,13 +39,11 @@ class GuiStateMachineImplSpec extends AbstractApiSpec with BeforeAndAfterEach { "add two transitions to two new states for the same action and two transitions for the same action to another state" in { val initialSutState = createSutState(rootElementA, rootElementB, rootElementC) val initial = sut.getState(initialSutState) - sut.getAllExploredActions.size shouldEqual 0 - sut.getActionExecutionTimes.size shouldEqual 0 // execute action0 for the first time val s0SutState = createSutState(rootElementA) val s0 = sut.getState(s0SutState) - sut.executeAction(initialSutState, action0, s0SutState) + sut.executeAction(initialSutState, action0, s0SutState) shouldEqual 1 initial.getOutgoingActionTransitions.size shouldEqual 1 initial.getOutgoingActionTransitions(action0Identifier).states.size shouldEqual 1 initial.getOutgoingActionTransitions(action0Identifier).executionCounter shouldEqual 1 @@ -54,14 +52,11 @@ class GuiStateMachineImplSpec extends AbstractApiSpec with BeforeAndAfterEach { s0.getIncomingActionTransitions.size shouldEqual 1 s0.getIncomingActionTransitions(action0Identifier).states.size shouldEqual 1 s0.getIncomingActionTransitions(action0Identifier).executionCounter shouldEqual 1 - sut.getAllExploredActions.size shouldEqual 1 - sut.getActionExecutionTimes.get(action0Identifier).isDefined shouldEqual true - sut.getActionExecutionTimes(action0Identifier) shouldEqual 1 // execute action0 for the second time val s1SutState = createSutState(rootElementB) val s1 = sut.getState(s1SutState) - sut.executeAction(initialSutState, action0, s1SutState) + sut.executeAction(initialSutState, action0, s1SutState) shouldEqual 2 initial.getOutgoingActionTransitions.size shouldEqual 1 initial.getOutgoingActionTransitions(action0Identifier).states.size shouldEqual 2 initial.getOutgoingActionTransitions(action0Identifier).executionCounter shouldEqual 2 @@ -70,14 +65,11 @@ class GuiStateMachineImplSpec extends AbstractApiSpec with BeforeAndAfterEach { s1.getIncomingActionTransitions.size shouldEqual 1 s1.getIncomingActionTransitions(action0Identifier).states.size shouldEqual 1 s1.getIncomingActionTransitions(action0Identifier).executionCounter shouldEqual 1 - sut.getAllExploredActions.size shouldEqual 1 - sut.getActionExecutionTimes.get(action0Identifier).isDefined shouldEqual true - sut.getActionExecutionTimes(action0Identifier) shouldEqual 2 // execute action1 for the first time val s2SutState = createSutState(rootElementC) val s2 = sut.getState(s2SutState) - sut.executeAction(initialSutState, action1, s2SutState) + sut.executeAction(initialSutState, action1, s2SutState) shouldEqual 1 initial.getOutgoingActionTransitions.size shouldEqual 2 initial.getOutgoingActionTransitions(action1Identifier).states.size shouldEqual 1 initial.getOutgoingActionTransitions(action1Identifier).executionCounter shouldEqual 1 @@ -86,12 +78,9 @@ class GuiStateMachineImplSpec extends AbstractApiSpec with BeforeAndAfterEach { s2.getIncomingActionTransitions.size shouldEqual 1 s2.getIncomingActionTransitions(action1Identifier).states.size shouldEqual 1 s2.getIncomingActionTransitions(action1Identifier).executionCounter shouldEqual 1 - sut.getAllExploredActions.size shouldEqual 2 - sut.getActionExecutionTimes.get(action1Identifier).isDefined shouldEqual true - sut.getActionExecutionTimes(action1Identifier) shouldEqual 1 // execute action1 for the second time but from s1SutState to create one incoming action from two different states - sut.executeAction(s1SutState, action1, s2SutState) + sut.executeAction(s1SutState, action1, s2SutState) shouldEqual 2 s1.getOutgoingActionTransitions.size shouldEqual 1 s1.getOutgoingActionTransitions(action1Identifier).states.size shouldEqual 1 s1.getOutgoingActionTransitions(action1Identifier).executionCounter shouldEqual 1 @@ -102,9 +91,6 @@ class GuiStateMachineImplSpec extends AbstractApiSpec with BeforeAndAfterEach { s2.getIncomingActionTransitions.size shouldEqual 1 s2.getIncomingActionTransitions(action1Identifier).states shouldEqual Set(initial, s1) s2.getIncomingActionTransitions(action1Identifier).executionCounter shouldEqual 2 - sut.getAllExploredActions.size shouldEqual 2 - sut.getActionExecutionTimes.get(action1Identifier).isDefined shouldEqual true - sut.getActionExecutionTimes(action1Identifier) shouldEqual 2 } "store a state for the second access" in { @@ -116,8 +102,6 @@ class GuiStateMachineImplSpec extends AbstractApiSpec with BeforeAndAfterEach { "clear the state machine" in { sut.clear() - sut.getAllExploredActions.isEmpty shouldEqual true - sut.getActionExecutionTimes.isEmpty shouldEqual true sut.getAllStates.isEmpty shouldEqual true } }