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

Commit

Permalink
Replace auto-generated ID with a given name #21
Browse files Browse the repository at this point in the history
The user has to specify a name per state machine.
This simplifies the code.
  • Loading branch information
tdauth committed Mar 25, 2019
1 parent e650379 commit aea36c3
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 153 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import de.retest.guistatemachine.api.GuiStateMachineSerializer
import de.retest.recheck.ui.descriptors.SutState
import de.retest.surili.commons.actions.NavigateToAction

val stateMachineId = GuiStateMachineApi().createStateMachine()
val stateMachine = GuiStateMachineApi().getStateMachine(stateMachineId).get
val stateMachine = GuiStateMachineApi().createStateMachine("test")
val currentState = new SutState(currentDescriptors)
val action = new NavigateToAction("http://google.com")
val nextState = new SutState(nextDescriptors)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.retest.guistatemachine.api

/**
* Represents transitions for one single symbol which is represented by an `de.retest.surili.model.Action` to a number of states.
* Represents transitions for one single symbol which is represented by an `de.retest.surili.commons.actions.Action` to a number of states.
* The corresponding symbol is not stored in this class but in the [[State]] from which the transitions are started.
*
* @param to The states which the transitions lead to. Since it is a NFA, there can be multiple states for the same symbol.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ trait GuiStateMachineApi {
*
* @return The new GUI state machine.
*/
def createStateMachine(): Id
def createStateMachine(name: String): GuiStateMachine

/**
* Removes an existing [[GuiStateMachine]].
*
* @param id The ID of the GUI state machine.
* @param name The name of the GUI state machine.
* @return True if it existed and was removed by this call. Otherwise, false.
*/
def removeStateMachine(id: Id): Boolean
def removeStateMachine(name: String): Boolean

/**
* Gets an existing [[GuiStateMachine]].
*
* @param id The ID of the GUI state machine.
* @param name The name of the GUI state machine.
* @return The existing GUI state machine or nothing.
*/
def getStateMachine(id: Id): Option[GuiStateMachine]
def getStateMachine(name: String): Option[GuiStateMachine]

