Adding a method from an external groovy script with GroovyShell() - groovy

I have a groovy script (source.groovy) that needs to call a method from another groovy script (external.groovy). The problem is external.groovy imports a library that does not exists so I get an error. Here is an example:
Source.groovy:
new GroovyShell().parse( new File( 'external.groovy' ) ).with {
method()
}
Here is external.groovy:
import com.foo.doesnotexsist
def method() {println "test"}
When I run Source.groovy I get an error because com.foo.doesnotexsist does not exist. I don't care that it does not exists because it does not effect the method() function. Is there a way I can call the method() function?

Maybe it is not the way we want to achieve that, but there is simple solution to remove unwanted imports:
def text = new File( 'external.groovy' ).findAll{!(it =~ /^\s*import/)}.join('\n')
new GroovyShell().parse( text ).with{method()}

Related

Groovy : java introspection is bypassing metaprogramming. How to fix that?

I'm trying to redefine a method of a class for test purpose.
For doing this I'm using the meta-programming feature of Groovy.
Unfortunately this method is called by introspection by some code out of my control.
In this situation the real method is called instead of the overridden version.
Here is a groovy script example demonstrating this:
class Dummy { void sayHello() {println "Hi"} }
def dummy = new Dummy()
dummy.sayHello()
dummy.metaClass.sayHello = {println "Hello world"}
dummy.sayHello()
def method = dummy.class.getDeclaredMethod("sayHello")
method.invoke(dummy)
This code output is:
Hi
Hello world
Hi
But I would like the last line to be 'Hello World'
I have tried to override the Method.invoke method, but it was a mess.
Is there a preferred way to tell to Groovy that it should also intercept call to Method.invoke when a call to Dummy#sayHello is detected ?

Parsing classes with GroovyShell

I have a groovy script that need to run a method inside a class inside an external groovy script.
I know how to run a method within an external groovy script:
new GroovyShell().parse( new File( 'foo.groovy' ) ).with {
method()
}
But what if the method is inside a class? I tried this but it gave me an error.
new GroovyShell().parse( new File( 'foo.groovy' ) ).with {
theclass.method()
}
You can use Java reflection to create new instance of a Class that is located in another script:
File sourceFile = new File("D:\\anoutherScript.groovy")
//here you have to update your classloader with external script
getClass().getClassLoader().addURL(sourceFile.toURI().toURL())
GroovyObject obj = Class.forName("ClassInAnotherObject").newInstance()
obj.doSth()
Script in your external file would be like that:
class ClassInAnotherObject{
def doSth(){
}
}
but there could be more classes in script file, also some more instructions and method call. Just like normal groovy script.

SoapUI write testsuite name to file

I want to print my test suite into file
def fileDir = new File("C:\\SoapUIResults");
if(!fileDir .exists()) {
fileDir .mkdirs();
} def myFile= new File(fileDir , "myFile.txt"); //If the file does not already exist, we want to create it, otherwise we want to append
if(!myFile.exists()) { myFile.createNewFile(); }
myFile.append(testRunner.testSuite.name +'\n' ); //Test suite Name
Your question is not clear for me, the only thing that I see wrong in your code is that you are missing testCase to access testSuite properties from testRunner, to do so you must use testRunner.testCase.testSuite.name instead of testRunner.testSuite.name.
If you are looking for a way to save all testSuite results on a file take a look on this answer
Hope this helps,

How can I use relative paths to external response files for soapUI MockService

What I've Done
I am using soapUI (3.6.1 Free version) mock services to serve up specific data to 2 client applications I am testing. With some simple Groovy script I've set up some mock operations to fetch responses from specific files based on the requests made by the client applications.
The static contents of the mock response is:
${responsefile}
The groovy in the operation dispatch scripting pane is:
def req = new XmlSlurper().parseText(mockRequest.requestContent)
if (req =~ "CategoryA")
{
context.responsefile = new File("C:/soapProject/Test_Files/ID_List_CategoryA.xml").text
}
else
{
context.responsefile = new File("C:/soapProject/Test_Files/ID_List_CategoryB.xml").text
}
In this example, when the client application issues a request to the mock service that contains the string CategoryA, the response returned by soapUI is the contents of file ID_List_CategoryA.xml
What I'm Trying To Achieve
This all works fine with the absolute paths in the groovy. Now I want to pull the whole collection of soapUI project file and external files into a package for easy re-deployment. From my reading about soapUI I hoped this would be as easy as setting the project Resource Root value to ${projectDir} and changing my paths to:
def req = new XmlSlurper().parseText(mockRequest.requestContent)
if (req =~ "CategoryA")
{
context.responsefile = new File("Test_Files/ID_List_CategoryA.xml").text
}
else
{
context.responsefile = new File("Test_Files/ID_List_CategoryB.xml").text
}
... keeping in mind that the soapUI project xml file resides in C:/soapProject/
What I've Tried So Far
So, that doesn't work. I've tried variations of relative paths:
./Test_Files/ID_List_CategoryA.xml
/Test_Files/ID_List_CategoryA.xml
Test_Files/ID_List_CategoryA.xml
One post indicated that soapUI might consider the project files parent directory as the root for the purposes of the relative path, so tried the following variations too:
./soapProject/Test_Files/ID_List_CategoryA.xml
/soapProject/Test_Files/ID_List_CategoryA.xml
soapProject/Test_Files/ID_List_CategoryA.xml
When none of that worked I tried making use of the ${projectDir} property in the groovy script, but all such attempts failed with a "No such property: mockService for class: Script[n]" error. Admittefly, I was really fumbling around when trying to do that.
I tried using information from this post and others: How do I make soapUI attachment paths relative?
... without any luck. Replacing "test" with "mock," (among other changes), in the solution code from that post resulted in more property errors, e.g.
testFile = new File(mockRunner.project.getPath())
.. led to...
No such property: mockRunner for class: Script3
What I Think I Need
The posts I've found related to this issue all focus on soapUI TestSuites. I really need a solution that is MockService centric or at least sheds some light on how it can be handled differently for MockServices as opposed to TestSuites.
Any help is greatly appreciated. Thanks. Mark.
The Solution - Provided by GargantuChet
The following includes the changes suggested by GargantuChet to solve the problem of trying to access the ${projectDir} property and enable the use of relative paths by defining a new projectDir object within the scope of the groovy script:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectDir = groovyUtils.projectPath
def req = new XmlSlurper().parseText(mockRequest.requestContent)
if (req =~ "CategoryA")
{
context.responsefile = new File(projectDir, "Test_Files/ID_List_CategoryA.xml").text
}
else
{
context.responsefile = new File(projectDir, "Test_Files/ID_List_CategoryB.xml").text
}
I'm not familiar with Groovy, but I assume the File is a normal java.io.File instance.
Relative paths are interpreted as being relative to the application's current directory. Try something like the following to verify:
def defaultPathBase = new File( "." ).getCanonicalPath()
println "Current dir:" + defaultPathBase
If this is the case here, then you may want to use the new File(String parent, String child) constructor, passing your resource directory as the first argument and the relative path as the second.
For example:
// hardcoded for demonstration purposes
def pathbase = "/Users/chet"
def content = new File(pathbase, "Desktop/sample.txt").text
println content
Here's the result of executing the script:
Chets-MacBook-Pro:Desktop chet$ groovy sample.groovy
This is a sample text file.
It will be displayed by a Groovy script.
Chets-MacBook-Pro:Desktop chet$ groovy sample.groovy
This is a sample text file.
It will be displayed by a Groovy script.
Chets-MacBook-Pro:Desktop chet$
You could have also done the following to get the value of projectDir:
def projectDir = context.expand('${projectDir}');

External Content with Groovy BuilderSupport

I've built a custom builder in Groovy by extending BuilderSupport. It works well when configured like nearly every builder code sample out there:
def builder = new MyBuilder()
builder.foo {
"Some Entry" (property1:value1, property2: value2)
}
This, of course, works perfectly. The problem is that I don't want the information I'm building to be in the code. I want to have this information in a file somewhere that is read in and built into objects by the builder. I cannot figure out how to do this.
I can't even make this work by moving the simple entry around in the code.
This works:
def textClosure = { "Some Entry" (property1:value1, property2: value2) }
builder.foo(textClosure)
because textClosure is a closure.
If I do this:
def text = '"Some Entry" (property1:value1, property2: value2)'
def textClosure = { text }
builder.foo(textClosure)
the builder only gets called for the "foo" node. I've tried many variants of this, including passing the text block directly into the builder without wrapping it in a closure. They all yield the same result.
Is there some way I take a piece of arbitrary text and pass it into my builder so that it will be able to correctly parse and build it?
Your problem is that a String is not Groovy code. The way ConfigSlurper handles this is to compile the text into an instance of Script using GroovyClassLoader#parseClass. e.g.,
// create a Binding subclass that delegates to the builder
class MyBinding extends Binding {
def builder
Object getVariable(String name) {
return { Object... args -> builder.invokeMethod(name,args) }
}
}
// parse the script and run it against the builder
new File("foo.groovy").withInputStream { input ->
Script s = new GroovyClassLoader().parseClass(input).newInstance()
s.binding = new MyBinding(builder:builder)
s.run()
}
The subclass of Binding simply returns a closure for all variables that delegates the call to the builder. So assuming foo.groovy contains:
foo {
"Some Entry" (property1:value1, property2: value2)
}
It would be equivalent to your code above.
I think the problem you described is better solved with a slurper or parser.
See:
http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlSlurper
http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlParser
for XML based examples.
In your case. Given the XML file:
<foo>
<entry name='Some Entry' property1="value1" property2="value2"/>
</foo>
You could slurp it with:
def text = new File("test.xml").text
def foo = new XmlSlurper().parseText(text)
def allEntries = foo.entry
allEntries.each {
println it.#name
println it.#property1
println it.#property2
}
Originally, I wanted to be able to specify
"Some Entry" (property1:value1, property2: value2)
in an external file. I'm specifically trying to avoid XML and XML-like syntax to make these files easier for regular users to create and modify. My current solution uses ConfigSlurper and the file now looks like:
"Some Entry"
{
property1 = value1
property2 = value2
}
ConfigSlurper gives me a map like this:
["Some Entry":[property1:value1,property2:value2]]
It's pretty simple to use these values to create my objects, especially since I can just pass the property/value map into the constructor.

Resources