Handling program args with Groovy Eclipse plugin v2 - eclipse

I am wondering how to handle program arguments when you are running Groovy within Eclipse. It isn't as straight forward as it is from the command line and I am having trouble figure it out. Im using Eclipse 3.5. My run configuration has these arguments all on one line:
--classpath "${workspace_loc:/GroovyProject};${workspace_loc:/GroovyProject}"
--main groovy.ui.GroovyMain "C:\Temp\Workspace\GroovyProject\GroovyTest.groovy "
argtest1
argtest2
argtest3
The script I am using to try to make this work looks like this:
// GroovyTest.groovy
class GroovyTest {
static main(args) {
println "hello, world"
for (arg in this.args ) {
println "Argument:" + arg;
}
}
}
The error I get is:
hello, world
Caught: groovy.lang.MissingPropertyException: No such property: args
for class: GroovyTest at GroovyTest.main(GroovyTest.groovy:5)

You have az unnecessary this in the for (arg in this.args) line.
this.args means that you have an instance of the GroovyTest object and you refer to its args field. In this case args is a method parameter so you have to refer to it simply as args.

Related

Cannot convert the "HeaderInterceptor" value of type "HeaderInterceptor" to type "HeaderInterceptor"

Thanks for taking the time to read my question!
I'm having an issue \w Pester. When several tests are run with one Invoke-Pester cmd, I suddenly get PSInvalidCastException on blocks where I'm passing an inherited test class to a method that takes its parent, for example:
class TestInterceptor : HeaderInterceptor
{
[void] Intercept([ApiRequest]$Request)
{
.... # override \w test data
}
}
[void] AddClientHeaderInterceptor([HeaderInterceptor]$HeaderInterceptor)
{
....
}
It 'Adds new host interceptor to all clients' {
$interceptor = [TestInterceptor]::new()
FAILS>> $testService.AddClientHeaderInterceptor($interceptor)
}
ERROR:
Cannot convert argument "HeaderInterceptor", with value: "HeaderInterceptor", for "AddClientHeaderInterceptor" to type "HeaderInterceptor": "Cannot convert the "HeaderInterceptor" value of type "HeaderInterceptor" to type "HeaderInterceptor"
The tests pass when run on their own (Invoke-Pester $testFile) in the same PWSH session, but when run with Invoke-Pester -configuration $config (Default config) and ran against all test files it fails. I've thus far been unable to replicate by running each test manually, in that case everything passes.
The only change is that I've added a using module to a module that is widely imported.
I can take that same test class and manually cast it in a separate pwsh session.
using module ApiClientCore
class TestInterceptor : HeaderInterceptor
>> {
>> [void] Intercept([ApiRequest]$Request)
>> {
>> $Request.Headers['Foo'] = 'Bar'
>> }
>> }
>$t = [TestInterceptor]::new()
>[HeaderInterceptor]$t
TestInterceptor
Through debugging, I have a vague sense that it has something to do \w the AppDomain loading in different version of the same object and when attempting to cast between versions I get the error above. That piece is outside of my understanding atm however.
>$t = [TestInterceptor]::New()
>$t.GetType().Assembly.FullName
PowerShell Class Assembly, Version=1.0.0.69, Culture=neutral, PublicKeyToken=null
>[HeaderInterceptor]::new().getType().Assembly.FullName
PowerShell Class Assembly, Version=1.0.0.71, Culture=neutral, PublicKeyToken=null
>[HeaderInterceptor]$t
InvalidArgument: Cannot convert the "TestInterceptor" value of type "TestInterceptor" to type "HeaderInterceptor".

How do I get args passed to this scala object?

I'm trying to figure out how to pass args to this scala object:
I have this class written in this sbt project path: allaboutscala/src/main/scala/gzip_practice/gzipwriter
package gzip_practice
import java.io._
import java.util.zip._
/** Gzcat
*/
object gzcat extends App {
private val buf = new Array[Byte](1024)
try {
for (path <- args) {
try {
var in = new GZIPInputStream(new FileInputStream(path))
var n = in.read(buf)
while (n >= 0) {
System.out.write(buf, 0, n)
n = in.read(buf)
}
}
catch {
case _:FileNotFoundException =>
System.err.printf("File Not Found: %s", path)
case _:SecurityException =>
System.err.printf("Permission Denied: %s", path)
}
}
}
finally {
System.out.flush
}
}
This is an sbt project called allaboutscala. I am trying to run it with:
scala src/main/scala/gzip_practice/gzipwriter.scala "hi" but the command just hangs and I don't know why.
How am I supposed to run this object constructor with args?
You can use the scala command as a script runner.
Normally, it will wrap your "script" code in a main method.
But if you have an object with a main method, like your App, it will use that for the entry point.
However, it doesn't like package statements in the script.
If you comment out your package statement, you can compile and run with:
scala -nc somefile.scala myarg.gz
-nc means "no compile daemon"; otherwise, it will start a second process to compile scripts, so that subsequent compiles go faster; but it is a brittle workflow and I don't recommend it.
I confirmed that your code works.
Usually, folks use sbt or an IDE to compile and package in a jar to run with scala myapp.jar.
An object is a static instance of a class. You could construct it using:
object gzcat(args: String*) extends App {
...
}
args is bound as a val within the object gzcat.
Are you trying to run it with repl? I would suggest running it with sbt, then you can run sbt projects from project root directory with command line parameter as follows:
sbt "run file1.txt file2.txt"
The quotes are required. If you leave sbt shell open, then it running it will be much faster. Open shell in project root with
sbt
In the sbt shell:
run file1.txt file2.txt
Within the sbt shell, no quotes.

Inline::java STUDY configuration

The Inline docs aren't too helpful in learning how to use the STUDY config,
can anyone clarify the syntax involved in calling a simple void method that prints a method, say, Hello() ?
Also, in terms of the external java file, is there a specific directory i need to put it in, or does it go in the same directory of the perl script?
Let's start with the file /home/foo/java_src/Hello.java, which contains:
public class Hello {
public Hello() {}
public void instance_hello() { System.out.println("hello world"); }
public static void static_hello() { System.out.println("HELLO WORLD"); }
}
Tackling your second question first, the first argument after use Inline Java ... can be a filename, and so you can put your source file anywhere and refer to it by its file name in your perl code:
use Inline Java => '/home/foo/java_src/Hello.java';
$obj = Hello->new();
$obj->instance_hello(); # "hello world"
Hello->static_hello(); # "HELLO WORLD"
Note that you don't need STUDY so far. The Hello class is defined in source code that is read directly by the Inline::Java module, so the module automatically creates and populates the Hello namespace in Perl.
STUDY is for classes that aren't parsed directly by Inline::Java. So let's say instead that our Hello class has been compiled into a jar file called /home/foo/jars/hello.jar. Now to use the Hello class you would need to (1) include hello.jar in your CLASSPATH and (2) use STUDY to tell Inline::Java to create the Hello namespace:
use Inline Java => 'STUDY',
CLASSPATH => '/home/foo/jars/hello.jar',
STUDY => ['Hello'];
$obj = Hello->new;
Hello->static_hello; # "HELLO WORLD"
$obj->instance_hello; # "hello world"
We include the first argument STUDY to signal to the Inline::Java that we're not passing any source code directly to the module. We could have also passed valid source code or a valid source code filename.
use Inline Java => 'public class Nothing() { }',
CLASSPATH => '/home/foo/jars/hello.jar',
STUDY => ['Hello'];

SBT Command that Calls Another Command and an InputTask

I am writing an SBT Command that is supposed to call another command (eclipse from the Eclipse SBT Plugin) and another InputTask.
How can one achieve this?
Assuming that you want to create a "release" command and it needs to call another task named "pack", you can add the following code to build.sbt:
commands += Command.command("release")((state:State) => {
Project.evaluateTask(pack, state)
println("release called")
state
})
Updated:
In addition, if you have to create the "release" command and it requires calling another command named "init_compile", then the following sample code can be used:
commands += Command.command("init_compile")((state:State) => {
println("init_compile called.")
state
})
commands += Command.command("release")((state:State) => {
val newState = Command.process("init_compile",state)
println("release called.")
newState
})

Scala problem with jMock expectations and returning a value from mock

Solved. IntelliJ didn't highlight the fact that my imports were incomplete.
Hi,
I have a simple Scala program that I'm trying to develop using jMock. Setting basic expectations works nicely but for some reason Scala does not understand my attempt to return a value from a mock object. My maven build spews out the following error
TestLocalCollector.scala:45: error: not found: value returnValue
one (nodeCtx).getParameter("FilenameRegex"); will( returnValue(regex))
^
And the respective code snippets are
#Before def setUp() : Unit = { nodeCtx = context.mock(classOf[NodeContext]) }
...
// the value to be returned
val regex = ".*\\.data"
...
// setting the expectations
one (nodeCtx).getParameter("FilenameRegex"); will( returnValue(regex))
To me it sounds that Scala is expecting that the static jMock method returnValue would be a val? What am I missing here?
Are you sure about the ';'?
one (nodeCtx).getParameter("FilenameRegex") will( returnValue(regex))
might work better.
In this example you see a line like:
expect {
one(blogger).todayPosts will returnValue(List(Post("...")))
}
with the following comment:
Specify what the return value should be in the same expression by defining "will" as Scala infix operator.
In the Java equivalent we would have to make a separate method call (which our favorite IDE may insist on putting on the next line!)
one(blogger).todayPosts; will(returnValue(List(Post("..."))))
^
|
-- semicolon only in the *Java* version
The OP explains it himself:
the returnValue static method was not visible, thus the errors.
And the will method just records an action on the latest mock operation, that's why it can be on the next line or after the semicolon :)
import org.jmock.Expectations
import org.jmock.Expectations._
...
context.checking(
new Expectations {
{ oneOf (nodeCtx).getParameter("FilenameRegex") will( returnValue(".*\\.data") ) }
}
)