Jenkinsfile using groovy classes not working when using lists - jenkins

I'm writing a simple pipeline using Jenkinsfile to test classes and interfaces in Groovy but often I stock in situations that Jenkins return errors that not make any sense. For example, the next code implements a simple builder pattern:
class Configuration implements Serializable {
ArrayList credentials
}
class ConfigurationBuilder implements Serializable {
Configuration configuration
ConfigurationBuilder() {
configuration = new Configuration()
}
ConfigurationBuilder withCredentials(ArrayList credentials) {
configuration.credentials = credentials
return this
}
Configuration build() {
return configuration
}
}
node("commons") {
ConfigurationBuilder builder = new ConfigurationBuilder()
Configuration conf = builder.withCredentials(['test']).build()
echo conf.credentials
}
The code above return this error when run-it on Jenkins:
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EchoStep.message expects class java.lang.String but received class java.util.ArrayList
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:492)
at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:409)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:329)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:272)
at org.jenkinsci.plugins.workflow.steps.StepDescriptor.newInstance(StepDescriptor.java:202)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:262)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:176)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
at sun.reflect.GeneratedMethodAccessor346.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:158)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:156)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:160)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:130)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
Caused: java.lang.IllegalArgumentException: Could not instantiate {message=[test]} for org.jenkinsci.plugins.workflow.steps.EchoStep
Any idea what's wrong?
I tried to implement it in the simplest way possible thinking that it was some limitation of Jenkins!

You passed an ArrayList to an echo step. This step accepts only values of type String.
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EchoStep.message expects class java.lang.String but received class java.util.ArrayList
You can fix this error by calling conf.credentials.toString()
node("commons") {
ConfigurationBuilder builder = new ConfigurationBuilder()
Configuration conf = builder.withCredentials(['test']).build()
echo conf.credentials.toString()
}

Related

one to many association is getting stack over flow error in groovy with grails.Please Check below classes, logs if there are any association errors

one to many association is getting stack over flow error in groovy with grails. Please Check below classes, error log to see if there are any association.
errors.
This is what I tried so far:
Earlier association is from B to C class, now we modified it A to B class after making this association changes. Changes looks good to me but while accessing b's from A class its throwing stackoverflow error getBs method from A.groovy. If I remove getBs method its giving null as B even though b's available for that contract in db. Also calling B.findAllByContract dynamic finder method its throwing could not resolve property 'a' even though its available at class level. Could you please guide me here
class A implements Serializable {
static stampable = true
static auditable = true
static hasMany = [
bs: B]
static mapping = {
bs joinTable: [column: "a_id"]
}
static mappedBy = [ bs: "a"]
def getBs() {
println "calling ..."
return bs
}
}
class B implements Serializable {
A a
static constraints = {
a nullable:true
}
static mapping = {
sort title: "asc"
a column: "a_id"
}
static transients = ['BValue']
}
error log:
2021-03-28 13:06:10,825 {yyyy-MM-dd HH:mm:ss} admin ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/app].[grailsDispatcherServlet] - Servlet.service() for servlet [grailsDispatcherServlet] in context with path [/app] threw exception [Handler processing failed; nested exception is java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError: null
at org.codehaus.groovy.runtime.InvokerHelper.format(InvokerHelper.java:583)
at org.codehaus.groovy.runtime.InvokerHelper.format(InvokerHelper.java:575)
at org.codehaus.groovy.runtime.InvokerHelper.toString(InvokerHelper.java:130)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.println(DefaultGroovyMethods.java:642)
at org.codehaus.groovy.runtime.dgm$504.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoMetaMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:251)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
at app.A.getBs(A.groovy:245)
at sun.reflect.GeneratedMethodAccessor878.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1852)
at groovy.lang.ExpandoMetaClass.getProperty(ExpandoMetaClass.java:1155)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3735)
at groovy.lang.ExpandoMetaClass.getProperty(ExpandoMetaClass.java:1167)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassGetPropertySite.getProperty(PogoMetaClassGetPropertySite.java:51)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
at app.A.getBs(A.groovy:246)
at sun.reflect.GeneratedMethodAccessor878.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1852)
at groovy.lang.ExpandoMetaClass.getProperty(ExpandoMetaClass.java:1155)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3735)
at groovy.lang.ExpandoMetaClass.getProperty(ExpandoMetaClass.java:1167)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassGetPropertySite.getProperty(PogoMetaClassGetPropertySite.java:51)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
at app.A.getBs(A.groovy:246)
at sun.reflect.GeneratedMethodAccessor878.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
Try updating the method getBs as follows:
def getBs() {
println "calling ..."
return this.bs
}
I think referring bs might be recursively calling getBs().

