|
| 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 | +} |
0 commit comments