Skip to content

Commit

Permalink
Fixed propertyMissing setter for GORM enhanced HibernateProxy. Added …
Browse files Browse the repository at this point in the history
…a test case for testing

setters on a proxy.
  • Loading branch information
lhotari committed Sep 15, 2009
1 parent 901e253 commit 7e69d0b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,30 +236,39 @@ Try using Grails' default cache provider: 'org.hibernate.cache.OSCacheProvider'"
}
}



public static void enhanceProxy ( HibernateProxy proxy ) {
// getter
proxy.metaClass.propertyMissing = { String name ->
if(delegate instanceof HibernateProxy) {
return GrailsHibernateUtil.unwrapProxy(delegate)."$name"
}
else {
throw new MissingPropertyException(name, delegate.class)
}
}

// setter
proxy.metaClass.propertyMissing = { String name, val ->
if(delegate instanceof HibernateProxy) {
def obj = GrailsHibernateUtil.unwrapProxy(delegate)
if(val != null) {
obj?."$name" = value
}
return obj."$name"
}
else {
throw new MissingPropertyException(name, delegate.class)
}
}
if(delegate instanceof HibernateProxy) {
GrailsHibernateUtil.unwrapProxy(delegate)."$name" = val
}
else {
throw new MissingPropertyException(name, delegate.class)
}
}

proxy.metaClass.methodMissing = { String name, args ->
if(delegate instanceof HibernateProxy) {
def obj = GrailsHibernateUtil.unwrapProxy(delegate)
return obj."$name"(*args)
}
else {
throw new MissingMethodException(name, delegate.class, args)
}

if(delegate instanceof HibernateProxy) {
def obj = GrailsHibernateUtil.unwrapProxy(delegate)
return obj."$name"(*args)
}
else {
throw new MissingPropertyException(name, delegate.class)
}

}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ class Article extends Content {
@Entity
class LazyProxiedAssociationsWithInheritancePerson {
static constraints = { name(nullable:true) }
String name
}
@Entity
class LazyProxiedAssociationsWithInheritanceAuthor extends LazyProxiedAssociationsWithInheritancePerson {
static constraints = { address(nullable:true) }
LazyProxiedAssociationsWithInheritanceAddress address
def houseNumber() {
Expand All @@ -63,6 +65,7 @@ class LazyProxiedAssociationsWithInheritanceAuthor extends LazyProxiedAssociatio
}
@Entity
class LazyProxiedAssociationsWithInheritanceAddress {
static constraints = { houseNumber(nullable:true) }
String houseNumber
}
@Entity
Expand Down Expand Up @@ -117,6 +120,68 @@ class LazyProxiedAssociationsWithInheritanceBook {
assertFalse "proxies should be instances of the actual class", Author.isInstance(proxy)
}

void testSettersOnProxiedObjects() {
def Author = ga.getDomainClass("LazyProxiedAssociationsWithInheritanceAuthor").clazz
def Address = ga.getDomainClass("LazyProxiedAssociationsWithInheritanceAddress").clazz
def Book = ga.getDomainClass("LazyProxiedAssociationsWithInheritanceBook").clazz


def addr = Address.newInstance(houseNumber:'52')
def auth = Author.newInstance(name:'Marc Palmer')
assert addr.save()
auth.address = addr
assert auth.save()

def book = Book.newInstance(title:"The Grails book of bugs")
book.author = auth
assertNotNull "book should have saved", book.save()

session.flush()
session.clear()

book = Book.get(1)

def proxy = PropertyUtils.getProperty(book, "author")
// test setter with non-null value
proxy.address.houseNumber = '123'
book.save()
session.flush()
session.clear()

book = Book.get(1)

assertEquals('123', book.author.address.houseNumber)

session.flush()
session.clear()

// test setting property to null
book = Book.get(1)
proxy = PropertyUtils.getProperty(book, "author")
proxy.address.houseNumber = null

book.save()
session.flush()
session.clear()

book = Book.get(1)

assertEquals(null, proxy.address.houseNumber)

// test setting proxy's property to null
// should delegate call to the closure defined in HibernatePluginSupport.enhanceProxy
// this broke with previous code
book = Book.get(1)
proxy = PropertyUtils.getProperty(book, "author")
proxy.address = null
book.save()
session.flush()
session.clear()

book = Book.get(1)

assertEquals(null, proxy.address)
}

void testLazyProxiesWithInheritance() {
def Article = ga.getDomainClass("Article").clazz
Expand Down

0 comments on commit 7e69d0b

Please sign in to comment.