grails 3.2.6 fails to initialise test data "is not a domain class or GORM has not been initialized correctly"

i've created a simple grails app with latest v3.2.6. created a Authors and Book classes in grails-app/domain.
book class in grails-app/domain looks like this ( i have a books class also )
package org.softwood
//Author has many books logical model
class Author {
String name
Collection books
//static hasMany = [books:Book]
static constraints = {
books nullable:true
}
}
and book domain class for completeness
package org.softwood
class Book {
String title
Author author
//static belongsTo = [author:Author]
static constraints = {
author nullable:true
}
}
I started app - no errors and tried to use grails console to create a instance of Author and got an error like this
java.lang.IllegalStateException: Either class [org.softwood.Author] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.
so i edited BootStrap.groovy to try and create one at startup like this. Why does it not brought up Gorm correctly?
import org.softwood.*
class BootStrap {
def init = { servletContext ->
environments {
development{
createDevData()
}
test {}
production {}
}
}
def destroy = {
}
def createDevData() {
Author a = new Author(name:"will")
def res = a.save (flush:true, failOnExit:true)
assert res
assert Author.get(1).name == "will"
}
}
this errors when i start the app with same message, stack trace as follows
java.lang.IllegalStateException: Either class [org.softwood.Author] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.
at org.grails.datastore.gorm.GormEnhancer.stateException(GormEnhancer.groovy:387)
at org.grails.datastore.gorm.GormEnhancer.findInstanceApi(GormEnhancer.groovy:273)
at org.grails.datastore.gorm.GormEnhancer.findInstanceApi(GormEnhancer.groovy:270)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.currentGormInstanceApi(GormEntity.groovy:1326)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.save(GormEntity.groovy:151)
at org.grails.datastore.gorm.GormEntity$Trait$Helper$save.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at org.softwood.Author.save(Author.groovy)
at org.softwood.Author.save(Author.groovy)
at org.grails.datastore.gorm.GormEntity$save.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at test.BootStrap.createDevData(BootStrap.groovy:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:384)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:158)
at test.BootStrap$_closure1$_closure3$_closure4.doCall(BootStrap.groovy:10)
at test.BootStrap$_closure1$_closure3$_closure4.doCall(BootStrap.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:408)
at grails.util.Environment$EnvironmentBlockEvaluator.execute(Environment.java:529)
at grails.util.Environment.executeForEnvironment(Environment.java:510)
at grails.util.Environment.executeForCurrentEnvironment(Environment.java:485)
at org.grails.web.servlet.boostrap.DefaultGrailsBootstrapClass.callInit(DefaultGrailsBootstrapClass.java:62)
at org.grails.web.servlet.context.GrailsConfigUtils.executeGrailsBootstraps(GrailsConfigUtils.java:65)
at org.grails.plugins.web.servlet.context.BootStrapClassRunner.onStartup(BootStrapClassRunner.groovy:53)
at grails.boot.config.GrailsApplicationPostProcessor.onApplicationEvent(GrailsApplicationPostProcessor.groovy:256)
at grails.boot.config.GrailsApplicationPostProcessor.onApplicationEvent(GrailsApplicationPostProcessor.groovy)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:372)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at grails.boot.GrailsApp.run(GrailsApp.groovy:83)
at grails.boot.GrailsApp.run(GrailsApp.groovy:388)
at grails.boot.GrailsApp.run(GrailsApp.groovy:375)
at grails.boot.GrailsApp$run.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at test.Application.main(Application.groovy:8)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
try with this
package org.softwood
//Author has many books logical model
class Author {
String name
//Collection books
static hasMany = [books:Book]
static constraints = {
books nullable:true // you may not need this.
}
}

Can't delegate methods within closure implicitly even with DELEGATE_FIRST/DELEGATE_ONLY resolve strategy

