expandometaclass

Overriding event closure on Grails GORM domain class for unit testing

泪湿孤枕 提交于 2019-12-21 17:56:47
问题 I'm working on a fresh Grails project and recently noticed the default convention in the Spring Security Core generated User class now auto-encodes the password via a beforeInsert/Update event. That's a nice, clean, DRY way of doing the encode, and also makes it impossible to forget to do so. However, now when trying to write up some unit tests which make use of said User class, I find I either have to mock out the springSecurityService (due to the encode), or more preferably (and cleanly), I

shortcut for creating a Map from a List in groovy?

拥有回忆 提交于 2019-12-18 10:11:52
问题 I'd like some sorthand for this: Map rowToMap(row) { def rowMap = [:]; row.columns.each{ rowMap[it.name] = it.val } return rowMap; } given the way the GDK stuff is, I'd expect to be able to do something like: Map rowToMap(row) { row.columns.collectMap{ [it.name,it.val] } } but I haven't seen anything in the docs... am I missing something? or am I just way too lazy? 回答1: I've recently came across the need to do exactly that: converting a list into a map. This question was posted before Groovy

Can you use Groovy meta programming to override a private method on a Java class

两盒软妹~` 提交于 2019-12-17 21:31:57
问题 I'm trying to override a private method on a Java class using meta programming. The code looks something like this: // Java class public class MyClass{ private ClassOfSomeSort property1; private ClassOfSomeOtherSort property2; public void init(){ property1 = new ClassOfSomeSort(); property2 = new ClassOfSomeOtherSort(); doSomethingCrazyExpensive(); } private void doSomethingCrazyExpensive(){ System.out.println("I'm doing something crazy expensive"); } } // Groovy class public class

How to keep service's metaClass from being overridden

时间秒杀一切 提交于 2019-12-13 07:43:33
问题 I'm trying to mock a call to an outside service in an integration test, the service is used in a grails webflow. The service isn't in flow or conversation scope, but is added via dependency injection see here. I've managed to figure out a way to override a service via replacing it's metaClass using ExpandoMetaClass. The changes only work when the test is ran alone, if another test that uses that same service is ran before this test, the metaClass changes are gone. Part that overrides the

How to change a class's metaClass per test

余生长醉 提交于 2019-12-13 03:05:13
问题 I'm using the ExpandoMetaClass to make a service always return success in an integration test but I would like to have one test that actually fails. Example use of ExpandoMetaClass: static { ExpandoMetaClass someService = new ExpandoMetaClass(Object, false) someService.accessAnotherSystem = { return 'success' } someService.initialize() SomeService.metaClass = someService } Note: currently the service isn't defined for the controller but since it's a spring bean referencing the class named

Getting org.codehaus.groovy.control.MultipleCompilationErrorsException using gmaven plugin

回眸只為那壹抹淺笑 提交于 2019-12-12 12:22:23
问题 This is my sample program, while compiling using mvn it throws me the compilation error, i'm trying to add a Static Methods using ExpandoMetaClass - @Singleton class ThrowError { def parse () { println "Anish" } } ThrowError.metaClass.static.getMap = {m_var -> ThrowError.instance.parse(m_var) } i'm using gmaven plugin to compile the project, while issuing mvn compile .......... [ERROR] Failed to execute goal org.codehaus.gmaven:gmaven-plugin:1.2:generateStubs (default) on project

Overriding event closure on Grails GORM domain class for unit testing

断了今生、忘了曾经 提交于 2019-12-04 09:54:24
I'm working on a fresh Grails project and recently noticed the default convention in the Spring Security Core generated User class now auto-encodes the password via a beforeInsert/Update event. That's a nice, clean, DRY way of doing the encode, and also makes it impossible to forget to do so. However, now when trying to write up some unit tests which make use of said User class, I find I either have to mock out the springSecurityService (due to the encode), or more preferably (and cleanly), I'd just override the beforeInsert/Update closure with one that does nothing. Typically in Groovy one

shortcut for creating a Map from a List in groovy?

时光毁灭记忆、已成空白 提交于 2019-11-29 20:24:48
I'd like some sorthand for this: Map rowToMap(row) { def rowMap = [:]; row.columns.each{ rowMap[it.name] = it.val } return rowMap; } given the way the GDK stuff is, I'd expect to be able to do something like: Map rowToMap(row) { row.columns.collectMap{ [it.name,it.val] } } but I haven't seen anything in the docs... am I missing something? or am I just way too lazy? epidemian I've recently came across the need to do exactly that: converting a list into a map. This question was posted before Groovy version 1.7.9 came out, so the method collectEntries didn't exist yet. It works exactly as the

Copy Groovy class properties

别说谁变了你拦得住时间么 提交于 2019-11-27 09:14:04
I want to copy object properties to another object in a generic way (if a property exists on target object, I copy it from the source object). My code works fine using ExpandoMetaClass , but I don't like the solution. Are there any other ways to do this? class User { String name = 'Arturo' String city = 'Madrid' Integer age = 27 } class AdminUser { String name String city Integer age } def copyProperties(source, target) { target.properties.each { key, value -> if (source.metaClass.hasProperty(source, key) && key != 'class' && key != 'metaClass') { target.setProperty(key, source.metaClass

Copy Groovy class properties

和自甴很熟 提交于 2019-11-26 14:35:33
问题 I want to copy object properties to another object in a generic way (if a property exists on target object, I copy it from the source object). My code works fine using ExpandoMetaClass, but I don't like the solution. Are there any other ways to do this? class User { String name = 'Arturo' String city = 'Madrid' Integer age = 27 } class AdminUser { String name String city Integer age } def copyProperties(source, target) { target.properties.each { key, value -> if (source.metaClass.hasProperty