Skip to content

Commit a58616b

Browse files
committed
update unit test
1 parent 5bd2f21 commit a58616b

File tree

1 file changed

+78
-13
lines changed

1 file changed

+78
-13
lines changed
Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,87 @@
11
package com.baeldung.cloningobject
22

3+
34
import org.junit.jupiter.api.Assertions.*
45
import org.junit.jupiter.api.Test
6+
7+
data class Address(val street: String, val city: String)
8+
9+
data class Person(val name: String, val address: Address): Cloneable {
10+
public override fun clone(): Person {
11+
return Person(name, address)
12+
}
13+
14+
constructor(person: Person) : this(person.name, person.address)
15+
}
16+
517
class CloningObjectUnitTest {
6-
data class Person(val name: String, val address: Address)
7-
data class Address(val street: String, val city: String)
18+
19+
private val address = Address("Jalan Kemasan", "Yogyakarta")
20+
private val person = Person("Hangga", address)
21+
22+
private fun serializePerson(person: Person): String {
23+
return "${person.name},${person.address.street},${person.address.city}"
24+
}
25+
26+
private fun deserializePerson(data: String): Person {
27+
val parts = data.split(",")
28+
val name = parts[0]
29+
val street = parts[1]
30+
val city = parts[2]
31+
return Person(name, Address(street, city))
32+
}
33+
34+
@Test
35+
fun `when using secondary constructor then proves that shallow copy`(){
36+
val clonedPerson = Person(person)
37+
38+
// original object and cloned object should have the same values
39+
assertEquals(person.name, clonedPerson.name)
40+
assertEquals(person.address, clonedPerson.address)
41+
42+
// But they should not refer to the same object
43+
assertNotSame(person, clonedPerson)
44+
assertSame(person.address, clonedPerson.address)
45+
}
46+
47+
@Test
48+
fun `when using copy then proves that shallow copy`() {
49+
val clonedPerson = person.copy()
50+
51+
// original object and cloned object should have the same values
52+
assertEquals(person.name, clonedPerson.name)
53+
assertEquals(person.address, clonedPerson.address)
54+
55+
// But they should not refer to the same object
56+
assertNotSame(person, clonedPerson)
57+
assertSame(person.address, clonedPerson.address)
58+
}
859

960
@Test
10-
fun `when copy an object then compare`(){
11-
val address = Address("Jalan Kemasan", "Yogyakarta")
12-
val firstPerson = Person("Hangga", address)
13-
val secondPerson = firstPerson.copy()
14-
15-
assertTrue(firstPerson == secondPerson)
16-
assertTrue(firstPerson.name == secondPerson.name)
17-
assertTrue(firstPerson.address === secondPerson.address)
18-
assertTrue(firstPerson.address.street == secondPerson.address.street)
19-
assertTrue(firstPerson.address.city == secondPerson.address.city)
20-
assertFalse(firstPerson === secondPerson)
61+
fun `with using clone then proves that shallow copy`(){
62+
val clonedPerson = person.clone()
63+
64+
// original object and cloned object should have the same values
65+
assertEquals(person.name, clonedPerson.name)
66+
assertEquals(person.address, clonedPerson.address)
67+
68+
// But they should not refer to the same object
69+
assertNotSame(person, clonedPerson)
70+
assertSame(person.address, clonedPerson.address)
71+
}
72+
73+
@Test
74+
fun `when using serialize then proves that deep copy`() {
75+
val serializedPerson = serializePerson(person)
76+
val clonedPerson = deserializePerson(serializedPerson)
77+
78+
// original object and cloned object should have the same values
79+
assertEquals(person.name, clonedPerson.name)
80+
assertEquals(person.address.street, clonedPerson.address.street)
81+
assertEquals(person.address.city, clonedPerson.address.city)
82+
83+
// But they should not refer to the same objects
84+
assertNotSame(person, clonedPerson)
85+
assertNotSame(person.address, clonedPerson.address)
2186
}
2287
}

0 commit comments

Comments
 (0)