Skip to content

Commit 3b4a2fc

Browse files
add test cases from #467
1 parent 5f34ee7 commit 3b4a2fc

File tree

2 files changed

+161
-52
lines changed

2 files changed

+161
-52
lines changed

ktorm-core/src/main/kotlin/org/ktorm/entity/EntityImplementation.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,15 @@ internal class EntityImplementation(
243243
}
244244

245245
override fun hashCode(): Int {
246-
var result = 31 + entityClass.hashCode()
246+
var hash = entityClass.hashCode()
247247

248248
for ((name, value) in values) {
249249
if (value != null) {
250-
result = 31 * result + name.hashCode()
251-
result = 31 * result + value.hashCode()
250+
hash += name.hashCode() xor value.hashCode()
252251
}
253252
}
254253

255-
return result
254+
return hash
256255
}
257256

258257
override fun toString(): String {

ktorm-core/src/test/kotlin/org/ktorm/entity/EntityTest.kt

Lines changed: 158 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import java.io.ObjectOutputStream
1212
import java.time.LocalDate
1313
import java.util.*
1414
import kotlin.reflect.jvm.jvmErasure
15+
import kotlin.test.assertNotNull
16+
import kotlin.test.assertNull
1517

1618
/**
1719
* Created by vince on Dec 09, 2018.
@@ -45,54 +47,6 @@ class EntityTest : BaseTest() {
4547
assert(employee.job == "")
4648
}
4749

48-
@Test
49-
fun testEquals() {
50-
val e1 = Employee {
51-
id = 1
52-
name = "vince"
53-
}
54-
55-
val e2 = Employee {
56-
id = 1
57-
name = "vince"
58-
manager = null
59-
}
60-
61-
println(e1)
62-
println(e2)
63-
assert(e1 == e2)
64-
assert(e2 == e1)
65-
assert(e1 !== e2)
66-
assert(e1.hashCode() == e2.hashCode())
67-
}
68-
69-
@Test
70-
fun testEqualsForNestedEntities() {
71-
val p1 = Parent {
72-
child = Child {
73-
grandChild = GrandChild {
74-
id = 1
75-
}
76-
}
77-
}
78-
79-
val p2 = Parent {
80-
child = Child {
81-
grandChild = GrandChild {
82-
id = 1
83-
name = null
84-
}
85-
}
86-
}
87-
88-
println(p1)
89-
println(p2)
90-
assert(p1 == p2)
91-
assert(p2 == p1)
92-
assert(p1 !== p2)
93-
assert(p1.hashCode() == p2.hashCode())
94-
}
95-
9650
@Test
9751
fun testDefaultMethod() {
9852
for (method in Employee::class.java.methods) {
@@ -378,6 +332,80 @@ class EntityTest : BaseTest() {
378332
assert(p7.implementation.getColumnValue(Parents.id.binding!!) == 6)
379333
}
380334

335+
@Test
336+
fun testHasColumnValueAttached() {
337+
val sofiaDepartment = Department {
338+
name = "Sofia Office"
339+
location = LocationWrapper("Sofia")
340+
}
341+
342+
database.departments.add(sofiaDepartment)
343+
344+
val now = LocalDate.now()
345+
val employeeManager = Employee {
346+
name = "Simpson"
347+
job = "Manager"
348+
hireDate = now
349+
department = sofiaDepartment
350+
salary = 100
351+
}
352+
353+
database.employees.add(employeeManager)
354+
355+
val employee1 = Employee {
356+
name = "McDonald"
357+
job = "Engineer"
358+
hireDate = now
359+
department = sofiaDepartment
360+
salary = 100
361+
}
362+
363+
val e1 = with(database.employees) {
364+
add(employee1)
365+
find { it.id eq employee1.id }
366+
}
367+
368+
assertNotNull(e1)
369+
assert(!e1.implementation.hasColumnValue(Employees.managerId.binding!!))
370+
assertNull(e1.implementation.getColumnValue(Employees.managerId.binding!!))
371+
372+
val employee2 = Employee {
373+
name = "Smith"
374+
job = "Engineer"
375+
hireDate = now
376+
department = sofiaDepartment
377+
manager = null
378+
salary = 100
379+
}
380+
381+
val e2 = with(database.employees) {
382+
add(employee2)
383+
find { it.id eq employee2.id }
384+
}
385+
386+
assertNotNull(e2)
387+
assert(!e2.implementation.hasColumnValue(Employees.managerId.binding!!))
388+
assertNull(e2.implementation.getColumnValue(Employees.managerId.binding!!))
389+
390+
val employee3 = Employee {
391+
name = "Dennis"
392+
job = "Engineer"
393+
hireDate = now
394+
department = sofiaDepartment
395+
manager = employeeManager
396+
salary = 100
397+
}
398+
399+
val e3 = with(database.employees) {
400+
add(employee3)
401+
find { it.id eq employee3.id }
402+
}
403+
404+
assertNotNull(e3)
405+
assert(e3.implementation.hasColumnValue(Employees.managerId.binding!!))
406+
assertNotNull(e3.implementation.getColumnValue(Employees.managerId.binding!!))
407+
}
408+
381409
@Test
382410
fun testUpdatePrimaryKey() {
383411
try {
@@ -658,7 +686,13 @@ class EntityTest : BaseTest() {
658686
salary = 50
659687
}
660688

689+
println(employee1)
690+
println(employee2)
691+
println(employee1.hashCode())
661692
assert(employee1 == employee2)
693+
assert(employee2 == employee1)
694+
assert(employee1 !== employee2)
695+
assert(employee1.hashCode() == employee2.hashCode())
662696
}
663697

664698
@Test
@@ -671,6 +705,82 @@ class EntityTest : BaseTest() {
671705
name = "name"
672706
}
673707

708+
println(employee)
709+
println(department)
710+
println(employee.hashCode())
711+
println(department.hashCode())
674712
assert(employee != department)
675713
}
714+
715+
@Test
716+
fun testEqualsWithNullValues() {
717+
val e1 = Employee {
718+
id = 1
719+
name = "vince"
720+
}
721+
722+
val e2 = Employee {
723+
id = 1
724+
name = "vince"
725+
manager = null
726+
}
727+
728+
println(e1)
729+
println(e2)
730+
println(e1.hashCode())
731+
assert(e1 == e2)
732+
assert(e2 == e1)
733+
assert(e1 !== e2)
734+
assert(e1.hashCode() == e2.hashCode())
735+
}
736+
737+
@Test
738+
fun testEqualsForNestedEntities() {
739+
val p1 = Parent {
740+
child = Child {
741+
grandChild = GrandChild {
742+
id = 1
743+
}
744+
}
745+
}
746+
747+
val p2 = Parent {
748+
child = Child {
749+
grandChild = GrandChild {
750+
id = 1
751+
name = null
752+
}
753+
}
754+
}
755+
756+
println(p1)
757+
println(p2)
758+
println(p1.hashCode())
759+
assert(p1 == p2)
760+
assert(p2 == p1)
761+
assert(p1 !== p2)
762+
assert(p1.hashCode() == p2.hashCode())
763+
}
764+
765+
@Test
766+
fun testValueNullEquality() {
767+
val departmentTransient = Department {
768+
name = "Sofia Office"
769+
location = LocationWrapper("Sofia")
770+
mixedCase = null // explicitly initialized to null
771+
}
772+
773+
database.departments.add(departmentTransient)
774+
775+
val departmentAttached = database.departments.find { it.name eq "Sofia Office" }
776+
assertNotNull(departmentAttached)
777+
778+
println(departmentTransient)
779+
println(departmentAttached)
780+
println(departmentTransient.hashCode())
781+
assert(departmentTransient == departmentAttached)
782+
assert(departmentAttached == departmentTransient)
783+
assert(departmentTransient !== departmentAttached)
784+
assert(departmentTransient.hashCode() == departmentAttached.hashCode())
785+
}
676786
}

0 commit comments

Comments
 (0)