Skip to content

Commit c89e238

Browse files
committed
Add tests
1 parent d5008b2 commit c89e238

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.fasterxml.jackson.module.kotlin
2+
3+
import com.fasterxml.jackson.databind.JsonMappingException
4+
import com.fasterxml.jackson.databind.node.NullNode
5+
import org.junit.jupiter.api.Nested
6+
import org.junit.jupiter.api.Test
7+
import org.junit.jupiter.api.assertThrows
8+
import java.io.StringReader
9+
10+
class ReadValueTest {
11+
@Nested
12+
inner class CheckTypeMismatchTest {
13+
@Test
14+
fun jsonParser() {
15+
val src = defaultMapper.createParser("null")
16+
assertThrows<JsonMappingException> {
17+
defaultMapper.readValue<String>(src)
18+
}
19+
}
20+
21+
@Test
22+
fun file() {
23+
val src = createTempJson("null")
24+
assertThrows<JsonMappingException> {
25+
defaultMapper.readValue<String>(src)
26+
}
27+
}
28+
29+
// Not implemented because a way to test without mocks was not found
30+
// @Test
31+
// fun url() {
32+
// }
33+
34+
@Test
35+
fun string() {
36+
val src = "null"
37+
assertThrows<JsonMappingException> {
38+
defaultMapper.readValue<String>(src)
39+
}
40+
}
41+
42+
@Test
43+
fun reader() {
44+
val src = StringReader("null")
45+
assertThrows<JsonMappingException> {
46+
defaultMapper.readValue<String>(src)
47+
}
48+
}
49+
50+
@Test
51+
fun inputStream() {
52+
val src = "null".byteInputStream()
53+
assertThrows<JsonMappingException> {
54+
defaultMapper.readValue<String>(src)
55+
}
56+
}
57+
58+
@Test
59+
fun byteArray() {
60+
val src = "null".toByteArray()
61+
assertThrows<JsonMappingException> {
62+
defaultMapper.readValue<String>(src)
63+
}
64+
}
65+
66+
@Test
67+
fun treeToValueTreeNode() {
68+
assertThrows<JsonMappingException> {
69+
defaultMapper.treeToValue<String>(NullNode.instance)
70+
}
71+
}
72+
73+
@Test
74+
fun convertValueAny() {
75+
assertThrows<JsonMappingException> {
76+
defaultMapper.convertValue<String>(null)
77+
}
78+
}
79+
80+
@Test
81+
fun readValueTypedJsonParser() {
82+
val reader = defaultMapper.reader()
83+
val src = reader.createParser("null")
84+
assertThrows<JsonMappingException> {
85+
reader.readValueTyped<String>(src)
86+
}
87+
}
88+
}
89+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import com.fasterxml.jackson.core.util.DefaultIndenter
55
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter
66
import com.fasterxml.jackson.databind.ObjectMapper
77
import com.fasterxml.jackson.databind.ObjectWriter
8+
import java.io.File
9+
import java.io.FileOutputStream
10+
import java.io.OutputStreamWriter
11+
import java.nio.charset.StandardCharsets
812
import kotlin.reflect.KParameter
913
import kotlin.reflect.full.memberProperties
1014
import kotlin.reflect.full.primaryConstructor
@@ -30,3 +34,16 @@ internal inline fun <reified T : Any> assertReflectEquals(expected: T, actual: T
3034
assertEquals(it.get(expected), it.get(actual))
3135
}
3236
}
37+
38+
internal fun createTempJson(json: String): File {
39+
val file = File.createTempFile("temp", ".json")
40+
file.deleteOnExit()
41+
OutputStreamWriter(
42+
FileOutputStream(file),
43+
StandardCharsets.UTF_8
44+
).use { writer ->
45+
writer.write(json)
46+
writer.flush()
47+
}
48+
return file
49+
}

0 commit comments

Comments
 (0)