BAEL-1913 kotline random string

This commit is contained in:
Hai Nguyen 2018-10-30 13:40:59 +08:00
parent ff8380a954
commit 3221968f9e
2 changed files with 47 additions and 9 deletions

View File

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

View File

@ -1,18 +1,50 @@
import org.apache.commons.lang3.RandomStringUtils
import org.junit.jupiter.api.Test
import java.util.concurrent.ThreadLocalRandom
import kotlin.test.assertTrue
import kotlin.streams.asSequence
import kotlin.test.assertEquals
class RandomNumberTest {
const val STRING_LENGTH = 10;
const val ALPHANUMERIC_REGEX = "[a-zA-Z0-9]+";
class RandomStringTest {
@Test
fun whenRandomNumberWithJavaUtilMath_thenResultIsBetween0And1() {
val source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var test = Random().ints(outputStrLength, 0, source.length)
fun generateRandomString_useJava_returnString() {
val charPool = ArrayList<Char>();
charPool.addAll('a'..'z');
charPool.addAll('A'..'Z');
charPool.addAll('0'..'9');
var randomString = java.util.Random().ints(STRING_LENGTH.toLong(), 0, charPool.size)
.asSequence()
.map(source::get)
.map(charPool::get)
.joinToString("")
print("message")
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)));
assertEquals(STRING_LENGTH, randomString.length);
}
@Test
fun generateRandomString_useKotlin_returnString() {
val charPool = ArrayList<Char>();
charPool.addAll('a'..'z');
charPool.addAll('A'..'Z');
charPool.addAll('0'..'9');
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 generateRandomString_useApacheCommon_returnString() {
var randomString = RandomStringUtils.randomAlphanumeric(STRING_LENGTH);
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)));
assertEquals(STRING_LENGTH, randomString.length);
}
}