Skip to content

Commit 0f6c78c

Browse files
committed
#1294 Added Tests for dirty checking and proxy
to demonstrate the need to override equals method for correctly marking association as dirty or not.
1 parent bc5d7d7 commit 0f6c78c

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

grails-datastore-gorm-test/src/test/groovy/org/grails/datastore/gorm/DirtyCheckingSpec.groovy

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.grails.datastore.gorm
22

3+
import grails.gorm.annotation.Entity
34
import grails.gorm.tests.GormDatastoreSpec
4-
55
import grails.gorm.tests.Person
66
import org.grails.datastore.mapping.dirty.checking.DirtyCheckable
77

@@ -10,6 +10,17 @@ import org.grails.datastore.mapping.dirty.checking.DirtyCheckable
1010
*/
1111
class DirtyCheckingSpec extends GormDatastoreSpec {
1212

13+
def proxyHandler
14+
15+
@Override
16+
List getDomainClasses() {
17+
[Person, TestBook, TestAuthor]
18+
}
19+
20+
def setup() {
21+
proxyHandler = session.getMappingContext().proxyHandler
22+
}
23+
1324
void "Test that dirty checking methods work when changing entities"() {
1425
when:"A new instance is created"
1526
def p = new Person(firstName: "Homer", lastName: "Simpson")
@@ -53,4 +64,72 @@ class DirtyCheckingSpec extends GormDatastoreSpec {
5364

5465

5566
}
67+
68+
void "test relationships not marked dirty when proxies are used"() {
69+
70+
given:
71+
Long bookId = new TestBook(title: 'Martin Fierro', author: new TestAuthor(name: 'Jose Hernandez'))
72+
.save(flush: true)
73+
.id
74+
session.flush()
75+
session.clear()
76+
77+
when:
78+
TestBook book = TestBook.get(bookId)
79+
book.author = book.author
80+
81+
then:
82+
proxyHandler.isProxy(book.author)
83+
!book.isDirty('author')
84+
!book.isDirty()
85+
86+
cleanup:
87+
TestBook.deleteAll()
88+
TestAuthor.deleteAll()
89+
}
90+
91+
void "test relationships not marked dirty when domain objects are used"() {
92+
93+
given:
94+
Long bookId = new TestBook(title: 'Martin Fierro', author: new TestAuthor(name: 'Jose Hernandez'))
95+
.save(flush: true, failOnError: true)
96+
.id
97+
session.flush()
98+
session.clear()
99+
100+
when:
101+
TestBook book = TestBook.get(bookId)
102+
book.author = TestAuthor.get(book.authorId)
103+
104+
then:
105+
!proxyHandler.isProxy(book.author)
106+
!book.isDirty('author')
107+
!book.isDirty()
108+
109+
cleanup:
110+
TestBook.deleteAll()
111+
TestAuthor.deleteAll()
112+
}
113+
}
114+
115+
@Entity
116+
class TestAuthor {
117+
String name
118+
long version
119+
120+
@Override
121+
boolean equals(o) {
122+
if (!(o instanceof TestAuthor)) return false
123+
if (this.is(o)) return true
124+
TestAuthor that = (TestAuthor) o
125+
if (id !=null && that.id !=null) return id == that.id
126+
return false
127+
}
128+
}
129+
130+
@Entity
131+
class TestBook {
132+
String title
133+
long version
134+
TestAuthor author
56135
}

0 commit comments

Comments
 (0)