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.
This commit is contained in:
pauljervis 2018-04-17 13:09:34 +01:00 committed by Grzegorz Piwowarek
parent c80befe225
commit d7eaa00804
9 changed files with 298 additions and 11 deletions

1
core-kotlin/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/bin/

View File

@ -21,6 +21,11 @@
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>${commons-math3.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
@ -33,12 +38,6 @@
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit4.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
@ -46,7 +45,7 @@
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin-stdlib.version}</version>
</dependency>
<dependency>
@ -194,14 +193,16 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin-maven-plugin.version>1.1.2</kotlin-maven-plugin.version>
<kotlin-test-junit.version>1.1.2</kotlin-test-junit.version>
<kotlin-stdlib.version>1.1.2</kotlin-stdlib.version>
<kotlin-reflect.version>1.1.2</kotlin-reflect.version>
<kotlin-maven-plugin.version>1.2.31</kotlin-maven-plugin.version>
<kotlin-test-junit.version>1.2.31</kotlin-test-junit.version>
<kotlin-stdlib.version>1.2.31</kotlin-stdlib.version>
<kotlin-reflect.version>1.2.31</kotlin-reflect.version>
<kotlinx.version>0.15</kotlinx.version>
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
<kodein.version>4.1.0</kodein.version>
<commons-math3.version>3.6.1</commons-math3.version>
<junit.jupiter.version>5.0.0</junit.jupiter.version>
<junit.platform.version>1.0.0</junit.platform.version>
<junit.vintage.version>4.12.0</junit.vintage.version>

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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<Int>()
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<String> = listOf()
assertIterableEquals(expected, result)
}
}

View File

@ -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<Int>()
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)
}
}

View File

@ -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)
}
}
}

View File

@ -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)
}
}

View File

@ -64,6 +64,7 @@
<module>core-java</module>
<module>core-java-io</module>
<module>core-java-8</module>
<!--<module>core-kotlin</module>-->
<module>core-groovy</module>
<module>core-java-concurrency</module>