DEV Community

hiclab
hiclab

Posted on • Updated on

Invoke a Java static method from RCPTT

It makes no sense to spend lot of time writing complex procedures when we could reuse all the functions available in our application. We don’t want to reinvent the wheel; instead we want to focus on the main goal to test the application.
The invoke-static ECL command can be used for this purpose, to invoke any static method in a java class. Any class available in the plugins provided by the application under test can be used depending on argument types.
Here are some situations where we can save time reusing utility functions provided by Java SE or Apache Commons.

Apache Commons StringUtils

/*
Returns true if the specified value is numeric.
*/
proc isNumeric [val value] {
    invoke-static -pluginId "org.apache.commons.lang3" -className "org.apache.commons.lang3.StringUtils" 
        -methodName isNumeric -args [str $value]
}

/*
Reverses a string.
*/
proc reverse [val string] {
    invoke-static -pluginId "org.apache.commons.lang3" -className "org.apache.commons.lang3.StringUtils" 
        -methodName reverse -args $string
}

Some verifications

isNumeric "abc" | verify-false
isNumeric "123" | verify-true

reverse "abc" | equals "cba" | verify-true

Apache Commons FileUtils

// get the user directory path
invoke-static -pluginId "org.apache.commons.io" -className "org.apache.commons.io.FileUtils" 
        -methodName getUserDirectoryPath | log

Java System

// get the value of a environment variable
invoke-static -pluginId "org.eclipse.rcptt.ecl.core" -className "java.lang.System" 
    -methodName getenv -args "JAVA_HOME" | log

Oldest comments (0)