[BAEL-2653] Added kotlin immutable collections code (#6563)
* Added kotlin immutable collections code * corrected wrong junit version
This commit is contained in:
parent
bce14928bd
commit
4372c6fa1c
|
@ -19,6 +19,14 @@
|
|||
<name>exposed</name>
|
||||
<url>https://dl.bintray.com/kotlin/exposed</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
<id>kotlinx</id>
|
||||
<name>bintray</name>
|
||||
<url>https://dl.bintray.com/kotlin/kotlinx</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
|
@ -112,9 +120,30 @@
|
|||
<version>3.3.0</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>27.1-jre</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-collections-immutable -->
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlinx</groupId>
|
||||
<artifactId>kotlinx-collections-immutable</artifactId>
|
||||
<version>0.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||
<kodein.version>4.1.0</kodein.version>
|
||||
<klaxon.version>3.0.4</klaxon.version>
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.kotlin.immutable
|
||||
|
||||
import junit.framework.Assert.assertEquals
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
import kotlinx.collections.immutable.immutableListOf
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.ExpectedException
|
||||
|
||||
class KotlinxImmutablesUnitTest{
|
||||
|
||||
|
||||
@Rule
|
||||
@JvmField
|
||||
var ee : ExpectedException = ExpectedException.none()
|
||||
|
||||
@Test
|
||||
fun givenKICLList_whenAddTried_checkExceptionThrown(){
|
||||
|
||||
val list: ImmutableList<String> = immutableListOf("I", "am", "immutable")
|
||||
|
||||
list.add("My new item")
|
||||
|
||||
assertEquals(listOf("I", "am", "immutable"), list)
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.baeldung.kotlin.immutable
|
||||
|
||||
import com.google.common.collect.ImmutableList
|
||||
import com.google.common.collect.ImmutableSet
|
||||
import junit.framework.Assert.assertEquals
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.ExpectedException
|
||||
|
||||
class ReadOnlyUnitTest{
|
||||
|
||||
@Test
|
||||
fun givenReadOnlyList_whenCastToMutableList_checkNewElementsAdded(){
|
||||
|
||||
val list: List<String> = listOf("This", "Is", "Totally", "Immutable")
|
||||
|
||||
(list as MutableList<String>)[2] = "Not"
|
||||
|
||||
assertEquals(listOf("This", "Is", "Not", "Immutable"), list)
|
||||
|
||||
}
|
||||
|
||||
@Rule
|
||||
@JvmField
|
||||
var ee : ExpectedException = ExpectedException.none()
|
||||
|
||||
@Test
|
||||
fun givenImmutableList_whenAddTried_checkExceptionThrown(){
|
||||
|
||||
val list: List<String> = ImmutableList.of("I", "am", "actually", "immutable")
|
||||
|
||||
ee.expect(UnsupportedOperationException::class.java)
|
||||
|
||||
(list as MutableList<String>).add("Oops")
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenMutableList_whenCopiedAndAddTried_checkExceptionThrown(){
|
||||
|
||||
val mutableList : List<String> = listOf("I", "Am", "Definitely", "Immutable")
|
||||
|
||||
(mutableList as MutableList<String>)[2] = "100% Not"
|
||||
|
||||
assertEquals(listOf("I", "Am", "100% Not", "Immutable"), mutableList)
|
||||
|
||||
val list: List<String> = ImmutableList.copyOf(mutableList)
|
||||
|
||||
ee.expect(UnsupportedOperationException::class.java)
|
||||
|
||||
(list as MutableList<String>)[2] = "Really?"
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenImmutableSetBuilder_whenAddTried_checkExceptionThrown(){
|
||||
|
||||
val mutableList : List<String> = listOf("Hello", "Baeldung")
|
||||
val set: ImmutableSet<String> = ImmutableSet.builder<String>()
|
||||
.add("I","am","immutable")
|
||||
.addAll(mutableList)
|
||||
.build()
|
||||
|
||||
assertEquals(setOf("Hello", "Baeldung", "I", "am", "immutable"), set)
|
||||
|
||||
ee.expect(UnsupportedOperationException::class.java)
|
||||
|
||||
(set as MutableSet<String>).add("Oops")
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue