Generate a random alphanumeric string in Kotlin

Issue: BAEL-1913
This commit is contained in:
Hai Nguyen 2018-11-16 23:39:40 +08:00 committed by Josh Cummings
parent eaa890aead
commit a0a80af9d0
2 changed files with 68 additions and 0 deletions

View File

@ -18,6 +18,11 @@
<artifactId>commons-math3</artifactId> <artifactId>commons-math3</artifactId>
<version>${commons-math3.version}</version> <version>${commons-math3.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.junit.platform</groupId> <groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId> <artifactId>junit-platform-runner</artifactId>
@ -70,6 +75,7 @@
<properties> <properties>
<commons-math3.version>3.6.1</commons-math3.version> <commons-math3.version>3.6.1</commons-math3.version>
<commons-lang3.version>3.8.1</commons-lang3.version>
<junit.platform.version>1.1.1</junit.platform.version> <junit.platform.version>1.1.1</junit.platform.version>
<junit.vintage.version>5.2.0</junit.vintage.version> <junit.vintage.version>5.2.0</junit.vintage.version>
<assertj.version>3.10.0</assertj.version> <assertj.version>3.10.0</assertj.version>

View File

@ -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<Char> = ('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)
}
}