From d7eaa0080402a23d625867a92a010b454895bea2 Mon Sep 17 00:00:00 2001 From: pauljervis Date: Tue, 17 Apr 2018 13:09:34 +0100 Subject: [PATCH] Bael 1601 kotlin (#4016) * add article files * update kotlin version in pom * IntelliJ added binary folder to all the .gitignores * IntelliJ added binary folder to all the .gitignores * update test function names * Expand tests and add more code for more meaningful content within the article. Remove core-kotlin from parent pom again for eclipse to work. * adjust for issues with custom-pmd * rename test * Revert "IntelliJ added binary folder to all the .gitignores" This reverts commit 0850a2acd5162499540231146c795cfda9ffcfc5. * Revert "IntelliJ added binary folder to all the .gitignores" This reverts commit 2b17cf2ff3624ecbe37f07bb9985ecfd3a977e01. --- core-kotlin/.gitignore | 1 + core-kotlin/pom.xml | 23 +++--- .../kotlin/com/baeldung/filter/ChunkedTest.kt | 39 ++++++++++ .../com/baeldung/filter/DistinctTest.kt | 71 +++++++++++++++++++ .../kotlin/com/baeldung/filter/DropTest.kt | 53 ++++++++++++++ .../kotlin/com/baeldung/filter/FilterTest.kt | 39 ++++++++++ .../kotlin/com/baeldung/filter/SliceTest.kt | 44 ++++++++++++ .../kotlin/com/baeldung/filter/TakeTest.kt | 38 ++++++++++ pom.xml | 1 + 9 files changed, 298 insertions(+), 11 deletions(-) create mode 100644 core-kotlin/.gitignore create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/filter/ChunkedTest.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/filter/DistinctTest.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/filter/DropTest.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/filter/FilterTest.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/filter/SliceTest.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/filter/TakeTest.kt diff --git a/core-kotlin/.gitignore b/core-kotlin/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/core-kotlin/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/core-kotlin/pom.xml b/core-kotlin/pom.xml index 36298ca084..6c729d5688 100644 --- a/core-kotlin/pom.xml +++ b/core-kotlin/pom.xml @@ -21,6 +21,11 @@ + + org.apache.commons + commons-math3 + ${commons-math3.version} + org.junit.jupiter junit-jupiter-engine @@ -33,12 +38,6 @@ ${junit.platform.version} test - - junit - junit - ${junit4.version} - test - org.jetbrains.kotlin kotlin-stdlib @@ -46,7 +45,7 @@ org.jetbrains.kotlin - kotlin-stdlib-jre8 + kotlin-stdlib-jdk8 ${kotlin-stdlib.version} @@ -194,14 +193,16 @@ UTF-8 - 1.1.2 - 1.1.2 - 1.1.2 - 1.1.2 + 1.2.31 + 1.2.31 + 1.2.31 + 1.2.31 0.15 1.5.0 4.1.0 + 3.6.1 + 5.0.0 1.0.0 4.12.0 diff --git a/core-kotlin/src/test/kotlin/com/baeldung/filter/ChunkedTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/filter/ChunkedTest.kt new file mode 100644 index 0000000000..20797cc633 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/filter/ChunkedTest.kt @@ -0,0 +1,39 @@ +package com.baeldung.filter + +import org.junit.jupiter.api.Assertions.assertIterableEquals +import org.junit.jupiter.api.Test + +internal class ChunkedTest { + + @Test + fun givenDNAFragmentString_whenChunking_thenProduceListOfChunks() { + val dnaFragment = "ATTCGCGGCCGCCAA" + + val fragments = dnaFragment.chunked(3) + + assertIterableEquals(listOf("ATT", "CGC", "GGC", "CGC", "CAA"), fragments) + } + + @Test + fun givenDNAString_whenChunkingWithTransformer_thenProduceTransformedList() { + val codonTable = mapOf("ATT" to "Isoleucine", "CAA" to "Glutamine", "CGC" to "Arginine", "GGC" to "Glycine") + val dnaFragment = "ATTCGCGGCCGCCAA" + + val proteins = dnaFragment.chunked(3) { codon -> + codonTable[codon.toString()] ?: error("Unknown codon") + } + + assertIterableEquals(listOf("Isoleucine", "Arginine", "Glycine", "Arginine", "Glutamine"), proteins) + } + + @Test + fun givenListOfValues_whenChunking_thenProduceListOfArrays() { + val whole = listOf(1, 4, 7, 4753, 2, 34, 62, 76, 5868, 0) + val chunks = whole.chunked(6) + + val expected = listOf(listOf(1, 4, 7, 4753, 2, 34), listOf(62, 76, 5868, 0)) + + assertIterableEquals(expected, chunks) + } + +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/filter/DistinctTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/filter/DistinctTest.kt new file mode 100644 index 0000000000..4e445af536 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/filter/DistinctTest.kt @@ -0,0 +1,71 @@ +package com.baeldung.filter + +import org.junit.jupiter.api.Assertions.assertIterableEquals +import org.junit.jupiter.api.Test + +internal class DistinctTest { + data class SmallClass(val key: String, val num: Int) + + @Test + fun givenArrayOfSomeDuplicateValues_whenApplyingDistinct_thenReturnListOfNoDuplicateValues() { + val array = arrayOf(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 7, 8, 9) + val result = array.distinct() + val expected = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9) + + assertIterableEquals(expected, result) + } + + @Test + fun givenArrayOfClassObjects_whenApplyingDistinctOnClassProperty_thenReturnListDistinctOnThatValue() { + + val original = arrayOf( + SmallClass("key1", 1), + SmallClass("key2", 2), + SmallClass("key3", 3), + SmallClass("key4", 3), + SmallClass("er", 9), + SmallClass("er", 10), + SmallClass("er", 11)) + + val actual = original.distinctBy { it.key } + + val expected = listOf( + SmallClass("key1", 1), + SmallClass("key2", 2), + SmallClass("key3", 3), + SmallClass("key4", 3), + SmallClass("er", 9)) + + + assertIterableEquals(expected, actual) + } + + @Test + fun givenArrayOfClassObjects_whenApplyingComplicatedSelector_thenReturnFirstElementToMatchEachSelectorValue() { + val array = arrayOf( + SmallClass("key1", 1), + SmallClass("key2", 2), + SmallClass("key3", 3), + SmallClass("key4", 3), + SmallClass("er", 9), + SmallClass("er", 10), + SmallClass("er", 11), + SmallClass("er", 11), + SmallClass("er", 91), + SmallClass("blob", 22), + SmallClass("dob", 27), + SmallClass("high", 201_434_314)) + + val actual = array.distinctBy { Math.floor(it.num / 10.0) } + + val expected = listOf( + SmallClass("key1", 1), + SmallClass("er", 10), + SmallClass("er", 91), + SmallClass("blob", 22), + SmallClass("high", 201_434_314)) + + assertIterableEquals(expected, actual) + } + +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/filter/DropTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/filter/DropTest.kt new file mode 100644 index 0000000000..7c2685f39b --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/filter/DropTest.kt @@ -0,0 +1,53 @@ +package com.baeldung.filter + +import org.junit.jupiter.api.Assertions.assertIterableEquals +import org.junit.jupiter.api.Test + +internal class DropTest { + + @Test + fun whenDroppingFirstTwoItemsOfArray_thenTwoLess() { + val array = arrayOf(1, 2, 3, 4) + val result = array.drop(2) + val expected = listOf(3, 4) + + assertIterableEquals(expected, result) + } + + @Test + fun whenDroppingMoreItemsOfArray_thenEmptyList() { + val array = arrayOf(1, 2, 3, 4) + val result = array.drop(5) + val expected = listOf() + + assertIterableEquals(expected, result) + } + + @Test + fun givenArray_whenDroppingLastElement_thenReturnListWithoutLastElement() { + val array = arrayOf("1", "2", "3", "4") + val result = array.dropLast(1) + val expected = listOf("1", "2", "3") + + assertIterableEquals(expected, result) + } + + @Test + fun givenArrayOfFloats_whenDroppingLastUntilPredicateIsFalse_thenReturnSubsetListOfFloats() { + val array = arrayOf(1f, 1f, 1f, 1f, 1f, 2f, 1f, 1f, 1f) + val result = array.dropLastWhile { it == 1f } + val expected = listOf(1f, 1f, 1f, 1f, 1f, 2f) + + assertIterableEquals(expected, result) + } + + @Test + fun givenList_whenDroppingMoreThanAvailable_thenThrowException() { + val list = listOf('a', 'e', 'i', 'o', 'u') + val result = list.drop(6) + val expected: List = listOf() + + assertIterableEquals(expected, result) + } + +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/filter/FilterTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/filter/FilterTest.kt new file mode 100644 index 0000000000..efe6354f25 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/filter/FilterTest.kt @@ -0,0 +1,39 @@ +package com.baeldung.filter + +import org.apache.commons.math3.primes.Primes +import org.junit.jupiter.api.Assertions.assertIterableEquals +import org.junit.jupiter.api.Test +import kotlin.test.assertTrue + +internal class FilterTest { + + @Test + fun givenAscendingValueMap_whenFilteringOnValue_ThenReturnSubsetOfMap() { + val originalMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3) + val filteredMap = originalMap.filter { it.value < 2 } + val expectedMap = mapOf("key1" to 1) + + assertTrue { expectedMap == filteredMap } + } + + @Test + fun givenSeveralCollections_whenFilteringToAccumulativeList_thenListContainsAllContents() { + val array1 = arrayOf(90, 92, 93, 94, 92, 95, 93) + val array2 = sequenceOf(51, 31, 83, 674_506_111, 256_203_161, 15_485_863) + val list1 = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) + val primes = mutableListOf() + + val expected = listOf(2, 3, 5, 7, 31, 83, 15_485_863, 256_203_161, 674_506_111) + + val primeCheck = { num: Int -> Primes.isPrime(num) } + + array1.filterTo(primes, primeCheck) + list1.filterTo(primes, primeCheck) + array2.filterTo(primes, primeCheck) + + primes.sort() + + assertIterableEquals(expected, primes) + } + +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/filter/SliceTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/filter/SliceTest.kt new file mode 100644 index 0000000000..dca167928d --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/filter/SliceTest.kt @@ -0,0 +1,44 @@ +package com.baeldung.filter + +import org.junit.jupiter.api.Assertions.assertIterableEquals +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test + +internal class SliceTest { + + @Test + fun whenSlicingAnArrayWithDotRange_ThenListEqualsTheSlice() { + val original = arrayOf(1, 2, 3, 2, 1) + val actual = original.slice(1..3) + val expected = listOf(2, 3, 2) + + assertIterableEquals(expected, actual) + } + + @Test + fun whenSlicingAnArrayWithDownToRange_thenListMadeUpOfReverseSlice() { + val original = arrayOf(1, 2, 3, 2, 1) + val actual = original.slice(3 downTo 0) + val expected = listOf(2, 3, 2, 1) + + assertIterableEquals(expected, actual) + } + + @Test + fun whenSlicingBeyondTheRangeOfTheArray_thenContainManyNulls() { + val original = arrayOf(12, 3, 34, 4) + val actual = original.slice(3..8) + val expected = listOf(4, null, null, null, null, null) + + assertIterableEquals(expected, actual) + } + + @Test + fun whenSlicingBeyondRangeOfArrayWithStep_thenOutOfBoundsException() { + assertThrows(ArrayIndexOutOfBoundsException::class.java) { + val original = arrayOf(12, 3, 34, 4) + original.slice(3..8 step 2) + } + } + +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/filter/TakeTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/filter/TakeTest.kt new file mode 100644 index 0000000000..d021177de8 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/filter/TakeTest.kt @@ -0,0 +1,38 @@ +package com.baeldung.filter + +import org.junit.jupiter.api.Assertions.assertIterableEquals +import org.junit.jupiter.api.Test + +internal class TakeTest { + + @Test + fun `given array of alternating types, when predicating on 'is String', then produce list of array up until predicate is false`() { + val originalArray = arrayOf("val1", 2, "val3", 4, "val5", 6) + val actualList = originalArray.takeWhile { it is String } + val expectedList = listOf("val1") + + assertIterableEquals(expectedList, actualList) + } + + @Test + fun `given array of alternating types, when taking 4 items, then produce list of first 4 items`() { + val originalArray = arrayOf("val1", 2, "val3", 4, "val5", 6) + val actualList = originalArray.take(4) + val expectedList = listOf("val1", 2, "val3", 4) + + println(originalArray.drop(4)) + println(actualList) + + assertIterableEquals(expectedList, actualList) + } + + @Test + fun `when taking more items than available, then return all elements`() { + val originalArray = arrayOf(1, 2) + val actual = originalArray.take(10) + val expected = listOf(1, 2) + + assertIterableEquals(expected, actual) + } + +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 64744eb2da..f0cb72e4d0 100644 --- a/pom.xml +++ b/pom.xml @@ -64,6 +64,7 @@ core-java core-java-io core-java-8 + core-groovy core-java-concurrency