-
-
Notifications
You must be signed in to change notification settings - Fork 4
Added type match check to read functions #295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/ReadValueTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package io.github.projectmapk.jackson.module.kogera.zPorted | ||
|
||
import com.fasterxml.jackson.databind.RuntimeJsonMappingException | ||
import com.fasterxml.jackson.databind.node.NullNode | ||
import io.github.projectmapk.jackson.module.kogera.createTempJson | ||
import io.github.projectmapk.jackson.module.kogera.defaultMapper | ||
import io.github.projectmapk.jackson.module.kogera.readValue | ||
import io.github.projectmapk.jackson.module.kogera.readValueTyped | ||
import io.github.projectmapk.jackson.module.kogera.treeToValue | ||
import io.github.projectmapk.jackson.module.kogera.convertValue | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
import java.io.StringReader | ||
|
||
class ReadValueTest { | ||
@Nested | ||
inner class CheckTypeMismatchTest { | ||
@Test | ||
fun jsonParser() { | ||
val src = defaultMapper.createParser("null") | ||
assertThrows<RuntimeJsonMappingException> { | ||
defaultMapper.readValue<String>(src) | ||
}.printStackTrace() | ||
} | ||
|
||
@Test | ||
fun file() { | ||
val src = createTempJson("null") | ||
assertThrows<RuntimeJsonMappingException> { | ||
defaultMapper.readValue<String>(src) | ||
} | ||
} | ||
|
||
// Not implemented because a way to test without mocks was not found | ||
// @Test | ||
// fun url() { | ||
// } | ||
|
||
@Test | ||
fun string() { | ||
val src = "null" | ||
assertThrows<RuntimeJsonMappingException> { | ||
defaultMapper.readValue<String>(src) | ||
} | ||
} | ||
|
||
@Test | ||
fun reader() { | ||
val src = StringReader("null") | ||
assertThrows<RuntimeJsonMappingException> { | ||
defaultMapper.readValue<String>(src) | ||
} | ||
} | ||
|
||
@Test | ||
fun inputStream() { | ||
val src = "null".byteInputStream() | ||
assertThrows<RuntimeJsonMappingException> { | ||
defaultMapper.readValue<String>(src) | ||
} | ||
} | ||
|
||
@Test | ||
fun byteArray() { | ||
val src = "null".toByteArray() | ||
assertThrows<RuntimeJsonMappingException> { | ||
defaultMapper.readValue<String>(src) | ||
} | ||
} | ||
|
||
@Test | ||
fun treeToValueTreeNode() { | ||
assertThrows<RuntimeJsonMappingException> { | ||
defaultMapper.treeToValue<String>(NullNode.instance) | ||
} | ||
} | ||
|
||
@Test | ||
fun convertValueAny() { | ||
assertThrows<RuntimeJsonMappingException> { | ||
defaultMapper.convertValue<String>(null) | ||
} | ||
} | ||
|
||
@Test | ||
fun readValueTypedJsonParser() { | ||
val reader = defaultMapper.reader() | ||
val src = reader.createParser("null") | ||
assertThrows<RuntimeJsonMappingException> { | ||
reader.readValueTyped<String>(src) | ||
} | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/ReadValuesTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.github.projectmapk.jackson.module.kogera.zPorted | ||
|
||
import com.fasterxml.jackson.core.JsonParser | ||
import com.fasterxml.jackson.databind.DeserializationContext | ||
import com.fasterxml.jackson.databind.RuntimeJsonMappingException | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer | ||
import com.fasterxml.jackson.databind.module.SimpleModule | ||
import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper | ||
import io.github.projectmapk.jackson.module.kogera.readValues | ||
import io.github.projectmapk.jackson.module.kogera.readValuesTyped | ||
import org.junit.jupiter.api.Assertions.assertDoesNotThrow | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
class ReadValuesTest { | ||
class MyStrDeser : StdDeserializer<String>(String::class.java) { | ||
override fun deserialize( | ||
p: JsonParser, | ||
ctxt: DeserializationContext | ||
): String? = p.valueAsString.takeIf { it != "bar" } | ||
} | ||
|
||
@Nested | ||
inner class CheckTypeMismatchTest { | ||
val mapper = jacksonObjectMapper().registerModule( | ||
object : SimpleModule() { | ||
init { | ||
addDeserializer(String::class.java, MyStrDeser()) | ||
} | ||
} | ||
)!! | ||
|
||
@Test | ||
fun readValuesJsonParserNext() { | ||
val src = mapper.createParser(""""foo"${"\n"}"bar"""") | ||
val itr = mapper.readValues<String>(src) | ||
|
||
assertEquals("foo", itr.next()) | ||
// TODO: It is expected to be checked after importing 2.19. | ||
// assertThrows<RuntimeJsonMappingException> { | ||
assertDoesNotThrow { | ||
itr.next() | ||
} | ||
} | ||
|
||
@Test | ||
fun readValuesJsonParserNextValue() { | ||
val src = mapper.createParser(""""foo"${"\n"}"bar"""") | ||
val itr = mapper.readValues<String>(src) | ||
|
||
assertEquals("foo", itr.nextValue()) | ||
// TODO: It is expected to be checked after importing 2.19. | ||
// assertThrows<RuntimeJsonMappingException> { | ||
assertDoesNotThrow { | ||
itr.nextValue() | ||
} | ||
} | ||
|
||
@Test | ||
fun readValuesTypedJsonParser() { | ||
val reader = mapper.reader() | ||
val src = reader.createParser(""""foo"${"\n"}"bar"""") | ||
val itr = reader.readValuesTyped<String>(src) | ||
|
||
assertEquals("foo", itr.next()) | ||
assertThrows<RuntimeJsonMappingException> { | ||
itr.next() | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could not import because of required changes to
MappingIterator
in 2.19.