From ff8380a954c4e10abfdb477f711eb326d4c470f6 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Tue, 30 Oct 2018 09:27:49 +0800 Subject: [PATCH 1/9] add random string test --- .../com/baeldung/random/RandomStringTest.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt new file mode 100644 index 0000000000..b8d0bd49cd --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt @@ -0,0 +1,18 @@ + +import org.junit.jupiter.api.Test +import java.util.concurrent.ThreadLocalRandom +import kotlin.test.assertTrue + +class RandomNumberTest { + + @Test + fun whenRandomNumberWithJavaUtilMath_thenResultIsBetween0And1() { + val source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + var test = Random().ints(outputStrLength, 0, source.length) + .asSequence() + .map(source::get) + .joinToString("") + print("message") + } + +} \ No newline at end of file From 3221968f9e4e3fb58f5039c80c68b0092bcbc77a Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Tue, 30 Oct 2018 13:40:59 +0800 Subject: [PATCH 2/9] BAEL-1913 kotline random string --- core-kotlin/pom.xml | 6 +++ .../com/baeldung/random/RandomStringTest.kt | 50 +++++++++++++++---- 2 files changed, 47 insertions(+), 9 deletions(-) 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/RandomStringTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt index b8d0bd49cd..b47a6ac455 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt @@ -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(); + 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(); + 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); } } \ No newline at end of file From a15a0809ec071dfc804fdf13c139ea7b1ec9a8c7 Mon Sep 17 00:00:00 2001 From: "nnhai1991@gmail.com" Date: Sun, 11 Nov 2018 18:24:01 +0800 Subject: [PATCH 3/9] fix java code --- .../src/test/kotlin/com/baeldung/random/RandomStringTest.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt index b47a6ac455..0795cd2a5b 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt @@ -1,5 +1,6 @@ import org.apache.commons.lang3.RandomStringUtils import org.junit.jupiter.api.Test +import java.util.concurrent.ThreadLocalRandom import kotlin.streams.asSequence import kotlin.test.assertEquals @@ -15,7 +16,8 @@ class RandomStringTest { charPool.addAll('A'..'Z'); charPool.addAll('0'..'9'); - var randomString = java.util.Random().ints(STRING_LENGTH.toLong(), 0, charPool.size) + var randomString = ThreadLocalRandom.current() + .ints(STRING_LENGTH.toLong(), 0, charPool.size) .asSequence() .map(charPool::get) .joinToString("") From 6557cf256408722e6b7e019cb3e314151d86742d Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Tue, 13 Nov 2018 09:43:13 +0800 Subject: [PATCH 4/9] add performance test --- .../com/baeldung/random/RandomStringTest.kt | 58 ++++++++++++------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt index 0795cd2a5b..0715870403 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt @@ -1,52 +1,70 @@ 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]+"; +const val STRING_LENGTH = 10 +const val ALPHANUMERIC_REGEX = "[a-zA-Z0-9]+" class RandomStringTest { - @Test - fun generateRandomString_useJava_returnString() { - val charPool = ArrayList(); + val charPool = ArrayList() + + @BeforeEach + fun charPool() { charPool.addAll('a'..'z'); charPool.addAll('A'..'Z'); charPool.addAll('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); + assert(randomString.matches(Regex(ALPHANUMERIC_REGEX))) + assertEquals(STRING_LENGTH, randomString.length) } @Test - fun generateRandomString_useKotlin_returnString() { - val charPool = ArrayList(); - charPool.addAll('a'..'z'); - charPool.addAll('A'..'Z'); - charPool.addAll('0'..'9'); - + fun givenAStringLength_whenUsingKotlin_thenReturnAlphanumericString() { var randomString = (1..STRING_LENGTH).map { i -> kotlin.random.Random.nextInt(0, charPool.size) } .map(charPool::get) - .joinToString(""); + .joinToString("") - assert(randomString.matches(Regex(ALPHANUMERIC_REGEX))); - assertEquals(STRING_LENGTH, randomString.length); + assert(randomString.matches(Regex(ALPHANUMERIC_REGEX))) + assertEquals(STRING_LENGTH, randomString.length) } @Test - fun generateRandomString_useApacheCommon_returnString() { - var randomString = RandomStringUtils.randomAlphanumeric(STRING_LENGTH); + fun givenAStringLength_whenUsingApacheCommon_thenReturnAlphanumericString() { + var randomString = RandomStringUtils.randomAlphanumeric(STRING_LENGTH) - assert(randomString.matches(Regex(ALPHANUMERIC_REGEX))); - assertEquals(STRING_LENGTH, randomString.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 From f0f1eba7b91e9743f15fd4e7b5d53bbccd5bac41 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Fri, 16 Nov 2018 10:04:48 +0800 Subject: [PATCH 5/9] change from @before to init --- .../test/kotlin/com/baeldung/random/RandomStringTest.kt | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt index 0715870403..f44b0cd437 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt @@ -16,11 +16,10 @@ class RandomStringTest { val charPool = ArrayList() - @BeforeEach - fun charPool() { - charPool.addAll('a'..'z'); - charPool.addAll('A'..'Z'); - charPool.addAll('0'..'9'); + init { + charPool.addAll('a'..'z') + charPool.addAll('A'..'Z') + charPool.addAll('0'..'9') } @Test From e27d5f4f1f81c5f3f0e9c69175d2bc21b751350e Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Fri, 16 Nov 2018 10:05:09 +0800 Subject: [PATCH 6/9] change from @before to init --- .../src/test/kotlin/com/baeldung/random/RandomStringTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt index f44b0cd437..a2a1ac58e3 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt @@ -14,7 +14,7 @@ const val ALPHANUMERIC_REGEX = "[a-zA-Z0-9]+" class RandomStringTest { - val charPool = ArrayList() + private val charPool = ArrayList() init { charPool.addAll('a'..'z') From 3ee9050138e10a087562d4b410d9d9eb7de158b4 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Fri, 16 Nov 2018 10:18:14 +0800 Subject: [PATCH 7/9] change from @before to init --- .../test/kotlin/com/baeldung/random/RandomStringTest.kt | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt index a2a1ac58e3..3c7bc44ea8 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt @@ -13,14 +13,7 @@ const val STRING_LENGTH = 10 const val ALPHANUMERIC_REGEX = "[a-zA-Z0-9]+" class RandomStringTest { - - private val charPool = ArrayList() - - init { - charPool.addAll('a'..'z') - charPool.addAll('A'..'Z') - charPool.addAll('0'..'9') - } + private val charPool : List = ('a'..'z') + ('A'..'Z') + ('0'..'9') @Test fun givenAStringLength_whenUsingJava_thenReturnAlphanumericString() { From 0b15affd0ecd22ba7fbd16d74da14dd707144c6a Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Fri, 16 Nov 2018 13:46:14 +0800 Subject: [PATCH 8/9] rename to RandomStringUnitTest --- .../random/{RandomStringTest.kt => RandomStringUnitTest.kt} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename core-kotlin/src/test/kotlin/com/baeldung/random/{RandomStringTest.kt => RandomStringUnitTest.kt} (98%) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringUnitTest.kt similarity index 98% rename from core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt rename to core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringUnitTest.kt index 3c7bc44ea8..74085367e8 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringUnitTest.kt @@ -12,7 +12,7 @@ import kotlin.test.assertEquals const val STRING_LENGTH = 10 const val ALPHANUMERIC_REGEX = "[a-zA-Z0-9]+" -class RandomStringTest { +class RandomStringUnitTest { private val charPool : List = ('a'..'z') + ('A'..'Z') + ('0'..'9') @Test From d2d77c56f83868222a432a3c58a6c148efa42fea Mon Sep 17 00:00:00 2001 From: "nnhai1991@gmail.com" Date: Sun, 18 Nov 2018 00:16:35 +0800 Subject: [PATCH 9/9] BAEL-1913 fix possible index out of bound --- .../src/test/kotlin/com/baeldung/random/RandomStringUnitTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringUnitTest.kt index 74085367e8..62e8dfe720 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringUnitTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomStringUnitTest.kt @@ -52,7 +52,7 @@ class RandomStringUnitTest { random.nextBytes(bytes) var randomString = (0..bytes.size - 1).map { i -> - charPool.get((bytes[i] and 0xFF.toByte() and charPool.size.toByte()).toInt()) + charPool.get((bytes[i] and 0xFF.toByte() and (charPool.size-1).toByte()).toInt()) }.joinToString("") assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)))