/**
* Clears all state machines.
Expand Down
7 changes: 0 additions & 7 deletions src/main/scala/de/retest/guistatemachine/api/Id.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package de.retest.guistatemachine.api.impl

import de.retest.guistatemachine.api.{GuiStateMachine, GuiStateMachineApi, Id}
import de.retest.guistatemachine.api.{GuiStateMachine, GuiStateMachineApi}

import scala.collection.concurrent.TrieMap

/**
* Thread-safe implementation of the API. It is thread-safe because it uses `IdMap`.
* Thread-safe implementation of the API.
*/
class GuiStateMachineApiImpl extends GuiStateMachineApi {
private val stateMachines = IdMap[GuiStateMachine]()
private val stateMachines = TrieMap[String, GuiStateMachine]()

override def createStateMachine(): Id = stateMachines.addNewElement(new GuiStateMachineImpl)
override def createStateMachine(name: String): GuiStateMachine = {
val guiStateMachine = new GuiStateMachineImpl
stateMachines += (name -> guiStateMachine)
guiStateMachine
}

override def removeStateMachine(id: Id): Boolean = stateMachines.removeElement(id)
override def removeStateMachine(name: String): Boolean = stateMachines.remove(name).isDefined

override def getStateMachine(id: Id): Option[GuiStateMachine] = stateMachines.getElement(id)
override def getStateMachine(name: String): Option[GuiStateMachine] = stateMachines.get(name)

override def clear(): Unit = stateMachines.clear()
}
63 changes: 0 additions & 63 deletions src/main/scala/de/retest/guistatemachine/api/impl/IdMap.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ case class StateImpl(sutState: SutState) extends State with Serializable {
* Currently, there is no MultiMap trait for immutable maps in the Scala standard library.
* The legacy code used `AmbigueState` here which was more complicated than just a multi map.
*/
var transitions = new HashMap[Action, ActionTransitions]
var transitions = HashMap[Action, ActionTransitions]()

override def getSutState: SutState = this.synchronized { sutState }
override def getTransitions: Map[Action, ActionTransitions] = this.synchronized { transitions }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import org.scalatest.{Matchers, WordSpec}

abstract trait AbstractApiSpec extends WordSpec with Matchers {

def createSutState(rootElements: RootElement*) = new SutState(Arrays.asList(rootElements: _*))

/**
* Creates a new identifying attributes collection which should only match other identifying attributes with the same ID.
*
Expand Down
16 changes: 0 additions & 16 deletions src/test/scala/de/retest/guistatemachine/api/IdSpec.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package de.retest.guistatemachine.api.impl

import de.retest.guistatemachine.api.{AbstractApiSpec, GuiStateMachineApi, Id}
import de.retest.guistatemachine.api.{AbstractApiSpec, GuiStateMachineApi}

class GuiStateMachineApiImplSpec extends AbstractApiSpec {
"GuiStateMachineApi" should {
"create, get and remove a new state machine" in {
val stateMachineId = GuiStateMachineApi().createStateMachine()
stateMachineId shouldEqual Id(0)

val stateMachine = GuiStateMachineApi().getStateMachine(stateMachineId)
GuiStateMachineApi().createStateMachine("tmp")
val stateMachine = GuiStateMachineApi().getStateMachine("tmp")
stateMachine.isDefined shouldBe true
val fsm = stateMachine.get
fsm.getActionExecutionTimes.size shouldEqual 0
fsm.getAllExploredActions.size shouldEqual 0

GuiStateMachineApi().removeStateMachine(stateMachineId) shouldBe true
GuiStateMachineApi().removeStateMachine("tmp") shouldBe true
GuiStateMachineApi().getStateMachine("tmp").isDefined shouldBe false
}

"clear all state machines" in {
GuiStateMachineApi().createStateMachine shouldEqual Id(0)
GuiStateMachineApi().createStateMachine shouldEqual Id(1)
GuiStateMachineApi().createStateMachine shouldEqual Id(2)
GuiStateMachineApi().createStateMachine("tmp0")
GuiStateMachineApi().createStateMachine("tmp1")
GuiStateMachineApi().createStateMachine("tmp2")
GuiStateMachineApi().clear()
GuiStateMachineApi().getStateMachine(Id(2)).isEmpty shouldEqual true
GuiStateMachineApi().getStateMachine("tmp0").isEmpty shouldEqual true
GuiStateMachineApi().getStateMachine("tmp1").isEmpty shouldEqual true
GuiStateMachineApi().getStateMachine("tmp2").isEmpty shouldEqual true
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package de.retest.guistatemachine.api.impl

import java.util.Arrays

import de.retest.guistatemachine.api.AbstractApiSpec
import de.retest.recheck.ui.descriptors.SutState
import de.retest.surili.commons.actions.NavigateToAction
import org.scalatest.BeforeAndAfterEach

Expand All @@ -21,9 +18,9 @@ class GuiStateMachineImplSpec extends AbstractApiSpec with BeforeAndAfterEach {

"GuiStateMachine" should {
"not create a new state when using the same root elements" in {
val s0 = new SutState(Arrays.asList(getRootElement("a", 1)))
val s0Equal = new SutState(Arrays.asList(getRootElement("a", 1)))
val differentState = new SutState(Arrays.asList(getRootElement("a", 2)))
val s0 = createSutState(getRootElement("a", 1))
val s0Equal = createSutState(getRootElement("a", 1))
val differentState = createSutState(getRootElement("a", 2))
s0.equals(s0Equal) shouldBe true
s0.hashCode() shouldEqual s0Equal.hashCode()
differentState.equals(s0) shouldBe false
Expand All @@ -38,13 +35,13 @@ class GuiStateMachineImplSpec extends AbstractApiSpec with BeforeAndAfterEach {
}

"add two transitions to two new states for the same action and one transition to another state for another action" in {
val initialSutState = getSutState
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 = new SutState(Arrays.asList(rootElementA))
val s0SutState = createSutState(rootElementA)
val s0 = sut.getState(s0SutState)
sut.executeAction(initialSutState, action0, s0SutState)
initial.getTransitions.size shouldEqual 1
Expand All @@ -56,7 +53,7 @@ class GuiStateMachineImplSpec extends AbstractApiSpec with BeforeAndAfterEach {
sut.getActionExecutionTimes(action0) shouldEqual 1

// execute action0 for the second time
val s1SutState = new SutState(Arrays.asList(rootElementB))
val s1SutState = createSutState(rootElementB)
val s1 = sut.getState(s1SutState)
sut.executeAction(initialSutState, action0, s1SutState)
initial.getTransitions.size shouldEqual 1
Expand All @@ -68,7 +65,7 @@ class GuiStateMachineImplSpec extends AbstractApiSpec with BeforeAndAfterEach {
sut.getActionExecutionTimes(action0) shouldEqual 2

// execute action1 for the first time
val s2SutState = new SutState(Arrays.asList(rootElementC))
val s2SutState = createSutState(rootElementC)
val s2 = sut.getState(s2SutState)
sut.executeAction(initialSutState, action1, s2SutState)
initial.getTransitions.size shouldEqual 2
Expand All @@ -81,7 +78,7 @@ class GuiStateMachineImplSpec extends AbstractApiSpec with BeforeAndAfterEach {
}

"store a state for the second access" in {
val initialSutState = getSutState
val initialSutState = createSutState(rootElementA, rootElementB, rootElementC)
val initialFromAccess0 = sut.getState(initialSutState)
val initialFromAccess1 = sut.getState(initialSutState)
initialFromAccess0 shouldEqual initialFromAccess1
Expand All @@ -94,6 +91,4 @@ class GuiStateMachineImplSpec extends AbstractApiSpec with BeforeAndAfterEach {
sut.getAllStates.isEmpty shouldEqual true
}
}

private def getSutState: SutState = new SutState(Arrays.asList(rootElementA, rootElementB, rootElementC))
}
24 changes: 0 additions & 24 deletions src/test/scala/de/retest/guistatemachine/api/impl/IdMapSpec.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package de.retest.guistatemachine.api.impl

import java.util.Arrays

import de.retest.guistatemachine.api.AbstractApiSpec
import de.retest.recheck.ui.descriptors.SutState

class StateImplSpec extends AbstractApiSpec {
private val rootElementA = getRootElement("a", 0)
private val rootElementB = getRootElement("b", 0)
private val sutStateA = new SutState(Arrays.asList(rootElementA))
private val sutStateB = new SutState(Arrays.asList(rootElementB))
private val sutStateA = createSutState(rootElementA)
private val sutStateB = createSutState(rootElementB)

"StateImpl" should {
"not equal" in {
Expand Down

0 comments on commit aea36c3

Please sign in to comment.