-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapperSuite.scala
80 lines (54 loc) · 2.51 KB
/
MapperSuite.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import org.viablespark.mapper.*
import org.viablespark.mapper.config.*
import scala.beans.BeanProperty
import scala.collection.mutable
import scala.collection.mutable.{ArrayBuffer, ListBuffer}
class MapperSuite extends munit.FunSuite :
test("should map BeanA to BeanB correctly") {
val entityMapper = getEntityMapper
val a = BeanA("martin", "some description", 28, Phone("123452032", "Home"))
a.groups.append(Group("group1", "rank1"))
a.groups.append(Group("group2", "rank2"))
val b = BeanB("", "", 0, "")
var result = entityMapper.map(a, b)
assert(result != null)
assertEquals("martin", result.title)
assertEquals(28, result.years)
a.desc = "second time desc"
a.age = 31
result = entityMapper.map(a, b)
assertEquals("martin", result.title)
assertEquals(31, result.years)
assertEquals("second time desc", result.description)
assertEquals(b.phoneNumber, a.phone.number)
assertEquals(a.groups.size, b.entityGroups.size)
for (index <- a.groups.indices)
assertEquals(a.groups.lift(index).get.name, b.entityGroups.lift(index).get.groupName)
assertEquals(a.groups.lift(index).get.rank, b.entityGroups.lift(index).get.rank)
}
private def getEntityMapper: EntityMapper =
ConfigurationFactory.add(
SimpleXmlMapperConfig("/sample/beanA-to-beanB.xml", classOf[BeanA], classOf[BeanB]),
)
EntityMapperImpl(ConfigurationFactory)
case class BeanA(@BeanProperty var name: String,
@BeanProperty var desc: String,
@BeanProperty var age: Int,
@BeanProperty var phone: Phone):
@BeanProperty
var groups: mutable.Buffer[Group] = ListBuffer[Group]()
case class BeanB(@BeanProperty var title: String,
@BeanProperty var description: String,
@BeanProperty var years: Int,
@BeanProperty var phoneNumber: String):
@BeanProperty
var entityGroups: mutable.Buffer[GroupEntity] = ListBuffer[GroupEntity]()
case class Group(@BeanProperty var name: String = "", @BeanProperty var rank: String = ""):
def this() = this("", "")
case class GroupEntity(@BeanProperty var groupName: String = "", @BeanProperty var rank: String = ""):
def this() = this("", "")
case class Phone(@BeanProperty var number: String,
@BeanProperty var where: String)
class ReportResultPostProcessor extends PostProcessor :
override def process(source: AnyRef, target: AnyRef): Unit =
println(s"Post Process Invoked. Processing source: $source, target: $target")