Skip to content

Commit f8b7981

Browse files
committed
Update MergeTest.scala
1 parent dd2e1d1 commit f8b7981

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

src/test/scala/com/fasterxml/jackson/module/scala/deser/MergeTest.scala

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.fasterxml.jackson.module.scala.deser
22

33
import com.fasterxml.jackson.annotation.JsonMerge
44
import com.fasterxml.jackson.core.`type`.TypeReference
5-
import com.fasterxml.jackson.databind.ObjectMapper
6-
import com.fasterxml.jackson.module.scala.{DefaultScalaModule, ScalaObjectMapper}
5+
import com.fasterxml.jackson.databind.{ObjectMapper, ObjectReader}
6+
import com.fasterxml.jackson.module.scala.{DefaultScalaModule}
77
import org.junit.runner.RunWith
88
import org.scalatestplus.junit.JUnitRunner
99

@@ -20,13 +20,13 @@ class MergeTest extends DeserializerTest {
2020

2121
val module: DefaultScalaModule.type = DefaultScalaModule
2222

23-
def newScalaMapper: ObjectMapper with ScalaObjectMapper = {
24-
val mapper = new ObjectMapper with ScalaObjectMapper
23+
def newScalaMapper: ObjectMapper = {
24+
val mapper = new ObjectMapper
2525
mapper.registerModule(module)
2626
mapper
2727
}
2828

29-
def newMergeableScalaMapper: ObjectMapper with ScalaObjectMapper = {
29+
def newMergeableScalaMapper: ObjectMapper = {
3030
val mapper = newScalaMapper
3131
mapper.setDefaultMergeable(true)
3232
mapper
@@ -35,57 +35,65 @@ class MergeTest extends DeserializerTest {
3535
behavior of "The DefaultScalaModule when reading for updating"
3636

3737
it should "merge both lists" in {
38-
val initial = deserialize(classJson(firstListJson), classOf[ClassWithLists])
39-
val result = newMergeableScalaMapper.updateValue(initial, classJson(secondListJson))
38+
val typeReference = new TypeReference[ClassWithLists] {}
39+
val initial = deserialize(classJson(firstListJson), typeReference)
40+
val result = updateValue(newMergeableScalaMapper, initial, typeReference, classJson(secondListJson))
4041

4142
result shouldBe ClassWithLists(mergedList, mergedList)
4243
}
4344

4445
it should "merge only the annotated list" in {
45-
val initial = deserialize(classJson(firstListJson), classOf[ClassWithLists])
46-
val result = newScalaMapper.updateValue(initial, classJson(secondListJson))
46+
val typeReference = new TypeReference[ClassWithLists] {}
47+
val initial = deserialize(classJson(firstListJson), typeReference)
48+
val result = updateValue(newScalaMapper, initial, typeReference, classJson(secondListJson))
4749

4850
result shouldBe ClassWithLists(secondList, mergedList)
4951
}
5052

5153
it should "merge both string maps" in {
52-
val initial = deserialize(classJson(firstStringMapJson), classOf[ClassWithMaps[String]])
53-
val result = newMergeableScalaMapper.updateValue(initial, classJson(secondStringMapJson))
54+
val typeReference = new TypeReference[ClassWithMaps[String]] {}
55+
val initial = deserialize(classJson(firstStringMapJson), typeReference)
56+
val result = updateValue(newMergeableScalaMapper, initial, typeReference, classJson(secondStringMapJson))
5457

5558
result shouldBe ClassWithMaps(mergedStringMap, mergedStringMap)
5659
}
5760

5861
it should "merge only the annotated string map" in {
59-
val initial = deserialize(classJson(firstStringMapJson), classOf[ClassWithMaps[String]])
60-
val result = newScalaMapper.updateValue(initial, classJson(secondStringMapJson))
62+
val typeReference = new TypeReference[ClassWithMaps[String]] {}
63+
val initial = deserialize(classJson(firstStringMapJson), typeReference)
64+
val result = updateValue(newScalaMapper, initial, typeReference, classJson(secondStringMapJson))
6165

6266
result shouldBe ClassWithMaps(secondStringMap, mergedStringMap)
6367
}
6468

6569
it should "merge both pair maps" in {
66-
val initial = deserialize(classJson(firstPairMapJson), new TypeReference[ClassWithMaps[Pair]]{})
67-
val result = newMergeableScalaMapper.updateValue(initial, classJson(secondPairMapJson))
70+
val typeReference = new TypeReference[ClassWithMaps[Pair]] {}
71+
val initial = deserialize(classJson(firstPairMapJson), typeReference)
72+
val result = updateValue(newMergeableScalaMapper, initial, typeReference, classJson(secondPairMapJson))
6873

6974
result shouldBe ClassWithMaps(mergedPairMap, mergedPairMap)
7075
}
7176

7277
it should "merge only the annotated pair map" in {
73-
val initial = deserialize(classJson(firstPairMapJson), new TypeReference[ClassWithMaps[Pair]]{})
74-
val result = newScalaMapper.updateValue(initial, classJson(secondPairMapJson))
78+
val typeReference = new TypeReference[ClassWithMaps[Pair]]{}
79+
val initial = deserialize(classJson(firstPairMapJson), typeReference)
80+
val result = updateValue(newScalaMapper, initial, typeReference, classJson(secondPairMapJson))
7581

7682
result shouldBe ClassWithMaps(secondPairMap, mergedPairMap)
7783
}
7884

7985
it should "merge both mutable maps" in {
80-
val initial = deserialize(classJson(firstStringMapJson), classOf[ClassWithMutableMaps[String]])
81-
val result = newMergeableScalaMapper.updateValue(initial, classJson(secondStringMapJson))
86+
val typeReference = new TypeReference[ClassWithMutableMaps[String]]{}
87+
val initial = deserialize(classJson(firstStringMapJson), typeReference)
88+
val result = updateValue(newMergeableScalaMapper, initial, typeReference, classJson(secondStringMapJson))
8289

8390
result shouldBe ClassWithMutableMaps(mutable.Map() ++ mergedStringMap, mutable.Map() ++ mergedStringMap)
8491
}
8592

8693
it should "merge only the annotated mutable map" in {
87-
val initial = deserialize(classJson(firstStringMapJson), classOf[ClassWithMutableMaps[String]])
88-
val result = newScalaMapper.updateValue(initial, classJson(secondStringMapJson))
94+
val typeReference = new TypeReference[ClassWithMutableMaps[String]]{}
95+
val initial = deserialize(classJson(firstStringMapJson), typeReference)
96+
val result = updateValue(newScalaMapper, initial, typeReference, classJson(secondStringMapJson))
8997

9098
result shouldBe ClassWithMutableMaps(mutable.Map() ++ secondStringMap, mutable.Map() ++ mergedStringMap)
9199
}
@@ -106,4 +114,14 @@ class MergeTest extends DeserializerTest {
106114
val secondPairMapJson = """{"two":{"first":"22"},"three":{"second":"33"}}"""
107115
val secondPairMap = Map("two" -> Pair("22", null), "three" -> Pair(null, "33"))
108116
val mergedPairMap = Map("one" -> Pair("1", null), "two" -> Pair("22", "2"), "three" -> Pair("3", "33"))
117+
118+
private def updateValue[T](mapper: ObjectMapper, valueToUpdate: T,
119+
typeReference: TypeReference[T], src: String): T = {
120+
objectReaderFor(mapper, valueToUpdate, typeReference).readValue(src)
121+
}
122+
123+
private def objectReaderFor[T](mapper: ObjectMapper, valueToUpdate: T,
124+
typeReference: TypeReference[T]): ObjectReader = {
125+
mapper.readerForUpdating(valueToUpdate).forType(typeReference)
126+
}
109127
}

0 commit comments

Comments
 (0)