From a0a80af9d0c7ec9cd5a244efd4b294bf9c06c232 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Fri, 16 Nov 2018 23:39:40 +0800 Subject: [PATCH] Generate a random alphanumeric string in Kotlin Issue: BAEL-1913 --- core-kotlin/pom.xml | 6 ++ .../baeldung/random/RandomStringUnitTest.kt | 62 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringUnitTest.kt diff --git a/core-kotlin/pom.xml b/core-kotlin/pom.xml index 5cdb5f700e..2b559b19e0 100644 --- a/core-kotlin/pom.xml +++ b/core-kotlin/pom.xml @@ -18,6 +18,11 @@ commons-math3 ${commons-math3.version} + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + org.junit.platform junit-platform-runner @@ -70,6 +75,7 @@ 3.6.1 + 3.8.1 1.1.1 5.2.0 3.10.0 diff --git a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringUnitTest.kt new file mode 100644 index 0000000000..74085367e8 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringUnitTest.kt @@ -0,0 +1,62 @@ +import org.apache.commons.lang3.RandomStringUtils +import org.junit.Before +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import java.security.SecureRandom +import java.util.concurrent.ThreadLocalRandom +import kotlin.experimental.and +import kotlin.streams.asSequence +import kotlin.test.assertEquals + +const val STRING_LENGTH = 10 +const val ALPHANUMERIC_REGEX = "[a-zA-Z0-9]+" + +class RandomStringUnitTest { + private val charPool : List = ('a'..'z') + ('A'..'Z') + ('0'..'9') + + @Test + fun givenAStringLength_whenUsingJava_thenReturnAlphanumericString() { + var randomString = ThreadLocalRandom.current() + .ints(STRING_LENGTH.toLong(), 0, charPool.size) + .asSequence() + .map(charPool::get) + .joinToString("") + + assert(randomString.matches(Regex(ALPHANUMERIC_REGEX))) + assertEquals(STRING_LENGTH, randomString.length) + } + + @Test + fun givenAStringLength_whenUsingKotlin_thenReturnAlphanumericString() { + var randomString = (1..STRING_LENGTH).map { i -> kotlin.random.Random.nextInt(0, charPool.size) } + .map(charPool::get) + .joinToString("") + + assert(randomString.matches(Regex(ALPHANUMERIC_REGEX))) + assertEquals(STRING_LENGTH, randomString.length) + } + + @Test + fun givenAStringLength_whenUsingApacheCommon_thenReturnAlphanumericString() { + var randomString = RandomStringUtils.randomAlphanumeric(STRING_LENGTH) + + assert(randomString.matches(Regex(ALPHANUMERIC_REGEX))) + assertEquals(STRING_LENGTH, randomString.length) + } + + @Test + fun givenAStringLength_whenUsingRandomForBytes_thenReturnAlphanumericString() { + val random = SecureRandom() + val bytes = ByteArray(STRING_LENGTH) + random.nextBytes(bytes) + + var randomString = (0..bytes.size - 1).map { i -> + charPool.get((bytes[i] and 0xFF.toByte() and charPool.size.toByte()).toInt()) + }.joinToString("") + + assert(randomString.matches(Regex(ALPHANUMERIC_REGEX))) + assertEquals(STRING_LENGTH, randomString.length) + } + +} \ No newline at end of file