|
Hi,
In an integration test, I am trying to cover a save() invocation that fails. My first impulse was to try the meta programming: registerMetaClass Invoice Invoice.metaClass.save = {Map args-> false delegate.errors.reject('Planted') } Invoice.metaClass.save = {-> false delegate.errors.reject('Planted') } However this does not work, the hibernate dynamic methods are not replaced by mine. I have followed the execution of the code through the meanders of groovy dynamic dispatching and the only methods being considered as CallSites are the hibernate provided closures. Is there anything i am forgetting here? Is there a better/cleaner way to achieve invoice.save() to return false? Thanks for any ideas. |
|
Hi,
I have found the following article: http://taapps-javalibs.blogspot.com/2009/02/grailscant-override-methodslike-save.html Basically this implies that the save() method is injected lazily. Has anyone solved this? The workaround suggested is not an option for me.On Sun, Oct 31, 2010 at 8:40 PM, Luis Muniz <[hidden email]> wrote: Hi, |
|
Yes, see http://grails.1312388.n4.nabble.com/Is-it-possible-to-customize-the-hibernate-plugin-autogenerated-CRUD-methods-on-my-grails-app-td2318957.html
Burt > Hi, > > I have found the following article: > > http://taapps-javalibs.blogspot.com/2009/02/grailscant-override-methodslike-save.html > > Basically this implies that the save() method is injected lazily. Has anyone > solved this? > > > > The workaround suggested is not an option for me.On Sun, Oct 31, 2010 at > 8:40 PM, Luis Muniz <[hidden email]> wrote: > > > Hi, > > > > In an integration test, I am trying to cover a save() invocation that > > fails. > > > > My first impulse was to try the meta programming: > > > > registerMetaClass Invoice > > Invoice.metaClass.save = {Map args-> > > false > > delegate.errors.reject('Planted') > > } > > Invoice.metaClass.save = {-> > > false > > delegate.errors.reject('Planted') > > } > > > > > > However this does not work, the hibernate dynamic methods are not replaced > > by mine. I have followed the execution of the code through the meanders of > > groovy dynamic dispatching and the only methods being considered as > > CallSites are the hibernate provided closures. > > > > Is there anything i am forgetting here? > > > > Is there a better/cleaner way to achieve invoice.save() to return false? > > > > Thanks for any ideas. > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
Thanks Burt,
Is the fact that I want to use this in an integration test (so not withing BootStrap) a complication? I can't think of a reason why. I have modified my code to use your trick: registerMetaClass Invoice Invoice.exists(-1) Invoice.metaClass.save = {-> delegate.errors.reject('Planted') false } Invoice.metaClass.save = {Map m-> delegate.errors.reject('Planted') false } And my override still doesn't get invoked On Sun, Oct 31, 2010 at 9:46 PM, Burt Beckwith <[hidden email]> wrote: Yes, see http://grails.1312388.n4.nabble.com/Is-it-possible-to-customize-the-hibernate-plugin-autogenerated-CRUD-methods-on-my-grails-app-td2318957.html |
|
I also added the third version of save for good measure but still no love
Invoice.metaClass.save = {boolean b-> delegate.errors.reject('Planted') false } On Sun, Oct 31, 2010 at 10:17 PM, Luis Muniz <[hidden email]> wrote: Thanks Burt, |
|
I have some news. (By the way I am using grails-1.3.4)
It seems that registerMetaClass Invoice causes problems. I created a dummy project to replicate the issue in an integration test. The test is the only thing that is executed. Here is the code for the test: void testSomething() { //registerMetaClass Invoice Invoice.exists(-1) Invoice.metaClass.save = {-> throw new RuntimeException("X")} Invoice.metaClass.save = {boolean b -> throw new RuntimeException("X")} Invoice.metaClass.save = {Map m -> throw new RuntimeException("X")} try { assertEquals 'X', shouldFail(RuntimeException) { new Invoice(ref: 'A').save() } } catch (StackOverflowError e) { StackTraceUtils.deepSanitize(e).printStackTrace() } } When I don't use registerMetaClass, the test succeeds. If I uncomment the registerMetaClass line, then I get a surprising exception: java.lang.StackOverflowError at groovy.lang.MetaObjectProtocol$invokeStaticMethod.call(Unknown Source) at org.codehaus.groovy.grails.plugins.orm.hibernate.HibernatePluginSupport$_enhanceSessionFactory_closure14.doCall(HibernatePluginSupport.groovy:382) at sun.reflect.GeneratedMethodAccessor189.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1058) at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1070) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886) at groovy.lang.Closure.call(Closure.java:276) at org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod.invoke(ClosureStaticMetaMethod.java:59) at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1355) at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1343) at groovy.lang.ExpandoMetaClass.invokeStaticMethod(ExpandoMetaClass.java:1082) at groovy.lang.MetaObjectProtocol$invokeStaticMethod.call(Unknown Source) at org.codehaus.groovy.grails.plugins.orm.hibernate.HibernatePluginSupport$_enhanceSessionFactory_closure14.doCall(HibernatePluginSupport.groovy:382) and on, and on So registering the class for cleanup in grails makes the whole thing go south. On Sun, Oct 31, 2010 at 10:21 PM, Luis Muniz <[hidden email]> wrote: I also added the third version of save for good measure but still no love |
|
The plot thickens.
This same test does actually works out OK in grails 1.2.4, WITH registerMetaClass I have decided to do this test (failure when save() returns false) in a unit test with mockDomain, but there seems to be something that changed in grails-1.3.4 that broke this. If this kind of thing should be allowed in integration tests (metaprogramming domain classes) maybe I should create a JIRA. On Mon, Nov 1, 2010 at 4:43 PM, Luis Muniz <[hidden email]> wrote: I have some news. (By the way I am using grails-1.3.4) |
|
Hi, Is this a known issue, or just an ignored issue ? ;-)
On Tue, Nov 2, 2010 at 11:43 AM, Luis Muniz <[hidden email]> wrote: The plot thickens. |
| Powered by Nabble | Edit this page |
