Skip to content

Commit 8c1402e

Browse files
committed
Added tests for NewStrictNullChecks
The test on the old option was renamed.
1 parent 1dd9401 commit 8c1402e

File tree

2 files changed

+168
-9
lines changed

2 files changed

+168
-9
lines changed

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/StrictNullChecksTest.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.fasterxml.jackson.module.kotlin.test
22

33
import com.fasterxml.jackson.databind.ObjectMapper
4-
import com.fasterxml.jackson.module.kotlin.KotlinFeature.StrictNullChecks
5-
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
4+
import com.fasterxml.jackson.databind.exc.InvalidNullException
5+
import com.fasterxml.jackson.module.kotlin.KotlinFeature.NewStrictNullChecks
66
import com.fasterxml.jackson.module.kotlin.kotlinModule
77
import com.fasterxml.jackson.module.kotlin.readValue
88
import org.junit.jupiter.api.Assertions.assertArrayEquals
@@ -13,7 +13,7 @@ import org.junit.jupiter.api.assertThrows
1313
import kotlin.test.assertNull
1414

1515
class StrictNullChecksTest {
16-
private val mapper = ObjectMapper().registerModule(kotlinModule { enable(StrictNullChecks) })
16+
private val mapper = ObjectMapper().registerModule(kotlinModule { enable(NewStrictNullChecks) })
1717

1818
/** collection tests */
1919

@@ -30,7 +30,7 @@ class StrictNullChecksTest {
3030

3131
@Test
3232
fun testListOfInt() {
33-
assertThrows<MissingKotlinParameterException> {
33+
assertThrows<InvalidNullException> {
3434
val json = """{"samples":[1, null]}"""
3535
mapper.readValue<ClassWithListOfInt>(json)
3636
}
@@ -60,7 +60,7 @@ class StrictNullChecksTest {
6060

6161
@Test
6262
fun testArrayOfInt() {
63-
assertThrows<MissingKotlinParameterException> {
63+
assertThrows<InvalidNullException> {
6464
val json = """{"samples":[1, null]}"""
6565
mapper.readValue<ClassWithArrayOfInt>(json)
6666
}
@@ -90,7 +90,7 @@ class StrictNullChecksTest {
9090

9191
@Test
9292
fun testMapOfStringToIntWithNullValue() {
93-
assertThrows<MissingKotlinParameterException> {
93+
assertThrows<InvalidNullException> {
9494
val json = """{ "samples": { "key": null } }"""
9595
mapper.readValue<ClassWithMapOfStringToInt>(json)
9696
}
@@ -119,7 +119,7 @@ class StrictNullChecksTest {
119119
@Disabled // this is a hard problem to solve and is currently not addressed
120120
@Test
121121
fun testListOfGenericWithNullValue() {
122-
assertThrows<MissingKotlinParameterException> {
122+
assertThrows<InvalidNullException> {
123123
val json = """{"samples":[1, null]}"""
124124
mapper.readValue<TestClass<List<Int>>>(json)
125125
}
@@ -135,7 +135,7 @@ class StrictNullChecksTest {
135135
@Disabled // this is a hard problem to solve and is currently not addressed
136136
@Test
137137
fun testMapOfGenericWithNullValue() {
138-
assertThrows<MissingKotlinParameterException> {
138+
assertThrows<InvalidNullException> {
139139
val json = """{ "samples": { "key": null } }"""
140140
mapper.readValue<TestClass<Map<String, Int>>>(json)
141141
}
@@ -151,7 +151,7 @@ class StrictNullChecksTest {
151151
@Disabled // this is a hard problem to solve and is currently not addressed
152152
@Test
153153
fun testArrayOfGenericWithNullValue() {
154-
assertThrows<MissingKotlinParameterException> {
154+
assertThrows<InvalidNullException> {
155155
val json = """{"samples":[1, null]}"""
156156
mapper.readValue<TestClass<Array<Int>>>(json)
157157
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package com.fasterxml.jackson.module.kotlin.test
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import com.fasterxml.jackson.module.kotlin.KotlinFeature.StrictNullChecks
5+
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
6+
import com.fasterxml.jackson.module.kotlin.kotlinModule
7+
import com.fasterxml.jackson.module.kotlin.readValue
8+
import org.junit.jupiter.api.Assertions.assertArrayEquals
9+
import org.junit.jupiter.api.Assertions.assertEquals
10+
import org.junit.jupiter.api.Disabled
11+
import org.junit.jupiter.api.Test
12+
import org.junit.jupiter.api.assertThrows
13+
import kotlin.test.assertNull
14+
15+
class StrictNullChecksTestOld {
16+
private val mapper = ObjectMapper().registerModule(kotlinModule { enable(StrictNullChecks) })
17+
18+
/** collection tests */
19+
20+
private data class ClassWithListOfNullableInt(val samples: List<Int?>)
21+
22+
@Test
23+
fun testListOfNullableInt() {
24+
val json = """{"samples":[1, null]}"""
25+
val stateObj = mapper.readValue<ClassWithListOfNullableInt>(json)
26+
assertEquals(listOf(1, null), stateObj.samples)
27+
}
28+
29+
private data class ClassWithListOfInt(val samples: List<Int>)
30+
31+
@Test
32+
fun testListOfInt() {
33+
assertThrows<MissingKotlinParameterException> {
34+
val json = """{"samples":[1, null]}"""
35+
mapper.readValue<ClassWithListOfInt>(json)
36+
}
37+
}
38+
39+
private data class ClassWithNullableListOfInt(val samples: List<Int>?)
40+
41+
@Test
42+
fun testNullableListOfInt() {
43+
val json = """{"samples": null}"""
44+
val stateObj = mapper.readValue<ClassWithNullableListOfInt>(json)
45+
assertNull(stateObj.samples)
46+
}
47+
48+
/** array tests */
49+
50+
private data class ClassWithArrayOfNullableInt(val samples: Array<Int?>)
51+
52+
@Test
53+
fun testArrayOfNullableInt() {
54+
val json = """{"samples":[1, null]}"""
55+
val stateObj = mapper.readValue<ClassWithArrayOfNullableInt>(json)
56+
assertArrayEquals(arrayOf(1, null), stateObj.samples)
57+
}
58+
59+
private data class ClassWithArrayOfInt(val samples: Array<Int>)
60+
61+
@Test
62+
fun testArrayOfInt() {
63+
assertThrows<MissingKotlinParameterException> {
64+
val json = """{"samples":[1, null]}"""
65+
mapper.readValue<ClassWithArrayOfInt>(json)
66+
}
67+
}
68+
69+
private data class ClassWithNullableArrayOfInt(val samples: Array<Int>?)
70+
71+
@Test
72+
fun testNullableArrayOfInt() {
73+
val json = """{"samples": null}"""
74+
val stateObj = mapper.readValue<ClassWithNullableArrayOfInt>(json)
75+
assertNull(stateObj.samples)
76+
}
77+
78+
/** map tests */
79+
80+
private data class ClassWithMapOfStringToNullableInt(val samples: Map<String, Int?>)
81+
82+
@Test
83+
fun testMapOfStringToNullableInt() {
84+
val json = """{ "samples": { "key": null } }"""
85+
val stateObj = mapper.readValue<ClassWithMapOfStringToNullableInt>(json)
86+
assertEquals(mapOf<String, Int?>("key" to null), stateObj.samples)
87+
}
88+
89+
private data class ClassWithMapOfStringToInt(val samples: Map<String, Int>)
90+
91+
@Test
92+
fun testMapOfStringToIntWithNullValue() {
93+
assertThrows<MissingKotlinParameterException> {
94+
val json = """{ "samples": { "key": null } }"""
95+
mapper.readValue<ClassWithMapOfStringToInt>(json)
96+
}
97+
}
98+
99+
private data class ClassWithNullableMapOfStringToInt(val samples: Map<String, Int>?)
100+
101+
@Test
102+
fun testNullableMapOfStringToInt() {
103+
val json = """{"samples": null}"""
104+
val stateObj = mapper.readValue<ClassWithNullableMapOfStringToInt>(json)
105+
assertNull(stateObj.samples)
106+
}
107+
108+
/** generics test */
109+
110+
private data class TestClass<T>(val samples: T)
111+
112+
@Test
113+
fun testListOfGeneric() {
114+
val json = """{"samples":[1, 2]}"""
115+
val stateObj = mapper.readValue<TestClass<List<Int>>>(json)
116+
assertEquals(listOf(1, 2), stateObj.samples)
117+
}
118+
119+
@Disabled // this is a hard problem to solve and is currently not addressed
120+
@Test
121+
fun testListOfGenericWithNullValue() {
122+
assertThrows<MissingKotlinParameterException> {
123+
val json = """{"samples":[1, null]}"""
124+
mapper.readValue<TestClass<List<Int>>>(json)
125+
}
126+
}
127+
128+
@Test
129+
fun testMapOfGeneric() {
130+
val json = """{ "samples": { "key": 1 } }"""
131+
val stateObj = mapper.readValue<TestClass<Map<String, Int>>>(json)
132+
assertEquals(mapOf("key" to 1), stateObj.samples)
133+
}
134+
135+
@Disabled // this is a hard problem to solve and is currently not addressed
136+
@Test
137+
fun testMapOfGenericWithNullValue() {
138+
assertThrows<MissingKotlinParameterException> {
139+
val json = """{ "samples": { "key": null } }"""
140+
mapper.readValue<TestClass<Map<String, Int>>>(json)
141+
}
142+
}
143+
144+
@Test
145+
fun testArrayOfGeneric() {
146+
val json = """{"samples":[1, 2]}"""
147+
val stateObj = mapper.readValue<TestClass<Array<Int>>>(json)
148+
assertArrayEquals(arrayOf(1, 2), stateObj.samples)
149+
}
150+
151+
@Disabled // this is a hard problem to solve and is currently not addressed
152+
@Test
153+
fun testArrayOfGenericWithNullValue() {
154+
assertThrows<MissingKotlinParameterException> {
155+
val json = """{"samples":[1, null]}"""
156+
mapper.readValue<TestClass<Array<Int>>>(json)
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)