I have a method which extends all domain classes:
static def bindGormStaticApiExtensions() {
Holders.grailsApplication.domainClasses.each { domainClass ->
domainClass.metaClass.static.withDataSource = { DataSource ds, Closure callable ->
HibernateDatastore datastore = Holders.applicationContext.getBean(HibernateDatastore)
SessionFactory sessionFactory = datastore.sessionFactory
Session session = null
Connection connection = null
try {
SessionBuilder sb = sessionFactory.withOptions()
connection = ds.getConnection()
session = sb.connection(connection).openSession()
callable.delegate = delegate
callable.resolveStrategy = Closure.DELEGATE_FIRST
return callable?.call(session)
} catch (Exception e) {
LOG.error("An error occured", e)
} finally {
session?.close()
if(connection && !connection.closed) {
connection.close()
}
}
}
}
}
However when I call this method on a domain class, I have to use delegate.findByXXX() otherwise groovy uses owner even though I've explicitly set the closure resolve strategy to DELEGATE_FIRST.
What am I doing wrong here?
You were right, the problem is in Groovy. Here's a simple test which demonstrates the problem:
assert MyClass.thisMethodDoesNotExist() == 'You called static method thisMethodDoesNotExist'
assert new MyClass().thisMethodDoesNotExist() == 'You called instance method thisMethodDoesNotExist'
new MyClass().with {
assert thisMethodDoesNotExistEither() == 'You called instance method thisMethodDoesNotExistEither'
}
MyClass.with {
// The following method call will throw a groovy.lang.MissingMethodException
assert thisMethodDoesNotExistEither() == 'You called static method thisMethodDoesNotExistEither'
}
class MyClass {
static Object $static_methodMissing(String name, Object args) {
"You called static method $name"
}
Object methodMissing(String name, Object args) {
"You called instance method $name"
}
}
The stackstrace looks like this:
groovy.lang.MissingMethodException: No signature of method: ConsoleScript10.thisMethodDoesNotExistEither() is applicable for argument types: () values: []
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:81)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:158)
at ConsoleScript10$_run_closure2.doCall(ConsoleScript10:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019)
at groovy.lang.Closure.call(Closure.java:426)
at groovy.lang.Closure.call(Closure.java:442)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.with(DefaultGroovyMethods.java:241)
at org.codehaus.groovy.runtime.dgm$757.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:151)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:91)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at ConsoleScript10.run(ConsoleScript10:8)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
at groovy.lang.GroovyShell.run(GroovyShell.java:524)
at groovy.lang.GroovyShell.run(GroovyShell.java:503)
at groovy.lang.GroovyShell.run(GroovyShell.java:170)
at groovy.lang.GroovyShell$run$1.call(Unknown Source)
at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy:980)
at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019)
at groovy.lang.Closure.call(Closure.java:426)
at groovy.lang.Closure.call(Closure.java:420)
at groovy.lang.Closure.run(Closure.java:507)
at java.lang.Thread.run(Thread.java:724)
I have my suspicions, I don't have a full understanding of Groovy's MOP implementation, so I don't know how to fix this issue. There's an open bug that reflects this problem.
Work-around
Good news! There's a work-around for this problem. And it's super simple: instead of using a class as the delegate for the closure, use an
instance which wraps the class; a proxy.
static def bindGormStaticApiExtensions() {
Holders.grailsApplication.domainClasses.each { domainClass ->
domainClass.metaClass.static.withDataSource = { DataSource ds, Closure callable ->
HibernateDatastore datastore = Holders.applicationContext.getBean(HibernateDatastore)
SessionFactory sessionFactory = datastore.sessionFactory
Session session = null
Connection connection = null
try {
SessionBuilder sb = sessionFactory.withOptions()
connection = ds.getConnection()
session = sb.connection(connection).openSession()
// Use a proxy as the delegate instead of the domain class.
callable.delegate = new ClassProxy(delegate)
callable?.call(session)
} catch (Exception e) {
LOG.error("An error occured", e)
} finally {
session?.close()
if(connection && !connection.closed) {
connection.close()
}
}
}
}
}
Here's the proxy:
// src/main/groovy/some/package/ClassProxy.groovy
#groovy.transform.TupleConstructor
/*
* Create an instance of my like so: new ClassProxy(SomeClass)
* and I will delegate method calls to the Class,
* essentially converting instance method calls to Class static
* method calls.
*/
class ClassProxy {
Class clazz
Object methodMissing(String name, Object args) {
clazz.invokeMethod(name, args)
}
}
methodMissing(), which is what dynamic finders depend on, works fine for instances, so the proxy takes advantage of this and simply invokes whatever method you call on it, on the real Class. In this case a domain class. I'm not sure if you need to change the default resolve strategy, but I don't think so. In my testing it was unnecessary.
...otherwise groovy uses owner even though I've explicitly set the
closure resolve strategy to DELEGATE_FIRST.
I don't think that is correct.
You have the following:
callable.delegate = delegate
That is inside of the closure that you are assigning to withDataSource.
When you use ExpandoMetaClass in that way, the delegate of the closure will be the thing that you invoked the method on. In your case, it will be the thing that you invoked withDataSource on.

ContextNotActiveException: WELD-001303: No active contexts for scope type org.apache.deltaspike.core.api.scope.GroupedConversationScoped

I want to use DeltaSpike for CDI-based tests. I use a lot of GroupedConversationScoped Beans for my application and when I'm trying to test them, I always run into this failure:
org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type org.apache.deltaspike.core.api.scope.GroupedConversationScoped
at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:708)
at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:90)
at org.jboss.weld.bean.ContextualInstance.getIfExists(ContextualInstance.java:63)
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:83)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125)
at com.pass.project.generated.context.MainDataContext$Proxy$_$$_WeldClientProxy.setTestAttr(Unknown Source)
at com.pass.project.test.DeltaSpikeTest.main(DeltaSpikeTest.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner$ContainerAwareMethodInvoker.invokeMethod(CdiTestRunner.java:340)
at org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner$ContainerAwareMethodInvoker.evaluate(CdiTestRunner.java:312)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner.runChild(CdiTestRunner.java:174)
at org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner.runChild(CdiTestRunner.java:75)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner$BeforeClassStatement.evaluate(CdiTestRunner.java:366)
at org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner$AfterClassStatement.evaluate(CdiTestRunner.java:392)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner.run(CdiTestRunner.java:141)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
I can't figure why it's not active. That's my test
#RunWith(CdiTestRunner.class)
public class DeltaSpikeTest {
#Inject
private DlgListBoRoleFormBean dlgListBoRoleFormBean;
#Inject
private MainDataContext mainDataContext;
#Test
public void main() {
TblTestAttrListBeanList tbl = (TblTestAttrListBeanList) dlgListBoRoleFormBean.getTblTestAttr();
List<ContactpersonValueImpl> li = new ArrayList<>();
mainDataContext.setTestAttr(li);
{
ContactpersonValueImpl person = new ContactpersonValueImpl();
person.setName("Harald");
li.add(person);
}
{
ContactpersonValueImpl person = new ContactpersonValueImpl();
person.setName("Wilhelm");
li.add(person);
}
tbl.update();
Assert.assertTrue(tbl.getList().size() == 2);
Assert.assertTrue(tbl.getList().get(0).getName().equals("Harald"));
System.out.println(tbl.toString());
}
}
Every help is highly appreciated!
You just need to activate a (test-)window, because grouped-conversations are based on the window-context to support different windows. #Inject WindowContext and use the method activateWindow. Provide any value you like.
See the example provided by OS890:
https://github.com/os890/ee6-ds-demo/blob/master/src/test/java/org/os890/demo/ee6/test/PageBeanTest.java
If you ask such questions on their mailing-list, you get answers petty quickly.
#John:
You don't need to list it via that annotation in this case.
It looks like you may have over looked the #TestControl() annotation, which is meant to start a context. Pass startScopes = GroupedConversationScoped.class into that to get the right output.
You can read more here: http://deltaspike.apache.org/documentation/test-control.html

how to mock ServletActionContext.getRequest() using Mockito we are using Struts2

I tried something like this but still throwing NullPointerException
#RunWith(MockitoJUnitRunner.class)
public class MyActionTest extends BaseActionTestCase {
#Mock private ServletActionContext context;
#Mock
private serviceclass serviceMock;
#InjectMocks
private MyAction action;
#Test
public void testSave() throws Exception {
when(serviceMock.getTaskCode()).thenReturn("tc");
action.save();
verify(serviceMock).saveObject(any(Task.class));
}
posting stacktrace:
java.lang.NullPointerException
at org.apache.struts2.ServletActionContext.getRequest(ServletActionContext.java:112)
at com.princess.ilo.webapp.action.BaseAction.getRequest(BaseAction.java:176)
at com.princess.ilo.webapp.action.BaseAction.getSession(BaseAction.java:198)
at com.princess.ilo.webapp.action.BaseAction.getUser(BaseAction.java:206)
at com.princess.ilo.webapp.action.TaskAction.save(TaskAction.java:116)
at com.princess.ilo.webapp.action.TaskActionTest.testSave(TaskActionTest.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
hm, I would say you still need to create the action object, like
#InjectMocks private MyAction action = new MyAction();
see http://docs.mockito.googlecode.com/hg/org/mockito/InjectMocks.html for some examples

Resources