DEV Community

hiclab
hiclab

Posted on • Edited 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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay