Skip to content

Commit 4372c6f

Browse files
sjmillingtonpivovarit
authored andcommitted
[BAEL-2653] Added kotlin immutable collections code (eugenp#6563)
* Added kotlin immutable collections code * corrected wrong junit version
1 parent bce1492 commit 4372c6f

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

‎kotlin-libraries/pom.xml‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
<name>exposed</name>
2020
<url>https://dl.bintray.com/kotlin/exposed</url>
2121
</repository>
22+
<repository>
23+
<snapshots>
24+
<enabled>false</enabled>
25+
</snapshots>
26+
<id>kotlinx</id>
27+
<name>bintray</name>
28+
<url>https://dl.bintray.com/kotlin/kotlinx</url>
29+
</repository>
2230
</repositories>
2331

2432
<dependencies>
@@ -112,9 +120,30 @@
112120
<version>3.3.0</version>
113121
<type>pom</type>
114122
</dependency>
123+
124+
<!-- https://mvnrepository.com/artifact/junit/junit -->
125+
<dependency>
126+
<groupId>junit</groupId>
127+
<artifactId>junit</artifactId>
128+
<version>${junit.version}</version>
129+
<scope>test</scope>
130+
</dependency>
131+
132+
<dependency>
133+
<groupId>com.google.guava</groupId>
134+
<artifactId>guava</artifactId>
135+
<version>27.1-jre</version>
136+
</dependency>
137+
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-collections-immutable -->
138+
<dependency>
139+
<groupId>org.jetbrains.kotlinx</groupId>
140+
<artifactId>kotlinx-collections-immutable</artifactId>
141+
<version>0.1</version>
142+
</dependency>
115143
</dependencies>
116144

117145
<properties>
146+
<junit.version>4.12</junit.version>
118147
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
119148
<kodein.version>4.1.0</kodein.version>
120149
<klaxon.version>3.0.4</klaxon.version>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.kotlin.immutable
2+
3+
import junit.framework.Assert.assertEquals
4+
import kotlinx.collections.immutable.ImmutableList
5+
import kotlinx.collections.immutable.immutableListOf
6+
import org.junit.Rule
7+
import org.junit.Test
8+
import org.junit.rules.ExpectedException
9+
10+
class KotlinxImmutablesUnitTest{
11+
12+
13+
@Rule
14+
@JvmField
15+
var ee : ExpectedException = ExpectedException.none()
16+
17+
@Test
18+
fun givenKICLList_whenAddTried_checkExceptionThrown(){
19+
20+
val list: ImmutableList<String> = immutableListOf("I", "am", "immutable")
21+
22+
list.add("My new item")
23+
24+
assertEquals(listOf("I", "am", "immutable"), list)
25+
26+
}
27+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.kotlin.immutable
2+
3+
import com.google.common.collect.ImmutableList
4+
import com.google.common.collect.ImmutableSet
5+
import junit.framework.Assert.assertEquals
6+
import org.junit.Rule
7+
import org.junit.Test
8+
import org.junit.rules.ExpectedException
9+
10+
class ReadOnlyUnitTest{
11+
12+
@Test
13+
fun givenReadOnlyList_whenCastToMutableList_checkNewElementsAdded(){
14+
15+
val list: List<String> = listOf("This", "Is", "Totally", "Immutable")
16+
17+
(list as MutableList<String>)[2] = "Not"
18+
19+
assertEquals(listOf("This", "Is", "Not", "Immutable"), list)
20+
21+
}
22+
23+
@Rule
24+
@JvmField
25+
var ee : ExpectedException = ExpectedException.none()
26+
27+
@Test
28+
fun givenImmutableList_whenAddTried_checkExceptionThrown(){
29+
30+
val list: List<String> = ImmutableList.of("I", "am", "actually", "immutable")
31+
32+
ee.expect(UnsupportedOperationException::class.java)
33+
34+
(list as MutableList<String>).add("Oops")
35+
36+
}
37+
38+
@Test
39+
fun givenMutableList_whenCopiedAndAddTried_checkExceptionThrown(){
40+
41+
val mutableList : List<String> = listOf("I", "Am", "Definitely", "Immutable")
42+
43+
(mutableList as MutableList<String>)[2] = "100% Not"
44+
45+
assertEquals(listOf("I", "Am", "100% Not", "Immutable"), mutableList)
46+
47+
val list: List<String> = ImmutableList.copyOf(mutableList)
48+
49+
ee.expect(UnsupportedOperationException::class.java)
50+
51+
(list as MutableList<String>)[2] = "Really?"
52+
53+
}
54+
55+
@Test
56+
fun givenImmutableSetBuilder_whenAddTried_checkExceptionThrown(){
57+
58+
val mutableList : List<String> = listOf("Hello", "Baeldung")
59+
val set: ImmutableSet<String> = ImmutableSet.builder<String>()
60+
.add("I","am","immutable")
61+
.addAll(mutableList)
62+
.build()
63+
64+
assertEquals(setOf("Hello", "Baeldung", "I", "am", "immutable"), set)
65+
66+
ee.expect(UnsupportedOperationException::class.java)
67+
68+
(set as MutableSet<String>).add("Oops")
69+
70+
}
71+
72+
73+
}

0 commit comments

Comments
 (0)