Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
Remove unnecessary helper methods #28
Browse files Browse the repository at this point in the history
These methods existed due to the legacy code.
  • Loading branch information
tdauth committed Apr 10, 2019
1 parent 811faeb commit f05b093
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 78 deletions.
23 changes: 4 additions & 19 deletions src/main/scala/de/retest/guistatemachine/api/GuiStateMachine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
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.
*/
@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 {
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
}
Expand Down

0 comments on commit f05b093

Please sign in to comment.