This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial hash code identifier support #27
We use a SHA-256 hash based on the binary object stream of serializables. We provide two difference classes for SutStates and Actions which can get more properties in the future.
- Loading branch information
Showing
16 changed files
with
191 additions
and
105 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
src/main/scala/de/retest/guistatemachine/api/ActionIdentifier.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package de.retest.guistatemachine.api | ||
|
||
import de.retest.surili.commons.actions.Action | ||
|
||
class ActionIdentifier(action: Action) extends HashIdentifier(action) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/scala/de/retest/guistatemachine/api/HashIdentifier.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package de.retest.guistatemachine.api | ||
|
||
import java.io.{ByteArrayOutputStream, ObjectOutputStream, Serializable} | ||
|
||
/** | ||
* Storing the whole states and actions as XML takes too much space. Therefore, we only store a hash value. The hash | ||
* should have no collisions to uniquely identify states and actions. Since we use SHA-256 the probability is very low | ||
* that any collisions will occur. | ||
* | ||
* @param serializable The serializable from which a SHA-256 is generated. | ||
*/ | ||
@SerialVersionUID(1L) | ||
class HashIdentifier(serializable: Serializable) extends scala.Serializable { | ||
val hash: String = HashIdentifier.sha256Hash(HashIdentifier.serializableToString(serializable)) | ||
|
||
override def equals(obj: Any): Boolean = | ||
if (!obj.isInstanceOf[HashIdentifier]) { | ||
false | ||
} else { | ||
val other = obj.asInstanceOf[HashIdentifier] | ||
hash.equals(other.hash) | ||
} | ||
|
||
override def hashCode(): Int = hash.hashCode | ||
|
||
override def toString: String = s"hash=$hash" | ||
|
||
} | ||
|
||
object HashIdentifier { | ||
def sha256Hash(text: String): String = | ||
String.format("%064x", new java.math.BigInteger(1, java.security.MessageDigest.getInstance("SHA-256").digest(text.getBytes("UTF-8")))) | ||
|
||
def serializableToString(serializable: Serializable): String = { | ||
val byteArrayOutputStream = new ByteArrayOutputStream() | ||
val oos = new ObjectOutputStream(byteArrayOutputStream) | ||
try { | ||
oos.writeObject(serializable) | ||
byteArrayOutputStream.toString("UTF-8") | ||
} finally { | ||
oos.close() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/scala/de/retest/guistatemachine/api/SutStateIdentifier.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package de.retest.guistatemachine.api | ||
|
||
import de.retest.recheck.ui.descriptors.SutState | ||
|
||
class SutStateIdentifier(sutState: SutState) extends HashIdentifier(sutState) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 8 additions & 10 deletions
18
src/main/scala/de/retest/guistatemachine/api/impl/StateImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
src/main/scala/de/retest/guistatemachine/api/impl/serialization/GraphActionEdge.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package de.retest.guistatemachine.api.impl.serialization | ||
import de.retest.recheck.ui.descriptors.SutState | ||
import de.retest.surili.commons.actions.Action | ||
import de.retest.guistatemachine.api.{ActionIdentifier, SutStateIdentifier} | ||
|
||
case class GraphActionEdge(from: SutState, to: SutState, action: Action) { | ||
case class GraphActionEdge(from: SutStateIdentifier, to: SutStateIdentifier, action: ActionIdentifier) { | ||
override def toString: String = action.toString | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/test/scala/de/retest/guistatemachine/api/HashIdentifierSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package de.retest.guistatemachine.api | ||
|
||
import de.retest.surili.commons.actions.NavigateToAction | ||
|
||
class HashIdentifierSpec extends AbstractApiSpec { | ||
private val action0 = new NavigateToAction("http://google.com") | ||
private val action0Identifier = new HashIdentifier(action0) | ||
private val action1 = new NavigateToAction("http://wikipedia.org") | ||
private val action1Identifier = new HashIdentifier(action1) | ||
|
||
"HashIdentifier" should { | ||
"generate SHA hashes" in { | ||
action0Identifier.hash shouldEqual "fd00ea22cb50efd96c3ff59d8900685d0d64f2cee1e77873133e7e186afd2e7f" | ||
action1Identifier.hash shouldEqual "240d08498736de4d893c146fd64b58b1ae1eda8c36a565919b035d86c6ee2084" | ||
} | ||
|
||
"not equal" in { | ||
action0Identifier.equals(action1Identifier) shouldEqual false | ||
action0Identifier.hashCode() should not equal action1Identifier.hashCode() | ||
} | ||
|
||
"equal" in { | ||
action0Identifier.equals(action0Identifier) shouldEqual true | ||
action0Identifier.hashCode() shouldEqual action0Identifier.hashCode() | ||
} | ||
} | ||
} |
Oops, something went wrong.