Implementing the function class
If you have a function that is written for IBM® DevOps Test Integrations and APIs (Test Integrations and APIs) 8.5, you can convert it to work in Test Integrations and APIs 8.6 or later.
For more information about converting the function, see Converting existing function classes with Eclipse.
Java™ class extending from function
Under the src
folder of the plug-in
project, create a Java class
that extends from
com.ghc.ghTester.expressions.Function
. For
example, com.samples.functions.FormatDate
.
Implement the following methods:
- The default public constructor
create(int, Vector)
evaluate(Object)
Default constructor
Create a default public constructor with an empty body. You can add other initialization as required, but it does not have to do anything, by default. The default constructor must be public because of the way the functions are created.
public FormatDate() {
}
Override create (int size, Vector) parameters
The create(int, Vector)
method is a
factory method that creates an instance of the particular function
with the parameters provided. The vector of parameters needs to be
treated as functions themselves and are likely to be processec at
run time.
public Function create(int size, Vector params) {
Function outputFormat = null;
if (size == 3) {
outputFormat = (Function) params.get(2);
}
return new FormatDate((Function) params.get(0), (Function) params
.get(1), outputFormat);
}
The three-argument constructor that is used in the example can be found in the source that is provided in the examples folder of the Test Integrations and APIs installation folder.
Override evaluate(Object) data
The evaluate(Object)
method performs
the actual work of the function. Test Integrations and APIs passes the context of the current evaluation to your function.
You can use this method to obtain the result from embedded functions
and then return your functions own result.
public Object evaluate(Object data) {
String date = m_fDate.evaluateAsString(data);
String inputFormat = m_fInputFormat.evaluateAsString(data);
String outputFormat = "yyyy-MM-dd"; // Default format
if (m_fOutputFormat != null) {
outputFormat = m_fOutputFormat.evaluateAsString(data);
}
//...
if (EvalUtils.isString(date)) {
date = EvalUtils.getString(date);
}
if (EvalUtils.isString(inputFormat)) {
inputFormat = EvalUtils.getString(inputFormat);
}
if (EvalUtils.isString(outputFormat)) {
outputFormat = EvalUtils.getString(outputFormat);
}
SimpleDateFormat inputFormatter = new
SimpleDateFormat(inputFormat);
String formattedDate = "";
try {
Date d = inputFormatter.parse(date);
SimpleDateFormat outputFormatter =new
SimpleDateFormat(outputFormat);
formattedDate = outputFormatter.format(d);
} catch (ParseException ex) {
// ...
}
return "\"" + formattedDate + "\"";
}
EvalUtils.getString(Object
string)
.