Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 27, 2023 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/af63689c697c3832b28cde723d41724e to your computer and use it in GitHub Desktop.
Save dacr/af63689c697c3832b28cde723d41724e to your computer and use it in GitHub Desktop.
Universally unique identifiers using standard and JUG APIs / published by https://github.com/dacr/code-examples-manager #df9334be-764b-4e63-a09a-03cb48540fbe/adc39e9edb82726d629b6dc9c883eb23b5650961
// summary : Universally unique identifiers using standard and JUG APIs
// keywords : uuid, named-uuid, jug, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : df9334be-764b-4e63-a09a-03cb48540fbe
// created-on : 2021-12-21T16:56:05+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.3.0"
//> using dep "org.scalatest::scalatest:3.2.16"
//> using dep "com.fasterxml.uuid:java-uuid-generator:4.2.0"
//> using dep "org.slf4j:slf4j-nop:2.0.7"
//> using objectWrapper
// ---------------------
import org.scalatest.*, flatspec.*, matchers.*
import java.util.UUID
import com.fasterxml.uuid.Generators
class ThatSpec extends AnyFlatSpec with should.Matchers {
override def suiteName = "ThatSpec"
"standard UUID" should "be able to generate random UUID" in {
val uuid = UUID.randomUUID()
UUID.fromString(uuid.toString) shouldBe uuid
}
"name based UUID generator" should "give always the same uuid when using the same input" in {
val myInput = "blah bouh"
val generator = Generators.nameBasedGenerator()
val uuid1 = generator.generate(myInput)
val uuid2 = generator.generate(myInput)
info("Using the same name source, you always get the same UUID")
uuid1 shouldBe uuid2
}
it should "give distinct uuids if a distinct namespace are used " in {
val myInput = "blah bouh"
val namespace1 = UUID.randomUUID()
val generator1a = Generators.nameBasedGenerator(namespace1)
val generator1b = Generators.nameBasedGenerator(namespace1)
val namespace2 = UUID.randomUUID()
val generator2 = Generators.nameBasedGenerator(namespace2)
generator1a.generate(myInput) shouldBe generator1a.generate(myInput)
generator1a.generate(myInput) shouldBe generator1b.generate(myInput)
generator1a.generate(myInput) should not be generator2.generate(myInput)
}
it should "also be possible to generate named base UUID using only the standard JDK" in {
val myInput = "blah bouh"
val uuid1 = UUID.nameUUIDFromBytes(myInput.getBytes)
val uuid2 = UUID.nameUUIDFromBytes(myInput.getBytes)
info("Using the same name source, you always get the same UUID")
uuid1 shouldBe uuid2
}
}
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[ThatSpec].getName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment