From fc33d62cc3aed06165d21b690b8a5eb7a5799236 Mon Sep 17 00:00:00 2001 From: Doha2012 Date: Sun, 9 Jul 2017 21:10:38 +0200 Subject: [PATCH] kotlin collection (#2241) * minor logging fix * spring security sso * use basic auth * use form login * cleanup * cleanup * final cleanup * second client app for sso * spring boot bootstrap * add logic * cleanup * add simple controller * add thymeleaf and security * minor fix * minor fix * add more boot properties * fix live test * fix live test * minor fix * semaphores * fix configuration * kotlin collection --- .../com/baeldung/kotlin/CollectionsTest.kt | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 kotlin/src/test/kotlin/com/baeldung/kotlin/CollectionsTest.kt diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/CollectionsTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/CollectionsTest.kt new file mode 100644 index 0000000000..81a01ca4fb --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/CollectionsTest.kt @@ -0,0 +1,121 @@ +package com.baeldung.kotlin + +import org.junit.Test +import kotlin.test.assertTrue +import kotlin.test.assertFalse +import kotlin.test.assertEquals + +class CollectionsTest { + + @Test + fun whenUseDifferentCollections_thenSuccess () { + val theList = listOf("one", "two", "three") + assertTrue(theList.contains("two")) + + val theMutableList = mutableListOf("one", "two", "three") + theMutableList.add("four") + assertTrue(theMutableList.contains("four")) + + val theSet = setOf("one", "two", "three") + assertTrue(theSet.contains("three")) + + val theMutableSet = mutableSetOf("one", "two", "three") + theMutableSet.add("four") + assertTrue(theMutableSet.contains("four")) + + val theMap = mapOf(1 to "one", 2 to "two", 3 to "three") + assertEquals(theMap[2],"two") + + val theMutableMap = mutableMapOf(1 to "one", 2 to "two", 3 to "three") + theMutableMap[4] = "four" + assertEquals(theMutableMap[4],"four") + } + + @Test + fun whenSliceCollection_thenSuccess () { + val theList = listOf("one", "two", "three") + val resultList = theList.slice(1..2) + + assertEquals(2, resultList.size) + assertTrue(resultList.contains("two")) + } + + @Test + fun whenJoinTwoCollections_thenSuccess () { + val firstList = listOf("one", "two", "three") + val secondList = listOf("four", "five", "six") + val resultList = firstList + secondList + + assertEquals(6, resultList.size) + assertTrue(resultList.contains("two")) + assertTrue(resultList.contains("five")) + } + + @Test + fun whenFilterNullValues_thenSuccess () { + val theList = listOf("one", null, "two", null, "three") + val resultList = theList.filterNotNull() + + assertEquals(3, resultList.size) + } + + @Test + fun whenFilterNonPositiveValues_thenSuccess () { + val theList = listOf(1, 2, -3, -4, 5, -6) + val resultList = theList.filter{ it > 0} + //val resultList = theList.filter{ x -> x > 0} + + assertEquals(3, resultList.size) + assertTrue(resultList.contains(1)) + assertFalse(resultList.contains(-4)) + } + + @Test + fun whenDropFirstItems_thenRemoved () { + val theList = listOf("one", "two", "three", "four") + val resultList = theList.drop(2) + + assertEquals(2, resultList.size) + assertFalse(resultList.contains("one")) + assertFalse(resultList.contains("two")) + } + + @Test + fun whenDropFirstItemsBasedOnCondition_thenRemoved () { + val theList = listOf("one", "two", "three", "four") + val resultList = theList.dropWhile{ it.length < 4 } + + assertEquals(2, resultList.size) + assertFalse(resultList.contains("one")) + assertFalse(resultList.contains("two")) + } + + @Test + fun whenExcludeItems_thenRemoved () { + val firstList = listOf("one", "two", "three") + val secondList = listOf("one", "three") + val resultList = firstList - secondList + + assertEquals(1, resultList.size) + assertTrue(resultList.contains("two")) + } + + @Test + fun whenSearchForExistingItem_thenFound () { + val theList = listOf("one", "two", "three") + + assertTrue("two" in theList) + } + + @Test + fun whenGroupItems_thenSuccess () { + val theList = listOf(1, 2, 3, 4, 5, 6) + val resultMap = theList.groupBy{ it % 3} + + assertEquals(3, resultMap.size) + print(resultMap[1]) + assertTrue(resultMap[1]!!.contains(1)) + assertTrue(resultMap[2]!!.contains(5)) + } + +} \ No newline at end of file