Last active
May 25, 2024 10:20
-
-
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/7fff8a4826c21f8b5f8e921372c938fcf652b80d
This file contains hidden or 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
// 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.4.2" | |
//> 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